re PR bootstrap/81470 (Bootstrap comparison failures in gcc/ada)
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "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 "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
97
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
103
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
107
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
111
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
114
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
127
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
135
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
140
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
144
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
150
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
157
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static unsigned macinfo_label_base = 1;
168 static GTY(()) section *debug_line_section;
169 static GTY(()) section *debug_skeleton_line_section;
170 static GTY(()) section *debug_loc_section;
171 static GTY(()) section *debug_pubnames_section;
172 static GTY(()) section *debug_pubtypes_section;
173 static GTY(()) section *debug_str_section;
174 static GTY(()) section *debug_line_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
179
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 40
182
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
187
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
190
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
194
195 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
196 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
197 #endif
198
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
202
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
210
211
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
216
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
218
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
225 };
226
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
228 {
229 typedef const char *compare_type;
230
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
233 };
234
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
236
237 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
238
239 /* With split_debug_info, both the comp_dir and dwo_name go in the
240 main object file, rather than the dwo, similar to the force_direct
241 parameter elsewhere but with additional complications:
242
243 1) The string is needed in both the main object file and the dwo.
244 That is, the comp_dir and dwo_name will appear in both places.
245
246 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
247 DW_FORM_line_strp or DW_FORM_GNU_str_index.
248
249 3) GCC chooses the form to use late, depending on the size and
250 reference count.
251
252 Rather than forcing the all debug string handling functions and
253 callers to deal with these complications, simply use a separate,
254 special-cased string table for any attribute that should go in the
255 main object file. This limits the complexity to just the places
256 that need it. */
257
258 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
259
260 static GTY(()) int dw2_string_counter;
261
262 /* True if the compilation unit places functions in more than one section. */
263 static GTY(()) bool have_multiple_function_sections = false;
264
265 /* Whether the default text and cold text sections have been used at all. */
266 static GTY(()) bool text_section_used = false;
267 static GTY(()) bool cold_text_section_used = false;
268
269 /* The default cold text section. */
270 static GTY(()) section *cold_text_section;
271
272 /* The DIE for C++14 'auto' in a function return type. */
273 static GTY(()) dw_die_ref auto_die;
274
275 /* The DIE for C++14 'decltype(auto)' in a function return type. */
276 static GTY(()) dw_die_ref decltype_auto_die;
277
278 /* Forward declarations for functions defined in this file. */
279
280 static void output_call_frame_info (int);
281 static void dwarf2out_note_section_used (void);
282
283 /* Personality decl of current unit. Used only when assembler does not support
284 personality CFI. */
285 static GTY(()) rtx current_unit_personality;
286
287 /* Whether an eh_frame section is required. */
288 static GTY(()) bool do_eh_frame = false;
289
290 /* .debug_rnglists next index. */
291 static unsigned int rnglist_idx;
292
293 /* Data and reference forms for relocatable data. */
294 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
295 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
296
297 #ifndef DEBUG_FRAME_SECTION
298 #define DEBUG_FRAME_SECTION ".debug_frame"
299 #endif
300
301 #ifndef FUNC_BEGIN_LABEL
302 #define FUNC_BEGIN_LABEL "LFB"
303 #endif
304
305 #ifndef FUNC_END_LABEL
306 #define FUNC_END_LABEL "LFE"
307 #endif
308
309 #ifndef PROLOGUE_END_LABEL
310 #define PROLOGUE_END_LABEL "LPE"
311 #endif
312
313 #ifndef EPILOGUE_BEGIN_LABEL
314 #define EPILOGUE_BEGIN_LABEL "LEB"
315 #endif
316
317 #ifndef FRAME_BEGIN_LABEL
318 #define FRAME_BEGIN_LABEL "Lframe"
319 #endif
320 #define CIE_AFTER_SIZE_LABEL "LSCIE"
321 #define CIE_END_LABEL "LECIE"
322 #define FDE_LABEL "LSFDE"
323 #define FDE_AFTER_SIZE_LABEL "LASFDE"
324 #define FDE_END_LABEL "LEFDE"
325 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
326 #define LINE_NUMBER_END_LABEL "LELT"
327 #define LN_PROLOG_AS_LABEL "LASLTP"
328 #define LN_PROLOG_END_LABEL "LELTP"
329 #define DIE_LABEL_PREFIX "DW"
330 \f
331 /* Match the base name of a file to the base name of a compilation unit. */
332
333 static int
334 matches_main_base (const char *path)
335 {
336 /* Cache the last query. */
337 static const char *last_path = NULL;
338 static int last_match = 0;
339 if (path != last_path)
340 {
341 const char *base;
342 int length = base_of_path (path, &base);
343 last_path = path;
344 last_match = (length == main_input_baselength
345 && memcmp (base, main_input_basename, length) == 0);
346 }
347 return last_match;
348 }
349
350 #ifdef DEBUG_DEBUG_STRUCT
351
352 static int
353 dump_struct_debug (tree type, enum debug_info_usage usage,
354 enum debug_struct_file criterion, int generic,
355 int matches, int result)
356 {
357 /* Find the type name. */
358 tree type_decl = TYPE_STUB_DECL (type);
359 tree t = type_decl;
360 const char *name = 0;
361 if (TREE_CODE (t) == TYPE_DECL)
362 t = DECL_NAME (t);
363 if (t)
364 name = IDENTIFIER_POINTER (t);
365
366 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
367 criterion,
368 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
369 matches ? "bas" : "hdr",
370 generic ? "gen" : "ord",
371 usage == DINFO_USAGE_DFN ? ";" :
372 usage == DINFO_USAGE_DIR_USE ? "." : "*",
373 result,
374 (void*) type_decl, name);
375 return result;
376 }
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378 dump_struct_debug (type, usage, criterion, generic, matches, result)
379
380 #else
381
382 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
383 (result)
384
385 #endif
386
387 /* Get the number of HOST_WIDE_INTs needed to represent the precision
388 of the number. Some constants have a large uniform precision, so
389 we get the precision needed for the actual value of the number. */
390
391 static unsigned int
392 get_full_len (const wide_int &op)
393 {
394 int prec = wi::min_precision (op, UNSIGNED);
395 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
396 / HOST_BITS_PER_WIDE_INT);
397 }
398
399 static bool
400 should_emit_struct_debug (tree type, enum debug_info_usage usage)
401 {
402 enum debug_struct_file criterion;
403 tree type_decl;
404 bool generic = lang_hooks.types.generic_p (type);
405
406 if (generic)
407 criterion = debug_struct_generic[usage];
408 else
409 criterion = debug_struct_ordinary[usage];
410
411 if (criterion == DINFO_STRUCT_FILE_NONE)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 if (criterion == DINFO_STRUCT_FILE_ANY)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
415
416 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
417
418 if (type_decl != NULL)
419 {
420 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
421 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
422
423 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
424 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
425 }
426
427 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
428 }
429 \f
430 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
431 switch to the data section instead, and write out a synthetic start label
432 for collect2 the first time around. */
433
434 static void
435 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
436 {
437 if (eh_frame_section == 0)
438 {
439 int flags;
440
441 if (EH_TABLES_CAN_BE_READ_ONLY)
442 {
443 int fde_encoding;
444 int per_encoding;
445 int lsda_encoding;
446
447 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
448 /*global=*/0);
449 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
450 /*global=*/1);
451 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
452 /*global=*/0);
453 flags = ((! flag_pic
454 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
455 && (fde_encoding & 0x70) != DW_EH_PE_aligned
456 && (per_encoding & 0x70) != DW_EH_PE_absptr
457 && (per_encoding & 0x70) != DW_EH_PE_aligned
458 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
459 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
460 ? 0 : SECTION_WRITE);
461 }
462 else
463 flags = SECTION_WRITE;
464
465 #ifdef EH_FRAME_SECTION_NAME
466 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
467 #else
468 eh_frame_section = ((flags == SECTION_WRITE)
469 ? data_section : readonly_data_section);
470 #endif /* EH_FRAME_SECTION_NAME */
471 }
472
473 switch_to_section (eh_frame_section);
474
475 #ifdef EH_FRAME_THROUGH_COLLECT2
476 /* We have no special eh_frame section. Emit special labels to guide
477 collect2. */
478 if (!back)
479 {
480 tree label = get_file_function_name ("F");
481 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
482 targetm.asm_out.globalize_label (asm_out_file,
483 IDENTIFIER_POINTER (label));
484 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
485 }
486 #endif
487 }
488
489 /* Switch [BACK] to the eh or debug frame table section, depending on
490 FOR_EH. */
491
492 static void
493 switch_to_frame_table_section (int for_eh, bool back)
494 {
495 if (for_eh)
496 switch_to_eh_frame_section (back);
497 else
498 {
499 if (!debug_frame_section)
500 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
501 SECTION_DEBUG, NULL);
502 switch_to_section (debug_frame_section);
503 }
504 }
505
506 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
507
508 enum dw_cfi_oprnd_type
509 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
510 {
511 switch (cfi)
512 {
513 case DW_CFA_nop:
514 case DW_CFA_GNU_window_save:
515 case DW_CFA_remember_state:
516 case DW_CFA_restore_state:
517 return dw_cfi_oprnd_unused;
518
519 case DW_CFA_set_loc:
520 case DW_CFA_advance_loc1:
521 case DW_CFA_advance_loc2:
522 case DW_CFA_advance_loc4:
523 case DW_CFA_MIPS_advance_loc8:
524 return dw_cfi_oprnd_addr;
525
526 case DW_CFA_offset:
527 case DW_CFA_offset_extended:
528 case DW_CFA_def_cfa:
529 case DW_CFA_offset_extended_sf:
530 case DW_CFA_def_cfa_sf:
531 case DW_CFA_restore:
532 case DW_CFA_restore_extended:
533 case DW_CFA_undefined:
534 case DW_CFA_same_value:
535 case DW_CFA_def_cfa_register:
536 case DW_CFA_register:
537 case DW_CFA_expression:
538 case DW_CFA_val_expression:
539 return dw_cfi_oprnd_reg_num;
540
541 case DW_CFA_def_cfa_offset:
542 case DW_CFA_GNU_args_size:
543 case DW_CFA_def_cfa_offset_sf:
544 return dw_cfi_oprnd_offset;
545
546 case DW_CFA_def_cfa_expression:
547 return dw_cfi_oprnd_loc;
548
549 default:
550 gcc_unreachable ();
551 }
552 }
553
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
555
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
558 {
559 switch (cfi)
560 {
561 case DW_CFA_def_cfa:
562 case DW_CFA_def_cfa_sf:
563 case DW_CFA_offset:
564 case DW_CFA_offset_extended_sf:
565 case DW_CFA_offset_extended:
566 return dw_cfi_oprnd_offset;
567
568 case DW_CFA_register:
569 return dw_cfi_oprnd_reg_num;
570
571 case DW_CFA_expression:
572 case DW_CFA_val_expression:
573 return dw_cfi_oprnd_loc;
574
575 default:
576 return dw_cfi_oprnd_unused;
577 }
578 }
579
580 /* Output one FDE. */
581
582 static void
583 output_fde (dw_fde_ref fde, bool for_eh, bool second,
584 char *section_start_label, int fde_encoding, char *augmentation,
585 bool any_lsda_needed, int lsda_encoding)
586 {
587 const char *begin, *end;
588 static unsigned int j;
589 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
590
591 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
592 /* empty */ 0);
593 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
594 for_eh + j);
595 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
596 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
597 if (!XCOFF_DEBUGGING_INFO || for_eh)
598 {
599 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
600 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
601 " indicating 64-bit DWARF extension");
602 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
603 "FDE Length");
604 }
605 ASM_OUTPUT_LABEL (asm_out_file, l1);
606
607 if (for_eh)
608 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
609 else
610 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
611 debug_frame_section, "FDE CIE offset");
612
613 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
614 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
615
616 if (for_eh)
617 {
618 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
619 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
620 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
621 "FDE initial location");
622 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
623 end, begin, "FDE address range");
624 }
625 else
626 {
627 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
628 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 }
630
631 if (augmentation[0])
632 {
633 if (any_lsda_needed)
634 {
635 int size = size_of_encoded_value (lsda_encoding);
636
637 if (lsda_encoding == DW_EH_PE_aligned)
638 {
639 int offset = ( 4 /* Length */
640 + 4 /* CIE offset */
641 + 2 * size_of_encoded_value (fde_encoding)
642 + 1 /* Augmentation size */ );
643 int pad = -offset & (PTR_SIZE - 1);
644
645 size += pad;
646 gcc_assert (size_of_uleb128 (size) == 1);
647 }
648
649 dw2_asm_output_data_uleb128 (size, "Augmentation size");
650
651 if (fde->uses_eh_lsda)
652 {
653 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
654 fde->funcdef_number);
655 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
656 gen_rtx_SYMBOL_REF (Pmode, l1),
657 false,
658 "Language Specific Data Area");
659 }
660 else
661 {
662 if (lsda_encoding == DW_EH_PE_aligned)
663 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
664 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
665 "Language Specific Data Area (none)");
666 }
667 }
668 else
669 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 }
671
672 /* Loop through the Call Frame Instructions associated with this FDE. */
673 fde->dw_fde_current_label = begin;
674 {
675 size_t from, until, i;
676
677 from = 0;
678 until = vec_safe_length (fde->dw_fde_cfi);
679
680 if (fde->dw_fde_second_begin == NULL)
681 ;
682 else if (!second)
683 until = fde->dw_fde_switch_cfi_index;
684 else
685 from = fde->dw_fde_switch_cfi_index;
686
687 for (i = from; i < until; i++)
688 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 }
690
691 /* If we are to emit a ref/link from function bodies to their frame tables,
692 do it now. This is typically performed to make sure that tables
693 associated with functions are dragged with them and not discarded in
694 garbage collecting links. We need to do this on a per function basis to
695 cope with -ffunction-sections. */
696
697 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
698 /* Switch to the function section, emit the ref to the tables, and
699 switch *back* into the table section. */
700 switch_to_section (function_section (fde->decl));
701 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
702 switch_to_frame_table_section (for_eh, true);
703 #endif
704
705 /* Pad the FDE out to an address sized boundary. */
706 ASM_OUTPUT_ALIGN (asm_out_file,
707 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
708 ASM_OUTPUT_LABEL (asm_out_file, l2);
709
710 j += 2;
711 }
712
713 /* Return true if frame description entry FDE is needed for EH. */
714
715 static bool
716 fde_needed_for_eh_p (dw_fde_ref fde)
717 {
718 if (flag_asynchronous_unwind_tables)
719 return true;
720
721 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
722 return true;
723
724 if (fde->uses_eh_lsda)
725 return true;
726
727 /* If exceptions are enabled, we have collected nothrow info. */
728 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
729 return false;
730
731 return true;
732 }
733
734 /* Output the call frame information used to record information
735 that relates to calculating the frame pointer, and records the
736 location of saved registers. */
737
738 static void
739 output_call_frame_info (int for_eh)
740 {
741 unsigned int i;
742 dw_fde_ref fde;
743 dw_cfi_ref cfi;
744 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
745 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
746 bool any_lsda_needed = false;
747 char augmentation[6];
748 int augmentation_size;
749 int fde_encoding = DW_EH_PE_absptr;
750 int per_encoding = DW_EH_PE_absptr;
751 int lsda_encoding = DW_EH_PE_absptr;
752 int return_reg;
753 rtx personality = NULL;
754 int dw_cie_version;
755
756 /* Don't emit a CIE if there won't be any FDEs. */
757 if (!fde_vec)
758 return;
759
760 /* Nothing to do if the assembler's doing it all. */
761 if (dwarf2out_do_cfi_asm ())
762 return;
763
764 /* If we don't have any functions we'll want to unwind out of, don't emit
765 any EH unwind information. If we make FDEs linkonce, we may have to
766 emit an empty label for an FDE that wouldn't otherwise be emitted. We
767 want to avoid having an FDE kept around when the function it refers to
768 is discarded. Example where this matters: a primary function template
769 in C++ requires EH information, an explicit specialization doesn't. */
770 if (for_eh)
771 {
772 bool any_eh_needed = false;
773
774 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
775 {
776 if (fde->uses_eh_lsda)
777 any_eh_needed = any_lsda_needed = true;
778 else if (fde_needed_for_eh_p (fde))
779 any_eh_needed = true;
780 else if (TARGET_USES_WEAK_UNWIND_INFO)
781 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 }
783
784 if (!any_eh_needed)
785 return;
786 }
787
788 /* We're going to be generating comments, so turn on app. */
789 if (flag_debug_asm)
790 app_enable ();
791
792 /* Switch to the proper frame section, first time. */
793 switch_to_frame_table_section (for_eh, false);
794
795 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
796 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
797
798 /* Output the CIE. */
799 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
800 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
801 if (!XCOFF_DEBUGGING_INFO || for_eh)
802 {
803 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
804 dw2_asm_output_data (4, 0xffffffff,
805 "Initial length escape value indicating 64-bit DWARF extension");
806 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
807 "Length of Common Information Entry");
808 }
809 ASM_OUTPUT_LABEL (asm_out_file, l1);
810
811 /* Now that the CIE pointer is PC-relative for EH,
812 use 0 to identify the CIE. */
813 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
814 (for_eh ? 0 : DWARF_CIE_ID),
815 "CIE Identifier Tag");
816
817 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
818 use CIE version 1, unless that would produce incorrect results
819 due to overflowing the return register column. */
820 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
821 dw_cie_version = 1;
822 if (return_reg >= 256 || dwarf_version > 2)
823 dw_cie_version = 3;
824 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
825
826 augmentation[0] = 0;
827 augmentation_size = 0;
828
829 personality = current_unit_personality;
830 if (for_eh)
831 {
832 char *p;
833
834 /* Augmentation:
835 z Indicates that a uleb128 is present to size the
836 augmentation section.
837 L Indicates the encoding (and thus presence) of
838 an LSDA pointer in the FDE augmentation.
839 R Indicates a non-default pointer encoding for
840 FDE code pointers.
841 P Indicates the presence of an encoding + language
842 personality routine in the CIE augmentation. */
843
844 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
845 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
846 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
847
848 p = augmentation + 1;
849 if (personality)
850 {
851 *p++ = 'P';
852 augmentation_size += 1 + size_of_encoded_value (per_encoding);
853 assemble_external_libcall (personality);
854 }
855 if (any_lsda_needed)
856 {
857 *p++ = 'L';
858 augmentation_size += 1;
859 }
860 if (fde_encoding != DW_EH_PE_absptr)
861 {
862 *p++ = 'R';
863 augmentation_size += 1;
864 }
865 if (p > augmentation + 1)
866 {
867 augmentation[0] = 'z';
868 *p = '\0';
869 }
870
871 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
872 if (personality && per_encoding == DW_EH_PE_aligned)
873 {
874 int offset = ( 4 /* Length */
875 + 4 /* CIE Id */
876 + 1 /* CIE version */
877 + strlen (augmentation) + 1 /* Augmentation */
878 + size_of_uleb128 (1) /* Code alignment */
879 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
880 + 1 /* RA column */
881 + 1 /* Augmentation size */
882 + 1 /* Personality encoding */ );
883 int pad = -offset & (PTR_SIZE - 1);
884
885 augmentation_size += pad;
886
887 /* Augmentations should be small, so there's scarce need to
888 iterate for a solution. Die if we exceed one uleb128 byte. */
889 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
890 }
891 }
892
893 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
894 if (dw_cie_version >= 4)
895 {
896 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
897 dw2_asm_output_data (1, 0, "CIE Segment Size");
898 }
899 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
900 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
901 "CIE Data Alignment Factor");
902
903 if (dw_cie_version == 1)
904 dw2_asm_output_data (1, return_reg, "CIE RA Column");
905 else
906 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
907
908 if (augmentation[0])
909 {
910 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
911 if (personality)
912 {
913 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
914 eh_data_format_name (per_encoding));
915 dw2_asm_output_encoded_addr_rtx (per_encoding,
916 personality,
917 true, NULL);
918 }
919
920 if (any_lsda_needed)
921 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
922 eh_data_format_name (lsda_encoding));
923
924 if (fde_encoding != DW_EH_PE_absptr)
925 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
926 eh_data_format_name (fde_encoding));
927 }
928
929 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
930 output_cfi (cfi, NULL, for_eh);
931
932 /* Pad the CIE out to an address sized boundary. */
933 ASM_OUTPUT_ALIGN (asm_out_file,
934 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
935 ASM_OUTPUT_LABEL (asm_out_file, l2);
936
937 /* Loop through all of the FDE's. */
938 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
939 {
940 unsigned int k;
941
942 /* Don't emit EH unwind info for leaf functions that don't need it. */
943 if (for_eh && !fde_needed_for_eh_p (fde))
944 continue;
945
946 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
947 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
948 augmentation, any_lsda_needed, lsda_encoding);
949 }
950
951 if (for_eh && targetm.terminate_dw2_eh_frame_info)
952 dw2_asm_output_data (4, 0, "End of Table");
953
954 /* Turn off app to make assembly quicker. */
955 if (flag_debug_asm)
956 app_disable ();
957 }
958
959 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
960
961 static void
962 dwarf2out_do_cfi_startproc (bool second)
963 {
964 int enc;
965 rtx ref;
966
967 fprintf (asm_out_file, "\t.cfi_startproc\n");
968
969 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
970 eh unwinders. */
971 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
972 return;
973
974 rtx personality = get_personality_function (current_function_decl);
975
976 if (personality)
977 {
978 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
979 ref = personality;
980
981 /* ??? The GAS support isn't entirely consistent. We have to
982 handle indirect support ourselves, but PC-relative is done
983 in the assembler. Further, the assembler can't handle any
984 of the weirder relocation types. */
985 if (enc & DW_EH_PE_indirect)
986 ref = dw2_force_const_mem (ref, true);
987
988 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
989 output_addr_const (asm_out_file, ref);
990 fputc ('\n', asm_out_file);
991 }
992
993 if (crtl->uses_eh_lsda)
994 {
995 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
996
997 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
998 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
999 current_function_funcdef_no);
1000 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1001 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1002
1003 if (enc & DW_EH_PE_indirect)
1004 ref = dw2_force_const_mem (ref, true);
1005
1006 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1007 output_addr_const (asm_out_file, ref);
1008 fputc ('\n', asm_out_file);
1009 }
1010 }
1011
1012 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1013 this allocation may be done before pass_final. */
1014
1015 dw_fde_ref
1016 dwarf2out_alloc_current_fde (void)
1017 {
1018 dw_fde_ref fde;
1019
1020 fde = ggc_cleared_alloc<dw_fde_node> ();
1021 fde->decl = current_function_decl;
1022 fde->funcdef_number = current_function_funcdef_no;
1023 fde->fde_index = vec_safe_length (fde_vec);
1024 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1025 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1026 fde->nothrow = crtl->nothrow;
1027 fde->drap_reg = INVALID_REGNUM;
1028 fde->vdrap_reg = INVALID_REGNUM;
1029
1030 /* Record the FDE associated with this function. */
1031 cfun->fde = fde;
1032 vec_safe_push (fde_vec, fde);
1033
1034 return fde;
1035 }
1036
1037 /* Output a marker (i.e. a label) for the beginning of a function, before
1038 the prologue. */
1039
1040 void
1041 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1042 unsigned int column ATTRIBUTE_UNUSED,
1043 const char *file ATTRIBUTE_UNUSED)
1044 {
1045 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1046 char * dup_label;
1047 dw_fde_ref fde;
1048 section *fnsec;
1049 bool do_frame;
1050
1051 current_function_func_begin_label = NULL;
1052
1053 do_frame = dwarf2out_do_frame ();
1054
1055 /* ??? current_function_func_begin_label is also used by except.c for
1056 call-site information. We must emit this label if it might be used. */
1057 if (!do_frame
1058 && (!flag_exceptions
1059 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1060 return;
1061
1062 fnsec = function_section (current_function_decl);
1063 switch_to_section (fnsec);
1064 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1065 current_function_funcdef_no);
1066 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1067 current_function_funcdef_no);
1068 dup_label = xstrdup (label);
1069 current_function_func_begin_label = dup_label;
1070
1071 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1072 if (!do_frame)
1073 return;
1074
1075 /* Unlike the debug version, the EH version of frame unwind info is a per-
1076 function setting so we need to record whether we need it for the unit. */
1077 do_eh_frame |= dwarf2out_do_eh_frame ();
1078
1079 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1080 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1081 would include pass_dwarf2_frame. If we've not created the FDE yet,
1082 do so now. */
1083 fde = cfun->fde;
1084 if (fde == NULL)
1085 fde = dwarf2out_alloc_current_fde ();
1086
1087 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1088 fde->dw_fde_begin = dup_label;
1089 fde->dw_fde_current_label = dup_label;
1090 fde->in_std_section = (fnsec == text_section
1091 || (cold_text_section && fnsec == cold_text_section));
1092
1093 /* We only want to output line number information for the genuine dwarf2
1094 prologue case, not the eh frame case. */
1095 #ifdef DWARF2_DEBUGGING_INFO
1096 if (file)
1097 dwarf2out_source_line (line, column, file, 0, true);
1098 #endif
1099
1100 if (dwarf2out_do_cfi_asm ())
1101 dwarf2out_do_cfi_startproc (false);
1102 else
1103 {
1104 rtx personality = get_personality_function (current_function_decl);
1105 if (!current_unit_personality)
1106 current_unit_personality = personality;
1107
1108 /* We cannot keep a current personality per function as without CFI
1109 asm, at the point where we emit the CFI data, there is no current
1110 function anymore. */
1111 if (personality && current_unit_personality != personality)
1112 sorry ("multiple EH personalities are supported only with assemblers "
1113 "supporting .cfi_personality directive");
1114 }
1115 }
1116
1117 /* Output a marker (i.e. a label) for the end of the generated code
1118 for a function prologue. This gets called *after* the prologue code has
1119 been generated. */
1120
1121 void
1122 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1123 const char *file ATTRIBUTE_UNUSED)
1124 {
1125 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1126
1127 /* Output a label to mark the endpoint of the code generated for this
1128 function. */
1129 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1130 current_function_funcdef_no);
1131 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1132 current_function_funcdef_no);
1133 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1134 }
1135
1136 /* Output a marker (i.e. a label) for the beginning of the generated code
1137 for a function epilogue. This gets called *before* the prologue code has
1138 been generated. */
1139
1140 void
1141 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1142 const char *file ATTRIBUTE_UNUSED)
1143 {
1144 dw_fde_ref fde = cfun->fde;
1145 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1146
1147 if (fde->dw_fde_vms_begin_epilogue)
1148 return;
1149
1150 /* Output a label to mark the endpoint of the code generated for this
1151 function. */
1152 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1153 current_function_funcdef_no);
1154 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1155 current_function_funcdef_no);
1156 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1157 }
1158
1159 /* Output a marker (i.e. a label) for the absolute end of the generated code
1160 for a function definition. This gets called *after* the epilogue code has
1161 been generated. */
1162
1163 void
1164 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1165 const char *file ATTRIBUTE_UNUSED)
1166 {
1167 dw_fde_ref fde;
1168 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1169
1170 last_var_location_insn = NULL;
1171 cached_next_real_insn = NULL;
1172
1173 if (dwarf2out_do_cfi_asm ())
1174 fprintf (asm_out_file, "\t.cfi_endproc\n");
1175
1176 /* Output a label to mark the endpoint of the code generated for this
1177 function. */
1178 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1179 current_function_funcdef_no);
1180 ASM_OUTPUT_LABEL (asm_out_file, label);
1181 fde = cfun->fde;
1182 gcc_assert (fde != NULL);
1183 if (fde->dw_fde_second_begin == NULL)
1184 fde->dw_fde_end = xstrdup (label);
1185 }
1186
1187 void
1188 dwarf2out_frame_finish (void)
1189 {
1190 /* Output call frame information. */
1191 if (targetm.debug_unwind_info () == UI_DWARF2)
1192 output_call_frame_info (0);
1193
1194 /* Output another copy for the unwinder. */
1195 if (do_eh_frame)
1196 output_call_frame_info (1);
1197 }
1198
1199 /* Note that the current function section is being used for code. */
1200
1201 static void
1202 dwarf2out_note_section_used (void)
1203 {
1204 section *sec = current_function_section ();
1205 if (sec == text_section)
1206 text_section_used = true;
1207 else if (sec == cold_text_section)
1208 cold_text_section_used = true;
1209 }
1210
1211 static void var_location_switch_text_section (void);
1212 static void set_cur_line_info_table (section *);
1213
1214 void
1215 dwarf2out_switch_text_section (void)
1216 {
1217 section *sect;
1218 dw_fde_ref fde = cfun->fde;
1219
1220 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1221
1222 if (!in_cold_section_p)
1223 {
1224 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1225 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1226 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1227 }
1228 else
1229 {
1230 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1231 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1232 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1233 }
1234 have_multiple_function_sections = true;
1235
1236 /* There is no need to mark used sections when not debugging. */
1237 if (cold_text_section != NULL)
1238 dwarf2out_note_section_used ();
1239
1240 if (dwarf2out_do_cfi_asm ())
1241 fprintf (asm_out_file, "\t.cfi_endproc\n");
1242
1243 /* Now do the real section switch. */
1244 sect = current_function_section ();
1245 switch_to_section (sect);
1246
1247 fde->second_in_std_section
1248 = (sect == text_section
1249 || (cold_text_section && sect == cold_text_section));
1250
1251 if (dwarf2out_do_cfi_asm ())
1252 dwarf2out_do_cfi_startproc (true);
1253
1254 var_location_switch_text_section ();
1255
1256 if (cold_text_section != NULL)
1257 set_cur_line_info_table (sect);
1258 }
1259 \f
1260 /* And now, the subset of the debugging information support code necessary
1261 for emitting location expressions. */
1262
1263 /* Data about a single source file. */
1264 struct GTY((for_user)) dwarf_file_data {
1265 const char * filename;
1266 int emitted_number;
1267 };
1268
1269 /* Describe an entry into the .debug_addr section. */
1270
1271 enum ate_kind {
1272 ate_kind_rtx,
1273 ate_kind_rtx_dtprel,
1274 ate_kind_label
1275 };
1276
1277 struct GTY((for_user)) addr_table_entry {
1278 enum ate_kind kind;
1279 unsigned int refcount;
1280 unsigned int index;
1281 union addr_table_entry_struct_union
1282 {
1283 rtx GTY ((tag ("0"))) rtl;
1284 char * GTY ((tag ("1"))) label;
1285 }
1286 GTY ((desc ("%1.kind"))) addr;
1287 };
1288
1289 /* Location lists are ranges + location descriptions for that range,
1290 so you can track variables that are in different places over
1291 their entire life. */
1292 typedef struct GTY(()) dw_loc_list_struct {
1293 dw_loc_list_ref dw_loc_next;
1294 const char *begin; /* Label and addr_entry for start of range */
1295 addr_table_entry *begin_entry;
1296 const char *end; /* Label for end of range */
1297 char *ll_symbol; /* Label for beginning of location list.
1298 Only on head of list */
1299 const char *section; /* Section this loclist is relative to */
1300 dw_loc_descr_ref expr;
1301 hashval_t hash;
1302 /* True if all addresses in this and subsequent lists are known to be
1303 resolved. */
1304 bool resolved_addr;
1305 /* True if this list has been replaced by dw_loc_next. */
1306 bool replaced;
1307 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1308 section. */
1309 unsigned char emitted : 1;
1310 /* True if hash field is index rather than hash value. */
1311 unsigned char num_assigned : 1;
1312 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1313 unsigned char offset_emitted : 1;
1314 /* True if note_variable_value_in_expr has been called on it. */
1315 unsigned char noted_variable_value : 1;
1316 /* True if the range should be emitted even if begin and end
1317 are the same. */
1318 bool force;
1319 } dw_loc_list_node;
1320
1321 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1322 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1323
1324 /* Convert a DWARF stack opcode into its string name. */
1325
1326 static const char *
1327 dwarf_stack_op_name (unsigned int op)
1328 {
1329 const char *name = get_DW_OP_name (op);
1330
1331 if (name != NULL)
1332 return name;
1333
1334 return "OP_<unknown>";
1335 }
1336
1337 /* Return a pointer to a newly allocated location description. Location
1338 descriptions are simple expression terms that can be strung
1339 together to form more complicated location (address) descriptions. */
1340
1341 static inline dw_loc_descr_ref
1342 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1343 unsigned HOST_WIDE_INT oprnd2)
1344 {
1345 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1346
1347 descr->dw_loc_opc = op;
1348 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1349 descr->dw_loc_oprnd1.val_entry = NULL;
1350 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1351 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1352 descr->dw_loc_oprnd2.val_entry = NULL;
1353 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1354
1355 return descr;
1356 }
1357
1358 /* Return a pointer to a newly allocated location description for
1359 REG and OFFSET. */
1360
1361 static inline dw_loc_descr_ref
1362 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1363 {
1364 if (reg <= 31)
1365 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1366 offset, 0);
1367 else
1368 return new_loc_descr (DW_OP_bregx, reg, offset);
1369 }
1370
1371 /* Add a location description term to a location description expression. */
1372
1373 static inline void
1374 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1375 {
1376 dw_loc_descr_ref *d;
1377
1378 /* Find the end of the chain. */
1379 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1380 ;
1381
1382 *d = descr;
1383 }
1384
1385 /* Compare two location operands for exact equality. */
1386
1387 static bool
1388 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1389 {
1390 if (a->val_class != b->val_class)
1391 return false;
1392 switch (a->val_class)
1393 {
1394 case dw_val_class_none:
1395 return true;
1396 case dw_val_class_addr:
1397 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1398
1399 case dw_val_class_offset:
1400 case dw_val_class_unsigned_const:
1401 case dw_val_class_const:
1402 case dw_val_class_unsigned_const_implicit:
1403 case dw_val_class_const_implicit:
1404 case dw_val_class_range_list:
1405 /* These are all HOST_WIDE_INT, signed or unsigned. */
1406 return a->v.val_unsigned == b->v.val_unsigned;
1407
1408 case dw_val_class_loc:
1409 return a->v.val_loc == b->v.val_loc;
1410 case dw_val_class_loc_list:
1411 return a->v.val_loc_list == b->v.val_loc_list;
1412 case dw_val_class_die_ref:
1413 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1414 case dw_val_class_fde_ref:
1415 return a->v.val_fde_index == b->v.val_fde_index;
1416 case dw_val_class_lbl_id:
1417 case dw_val_class_lineptr:
1418 case dw_val_class_macptr:
1419 case dw_val_class_loclistsptr:
1420 case dw_val_class_high_pc:
1421 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1422 case dw_val_class_str:
1423 return a->v.val_str == b->v.val_str;
1424 case dw_val_class_flag:
1425 return a->v.val_flag == b->v.val_flag;
1426 case dw_val_class_file:
1427 case dw_val_class_file_implicit:
1428 return a->v.val_file == b->v.val_file;
1429 case dw_val_class_decl_ref:
1430 return a->v.val_decl_ref == b->v.val_decl_ref;
1431
1432 case dw_val_class_const_double:
1433 return (a->v.val_double.high == b->v.val_double.high
1434 && a->v.val_double.low == b->v.val_double.low);
1435
1436 case dw_val_class_wide_int:
1437 return *a->v.val_wide == *b->v.val_wide;
1438
1439 case dw_val_class_vec:
1440 {
1441 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1442 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1443
1444 return (a_len == b_len
1445 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1446 }
1447
1448 case dw_val_class_data8:
1449 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1450
1451 case dw_val_class_vms_delta:
1452 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1453 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1454
1455 case dw_val_class_discr_value:
1456 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1457 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1458 case dw_val_class_discr_list:
1459 /* It makes no sense comparing two discriminant value lists. */
1460 return false;
1461 }
1462 gcc_unreachable ();
1463 }
1464
1465 /* Compare two location atoms for exact equality. */
1466
1467 static bool
1468 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1469 {
1470 if (a->dw_loc_opc != b->dw_loc_opc)
1471 return false;
1472
1473 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1474 address size, but since we always allocate cleared storage it
1475 should be zero for other types of locations. */
1476 if (a->dtprel != b->dtprel)
1477 return false;
1478
1479 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1480 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1481 }
1482
1483 /* Compare two complete location expressions for exact equality. */
1484
1485 bool
1486 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1487 {
1488 while (1)
1489 {
1490 if (a == b)
1491 return true;
1492 if (a == NULL || b == NULL)
1493 return false;
1494 if (!loc_descr_equal_p_1 (a, b))
1495 return false;
1496
1497 a = a->dw_loc_next;
1498 b = b->dw_loc_next;
1499 }
1500 }
1501
1502
1503 /* Add a constant OFFSET to a location expression. */
1504
1505 static void
1506 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1507 {
1508 dw_loc_descr_ref loc;
1509 HOST_WIDE_INT *p;
1510
1511 gcc_assert (*list_head != NULL);
1512
1513 if (!offset)
1514 return;
1515
1516 /* Find the end of the chain. */
1517 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1518 ;
1519
1520 p = NULL;
1521 if (loc->dw_loc_opc == DW_OP_fbreg
1522 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1523 p = &loc->dw_loc_oprnd1.v.val_int;
1524 else if (loc->dw_loc_opc == DW_OP_bregx)
1525 p = &loc->dw_loc_oprnd2.v.val_int;
1526
1527 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1528 offset. Don't optimize if an signed integer overflow would happen. */
1529 if (p != NULL
1530 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1531 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1532 *p += offset;
1533
1534 else if (offset > 0)
1535 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1536
1537 else
1538 {
1539 loc->dw_loc_next
1540 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1541 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1542 }
1543 }
1544
1545 /* Add a constant OFFSET to a location list. */
1546
1547 static void
1548 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1549 {
1550 dw_loc_list_ref d;
1551 for (d = list_head; d != NULL; d = d->dw_loc_next)
1552 loc_descr_plus_const (&d->expr, offset);
1553 }
1554
1555 #define DWARF_REF_SIZE \
1556 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1557
1558 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1559 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1560 DW_FORM_data16 with 128 bits. */
1561 #define DWARF_LARGEST_DATA_FORM_BITS \
1562 (dwarf_version >= 5 ? 128 : 64)
1563
1564 /* Utility inline function for construction of ops that were GNU extension
1565 before DWARF 5. */
1566 static inline enum dwarf_location_atom
1567 dwarf_OP (enum dwarf_location_atom op)
1568 {
1569 switch (op)
1570 {
1571 case DW_OP_implicit_pointer:
1572 if (dwarf_version < 5)
1573 return DW_OP_GNU_implicit_pointer;
1574 break;
1575
1576 case DW_OP_entry_value:
1577 if (dwarf_version < 5)
1578 return DW_OP_GNU_entry_value;
1579 break;
1580
1581 case DW_OP_const_type:
1582 if (dwarf_version < 5)
1583 return DW_OP_GNU_const_type;
1584 break;
1585
1586 case DW_OP_regval_type:
1587 if (dwarf_version < 5)
1588 return DW_OP_GNU_regval_type;
1589 break;
1590
1591 case DW_OP_deref_type:
1592 if (dwarf_version < 5)
1593 return DW_OP_GNU_deref_type;
1594 break;
1595
1596 case DW_OP_convert:
1597 if (dwarf_version < 5)
1598 return DW_OP_GNU_convert;
1599 break;
1600
1601 case DW_OP_reinterpret:
1602 if (dwarf_version < 5)
1603 return DW_OP_GNU_reinterpret;
1604 break;
1605
1606 default:
1607 break;
1608 }
1609 return op;
1610 }
1611
1612 /* Similarly for attributes. */
1613 static inline enum dwarf_attribute
1614 dwarf_AT (enum dwarf_attribute at)
1615 {
1616 switch (at)
1617 {
1618 case DW_AT_call_return_pc:
1619 if (dwarf_version < 5)
1620 return DW_AT_low_pc;
1621 break;
1622
1623 case DW_AT_call_tail_call:
1624 if (dwarf_version < 5)
1625 return DW_AT_GNU_tail_call;
1626 break;
1627
1628 case DW_AT_call_origin:
1629 if (dwarf_version < 5)
1630 return DW_AT_abstract_origin;
1631 break;
1632
1633 case DW_AT_call_target:
1634 if (dwarf_version < 5)
1635 return DW_AT_GNU_call_site_target;
1636 break;
1637
1638 case DW_AT_call_target_clobbered:
1639 if (dwarf_version < 5)
1640 return DW_AT_GNU_call_site_target_clobbered;
1641 break;
1642
1643 case DW_AT_call_parameter:
1644 if (dwarf_version < 5)
1645 return DW_AT_abstract_origin;
1646 break;
1647
1648 case DW_AT_call_value:
1649 if (dwarf_version < 5)
1650 return DW_AT_GNU_call_site_value;
1651 break;
1652
1653 case DW_AT_call_data_value:
1654 if (dwarf_version < 5)
1655 return DW_AT_GNU_call_site_data_value;
1656 break;
1657
1658 case DW_AT_call_all_calls:
1659 if (dwarf_version < 5)
1660 return DW_AT_GNU_all_call_sites;
1661 break;
1662
1663 case DW_AT_call_all_tail_calls:
1664 if (dwarf_version < 5)
1665 return DW_AT_GNU_all_tail_call_sites;
1666 break;
1667
1668 case DW_AT_dwo_name:
1669 if (dwarf_version < 5)
1670 return DW_AT_GNU_dwo_name;
1671 break;
1672
1673 default:
1674 break;
1675 }
1676 return at;
1677 }
1678
1679 /* And similarly for tags. */
1680 static inline enum dwarf_tag
1681 dwarf_TAG (enum dwarf_tag tag)
1682 {
1683 switch (tag)
1684 {
1685 case DW_TAG_call_site:
1686 if (dwarf_version < 5)
1687 return DW_TAG_GNU_call_site;
1688 break;
1689
1690 case DW_TAG_call_site_parameter:
1691 if (dwarf_version < 5)
1692 return DW_TAG_GNU_call_site_parameter;
1693 break;
1694
1695 default:
1696 break;
1697 }
1698 return tag;
1699 }
1700
1701 static unsigned long int get_base_type_offset (dw_die_ref);
1702
1703 /* Return the size of a location descriptor. */
1704
1705 static unsigned long
1706 size_of_loc_descr (dw_loc_descr_ref loc)
1707 {
1708 unsigned long size = 1;
1709
1710 switch (loc->dw_loc_opc)
1711 {
1712 case DW_OP_addr:
1713 size += DWARF2_ADDR_SIZE;
1714 break;
1715 case DW_OP_GNU_addr_index:
1716 case DW_OP_GNU_const_index:
1717 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1718 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1719 break;
1720 case DW_OP_const1u:
1721 case DW_OP_const1s:
1722 size += 1;
1723 break;
1724 case DW_OP_const2u:
1725 case DW_OP_const2s:
1726 size += 2;
1727 break;
1728 case DW_OP_const4u:
1729 case DW_OP_const4s:
1730 size += 4;
1731 break;
1732 case DW_OP_const8u:
1733 case DW_OP_const8s:
1734 size += 8;
1735 break;
1736 case DW_OP_constu:
1737 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1738 break;
1739 case DW_OP_consts:
1740 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1741 break;
1742 case DW_OP_pick:
1743 size += 1;
1744 break;
1745 case DW_OP_plus_uconst:
1746 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1747 break;
1748 case DW_OP_skip:
1749 case DW_OP_bra:
1750 size += 2;
1751 break;
1752 case DW_OP_breg0:
1753 case DW_OP_breg1:
1754 case DW_OP_breg2:
1755 case DW_OP_breg3:
1756 case DW_OP_breg4:
1757 case DW_OP_breg5:
1758 case DW_OP_breg6:
1759 case DW_OP_breg7:
1760 case DW_OP_breg8:
1761 case DW_OP_breg9:
1762 case DW_OP_breg10:
1763 case DW_OP_breg11:
1764 case DW_OP_breg12:
1765 case DW_OP_breg13:
1766 case DW_OP_breg14:
1767 case DW_OP_breg15:
1768 case DW_OP_breg16:
1769 case DW_OP_breg17:
1770 case DW_OP_breg18:
1771 case DW_OP_breg19:
1772 case DW_OP_breg20:
1773 case DW_OP_breg21:
1774 case DW_OP_breg22:
1775 case DW_OP_breg23:
1776 case DW_OP_breg24:
1777 case DW_OP_breg25:
1778 case DW_OP_breg26:
1779 case DW_OP_breg27:
1780 case DW_OP_breg28:
1781 case DW_OP_breg29:
1782 case DW_OP_breg30:
1783 case DW_OP_breg31:
1784 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1785 break;
1786 case DW_OP_regx:
1787 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1788 break;
1789 case DW_OP_fbreg:
1790 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1791 break;
1792 case DW_OP_bregx:
1793 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1794 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1795 break;
1796 case DW_OP_piece:
1797 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1798 break;
1799 case DW_OP_bit_piece:
1800 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1801 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1802 break;
1803 case DW_OP_deref_size:
1804 case DW_OP_xderef_size:
1805 size += 1;
1806 break;
1807 case DW_OP_call2:
1808 size += 2;
1809 break;
1810 case DW_OP_call4:
1811 size += 4;
1812 break;
1813 case DW_OP_call_ref:
1814 case DW_OP_GNU_variable_value:
1815 size += DWARF_REF_SIZE;
1816 break;
1817 case DW_OP_implicit_value:
1818 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1819 + loc->dw_loc_oprnd1.v.val_unsigned;
1820 break;
1821 case DW_OP_implicit_pointer:
1822 case DW_OP_GNU_implicit_pointer:
1823 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1824 break;
1825 case DW_OP_entry_value:
1826 case DW_OP_GNU_entry_value:
1827 {
1828 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1829 size += size_of_uleb128 (op_size) + op_size;
1830 break;
1831 }
1832 case DW_OP_const_type:
1833 case DW_OP_GNU_const_type:
1834 {
1835 unsigned long o
1836 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1837 size += size_of_uleb128 (o) + 1;
1838 switch (loc->dw_loc_oprnd2.val_class)
1839 {
1840 case dw_val_class_vec:
1841 size += loc->dw_loc_oprnd2.v.val_vec.length
1842 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1843 break;
1844 case dw_val_class_const:
1845 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1846 break;
1847 case dw_val_class_const_double:
1848 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1849 break;
1850 case dw_val_class_wide_int:
1851 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1852 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1853 break;
1854 default:
1855 gcc_unreachable ();
1856 }
1857 break;
1858 }
1859 case DW_OP_regval_type:
1860 case DW_OP_GNU_regval_type:
1861 {
1862 unsigned long o
1863 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1864 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1865 + size_of_uleb128 (o);
1866 }
1867 break;
1868 case DW_OP_deref_type:
1869 case DW_OP_GNU_deref_type:
1870 {
1871 unsigned long o
1872 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1873 size += 1 + size_of_uleb128 (o);
1874 }
1875 break;
1876 case DW_OP_convert:
1877 case DW_OP_reinterpret:
1878 case DW_OP_GNU_convert:
1879 case DW_OP_GNU_reinterpret:
1880 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1881 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1882 else
1883 {
1884 unsigned long o
1885 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1886 size += size_of_uleb128 (o);
1887 }
1888 break;
1889 case DW_OP_GNU_parameter_ref:
1890 size += 4;
1891 break;
1892 default:
1893 break;
1894 }
1895
1896 return size;
1897 }
1898
1899 /* Return the size of a series of location descriptors. */
1900
1901 unsigned long
1902 size_of_locs (dw_loc_descr_ref loc)
1903 {
1904 dw_loc_descr_ref l;
1905 unsigned long size;
1906
1907 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1908 field, to avoid writing to a PCH file. */
1909 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1910 {
1911 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1912 break;
1913 size += size_of_loc_descr (l);
1914 }
1915 if (! l)
1916 return size;
1917
1918 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1919 {
1920 l->dw_loc_addr = size;
1921 size += size_of_loc_descr (l);
1922 }
1923
1924 return size;
1925 }
1926
1927 /* Return the size of the value in a DW_AT_discr_value attribute. */
1928
1929 static int
1930 size_of_discr_value (dw_discr_value *discr_value)
1931 {
1932 if (discr_value->pos)
1933 return size_of_uleb128 (discr_value->v.uval);
1934 else
1935 return size_of_sleb128 (discr_value->v.sval);
1936 }
1937
1938 /* Return the size of the value in a DW_AT_discr_list attribute. */
1939
1940 static int
1941 size_of_discr_list (dw_discr_list_ref discr_list)
1942 {
1943 int size = 0;
1944
1945 for (dw_discr_list_ref list = discr_list;
1946 list != NULL;
1947 list = list->dw_discr_next)
1948 {
1949 /* One byte for the discriminant value descriptor, and then one or two
1950 LEB128 numbers, depending on whether it's a single case label or a
1951 range label. */
1952 size += 1;
1953 size += size_of_discr_value (&list->dw_discr_lower_bound);
1954 if (list->dw_discr_range != 0)
1955 size += size_of_discr_value (&list->dw_discr_upper_bound);
1956 }
1957 return size;
1958 }
1959
1960 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1961 static void get_ref_die_offset_label (char *, dw_die_ref);
1962 static unsigned long int get_ref_die_offset (dw_die_ref);
1963
1964 /* Output location description stack opcode's operands (if any).
1965 The for_eh_or_skip parameter controls whether register numbers are
1966 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1967 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1968 info). This should be suppressed for the cases that have not been converted
1969 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1970
1971 static void
1972 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1973 {
1974 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1975 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1976
1977 switch (loc->dw_loc_opc)
1978 {
1979 #ifdef DWARF2_DEBUGGING_INFO
1980 case DW_OP_const2u:
1981 case DW_OP_const2s:
1982 dw2_asm_output_data (2, val1->v.val_int, NULL);
1983 break;
1984 case DW_OP_const4u:
1985 if (loc->dtprel)
1986 {
1987 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1988 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1989 val1->v.val_addr);
1990 fputc ('\n', asm_out_file);
1991 break;
1992 }
1993 /* FALLTHRU */
1994 case DW_OP_const4s:
1995 dw2_asm_output_data (4, val1->v.val_int, NULL);
1996 break;
1997 case DW_OP_const8u:
1998 if (loc->dtprel)
1999 {
2000 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2001 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2002 val1->v.val_addr);
2003 fputc ('\n', asm_out_file);
2004 break;
2005 }
2006 /* FALLTHRU */
2007 case DW_OP_const8s:
2008 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2009 dw2_asm_output_data (8, val1->v.val_int, NULL);
2010 break;
2011 case DW_OP_skip:
2012 case DW_OP_bra:
2013 {
2014 int offset;
2015
2016 gcc_assert (val1->val_class == dw_val_class_loc);
2017 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2018
2019 dw2_asm_output_data (2, offset, NULL);
2020 }
2021 break;
2022 case DW_OP_implicit_value:
2023 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2024 switch (val2->val_class)
2025 {
2026 case dw_val_class_const:
2027 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2028 break;
2029 case dw_val_class_vec:
2030 {
2031 unsigned int elt_size = val2->v.val_vec.elt_size;
2032 unsigned int len = val2->v.val_vec.length;
2033 unsigned int i;
2034 unsigned char *p;
2035
2036 if (elt_size > sizeof (HOST_WIDE_INT))
2037 {
2038 elt_size /= 2;
2039 len *= 2;
2040 }
2041 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2042 i < len;
2043 i++, p += elt_size)
2044 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2045 "fp or vector constant word %u", i);
2046 }
2047 break;
2048 case dw_val_class_const_double:
2049 {
2050 unsigned HOST_WIDE_INT first, second;
2051
2052 if (WORDS_BIG_ENDIAN)
2053 {
2054 first = val2->v.val_double.high;
2055 second = val2->v.val_double.low;
2056 }
2057 else
2058 {
2059 first = val2->v.val_double.low;
2060 second = val2->v.val_double.high;
2061 }
2062 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2063 first, NULL);
2064 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2065 second, NULL);
2066 }
2067 break;
2068 case dw_val_class_wide_int:
2069 {
2070 int i;
2071 int len = get_full_len (*val2->v.val_wide);
2072 if (WORDS_BIG_ENDIAN)
2073 for (i = len - 1; i >= 0; --i)
2074 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2075 val2->v.val_wide->elt (i), NULL);
2076 else
2077 for (i = 0; i < len; ++i)
2078 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2079 val2->v.val_wide->elt (i), NULL);
2080 }
2081 break;
2082 case dw_val_class_addr:
2083 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2084 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2085 break;
2086 default:
2087 gcc_unreachable ();
2088 }
2089 break;
2090 #else
2091 case DW_OP_const2u:
2092 case DW_OP_const2s:
2093 case DW_OP_const4u:
2094 case DW_OP_const4s:
2095 case DW_OP_const8u:
2096 case DW_OP_const8s:
2097 case DW_OP_skip:
2098 case DW_OP_bra:
2099 case DW_OP_implicit_value:
2100 /* We currently don't make any attempt to make sure these are
2101 aligned properly like we do for the main unwind info, so
2102 don't support emitting things larger than a byte if we're
2103 only doing unwinding. */
2104 gcc_unreachable ();
2105 #endif
2106 case DW_OP_const1u:
2107 case DW_OP_const1s:
2108 dw2_asm_output_data (1, val1->v.val_int, NULL);
2109 break;
2110 case DW_OP_constu:
2111 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2112 break;
2113 case DW_OP_consts:
2114 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2115 break;
2116 case DW_OP_pick:
2117 dw2_asm_output_data (1, val1->v.val_int, NULL);
2118 break;
2119 case DW_OP_plus_uconst:
2120 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2121 break;
2122 case DW_OP_breg0:
2123 case DW_OP_breg1:
2124 case DW_OP_breg2:
2125 case DW_OP_breg3:
2126 case DW_OP_breg4:
2127 case DW_OP_breg5:
2128 case DW_OP_breg6:
2129 case DW_OP_breg7:
2130 case DW_OP_breg8:
2131 case DW_OP_breg9:
2132 case DW_OP_breg10:
2133 case DW_OP_breg11:
2134 case DW_OP_breg12:
2135 case DW_OP_breg13:
2136 case DW_OP_breg14:
2137 case DW_OP_breg15:
2138 case DW_OP_breg16:
2139 case DW_OP_breg17:
2140 case DW_OP_breg18:
2141 case DW_OP_breg19:
2142 case DW_OP_breg20:
2143 case DW_OP_breg21:
2144 case DW_OP_breg22:
2145 case DW_OP_breg23:
2146 case DW_OP_breg24:
2147 case DW_OP_breg25:
2148 case DW_OP_breg26:
2149 case DW_OP_breg27:
2150 case DW_OP_breg28:
2151 case DW_OP_breg29:
2152 case DW_OP_breg30:
2153 case DW_OP_breg31:
2154 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2155 break;
2156 case DW_OP_regx:
2157 {
2158 unsigned r = val1->v.val_unsigned;
2159 if (for_eh_or_skip >= 0)
2160 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2161 gcc_assert (size_of_uleb128 (r)
2162 == size_of_uleb128 (val1->v.val_unsigned));
2163 dw2_asm_output_data_uleb128 (r, NULL);
2164 }
2165 break;
2166 case DW_OP_fbreg:
2167 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2168 break;
2169 case DW_OP_bregx:
2170 {
2171 unsigned r = val1->v.val_unsigned;
2172 if (for_eh_or_skip >= 0)
2173 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2174 gcc_assert (size_of_uleb128 (r)
2175 == size_of_uleb128 (val1->v.val_unsigned));
2176 dw2_asm_output_data_uleb128 (r, NULL);
2177 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2178 }
2179 break;
2180 case DW_OP_piece:
2181 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2182 break;
2183 case DW_OP_bit_piece:
2184 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2185 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2186 break;
2187 case DW_OP_deref_size:
2188 case DW_OP_xderef_size:
2189 dw2_asm_output_data (1, val1->v.val_int, NULL);
2190 break;
2191
2192 case DW_OP_addr:
2193 if (loc->dtprel)
2194 {
2195 if (targetm.asm_out.output_dwarf_dtprel)
2196 {
2197 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2198 DWARF2_ADDR_SIZE,
2199 val1->v.val_addr);
2200 fputc ('\n', asm_out_file);
2201 }
2202 else
2203 gcc_unreachable ();
2204 }
2205 else
2206 {
2207 #ifdef DWARF2_DEBUGGING_INFO
2208 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2209 #else
2210 gcc_unreachable ();
2211 #endif
2212 }
2213 break;
2214
2215 case DW_OP_GNU_addr_index:
2216 case DW_OP_GNU_const_index:
2217 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2218 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2219 "(index into .debug_addr)");
2220 break;
2221
2222 case DW_OP_call2:
2223 case DW_OP_call4:
2224 {
2225 unsigned long die_offset
2226 = get_ref_die_offset (val1->v.val_die_ref.die);
2227 /* Make sure the offset has been computed and that we can encode it as
2228 an operand. */
2229 gcc_assert (die_offset > 0
2230 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2231 ? 0xffff
2232 : 0xffffffff));
2233 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2234 die_offset, NULL);
2235 }
2236 break;
2237
2238 case DW_OP_call_ref:
2239 case DW_OP_GNU_variable_value:
2240 {
2241 char label[MAX_ARTIFICIAL_LABEL_BYTES
2242 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2243 gcc_assert (val1->val_class == dw_val_class_die_ref);
2244 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2245 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2246 }
2247 break;
2248
2249 case DW_OP_implicit_pointer:
2250 case DW_OP_GNU_implicit_pointer:
2251 {
2252 char label[MAX_ARTIFICIAL_LABEL_BYTES
2253 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2254 gcc_assert (val1->val_class == dw_val_class_die_ref);
2255 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2256 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2257 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2258 }
2259 break;
2260
2261 case DW_OP_entry_value:
2262 case DW_OP_GNU_entry_value:
2263 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2264 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2265 break;
2266
2267 case DW_OP_const_type:
2268 case DW_OP_GNU_const_type:
2269 {
2270 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2271 gcc_assert (o);
2272 dw2_asm_output_data_uleb128 (o, NULL);
2273 switch (val2->val_class)
2274 {
2275 case dw_val_class_const:
2276 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2277 dw2_asm_output_data (1, l, NULL);
2278 dw2_asm_output_data (l, val2->v.val_int, NULL);
2279 break;
2280 case dw_val_class_vec:
2281 {
2282 unsigned int elt_size = val2->v.val_vec.elt_size;
2283 unsigned int len = val2->v.val_vec.length;
2284 unsigned int i;
2285 unsigned char *p;
2286
2287 l = len * elt_size;
2288 dw2_asm_output_data (1, l, NULL);
2289 if (elt_size > sizeof (HOST_WIDE_INT))
2290 {
2291 elt_size /= 2;
2292 len *= 2;
2293 }
2294 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2295 i < len;
2296 i++, p += elt_size)
2297 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2298 "fp or vector constant word %u", i);
2299 }
2300 break;
2301 case dw_val_class_const_double:
2302 {
2303 unsigned HOST_WIDE_INT first, second;
2304 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2305
2306 dw2_asm_output_data (1, 2 * l, NULL);
2307 if (WORDS_BIG_ENDIAN)
2308 {
2309 first = val2->v.val_double.high;
2310 second = val2->v.val_double.low;
2311 }
2312 else
2313 {
2314 first = val2->v.val_double.low;
2315 second = val2->v.val_double.high;
2316 }
2317 dw2_asm_output_data (l, first, NULL);
2318 dw2_asm_output_data (l, second, NULL);
2319 }
2320 break;
2321 case dw_val_class_wide_int:
2322 {
2323 int i;
2324 int len = get_full_len (*val2->v.val_wide);
2325 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2326
2327 dw2_asm_output_data (1, len * l, NULL);
2328 if (WORDS_BIG_ENDIAN)
2329 for (i = len - 1; i >= 0; --i)
2330 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2331 else
2332 for (i = 0; i < len; ++i)
2333 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2334 }
2335 break;
2336 default:
2337 gcc_unreachable ();
2338 }
2339 }
2340 break;
2341 case DW_OP_regval_type:
2342 case DW_OP_GNU_regval_type:
2343 {
2344 unsigned r = val1->v.val_unsigned;
2345 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2346 gcc_assert (o);
2347 if (for_eh_or_skip >= 0)
2348 {
2349 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2350 gcc_assert (size_of_uleb128 (r)
2351 == size_of_uleb128 (val1->v.val_unsigned));
2352 }
2353 dw2_asm_output_data_uleb128 (r, NULL);
2354 dw2_asm_output_data_uleb128 (o, NULL);
2355 }
2356 break;
2357 case DW_OP_deref_type:
2358 case DW_OP_GNU_deref_type:
2359 {
2360 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2361 gcc_assert (o);
2362 dw2_asm_output_data (1, val1->v.val_int, NULL);
2363 dw2_asm_output_data_uleb128 (o, NULL);
2364 }
2365 break;
2366 case DW_OP_convert:
2367 case DW_OP_reinterpret:
2368 case DW_OP_GNU_convert:
2369 case DW_OP_GNU_reinterpret:
2370 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2371 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2372 else
2373 {
2374 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2375 gcc_assert (o);
2376 dw2_asm_output_data_uleb128 (o, NULL);
2377 }
2378 break;
2379
2380 case DW_OP_GNU_parameter_ref:
2381 {
2382 unsigned long o;
2383 gcc_assert (val1->val_class == dw_val_class_die_ref);
2384 o = get_ref_die_offset (val1->v.val_die_ref.die);
2385 dw2_asm_output_data (4, o, NULL);
2386 }
2387 break;
2388
2389 default:
2390 /* Other codes have no operands. */
2391 break;
2392 }
2393 }
2394
2395 /* Output a sequence of location operations.
2396 The for_eh_or_skip parameter controls whether register numbers are
2397 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2398 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2399 info). This should be suppressed for the cases that have not been converted
2400 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2401
2402 void
2403 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2404 {
2405 for (; loc != NULL; loc = loc->dw_loc_next)
2406 {
2407 enum dwarf_location_atom opc = loc->dw_loc_opc;
2408 /* Output the opcode. */
2409 if (for_eh_or_skip >= 0
2410 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2411 {
2412 unsigned r = (opc - DW_OP_breg0);
2413 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2414 gcc_assert (r <= 31);
2415 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2416 }
2417 else if (for_eh_or_skip >= 0
2418 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2419 {
2420 unsigned r = (opc - DW_OP_reg0);
2421 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2422 gcc_assert (r <= 31);
2423 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2424 }
2425
2426 dw2_asm_output_data (1, opc,
2427 "%s", dwarf_stack_op_name (opc));
2428
2429 /* Output the operand(s) (if any). */
2430 output_loc_operands (loc, for_eh_or_skip);
2431 }
2432 }
2433
2434 /* Output location description stack opcode's operands (if any).
2435 The output is single bytes on a line, suitable for .cfi_escape. */
2436
2437 static void
2438 output_loc_operands_raw (dw_loc_descr_ref loc)
2439 {
2440 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2441 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2442
2443 switch (loc->dw_loc_opc)
2444 {
2445 case DW_OP_addr:
2446 case DW_OP_GNU_addr_index:
2447 case DW_OP_GNU_const_index:
2448 case DW_OP_implicit_value:
2449 /* We cannot output addresses in .cfi_escape, only bytes. */
2450 gcc_unreachable ();
2451
2452 case DW_OP_const1u:
2453 case DW_OP_const1s:
2454 case DW_OP_pick:
2455 case DW_OP_deref_size:
2456 case DW_OP_xderef_size:
2457 fputc (',', asm_out_file);
2458 dw2_asm_output_data_raw (1, val1->v.val_int);
2459 break;
2460
2461 case DW_OP_const2u:
2462 case DW_OP_const2s:
2463 fputc (',', asm_out_file);
2464 dw2_asm_output_data_raw (2, val1->v.val_int);
2465 break;
2466
2467 case DW_OP_const4u:
2468 case DW_OP_const4s:
2469 fputc (',', asm_out_file);
2470 dw2_asm_output_data_raw (4, val1->v.val_int);
2471 break;
2472
2473 case DW_OP_const8u:
2474 case DW_OP_const8s:
2475 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2476 fputc (',', asm_out_file);
2477 dw2_asm_output_data_raw (8, val1->v.val_int);
2478 break;
2479
2480 case DW_OP_skip:
2481 case DW_OP_bra:
2482 {
2483 int offset;
2484
2485 gcc_assert (val1->val_class == dw_val_class_loc);
2486 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2487
2488 fputc (',', asm_out_file);
2489 dw2_asm_output_data_raw (2, offset);
2490 }
2491 break;
2492
2493 case DW_OP_regx:
2494 {
2495 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2496 gcc_assert (size_of_uleb128 (r)
2497 == size_of_uleb128 (val1->v.val_unsigned));
2498 fputc (',', asm_out_file);
2499 dw2_asm_output_data_uleb128_raw (r);
2500 }
2501 break;
2502
2503 case DW_OP_constu:
2504 case DW_OP_plus_uconst:
2505 case DW_OP_piece:
2506 fputc (',', asm_out_file);
2507 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2508 break;
2509
2510 case DW_OP_bit_piece:
2511 fputc (',', asm_out_file);
2512 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2513 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2514 break;
2515
2516 case DW_OP_consts:
2517 case DW_OP_breg0:
2518 case DW_OP_breg1:
2519 case DW_OP_breg2:
2520 case DW_OP_breg3:
2521 case DW_OP_breg4:
2522 case DW_OP_breg5:
2523 case DW_OP_breg6:
2524 case DW_OP_breg7:
2525 case DW_OP_breg8:
2526 case DW_OP_breg9:
2527 case DW_OP_breg10:
2528 case DW_OP_breg11:
2529 case DW_OP_breg12:
2530 case DW_OP_breg13:
2531 case DW_OP_breg14:
2532 case DW_OP_breg15:
2533 case DW_OP_breg16:
2534 case DW_OP_breg17:
2535 case DW_OP_breg18:
2536 case DW_OP_breg19:
2537 case DW_OP_breg20:
2538 case DW_OP_breg21:
2539 case DW_OP_breg22:
2540 case DW_OP_breg23:
2541 case DW_OP_breg24:
2542 case DW_OP_breg25:
2543 case DW_OP_breg26:
2544 case DW_OP_breg27:
2545 case DW_OP_breg28:
2546 case DW_OP_breg29:
2547 case DW_OP_breg30:
2548 case DW_OP_breg31:
2549 case DW_OP_fbreg:
2550 fputc (',', asm_out_file);
2551 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2552 break;
2553
2554 case DW_OP_bregx:
2555 {
2556 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2557 gcc_assert (size_of_uleb128 (r)
2558 == size_of_uleb128 (val1->v.val_unsigned));
2559 fputc (',', asm_out_file);
2560 dw2_asm_output_data_uleb128_raw (r);
2561 fputc (',', asm_out_file);
2562 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2563 }
2564 break;
2565
2566 case DW_OP_implicit_pointer:
2567 case DW_OP_entry_value:
2568 case DW_OP_const_type:
2569 case DW_OP_regval_type:
2570 case DW_OP_deref_type:
2571 case DW_OP_convert:
2572 case DW_OP_reinterpret:
2573 case DW_OP_GNU_implicit_pointer:
2574 case DW_OP_GNU_entry_value:
2575 case DW_OP_GNU_const_type:
2576 case DW_OP_GNU_regval_type:
2577 case DW_OP_GNU_deref_type:
2578 case DW_OP_GNU_convert:
2579 case DW_OP_GNU_reinterpret:
2580 case DW_OP_GNU_parameter_ref:
2581 gcc_unreachable ();
2582 break;
2583
2584 default:
2585 /* Other codes have no operands. */
2586 break;
2587 }
2588 }
2589
2590 void
2591 output_loc_sequence_raw (dw_loc_descr_ref loc)
2592 {
2593 while (1)
2594 {
2595 enum dwarf_location_atom opc = loc->dw_loc_opc;
2596 /* Output the opcode. */
2597 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2598 {
2599 unsigned r = (opc - DW_OP_breg0);
2600 r = DWARF2_FRAME_REG_OUT (r, 1);
2601 gcc_assert (r <= 31);
2602 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2603 }
2604 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2605 {
2606 unsigned r = (opc - DW_OP_reg0);
2607 r = DWARF2_FRAME_REG_OUT (r, 1);
2608 gcc_assert (r <= 31);
2609 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2610 }
2611 /* Output the opcode. */
2612 fprintf (asm_out_file, "%#x", opc);
2613 output_loc_operands_raw (loc);
2614
2615 if (!loc->dw_loc_next)
2616 break;
2617 loc = loc->dw_loc_next;
2618
2619 fputc (',', asm_out_file);
2620 }
2621 }
2622
2623 /* This function builds a dwarf location descriptor sequence from a
2624 dw_cfa_location, adding the given OFFSET to the result of the
2625 expression. */
2626
2627 struct dw_loc_descr_node *
2628 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2629 {
2630 struct dw_loc_descr_node *head, *tmp;
2631
2632 offset += cfa->offset;
2633
2634 if (cfa->indirect)
2635 {
2636 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2637 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2638 head->dw_loc_oprnd1.val_entry = NULL;
2639 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2640 add_loc_descr (&head, tmp);
2641 if (offset != 0)
2642 {
2643 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2644 add_loc_descr (&head, tmp);
2645 }
2646 }
2647 else
2648 head = new_reg_loc_descr (cfa->reg, offset);
2649
2650 return head;
2651 }
2652
2653 /* This function builds a dwarf location descriptor sequence for
2654 the address at OFFSET from the CFA when stack is aligned to
2655 ALIGNMENT byte. */
2656
2657 struct dw_loc_descr_node *
2658 build_cfa_aligned_loc (dw_cfa_location *cfa,
2659 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2660 {
2661 struct dw_loc_descr_node *head;
2662 unsigned int dwarf_fp
2663 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2664
2665 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2666 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2667 {
2668 head = new_reg_loc_descr (dwarf_fp, 0);
2669 add_loc_descr (&head, int_loc_descriptor (alignment));
2670 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2671 loc_descr_plus_const (&head, offset);
2672 }
2673 else
2674 head = new_reg_loc_descr (dwarf_fp, offset);
2675 return head;
2676 }
2677 \f
2678 /* And now, the support for symbolic debugging information. */
2679
2680 /* .debug_str support. */
2681
2682 static void dwarf2out_init (const char *);
2683 static void dwarf2out_finish (const char *);
2684 static void dwarf2out_early_finish (const char *);
2685 static void dwarf2out_assembly_start (void);
2686 static void dwarf2out_define (unsigned int, const char *);
2687 static void dwarf2out_undef (unsigned int, const char *);
2688 static void dwarf2out_start_source_file (unsigned, const char *);
2689 static void dwarf2out_end_source_file (unsigned);
2690 static void dwarf2out_function_decl (tree);
2691 static void dwarf2out_begin_block (unsigned, unsigned);
2692 static void dwarf2out_end_block (unsigned, unsigned);
2693 static bool dwarf2out_ignore_block (const_tree);
2694 static void dwarf2out_early_global_decl (tree);
2695 static void dwarf2out_late_global_decl (tree);
2696 static void dwarf2out_type_decl (tree, int);
2697 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2698 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2699 dw_die_ref);
2700 static void dwarf2out_abstract_function (tree);
2701 static void dwarf2out_var_location (rtx_insn *);
2702 static void dwarf2out_size_function (tree);
2703 static void dwarf2out_begin_function (tree);
2704 static void dwarf2out_end_function (unsigned int);
2705 static void dwarf2out_register_main_translation_unit (tree unit);
2706 static void dwarf2out_set_name (tree, tree);
2707 static void dwarf2out_register_external_die (tree decl, const char *sym,
2708 unsigned HOST_WIDE_INT off);
2709 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2710 unsigned HOST_WIDE_INT *off);
2711
2712 /* The debug hooks structure. */
2713
2714 const struct gcc_debug_hooks dwarf2_debug_hooks =
2715 {
2716 dwarf2out_init,
2717 dwarf2out_finish,
2718 dwarf2out_early_finish,
2719 dwarf2out_assembly_start,
2720 dwarf2out_define,
2721 dwarf2out_undef,
2722 dwarf2out_start_source_file,
2723 dwarf2out_end_source_file,
2724 dwarf2out_begin_block,
2725 dwarf2out_end_block,
2726 dwarf2out_ignore_block,
2727 dwarf2out_source_line,
2728 dwarf2out_begin_prologue,
2729 #if VMS_DEBUGGING_INFO
2730 dwarf2out_vms_end_prologue,
2731 dwarf2out_vms_begin_epilogue,
2732 #else
2733 debug_nothing_int_charstar,
2734 debug_nothing_int_charstar,
2735 #endif
2736 dwarf2out_end_epilogue,
2737 dwarf2out_begin_function,
2738 dwarf2out_end_function, /* end_function */
2739 dwarf2out_register_main_translation_unit,
2740 dwarf2out_function_decl, /* function_decl */
2741 dwarf2out_early_global_decl,
2742 dwarf2out_late_global_decl,
2743 dwarf2out_type_decl, /* type_decl */
2744 dwarf2out_imported_module_or_decl,
2745 dwarf2out_die_ref_for_decl,
2746 dwarf2out_register_external_die,
2747 debug_nothing_tree, /* deferred_inline_function */
2748 /* The DWARF 2 backend tries to reduce debugging bloat by not
2749 emitting the abstract description of inline functions until
2750 something tries to reference them. */
2751 dwarf2out_abstract_function, /* outlining_inline_function */
2752 debug_nothing_rtx_code_label, /* label */
2753 debug_nothing_int, /* handle_pch */
2754 dwarf2out_var_location,
2755 dwarf2out_size_function, /* size_function */
2756 dwarf2out_switch_text_section,
2757 dwarf2out_set_name,
2758 1, /* start_end_main_source_file */
2759 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2760 };
2761
2762 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2763 {
2764 dwarf2out_init,
2765 debug_nothing_charstar,
2766 debug_nothing_charstar,
2767 dwarf2out_assembly_start,
2768 debug_nothing_int_charstar,
2769 debug_nothing_int_charstar,
2770 debug_nothing_int_charstar,
2771 debug_nothing_int,
2772 debug_nothing_int_int, /* begin_block */
2773 debug_nothing_int_int, /* end_block */
2774 debug_true_const_tree, /* ignore_block */
2775 dwarf2out_source_line, /* source_line */
2776 debug_nothing_int_int_charstar, /* begin_prologue */
2777 debug_nothing_int_charstar, /* end_prologue */
2778 debug_nothing_int_charstar, /* begin_epilogue */
2779 debug_nothing_int_charstar, /* end_epilogue */
2780 debug_nothing_tree, /* begin_function */
2781 debug_nothing_int, /* end_function */
2782 debug_nothing_tree, /* register_main_translation_unit */
2783 debug_nothing_tree, /* function_decl */
2784 debug_nothing_tree, /* early_global_decl */
2785 debug_nothing_tree, /* late_global_decl */
2786 debug_nothing_tree_int, /* type_decl */
2787 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2788 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2789 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2790 debug_nothing_tree, /* deferred_inline_function */
2791 debug_nothing_tree, /* outlining_inline_function */
2792 debug_nothing_rtx_code_label, /* label */
2793 debug_nothing_int, /* handle_pch */
2794 debug_nothing_rtx_insn, /* var_location */
2795 debug_nothing_tree, /* size_function */
2796 debug_nothing_void, /* switch_text_section */
2797 debug_nothing_tree_tree, /* set_name */
2798 0, /* start_end_main_source_file */
2799 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2800 };
2801 \f
2802 /* NOTE: In the comments in this file, many references are made to
2803 "Debugging Information Entries". This term is abbreviated as `DIE'
2804 throughout the remainder of this file. */
2805
2806 /* An internal representation of the DWARF output is built, and then
2807 walked to generate the DWARF debugging info. The walk of the internal
2808 representation is done after the entire program has been compiled.
2809 The types below are used to describe the internal representation. */
2810
2811 /* Whether to put type DIEs into their own section .debug_types instead
2812 of making them part of the .debug_info section. Only supported for
2813 Dwarf V4 or higher and the user didn't disable them through
2814 -fno-debug-types-section. It is more efficient to put them in a
2815 separate comdat sections since the linker will then be able to
2816 remove duplicates. But not all tools support .debug_types sections
2817 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2818 it is DW_UT_type unit type in .debug_info section. */
2819
2820 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2821
2822 /* Various DIE's use offsets relative to the beginning of the
2823 .debug_info section to refer to each other. */
2824
2825 typedef long int dw_offset;
2826
2827 struct comdat_type_node;
2828
2829 /* The entries in the line_info table more-or-less mirror the opcodes
2830 that are used in the real dwarf line table. Arrays of these entries
2831 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2832 supported. */
2833
2834 enum dw_line_info_opcode {
2835 /* Emit DW_LNE_set_address; the operand is the label index. */
2836 LI_set_address,
2837
2838 /* Emit a row to the matrix with the given line. This may be done
2839 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2840 special opcodes. */
2841 LI_set_line,
2842
2843 /* Emit a DW_LNS_set_file. */
2844 LI_set_file,
2845
2846 /* Emit a DW_LNS_set_column. */
2847 LI_set_column,
2848
2849 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2850 LI_negate_stmt,
2851
2852 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2853 LI_set_prologue_end,
2854 LI_set_epilogue_begin,
2855
2856 /* Emit a DW_LNE_set_discriminator. */
2857 LI_set_discriminator
2858 };
2859
2860 typedef struct GTY(()) dw_line_info_struct {
2861 enum dw_line_info_opcode opcode;
2862 unsigned int val;
2863 } dw_line_info_entry;
2864
2865
2866 struct GTY(()) dw_line_info_table {
2867 /* The label that marks the end of this section. */
2868 const char *end_label;
2869
2870 /* The values for the last row of the matrix, as collected in the table.
2871 These are used to minimize the changes to the next row. */
2872 unsigned int file_num;
2873 unsigned int line_num;
2874 unsigned int column_num;
2875 int discrim_num;
2876 bool is_stmt;
2877 bool in_use;
2878
2879 vec<dw_line_info_entry, va_gc> *entries;
2880 };
2881
2882
2883 /* Each DIE attribute has a field specifying the attribute kind,
2884 a link to the next attribute in the chain, and an attribute value.
2885 Attributes are typically linked below the DIE they modify. */
2886
2887 typedef struct GTY(()) dw_attr_struct {
2888 enum dwarf_attribute dw_attr;
2889 dw_val_node dw_attr_val;
2890 }
2891 dw_attr_node;
2892
2893
2894 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2895 The children of each node form a circular list linked by
2896 die_sib. die_child points to the node *before* the "first" child node. */
2897
2898 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2899 union die_symbol_or_type_node
2900 {
2901 const char * GTY ((tag ("0"))) die_symbol;
2902 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2903 }
2904 GTY ((desc ("%0.comdat_type_p"))) die_id;
2905 vec<dw_attr_node, va_gc> *die_attr;
2906 dw_die_ref die_parent;
2907 dw_die_ref die_child;
2908 dw_die_ref die_sib;
2909 dw_die_ref die_definition; /* ref from a specification to its definition */
2910 dw_offset die_offset;
2911 unsigned long die_abbrev;
2912 int die_mark;
2913 unsigned int decl_id;
2914 enum dwarf_tag die_tag;
2915 /* Die is used and must not be pruned as unused. */
2916 BOOL_BITFIELD die_perennial_p : 1;
2917 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2918 /* For an external ref to die_symbol if die_offset contains an extra
2919 offset to that symbol. */
2920 BOOL_BITFIELD with_offset : 1;
2921 /* Whether this DIE was removed from the DIE tree, for example via
2922 prune_unused_types. We don't consider those present from the
2923 DIE lookup routines. */
2924 BOOL_BITFIELD removed : 1;
2925 /* Lots of spare bits. */
2926 }
2927 die_node;
2928
2929 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2930 static bool early_dwarf;
2931 static bool early_dwarf_finished;
2932 struct set_early_dwarf {
2933 bool saved;
2934 set_early_dwarf () : saved(early_dwarf)
2935 {
2936 gcc_assert (! early_dwarf_finished);
2937 early_dwarf = true;
2938 }
2939 ~set_early_dwarf () { early_dwarf = saved; }
2940 };
2941
2942 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2943 #define FOR_EACH_CHILD(die, c, expr) do { \
2944 c = die->die_child; \
2945 if (c) do { \
2946 c = c->die_sib; \
2947 expr; \
2948 } while (c != die->die_child); \
2949 } while (0)
2950
2951 /* The pubname structure */
2952
2953 typedef struct GTY(()) pubname_struct {
2954 dw_die_ref die;
2955 const char *name;
2956 }
2957 pubname_entry;
2958
2959
2960 struct GTY(()) dw_ranges {
2961 const char *label;
2962 /* If this is positive, it's a block number, otherwise it's a
2963 bitwise-negated index into dw_ranges_by_label. */
2964 int num;
2965 /* Index for the range list for DW_FORM_rnglistx. */
2966 unsigned int idx : 31;
2967 /* True if this range might be possibly in a different section
2968 from previous entry. */
2969 unsigned int maybe_new_sec : 1;
2970 };
2971
2972 /* A structure to hold a macinfo entry. */
2973
2974 typedef struct GTY(()) macinfo_struct {
2975 unsigned char code;
2976 unsigned HOST_WIDE_INT lineno;
2977 const char *info;
2978 }
2979 macinfo_entry;
2980
2981
2982 struct GTY(()) dw_ranges_by_label {
2983 const char *begin;
2984 const char *end;
2985 };
2986
2987 /* The comdat type node structure. */
2988 struct GTY(()) comdat_type_node
2989 {
2990 dw_die_ref root_die;
2991 dw_die_ref type_die;
2992 dw_die_ref skeleton_die;
2993 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2994 comdat_type_node *next;
2995 };
2996
2997 /* A list of DIEs for which we can't determine ancestry (parent_die
2998 field) just yet. Later in dwarf2out_finish we will fill in the
2999 missing bits. */
3000 typedef struct GTY(()) limbo_die_struct {
3001 dw_die_ref die;
3002 /* The tree for which this DIE was created. We use this to
3003 determine ancestry later. */
3004 tree created_for;
3005 struct limbo_die_struct *next;
3006 }
3007 limbo_die_node;
3008
3009 typedef struct skeleton_chain_struct
3010 {
3011 dw_die_ref old_die;
3012 dw_die_ref new_die;
3013 struct skeleton_chain_struct *parent;
3014 }
3015 skeleton_chain_node;
3016
3017 /* Define a macro which returns nonzero for a TYPE_DECL which was
3018 implicitly generated for a type.
3019
3020 Note that, unlike the C front-end (which generates a NULL named
3021 TYPE_DECL node for each complete tagged type, each array type,
3022 and each function type node created) the C++ front-end generates
3023 a _named_ TYPE_DECL node for each tagged type node created.
3024 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3025 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3026 front-end, but for each type, tagged or not. */
3027
3028 #define TYPE_DECL_IS_STUB(decl) \
3029 (DECL_NAME (decl) == NULL_TREE \
3030 || (DECL_ARTIFICIAL (decl) \
3031 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3032 /* This is necessary for stub decls that \
3033 appear in nested inline functions. */ \
3034 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3035 && (decl_ultimate_origin (decl) \
3036 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3037
3038 /* Information concerning the compilation unit's programming
3039 language, and compiler version. */
3040
3041 /* Fixed size portion of the DWARF compilation unit header. */
3042 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3043 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3044 + (dwarf_version >= 5 ? 4 : 3))
3045
3046 /* Fixed size portion of the DWARF comdat type unit header. */
3047 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3048 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3049 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3050
3051 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3052 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3053 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3054
3055 /* Fixed size portion of public names info. */
3056 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3057
3058 /* Fixed size portion of the address range info. */
3059 #define DWARF_ARANGES_HEADER_SIZE \
3060 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3061 DWARF2_ADDR_SIZE * 2) \
3062 - DWARF_INITIAL_LENGTH_SIZE)
3063
3064 /* Size of padding portion in the address range info. It must be
3065 aligned to twice the pointer size. */
3066 #define DWARF_ARANGES_PAD_SIZE \
3067 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3068 DWARF2_ADDR_SIZE * 2) \
3069 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3070
3071 /* Use assembler line directives if available. */
3072 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3073 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3074 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3075 #else
3076 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3077 #endif
3078 #endif
3079
3080 /* Minimum line offset in a special line info. opcode.
3081 This value was chosen to give a reasonable range of values. */
3082 #define DWARF_LINE_BASE -10
3083
3084 /* First special line opcode - leave room for the standard opcodes. */
3085 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3086
3087 /* Range of line offsets in a special line info. opcode. */
3088 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3089
3090 /* Flag that indicates the initial value of the is_stmt_start flag.
3091 In the present implementation, we do not mark any lines as
3092 the beginning of a source statement, because that information
3093 is not made available by the GCC front-end. */
3094 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3095
3096 /* Maximum number of operations per instruction bundle. */
3097 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3098 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3099 #endif
3100
3101 /* This location is used by calc_die_sizes() to keep track
3102 the offset of each DIE within the .debug_info section. */
3103 static unsigned long next_die_offset;
3104
3105 /* Record the root of the DIE's built for the current compilation unit. */
3106 static GTY(()) dw_die_ref single_comp_unit_die;
3107
3108 /* A list of type DIEs that have been separated into comdat sections. */
3109 static GTY(()) comdat_type_node *comdat_type_list;
3110
3111 /* A list of CU DIEs that have been separated. */
3112 static GTY(()) limbo_die_node *cu_die_list;
3113
3114 /* A list of DIEs with a NULL parent waiting to be relocated. */
3115 static GTY(()) limbo_die_node *limbo_die_list;
3116
3117 /* A list of DIEs for which we may have to generate
3118 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3119 static GTY(()) limbo_die_node *deferred_asm_name;
3120
3121 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3122 {
3123 typedef const char *compare_type;
3124
3125 static hashval_t hash (dwarf_file_data *);
3126 static bool equal (dwarf_file_data *, const char *);
3127 };
3128
3129 /* Filenames referenced by this compilation unit. */
3130 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3131
3132 struct decl_die_hasher : ggc_ptr_hash<die_node>
3133 {
3134 typedef tree compare_type;
3135
3136 static hashval_t hash (die_node *);
3137 static bool equal (die_node *, tree);
3138 };
3139 /* A hash table of references to DIE's that describe declarations.
3140 The key is a DECL_UID() which is a unique number identifying each decl. */
3141 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3142
3143 struct GTY ((for_user)) variable_value_struct {
3144 unsigned int decl_id;
3145 vec<dw_die_ref, va_gc> *dies;
3146 };
3147
3148 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3149 {
3150 typedef tree compare_type;
3151
3152 static hashval_t hash (variable_value_struct *);
3153 static bool equal (variable_value_struct *, tree);
3154 };
3155 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3156 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3157 DECL_CONTEXT of the referenced VAR_DECLs. */
3158 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3159
3160 struct block_die_hasher : ggc_ptr_hash<die_struct>
3161 {
3162 static hashval_t hash (die_struct *);
3163 static bool equal (die_struct *, die_struct *);
3164 };
3165
3166 /* A hash table of references to DIE's that describe COMMON blocks.
3167 The key is DECL_UID() ^ die_parent. */
3168 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3169
3170 typedef struct GTY(()) die_arg_entry_struct {
3171 dw_die_ref die;
3172 tree arg;
3173 } die_arg_entry;
3174
3175
3176 /* Node of the variable location list. */
3177 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3178 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3179 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3180 in mode of the EXPR_LIST node and first EXPR_LIST operand
3181 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3182 location or NULL for padding. For larger bitsizes,
3183 mode is 0 and first operand is a CONCAT with bitsize
3184 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3185 NULL as second operand. */
3186 rtx GTY (()) loc;
3187 const char * GTY (()) label;
3188 struct var_loc_node * GTY (()) next;
3189 };
3190
3191 /* Variable location list. */
3192 struct GTY ((for_user)) var_loc_list_def {
3193 struct var_loc_node * GTY (()) first;
3194
3195 /* Pointer to the last but one or last element of the
3196 chained list. If the list is empty, both first and
3197 last are NULL, if the list contains just one node
3198 or the last node certainly is not redundant, it points
3199 to the last node, otherwise points to the last but one.
3200 Do not mark it for GC because it is marked through the chain. */
3201 struct var_loc_node * GTY ((skip ("%h"))) last;
3202
3203 /* Pointer to the last element before section switch,
3204 if NULL, either sections weren't switched or first
3205 is after section switch. */
3206 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3207
3208 /* DECL_UID of the variable decl. */
3209 unsigned int decl_id;
3210 };
3211 typedef struct var_loc_list_def var_loc_list;
3212
3213 /* Call argument location list. */
3214 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3215 rtx GTY (()) call_arg_loc_note;
3216 const char * GTY (()) label;
3217 tree GTY (()) block;
3218 bool tail_call_p;
3219 rtx GTY (()) symbol_ref;
3220 struct call_arg_loc_node * GTY (()) next;
3221 };
3222
3223
3224 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3225 {
3226 typedef const_tree compare_type;
3227
3228 static hashval_t hash (var_loc_list *);
3229 static bool equal (var_loc_list *, const_tree);
3230 };
3231
3232 /* Table of decl location linked lists. */
3233 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3234
3235 /* Head and tail of call_arg_loc chain. */
3236 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3237 static struct call_arg_loc_node *call_arg_loc_last;
3238
3239 /* Number of call sites in the current function. */
3240 static int call_site_count = -1;
3241 /* Number of tail call sites in the current function. */
3242 static int tail_call_site_count = -1;
3243
3244 /* A cached location list. */
3245 struct GTY ((for_user)) cached_dw_loc_list_def {
3246 /* The DECL_UID of the decl that this entry describes. */
3247 unsigned int decl_id;
3248
3249 /* The cached location list. */
3250 dw_loc_list_ref loc_list;
3251 };
3252 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3253
3254 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3255 {
3256
3257 typedef const_tree compare_type;
3258
3259 static hashval_t hash (cached_dw_loc_list *);
3260 static bool equal (cached_dw_loc_list *, const_tree);
3261 };
3262
3263 /* Table of cached location lists. */
3264 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3265
3266 /* A vector of references to DIE's that are uniquely identified by their tag,
3267 presence/absence of children DIE's, and list of attribute/value pairs. */
3268 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3269
3270 /* A hash map to remember the stack usage for DWARF procedures. The value
3271 stored is the stack size difference between before the DWARF procedure
3272 invokation and after it returned. In other words, for a DWARF procedure
3273 that consumes N stack slots and that pushes M ones, this stores M - N. */
3274 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3275
3276 /* A global counter for generating labels for line number data. */
3277 static unsigned int line_info_label_num;
3278
3279 /* The current table to which we should emit line number information
3280 for the current function. This will be set up at the beginning of
3281 assembly for the function. */
3282 static GTY(()) dw_line_info_table *cur_line_info_table;
3283
3284 /* The two default tables of line number info. */
3285 static GTY(()) dw_line_info_table *text_section_line_info;
3286 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3287
3288 /* The set of all non-default tables of line number info. */
3289 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3290
3291 /* A flag to tell pubnames/types export if there is an info section to
3292 refer to. */
3293 static bool info_section_emitted;
3294
3295 /* A pointer to the base of a table that contains a list of publicly
3296 accessible names. */
3297 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3298
3299 /* A pointer to the base of a table that contains a list of publicly
3300 accessible types. */
3301 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3302
3303 /* A pointer to the base of a table that contains a list of macro
3304 defines/undefines (and file start/end markers). */
3305 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3306
3307 /* True if .debug_macinfo or .debug_macros section is going to be
3308 emitted. */
3309 #define have_macinfo \
3310 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3311 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3312 && !macinfo_table->is_empty ())
3313
3314 /* Vector of dies for which we should generate .debug_ranges info. */
3315 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3316
3317 /* Vector of pairs of labels referenced in ranges_table. */
3318 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3319
3320 /* Whether we have location lists that need outputting */
3321 static GTY(()) bool have_location_lists;
3322
3323 /* Unique label counter. */
3324 static GTY(()) unsigned int loclabel_num;
3325
3326 /* Unique label counter for point-of-call tables. */
3327 static GTY(()) unsigned int poc_label_num;
3328
3329 /* The last file entry emitted by maybe_emit_file(). */
3330 static GTY(()) struct dwarf_file_data * last_emitted_file;
3331
3332 /* Number of internal labels generated by gen_internal_sym(). */
3333 static GTY(()) int label_num;
3334
3335 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3336
3337 /* Instances of generic types for which we need to generate debug
3338 info that describe their generic parameters and arguments. That
3339 generation needs to happen once all types are properly laid out so
3340 we do it at the end of compilation. */
3341 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3342
3343 /* Offset from the "steady-state frame pointer" to the frame base,
3344 within the current function. */
3345 static HOST_WIDE_INT frame_pointer_fb_offset;
3346 static bool frame_pointer_fb_offset_valid;
3347
3348 static vec<dw_die_ref> base_types;
3349
3350 /* Flags to represent a set of attribute classes for attributes that represent
3351 a scalar value (bounds, pointers, ...). */
3352 enum dw_scalar_form
3353 {
3354 dw_scalar_form_constant = 0x01,
3355 dw_scalar_form_exprloc = 0x02,
3356 dw_scalar_form_reference = 0x04
3357 };
3358
3359 /* Forward declarations for functions defined in this file. */
3360
3361 static int is_pseudo_reg (const_rtx);
3362 static tree type_main_variant (tree);
3363 static int is_tagged_type (const_tree);
3364 static const char *dwarf_tag_name (unsigned);
3365 static const char *dwarf_attr_name (unsigned);
3366 static const char *dwarf_form_name (unsigned);
3367 static tree decl_ultimate_origin (const_tree);
3368 static tree decl_class_context (tree);
3369 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3370 static inline enum dw_val_class AT_class (dw_attr_node *);
3371 static inline unsigned int AT_index (dw_attr_node *);
3372 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3373 static inline unsigned AT_flag (dw_attr_node *);
3374 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3375 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3376 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3377 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3378 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3379 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3380 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3381 unsigned int, unsigned char *);
3382 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3383 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3384 static inline const char *AT_string (dw_attr_node *);
3385 static enum dwarf_form AT_string_form (dw_attr_node *);
3386 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3387 static void add_AT_specification (dw_die_ref, dw_die_ref);
3388 static inline dw_die_ref AT_ref (dw_attr_node *);
3389 static inline int AT_ref_external (dw_attr_node *);
3390 static inline void set_AT_ref_external (dw_attr_node *, int);
3391 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3392 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3393 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3394 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3395 dw_loc_list_ref);
3396 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3397 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3398 static void remove_addr_table_entry (addr_table_entry *);
3399 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3400 static inline rtx AT_addr (dw_attr_node *);
3401 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3402 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3403 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3404 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3405 const char *);
3406 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3407 unsigned HOST_WIDE_INT);
3408 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3409 unsigned long, bool);
3410 static inline const char *AT_lbl (dw_attr_node *);
3411 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3412 static const char *get_AT_low_pc (dw_die_ref);
3413 static const char *get_AT_hi_pc (dw_die_ref);
3414 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3415 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3416 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3417 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3418 static bool is_cxx (void);
3419 static bool is_cxx (const_tree);
3420 static bool is_fortran (void);
3421 static bool is_ada (void);
3422 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3423 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3424 static void add_child_die (dw_die_ref, dw_die_ref);
3425 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3426 static dw_die_ref lookup_type_die (tree);
3427 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3428 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3429 static void equate_type_number_to_die (tree, dw_die_ref);
3430 static dw_die_ref lookup_decl_die (tree);
3431 static var_loc_list *lookup_decl_loc (const_tree);
3432 static void equate_decl_number_to_die (tree, dw_die_ref);
3433 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3434 static void print_spaces (FILE *);
3435 static void print_die (dw_die_ref, FILE *);
3436 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3437 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3438 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3439 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3440 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3441 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3442 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3443 struct md5_ctx *, int *);
3444 struct checksum_attributes;
3445 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3446 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3447 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3448 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3449 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3450 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3451 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3452 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3453 static int is_type_die (dw_die_ref);
3454 static int is_comdat_die (dw_die_ref);
3455 static inline bool is_template_instantiation (dw_die_ref);
3456 static int is_declaration_die (dw_die_ref);
3457 static int should_move_die_to_comdat (dw_die_ref);
3458 static dw_die_ref clone_as_declaration (dw_die_ref);
3459 static dw_die_ref clone_die (dw_die_ref);
3460 static dw_die_ref clone_tree (dw_die_ref);
3461 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3462 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3463 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3464 static dw_die_ref generate_skeleton (dw_die_ref);
3465 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3466 dw_die_ref,
3467 dw_die_ref);
3468 static void break_out_comdat_types (dw_die_ref);
3469 static void copy_decls_for_unworthy_types (dw_die_ref);
3470
3471 static void add_sibling_attributes (dw_die_ref);
3472 static void output_location_lists (dw_die_ref);
3473 static int constant_size (unsigned HOST_WIDE_INT);
3474 static unsigned long size_of_die (dw_die_ref);
3475 static void calc_die_sizes (dw_die_ref);
3476 static void calc_base_type_die_sizes (void);
3477 static void mark_dies (dw_die_ref);
3478 static void unmark_dies (dw_die_ref);
3479 static void unmark_all_dies (dw_die_ref);
3480 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3481 static unsigned long size_of_aranges (void);
3482 static enum dwarf_form value_format (dw_attr_node *);
3483 static void output_value_format (dw_attr_node *);
3484 static void output_abbrev_section (void);
3485 static void output_die_abbrevs (unsigned long, dw_die_ref);
3486 static void output_die (dw_die_ref);
3487 static void output_compilation_unit_header (enum dwarf_unit_type);
3488 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3489 static void output_comdat_type_unit (comdat_type_node *);
3490 static const char *dwarf2_name (tree, int);
3491 static void add_pubname (tree, dw_die_ref);
3492 static void add_enumerator_pubname (const char *, dw_die_ref);
3493 static void add_pubname_string (const char *, dw_die_ref);
3494 static void add_pubtype (tree, dw_die_ref);
3495 static void output_pubnames (vec<pubname_entry, va_gc> *);
3496 static void output_aranges (void);
3497 static unsigned int add_ranges (const_tree, bool = false);
3498 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3499 bool *, bool);
3500 static void output_ranges (void);
3501 static dw_line_info_table *new_line_info_table (void);
3502 static void output_line_info (bool);
3503 static void output_file_names (void);
3504 static dw_die_ref base_type_die (tree, bool);
3505 static int is_base_type (tree);
3506 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3507 static int decl_quals (const_tree);
3508 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3509 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3510 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3511 static int type_is_enum (const_tree);
3512 static unsigned int dbx_reg_number (const_rtx);
3513 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3514 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3515 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3516 enum var_init_status);
3517 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3518 enum var_init_status);
3519 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3520 enum var_init_status);
3521 static int is_based_loc (const_rtx);
3522 static bool resolve_one_addr (rtx *);
3523 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3524 enum var_init_status);
3525 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3526 enum var_init_status);
3527 struct loc_descr_context;
3528 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3529 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3530 static dw_loc_list_ref loc_list_from_tree (tree, int,
3531 struct loc_descr_context *);
3532 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3533 struct loc_descr_context *);
3534 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3535 static tree field_type (const_tree);
3536 static unsigned int simple_type_align_in_bits (const_tree);
3537 static unsigned int simple_decl_align_in_bits (const_tree);
3538 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3539 struct vlr_context;
3540 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3541 HOST_WIDE_INT *);
3542 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3543 dw_loc_list_ref);
3544 static void add_data_member_location_attribute (dw_die_ref, tree,
3545 struct vlr_context *);
3546 static bool add_const_value_attribute (dw_die_ref, rtx);
3547 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3548 static void insert_wide_int (const wide_int &, unsigned char *, int);
3549 static void insert_float (const_rtx, unsigned char *);
3550 static rtx rtl_for_decl_location (tree);
3551 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3552 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3553 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3554 static void add_name_attribute (dw_die_ref, const char *);
3555 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3556 static void add_comp_dir_attribute (dw_die_ref);
3557 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3558 struct loc_descr_context *);
3559 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3560 struct loc_descr_context *);
3561 static void add_subscript_info (dw_die_ref, tree, bool);
3562 static void add_byte_size_attribute (dw_die_ref, tree);
3563 static void add_alignment_attribute (dw_die_ref, tree);
3564 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3565 struct vlr_context *);
3566 static void add_bit_size_attribute (dw_die_ref, tree);
3567 static void add_prototyped_attribute (dw_die_ref, tree);
3568 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3569 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3570 static void add_src_coords_attributes (dw_die_ref, tree);
3571 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3572 static void add_discr_value (dw_die_ref, dw_discr_value *);
3573 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3574 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3575 static void push_decl_scope (tree);
3576 static void pop_decl_scope (void);
3577 static dw_die_ref scope_die_for (tree, dw_die_ref);
3578 static inline int local_scope_p (dw_die_ref);
3579 static inline int class_scope_p (dw_die_ref);
3580 static inline int class_or_namespace_scope_p (dw_die_ref);
3581 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3582 static void add_calling_convention_attribute (dw_die_ref, tree);
3583 static const char *type_tag (const_tree);
3584 static tree member_declared_type (const_tree);
3585 #if 0
3586 static const char *decl_start_label (tree);
3587 #endif
3588 static void gen_array_type_die (tree, dw_die_ref);
3589 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3590 #if 0
3591 static void gen_entry_point_die (tree, dw_die_ref);
3592 #endif
3593 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3594 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3595 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3596 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3597 static void gen_formal_types_die (tree, dw_die_ref);
3598 static void gen_subprogram_die (tree, dw_die_ref);
3599 static void gen_variable_die (tree, tree, dw_die_ref);
3600 static void gen_const_die (tree, dw_die_ref);
3601 static void gen_label_die (tree, dw_die_ref);
3602 static void gen_lexical_block_die (tree, dw_die_ref);
3603 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3604 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3605 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3606 static dw_die_ref gen_compile_unit_die (const char *);
3607 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3608 static void gen_member_die (tree, dw_die_ref);
3609 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3610 enum debug_info_usage);
3611 static void gen_subroutine_type_die (tree, dw_die_ref);
3612 static void gen_typedef_die (tree, dw_die_ref);
3613 static void gen_type_die (tree, dw_die_ref);
3614 static void gen_block_die (tree, dw_die_ref);
3615 static void decls_for_scope (tree, dw_die_ref);
3616 static bool is_naming_typedef_decl (const_tree);
3617 static inline dw_die_ref get_context_die (tree);
3618 static void gen_namespace_die (tree, dw_die_ref);
3619 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3620 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3621 static dw_die_ref force_decl_die (tree);
3622 static dw_die_ref force_type_die (tree);
3623 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3624 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3625 static struct dwarf_file_data * lookup_filename (const char *);
3626 static void retry_incomplete_types (void);
3627 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3628 static void gen_generic_params_dies (tree);
3629 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3630 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3631 static void splice_child_die (dw_die_ref, dw_die_ref);
3632 static int file_info_cmp (const void *, const void *);
3633 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3634 const char *, const char *);
3635 static void output_loc_list (dw_loc_list_ref);
3636 static char *gen_internal_sym (const char *);
3637 static bool want_pubnames (void);
3638
3639 static void prune_unmark_dies (dw_die_ref);
3640 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3641 static void prune_unused_types_mark (dw_die_ref, int);
3642 static void prune_unused_types_walk (dw_die_ref);
3643 static void prune_unused_types_walk_attribs (dw_die_ref);
3644 static void prune_unused_types_prune (dw_die_ref);
3645 static void prune_unused_types (void);
3646 static int maybe_emit_file (struct dwarf_file_data *fd);
3647 static inline const char *AT_vms_delta1 (dw_attr_node *);
3648 static inline const char *AT_vms_delta2 (dw_attr_node *);
3649 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3650 const char *, const char *);
3651 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3652 static void gen_remaining_tmpl_value_param_die_attribute (void);
3653 static bool generic_type_p (tree);
3654 static void schedule_generic_params_dies_gen (tree t);
3655 static void gen_scheduled_generic_parms_dies (void);
3656 static void resolve_variable_values (void);
3657
3658 static const char *comp_dir_string (void);
3659
3660 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3661
3662 /* enum for tracking thread-local variables whose address is really an offset
3663 relative to the TLS pointer, which will need link-time relocation, but will
3664 not need relocation by the DWARF consumer. */
3665
3666 enum dtprel_bool
3667 {
3668 dtprel_false = 0,
3669 dtprel_true = 1
3670 };
3671
3672 /* Return the operator to use for an address of a variable. For dtprel_true, we
3673 use DW_OP_const*. For regular variables, which need both link-time
3674 relocation and consumer-level relocation (e.g., to account for shared objects
3675 loaded at a random address), we use DW_OP_addr*. */
3676
3677 static inline enum dwarf_location_atom
3678 dw_addr_op (enum dtprel_bool dtprel)
3679 {
3680 if (dtprel == dtprel_true)
3681 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3682 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3683 else
3684 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3685 }
3686
3687 /* Return a pointer to a newly allocated address location description. If
3688 dwarf_split_debug_info is true, then record the address with the appropriate
3689 relocation. */
3690 static inline dw_loc_descr_ref
3691 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3692 {
3693 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3694
3695 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3696 ref->dw_loc_oprnd1.v.val_addr = addr;
3697 ref->dtprel = dtprel;
3698 if (dwarf_split_debug_info)
3699 ref->dw_loc_oprnd1.val_entry
3700 = add_addr_table_entry (addr,
3701 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3702 else
3703 ref->dw_loc_oprnd1.val_entry = NULL;
3704
3705 return ref;
3706 }
3707
3708 /* Section names used to hold DWARF debugging information. */
3709
3710 #ifndef DEBUG_INFO_SECTION
3711 #define DEBUG_INFO_SECTION ".debug_info"
3712 #endif
3713 #ifndef DEBUG_DWO_INFO_SECTION
3714 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3715 #endif
3716 #ifndef DEBUG_LTO_INFO_SECTION
3717 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3718 #endif
3719 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3720 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3721 #endif
3722 #ifndef DEBUG_ABBREV_SECTION
3723 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3724 #endif
3725 #ifndef DEBUG_LTO_ABBREV_SECTION
3726 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3727 #endif
3728 #ifndef DEBUG_DWO_ABBREV_SECTION
3729 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3730 #endif
3731 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3732 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3733 #endif
3734 #ifndef DEBUG_ARANGES_SECTION
3735 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3736 #endif
3737 #ifndef DEBUG_ADDR_SECTION
3738 #define DEBUG_ADDR_SECTION ".debug_addr"
3739 #endif
3740 #ifndef DEBUG_MACINFO_SECTION
3741 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3742 #endif
3743 #ifndef DEBUG_LTO_MACINFO_SECTION
3744 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3745 #endif
3746 #ifndef DEBUG_DWO_MACINFO_SECTION
3747 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3748 #endif
3749 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3750 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3751 #endif
3752 #ifndef DEBUG_MACRO_SECTION
3753 #define DEBUG_MACRO_SECTION ".debug_macro"
3754 #endif
3755 #ifndef DEBUG_LTO_MACRO_SECTION
3756 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3757 #endif
3758 #ifndef DEBUG_DWO_MACRO_SECTION
3759 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3760 #endif
3761 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3762 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3763 #endif
3764 #ifndef DEBUG_LINE_SECTION
3765 #define DEBUG_LINE_SECTION ".debug_line"
3766 #endif
3767 #ifndef DEBUG_LTO_LINE_SECTION
3768 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3769 #endif
3770 #ifndef DEBUG_DWO_LINE_SECTION
3771 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3772 #endif
3773 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3774 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3775 #endif
3776 #ifndef DEBUG_LOC_SECTION
3777 #define DEBUG_LOC_SECTION ".debug_loc"
3778 #endif
3779 #ifndef DEBUG_DWO_LOC_SECTION
3780 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3781 #endif
3782 #ifndef DEBUG_LOCLISTS_SECTION
3783 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3784 #endif
3785 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3786 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3787 #endif
3788 #ifndef DEBUG_PUBNAMES_SECTION
3789 #define DEBUG_PUBNAMES_SECTION \
3790 ((debug_generate_pub_sections == 2) \
3791 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3792 #endif
3793 #ifndef DEBUG_PUBTYPES_SECTION
3794 #define DEBUG_PUBTYPES_SECTION \
3795 ((debug_generate_pub_sections == 2) \
3796 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3797 #endif
3798 #ifndef DEBUG_STR_OFFSETS_SECTION
3799 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3800 #endif
3801 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3802 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3803 #endif
3804 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3805 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3806 #endif
3807 #ifndef DEBUG_STR_SECTION
3808 #define DEBUG_STR_SECTION ".debug_str"
3809 #endif
3810 #ifndef DEBUG_LTO_STR_SECTION
3811 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3812 #endif
3813 #ifndef DEBUG_STR_DWO_SECTION
3814 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3815 #endif
3816 #ifndef DEBUG_LTO_STR_DWO_SECTION
3817 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3818 #endif
3819 #ifndef DEBUG_RANGES_SECTION
3820 #define DEBUG_RANGES_SECTION ".debug_ranges"
3821 #endif
3822 #ifndef DEBUG_RNGLISTS_SECTION
3823 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3824 #endif
3825 #ifndef DEBUG_LINE_STR_SECTION
3826 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3827 #endif
3828 #ifndef DEBUG_LTO_LINE_STR_SECTION
3829 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3830 #endif
3831
3832 /* Standard ELF section names for compiled code and data. */
3833 #ifndef TEXT_SECTION_NAME
3834 #define TEXT_SECTION_NAME ".text"
3835 #endif
3836
3837 /* Section flags for .debug_str section. */
3838 #define DEBUG_STR_SECTION_FLAGS \
3839 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3840 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3841 : SECTION_DEBUG)
3842
3843 /* Section flags for .debug_str.dwo section. */
3844 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3845
3846 /* Attribute used to refer to the macro section. */
3847 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3848 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3849
3850 /* Labels we insert at beginning sections we can reference instead of
3851 the section names themselves. */
3852
3853 #ifndef TEXT_SECTION_LABEL
3854 #define TEXT_SECTION_LABEL "Ltext"
3855 #endif
3856 #ifndef COLD_TEXT_SECTION_LABEL
3857 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3858 #endif
3859 #ifndef DEBUG_LINE_SECTION_LABEL
3860 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3861 #endif
3862 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3863 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3864 #endif
3865 #ifndef DEBUG_INFO_SECTION_LABEL
3866 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3867 #endif
3868 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3869 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3870 #endif
3871 #ifndef DEBUG_ABBREV_SECTION_LABEL
3872 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3873 #endif
3874 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3875 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3876 #endif
3877 #ifndef DEBUG_ADDR_SECTION_LABEL
3878 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3879 #endif
3880 #ifndef DEBUG_LOC_SECTION_LABEL
3881 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3882 #endif
3883 #ifndef DEBUG_RANGES_SECTION_LABEL
3884 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3885 #endif
3886 #ifndef DEBUG_MACINFO_SECTION_LABEL
3887 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3888 #endif
3889 #ifndef DEBUG_MACRO_SECTION_LABEL
3890 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3891 #endif
3892 #define SKELETON_COMP_DIE_ABBREV 1
3893 #define SKELETON_TYPE_DIE_ABBREV 2
3894
3895 /* Definitions of defaults for formats and names of various special
3896 (artificial) labels which may be generated within this file (when the -g
3897 options is used and DWARF2_DEBUGGING_INFO is in effect.
3898 If necessary, these may be overridden from within the tm.h file, but
3899 typically, overriding these defaults is unnecessary. */
3900
3901 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3902 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3903 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3904 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3905 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3906 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3907 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3908 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3909 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3910 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3911 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3912 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3913 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3914 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3915 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3916
3917 #ifndef TEXT_END_LABEL
3918 #define TEXT_END_LABEL "Letext"
3919 #endif
3920 #ifndef COLD_END_LABEL
3921 #define COLD_END_LABEL "Letext_cold"
3922 #endif
3923 #ifndef BLOCK_BEGIN_LABEL
3924 #define BLOCK_BEGIN_LABEL "LBB"
3925 #endif
3926 #ifndef BLOCK_END_LABEL
3927 #define BLOCK_END_LABEL "LBE"
3928 #endif
3929 #ifndef LINE_CODE_LABEL
3930 #define LINE_CODE_LABEL "LM"
3931 #endif
3932
3933 \f
3934 /* Return the root of the DIE's built for the current compilation unit. */
3935 static dw_die_ref
3936 comp_unit_die (void)
3937 {
3938 if (!single_comp_unit_die)
3939 single_comp_unit_die = gen_compile_unit_die (NULL);
3940 return single_comp_unit_die;
3941 }
3942
3943 /* We allow a language front-end to designate a function that is to be
3944 called to "demangle" any name before it is put into a DIE. */
3945
3946 static const char *(*demangle_name_func) (const char *);
3947
3948 void
3949 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3950 {
3951 demangle_name_func = func;
3952 }
3953
3954 /* Test if rtl node points to a pseudo register. */
3955
3956 static inline int
3957 is_pseudo_reg (const_rtx rtl)
3958 {
3959 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3960 || (GET_CODE (rtl) == SUBREG
3961 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3962 }
3963
3964 /* Return a reference to a type, with its const and volatile qualifiers
3965 removed. */
3966
3967 static inline tree
3968 type_main_variant (tree type)
3969 {
3970 type = TYPE_MAIN_VARIANT (type);
3971
3972 /* ??? There really should be only one main variant among any group of
3973 variants of a given type (and all of the MAIN_VARIANT values for all
3974 members of the group should point to that one type) but sometimes the C
3975 front-end messes this up for array types, so we work around that bug
3976 here. */
3977 if (TREE_CODE (type) == ARRAY_TYPE)
3978 while (type != TYPE_MAIN_VARIANT (type))
3979 type = TYPE_MAIN_VARIANT (type);
3980
3981 return type;
3982 }
3983
3984 /* Return nonzero if the given type node represents a tagged type. */
3985
3986 static inline int
3987 is_tagged_type (const_tree type)
3988 {
3989 enum tree_code code = TREE_CODE (type);
3990
3991 return (code == RECORD_TYPE || code == UNION_TYPE
3992 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3993 }
3994
3995 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3996
3997 static void
3998 get_ref_die_offset_label (char *label, dw_die_ref ref)
3999 {
4000 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4001 }
4002
4003 /* Return die_offset of a DIE reference to a base type. */
4004
4005 static unsigned long int
4006 get_base_type_offset (dw_die_ref ref)
4007 {
4008 if (ref->die_offset)
4009 return ref->die_offset;
4010 if (comp_unit_die ()->die_abbrev)
4011 {
4012 calc_base_type_die_sizes ();
4013 gcc_assert (ref->die_offset);
4014 }
4015 return ref->die_offset;
4016 }
4017
4018 /* Return die_offset of a DIE reference other than base type. */
4019
4020 static unsigned long int
4021 get_ref_die_offset (dw_die_ref ref)
4022 {
4023 gcc_assert (ref->die_offset);
4024 return ref->die_offset;
4025 }
4026
4027 /* Convert a DIE tag into its string name. */
4028
4029 static const char *
4030 dwarf_tag_name (unsigned int tag)
4031 {
4032 const char *name = get_DW_TAG_name (tag);
4033
4034 if (name != NULL)
4035 return name;
4036
4037 return "DW_TAG_<unknown>";
4038 }
4039
4040 /* Convert a DWARF attribute code into its string name. */
4041
4042 static const char *
4043 dwarf_attr_name (unsigned int attr)
4044 {
4045 const char *name;
4046
4047 switch (attr)
4048 {
4049 #if VMS_DEBUGGING_INFO
4050 case DW_AT_HP_prologue:
4051 return "DW_AT_HP_prologue";
4052 #else
4053 case DW_AT_MIPS_loop_unroll_factor:
4054 return "DW_AT_MIPS_loop_unroll_factor";
4055 #endif
4056
4057 #if VMS_DEBUGGING_INFO
4058 case DW_AT_HP_epilogue:
4059 return "DW_AT_HP_epilogue";
4060 #else
4061 case DW_AT_MIPS_stride:
4062 return "DW_AT_MIPS_stride";
4063 #endif
4064 }
4065
4066 name = get_DW_AT_name (attr);
4067
4068 if (name != NULL)
4069 return name;
4070
4071 return "DW_AT_<unknown>";
4072 }
4073
4074 /* Convert a DWARF value form code into its string name. */
4075
4076 static const char *
4077 dwarf_form_name (unsigned int form)
4078 {
4079 const char *name = get_DW_FORM_name (form);
4080
4081 if (name != NULL)
4082 return name;
4083
4084 return "DW_FORM_<unknown>";
4085 }
4086 \f
4087 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4088 instance of an inlined instance of a decl which is local to an inline
4089 function, so we have to trace all of the way back through the origin chain
4090 to find out what sort of node actually served as the original seed for the
4091 given block. */
4092
4093 static tree
4094 decl_ultimate_origin (const_tree decl)
4095 {
4096 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4097 return NULL_TREE;
4098
4099 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4100 we're trying to output the abstract instance of this function. */
4101 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4102 return NULL_TREE;
4103
4104 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4105 most distant ancestor, this should never happen. */
4106 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4107
4108 return DECL_ABSTRACT_ORIGIN (decl);
4109 }
4110
4111 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4112 of a virtual function may refer to a base class, so we check the 'this'
4113 parameter. */
4114
4115 static tree
4116 decl_class_context (tree decl)
4117 {
4118 tree context = NULL_TREE;
4119
4120 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4121 context = DECL_CONTEXT (decl);
4122 else
4123 context = TYPE_MAIN_VARIANT
4124 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4125
4126 if (context && !TYPE_P (context))
4127 context = NULL_TREE;
4128
4129 return context;
4130 }
4131 \f
4132 /* Add an attribute/value pair to a DIE. */
4133
4134 static inline void
4135 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4136 {
4137 /* Maybe this should be an assert? */
4138 if (die == NULL)
4139 return;
4140
4141 if (flag_checking)
4142 {
4143 /* Check we do not add duplicate attrs. Can't use get_AT here
4144 because that recurses to the specification/abstract origin DIE. */
4145 dw_attr_node *a;
4146 unsigned ix;
4147 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4148 gcc_assert (a->dw_attr != attr->dw_attr);
4149 }
4150
4151 vec_safe_reserve (die->die_attr, 1);
4152 vec_safe_push (die->die_attr, *attr);
4153 }
4154
4155 static inline enum dw_val_class
4156 AT_class (dw_attr_node *a)
4157 {
4158 return a->dw_attr_val.val_class;
4159 }
4160
4161 /* Return the index for any attribute that will be referenced with a
4162 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4163 are stored in dw_attr_val.v.val_str for reference counting
4164 pruning. */
4165
4166 static inline unsigned int
4167 AT_index (dw_attr_node *a)
4168 {
4169 if (AT_class (a) == dw_val_class_str)
4170 return a->dw_attr_val.v.val_str->index;
4171 else if (a->dw_attr_val.val_entry != NULL)
4172 return a->dw_attr_val.val_entry->index;
4173 return NOT_INDEXED;
4174 }
4175
4176 /* Add a flag value attribute to a DIE. */
4177
4178 static inline void
4179 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4180 {
4181 dw_attr_node attr;
4182
4183 attr.dw_attr = attr_kind;
4184 attr.dw_attr_val.val_class = dw_val_class_flag;
4185 attr.dw_attr_val.val_entry = NULL;
4186 attr.dw_attr_val.v.val_flag = flag;
4187 add_dwarf_attr (die, &attr);
4188 }
4189
4190 static inline unsigned
4191 AT_flag (dw_attr_node *a)
4192 {
4193 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4194 return a->dw_attr_val.v.val_flag;
4195 }
4196
4197 /* Add a signed integer attribute value to a DIE. */
4198
4199 static inline void
4200 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4201 {
4202 dw_attr_node attr;
4203
4204 attr.dw_attr = attr_kind;
4205 attr.dw_attr_val.val_class = dw_val_class_const;
4206 attr.dw_attr_val.val_entry = NULL;
4207 attr.dw_attr_val.v.val_int = int_val;
4208 add_dwarf_attr (die, &attr);
4209 }
4210
4211 static inline HOST_WIDE_INT
4212 AT_int (dw_attr_node *a)
4213 {
4214 gcc_assert (a && (AT_class (a) == dw_val_class_const
4215 || AT_class (a) == dw_val_class_const_implicit));
4216 return a->dw_attr_val.v.val_int;
4217 }
4218
4219 /* Add an unsigned integer attribute value to a DIE. */
4220
4221 static inline void
4222 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4223 unsigned HOST_WIDE_INT unsigned_val)
4224 {
4225 dw_attr_node attr;
4226
4227 attr.dw_attr = attr_kind;
4228 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4229 attr.dw_attr_val.val_entry = NULL;
4230 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4231 add_dwarf_attr (die, &attr);
4232 }
4233
4234 static inline unsigned HOST_WIDE_INT
4235 AT_unsigned (dw_attr_node *a)
4236 {
4237 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4238 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4239 return a->dw_attr_val.v.val_unsigned;
4240 }
4241
4242 /* Add an unsigned wide integer attribute value to a DIE. */
4243
4244 static inline void
4245 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4246 const wide_int& w)
4247 {
4248 dw_attr_node attr;
4249
4250 attr.dw_attr = attr_kind;
4251 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4252 attr.dw_attr_val.val_entry = NULL;
4253 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4254 *attr.dw_attr_val.v.val_wide = w;
4255 add_dwarf_attr (die, &attr);
4256 }
4257
4258 /* Add an unsigned double integer attribute value to a DIE. */
4259
4260 static inline void
4261 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4262 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4263 {
4264 dw_attr_node attr;
4265
4266 attr.dw_attr = attr_kind;
4267 attr.dw_attr_val.val_class = dw_val_class_const_double;
4268 attr.dw_attr_val.val_entry = NULL;
4269 attr.dw_attr_val.v.val_double.high = high;
4270 attr.dw_attr_val.v.val_double.low = low;
4271 add_dwarf_attr (die, &attr);
4272 }
4273
4274 /* Add a floating point attribute value to a DIE and return it. */
4275
4276 static inline void
4277 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4278 unsigned int length, unsigned int elt_size, unsigned char *array)
4279 {
4280 dw_attr_node attr;
4281
4282 attr.dw_attr = attr_kind;
4283 attr.dw_attr_val.val_class = dw_val_class_vec;
4284 attr.dw_attr_val.val_entry = NULL;
4285 attr.dw_attr_val.v.val_vec.length = length;
4286 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4287 attr.dw_attr_val.v.val_vec.array = array;
4288 add_dwarf_attr (die, &attr);
4289 }
4290
4291 /* Add an 8-byte data attribute value to a DIE. */
4292
4293 static inline void
4294 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4295 unsigned char data8[8])
4296 {
4297 dw_attr_node attr;
4298
4299 attr.dw_attr = attr_kind;
4300 attr.dw_attr_val.val_class = dw_val_class_data8;
4301 attr.dw_attr_val.val_entry = NULL;
4302 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4303 add_dwarf_attr (die, &attr);
4304 }
4305
4306 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4307 dwarf_split_debug_info, address attributes in dies destined for the
4308 final executable have force_direct set to avoid using indexed
4309 references. */
4310
4311 static inline void
4312 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4313 bool force_direct)
4314 {
4315 dw_attr_node attr;
4316 char * lbl_id;
4317
4318 lbl_id = xstrdup (lbl_low);
4319 attr.dw_attr = DW_AT_low_pc;
4320 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4321 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4322 if (dwarf_split_debug_info && !force_direct)
4323 attr.dw_attr_val.val_entry
4324 = add_addr_table_entry (lbl_id, ate_kind_label);
4325 else
4326 attr.dw_attr_val.val_entry = NULL;
4327 add_dwarf_attr (die, &attr);
4328
4329 attr.dw_attr = DW_AT_high_pc;
4330 if (dwarf_version < 4)
4331 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4332 else
4333 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4334 lbl_id = xstrdup (lbl_high);
4335 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4336 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4337 && dwarf_split_debug_info && !force_direct)
4338 attr.dw_attr_val.val_entry
4339 = add_addr_table_entry (lbl_id, ate_kind_label);
4340 else
4341 attr.dw_attr_val.val_entry = NULL;
4342 add_dwarf_attr (die, &attr);
4343 }
4344
4345 /* Hash and equality functions for debug_str_hash. */
4346
4347 hashval_t
4348 indirect_string_hasher::hash (indirect_string_node *x)
4349 {
4350 return htab_hash_string (x->str);
4351 }
4352
4353 bool
4354 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4355 {
4356 return strcmp (x1->str, x2) == 0;
4357 }
4358
4359 /* Add STR to the given string hash table. */
4360
4361 static struct indirect_string_node *
4362 find_AT_string_in_table (const char *str,
4363 hash_table<indirect_string_hasher> *table)
4364 {
4365 struct indirect_string_node *node;
4366
4367 indirect_string_node **slot
4368 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4369 if (*slot == NULL)
4370 {
4371 node = ggc_cleared_alloc<indirect_string_node> ();
4372 node->str = ggc_strdup (str);
4373 *slot = node;
4374 }
4375 else
4376 node = *slot;
4377
4378 node->refcount++;
4379 return node;
4380 }
4381
4382 /* Add STR to the indirect string hash table. */
4383
4384 static struct indirect_string_node *
4385 find_AT_string (const char *str)
4386 {
4387 if (! debug_str_hash)
4388 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4389
4390 return find_AT_string_in_table (str, debug_str_hash);
4391 }
4392
4393 /* Add a string attribute value to a DIE. */
4394
4395 static inline void
4396 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4397 {
4398 dw_attr_node attr;
4399 struct indirect_string_node *node;
4400
4401 node = find_AT_string (str);
4402
4403 attr.dw_attr = attr_kind;
4404 attr.dw_attr_val.val_class = dw_val_class_str;
4405 attr.dw_attr_val.val_entry = NULL;
4406 attr.dw_attr_val.v.val_str = node;
4407 add_dwarf_attr (die, &attr);
4408 }
4409
4410 static inline const char *
4411 AT_string (dw_attr_node *a)
4412 {
4413 gcc_assert (a && AT_class (a) == dw_val_class_str);
4414 return a->dw_attr_val.v.val_str->str;
4415 }
4416
4417 /* Call this function directly to bypass AT_string_form's logic to put
4418 the string inline in the die. */
4419
4420 static void
4421 set_indirect_string (struct indirect_string_node *node)
4422 {
4423 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4424 /* Already indirect is a no op. */
4425 if (node->form == DW_FORM_strp
4426 || node->form == DW_FORM_line_strp
4427 || node->form == DW_FORM_GNU_str_index)
4428 {
4429 gcc_assert (node->label);
4430 return;
4431 }
4432 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4433 ++dw2_string_counter;
4434 node->label = xstrdup (label);
4435
4436 if (!dwarf_split_debug_info)
4437 {
4438 node->form = DW_FORM_strp;
4439 node->index = NOT_INDEXED;
4440 }
4441 else
4442 {
4443 node->form = DW_FORM_GNU_str_index;
4444 node->index = NO_INDEX_ASSIGNED;
4445 }
4446 }
4447
4448 /* A helper function for dwarf2out_finish, called to reset indirect
4449 string decisions done for early LTO dwarf output before fat object
4450 dwarf output. */
4451
4452 int
4453 reset_indirect_string (indirect_string_node **h, void *)
4454 {
4455 struct indirect_string_node *node = *h;
4456 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4457 {
4458 free (node->label);
4459 node->label = NULL;
4460 node->form = (dwarf_form) 0;
4461 node->index = 0;
4462 }
4463 return 1;
4464 }
4465
4466 /* Find out whether a string should be output inline in DIE
4467 or out-of-line in .debug_str section. */
4468
4469 static enum dwarf_form
4470 find_string_form (struct indirect_string_node *node)
4471 {
4472 unsigned int len;
4473
4474 if (node->form)
4475 return node->form;
4476
4477 len = strlen (node->str) + 1;
4478
4479 /* If the string is shorter or equal to the size of the reference, it is
4480 always better to put it inline. */
4481 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4482 return node->form = DW_FORM_string;
4483
4484 /* If we cannot expect the linker to merge strings in .debug_str
4485 section, only put it into .debug_str if it is worth even in this
4486 single module. */
4487 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4488 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4489 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4490 return node->form = DW_FORM_string;
4491
4492 set_indirect_string (node);
4493
4494 return node->form;
4495 }
4496
4497 /* Find out whether the string referenced from the attribute should be
4498 output inline in DIE or out-of-line in .debug_str section. */
4499
4500 static enum dwarf_form
4501 AT_string_form (dw_attr_node *a)
4502 {
4503 gcc_assert (a && AT_class (a) == dw_val_class_str);
4504 return find_string_form (a->dw_attr_val.v.val_str);
4505 }
4506
4507 /* Add a DIE reference attribute value to a DIE. */
4508
4509 static inline void
4510 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4511 {
4512 dw_attr_node attr;
4513 gcc_checking_assert (targ_die != NULL);
4514
4515 /* With LTO we can end up trying to reference something we didn't create
4516 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4517 if (targ_die == NULL)
4518 return;
4519
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4524 attr.dw_attr_val.v.val_die_ref.external = 0;
4525 add_dwarf_attr (die, &attr);
4526 }
4527
4528 /* Change DIE reference REF to point to NEW_DIE instead. */
4529
4530 static inline void
4531 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4532 {
4533 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4534 ref->dw_attr_val.v.val_die_ref.die = new_die;
4535 ref->dw_attr_val.v.val_die_ref.external = 0;
4536 }
4537
4538 /* Add an AT_specification attribute to a DIE, and also make the back
4539 pointer from the specification to the definition. */
4540
4541 static inline void
4542 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4543 {
4544 add_AT_die_ref (die, DW_AT_specification, targ_die);
4545 gcc_assert (!targ_die->die_definition);
4546 targ_die->die_definition = die;
4547 }
4548
4549 static inline dw_die_ref
4550 AT_ref (dw_attr_node *a)
4551 {
4552 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4553 return a->dw_attr_val.v.val_die_ref.die;
4554 }
4555
4556 static inline int
4557 AT_ref_external (dw_attr_node *a)
4558 {
4559 if (a && AT_class (a) == dw_val_class_die_ref)
4560 return a->dw_attr_val.v.val_die_ref.external;
4561
4562 return 0;
4563 }
4564
4565 static inline void
4566 set_AT_ref_external (dw_attr_node *a, int i)
4567 {
4568 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4569 a->dw_attr_val.v.val_die_ref.external = i;
4570 }
4571
4572 /* Add an FDE reference attribute value to a DIE. */
4573
4574 static inline void
4575 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4576 {
4577 dw_attr_node attr;
4578
4579 attr.dw_attr = attr_kind;
4580 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4581 attr.dw_attr_val.val_entry = NULL;
4582 attr.dw_attr_val.v.val_fde_index = targ_fde;
4583 add_dwarf_attr (die, &attr);
4584 }
4585
4586 /* Add a location description attribute value to a DIE. */
4587
4588 static inline void
4589 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4590 {
4591 dw_attr_node attr;
4592
4593 attr.dw_attr = attr_kind;
4594 attr.dw_attr_val.val_class = dw_val_class_loc;
4595 attr.dw_attr_val.val_entry = NULL;
4596 attr.dw_attr_val.v.val_loc = loc;
4597 add_dwarf_attr (die, &attr);
4598 }
4599
4600 static inline dw_loc_descr_ref
4601 AT_loc (dw_attr_node *a)
4602 {
4603 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4604 return a->dw_attr_val.v.val_loc;
4605 }
4606
4607 static inline void
4608 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4609 {
4610 dw_attr_node attr;
4611
4612 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4613 return;
4614
4615 attr.dw_attr = attr_kind;
4616 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4617 attr.dw_attr_val.val_entry = NULL;
4618 attr.dw_attr_val.v.val_loc_list = loc_list;
4619 add_dwarf_attr (die, &attr);
4620 have_location_lists = true;
4621 }
4622
4623 static inline dw_loc_list_ref
4624 AT_loc_list (dw_attr_node *a)
4625 {
4626 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4627 return a->dw_attr_val.v.val_loc_list;
4628 }
4629
4630 static inline dw_loc_list_ref *
4631 AT_loc_list_ptr (dw_attr_node *a)
4632 {
4633 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4634 return &a->dw_attr_val.v.val_loc_list;
4635 }
4636
4637 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4638 {
4639 static hashval_t hash (addr_table_entry *);
4640 static bool equal (addr_table_entry *, addr_table_entry *);
4641 };
4642
4643 /* Table of entries into the .debug_addr section. */
4644
4645 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4646
4647 /* Hash an address_table_entry. */
4648
4649 hashval_t
4650 addr_hasher::hash (addr_table_entry *a)
4651 {
4652 inchash::hash hstate;
4653 switch (a->kind)
4654 {
4655 case ate_kind_rtx:
4656 hstate.add_int (0);
4657 break;
4658 case ate_kind_rtx_dtprel:
4659 hstate.add_int (1);
4660 break;
4661 case ate_kind_label:
4662 return htab_hash_string (a->addr.label);
4663 default:
4664 gcc_unreachable ();
4665 }
4666 inchash::add_rtx (a->addr.rtl, hstate);
4667 return hstate.end ();
4668 }
4669
4670 /* Determine equality for two address_table_entries. */
4671
4672 bool
4673 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4674 {
4675 if (a1->kind != a2->kind)
4676 return 0;
4677 switch (a1->kind)
4678 {
4679 case ate_kind_rtx:
4680 case ate_kind_rtx_dtprel:
4681 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4682 case ate_kind_label:
4683 return strcmp (a1->addr.label, a2->addr.label) == 0;
4684 default:
4685 gcc_unreachable ();
4686 }
4687 }
4688
4689 /* Initialize an addr_table_entry. */
4690
4691 void
4692 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4693 {
4694 e->kind = kind;
4695 switch (kind)
4696 {
4697 case ate_kind_rtx:
4698 case ate_kind_rtx_dtprel:
4699 e->addr.rtl = (rtx) addr;
4700 break;
4701 case ate_kind_label:
4702 e->addr.label = (char *) addr;
4703 break;
4704 }
4705 e->refcount = 0;
4706 e->index = NO_INDEX_ASSIGNED;
4707 }
4708
4709 /* Add attr to the address table entry to the table. Defer setting an
4710 index until output time. */
4711
4712 static addr_table_entry *
4713 add_addr_table_entry (void *addr, enum ate_kind kind)
4714 {
4715 addr_table_entry *node;
4716 addr_table_entry finder;
4717
4718 gcc_assert (dwarf_split_debug_info);
4719 if (! addr_index_table)
4720 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4721 init_addr_table_entry (&finder, kind, addr);
4722 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4723
4724 if (*slot == HTAB_EMPTY_ENTRY)
4725 {
4726 node = ggc_cleared_alloc<addr_table_entry> ();
4727 init_addr_table_entry (node, kind, addr);
4728 *slot = node;
4729 }
4730 else
4731 node = *slot;
4732
4733 node->refcount++;
4734 return node;
4735 }
4736
4737 /* Remove an entry from the addr table by decrementing its refcount.
4738 Strictly, decrementing the refcount would be enough, but the
4739 assertion that the entry is actually in the table has found
4740 bugs. */
4741
4742 static void
4743 remove_addr_table_entry (addr_table_entry *entry)
4744 {
4745 gcc_assert (dwarf_split_debug_info && addr_index_table);
4746 /* After an index is assigned, the table is frozen. */
4747 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4748 entry->refcount--;
4749 }
4750
4751 /* Given a location list, remove all addresses it refers to from the
4752 address_table. */
4753
4754 static void
4755 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4756 {
4757 for (; descr; descr = descr->dw_loc_next)
4758 if (descr->dw_loc_oprnd1.val_entry != NULL)
4759 {
4760 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4761 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4762 }
4763 }
4764
4765 /* A helper function for dwarf2out_finish called through
4766 htab_traverse. Assign an addr_table_entry its index. All entries
4767 must be collected into the table when this function is called,
4768 because the indexing code relies on htab_traverse to traverse nodes
4769 in the same order for each run. */
4770
4771 int
4772 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4773 {
4774 addr_table_entry *node = *h;
4775
4776 /* Don't index unreferenced nodes. */
4777 if (node->refcount == 0)
4778 return 1;
4779
4780 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4781 node->index = *index;
4782 *index += 1;
4783
4784 return 1;
4785 }
4786
4787 /* Add an address constant attribute value to a DIE. When using
4788 dwarf_split_debug_info, address attributes in dies destined for the
4789 final executable should be direct references--setting the parameter
4790 force_direct ensures this behavior. */
4791
4792 static inline void
4793 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4794 bool force_direct)
4795 {
4796 dw_attr_node attr;
4797
4798 attr.dw_attr = attr_kind;
4799 attr.dw_attr_val.val_class = dw_val_class_addr;
4800 attr.dw_attr_val.v.val_addr = addr;
4801 if (dwarf_split_debug_info && !force_direct)
4802 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4803 else
4804 attr.dw_attr_val.val_entry = NULL;
4805 add_dwarf_attr (die, &attr);
4806 }
4807
4808 /* Get the RTX from to an address DIE attribute. */
4809
4810 static inline rtx
4811 AT_addr (dw_attr_node *a)
4812 {
4813 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4814 return a->dw_attr_val.v.val_addr;
4815 }
4816
4817 /* Add a file attribute value to a DIE. */
4818
4819 static inline void
4820 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4821 struct dwarf_file_data *fd)
4822 {
4823 dw_attr_node attr;
4824
4825 attr.dw_attr = attr_kind;
4826 attr.dw_attr_val.val_class = dw_val_class_file;
4827 attr.dw_attr_val.val_entry = NULL;
4828 attr.dw_attr_val.v.val_file = fd;
4829 add_dwarf_attr (die, &attr);
4830 }
4831
4832 /* Get the dwarf_file_data from a file DIE attribute. */
4833
4834 static inline struct dwarf_file_data *
4835 AT_file (dw_attr_node *a)
4836 {
4837 gcc_assert (a && (AT_class (a) == dw_val_class_file
4838 || AT_class (a) == dw_val_class_file_implicit));
4839 return a->dw_attr_val.v.val_file;
4840 }
4841
4842 /* Add a vms delta attribute value to a DIE. */
4843
4844 static inline void
4845 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4846 const char *lbl1, const char *lbl2)
4847 {
4848 dw_attr_node attr;
4849
4850 attr.dw_attr = attr_kind;
4851 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4852 attr.dw_attr_val.val_entry = NULL;
4853 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4854 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4855 add_dwarf_attr (die, &attr);
4856 }
4857
4858 /* Add a label identifier attribute value to a DIE. */
4859
4860 static inline void
4861 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4862 const char *lbl_id)
4863 {
4864 dw_attr_node attr;
4865
4866 attr.dw_attr = attr_kind;
4867 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4868 attr.dw_attr_val.val_entry = NULL;
4869 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4870 if (dwarf_split_debug_info)
4871 attr.dw_attr_val.val_entry
4872 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4873 ate_kind_label);
4874 add_dwarf_attr (die, &attr);
4875 }
4876
4877 /* Add a section offset attribute value to a DIE, an offset into the
4878 debug_line section. */
4879
4880 static inline void
4881 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4882 const char *label)
4883 {
4884 dw_attr_node attr;
4885
4886 attr.dw_attr = attr_kind;
4887 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4888 attr.dw_attr_val.val_entry = NULL;
4889 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4890 add_dwarf_attr (die, &attr);
4891 }
4892
4893 /* Add a section offset attribute value to a DIE, an offset into the
4894 debug_loclists section. */
4895
4896 static inline void
4897 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4898 const char *label)
4899 {
4900 dw_attr_node attr;
4901
4902 attr.dw_attr = attr_kind;
4903 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4904 attr.dw_attr_val.val_entry = NULL;
4905 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4906 add_dwarf_attr (die, &attr);
4907 }
4908
4909 /* Add a section offset attribute value to a DIE, an offset into the
4910 debug_macinfo section. */
4911
4912 static inline void
4913 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4914 const char *label)
4915 {
4916 dw_attr_node attr;
4917
4918 attr.dw_attr = attr_kind;
4919 attr.dw_attr_val.val_class = dw_val_class_macptr;
4920 attr.dw_attr_val.val_entry = NULL;
4921 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4922 add_dwarf_attr (die, &attr);
4923 }
4924
4925 /* Add an offset attribute value to a DIE. */
4926
4927 static inline void
4928 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4929 unsigned HOST_WIDE_INT offset)
4930 {
4931 dw_attr_node attr;
4932
4933 attr.dw_attr = attr_kind;
4934 attr.dw_attr_val.val_class = dw_val_class_offset;
4935 attr.dw_attr_val.val_entry = NULL;
4936 attr.dw_attr_val.v.val_offset = offset;
4937 add_dwarf_attr (die, &attr);
4938 }
4939
4940 /* Add a range_list attribute value to a DIE. When using
4941 dwarf_split_debug_info, address attributes in dies destined for the
4942 final executable should be direct references--setting the parameter
4943 force_direct ensures this behavior. */
4944
4945 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4946 #define RELOCATED_OFFSET (NULL)
4947
4948 static void
4949 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4950 long unsigned int offset, bool force_direct)
4951 {
4952 dw_attr_node attr;
4953
4954 attr.dw_attr = attr_kind;
4955 attr.dw_attr_val.val_class = dw_val_class_range_list;
4956 /* For the range_list attribute, use val_entry to store whether the
4957 offset should follow split-debug-info or normal semantics. This
4958 value is read in output_range_list_offset. */
4959 if (dwarf_split_debug_info && !force_direct)
4960 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4961 else
4962 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4963 attr.dw_attr_val.v.val_offset = offset;
4964 add_dwarf_attr (die, &attr);
4965 }
4966
4967 /* Return the start label of a delta attribute. */
4968
4969 static inline const char *
4970 AT_vms_delta1 (dw_attr_node *a)
4971 {
4972 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4973 return a->dw_attr_val.v.val_vms_delta.lbl1;
4974 }
4975
4976 /* Return the end label of a delta attribute. */
4977
4978 static inline const char *
4979 AT_vms_delta2 (dw_attr_node *a)
4980 {
4981 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4982 return a->dw_attr_val.v.val_vms_delta.lbl2;
4983 }
4984
4985 static inline const char *
4986 AT_lbl (dw_attr_node *a)
4987 {
4988 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4989 || AT_class (a) == dw_val_class_lineptr
4990 || AT_class (a) == dw_val_class_macptr
4991 || AT_class (a) == dw_val_class_loclistsptr
4992 || AT_class (a) == dw_val_class_high_pc));
4993 return a->dw_attr_val.v.val_lbl_id;
4994 }
4995
4996 /* Get the attribute of type attr_kind. */
4997
4998 static dw_attr_node *
4999 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5000 {
5001 dw_attr_node *a;
5002 unsigned ix;
5003 dw_die_ref spec = NULL;
5004
5005 if (! die)
5006 return NULL;
5007
5008 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5009 if (a->dw_attr == attr_kind)
5010 return a;
5011 else if (a->dw_attr == DW_AT_specification
5012 || a->dw_attr == DW_AT_abstract_origin)
5013 spec = AT_ref (a);
5014
5015 if (spec)
5016 return get_AT (spec, attr_kind);
5017
5018 return NULL;
5019 }
5020
5021 /* Returns the parent of the declaration of DIE. */
5022
5023 static dw_die_ref
5024 get_die_parent (dw_die_ref die)
5025 {
5026 dw_die_ref t;
5027
5028 if (!die)
5029 return NULL;
5030
5031 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5032 || (t = get_AT_ref (die, DW_AT_specification)))
5033 die = t;
5034
5035 return die->die_parent;
5036 }
5037
5038 /* Return the "low pc" attribute value, typically associated with a subprogram
5039 DIE. Return null if the "low pc" attribute is either not present, or if it
5040 cannot be represented as an assembler label identifier. */
5041
5042 static inline const char *
5043 get_AT_low_pc (dw_die_ref die)
5044 {
5045 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5046
5047 return a ? AT_lbl (a) : NULL;
5048 }
5049
5050 /* Return the "high pc" attribute value, typically associated with a subprogram
5051 DIE. Return null if the "high pc" attribute is either not present, or if it
5052 cannot be represented as an assembler label identifier. */
5053
5054 static inline const char *
5055 get_AT_hi_pc (dw_die_ref die)
5056 {
5057 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5058
5059 return a ? AT_lbl (a) : NULL;
5060 }
5061
5062 /* Return the value of the string attribute designated by ATTR_KIND, or
5063 NULL if it is not present. */
5064
5065 static inline const char *
5066 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5067 {
5068 dw_attr_node *a = get_AT (die, attr_kind);
5069
5070 return a ? AT_string (a) : NULL;
5071 }
5072
5073 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5074 if it is not present. */
5075
5076 static inline int
5077 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5078 {
5079 dw_attr_node *a = get_AT (die, attr_kind);
5080
5081 return a ? AT_flag (a) : 0;
5082 }
5083
5084 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5085 if it is not present. */
5086
5087 static inline unsigned
5088 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5089 {
5090 dw_attr_node *a = get_AT (die, attr_kind);
5091
5092 return a ? AT_unsigned (a) : 0;
5093 }
5094
5095 static inline dw_die_ref
5096 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5097 {
5098 dw_attr_node *a = get_AT (die, attr_kind);
5099
5100 return a ? AT_ref (a) : NULL;
5101 }
5102
5103 static inline struct dwarf_file_data *
5104 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5105 {
5106 dw_attr_node *a = get_AT (die, attr_kind);
5107
5108 return a ? AT_file (a) : NULL;
5109 }
5110
5111 /* Return TRUE if the language is C++. */
5112
5113 static inline bool
5114 is_cxx (void)
5115 {
5116 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5117
5118 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5119 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5120 }
5121
5122 /* Return TRUE if DECL was created by the C++ frontend. */
5123
5124 static bool
5125 is_cxx (const_tree decl)
5126 {
5127 if (in_lto_p)
5128 {
5129 const_tree context = get_ultimate_context (decl);
5130 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5131 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5132 }
5133 return is_cxx ();
5134 }
5135
5136 /* Return TRUE if the language is Fortran. */
5137
5138 static inline bool
5139 is_fortran (void)
5140 {
5141 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5142
5143 return (lang == DW_LANG_Fortran77
5144 || lang == DW_LANG_Fortran90
5145 || lang == DW_LANG_Fortran95
5146 || lang == DW_LANG_Fortran03
5147 || lang == DW_LANG_Fortran08);
5148 }
5149
5150 static inline bool
5151 is_fortran (const_tree decl)
5152 {
5153 if (in_lto_p)
5154 {
5155 const_tree context = get_ultimate_context (decl);
5156 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5157 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5158 "GNU Fortran", 11) == 0
5159 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5160 "GNU F77") == 0);
5161 }
5162 return is_fortran ();
5163 }
5164
5165 /* Return TRUE if the language is Ada. */
5166
5167 static inline bool
5168 is_ada (void)
5169 {
5170 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5171
5172 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5173 }
5174
5175 /* Remove the specified attribute if present. Return TRUE if removal
5176 was successful. */
5177
5178 static bool
5179 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5180 {
5181 dw_attr_node *a;
5182 unsigned ix;
5183
5184 if (! die)
5185 return false;
5186
5187 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5188 if (a->dw_attr == attr_kind)
5189 {
5190 if (AT_class (a) == dw_val_class_str)
5191 if (a->dw_attr_val.v.val_str->refcount)
5192 a->dw_attr_val.v.val_str->refcount--;
5193
5194 /* vec::ordered_remove should help reduce the number of abbrevs
5195 that are needed. */
5196 die->die_attr->ordered_remove (ix);
5197 return true;
5198 }
5199 return false;
5200 }
5201
5202 /* Remove CHILD from its parent. PREV must have the property that
5203 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5204
5205 static void
5206 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5207 {
5208 gcc_assert (child->die_parent == prev->die_parent);
5209 gcc_assert (prev->die_sib == child);
5210 if (prev == child)
5211 {
5212 gcc_assert (child->die_parent->die_child == child);
5213 prev = NULL;
5214 }
5215 else
5216 prev->die_sib = child->die_sib;
5217 if (child->die_parent->die_child == child)
5218 child->die_parent->die_child = prev;
5219 child->die_sib = NULL;
5220 }
5221
5222 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5223 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5224
5225 static void
5226 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5227 {
5228 dw_die_ref parent = old_child->die_parent;
5229
5230 gcc_assert (parent == prev->die_parent);
5231 gcc_assert (prev->die_sib == old_child);
5232
5233 new_child->die_parent = parent;
5234 if (prev == old_child)
5235 {
5236 gcc_assert (parent->die_child == old_child);
5237 new_child->die_sib = new_child;
5238 }
5239 else
5240 {
5241 prev->die_sib = new_child;
5242 new_child->die_sib = old_child->die_sib;
5243 }
5244 if (old_child->die_parent->die_child == old_child)
5245 old_child->die_parent->die_child = new_child;
5246 old_child->die_sib = NULL;
5247 }
5248
5249 /* Move all children from OLD_PARENT to NEW_PARENT. */
5250
5251 static void
5252 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5253 {
5254 dw_die_ref c;
5255 new_parent->die_child = old_parent->die_child;
5256 old_parent->die_child = NULL;
5257 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5258 }
5259
5260 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5261 matches TAG. */
5262
5263 static void
5264 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5265 {
5266 dw_die_ref c;
5267
5268 c = die->die_child;
5269 if (c) do {
5270 dw_die_ref prev = c;
5271 c = c->die_sib;
5272 while (c->die_tag == tag)
5273 {
5274 remove_child_with_prev (c, prev);
5275 c->die_parent = NULL;
5276 /* Might have removed every child. */
5277 if (die->die_child == NULL)
5278 return;
5279 c = prev->die_sib;
5280 }
5281 } while (c != die->die_child);
5282 }
5283
5284 /* Add a CHILD_DIE as the last child of DIE. */
5285
5286 static void
5287 add_child_die (dw_die_ref die, dw_die_ref child_die)
5288 {
5289 /* FIXME this should probably be an assert. */
5290 if (! die || ! child_die)
5291 return;
5292 gcc_assert (die != child_die);
5293
5294 child_die->die_parent = die;
5295 if (die->die_child)
5296 {
5297 child_die->die_sib = die->die_child->die_sib;
5298 die->die_child->die_sib = child_die;
5299 }
5300 else
5301 child_die->die_sib = child_die;
5302 die->die_child = child_die;
5303 }
5304
5305 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5306
5307 static void
5308 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5309 dw_die_ref after_die)
5310 {
5311 gcc_assert (die
5312 && child_die
5313 && after_die
5314 && die->die_child
5315 && die != child_die);
5316
5317 child_die->die_parent = die;
5318 child_die->die_sib = after_die->die_sib;
5319 after_die->die_sib = child_die;
5320 if (die->die_child == after_die)
5321 die->die_child = child_die;
5322 }
5323
5324 /* Unassociate CHILD from its parent, and make its parent be
5325 NEW_PARENT. */
5326
5327 static void
5328 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5329 {
5330 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5331 if (p->die_sib == child)
5332 {
5333 remove_child_with_prev (child, p);
5334 break;
5335 }
5336 add_child_die (new_parent, child);
5337 }
5338
5339 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5340 is the specification, to the end of PARENT's list of children.
5341 This is done by removing and re-adding it. */
5342
5343 static void
5344 splice_child_die (dw_die_ref parent, dw_die_ref child)
5345 {
5346 /* We want the declaration DIE from inside the class, not the
5347 specification DIE at toplevel. */
5348 if (child->die_parent != parent)
5349 {
5350 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5351
5352 if (tmp)
5353 child = tmp;
5354 }
5355
5356 gcc_assert (child->die_parent == parent
5357 || (child->die_parent
5358 == get_AT_ref (parent, DW_AT_specification)));
5359
5360 reparent_child (child, parent);
5361 }
5362
5363 /* Create and return a new die with TAG_VALUE as tag. */
5364
5365 static inline dw_die_ref
5366 new_die_raw (enum dwarf_tag tag_value)
5367 {
5368 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5369 die->die_tag = tag_value;
5370 return die;
5371 }
5372
5373 /* Create and return a new die with a parent of PARENT_DIE. If
5374 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5375 associated tree T must be supplied to determine parenthood
5376 later. */
5377
5378 static inline dw_die_ref
5379 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5380 {
5381 dw_die_ref die = new_die_raw (tag_value);
5382
5383 if (parent_die != NULL)
5384 add_child_die (parent_die, die);
5385 else
5386 {
5387 limbo_die_node *limbo_node;
5388
5389 /* No DIEs created after early dwarf should end up in limbo,
5390 because the limbo list should not persist past LTO
5391 streaming. */
5392 if (tag_value != DW_TAG_compile_unit
5393 /* These are allowed because they're generated while
5394 breaking out COMDAT units late. */
5395 && tag_value != DW_TAG_type_unit
5396 && tag_value != DW_TAG_skeleton_unit
5397 && !early_dwarf
5398 /* Allow nested functions to live in limbo because they will
5399 only temporarily live there, as decls_for_scope will fix
5400 them up. */
5401 && (TREE_CODE (t) != FUNCTION_DECL
5402 || !decl_function_context (t))
5403 /* Same as nested functions above but for types. Types that
5404 are local to a function will be fixed in
5405 decls_for_scope. */
5406 && (!RECORD_OR_UNION_TYPE_P (t)
5407 || !TYPE_CONTEXT (t)
5408 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5409 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5410 especially in the ltrans stage, but once we implement LTO
5411 dwarf streaming, we should remove this exception. */
5412 && !in_lto_p)
5413 {
5414 fprintf (stderr, "symbol ended up in limbo too late:");
5415 debug_generic_stmt (t);
5416 gcc_unreachable ();
5417 }
5418
5419 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5420 limbo_node->die = die;
5421 limbo_node->created_for = t;
5422 limbo_node->next = limbo_die_list;
5423 limbo_die_list = limbo_node;
5424 }
5425
5426 return die;
5427 }
5428
5429 /* Return the DIE associated with the given type specifier. */
5430
5431 static inline dw_die_ref
5432 lookup_type_die (tree type)
5433 {
5434 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5435 if (die && die->removed)
5436 {
5437 TYPE_SYMTAB_DIE (type) = NULL;
5438 return NULL;
5439 }
5440 return die;
5441 }
5442
5443 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5444 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5445 anonymous type instead the one of the naming typedef. */
5446
5447 static inline dw_die_ref
5448 strip_naming_typedef (tree type, dw_die_ref type_die)
5449 {
5450 if (type
5451 && TREE_CODE (type) == RECORD_TYPE
5452 && type_die
5453 && type_die->die_tag == DW_TAG_typedef
5454 && is_naming_typedef_decl (TYPE_NAME (type)))
5455 type_die = get_AT_ref (type_die, DW_AT_type);
5456 return type_die;
5457 }
5458
5459 /* Like lookup_type_die, but if type is an anonymous type named by a
5460 typedef[1], return the DIE of the anonymous type instead the one of
5461 the naming typedef. This is because in gen_typedef_die, we did
5462 equate the anonymous struct named by the typedef with the DIE of
5463 the naming typedef. So by default, lookup_type_die on an anonymous
5464 struct yields the DIE of the naming typedef.
5465
5466 [1]: Read the comment of is_naming_typedef_decl to learn about what
5467 a naming typedef is. */
5468
5469 static inline dw_die_ref
5470 lookup_type_die_strip_naming_typedef (tree type)
5471 {
5472 dw_die_ref die = lookup_type_die (type);
5473 return strip_naming_typedef (type, die);
5474 }
5475
5476 /* Equate a DIE to a given type specifier. */
5477
5478 static inline void
5479 equate_type_number_to_die (tree type, dw_die_ref type_die)
5480 {
5481 TYPE_SYMTAB_DIE (type) = type_die;
5482 }
5483
5484 /* Returns a hash value for X (which really is a die_struct). */
5485
5486 inline hashval_t
5487 decl_die_hasher::hash (die_node *x)
5488 {
5489 return (hashval_t) x->decl_id;
5490 }
5491
5492 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5493
5494 inline bool
5495 decl_die_hasher::equal (die_node *x, tree y)
5496 {
5497 return (x->decl_id == DECL_UID (y));
5498 }
5499
5500 /* Return the DIE associated with a given declaration. */
5501
5502 static inline dw_die_ref
5503 lookup_decl_die (tree decl)
5504 {
5505 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5506 NO_INSERT);
5507 if (!die)
5508 return NULL;
5509 if ((*die)->removed)
5510 {
5511 decl_die_table->clear_slot (die);
5512 return NULL;
5513 }
5514 return *die;
5515 }
5516
5517
5518 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5519 style reference. Return true if we found one refering to a DIE for
5520 DECL, otherwise return false. */
5521
5522 static bool
5523 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5524 unsigned HOST_WIDE_INT *off)
5525 {
5526 dw_die_ref die;
5527
5528 if (flag_wpa && !decl_die_table)
5529 return false;
5530
5531 if (TREE_CODE (decl) == BLOCK)
5532 die = BLOCK_DIE (decl);
5533 else
5534 die = lookup_decl_die (decl);
5535 if (!die)
5536 return false;
5537
5538 /* During WPA stage we currently use DIEs to store the
5539 decl <-> label + offset map. That's quite inefficient but it
5540 works for now. */
5541 if (flag_wpa)
5542 {
5543 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5544 if (!ref)
5545 {
5546 gcc_assert (die == comp_unit_die ());
5547 return false;
5548 }
5549 *off = ref->die_offset;
5550 *sym = ref->die_id.die_symbol;
5551 return true;
5552 }
5553
5554 /* Similar to get_ref_die_offset_label, but using the "correct"
5555 label. */
5556 *off = die->die_offset;
5557 while (die->die_parent)
5558 die = die->die_parent;
5559 /* For the containing CU DIE we compute a die_symbol in
5560 compute_comp_unit_symbol. */
5561 gcc_assert (die->die_tag == DW_TAG_compile_unit
5562 && die->die_id.die_symbol != NULL);
5563 *sym = die->die_id.die_symbol;
5564 return true;
5565 }
5566
5567 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5568
5569 static void
5570 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5571 const char *symbol, HOST_WIDE_INT offset)
5572 {
5573 /* Create a fake DIE that contains the reference. Don't use
5574 new_die because we don't want to end up in the limbo list. */
5575 dw_die_ref ref = new_die_raw (die->die_tag);
5576 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5577 ref->die_offset = offset;
5578 ref->with_offset = 1;
5579 add_AT_die_ref (die, attr_kind, ref);
5580 }
5581
5582 /* Create a DIE for DECL if required and add a reference to a DIE
5583 at SYMBOL + OFFSET which contains attributes dumped early. */
5584
5585 static void
5586 dwarf2out_register_external_die (tree decl, const char *sym,
5587 unsigned HOST_WIDE_INT off)
5588 {
5589 if (debug_info_level == DINFO_LEVEL_NONE)
5590 return;
5591
5592 if (flag_wpa && !decl_die_table)
5593 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5594
5595 dw_die_ref die
5596 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5597 gcc_assert (!die);
5598
5599 tree ctx;
5600 dw_die_ref parent = NULL;
5601 /* Need to lookup a DIE for the decls context - the containing
5602 function or translation unit. */
5603 if (TREE_CODE (decl) == BLOCK)
5604 {
5605 ctx = BLOCK_SUPERCONTEXT (decl);
5606 /* ??? We do not output DIEs for all scopes thus skip as
5607 many DIEs as needed. */
5608 while (TREE_CODE (ctx) == BLOCK
5609 && !BLOCK_DIE (ctx))
5610 ctx = BLOCK_SUPERCONTEXT (ctx);
5611 }
5612 else
5613 ctx = DECL_CONTEXT (decl);
5614 while (ctx && TYPE_P (ctx))
5615 ctx = TYPE_CONTEXT (ctx);
5616 if (ctx)
5617 {
5618 if (TREE_CODE (ctx) == BLOCK)
5619 parent = BLOCK_DIE (ctx);
5620 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5621 /* Keep the 1:1 association during WPA. */
5622 && !flag_wpa)
5623 /* Otherwise all late annotations go to the main CU which
5624 imports the original CUs. */
5625 parent = comp_unit_die ();
5626 else if (TREE_CODE (ctx) == FUNCTION_DECL
5627 && TREE_CODE (decl) != PARM_DECL
5628 && TREE_CODE (decl) != BLOCK)
5629 /* Leave function local entities parent determination to when
5630 we process scope vars. */
5631 ;
5632 else
5633 parent = lookup_decl_die (ctx);
5634 }
5635 else
5636 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5637 Handle this case gracefully by globalizing stuff. */
5638 parent = comp_unit_die ();
5639 /* Create a DIE "stub". */
5640 switch (TREE_CODE (decl))
5641 {
5642 case TRANSLATION_UNIT_DECL:
5643 if (! flag_wpa)
5644 {
5645 die = comp_unit_die ();
5646 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5647 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5648 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5649 to create a DIE for the original CUs. */
5650 return;
5651 }
5652 /* Keep the 1:1 association during WPA. */
5653 die = new_die (DW_TAG_compile_unit, NULL, decl);
5654 break;
5655 case NAMESPACE_DECL:
5656 if (is_fortran (decl))
5657 die = new_die (DW_TAG_module, parent, decl);
5658 else
5659 die = new_die (DW_TAG_namespace, parent, decl);
5660 break;
5661 case FUNCTION_DECL:
5662 die = new_die (DW_TAG_subprogram, parent, decl);
5663 break;
5664 case VAR_DECL:
5665 die = new_die (DW_TAG_variable, parent, decl);
5666 break;
5667 case RESULT_DECL:
5668 die = new_die (DW_TAG_variable, parent, decl);
5669 break;
5670 case PARM_DECL:
5671 die = new_die (DW_TAG_formal_parameter, parent, decl);
5672 break;
5673 case CONST_DECL:
5674 die = new_die (DW_TAG_constant, parent, decl);
5675 break;
5676 case LABEL_DECL:
5677 die = new_die (DW_TAG_label, parent, decl);
5678 break;
5679 case BLOCK:
5680 die = new_die (DW_TAG_lexical_block, parent, decl);
5681 break;
5682 default:
5683 gcc_unreachable ();
5684 }
5685 if (TREE_CODE (decl) == BLOCK)
5686 BLOCK_DIE (decl) = die;
5687 else
5688 equate_decl_number_to_die (decl, die);
5689
5690 /* Add a reference to the DIE providing early debug at $sym + off. */
5691 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5692 }
5693
5694 /* Returns a hash value for X (which really is a var_loc_list). */
5695
5696 inline hashval_t
5697 decl_loc_hasher::hash (var_loc_list *x)
5698 {
5699 return (hashval_t) x->decl_id;
5700 }
5701
5702 /* Return nonzero if decl_id of var_loc_list X is the same as
5703 UID of decl *Y. */
5704
5705 inline bool
5706 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5707 {
5708 return (x->decl_id == DECL_UID (y));
5709 }
5710
5711 /* Return the var_loc list associated with a given declaration. */
5712
5713 static inline var_loc_list *
5714 lookup_decl_loc (const_tree decl)
5715 {
5716 if (!decl_loc_table)
5717 return NULL;
5718 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5719 }
5720
5721 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5722
5723 inline hashval_t
5724 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5725 {
5726 return (hashval_t) x->decl_id;
5727 }
5728
5729 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5730 UID of decl *Y. */
5731
5732 inline bool
5733 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5734 {
5735 return (x->decl_id == DECL_UID (y));
5736 }
5737
5738 /* Equate a DIE to a particular declaration. */
5739
5740 static void
5741 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5742 {
5743 unsigned int decl_id = DECL_UID (decl);
5744
5745 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5746 decl_die->decl_id = decl_id;
5747 }
5748
5749 /* Return how many bits covers PIECE EXPR_LIST. */
5750
5751 static HOST_WIDE_INT
5752 decl_piece_bitsize (rtx piece)
5753 {
5754 int ret = (int) GET_MODE (piece);
5755 if (ret)
5756 return ret;
5757 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5758 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5759 return INTVAL (XEXP (XEXP (piece, 0), 0));
5760 }
5761
5762 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5763
5764 static rtx *
5765 decl_piece_varloc_ptr (rtx piece)
5766 {
5767 if ((int) GET_MODE (piece))
5768 return &XEXP (piece, 0);
5769 else
5770 return &XEXP (XEXP (piece, 0), 1);
5771 }
5772
5773 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5774 Next is the chain of following piece nodes. */
5775
5776 static rtx_expr_list *
5777 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5778 {
5779 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5780 return alloc_EXPR_LIST (bitsize, loc_note, next);
5781 else
5782 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5783 GEN_INT (bitsize),
5784 loc_note), next);
5785 }
5786
5787 /* Return rtx that should be stored into loc field for
5788 LOC_NOTE and BITPOS/BITSIZE. */
5789
5790 static rtx
5791 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5792 HOST_WIDE_INT bitsize)
5793 {
5794 if (bitsize != -1)
5795 {
5796 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5797 if (bitpos != 0)
5798 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5799 }
5800 return loc_note;
5801 }
5802
5803 /* This function either modifies location piece list *DEST in
5804 place (if SRC and INNER is NULL), or copies location piece list
5805 *SRC to *DEST while modifying it. Location BITPOS is modified
5806 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5807 not copied and if needed some padding around it is added.
5808 When modifying in place, DEST should point to EXPR_LIST where
5809 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5810 to the start of the whole list and INNER points to the EXPR_LIST
5811 where earlier pieces cover PIECE_BITPOS bits. */
5812
5813 static void
5814 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5815 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5816 HOST_WIDE_INT bitsize, rtx loc_note)
5817 {
5818 HOST_WIDE_INT diff;
5819 bool copy = inner != NULL;
5820
5821 if (copy)
5822 {
5823 /* First copy all nodes preceding the current bitpos. */
5824 while (src != inner)
5825 {
5826 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5827 decl_piece_bitsize (*src), NULL_RTX);
5828 dest = &XEXP (*dest, 1);
5829 src = &XEXP (*src, 1);
5830 }
5831 }
5832 /* Add padding if needed. */
5833 if (bitpos != piece_bitpos)
5834 {
5835 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5836 copy ? NULL_RTX : *dest);
5837 dest = &XEXP (*dest, 1);
5838 }
5839 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5840 {
5841 gcc_assert (!copy);
5842 /* A piece with correct bitpos and bitsize already exist,
5843 just update the location for it and return. */
5844 *decl_piece_varloc_ptr (*dest) = loc_note;
5845 return;
5846 }
5847 /* Add the piece that changed. */
5848 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5849 dest = &XEXP (*dest, 1);
5850 /* Skip over pieces that overlap it. */
5851 diff = bitpos - piece_bitpos + bitsize;
5852 if (!copy)
5853 src = dest;
5854 while (diff > 0 && *src)
5855 {
5856 rtx piece = *src;
5857 diff -= decl_piece_bitsize (piece);
5858 if (copy)
5859 src = &XEXP (piece, 1);
5860 else
5861 {
5862 *src = XEXP (piece, 1);
5863 free_EXPR_LIST_node (piece);
5864 }
5865 }
5866 /* Add padding if needed. */
5867 if (diff < 0 && *src)
5868 {
5869 if (!copy)
5870 dest = src;
5871 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5872 dest = &XEXP (*dest, 1);
5873 }
5874 if (!copy)
5875 return;
5876 /* Finally copy all nodes following it. */
5877 while (*src)
5878 {
5879 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5880 decl_piece_bitsize (*src), NULL_RTX);
5881 dest = &XEXP (*dest, 1);
5882 src = &XEXP (*src, 1);
5883 }
5884 }
5885
5886 /* Add a variable location node to the linked list for DECL. */
5887
5888 static struct var_loc_node *
5889 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5890 {
5891 unsigned int decl_id;
5892 var_loc_list *temp;
5893 struct var_loc_node *loc = NULL;
5894 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5895
5896 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5897 {
5898 tree realdecl = DECL_DEBUG_EXPR (decl);
5899 if (handled_component_p (realdecl)
5900 || (TREE_CODE (realdecl) == MEM_REF
5901 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5902 {
5903 HOST_WIDE_INT maxsize;
5904 bool reverse;
5905 tree innerdecl
5906 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5907 &reverse);
5908 if (!DECL_P (innerdecl)
5909 || DECL_IGNORED_P (innerdecl)
5910 || TREE_STATIC (innerdecl)
5911 || bitsize <= 0
5912 || bitpos + bitsize > 256
5913 || bitsize != maxsize)
5914 return NULL;
5915 decl = innerdecl;
5916 }
5917 }
5918
5919 decl_id = DECL_UID (decl);
5920 var_loc_list **slot
5921 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5922 if (*slot == NULL)
5923 {
5924 temp = ggc_cleared_alloc<var_loc_list> ();
5925 temp->decl_id = decl_id;
5926 *slot = temp;
5927 }
5928 else
5929 temp = *slot;
5930
5931 /* For PARM_DECLs try to keep around the original incoming value,
5932 even if that means we'll emit a zero-range .debug_loc entry. */
5933 if (temp->last
5934 && temp->first == temp->last
5935 && TREE_CODE (decl) == PARM_DECL
5936 && NOTE_P (temp->first->loc)
5937 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5938 && DECL_INCOMING_RTL (decl)
5939 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5940 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5941 == GET_CODE (DECL_INCOMING_RTL (decl))
5942 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5943 && (bitsize != -1
5944 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5945 NOTE_VAR_LOCATION_LOC (loc_note))
5946 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5947 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5948 {
5949 loc = ggc_cleared_alloc<var_loc_node> ();
5950 temp->first->next = loc;
5951 temp->last = loc;
5952 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5953 }
5954 else if (temp->last)
5955 {
5956 struct var_loc_node *last = temp->last, *unused = NULL;
5957 rtx *piece_loc = NULL, last_loc_note;
5958 HOST_WIDE_INT piece_bitpos = 0;
5959 if (last->next)
5960 {
5961 last = last->next;
5962 gcc_assert (last->next == NULL);
5963 }
5964 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5965 {
5966 piece_loc = &last->loc;
5967 do
5968 {
5969 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5970 if (piece_bitpos + cur_bitsize > bitpos)
5971 break;
5972 piece_bitpos += cur_bitsize;
5973 piece_loc = &XEXP (*piece_loc, 1);
5974 }
5975 while (*piece_loc);
5976 }
5977 /* TEMP->LAST here is either pointer to the last but one or
5978 last element in the chained list, LAST is pointer to the
5979 last element. */
5980 if (label && strcmp (last->label, label) == 0)
5981 {
5982 /* For SRA optimized variables if there weren't any real
5983 insns since last note, just modify the last node. */
5984 if (piece_loc != NULL)
5985 {
5986 adjust_piece_list (piece_loc, NULL, NULL,
5987 bitpos, piece_bitpos, bitsize, loc_note);
5988 return NULL;
5989 }
5990 /* If the last note doesn't cover any instructions, remove it. */
5991 if (temp->last != last)
5992 {
5993 temp->last->next = NULL;
5994 unused = last;
5995 last = temp->last;
5996 gcc_assert (strcmp (last->label, label) != 0);
5997 }
5998 else
5999 {
6000 gcc_assert (temp->first == temp->last
6001 || (temp->first->next == temp->last
6002 && TREE_CODE (decl) == PARM_DECL));
6003 memset (temp->last, '\0', sizeof (*temp->last));
6004 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6005 return temp->last;
6006 }
6007 }
6008 if (bitsize == -1 && NOTE_P (last->loc))
6009 last_loc_note = last->loc;
6010 else if (piece_loc != NULL
6011 && *piece_loc != NULL_RTX
6012 && piece_bitpos == bitpos
6013 && decl_piece_bitsize (*piece_loc) == bitsize)
6014 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6015 else
6016 last_loc_note = NULL_RTX;
6017 /* If the current location is the same as the end of the list,
6018 and either both or neither of the locations is uninitialized,
6019 we have nothing to do. */
6020 if (last_loc_note == NULL_RTX
6021 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6022 NOTE_VAR_LOCATION_LOC (loc_note)))
6023 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6024 != NOTE_VAR_LOCATION_STATUS (loc_note))
6025 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6026 == VAR_INIT_STATUS_UNINITIALIZED)
6027 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6028 == VAR_INIT_STATUS_UNINITIALIZED))))
6029 {
6030 /* Add LOC to the end of list and update LAST. If the last
6031 element of the list has been removed above, reuse its
6032 memory for the new node, otherwise allocate a new one. */
6033 if (unused)
6034 {
6035 loc = unused;
6036 memset (loc, '\0', sizeof (*loc));
6037 }
6038 else
6039 loc = ggc_cleared_alloc<var_loc_node> ();
6040 if (bitsize == -1 || piece_loc == NULL)
6041 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6042 else
6043 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6044 bitpos, piece_bitpos, bitsize, loc_note);
6045 last->next = loc;
6046 /* Ensure TEMP->LAST will point either to the new last but one
6047 element of the chain, or to the last element in it. */
6048 if (last != temp->last)
6049 temp->last = last;
6050 }
6051 else if (unused)
6052 ggc_free (unused);
6053 }
6054 else
6055 {
6056 loc = ggc_cleared_alloc<var_loc_node> ();
6057 temp->first = loc;
6058 temp->last = loc;
6059 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6060 }
6061 return loc;
6062 }
6063 \f
6064 /* Keep track of the number of spaces used to indent the
6065 output of the debugging routines that print the structure of
6066 the DIE internal representation. */
6067 static int print_indent;
6068
6069 /* Indent the line the number of spaces given by print_indent. */
6070
6071 static inline void
6072 print_spaces (FILE *outfile)
6073 {
6074 fprintf (outfile, "%*s", print_indent, "");
6075 }
6076
6077 /* Print a type signature in hex. */
6078
6079 static inline void
6080 print_signature (FILE *outfile, char *sig)
6081 {
6082 int i;
6083
6084 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6085 fprintf (outfile, "%02x", sig[i] & 0xff);
6086 }
6087
6088 static inline void
6089 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6090 {
6091 if (discr_value->pos)
6092 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6093 else
6094 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6095 }
6096
6097 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6098
6099 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6100 RECURSE, output location descriptor operations. */
6101
6102 static void
6103 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6104 {
6105 switch (val->val_class)
6106 {
6107 case dw_val_class_addr:
6108 fprintf (outfile, "address");
6109 break;
6110 case dw_val_class_offset:
6111 fprintf (outfile, "offset");
6112 break;
6113 case dw_val_class_loc:
6114 fprintf (outfile, "location descriptor");
6115 if (val->v.val_loc == NULL)
6116 fprintf (outfile, " -> <null>\n");
6117 else if (recurse)
6118 {
6119 fprintf (outfile, ":\n");
6120 print_indent += 4;
6121 print_loc_descr (val->v.val_loc, outfile);
6122 print_indent -= 4;
6123 }
6124 else
6125 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6126 break;
6127 case dw_val_class_loc_list:
6128 fprintf (outfile, "location list -> label:%s",
6129 val->v.val_loc_list->ll_symbol);
6130 break;
6131 case dw_val_class_range_list:
6132 fprintf (outfile, "range list");
6133 break;
6134 case dw_val_class_const:
6135 case dw_val_class_const_implicit:
6136 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6137 break;
6138 case dw_val_class_unsigned_const:
6139 case dw_val_class_unsigned_const_implicit:
6140 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6141 break;
6142 case dw_val_class_const_double:
6143 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6144 HOST_WIDE_INT_PRINT_UNSIGNED")",
6145 val->v.val_double.high,
6146 val->v.val_double.low);
6147 break;
6148 case dw_val_class_wide_int:
6149 {
6150 int i = val->v.val_wide->get_len ();
6151 fprintf (outfile, "constant (");
6152 gcc_assert (i > 0);
6153 if (val->v.val_wide->elt (i - 1) == 0)
6154 fprintf (outfile, "0x");
6155 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6156 val->v.val_wide->elt (--i));
6157 while (--i >= 0)
6158 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6159 val->v.val_wide->elt (i));
6160 fprintf (outfile, ")");
6161 break;
6162 }
6163 case dw_val_class_vec:
6164 fprintf (outfile, "floating-point or vector constant");
6165 break;
6166 case dw_val_class_flag:
6167 fprintf (outfile, "%u", val->v.val_flag);
6168 break;
6169 case dw_val_class_die_ref:
6170 if (val->v.val_die_ref.die != NULL)
6171 {
6172 dw_die_ref die = val->v.val_die_ref.die;
6173
6174 if (die->comdat_type_p)
6175 {
6176 fprintf (outfile, "die -> signature: ");
6177 print_signature (outfile,
6178 die->die_id.die_type_node->signature);
6179 }
6180 else if (die->die_id.die_symbol)
6181 {
6182 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6183 if (die->with_offset)
6184 fprintf (outfile, " + %ld", die->die_offset);
6185 }
6186 else
6187 fprintf (outfile, "die -> %ld", die->die_offset);
6188 fprintf (outfile, " (%p)", (void *) die);
6189 }
6190 else
6191 fprintf (outfile, "die -> <null>");
6192 break;
6193 case dw_val_class_vms_delta:
6194 fprintf (outfile, "delta: @slotcount(%s-%s)",
6195 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6196 break;
6197 case dw_val_class_lbl_id:
6198 case dw_val_class_lineptr:
6199 case dw_val_class_macptr:
6200 case dw_val_class_loclistsptr:
6201 case dw_val_class_high_pc:
6202 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6203 break;
6204 case dw_val_class_str:
6205 if (val->v.val_str->str != NULL)
6206 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6207 else
6208 fprintf (outfile, "<null>");
6209 break;
6210 case dw_val_class_file:
6211 case dw_val_class_file_implicit:
6212 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6213 val->v.val_file->emitted_number);
6214 break;
6215 case dw_val_class_data8:
6216 {
6217 int i;
6218
6219 for (i = 0; i < 8; i++)
6220 fprintf (outfile, "%02x", val->v.val_data8[i]);
6221 break;
6222 }
6223 case dw_val_class_discr_value:
6224 print_discr_value (outfile, &val->v.val_discr_value);
6225 break;
6226 case dw_val_class_discr_list:
6227 for (dw_discr_list_ref node = val->v.val_discr_list;
6228 node != NULL;
6229 node = node->dw_discr_next)
6230 {
6231 if (node->dw_discr_range)
6232 {
6233 fprintf (outfile, " .. ");
6234 print_discr_value (outfile, &node->dw_discr_lower_bound);
6235 print_discr_value (outfile, &node->dw_discr_upper_bound);
6236 }
6237 else
6238 print_discr_value (outfile, &node->dw_discr_lower_bound);
6239
6240 if (node->dw_discr_next != NULL)
6241 fprintf (outfile, " | ");
6242 }
6243 default:
6244 break;
6245 }
6246 }
6247
6248 /* Likewise, for a DIE attribute. */
6249
6250 static void
6251 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6252 {
6253 print_dw_val (&a->dw_attr_val, recurse, outfile);
6254 }
6255
6256
6257 /* Print the list of operands in the LOC location description to OUTFILE. This
6258 routine is a debugging aid only. */
6259
6260 static void
6261 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6262 {
6263 dw_loc_descr_ref l = loc;
6264
6265 if (loc == NULL)
6266 {
6267 print_spaces (outfile);
6268 fprintf (outfile, "<null>\n");
6269 return;
6270 }
6271
6272 for (l = loc; l != NULL; l = l->dw_loc_next)
6273 {
6274 print_spaces (outfile);
6275 fprintf (outfile, "(%p) %s",
6276 (void *) l,
6277 dwarf_stack_op_name (l->dw_loc_opc));
6278 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6279 {
6280 fprintf (outfile, " ");
6281 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6282 }
6283 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6284 {
6285 fprintf (outfile, ", ");
6286 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6287 }
6288 fprintf (outfile, "\n");
6289 }
6290 }
6291
6292 /* Print the information associated with a given DIE, and its children.
6293 This routine is a debugging aid only. */
6294
6295 static void
6296 print_die (dw_die_ref die, FILE *outfile)
6297 {
6298 dw_attr_node *a;
6299 dw_die_ref c;
6300 unsigned ix;
6301
6302 print_spaces (outfile);
6303 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6304 die->die_offset, dwarf_tag_name (die->die_tag),
6305 (void*) die);
6306 print_spaces (outfile);
6307 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6308 fprintf (outfile, " offset: %ld", die->die_offset);
6309 fprintf (outfile, " mark: %d\n", die->die_mark);
6310
6311 if (die->comdat_type_p)
6312 {
6313 print_spaces (outfile);
6314 fprintf (outfile, " signature: ");
6315 print_signature (outfile, die->die_id.die_type_node->signature);
6316 fprintf (outfile, "\n");
6317 }
6318
6319 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6320 {
6321 print_spaces (outfile);
6322 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6323
6324 print_attribute (a, true, outfile);
6325 fprintf (outfile, "\n");
6326 }
6327
6328 if (die->die_child != NULL)
6329 {
6330 print_indent += 4;
6331 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6332 print_indent -= 4;
6333 }
6334 if (print_indent == 0)
6335 fprintf (outfile, "\n");
6336 }
6337
6338 /* Print the list of operations in the LOC location description. */
6339
6340 DEBUG_FUNCTION void
6341 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6342 {
6343 print_loc_descr (loc, stderr);
6344 }
6345
6346 /* Print the information collected for a given DIE. */
6347
6348 DEBUG_FUNCTION void
6349 debug_dwarf_die (dw_die_ref die)
6350 {
6351 print_die (die, stderr);
6352 }
6353
6354 DEBUG_FUNCTION void
6355 debug (die_struct &ref)
6356 {
6357 print_die (&ref, stderr);
6358 }
6359
6360 DEBUG_FUNCTION void
6361 debug (die_struct *ptr)
6362 {
6363 if (ptr)
6364 debug (*ptr);
6365 else
6366 fprintf (stderr, "<nil>\n");
6367 }
6368
6369
6370 /* Print all DWARF information collected for the compilation unit.
6371 This routine is a debugging aid only. */
6372
6373 DEBUG_FUNCTION void
6374 debug_dwarf (void)
6375 {
6376 print_indent = 0;
6377 print_die (comp_unit_die (), stderr);
6378 }
6379
6380 /* Verify the DIE tree structure. */
6381
6382 DEBUG_FUNCTION void
6383 verify_die (dw_die_ref die)
6384 {
6385 gcc_assert (!die->die_mark);
6386 if (die->die_parent == NULL
6387 && die->die_sib == NULL)
6388 return;
6389 /* Verify the die_sib list is cyclic. */
6390 dw_die_ref x = die;
6391 do
6392 {
6393 x->die_mark = 1;
6394 x = x->die_sib;
6395 }
6396 while (x && !x->die_mark);
6397 gcc_assert (x == die);
6398 x = die;
6399 do
6400 {
6401 /* Verify all dies have the same parent. */
6402 gcc_assert (x->die_parent == die->die_parent);
6403 if (x->die_child)
6404 {
6405 /* Verify the child has the proper parent and recurse. */
6406 gcc_assert (x->die_child->die_parent == x);
6407 verify_die (x->die_child);
6408 }
6409 x->die_mark = 0;
6410 x = x->die_sib;
6411 }
6412 while (x && x->die_mark);
6413 }
6414
6415 /* Sanity checks on DIEs. */
6416
6417 static void
6418 check_die (dw_die_ref die)
6419 {
6420 unsigned ix;
6421 dw_attr_node *a;
6422 bool inline_found = false;
6423 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6424 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6425 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6426 {
6427 switch (a->dw_attr)
6428 {
6429 case DW_AT_inline:
6430 if (a->dw_attr_val.v.val_unsigned)
6431 inline_found = true;
6432 break;
6433 case DW_AT_location:
6434 ++n_location;
6435 break;
6436 case DW_AT_low_pc:
6437 ++n_low_pc;
6438 break;
6439 case DW_AT_high_pc:
6440 ++n_high_pc;
6441 break;
6442 case DW_AT_artificial:
6443 ++n_artificial;
6444 break;
6445 case DW_AT_decl_column:
6446 ++n_decl_column;
6447 break;
6448 case DW_AT_decl_line:
6449 ++n_decl_line;
6450 break;
6451 case DW_AT_decl_file:
6452 ++n_decl_file;
6453 break;
6454 default:
6455 break;
6456 }
6457 }
6458 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6459 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6460 {
6461 fprintf (stderr, "Duplicate attributes in DIE:\n");
6462 debug_dwarf_die (die);
6463 gcc_unreachable ();
6464 }
6465 if (inline_found)
6466 {
6467 /* A debugging information entry that is a member of an abstract
6468 instance tree [that has DW_AT_inline] should not contain any
6469 attributes which describe aspects of the subroutine which vary
6470 between distinct inlined expansions or distinct out-of-line
6471 expansions. */
6472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6473 gcc_assert (a->dw_attr != DW_AT_low_pc
6474 && a->dw_attr != DW_AT_high_pc
6475 && a->dw_attr != DW_AT_location
6476 && a->dw_attr != DW_AT_frame_base
6477 && a->dw_attr != DW_AT_call_all_calls
6478 && a->dw_attr != DW_AT_GNU_all_call_sites);
6479 }
6480 }
6481 \f
6482 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6483 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6484 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6485
6486 /* Calculate the checksum of a location expression. */
6487
6488 static inline void
6489 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6490 {
6491 int tem;
6492 inchash::hash hstate;
6493 hashval_t hash;
6494
6495 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6496 CHECKSUM (tem);
6497 hash_loc_operands (loc, hstate);
6498 hash = hstate.end();
6499 CHECKSUM (hash);
6500 }
6501
6502 /* Calculate the checksum of an attribute. */
6503
6504 static void
6505 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6506 {
6507 dw_loc_descr_ref loc;
6508 rtx r;
6509
6510 CHECKSUM (at->dw_attr);
6511
6512 /* We don't care that this was compiled with a different compiler
6513 snapshot; if the output is the same, that's what matters. */
6514 if (at->dw_attr == DW_AT_producer)
6515 return;
6516
6517 switch (AT_class (at))
6518 {
6519 case dw_val_class_const:
6520 case dw_val_class_const_implicit:
6521 CHECKSUM (at->dw_attr_val.v.val_int);
6522 break;
6523 case dw_val_class_unsigned_const:
6524 case dw_val_class_unsigned_const_implicit:
6525 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6526 break;
6527 case dw_val_class_const_double:
6528 CHECKSUM (at->dw_attr_val.v.val_double);
6529 break;
6530 case dw_val_class_wide_int:
6531 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6532 get_full_len (*at->dw_attr_val.v.val_wide)
6533 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6534 break;
6535 case dw_val_class_vec:
6536 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6537 (at->dw_attr_val.v.val_vec.length
6538 * at->dw_attr_val.v.val_vec.elt_size));
6539 break;
6540 case dw_val_class_flag:
6541 CHECKSUM (at->dw_attr_val.v.val_flag);
6542 break;
6543 case dw_val_class_str:
6544 CHECKSUM_STRING (AT_string (at));
6545 break;
6546
6547 case dw_val_class_addr:
6548 r = AT_addr (at);
6549 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6550 CHECKSUM_STRING (XSTR (r, 0));
6551 break;
6552
6553 case dw_val_class_offset:
6554 CHECKSUM (at->dw_attr_val.v.val_offset);
6555 break;
6556
6557 case dw_val_class_loc:
6558 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6559 loc_checksum (loc, ctx);
6560 break;
6561
6562 case dw_val_class_die_ref:
6563 die_checksum (AT_ref (at), ctx, mark);
6564 break;
6565
6566 case dw_val_class_fde_ref:
6567 case dw_val_class_vms_delta:
6568 case dw_val_class_lbl_id:
6569 case dw_val_class_lineptr:
6570 case dw_val_class_macptr:
6571 case dw_val_class_loclistsptr:
6572 case dw_val_class_high_pc:
6573 break;
6574
6575 case dw_val_class_file:
6576 case dw_val_class_file_implicit:
6577 CHECKSUM_STRING (AT_file (at)->filename);
6578 break;
6579
6580 case dw_val_class_data8:
6581 CHECKSUM (at->dw_attr_val.v.val_data8);
6582 break;
6583
6584 default:
6585 break;
6586 }
6587 }
6588
6589 /* Calculate the checksum of a DIE. */
6590
6591 static void
6592 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6593 {
6594 dw_die_ref c;
6595 dw_attr_node *a;
6596 unsigned ix;
6597
6598 /* To avoid infinite recursion. */
6599 if (die->die_mark)
6600 {
6601 CHECKSUM (die->die_mark);
6602 return;
6603 }
6604 die->die_mark = ++(*mark);
6605
6606 CHECKSUM (die->die_tag);
6607
6608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6609 attr_checksum (a, ctx, mark);
6610
6611 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6612 }
6613
6614 #undef CHECKSUM
6615 #undef CHECKSUM_BLOCK
6616 #undef CHECKSUM_STRING
6617
6618 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6619 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6620 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6621 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6622 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6623 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6624 #define CHECKSUM_ATTR(FOO) \
6625 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6626
6627 /* Calculate the checksum of a number in signed LEB128 format. */
6628
6629 static void
6630 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6631 {
6632 unsigned char byte;
6633 bool more;
6634
6635 while (1)
6636 {
6637 byte = (value & 0x7f);
6638 value >>= 7;
6639 more = !((value == 0 && (byte & 0x40) == 0)
6640 || (value == -1 && (byte & 0x40) != 0));
6641 if (more)
6642 byte |= 0x80;
6643 CHECKSUM (byte);
6644 if (!more)
6645 break;
6646 }
6647 }
6648
6649 /* Calculate the checksum of a number in unsigned LEB128 format. */
6650
6651 static void
6652 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6653 {
6654 while (1)
6655 {
6656 unsigned char byte = (value & 0x7f);
6657 value >>= 7;
6658 if (value != 0)
6659 /* More bytes to follow. */
6660 byte |= 0x80;
6661 CHECKSUM (byte);
6662 if (value == 0)
6663 break;
6664 }
6665 }
6666
6667 /* Checksum the context of the DIE. This adds the names of any
6668 surrounding namespaces or structures to the checksum. */
6669
6670 static void
6671 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6672 {
6673 const char *name;
6674 dw_die_ref spec;
6675 int tag = die->die_tag;
6676
6677 if (tag != DW_TAG_namespace
6678 && tag != DW_TAG_structure_type
6679 && tag != DW_TAG_class_type)
6680 return;
6681
6682 name = get_AT_string (die, DW_AT_name);
6683
6684 spec = get_AT_ref (die, DW_AT_specification);
6685 if (spec != NULL)
6686 die = spec;
6687
6688 if (die->die_parent != NULL)
6689 checksum_die_context (die->die_parent, ctx);
6690
6691 CHECKSUM_ULEB128 ('C');
6692 CHECKSUM_ULEB128 (tag);
6693 if (name != NULL)
6694 CHECKSUM_STRING (name);
6695 }
6696
6697 /* Calculate the checksum of a location expression. */
6698
6699 static inline void
6700 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6701 {
6702 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6703 were emitted as a DW_FORM_sdata instead of a location expression. */
6704 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6705 {
6706 CHECKSUM_ULEB128 (DW_FORM_sdata);
6707 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6708 return;
6709 }
6710
6711 /* Otherwise, just checksum the raw location expression. */
6712 while (loc != NULL)
6713 {
6714 inchash::hash hstate;
6715 hashval_t hash;
6716
6717 CHECKSUM_ULEB128 (loc->dtprel);
6718 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6719 hash_loc_operands (loc, hstate);
6720 hash = hstate.end ();
6721 CHECKSUM (hash);
6722 loc = loc->dw_loc_next;
6723 }
6724 }
6725
6726 /* Calculate the checksum of an attribute. */
6727
6728 static void
6729 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6730 struct md5_ctx *ctx, int *mark)
6731 {
6732 dw_loc_descr_ref loc;
6733 rtx r;
6734
6735 if (AT_class (at) == dw_val_class_die_ref)
6736 {
6737 dw_die_ref target_die = AT_ref (at);
6738
6739 /* For pointer and reference types, we checksum only the (qualified)
6740 name of the target type (if there is a name). For friend entries,
6741 we checksum only the (qualified) name of the target type or function.
6742 This allows the checksum to remain the same whether the target type
6743 is complete or not. */
6744 if ((at->dw_attr == DW_AT_type
6745 && (tag == DW_TAG_pointer_type
6746 || tag == DW_TAG_reference_type
6747 || tag == DW_TAG_rvalue_reference_type
6748 || tag == DW_TAG_ptr_to_member_type))
6749 || (at->dw_attr == DW_AT_friend
6750 && tag == DW_TAG_friend))
6751 {
6752 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6753
6754 if (name_attr != NULL)
6755 {
6756 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6757
6758 if (decl == NULL)
6759 decl = target_die;
6760 CHECKSUM_ULEB128 ('N');
6761 CHECKSUM_ULEB128 (at->dw_attr);
6762 if (decl->die_parent != NULL)
6763 checksum_die_context (decl->die_parent, ctx);
6764 CHECKSUM_ULEB128 ('E');
6765 CHECKSUM_STRING (AT_string (name_attr));
6766 return;
6767 }
6768 }
6769
6770 /* For all other references to another DIE, we check to see if the
6771 target DIE has already been visited. If it has, we emit a
6772 backward reference; if not, we descend recursively. */
6773 if (target_die->die_mark > 0)
6774 {
6775 CHECKSUM_ULEB128 ('R');
6776 CHECKSUM_ULEB128 (at->dw_attr);
6777 CHECKSUM_ULEB128 (target_die->die_mark);
6778 }
6779 else
6780 {
6781 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6782
6783 if (decl == NULL)
6784 decl = target_die;
6785 target_die->die_mark = ++(*mark);
6786 CHECKSUM_ULEB128 ('T');
6787 CHECKSUM_ULEB128 (at->dw_attr);
6788 if (decl->die_parent != NULL)
6789 checksum_die_context (decl->die_parent, ctx);
6790 die_checksum_ordered (target_die, ctx, mark);
6791 }
6792 return;
6793 }
6794
6795 CHECKSUM_ULEB128 ('A');
6796 CHECKSUM_ULEB128 (at->dw_attr);
6797
6798 switch (AT_class (at))
6799 {
6800 case dw_val_class_const:
6801 case dw_val_class_const_implicit:
6802 CHECKSUM_ULEB128 (DW_FORM_sdata);
6803 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6804 break;
6805
6806 case dw_val_class_unsigned_const:
6807 case dw_val_class_unsigned_const_implicit:
6808 CHECKSUM_ULEB128 (DW_FORM_sdata);
6809 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6810 break;
6811
6812 case dw_val_class_const_double:
6813 CHECKSUM_ULEB128 (DW_FORM_block);
6814 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6815 CHECKSUM (at->dw_attr_val.v.val_double);
6816 break;
6817
6818 case dw_val_class_wide_int:
6819 CHECKSUM_ULEB128 (DW_FORM_block);
6820 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6821 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6822 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6823 get_full_len (*at->dw_attr_val.v.val_wide)
6824 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6825 break;
6826
6827 case dw_val_class_vec:
6828 CHECKSUM_ULEB128 (DW_FORM_block);
6829 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6830 * at->dw_attr_val.v.val_vec.elt_size);
6831 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6832 (at->dw_attr_val.v.val_vec.length
6833 * at->dw_attr_val.v.val_vec.elt_size));
6834 break;
6835
6836 case dw_val_class_flag:
6837 CHECKSUM_ULEB128 (DW_FORM_flag);
6838 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6839 break;
6840
6841 case dw_val_class_str:
6842 CHECKSUM_ULEB128 (DW_FORM_string);
6843 CHECKSUM_STRING (AT_string (at));
6844 break;
6845
6846 case dw_val_class_addr:
6847 r = AT_addr (at);
6848 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6849 CHECKSUM_ULEB128 (DW_FORM_string);
6850 CHECKSUM_STRING (XSTR (r, 0));
6851 break;
6852
6853 case dw_val_class_offset:
6854 CHECKSUM_ULEB128 (DW_FORM_sdata);
6855 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6856 break;
6857
6858 case dw_val_class_loc:
6859 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6860 loc_checksum_ordered (loc, ctx);
6861 break;
6862
6863 case dw_val_class_fde_ref:
6864 case dw_val_class_lbl_id:
6865 case dw_val_class_lineptr:
6866 case dw_val_class_macptr:
6867 case dw_val_class_loclistsptr:
6868 case dw_val_class_high_pc:
6869 break;
6870
6871 case dw_val_class_file:
6872 case dw_val_class_file_implicit:
6873 CHECKSUM_ULEB128 (DW_FORM_string);
6874 CHECKSUM_STRING (AT_file (at)->filename);
6875 break;
6876
6877 case dw_val_class_data8:
6878 CHECKSUM (at->dw_attr_val.v.val_data8);
6879 break;
6880
6881 default:
6882 break;
6883 }
6884 }
6885
6886 struct checksum_attributes
6887 {
6888 dw_attr_node *at_name;
6889 dw_attr_node *at_type;
6890 dw_attr_node *at_friend;
6891 dw_attr_node *at_accessibility;
6892 dw_attr_node *at_address_class;
6893 dw_attr_node *at_alignment;
6894 dw_attr_node *at_allocated;
6895 dw_attr_node *at_artificial;
6896 dw_attr_node *at_associated;
6897 dw_attr_node *at_binary_scale;
6898 dw_attr_node *at_bit_offset;
6899 dw_attr_node *at_bit_size;
6900 dw_attr_node *at_bit_stride;
6901 dw_attr_node *at_byte_size;
6902 dw_attr_node *at_byte_stride;
6903 dw_attr_node *at_const_value;
6904 dw_attr_node *at_containing_type;
6905 dw_attr_node *at_count;
6906 dw_attr_node *at_data_location;
6907 dw_attr_node *at_data_member_location;
6908 dw_attr_node *at_decimal_scale;
6909 dw_attr_node *at_decimal_sign;
6910 dw_attr_node *at_default_value;
6911 dw_attr_node *at_digit_count;
6912 dw_attr_node *at_discr;
6913 dw_attr_node *at_discr_list;
6914 dw_attr_node *at_discr_value;
6915 dw_attr_node *at_encoding;
6916 dw_attr_node *at_endianity;
6917 dw_attr_node *at_explicit;
6918 dw_attr_node *at_is_optional;
6919 dw_attr_node *at_location;
6920 dw_attr_node *at_lower_bound;
6921 dw_attr_node *at_mutable;
6922 dw_attr_node *at_ordering;
6923 dw_attr_node *at_picture_string;
6924 dw_attr_node *at_prototyped;
6925 dw_attr_node *at_small;
6926 dw_attr_node *at_segment;
6927 dw_attr_node *at_string_length;
6928 dw_attr_node *at_string_length_bit_size;
6929 dw_attr_node *at_string_length_byte_size;
6930 dw_attr_node *at_threads_scaled;
6931 dw_attr_node *at_upper_bound;
6932 dw_attr_node *at_use_location;
6933 dw_attr_node *at_use_UTF8;
6934 dw_attr_node *at_variable_parameter;
6935 dw_attr_node *at_virtuality;
6936 dw_attr_node *at_visibility;
6937 dw_attr_node *at_vtable_elem_location;
6938 };
6939
6940 /* Collect the attributes that we will want to use for the checksum. */
6941
6942 static void
6943 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6944 {
6945 dw_attr_node *a;
6946 unsigned ix;
6947
6948 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6949 {
6950 switch (a->dw_attr)
6951 {
6952 case DW_AT_name:
6953 attrs->at_name = a;
6954 break;
6955 case DW_AT_type:
6956 attrs->at_type = a;
6957 break;
6958 case DW_AT_friend:
6959 attrs->at_friend = a;
6960 break;
6961 case DW_AT_accessibility:
6962 attrs->at_accessibility = a;
6963 break;
6964 case DW_AT_address_class:
6965 attrs->at_address_class = a;
6966 break;
6967 case DW_AT_alignment:
6968 attrs->at_alignment = a;
6969 break;
6970 case DW_AT_allocated:
6971 attrs->at_allocated = a;
6972 break;
6973 case DW_AT_artificial:
6974 attrs->at_artificial = a;
6975 break;
6976 case DW_AT_associated:
6977 attrs->at_associated = a;
6978 break;
6979 case DW_AT_binary_scale:
6980 attrs->at_binary_scale = a;
6981 break;
6982 case DW_AT_bit_offset:
6983 attrs->at_bit_offset = a;
6984 break;
6985 case DW_AT_bit_size:
6986 attrs->at_bit_size = a;
6987 break;
6988 case DW_AT_bit_stride:
6989 attrs->at_bit_stride = a;
6990 break;
6991 case DW_AT_byte_size:
6992 attrs->at_byte_size = a;
6993 break;
6994 case DW_AT_byte_stride:
6995 attrs->at_byte_stride = a;
6996 break;
6997 case DW_AT_const_value:
6998 attrs->at_const_value = a;
6999 break;
7000 case DW_AT_containing_type:
7001 attrs->at_containing_type = a;
7002 break;
7003 case DW_AT_count:
7004 attrs->at_count = a;
7005 break;
7006 case DW_AT_data_location:
7007 attrs->at_data_location = a;
7008 break;
7009 case DW_AT_data_member_location:
7010 attrs->at_data_member_location = a;
7011 break;
7012 case DW_AT_decimal_scale:
7013 attrs->at_decimal_scale = a;
7014 break;
7015 case DW_AT_decimal_sign:
7016 attrs->at_decimal_sign = a;
7017 break;
7018 case DW_AT_default_value:
7019 attrs->at_default_value = a;
7020 break;
7021 case DW_AT_digit_count:
7022 attrs->at_digit_count = a;
7023 break;
7024 case DW_AT_discr:
7025 attrs->at_discr = a;
7026 break;
7027 case DW_AT_discr_list:
7028 attrs->at_discr_list = a;
7029 break;
7030 case DW_AT_discr_value:
7031 attrs->at_discr_value = a;
7032 break;
7033 case DW_AT_encoding:
7034 attrs->at_encoding = a;
7035 break;
7036 case DW_AT_endianity:
7037 attrs->at_endianity = a;
7038 break;
7039 case DW_AT_explicit:
7040 attrs->at_explicit = a;
7041 break;
7042 case DW_AT_is_optional:
7043 attrs->at_is_optional = a;
7044 break;
7045 case DW_AT_location:
7046 attrs->at_location = a;
7047 break;
7048 case DW_AT_lower_bound:
7049 attrs->at_lower_bound = a;
7050 break;
7051 case DW_AT_mutable:
7052 attrs->at_mutable = a;
7053 break;
7054 case DW_AT_ordering:
7055 attrs->at_ordering = a;
7056 break;
7057 case DW_AT_picture_string:
7058 attrs->at_picture_string = a;
7059 break;
7060 case DW_AT_prototyped:
7061 attrs->at_prototyped = a;
7062 break;
7063 case DW_AT_small:
7064 attrs->at_small = a;
7065 break;
7066 case DW_AT_segment:
7067 attrs->at_segment = a;
7068 break;
7069 case DW_AT_string_length:
7070 attrs->at_string_length = a;
7071 break;
7072 case DW_AT_string_length_bit_size:
7073 attrs->at_string_length_bit_size = a;
7074 break;
7075 case DW_AT_string_length_byte_size:
7076 attrs->at_string_length_byte_size = a;
7077 break;
7078 case DW_AT_threads_scaled:
7079 attrs->at_threads_scaled = a;
7080 break;
7081 case DW_AT_upper_bound:
7082 attrs->at_upper_bound = a;
7083 break;
7084 case DW_AT_use_location:
7085 attrs->at_use_location = a;
7086 break;
7087 case DW_AT_use_UTF8:
7088 attrs->at_use_UTF8 = a;
7089 break;
7090 case DW_AT_variable_parameter:
7091 attrs->at_variable_parameter = a;
7092 break;
7093 case DW_AT_virtuality:
7094 attrs->at_virtuality = a;
7095 break;
7096 case DW_AT_visibility:
7097 attrs->at_visibility = a;
7098 break;
7099 case DW_AT_vtable_elem_location:
7100 attrs->at_vtable_elem_location = a;
7101 break;
7102 default:
7103 break;
7104 }
7105 }
7106 }
7107
7108 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7109
7110 static void
7111 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7112 {
7113 dw_die_ref c;
7114 dw_die_ref decl;
7115 struct checksum_attributes attrs;
7116
7117 CHECKSUM_ULEB128 ('D');
7118 CHECKSUM_ULEB128 (die->die_tag);
7119
7120 memset (&attrs, 0, sizeof (attrs));
7121
7122 decl = get_AT_ref (die, DW_AT_specification);
7123 if (decl != NULL)
7124 collect_checksum_attributes (&attrs, decl);
7125 collect_checksum_attributes (&attrs, die);
7126
7127 CHECKSUM_ATTR (attrs.at_name);
7128 CHECKSUM_ATTR (attrs.at_accessibility);
7129 CHECKSUM_ATTR (attrs.at_address_class);
7130 CHECKSUM_ATTR (attrs.at_allocated);
7131 CHECKSUM_ATTR (attrs.at_artificial);
7132 CHECKSUM_ATTR (attrs.at_associated);
7133 CHECKSUM_ATTR (attrs.at_binary_scale);
7134 CHECKSUM_ATTR (attrs.at_bit_offset);
7135 CHECKSUM_ATTR (attrs.at_bit_size);
7136 CHECKSUM_ATTR (attrs.at_bit_stride);
7137 CHECKSUM_ATTR (attrs.at_byte_size);
7138 CHECKSUM_ATTR (attrs.at_byte_stride);
7139 CHECKSUM_ATTR (attrs.at_const_value);
7140 CHECKSUM_ATTR (attrs.at_containing_type);
7141 CHECKSUM_ATTR (attrs.at_count);
7142 CHECKSUM_ATTR (attrs.at_data_location);
7143 CHECKSUM_ATTR (attrs.at_data_member_location);
7144 CHECKSUM_ATTR (attrs.at_decimal_scale);
7145 CHECKSUM_ATTR (attrs.at_decimal_sign);
7146 CHECKSUM_ATTR (attrs.at_default_value);
7147 CHECKSUM_ATTR (attrs.at_digit_count);
7148 CHECKSUM_ATTR (attrs.at_discr);
7149 CHECKSUM_ATTR (attrs.at_discr_list);
7150 CHECKSUM_ATTR (attrs.at_discr_value);
7151 CHECKSUM_ATTR (attrs.at_encoding);
7152 CHECKSUM_ATTR (attrs.at_endianity);
7153 CHECKSUM_ATTR (attrs.at_explicit);
7154 CHECKSUM_ATTR (attrs.at_is_optional);
7155 CHECKSUM_ATTR (attrs.at_location);
7156 CHECKSUM_ATTR (attrs.at_lower_bound);
7157 CHECKSUM_ATTR (attrs.at_mutable);
7158 CHECKSUM_ATTR (attrs.at_ordering);
7159 CHECKSUM_ATTR (attrs.at_picture_string);
7160 CHECKSUM_ATTR (attrs.at_prototyped);
7161 CHECKSUM_ATTR (attrs.at_small);
7162 CHECKSUM_ATTR (attrs.at_segment);
7163 CHECKSUM_ATTR (attrs.at_string_length);
7164 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7165 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7166 CHECKSUM_ATTR (attrs.at_threads_scaled);
7167 CHECKSUM_ATTR (attrs.at_upper_bound);
7168 CHECKSUM_ATTR (attrs.at_use_location);
7169 CHECKSUM_ATTR (attrs.at_use_UTF8);
7170 CHECKSUM_ATTR (attrs.at_variable_parameter);
7171 CHECKSUM_ATTR (attrs.at_virtuality);
7172 CHECKSUM_ATTR (attrs.at_visibility);
7173 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7174 CHECKSUM_ATTR (attrs.at_type);
7175 CHECKSUM_ATTR (attrs.at_friend);
7176 CHECKSUM_ATTR (attrs.at_alignment);
7177
7178 /* Checksum the child DIEs. */
7179 c = die->die_child;
7180 if (c) do {
7181 dw_attr_node *name_attr;
7182
7183 c = c->die_sib;
7184 name_attr = get_AT (c, DW_AT_name);
7185 if (is_template_instantiation (c))
7186 {
7187 /* Ignore instantiations of member type and function templates. */
7188 }
7189 else if (name_attr != NULL
7190 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7191 {
7192 /* Use a shallow checksum for named nested types and member
7193 functions. */
7194 CHECKSUM_ULEB128 ('S');
7195 CHECKSUM_ULEB128 (c->die_tag);
7196 CHECKSUM_STRING (AT_string (name_attr));
7197 }
7198 else
7199 {
7200 /* Use a deep checksum for other children. */
7201 /* Mark this DIE so it gets processed when unmarking. */
7202 if (c->die_mark == 0)
7203 c->die_mark = -1;
7204 die_checksum_ordered (c, ctx, mark);
7205 }
7206 } while (c != die->die_child);
7207
7208 CHECKSUM_ULEB128 (0);
7209 }
7210
7211 /* Add a type name and tag to a hash. */
7212 static void
7213 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7214 {
7215 CHECKSUM_ULEB128 (tag);
7216 CHECKSUM_STRING (name);
7217 }
7218
7219 #undef CHECKSUM
7220 #undef CHECKSUM_STRING
7221 #undef CHECKSUM_ATTR
7222 #undef CHECKSUM_LEB128
7223 #undef CHECKSUM_ULEB128
7224
7225 /* Generate the type signature for DIE. This is computed by generating an
7226 MD5 checksum over the DIE's tag, its relevant attributes, and its
7227 children. Attributes that are references to other DIEs are processed
7228 by recursion, using the MARK field to prevent infinite recursion.
7229 If the DIE is nested inside a namespace or another type, we also
7230 need to include that context in the signature. The lower 64 bits
7231 of the resulting MD5 checksum comprise the signature. */
7232
7233 static void
7234 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7235 {
7236 int mark;
7237 const char *name;
7238 unsigned char checksum[16];
7239 struct md5_ctx ctx;
7240 dw_die_ref decl;
7241 dw_die_ref parent;
7242
7243 name = get_AT_string (die, DW_AT_name);
7244 decl = get_AT_ref (die, DW_AT_specification);
7245 parent = get_die_parent (die);
7246
7247 /* First, compute a signature for just the type name (and its surrounding
7248 context, if any. This is stored in the type unit DIE for link-time
7249 ODR (one-definition rule) checking. */
7250
7251 if (is_cxx () && name != NULL)
7252 {
7253 md5_init_ctx (&ctx);
7254
7255 /* Checksum the names of surrounding namespaces and structures. */
7256 if (parent != NULL)
7257 checksum_die_context (parent, &ctx);
7258
7259 /* Checksum the current DIE. */
7260 die_odr_checksum (die->die_tag, name, &ctx);
7261 md5_finish_ctx (&ctx, checksum);
7262
7263 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7264 }
7265
7266 /* Next, compute the complete type signature. */
7267
7268 md5_init_ctx (&ctx);
7269 mark = 1;
7270 die->die_mark = mark;
7271
7272 /* Checksum the names of surrounding namespaces and structures. */
7273 if (parent != NULL)
7274 checksum_die_context (parent, &ctx);
7275
7276 /* Checksum the DIE and its children. */
7277 die_checksum_ordered (die, &ctx, &mark);
7278 unmark_all_dies (die);
7279 md5_finish_ctx (&ctx, checksum);
7280
7281 /* Store the signature in the type node and link the type DIE and the
7282 type node together. */
7283 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7284 DWARF_TYPE_SIGNATURE_SIZE);
7285 die->comdat_type_p = true;
7286 die->die_id.die_type_node = type_node;
7287 type_node->type_die = die;
7288
7289 /* If the DIE is a specification, link its declaration to the type node
7290 as well. */
7291 if (decl != NULL)
7292 {
7293 decl->comdat_type_p = true;
7294 decl->die_id.die_type_node = type_node;
7295 }
7296 }
7297
7298 /* Do the location expressions look same? */
7299 static inline int
7300 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7301 {
7302 return loc1->dw_loc_opc == loc2->dw_loc_opc
7303 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7304 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7305 }
7306
7307 /* Do the values look the same? */
7308 static int
7309 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7310 {
7311 dw_loc_descr_ref loc1, loc2;
7312 rtx r1, r2;
7313
7314 if (v1->val_class != v2->val_class)
7315 return 0;
7316
7317 switch (v1->val_class)
7318 {
7319 case dw_val_class_const:
7320 case dw_val_class_const_implicit:
7321 return v1->v.val_int == v2->v.val_int;
7322 case dw_val_class_unsigned_const:
7323 case dw_val_class_unsigned_const_implicit:
7324 return v1->v.val_unsigned == v2->v.val_unsigned;
7325 case dw_val_class_const_double:
7326 return v1->v.val_double.high == v2->v.val_double.high
7327 && v1->v.val_double.low == v2->v.val_double.low;
7328 case dw_val_class_wide_int:
7329 return *v1->v.val_wide == *v2->v.val_wide;
7330 case dw_val_class_vec:
7331 if (v1->v.val_vec.length != v2->v.val_vec.length
7332 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7333 return 0;
7334 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7335 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7336 return 0;
7337 return 1;
7338 case dw_val_class_flag:
7339 return v1->v.val_flag == v2->v.val_flag;
7340 case dw_val_class_str:
7341 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7342
7343 case dw_val_class_addr:
7344 r1 = v1->v.val_addr;
7345 r2 = v2->v.val_addr;
7346 if (GET_CODE (r1) != GET_CODE (r2))
7347 return 0;
7348 return !rtx_equal_p (r1, r2);
7349
7350 case dw_val_class_offset:
7351 return v1->v.val_offset == v2->v.val_offset;
7352
7353 case dw_val_class_loc:
7354 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7355 loc1 && loc2;
7356 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7357 if (!same_loc_p (loc1, loc2, mark))
7358 return 0;
7359 return !loc1 && !loc2;
7360
7361 case dw_val_class_die_ref:
7362 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7363
7364 case dw_val_class_fde_ref:
7365 case dw_val_class_vms_delta:
7366 case dw_val_class_lbl_id:
7367 case dw_val_class_lineptr:
7368 case dw_val_class_macptr:
7369 case dw_val_class_loclistsptr:
7370 case dw_val_class_high_pc:
7371 return 1;
7372
7373 case dw_val_class_file:
7374 case dw_val_class_file_implicit:
7375 return v1->v.val_file == v2->v.val_file;
7376
7377 case dw_val_class_data8:
7378 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7379
7380 default:
7381 return 1;
7382 }
7383 }
7384
7385 /* Do the attributes look the same? */
7386
7387 static int
7388 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7389 {
7390 if (at1->dw_attr != at2->dw_attr)
7391 return 0;
7392
7393 /* We don't care that this was compiled with a different compiler
7394 snapshot; if the output is the same, that's what matters. */
7395 if (at1->dw_attr == DW_AT_producer)
7396 return 1;
7397
7398 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7399 }
7400
7401 /* Do the dies look the same? */
7402
7403 static int
7404 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7405 {
7406 dw_die_ref c1, c2;
7407 dw_attr_node *a1;
7408 unsigned ix;
7409
7410 /* To avoid infinite recursion. */
7411 if (die1->die_mark)
7412 return die1->die_mark == die2->die_mark;
7413 die1->die_mark = die2->die_mark = ++(*mark);
7414
7415 if (die1->die_tag != die2->die_tag)
7416 return 0;
7417
7418 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7419 return 0;
7420
7421 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7422 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7423 return 0;
7424
7425 c1 = die1->die_child;
7426 c2 = die2->die_child;
7427 if (! c1)
7428 {
7429 if (c2)
7430 return 0;
7431 }
7432 else
7433 for (;;)
7434 {
7435 if (!same_die_p (c1, c2, mark))
7436 return 0;
7437 c1 = c1->die_sib;
7438 c2 = c2->die_sib;
7439 if (c1 == die1->die_child)
7440 {
7441 if (c2 == die2->die_child)
7442 break;
7443 else
7444 return 0;
7445 }
7446 }
7447
7448 return 1;
7449 }
7450
7451 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7452 children, and set die_symbol. */
7453
7454 static void
7455 compute_comp_unit_symbol (dw_die_ref unit_die)
7456 {
7457 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7458 const char *base = die_name ? lbasename (die_name) : "anonymous";
7459 char *name = XALLOCAVEC (char, strlen (base) + 64);
7460 char *p;
7461 int i, mark;
7462 unsigned char checksum[16];
7463 struct md5_ctx ctx;
7464
7465 /* Compute the checksum of the DIE, then append part of it as hex digits to
7466 the name filename of the unit. */
7467
7468 md5_init_ctx (&ctx);
7469 mark = 0;
7470 die_checksum (unit_die, &ctx, &mark);
7471 unmark_all_dies (unit_die);
7472 md5_finish_ctx (&ctx, checksum);
7473
7474 /* When we this for comp_unit_die () we have a DW_AT_name that might
7475 not start with a letter but with anything valid for filenames and
7476 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7477 character is not a letter. */
7478 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7479 clean_symbol_name (name);
7480
7481 p = name + strlen (name);
7482 for (i = 0; i < 4; i++)
7483 {
7484 sprintf (p, "%.2x", checksum[i]);
7485 p += 2;
7486 }
7487
7488 unit_die->die_id.die_symbol = xstrdup (name);
7489 }
7490
7491 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7492
7493 static int
7494 is_type_die (dw_die_ref die)
7495 {
7496 switch (die->die_tag)
7497 {
7498 case DW_TAG_array_type:
7499 case DW_TAG_class_type:
7500 case DW_TAG_interface_type:
7501 case DW_TAG_enumeration_type:
7502 case DW_TAG_pointer_type:
7503 case DW_TAG_reference_type:
7504 case DW_TAG_rvalue_reference_type:
7505 case DW_TAG_string_type:
7506 case DW_TAG_structure_type:
7507 case DW_TAG_subroutine_type:
7508 case DW_TAG_union_type:
7509 case DW_TAG_ptr_to_member_type:
7510 case DW_TAG_set_type:
7511 case DW_TAG_subrange_type:
7512 case DW_TAG_base_type:
7513 case DW_TAG_const_type:
7514 case DW_TAG_file_type:
7515 case DW_TAG_packed_type:
7516 case DW_TAG_volatile_type:
7517 case DW_TAG_typedef:
7518 return 1;
7519 default:
7520 return 0;
7521 }
7522 }
7523
7524 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7525 Basically, we want to choose the bits that are likely to be shared between
7526 compilations (types) and leave out the bits that are specific to individual
7527 compilations (functions). */
7528
7529 static int
7530 is_comdat_die (dw_die_ref c)
7531 {
7532 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7533 we do for stabs. The advantage is a greater likelihood of sharing between
7534 objects that don't include headers in the same order (and therefore would
7535 put the base types in a different comdat). jason 8/28/00 */
7536
7537 if (c->die_tag == DW_TAG_base_type)
7538 return 0;
7539
7540 if (c->die_tag == DW_TAG_pointer_type
7541 || c->die_tag == DW_TAG_reference_type
7542 || c->die_tag == DW_TAG_rvalue_reference_type
7543 || c->die_tag == DW_TAG_const_type
7544 || c->die_tag == DW_TAG_volatile_type)
7545 {
7546 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7547
7548 return t ? is_comdat_die (t) : 0;
7549 }
7550
7551 return is_type_die (c);
7552 }
7553
7554 /* Returns true iff C is a compile-unit DIE. */
7555
7556 static inline bool
7557 is_cu_die (dw_die_ref c)
7558 {
7559 return c && (c->die_tag == DW_TAG_compile_unit
7560 || c->die_tag == DW_TAG_skeleton_unit);
7561 }
7562
7563 /* Returns true iff C is a unit DIE of some sort. */
7564
7565 static inline bool
7566 is_unit_die (dw_die_ref c)
7567 {
7568 return c && (c->die_tag == DW_TAG_compile_unit
7569 || c->die_tag == DW_TAG_partial_unit
7570 || c->die_tag == DW_TAG_type_unit
7571 || c->die_tag == DW_TAG_skeleton_unit);
7572 }
7573
7574 /* Returns true iff C is a namespace DIE. */
7575
7576 static inline bool
7577 is_namespace_die (dw_die_ref c)
7578 {
7579 return c && c->die_tag == DW_TAG_namespace;
7580 }
7581
7582 /* Returns true iff C is a class or structure DIE. */
7583
7584 static inline bool
7585 is_class_die (dw_die_ref c)
7586 {
7587 return c && (c->die_tag == DW_TAG_class_type
7588 || c->die_tag == DW_TAG_structure_type);
7589 }
7590
7591 /* Return non-zero if this DIE is a template parameter. */
7592
7593 static inline bool
7594 is_template_parameter (dw_die_ref die)
7595 {
7596 switch (die->die_tag)
7597 {
7598 case DW_TAG_template_type_param:
7599 case DW_TAG_template_value_param:
7600 case DW_TAG_GNU_template_template_param:
7601 case DW_TAG_GNU_template_parameter_pack:
7602 return true;
7603 default:
7604 return false;
7605 }
7606 }
7607
7608 /* Return non-zero if this DIE represents a template instantiation. */
7609
7610 static inline bool
7611 is_template_instantiation (dw_die_ref die)
7612 {
7613 dw_die_ref c;
7614
7615 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7616 return false;
7617 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7618 return false;
7619 }
7620
7621 static char *
7622 gen_internal_sym (const char *prefix)
7623 {
7624 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7625
7626 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7627 return xstrdup (buf);
7628 }
7629
7630 /* Return non-zero if this DIE is a declaration. */
7631
7632 static int
7633 is_declaration_die (dw_die_ref die)
7634 {
7635 dw_attr_node *a;
7636 unsigned ix;
7637
7638 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7639 if (a->dw_attr == DW_AT_declaration)
7640 return 1;
7641
7642 return 0;
7643 }
7644
7645 /* Return non-zero if this DIE is nested inside a subprogram. */
7646
7647 static int
7648 is_nested_in_subprogram (dw_die_ref die)
7649 {
7650 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7651
7652 if (decl == NULL)
7653 decl = die;
7654 return local_scope_p (decl);
7655 }
7656
7657 /* Return non-zero if this DIE contains a defining declaration of a
7658 subprogram. */
7659
7660 static int
7661 contains_subprogram_definition (dw_die_ref die)
7662 {
7663 dw_die_ref c;
7664
7665 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7666 return 1;
7667 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7668 return 0;
7669 }
7670
7671 /* Return non-zero if this is a type DIE that should be moved to a
7672 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7673 unit type. */
7674
7675 static int
7676 should_move_die_to_comdat (dw_die_ref die)
7677 {
7678 switch (die->die_tag)
7679 {
7680 case DW_TAG_class_type:
7681 case DW_TAG_structure_type:
7682 case DW_TAG_enumeration_type:
7683 case DW_TAG_union_type:
7684 /* Don't move declarations, inlined instances, types nested in a
7685 subprogram, or types that contain subprogram definitions. */
7686 if (is_declaration_die (die)
7687 || get_AT (die, DW_AT_abstract_origin)
7688 || is_nested_in_subprogram (die)
7689 || contains_subprogram_definition (die))
7690 return 0;
7691 return 1;
7692 case DW_TAG_array_type:
7693 case DW_TAG_interface_type:
7694 case DW_TAG_pointer_type:
7695 case DW_TAG_reference_type:
7696 case DW_TAG_rvalue_reference_type:
7697 case DW_TAG_string_type:
7698 case DW_TAG_subroutine_type:
7699 case DW_TAG_ptr_to_member_type:
7700 case DW_TAG_set_type:
7701 case DW_TAG_subrange_type:
7702 case DW_TAG_base_type:
7703 case DW_TAG_const_type:
7704 case DW_TAG_file_type:
7705 case DW_TAG_packed_type:
7706 case DW_TAG_volatile_type:
7707 case DW_TAG_typedef:
7708 default:
7709 return 0;
7710 }
7711 }
7712
7713 /* Make a clone of DIE. */
7714
7715 static dw_die_ref
7716 clone_die (dw_die_ref die)
7717 {
7718 dw_die_ref clone = new_die_raw (die->die_tag);
7719 dw_attr_node *a;
7720 unsigned ix;
7721
7722 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7723 add_dwarf_attr (clone, a);
7724
7725 return clone;
7726 }
7727
7728 /* Make a clone of the tree rooted at DIE. */
7729
7730 static dw_die_ref
7731 clone_tree (dw_die_ref die)
7732 {
7733 dw_die_ref c;
7734 dw_die_ref clone = clone_die (die);
7735
7736 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7737
7738 return clone;
7739 }
7740
7741 /* Make a clone of DIE as a declaration. */
7742
7743 static dw_die_ref
7744 clone_as_declaration (dw_die_ref die)
7745 {
7746 dw_die_ref clone;
7747 dw_die_ref decl;
7748 dw_attr_node *a;
7749 unsigned ix;
7750
7751 /* If the DIE is already a declaration, just clone it. */
7752 if (is_declaration_die (die))
7753 return clone_die (die);
7754
7755 /* If the DIE is a specification, just clone its declaration DIE. */
7756 decl = get_AT_ref (die, DW_AT_specification);
7757 if (decl != NULL)
7758 {
7759 clone = clone_die (decl);
7760 if (die->comdat_type_p)
7761 add_AT_die_ref (clone, DW_AT_signature, die);
7762 return clone;
7763 }
7764
7765 clone = new_die_raw (die->die_tag);
7766
7767 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7768 {
7769 /* We don't want to copy over all attributes.
7770 For example we don't want DW_AT_byte_size because otherwise we will no
7771 longer have a declaration and GDB will treat it as a definition. */
7772
7773 switch (a->dw_attr)
7774 {
7775 case DW_AT_abstract_origin:
7776 case DW_AT_artificial:
7777 case DW_AT_containing_type:
7778 case DW_AT_external:
7779 case DW_AT_name:
7780 case DW_AT_type:
7781 case DW_AT_virtuality:
7782 case DW_AT_linkage_name:
7783 case DW_AT_MIPS_linkage_name:
7784 add_dwarf_attr (clone, a);
7785 break;
7786 case DW_AT_byte_size:
7787 case DW_AT_alignment:
7788 default:
7789 break;
7790 }
7791 }
7792
7793 if (die->comdat_type_p)
7794 add_AT_die_ref (clone, DW_AT_signature, die);
7795
7796 add_AT_flag (clone, DW_AT_declaration, 1);
7797 return clone;
7798 }
7799
7800
7801 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7802
7803 struct decl_table_entry
7804 {
7805 dw_die_ref orig;
7806 dw_die_ref copy;
7807 };
7808
7809 /* Helpers to manipulate hash table of copied declarations. */
7810
7811 /* Hashtable helpers. */
7812
7813 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7814 {
7815 typedef die_struct *compare_type;
7816 static inline hashval_t hash (const decl_table_entry *);
7817 static inline bool equal (const decl_table_entry *, const die_struct *);
7818 };
7819
7820 inline hashval_t
7821 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7822 {
7823 return htab_hash_pointer (entry->orig);
7824 }
7825
7826 inline bool
7827 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7828 const die_struct *entry2)
7829 {
7830 return entry1->orig == entry2;
7831 }
7832
7833 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7834
7835 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7836 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7837 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7838 to check if the ancestor has already been copied into UNIT. */
7839
7840 static dw_die_ref
7841 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7842 decl_hash_type *decl_table)
7843 {
7844 dw_die_ref parent = die->die_parent;
7845 dw_die_ref new_parent = unit;
7846 dw_die_ref copy;
7847 decl_table_entry **slot = NULL;
7848 struct decl_table_entry *entry = NULL;
7849
7850 if (decl_table)
7851 {
7852 /* Check if the entry has already been copied to UNIT. */
7853 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7854 INSERT);
7855 if (*slot != HTAB_EMPTY_ENTRY)
7856 {
7857 entry = *slot;
7858 return entry->copy;
7859 }
7860
7861 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7862 entry = XCNEW (struct decl_table_entry);
7863 entry->orig = die;
7864 entry->copy = NULL;
7865 *slot = entry;
7866 }
7867
7868 if (parent != NULL)
7869 {
7870 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7871 if (spec != NULL)
7872 parent = spec;
7873 if (!is_unit_die (parent))
7874 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7875 }
7876
7877 copy = clone_as_declaration (die);
7878 add_child_die (new_parent, copy);
7879
7880 if (decl_table)
7881 {
7882 /* Record the pointer to the copy. */
7883 entry->copy = copy;
7884 }
7885
7886 return copy;
7887 }
7888 /* Copy the declaration context to the new type unit DIE. This includes
7889 any surrounding namespace or type declarations. If the DIE has an
7890 AT_specification attribute, it also includes attributes and children
7891 attached to the specification, and returns a pointer to the original
7892 parent of the declaration DIE. Returns NULL otherwise. */
7893
7894 static dw_die_ref
7895 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7896 {
7897 dw_die_ref decl;
7898 dw_die_ref new_decl;
7899 dw_die_ref orig_parent = NULL;
7900
7901 decl = get_AT_ref (die, DW_AT_specification);
7902 if (decl == NULL)
7903 decl = die;
7904 else
7905 {
7906 unsigned ix;
7907 dw_die_ref c;
7908 dw_attr_node *a;
7909
7910 /* The original DIE will be changed to a declaration, and must
7911 be moved to be a child of the original declaration DIE. */
7912 orig_parent = decl->die_parent;
7913
7914 /* Copy the type node pointer from the new DIE to the original
7915 declaration DIE so we can forward references later. */
7916 decl->comdat_type_p = true;
7917 decl->die_id.die_type_node = die->die_id.die_type_node;
7918
7919 remove_AT (die, DW_AT_specification);
7920
7921 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7922 {
7923 if (a->dw_attr != DW_AT_name
7924 && a->dw_attr != DW_AT_declaration
7925 && a->dw_attr != DW_AT_external)
7926 add_dwarf_attr (die, a);
7927 }
7928
7929 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7930 }
7931
7932 if (decl->die_parent != NULL
7933 && !is_unit_die (decl->die_parent))
7934 {
7935 new_decl = copy_ancestor_tree (unit, decl, NULL);
7936 if (new_decl != NULL)
7937 {
7938 remove_AT (new_decl, DW_AT_signature);
7939 add_AT_specification (die, new_decl);
7940 }
7941 }
7942
7943 return orig_parent;
7944 }
7945
7946 /* Generate the skeleton ancestor tree for the given NODE, then clone
7947 the DIE and add the clone into the tree. */
7948
7949 static void
7950 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7951 {
7952 if (node->new_die != NULL)
7953 return;
7954
7955 node->new_die = clone_as_declaration (node->old_die);
7956
7957 if (node->parent != NULL)
7958 {
7959 generate_skeleton_ancestor_tree (node->parent);
7960 add_child_die (node->parent->new_die, node->new_die);
7961 }
7962 }
7963
7964 /* Generate a skeleton tree of DIEs containing any declarations that are
7965 found in the original tree. We traverse the tree looking for declaration
7966 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7967
7968 static void
7969 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7970 {
7971 skeleton_chain_node node;
7972 dw_die_ref c;
7973 dw_die_ref first;
7974 dw_die_ref prev = NULL;
7975 dw_die_ref next = NULL;
7976
7977 node.parent = parent;
7978
7979 first = c = parent->old_die->die_child;
7980 if (c)
7981 next = c->die_sib;
7982 if (c) do {
7983 if (prev == NULL || prev->die_sib == c)
7984 prev = c;
7985 c = next;
7986 next = (c == first ? NULL : c->die_sib);
7987 node.old_die = c;
7988 node.new_die = NULL;
7989 if (is_declaration_die (c))
7990 {
7991 if (is_template_instantiation (c))
7992 {
7993 /* Instantiated templates do not need to be cloned into the
7994 type unit. Just move the DIE and its children back to
7995 the skeleton tree (in the main CU). */
7996 remove_child_with_prev (c, prev);
7997 add_child_die (parent->new_die, c);
7998 c = prev;
7999 }
8000 else if (c->comdat_type_p)
8001 {
8002 /* This is the skeleton of earlier break_out_comdat_types
8003 type. Clone the existing DIE, but keep the children
8004 under the original (which is in the main CU). */
8005 dw_die_ref clone = clone_die (c);
8006
8007 replace_child (c, clone, prev);
8008 generate_skeleton_ancestor_tree (parent);
8009 add_child_die (parent->new_die, c);
8010 c = clone;
8011 continue;
8012 }
8013 else
8014 {
8015 /* Clone the existing DIE, move the original to the skeleton
8016 tree (which is in the main CU), and put the clone, with
8017 all the original's children, where the original came from
8018 (which is about to be moved to the type unit). */
8019 dw_die_ref clone = clone_die (c);
8020 move_all_children (c, clone);
8021
8022 /* If the original has a DW_AT_object_pointer attribute,
8023 it would now point to a child DIE just moved to the
8024 cloned tree, so we need to remove that attribute from
8025 the original. */
8026 remove_AT (c, DW_AT_object_pointer);
8027
8028 replace_child (c, clone, prev);
8029 generate_skeleton_ancestor_tree (parent);
8030 add_child_die (parent->new_die, c);
8031 node.old_die = clone;
8032 node.new_die = c;
8033 c = clone;
8034 }
8035 }
8036 generate_skeleton_bottom_up (&node);
8037 } while (next != NULL);
8038 }
8039
8040 /* Wrapper function for generate_skeleton_bottom_up. */
8041
8042 static dw_die_ref
8043 generate_skeleton (dw_die_ref die)
8044 {
8045 skeleton_chain_node node;
8046
8047 node.old_die = die;
8048 node.new_die = NULL;
8049 node.parent = NULL;
8050
8051 /* If this type definition is nested inside another type,
8052 and is not an instantiation of a template, always leave
8053 at least a declaration in its place. */
8054 if (die->die_parent != NULL
8055 && is_type_die (die->die_parent)
8056 && !is_template_instantiation (die))
8057 node.new_die = clone_as_declaration (die);
8058
8059 generate_skeleton_bottom_up (&node);
8060 return node.new_die;
8061 }
8062
8063 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8064 declaration. The original DIE is moved to a new compile unit so that
8065 existing references to it follow it to the new location. If any of the
8066 original DIE's descendants is a declaration, we need to replace the
8067 original DIE with a skeleton tree and move the declarations back into the
8068 skeleton tree. */
8069
8070 static dw_die_ref
8071 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8072 dw_die_ref prev)
8073 {
8074 dw_die_ref skeleton, orig_parent;
8075
8076 /* Copy the declaration context to the type unit DIE. If the returned
8077 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8078 that DIE. */
8079 orig_parent = copy_declaration_context (unit, child);
8080
8081 skeleton = generate_skeleton (child);
8082 if (skeleton == NULL)
8083 remove_child_with_prev (child, prev);
8084 else
8085 {
8086 skeleton->comdat_type_p = true;
8087 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8088
8089 /* If the original DIE was a specification, we need to put
8090 the skeleton under the parent DIE of the declaration.
8091 This leaves the original declaration in the tree, but
8092 it will be pruned later since there are no longer any
8093 references to it. */
8094 if (orig_parent != NULL)
8095 {
8096 remove_child_with_prev (child, prev);
8097 add_child_die (orig_parent, skeleton);
8098 }
8099 else
8100 replace_child (child, skeleton, prev);
8101 }
8102
8103 return skeleton;
8104 }
8105
8106 static void
8107 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8108 comdat_type_node *type_node,
8109 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8110
8111 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8112 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8113 DWARF procedure references in the DW_AT_location attribute. */
8114
8115 static dw_die_ref
8116 copy_dwarf_procedure (dw_die_ref die,
8117 comdat_type_node *type_node,
8118 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8119 {
8120 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8121
8122 /* DWARF procedures are not supposed to have children... */
8123 gcc_assert (die->die_child == NULL);
8124
8125 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8126 gcc_assert (vec_safe_length (die->die_attr) == 1
8127 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8128
8129 /* Do not copy more than once DWARF procedures. */
8130 bool existed;
8131 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8132 if (existed)
8133 return die_copy;
8134
8135 die_copy = clone_die (die);
8136 add_child_die (type_node->root_die, die_copy);
8137 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8138 return die_copy;
8139 }
8140
8141 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8142 procedures in DIE's attributes. */
8143
8144 static void
8145 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8146 comdat_type_node *type_node,
8147 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8148 {
8149 dw_attr_node *a;
8150 unsigned i;
8151
8152 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8153 {
8154 dw_loc_descr_ref loc;
8155
8156 if (a->dw_attr_val.val_class != dw_val_class_loc)
8157 continue;
8158
8159 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8160 {
8161 switch (loc->dw_loc_opc)
8162 {
8163 case DW_OP_call2:
8164 case DW_OP_call4:
8165 case DW_OP_call_ref:
8166 gcc_assert (loc->dw_loc_oprnd1.val_class
8167 == dw_val_class_die_ref);
8168 loc->dw_loc_oprnd1.v.val_die_ref.die
8169 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8170 type_node,
8171 copied_dwarf_procs);
8172
8173 default:
8174 break;
8175 }
8176 }
8177 }
8178 }
8179
8180 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8181 rewrite references to point to the copies.
8182
8183 References are looked for in DIE's attributes and recursively in all its
8184 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8185 mapping from old DWARF procedures to their copy. It is used not to copy
8186 twice the same DWARF procedure under TYPE_NODE. */
8187
8188 static void
8189 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8190 comdat_type_node *type_node,
8191 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8192 {
8193 dw_die_ref c;
8194
8195 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8196 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8197 type_node,
8198 copied_dwarf_procs));
8199 }
8200
8201 /* Traverse the DIE and set up additional .debug_types or .debug_info
8202 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8203 section. */
8204
8205 static void
8206 break_out_comdat_types (dw_die_ref die)
8207 {
8208 dw_die_ref c;
8209 dw_die_ref first;
8210 dw_die_ref prev = NULL;
8211 dw_die_ref next = NULL;
8212 dw_die_ref unit = NULL;
8213
8214 first = c = die->die_child;
8215 if (c)
8216 next = c->die_sib;
8217 if (c) do {
8218 if (prev == NULL || prev->die_sib == c)
8219 prev = c;
8220 c = next;
8221 next = (c == first ? NULL : c->die_sib);
8222 if (should_move_die_to_comdat (c))
8223 {
8224 dw_die_ref replacement;
8225 comdat_type_node *type_node;
8226
8227 /* Break out nested types into their own type units. */
8228 break_out_comdat_types (c);
8229
8230 /* Create a new type unit DIE as the root for the new tree, and
8231 add it to the list of comdat types. */
8232 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8233 add_AT_unsigned (unit, DW_AT_language,
8234 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8235 type_node = ggc_cleared_alloc<comdat_type_node> ();
8236 type_node->root_die = unit;
8237 type_node->next = comdat_type_list;
8238 comdat_type_list = type_node;
8239
8240 /* Generate the type signature. */
8241 generate_type_signature (c, type_node);
8242
8243 /* Copy the declaration context, attributes, and children of the
8244 declaration into the new type unit DIE, then remove this DIE
8245 from the main CU (or replace it with a skeleton if necessary). */
8246 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8247 type_node->skeleton_die = replacement;
8248
8249 /* Add the DIE to the new compunit. */
8250 add_child_die (unit, c);
8251
8252 /* Types can reference DWARF procedures for type size or data location
8253 expressions. Calls in DWARF expressions cannot target procedures
8254 that are not in the same section. So we must copy DWARF procedures
8255 along with this type and then rewrite references to them. */
8256 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8257 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8258
8259 if (replacement != NULL)
8260 c = replacement;
8261 }
8262 else if (c->die_tag == DW_TAG_namespace
8263 || c->die_tag == DW_TAG_class_type
8264 || c->die_tag == DW_TAG_structure_type
8265 || c->die_tag == DW_TAG_union_type)
8266 {
8267 /* Look for nested types that can be broken out. */
8268 break_out_comdat_types (c);
8269 }
8270 } while (next != NULL);
8271 }
8272
8273 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8274 Enter all the cloned children into the hash table decl_table. */
8275
8276 static dw_die_ref
8277 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8278 {
8279 dw_die_ref c;
8280 dw_die_ref clone;
8281 struct decl_table_entry *entry;
8282 decl_table_entry **slot;
8283
8284 if (die->die_tag == DW_TAG_subprogram)
8285 clone = clone_as_declaration (die);
8286 else
8287 clone = clone_die (die);
8288
8289 slot = decl_table->find_slot_with_hash (die,
8290 htab_hash_pointer (die), INSERT);
8291
8292 /* Assert that DIE isn't in the hash table yet. If it would be there
8293 before, the ancestors would be necessarily there as well, therefore
8294 clone_tree_partial wouldn't be called. */
8295 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8296
8297 entry = XCNEW (struct decl_table_entry);
8298 entry->orig = die;
8299 entry->copy = clone;
8300 *slot = entry;
8301
8302 if (die->die_tag != DW_TAG_subprogram)
8303 FOR_EACH_CHILD (die, c,
8304 add_child_die (clone, clone_tree_partial (c, decl_table)));
8305
8306 return clone;
8307 }
8308
8309 /* Walk the DIE and its children, looking for references to incomplete
8310 or trivial types that are unmarked (i.e., that are not in the current
8311 type_unit). */
8312
8313 static void
8314 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8315 {
8316 dw_die_ref c;
8317 dw_attr_node *a;
8318 unsigned ix;
8319
8320 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8321 {
8322 if (AT_class (a) == dw_val_class_die_ref)
8323 {
8324 dw_die_ref targ = AT_ref (a);
8325 decl_table_entry **slot;
8326 struct decl_table_entry *entry;
8327
8328 if (targ->die_mark != 0 || targ->comdat_type_p)
8329 continue;
8330
8331 slot = decl_table->find_slot_with_hash (targ,
8332 htab_hash_pointer (targ),
8333 INSERT);
8334
8335 if (*slot != HTAB_EMPTY_ENTRY)
8336 {
8337 /* TARG has already been copied, so we just need to
8338 modify the reference to point to the copy. */
8339 entry = *slot;
8340 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8341 }
8342 else
8343 {
8344 dw_die_ref parent = unit;
8345 dw_die_ref copy = clone_die (targ);
8346
8347 /* Record in DECL_TABLE that TARG has been copied.
8348 Need to do this now, before the recursive call,
8349 because DECL_TABLE may be expanded and SLOT
8350 would no longer be a valid pointer. */
8351 entry = XCNEW (struct decl_table_entry);
8352 entry->orig = targ;
8353 entry->copy = copy;
8354 *slot = entry;
8355
8356 /* If TARG is not a declaration DIE, we need to copy its
8357 children. */
8358 if (!is_declaration_die (targ))
8359 {
8360 FOR_EACH_CHILD (
8361 targ, c,
8362 add_child_die (copy,
8363 clone_tree_partial (c, decl_table)));
8364 }
8365
8366 /* Make sure the cloned tree is marked as part of the
8367 type unit. */
8368 mark_dies (copy);
8369
8370 /* If TARG has surrounding context, copy its ancestor tree
8371 into the new type unit. */
8372 if (targ->die_parent != NULL
8373 && !is_unit_die (targ->die_parent))
8374 parent = copy_ancestor_tree (unit, targ->die_parent,
8375 decl_table);
8376
8377 add_child_die (parent, copy);
8378 a->dw_attr_val.v.val_die_ref.die = copy;
8379
8380 /* Make sure the newly-copied DIE is walked. If it was
8381 installed in a previously-added context, it won't
8382 get visited otherwise. */
8383 if (parent != unit)
8384 {
8385 /* Find the highest point of the newly-added tree,
8386 mark each node along the way, and walk from there. */
8387 parent->die_mark = 1;
8388 while (parent->die_parent
8389 && parent->die_parent->die_mark == 0)
8390 {
8391 parent = parent->die_parent;
8392 parent->die_mark = 1;
8393 }
8394 copy_decls_walk (unit, parent, decl_table);
8395 }
8396 }
8397 }
8398 }
8399
8400 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8401 }
8402
8403 /* Copy declarations for "unworthy" types into the new comdat section.
8404 Incomplete types, modified types, and certain other types aren't broken
8405 out into comdat sections of their own, so they don't have a signature,
8406 and we need to copy the declaration into the same section so that we
8407 don't have an external reference. */
8408
8409 static void
8410 copy_decls_for_unworthy_types (dw_die_ref unit)
8411 {
8412 mark_dies (unit);
8413 decl_hash_type decl_table (10);
8414 copy_decls_walk (unit, unit, &decl_table);
8415 unmark_dies (unit);
8416 }
8417
8418 /* Traverse the DIE and add a sibling attribute if it may have the
8419 effect of speeding up access to siblings. To save some space,
8420 avoid generating sibling attributes for DIE's without children. */
8421
8422 static void
8423 add_sibling_attributes (dw_die_ref die)
8424 {
8425 dw_die_ref c;
8426
8427 if (! die->die_child)
8428 return;
8429
8430 if (die->die_parent && die != die->die_parent->die_child)
8431 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8432
8433 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8434 }
8435
8436 /* Output all location lists for the DIE and its children. */
8437
8438 static void
8439 output_location_lists (dw_die_ref die)
8440 {
8441 dw_die_ref c;
8442 dw_attr_node *a;
8443 unsigned ix;
8444
8445 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8446 if (AT_class (a) == dw_val_class_loc_list)
8447 output_loc_list (AT_loc_list (a));
8448
8449 FOR_EACH_CHILD (die, c, output_location_lists (c));
8450 }
8451
8452 /* During assign_location_list_indexes and output_loclists_offset the
8453 current index, after it the number of assigned indexes (i.e. how
8454 large the .debug_loclists* offset table should be). */
8455 static unsigned int loc_list_idx;
8456
8457 /* Output all location list offsets for the DIE and its children. */
8458
8459 static void
8460 output_loclists_offsets (dw_die_ref die)
8461 {
8462 dw_die_ref c;
8463 dw_attr_node *a;
8464 unsigned ix;
8465
8466 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8467 if (AT_class (a) == dw_val_class_loc_list)
8468 {
8469 dw_loc_list_ref l = AT_loc_list (a);
8470 if (l->offset_emitted)
8471 continue;
8472 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8473 loc_section_label, NULL);
8474 gcc_assert (l->hash == loc_list_idx);
8475 loc_list_idx++;
8476 l->offset_emitted = true;
8477 }
8478
8479 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8480 }
8481
8482 /* Recursively set indexes of location lists. */
8483
8484 static void
8485 assign_location_list_indexes (dw_die_ref die)
8486 {
8487 dw_die_ref c;
8488 dw_attr_node *a;
8489 unsigned ix;
8490
8491 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8492 if (AT_class (a) == dw_val_class_loc_list)
8493 {
8494 dw_loc_list_ref list = AT_loc_list (a);
8495 if (!list->num_assigned)
8496 {
8497 list->num_assigned = true;
8498 list->hash = loc_list_idx++;
8499 }
8500 }
8501
8502 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8503 }
8504
8505 /* We want to limit the number of external references, because they are
8506 larger than local references: a relocation takes multiple words, and
8507 even a sig8 reference is always eight bytes, whereas a local reference
8508 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8509 So if we encounter multiple external references to the same type DIE, we
8510 make a local typedef stub for it and redirect all references there.
8511
8512 This is the element of the hash table for keeping track of these
8513 references. */
8514
8515 struct external_ref
8516 {
8517 dw_die_ref type;
8518 dw_die_ref stub;
8519 unsigned n_refs;
8520 };
8521
8522 /* Hashtable helpers. */
8523
8524 struct external_ref_hasher : free_ptr_hash <external_ref>
8525 {
8526 static inline hashval_t hash (const external_ref *);
8527 static inline bool equal (const external_ref *, const external_ref *);
8528 };
8529
8530 inline hashval_t
8531 external_ref_hasher::hash (const external_ref *r)
8532 {
8533 dw_die_ref die = r->type;
8534 hashval_t h = 0;
8535
8536 /* We can't use the address of the DIE for hashing, because
8537 that will make the order of the stub DIEs non-deterministic. */
8538 if (! die->comdat_type_p)
8539 /* We have a symbol; use it to compute a hash. */
8540 h = htab_hash_string (die->die_id.die_symbol);
8541 else
8542 {
8543 /* We have a type signature; use a subset of the bits as the hash.
8544 The 8-byte signature is at least as large as hashval_t. */
8545 comdat_type_node *type_node = die->die_id.die_type_node;
8546 memcpy (&h, type_node->signature, sizeof (h));
8547 }
8548 return h;
8549 }
8550
8551 inline bool
8552 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8553 {
8554 return r1->type == r2->type;
8555 }
8556
8557 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8558
8559 /* Return a pointer to the external_ref for references to DIE. */
8560
8561 static struct external_ref *
8562 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8563 {
8564 struct external_ref ref, *ref_p;
8565 external_ref **slot;
8566
8567 ref.type = die;
8568 slot = map->find_slot (&ref, INSERT);
8569 if (*slot != HTAB_EMPTY_ENTRY)
8570 return *slot;
8571
8572 ref_p = XCNEW (struct external_ref);
8573 ref_p->type = die;
8574 *slot = ref_p;
8575 return ref_p;
8576 }
8577
8578 /* Subroutine of optimize_external_refs, below.
8579
8580 If we see a type skeleton, record it as our stub. If we see external
8581 references, remember how many we've seen. */
8582
8583 static void
8584 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8585 {
8586 dw_die_ref c;
8587 dw_attr_node *a;
8588 unsigned ix;
8589 struct external_ref *ref_p;
8590
8591 if (is_type_die (die)
8592 && (c = get_AT_ref (die, DW_AT_signature)))
8593 {
8594 /* This is a local skeleton; use it for local references. */
8595 ref_p = lookup_external_ref (map, c);
8596 ref_p->stub = die;
8597 }
8598
8599 /* Scan the DIE references, and remember any that refer to DIEs from
8600 other CUs (i.e. those which are not marked). */
8601 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8602 if (AT_class (a) == dw_val_class_die_ref
8603 && (c = AT_ref (a))->die_mark == 0
8604 && is_type_die (c))
8605 {
8606 ref_p = lookup_external_ref (map, c);
8607 ref_p->n_refs++;
8608 }
8609
8610 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8611 }
8612
8613 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8614 points to an external_ref, DATA is the CU we're processing. If we don't
8615 already have a local stub, and we have multiple refs, build a stub. */
8616
8617 int
8618 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8619 {
8620 struct external_ref *ref_p = *slot;
8621
8622 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8623 {
8624 /* We have multiple references to this type, so build a small stub.
8625 Both of these forms are a bit dodgy from the perspective of the
8626 DWARF standard, since technically they should have names. */
8627 dw_die_ref cu = data;
8628 dw_die_ref type = ref_p->type;
8629 dw_die_ref stub = NULL;
8630
8631 if (type->comdat_type_p)
8632 {
8633 /* If we refer to this type via sig8, use AT_signature. */
8634 stub = new_die (type->die_tag, cu, NULL_TREE);
8635 add_AT_die_ref (stub, DW_AT_signature, type);
8636 }
8637 else
8638 {
8639 /* Otherwise, use a typedef with no name. */
8640 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8641 add_AT_die_ref (stub, DW_AT_type, type);
8642 }
8643
8644 stub->die_mark++;
8645 ref_p->stub = stub;
8646 }
8647 return 1;
8648 }
8649
8650 /* DIE is a unit; look through all the DIE references to see if there are
8651 any external references to types, and if so, create local stubs for
8652 them which will be applied in build_abbrev_table. This is useful because
8653 references to local DIEs are smaller. */
8654
8655 static external_ref_hash_type *
8656 optimize_external_refs (dw_die_ref die)
8657 {
8658 external_ref_hash_type *map = new external_ref_hash_type (10);
8659 optimize_external_refs_1 (die, map);
8660 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8661 return map;
8662 }
8663
8664 /* The following 3 variables are temporaries that are computed only during the
8665 build_abbrev_table call and used and released during the following
8666 optimize_abbrev_table call. */
8667
8668 /* First abbrev_id that can be optimized based on usage. */
8669 static unsigned int abbrev_opt_start;
8670
8671 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8672 abbrev_id smaller than this, because they must be already sized
8673 during build_abbrev_table). */
8674 static unsigned int abbrev_opt_base_type_end;
8675
8676 /* Vector of usage counts during build_abbrev_table. Indexed by
8677 abbrev_id - abbrev_opt_start. */
8678 static vec<unsigned int> abbrev_usage_count;
8679
8680 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8681 static vec<dw_die_ref> sorted_abbrev_dies;
8682
8683 /* The format of each DIE (and its attribute value pairs) is encoded in an
8684 abbreviation table. This routine builds the abbreviation table and assigns
8685 a unique abbreviation id for each abbreviation entry. The children of each
8686 die are visited recursively. */
8687
8688 static void
8689 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8690 {
8691 unsigned int abbrev_id = 0;
8692 dw_die_ref c;
8693 dw_attr_node *a;
8694 unsigned ix;
8695 dw_die_ref abbrev;
8696
8697 /* Scan the DIE references, and replace any that refer to
8698 DIEs from other CUs (i.e. those which are not marked) with
8699 the local stubs we built in optimize_external_refs. */
8700 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8701 if (AT_class (a) == dw_val_class_die_ref
8702 && (c = AT_ref (a))->die_mark == 0)
8703 {
8704 struct external_ref *ref_p;
8705 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8706
8707 ref_p = lookup_external_ref (extern_map, c);
8708 if (ref_p->stub && ref_p->stub != die)
8709 change_AT_die_ref (a, ref_p->stub);
8710 else
8711 /* We aren't changing this reference, so mark it external. */
8712 set_AT_ref_external (a, 1);
8713 }
8714
8715 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8716 {
8717 dw_attr_node *die_a, *abbrev_a;
8718 unsigned ix;
8719 bool ok = true;
8720
8721 if (abbrev_id == 0)
8722 continue;
8723 if (abbrev->die_tag != die->die_tag)
8724 continue;
8725 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8726 continue;
8727
8728 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8729 continue;
8730
8731 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8732 {
8733 abbrev_a = &(*abbrev->die_attr)[ix];
8734 if ((abbrev_a->dw_attr != die_a->dw_attr)
8735 || (value_format (abbrev_a) != value_format (die_a)))
8736 {
8737 ok = false;
8738 break;
8739 }
8740 }
8741 if (ok)
8742 break;
8743 }
8744
8745 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8746 {
8747 vec_safe_push (abbrev_die_table, die);
8748 if (abbrev_opt_start)
8749 abbrev_usage_count.safe_push (0);
8750 }
8751 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8752 {
8753 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8754 sorted_abbrev_dies.safe_push (die);
8755 }
8756
8757 die->die_abbrev = abbrev_id;
8758 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8759 }
8760
8761 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8762 by die_abbrev's usage count, from the most commonly used
8763 abbreviation to the least. */
8764
8765 static int
8766 die_abbrev_cmp (const void *p1, const void *p2)
8767 {
8768 dw_die_ref die1 = *(const dw_die_ref *) p1;
8769 dw_die_ref die2 = *(const dw_die_ref *) p2;
8770
8771 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8772 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8773
8774 if (die1->die_abbrev >= abbrev_opt_base_type_end
8775 && die2->die_abbrev >= abbrev_opt_base_type_end)
8776 {
8777 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8778 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8779 return -1;
8780 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8781 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8782 return 1;
8783 }
8784
8785 /* Stabilize the sort. */
8786 if (die1->die_abbrev < die2->die_abbrev)
8787 return -1;
8788 if (die1->die_abbrev > die2->die_abbrev)
8789 return 1;
8790
8791 return 0;
8792 }
8793
8794 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8795 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8796 into dw_val_class_const_implicit or
8797 dw_val_class_unsigned_const_implicit. */
8798
8799 static void
8800 optimize_implicit_const (unsigned int first_id, unsigned int end,
8801 vec<bool> &implicit_consts)
8802 {
8803 /* It never makes sense if there is just one DIE using the abbreviation. */
8804 if (end < first_id + 2)
8805 return;
8806
8807 dw_attr_node *a;
8808 unsigned ix, i;
8809 dw_die_ref die = sorted_abbrev_dies[first_id];
8810 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8811 if (implicit_consts[ix])
8812 {
8813 enum dw_val_class new_class = dw_val_class_none;
8814 switch (AT_class (a))
8815 {
8816 case dw_val_class_unsigned_const:
8817 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8818 continue;
8819
8820 /* The .debug_abbrev section will grow by
8821 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8822 in all the DIEs using that abbreviation. */
8823 if (constant_size (AT_unsigned (a)) * (end - first_id)
8824 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8825 continue;
8826
8827 new_class = dw_val_class_unsigned_const_implicit;
8828 break;
8829
8830 case dw_val_class_const:
8831 new_class = dw_val_class_const_implicit;
8832 break;
8833
8834 case dw_val_class_file:
8835 new_class = dw_val_class_file_implicit;
8836 break;
8837
8838 default:
8839 continue;
8840 }
8841 for (i = first_id; i < end; i++)
8842 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8843 = new_class;
8844 }
8845 }
8846
8847 /* Attempt to optimize abbreviation table from abbrev_opt_start
8848 abbreviation above. */
8849
8850 static void
8851 optimize_abbrev_table (void)
8852 {
8853 if (abbrev_opt_start
8854 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8855 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8856 {
8857 auto_vec<bool, 32> implicit_consts;
8858 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8859
8860 unsigned int abbrev_id = abbrev_opt_start - 1;
8861 unsigned int first_id = ~0U;
8862 unsigned int last_abbrev_id = 0;
8863 unsigned int i;
8864 dw_die_ref die;
8865 if (abbrev_opt_base_type_end > abbrev_opt_start)
8866 abbrev_id = abbrev_opt_base_type_end - 1;
8867 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8868 most commonly used abbreviations come first. */
8869 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8870 {
8871 dw_attr_node *a;
8872 unsigned ix;
8873
8874 /* If calc_base_type_die_sizes has been called, the CU and
8875 base types after it can't be optimized, because we've already
8876 calculated their DIE offsets. We've sorted them first. */
8877 if (die->die_abbrev < abbrev_opt_base_type_end)
8878 continue;
8879 if (die->die_abbrev != last_abbrev_id)
8880 {
8881 last_abbrev_id = die->die_abbrev;
8882 if (dwarf_version >= 5 && first_id != ~0U)
8883 optimize_implicit_const (first_id, i, implicit_consts);
8884 abbrev_id++;
8885 (*abbrev_die_table)[abbrev_id] = die;
8886 if (dwarf_version >= 5)
8887 {
8888 first_id = i;
8889 implicit_consts.truncate (0);
8890
8891 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8892 switch (AT_class (a))
8893 {
8894 case dw_val_class_const:
8895 case dw_val_class_unsigned_const:
8896 case dw_val_class_file:
8897 implicit_consts.safe_push (true);
8898 break;
8899 default:
8900 implicit_consts.safe_push (false);
8901 break;
8902 }
8903 }
8904 }
8905 else if (dwarf_version >= 5)
8906 {
8907 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8908 if (!implicit_consts[ix])
8909 continue;
8910 else
8911 {
8912 dw_attr_node *other_a
8913 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8914 if (!dw_val_equal_p (&a->dw_attr_val,
8915 &other_a->dw_attr_val))
8916 implicit_consts[ix] = false;
8917 }
8918 }
8919 die->die_abbrev = abbrev_id;
8920 }
8921 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8922 if (dwarf_version >= 5 && first_id != ~0U)
8923 optimize_implicit_const (first_id, i, implicit_consts);
8924 }
8925
8926 abbrev_opt_start = 0;
8927 abbrev_opt_base_type_end = 0;
8928 abbrev_usage_count.release ();
8929 sorted_abbrev_dies.release ();
8930 }
8931 \f
8932 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8933
8934 static int
8935 constant_size (unsigned HOST_WIDE_INT value)
8936 {
8937 int log;
8938
8939 if (value == 0)
8940 log = 0;
8941 else
8942 log = floor_log2 (value);
8943
8944 log = log / 8;
8945 log = 1 << (floor_log2 (log) + 1);
8946
8947 return log;
8948 }
8949
8950 /* Return the size of a DIE as it is represented in the
8951 .debug_info section. */
8952
8953 static unsigned long
8954 size_of_die (dw_die_ref die)
8955 {
8956 unsigned long size = 0;
8957 dw_attr_node *a;
8958 unsigned ix;
8959 enum dwarf_form form;
8960
8961 size += size_of_uleb128 (die->die_abbrev);
8962 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8963 {
8964 switch (AT_class (a))
8965 {
8966 case dw_val_class_addr:
8967 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8968 {
8969 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8970 size += size_of_uleb128 (AT_index (a));
8971 }
8972 else
8973 size += DWARF2_ADDR_SIZE;
8974 break;
8975 case dw_val_class_offset:
8976 size += DWARF_OFFSET_SIZE;
8977 break;
8978 case dw_val_class_loc:
8979 {
8980 unsigned long lsize = size_of_locs (AT_loc (a));
8981
8982 /* Block length. */
8983 if (dwarf_version >= 4)
8984 size += size_of_uleb128 (lsize);
8985 else
8986 size += constant_size (lsize);
8987 size += lsize;
8988 }
8989 break;
8990 case dw_val_class_loc_list:
8991 if (dwarf_split_debug_info && dwarf_version >= 5)
8992 {
8993 gcc_assert (AT_loc_list (a)->num_assigned);
8994 size += size_of_uleb128 (AT_loc_list (a)->hash);
8995 }
8996 else
8997 size += DWARF_OFFSET_SIZE;
8998 break;
8999 case dw_val_class_range_list:
9000 if (value_format (a) == DW_FORM_rnglistx)
9001 {
9002 gcc_assert (rnglist_idx);
9003 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9004 size += size_of_uleb128 (r->idx);
9005 }
9006 else
9007 size += DWARF_OFFSET_SIZE;
9008 break;
9009 case dw_val_class_const:
9010 size += size_of_sleb128 (AT_int (a));
9011 break;
9012 case dw_val_class_unsigned_const:
9013 {
9014 int csize = constant_size (AT_unsigned (a));
9015 if (dwarf_version == 3
9016 && a->dw_attr == DW_AT_data_member_location
9017 && csize >= 4)
9018 size += size_of_uleb128 (AT_unsigned (a));
9019 else
9020 size += csize;
9021 }
9022 break;
9023 case dw_val_class_const_implicit:
9024 case dw_val_class_unsigned_const_implicit:
9025 case dw_val_class_file_implicit:
9026 /* These occupy no size in the DIE, just an extra sleb128 in
9027 .debug_abbrev. */
9028 break;
9029 case dw_val_class_const_double:
9030 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9031 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9032 size++; /* block */
9033 break;
9034 case dw_val_class_wide_int:
9035 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9036 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9037 if (get_full_len (*a->dw_attr_val.v.val_wide)
9038 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9039 size++; /* block */
9040 break;
9041 case dw_val_class_vec:
9042 size += constant_size (a->dw_attr_val.v.val_vec.length
9043 * a->dw_attr_val.v.val_vec.elt_size)
9044 + a->dw_attr_val.v.val_vec.length
9045 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9046 break;
9047 case dw_val_class_flag:
9048 if (dwarf_version >= 4)
9049 /* Currently all add_AT_flag calls pass in 1 as last argument,
9050 so DW_FORM_flag_present can be used. If that ever changes,
9051 we'll need to use DW_FORM_flag and have some optimization
9052 in build_abbrev_table that will change those to
9053 DW_FORM_flag_present if it is set to 1 in all DIEs using
9054 the same abbrev entry. */
9055 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9056 else
9057 size += 1;
9058 break;
9059 case dw_val_class_die_ref:
9060 if (AT_ref_external (a))
9061 {
9062 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9063 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9064 is sized by target address length, whereas in DWARF3
9065 it's always sized as an offset. */
9066 if (use_debug_types)
9067 size += DWARF_TYPE_SIGNATURE_SIZE;
9068 else if (dwarf_version == 2)
9069 size += DWARF2_ADDR_SIZE;
9070 else
9071 size += DWARF_OFFSET_SIZE;
9072 }
9073 else
9074 size += DWARF_OFFSET_SIZE;
9075 break;
9076 case dw_val_class_fde_ref:
9077 size += DWARF_OFFSET_SIZE;
9078 break;
9079 case dw_val_class_lbl_id:
9080 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9081 {
9082 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9083 size += size_of_uleb128 (AT_index (a));
9084 }
9085 else
9086 size += DWARF2_ADDR_SIZE;
9087 break;
9088 case dw_val_class_lineptr:
9089 case dw_val_class_macptr:
9090 case dw_val_class_loclistsptr:
9091 size += DWARF_OFFSET_SIZE;
9092 break;
9093 case dw_val_class_str:
9094 form = AT_string_form (a);
9095 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9096 size += DWARF_OFFSET_SIZE;
9097 else if (form == DW_FORM_GNU_str_index)
9098 size += size_of_uleb128 (AT_index (a));
9099 else
9100 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9101 break;
9102 case dw_val_class_file:
9103 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9104 break;
9105 case dw_val_class_data8:
9106 size += 8;
9107 break;
9108 case dw_val_class_vms_delta:
9109 size += DWARF_OFFSET_SIZE;
9110 break;
9111 case dw_val_class_high_pc:
9112 size += DWARF2_ADDR_SIZE;
9113 break;
9114 case dw_val_class_discr_value:
9115 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9116 break;
9117 case dw_val_class_discr_list:
9118 {
9119 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9120
9121 /* This is a block, so we have the block length and then its
9122 data. */
9123 size += constant_size (block_size) + block_size;
9124 }
9125 break;
9126 default:
9127 gcc_unreachable ();
9128 }
9129 }
9130
9131 return size;
9132 }
9133
9134 /* Size the debugging information associated with a given DIE. Visits the
9135 DIE's children recursively. Updates the global variable next_die_offset, on
9136 each time through. Uses the current value of next_die_offset to update the
9137 die_offset field in each DIE. */
9138
9139 static void
9140 calc_die_sizes (dw_die_ref die)
9141 {
9142 dw_die_ref c;
9143
9144 gcc_assert (die->die_offset == 0
9145 || (unsigned long int) die->die_offset == next_die_offset);
9146 die->die_offset = next_die_offset;
9147 next_die_offset += size_of_die (die);
9148
9149 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9150
9151 if (die->die_child != NULL)
9152 /* Count the null byte used to terminate sibling lists. */
9153 next_die_offset += 1;
9154 }
9155
9156 /* Size just the base type children at the start of the CU.
9157 This is needed because build_abbrev needs to size locs
9158 and sizing of type based stack ops needs to know die_offset
9159 values for the base types. */
9160
9161 static void
9162 calc_base_type_die_sizes (void)
9163 {
9164 unsigned long die_offset = (dwarf_split_debug_info
9165 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9166 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9167 unsigned int i;
9168 dw_die_ref base_type;
9169 #if ENABLE_ASSERT_CHECKING
9170 dw_die_ref prev = comp_unit_die ()->die_child;
9171 #endif
9172
9173 die_offset += size_of_die (comp_unit_die ());
9174 for (i = 0; base_types.iterate (i, &base_type); i++)
9175 {
9176 #if ENABLE_ASSERT_CHECKING
9177 gcc_assert (base_type->die_offset == 0
9178 && prev->die_sib == base_type
9179 && base_type->die_child == NULL
9180 && base_type->die_abbrev);
9181 prev = base_type;
9182 #endif
9183 if (abbrev_opt_start
9184 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9185 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9186 base_type->die_offset = die_offset;
9187 die_offset += size_of_die (base_type);
9188 }
9189 }
9190
9191 /* Set the marks for a die and its children. We do this so
9192 that we know whether or not a reference needs to use FORM_ref_addr; only
9193 DIEs in the same CU will be marked. We used to clear out the offset
9194 and use that as the flag, but ran into ordering problems. */
9195
9196 static void
9197 mark_dies (dw_die_ref die)
9198 {
9199 dw_die_ref c;
9200
9201 gcc_assert (!die->die_mark);
9202
9203 die->die_mark = 1;
9204 FOR_EACH_CHILD (die, c, mark_dies (c));
9205 }
9206
9207 /* Clear the marks for a die and its children. */
9208
9209 static void
9210 unmark_dies (dw_die_ref die)
9211 {
9212 dw_die_ref c;
9213
9214 if (! use_debug_types)
9215 gcc_assert (die->die_mark);
9216
9217 die->die_mark = 0;
9218 FOR_EACH_CHILD (die, c, unmark_dies (c));
9219 }
9220
9221 /* Clear the marks for a die, its children and referred dies. */
9222
9223 static void
9224 unmark_all_dies (dw_die_ref die)
9225 {
9226 dw_die_ref c;
9227 dw_attr_node *a;
9228 unsigned ix;
9229
9230 if (!die->die_mark)
9231 return;
9232 die->die_mark = 0;
9233
9234 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9235
9236 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9237 if (AT_class (a) == dw_val_class_die_ref)
9238 unmark_all_dies (AT_ref (a));
9239 }
9240
9241 /* Calculate if the entry should appear in the final output file. It may be
9242 from a pruned a type. */
9243
9244 static bool
9245 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9246 {
9247 /* By limiting gnu pubnames to definitions only, gold can generate a
9248 gdb index without entries for declarations, which don't include
9249 enough information to be useful. */
9250 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9251 return false;
9252
9253 if (table == pubname_table)
9254 {
9255 /* Enumerator names are part of the pubname table, but the
9256 parent DW_TAG_enumeration_type die may have been pruned.
9257 Don't output them if that is the case. */
9258 if (p->die->die_tag == DW_TAG_enumerator &&
9259 (p->die->die_parent == NULL
9260 || !p->die->die_parent->die_perennial_p))
9261 return false;
9262
9263 /* Everything else in the pubname table is included. */
9264 return true;
9265 }
9266
9267 /* The pubtypes table shouldn't include types that have been
9268 pruned. */
9269 return (p->die->die_offset != 0
9270 || !flag_eliminate_unused_debug_types);
9271 }
9272
9273 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9274 generated for the compilation unit. */
9275
9276 static unsigned long
9277 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9278 {
9279 unsigned long size;
9280 unsigned i;
9281 pubname_entry *p;
9282 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9283
9284 size = DWARF_PUBNAMES_HEADER_SIZE;
9285 FOR_EACH_VEC_ELT (*names, i, p)
9286 if (include_pubname_in_output (names, p))
9287 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9288
9289 size += DWARF_OFFSET_SIZE;
9290 return size;
9291 }
9292
9293 /* Return the size of the information in the .debug_aranges section. */
9294
9295 static unsigned long
9296 size_of_aranges (void)
9297 {
9298 unsigned long size;
9299
9300 size = DWARF_ARANGES_HEADER_SIZE;
9301
9302 /* Count the address/length pair for this compilation unit. */
9303 if (text_section_used)
9304 size += 2 * DWARF2_ADDR_SIZE;
9305 if (cold_text_section_used)
9306 size += 2 * DWARF2_ADDR_SIZE;
9307 if (have_multiple_function_sections)
9308 {
9309 unsigned fde_idx;
9310 dw_fde_ref fde;
9311
9312 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9313 {
9314 if (DECL_IGNORED_P (fde->decl))
9315 continue;
9316 if (!fde->in_std_section)
9317 size += 2 * DWARF2_ADDR_SIZE;
9318 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9319 size += 2 * DWARF2_ADDR_SIZE;
9320 }
9321 }
9322
9323 /* Count the two zero words used to terminated the address range table. */
9324 size += 2 * DWARF2_ADDR_SIZE;
9325 return size;
9326 }
9327 \f
9328 /* Select the encoding of an attribute value. */
9329
9330 static enum dwarf_form
9331 value_format (dw_attr_node *a)
9332 {
9333 switch (AT_class (a))
9334 {
9335 case dw_val_class_addr:
9336 /* Only very few attributes allow DW_FORM_addr. */
9337 switch (a->dw_attr)
9338 {
9339 case DW_AT_low_pc:
9340 case DW_AT_high_pc:
9341 case DW_AT_entry_pc:
9342 case DW_AT_trampoline:
9343 return (AT_index (a) == NOT_INDEXED
9344 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9345 default:
9346 break;
9347 }
9348 switch (DWARF2_ADDR_SIZE)
9349 {
9350 case 1:
9351 return DW_FORM_data1;
9352 case 2:
9353 return DW_FORM_data2;
9354 case 4:
9355 return DW_FORM_data4;
9356 case 8:
9357 return DW_FORM_data8;
9358 default:
9359 gcc_unreachable ();
9360 }
9361 case dw_val_class_loc_list:
9362 if (dwarf_split_debug_info
9363 && dwarf_version >= 5
9364 && AT_loc_list (a)->num_assigned)
9365 return DW_FORM_loclistx;
9366 /* FALLTHRU */
9367 case dw_val_class_range_list:
9368 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9369 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9370 care about sizes of .debug* sections in shared libraries and
9371 executables and don't take into account relocations that affect just
9372 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9373 table in the .debug_rnglists section. */
9374 if (dwarf_split_debug_info
9375 && dwarf_version >= 5
9376 && AT_class (a) == dw_val_class_range_list
9377 && rnglist_idx
9378 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9379 return DW_FORM_rnglistx;
9380 if (dwarf_version >= 4)
9381 return DW_FORM_sec_offset;
9382 /* FALLTHRU */
9383 case dw_val_class_vms_delta:
9384 case dw_val_class_offset:
9385 switch (DWARF_OFFSET_SIZE)
9386 {
9387 case 4:
9388 return DW_FORM_data4;
9389 case 8:
9390 return DW_FORM_data8;
9391 default:
9392 gcc_unreachable ();
9393 }
9394 case dw_val_class_loc:
9395 if (dwarf_version >= 4)
9396 return DW_FORM_exprloc;
9397 switch (constant_size (size_of_locs (AT_loc (a))))
9398 {
9399 case 1:
9400 return DW_FORM_block1;
9401 case 2:
9402 return DW_FORM_block2;
9403 case 4:
9404 return DW_FORM_block4;
9405 default:
9406 gcc_unreachable ();
9407 }
9408 case dw_val_class_const:
9409 return DW_FORM_sdata;
9410 case dw_val_class_unsigned_const:
9411 switch (constant_size (AT_unsigned (a)))
9412 {
9413 case 1:
9414 return DW_FORM_data1;
9415 case 2:
9416 return DW_FORM_data2;
9417 case 4:
9418 /* In DWARF3 DW_AT_data_member_location with
9419 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9420 constant, so we need to use DW_FORM_udata if we need
9421 a large constant. */
9422 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9423 return DW_FORM_udata;
9424 return DW_FORM_data4;
9425 case 8:
9426 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9427 return DW_FORM_udata;
9428 return DW_FORM_data8;
9429 default:
9430 gcc_unreachable ();
9431 }
9432 case dw_val_class_const_implicit:
9433 case dw_val_class_unsigned_const_implicit:
9434 case dw_val_class_file_implicit:
9435 return DW_FORM_implicit_const;
9436 case dw_val_class_const_double:
9437 switch (HOST_BITS_PER_WIDE_INT)
9438 {
9439 case 8:
9440 return DW_FORM_data2;
9441 case 16:
9442 return DW_FORM_data4;
9443 case 32:
9444 return DW_FORM_data8;
9445 case 64:
9446 if (dwarf_version >= 5)
9447 return DW_FORM_data16;
9448 /* FALLTHRU */
9449 default:
9450 return DW_FORM_block1;
9451 }
9452 case dw_val_class_wide_int:
9453 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9454 {
9455 case 8:
9456 return DW_FORM_data1;
9457 case 16:
9458 return DW_FORM_data2;
9459 case 32:
9460 return DW_FORM_data4;
9461 case 64:
9462 return DW_FORM_data8;
9463 case 128:
9464 if (dwarf_version >= 5)
9465 return DW_FORM_data16;
9466 /* FALLTHRU */
9467 default:
9468 return DW_FORM_block1;
9469 }
9470 case dw_val_class_vec:
9471 switch (constant_size (a->dw_attr_val.v.val_vec.length
9472 * a->dw_attr_val.v.val_vec.elt_size))
9473 {
9474 case 1:
9475 return DW_FORM_block1;
9476 case 2:
9477 return DW_FORM_block2;
9478 case 4:
9479 return DW_FORM_block4;
9480 default:
9481 gcc_unreachable ();
9482 }
9483 case dw_val_class_flag:
9484 if (dwarf_version >= 4)
9485 {
9486 /* Currently all add_AT_flag calls pass in 1 as last argument,
9487 so DW_FORM_flag_present can be used. If that ever changes,
9488 we'll need to use DW_FORM_flag and have some optimization
9489 in build_abbrev_table that will change those to
9490 DW_FORM_flag_present if it is set to 1 in all DIEs using
9491 the same abbrev entry. */
9492 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9493 return DW_FORM_flag_present;
9494 }
9495 return DW_FORM_flag;
9496 case dw_val_class_die_ref:
9497 if (AT_ref_external (a))
9498 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9499 else
9500 return DW_FORM_ref;
9501 case dw_val_class_fde_ref:
9502 return DW_FORM_data;
9503 case dw_val_class_lbl_id:
9504 return (AT_index (a) == NOT_INDEXED
9505 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9506 case dw_val_class_lineptr:
9507 case dw_val_class_macptr:
9508 case dw_val_class_loclistsptr:
9509 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9510 case dw_val_class_str:
9511 return AT_string_form (a);
9512 case dw_val_class_file:
9513 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9514 {
9515 case 1:
9516 return DW_FORM_data1;
9517 case 2:
9518 return DW_FORM_data2;
9519 case 4:
9520 return DW_FORM_data4;
9521 default:
9522 gcc_unreachable ();
9523 }
9524
9525 case dw_val_class_data8:
9526 return DW_FORM_data8;
9527
9528 case dw_val_class_high_pc:
9529 switch (DWARF2_ADDR_SIZE)
9530 {
9531 case 1:
9532 return DW_FORM_data1;
9533 case 2:
9534 return DW_FORM_data2;
9535 case 4:
9536 return DW_FORM_data4;
9537 case 8:
9538 return DW_FORM_data8;
9539 default:
9540 gcc_unreachable ();
9541 }
9542
9543 case dw_val_class_discr_value:
9544 return (a->dw_attr_val.v.val_discr_value.pos
9545 ? DW_FORM_udata
9546 : DW_FORM_sdata);
9547 case dw_val_class_discr_list:
9548 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9549 {
9550 case 1:
9551 return DW_FORM_block1;
9552 case 2:
9553 return DW_FORM_block2;
9554 case 4:
9555 return DW_FORM_block4;
9556 default:
9557 gcc_unreachable ();
9558 }
9559
9560 default:
9561 gcc_unreachable ();
9562 }
9563 }
9564
9565 /* Output the encoding of an attribute value. */
9566
9567 static void
9568 output_value_format (dw_attr_node *a)
9569 {
9570 enum dwarf_form form = value_format (a);
9571
9572 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9573 }
9574
9575 /* Given a die and id, produce the appropriate abbreviations. */
9576
9577 static void
9578 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9579 {
9580 unsigned ix;
9581 dw_attr_node *a_attr;
9582
9583 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9584 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9585 dwarf_tag_name (abbrev->die_tag));
9586
9587 if (abbrev->die_child != NULL)
9588 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9589 else
9590 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9591
9592 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9593 {
9594 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9595 dwarf_attr_name (a_attr->dw_attr));
9596 output_value_format (a_attr);
9597 if (value_format (a_attr) == DW_FORM_implicit_const)
9598 {
9599 if (AT_class (a_attr) == dw_val_class_file_implicit)
9600 {
9601 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9602 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9603 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9604 }
9605 else
9606 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9607 }
9608 }
9609
9610 dw2_asm_output_data (1, 0, NULL);
9611 dw2_asm_output_data (1, 0, NULL);
9612 }
9613
9614
9615 /* Output the .debug_abbrev section which defines the DIE abbreviation
9616 table. */
9617
9618 static void
9619 output_abbrev_section (void)
9620 {
9621 unsigned int abbrev_id;
9622 dw_die_ref abbrev;
9623
9624 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9625 if (abbrev_id != 0)
9626 output_die_abbrevs (abbrev_id, abbrev);
9627
9628 /* Terminate the table. */
9629 dw2_asm_output_data (1, 0, NULL);
9630 }
9631
9632 /* Return a new location list, given the begin and end range, and the
9633 expression. */
9634
9635 static inline dw_loc_list_ref
9636 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9637 const char *section)
9638 {
9639 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9640
9641 retlist->begin = begin;
9642 retlist->begin_entry = NULL;
9643 retlist->end = end;
9644 retlist->expr = expr;
9645 retlist->section = section;
9646
9647 return retlist;
9648 }
9649
9650 /* Generate a new internal symbol for this location list node, if it
9651 hasn't got one yet. */
9652
9653 static inline void
9654 gen_llsym (dw_loc_list_ref list)
9655 {
9656 gcc_assert (!list->ll_symbol);
9657 list->ll_symbol = gen_internal_sym ("LLST");
9658 }
9659
9660 /* Output the location list given to us. */
9661
9662 static void
9663 output_loc_list (dw_loc_list_ref list_head)
9664 {
9665 if (list_head->emitted)
9666 return;
9667 list_head->emitted = true;
9668
9669 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9670
9671 dw_loc_list_ref curr = list_head;
9672 const char *last_section = NULL;
9673 const char *base_label = NULL;
9674
9675 /* Walk the location list, and output each range + expression. */
9676 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9677 {
9678 unsigned long size;
9679 /* Don't output an entry that starts and ends at the same address. */
9680 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9681 continue;
9682 size = size_of_locs (curr->expr);
9683 /* If the expression is too large, drop it on the floor. We could
9684 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9685 in the expression, but >= 64KB expressions for a single value
9686 in a single range are unlikely very useful. */
9687 if (dwarf_version < 5 && size > 0xffff)
9688 continue;
9689 if (dwarf_version >= 5)
9690 {
9691 if (dwarf_split_debug_info)
9692 {
9693 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9694 uleb128 index into .debug_addr and uleb128 length. */
9695 dw2_asm_output_data (1, DW_LLE_startx_length,
9696 "DW_LLE_startx_length (%s)",
9697 list_head->ll_symbol);
9698 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9699 "Location list range start index "
9700 "(%s)", curr->begin);
9701 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9702 For that case we probably need to emit DW_LLE_startx_endx,
9703 but we'd need 2 .debug_addr entries rather than just one. */
9704 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9705 "Location list length (%s)",
9706 list_head->ll_symbol);
9707 }
9708 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9709 {
9710 /* If all code is in .text section, the base address is
9711 already provided by the CU attributes. Use
9712 DW_LLE_offset_pair where both addresses are uleb128 encoded
9713 offsets against that base. */
9714 dw2_asm_output_data (1, DW_LLE_offset_pair,
9715 "DW_LLE_offset_pair (%s)",
9716 list_head->ll_symbol);
9717 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9718 "Location list begin address (%s)",
9719 list_head->ll_symbol);
9720 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9721 "Location list end address (%s)",
9722 list_head->ll_symbol);
9723 }
9724 else if (HAVE_AS_LEB128)
9725 {
9726 /* Otherwise, find out how many consecutive entries could share
9727 the same base entry. If just one, emit DW_LLE_start_length,
9728 otherwise emit DW_LLE_base_address for the base address
9729 followed by a series of DW_LLE_offset_pair. */
9730 if (last_section == NULL || curr->section != last_section)
9731 {
9732 dw_loc_list_ref curr2;
9733 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9734 curr2 = curr2->dw_loc_next)
9735 {
9736 if (strcmp (curr2->begin, curr2->end) == 0
9737 && !curr2->force)
9738 continue;
9739 break;
9740 }
9741 if (curr2 == NULL || curr->section != curr2->section)
9742 last_section = NULL;
9743 else
9744 {
9745 last_section = curr->section;
9746 base_label = curr->begin;
9747 dw2_asm_output_data (1, DW_LLE_base_address,
9748 "DW_LLE_base_address (%s)",
9749 list_head->ll_symbol);
9750 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9751 "Base address (%s)",
9752 list_head->ll_symbol);
9753 }
9754 }
9755 /* Only one entry with the same base address. Use
9756 DW_LLE_start_length with absolute address and uleb128
9757 length. */
9758 if (last_section == NULL)
9759 {
9760 dw2_asm_output_data (1, DW_LLE_start_length,
9761 "DW_LLE_start_length (%s)",
9762 list_head->ll_symbol);
9763 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9764 "Location list begin address (%s)",
9765 list_head->ll_symbol);
9766 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9767 "Location list length "
9768 "(%s)", list_head->ll_symbol);
9769 }
9770 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9771 DW_LLE_base_address. */
9772 else
9773 {
9774 dw2_asm_output_data (1, DW_LLE_offset_pair,
9775 "DW_LLE_offset_pair (%s)",
9776 list_head->ll_symbol);
9777 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9778 "Location list begin address "
9779 "(%s)", list_head->ll_symbol);
9780 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9781 "Location list end address "
9782 "(%s)", list_head->ll_symbol);
9783 }
9784 }
9785 /* The assembler does not support .uleb128 directive. Emit
9786 DW_LLE_start_end with a pair of absolute addresses. */
9787 else
9788 {
9789 dw2_asm_output_data (1, DW_LLE_start_end,
9790 "DW_LLE_start_end (%s)",
9791 list_head->ll_symbol);
9792 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9793 "Location list begin address (%s)",
9794 list_head->ll_symbol);
9795 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9796 "Location list end address (%s)",
9797 list_head->ll_symbol);
9798 }
9799 }
9800 else if (dwarf_split_debug_info)
9801 {
9802 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9803 and 4 byte length. */
9804 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9805 "Location list start/length entry (%s)",
9806 list_head->ll_symbol);
9807 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9808 "Location list range start index (%s)",
9809 curr->begin);
9810 /* The length field is 4 bytes. If we ever need to support
9811 an 8-byte length, we can add a new DW_LLE code or fall back
9812 to DW_LLE_GNU_start_end_entry. */
9813 dw2_asm_output_delta (4, curr->end, curr->begin,
9814 "Location list range length (%s)",
9815 list_head->ll_symbol);
9816 }
9817 else if (!have_multiple_function_sections)
9818 {
9819 /* Pair of relative addresses against start of text section. */
9820 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9821 "Location list begin address (%s)",
9822 list_head->ll_symbol);
9823 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9824 "Location list end address (%s)",
9825 list_head->ll_symbol);
9826 }
9827 else
9828 {
9829 /* Pair of absolute addresses. */
9830 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9831 "Location list begin address (%s)",
9832 list_head->ll_symbol);
9833 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9834 "Location list end address (%s)",
9835 list_head->ll_symbol);
9836 }
9837
9838 /* Output the block length for this list of location operations. */
9839 if (dwarf_version >= 5)
9840 dw2_asm_output_data_uleb128 (size, "Location expression size");
9841 else
9842 {
9843 gcc_assert (size <= 0xffff);
9844 dw2_asm_output_data (2, size, "Location expression size");
9845 }
9846
9847 output_loc_sequence (curr->expr, -1);
9848 }
9849
9850 /* And finally list termination. */
9851 if (dwarf_version >= 5)
9852 dw2_asm_output_data (1, DW_LLE_end_of_list,
9853 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9854 else if (dwarf_split_debug_info)
9855 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9856 "Location list terminator (%s)",
9857 list_head->ll_symbol);
9858 else
9859 {
9860 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9861 "Location list terminator begin (%s)",
9862 list_head->ll_symbol);
9863 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9864 "Location list terminator end (%s)",
9865 list_head->ll_symbol);
9866 }
9867 }
9868
9869 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9870 section. Emit a relocated reference if val_entry is NULL, otherwise,
9871 emit an indirect reference. */
9872
9873 static void
9874 output_range_list_offset (dw_attr_node *a)
9875 {
9876 const char *name = dwarf_attr_name (a->dw_attr);
9877
9878 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9879 {
9880 if (dwarf_version >= 5)
9881 {
9882 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9883 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9884 debug_ranges_section, "%s", name);
9885 }
9886 else
9887 {
9888 char *p = strchr (ranges_section_label, '\0');
9889 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9890 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9891 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9892 debug_ranges_section, "%s", name);
9893 *p = '\0';
9894 }
9895 }
9896 else if (dwarf_version >= 5)
9897 {
9898 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9899 gcc_assert (rnglist_idx);
9900 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9901 }
9902 else
9903 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9904 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9905 "%s (offset from %s)", name, ranges_section_label);
9906 }
9907
9908 /* Output the offset into the debug_loc section. */
9909
9910 static void
9911 output_loc_list_offset (dw_attr_node *a)
9912 {
9913 char *sym = AT_loc_list (a)->ll_symbol;
9914
9915 gcc_assert (sym);
9916 if (!dwarf_split_debug_info)
9917 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9918 "%s", dwarf_attr_name (a->dw_attr));
9919 else if (dwarf_version >= 5)
9920 {
9921 gcc_assert (AT_loc_list (a)->num_assigned);
9922 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9923 dwarf_attr_name (a->dw_attr),
9924 sym);
9925 }
9926 else
9927 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9928 "%s", dwarf_attr_name (a->dw_attr));
9929 }
9930
9931 /* Output an attribute's index or value appropriately. */
9932
9933 static void
9934 output_attr_index_or_value (dw_attr_node *a)
9935 {
9936 const char *name = dwarf_attr_name (a->dw_attr);
9937
9938 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9939 {
9940 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9941 return;
9942 }
9943 switch (AT_class (a))
9944 {
9945 case dw_val_class_addr:
9946 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9947 break;
9948 case dw_val_class_high_pc:
9949 case dw_val_class_lbl_id:
9950 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9951 break;
9952 default:
9953 gcc_unreachable ();
9954 }
9955 }
9956
9957 /* Output a type signature. */
9958
9959 static inline void
9960 output_signature (const char *sig, const char *name)
9961 {
9962 int i;
9963
9964 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9965 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9966 }
9967
9968 /* Output a discriminant value. */
9969
9970 static inline void
9971 output_discr_value (dw_discr_value *discr_value, const char *name)
9972 {
9973 if (discr_value->pos)
9974 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9975 else
9976 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9977 }
9978
9979 /* Output the DIE and its attributes. Called recursively to generate
9980 the definitions of each child DIE. */
9981
9982 static void
9983 output_die (dw_die_ref die)
9984 {
9985 dw_attr_node *a;
9986 dw_die_ref c;
9987 unsigned long size;
9988 unsigned ix;
9989
9990 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9991 (unsigned long)die->die_offset,
9992 dwarf_tag_name (die->die_tag));
9993
9994 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9995 {
9996 const char *name = dwarf_attr_name (a->dw_attr);
9997
9998 switch (AT_class (a))
9999 {
10000 case dw_val_class_addr:
10001 output_attr_index_or_value (a);
10002 break;
10003
10004 case dw_val_class_offset:
10005 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10006 "%s", name);
10007 break;
10008
10009 case dw_val_class_range_list:
10010 output_range_list_offset (a);
10011 break;
10012
10013 case dw_val_class_loc:
10014 size = size_of_locs (AT_loc (a));
10015
10016 /* Output the block length for this list of location operations. */
10017 if (dwarf_version >= 4)
10018 dw2_asm_output_data_uleb128 (size, "%s", name);
10019 else
10020 dw2_asm_output_data (constant_size (size), size, "%s", name);
10021
10022 output_loc_sequence (AT_loc (a), -1);
10023 break;
10024
10025 case dw_val_class_const:
10026 /* ??? It would be slightly more efficient to use a scheme like is
10027 used for unsigned constants below, but gdb 4.x does not sign
10028 extend. Gdb 5.x does sign extend. */
10029 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10030 break;
10031
10032 case dw_val_class_unsigned_const:
10033 {
10034 int csize = constant_size (AT_unsigned (a));
10035 if (dwarf_version == 3
10036 && a->dw_attr == DW_AT_data_member_location
10037 && csize >= 4)
10038 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10039 else
10040 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10041 }
10042 break;
10043
10044 case dw_val_class_const_implicit:
10045 if (flag_debug_asm)
10046 fprintf (asm_out_file, "\t\t\t%s %s ("
10047 HOST_WIDE_INT_PRINT_DEC ")\n",
10048 ASM_COMMENT_START, name, AT_int (a));
10049 break;
10050
10051 case dw_val_class_unsigned_const_implicit:
10052 if (flag_debug_asm)
10053 fprintf (asm_out_file, "\t\t\t%s %s ("
10054 HOST_WIDE_INT_PRINT_HEX ")\n",
10055 ASM_COMMENT_START, name, AT_unsigned (a));
10056 break;
10057
10058 case dw_val_class_const_double:
10059 {
10060 unsigned HOST_WIDE_INT first, second;
10061
10062 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10063 dw2_asm_output_data (1,
10064 HOST_BITS_PER_DOUBLE_INT
10065 / HOST_BITS_PER_CHAR,
10066 NULL);
10067
10068 if (WORDS_BIG_ENDIAN)
10069 {
10070 first = a->dw_attr_val.v.val_double.high;
10071 second = a->dw_attr_val.v.val_double.low;
10072 }
10073 else
10074 {
10075 first = a->dw_attr_val.v.val_double.low;
10076 second = a->dw_attr_val.v.val_double.high;
10077 }
10078
10079 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10080 first, "%s", name);
10081 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10082 second, NULL);
10083 }
10084 break;
10085
10086 case dw_val_class_wide_int:
10087 {
10088 int i;
10089 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10090 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10091 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10092 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10093 * l, NULL);
10094
10095 if (WORDS_BIG_ENDIAN)
10096 for (i = len - 1; i >= 0; --i)
10097 {
10098 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10099 "%s", name);
10100 name = "";
10101 }
10102 else
10103 for (i = 0; i < len; ++i)
10104 {
10105 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10106 "%s", name);
10107 name = "";
10108 }
10109 }
10110 break;
10111
10112 case dw_val_class_vec:
10113 {
10114 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10115 unsigned int len = a->dw_attr_val.v.val_vec.length;
10116 unsigned int i;
10117 unsigned char *p;
10118
10119 dw2_asm_output_data (constant_size (len * elt_size),
10120 len * elt_size, "%s", name);
10121 if (elt_size > sizeof (HOST_WIDE_INT))
10122 {
10123 elt_size /= 2;
10124 len *= 2;
10125 }
10126 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10127 i < len;
10128 i++, p += elt_size)
10129 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10130 "fp or vector constant word %u", i);
10131 break;
10132 }
10133
10134 case dw_val_class_flag:
10135 if (dwarf_version >= 4)
10136 {
10137 /* Currently all add_AT_flag calls pass in 1 as last argument,
10138 so DW_FORM_flag_present can be used. If that ever changes,
10139 we'll need to use DW_FORM_flag and have some optimization
10140 in build_abbrev_table that will change those to
10141 DW_FORM_flag_present if it is set to 1 in all DIEs using
10142 the same abbrev entry. */
10143 gcc_assert (AT_flag (a) == 1);
10144 if (flag_debug_asm)
10145 fprintf (asm_out_file, "\t\t\t%s %s\n",
10146 ASM_COMMENT_START, name);
10147 break;
10148 }
10149 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10150 break;
10151
10152 case dw_val_class_loc_list:
10153 output_loc_list_offset (a);
10154 break;
10155
10156 case dw_val_class_die_ref:
10157 if (AT_ref_external (a))
10158 {
10159 if (AT_ref (a)->comdat_type_p)
10160 {
10161 comdat_type_node *type_node
10162 = AT_ref (a)->die_id.die_type_node;
10163
10164 gcc_assert (type_node);
10165 output_signature (type_node->signature, name);
10166 }
10167 else
10168 {
10169 const char *sym = AT_ref (a)->die_id.die_symbol;
10170 int size;
10171
10172 gcc_assert (sym);
10173 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10174 length, whereas in DWARF3 it's always sized as an
10175 offset. */
10176 if (dwarf_version == 2)
10177 size = DWARF2_ADDR_SIZE;
10178 else
10179 size = DWARF_OFFSET_SIZE;
10180 /* ??? We cannot unconditionally output die_offset if
10181 non-zero - others might create references to those
10182 DIEs via symbols.
10183 And we do not clear its DIE offset after outputting it
10184 (and the label refers to the actual DIEs, not the
10185 DWARF CU unit header which is when using label + offset
10186 would be the correct thing to do).
10187 ??? This is the reason for the with_offset flag. */
10188 if (AT_ref (a)->with_offset)
10189 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10190 debug_info_section, "%s", name);
10191 else
10192 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10193 name);
10194 }
10195 }
10196 else
10197 {
10198 gcc_assert (AT_ref (a)->die_offset);
10199 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10200 "%s", name);
10201 }
10202 break;
10203
10204 case dw_val_class_fde_ref:
10205 {
10206 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10207
10208 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10209 a->dw_attr_val.v.val_fde_index * 2);
10210 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10211 "%s", name);
10212 }
10213 break;
10214
10215 case dw_val_class_vms_delta:
10216 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10217 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10218 AT_vms_delta2 (a), AT_vms_delta1 (a),
10219 "%s", name);
10220 #else
10221 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10222 AT_vms_delta2 (a), AT_vms_delta1 (a),
10223 "%s", name);
10224 #endif
10225 break;
10226
10227 case dw_val_class_lbl_id:
10228 output_attr_index_or_value (a);
10229 break;
10230
10231 case dw_val_class_lineptr:
10232 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10233 debug_line_section, "%s", name);
10234 break;
10235
10236 case dw_val_class_macptr:
10237 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10238 debug_macinfo_section, "%s", name);
10239 break;
10240
10241 case dw_val_class_loclistsptr:
10242 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10243 debug_loc_section, "%s", name);
10244 break;
10245
10246 case dw_val_class_str:
10247 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10248 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10249 a->dw_attr_val.v.val_str->label,
10250 debug_str_section,
10251 "%s: \"%s\"", name, AT_string (a));
10252 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10253 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10254 a->dw_attr_val.v.val_str->label,
10255 debug_line_str_section,
10256 "%s: \"%s\"", name, AT_string (a));
10257 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10258 dw2_asm_output_data_uleb128 (AT_index (a),
10259 "%s: \"%s\"", name, AT_string (a));
10260 else
10261 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10262 break;
10263
10264 case dw_val_class_file:
10265 {
10266 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10267
10268 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10269 a->dw_attr_val.v.val_file->filename);
10270 break;
10271 }
10272
10273 case dw_val_class_file_implicit:
10274 if (flag_debug_asm)
10275 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10276 ASM_COMMENT_START, name,
10277 maybe_emit_file (a->dw_attr_val.v.val_file),
10278 a->dw_attr_val.v.val_file->filename);
10279 break;
10280
10281 case dw_val_class_data8:
10282 {
10283 int i;
10284
10285 for (i = 0; i < 8; i++)
10286 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10287 i == 0 ? "%s" : NULL, name);
10288 break;
10289 }
10290
10291 case dw_val_class_high_pc:
10292 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10293 get_AT_low_pc (die), "DW_AT_high_pc");
10294 break;
10295
10296 case dw_val_class_discr_value:
10297 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10298 break;
10299
10300 case dw_val_class_discr_list:
10301 {
10302 dw_discr_list_ref list = AT_discr_list (a);
10303 const int size = size_of_discr_list (list);
10304
10305 /* This is a block, so output its length first. */
10306 dw2_asm_output_data (constant_size (size), size,
10307 "%s: block size", name);
10308
10309 for (; list != NULL; list = list->dw_discr_next)
10310 {
10311 /* One byte for the discriminant value descriptor, and then as
10312 many LEB128 numbers as required. */
10313 if (list->dw_discr_range)
10314 dw2_asm_output_data (1, DW_DSC_range,
10315 "%s: DW_DSC_range", name);
10316 else
10317 dw2_asm_output_data (1, DW_DSC_label,
10318 "%s: DW_DSC_label", name);
10319
10320 output_discr_value (&list->dw_discr_lower_bound, name);
10321 if (list->dw_discr_range)
10322 output_discr_value (&list->dw_discr_upper_bound, name);
10323 }
10324 break;
10325 }
10326
10327 default:
10328 gcc_unreachable ();
10329 }
10330 }
10331
10332 FOR_EACH_CHILD (die, c, output_die (c));
10333
10334 /* Add null byte to terminate sibling list. */
10335 if (die->die_child != NULL)
10336 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10337 (unsigned long) die->die_offset);
10338 }
10339
10340 /* Output the compilation unit that appears at the beginning of the
10341 .debug_info section, and precedes the DIE descriptions. */
10342
10343 static void
10344 output_compilation_unit_header (enum dwarf_unit_type ut)
10345 {
10346 if (!XCOFF_DEBUGGING_INFO)
10347 {
10348 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10349 dw2_asm_output_data (4, 0xffffffff,
10350 "Initial length escape value indicating 64-bit DWARF extension");
10351 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10352 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10353 "Length of Compilation Unit Info");
10354 }
10355
10356 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10357 if (dwarf_version >= 5)
10358 {
10359 const char *name;
10360 switch (ut)
10361 {
10362 case DW_UT_compile: name = "DW_UT_compile"; break;
10363 case DW_UT_type: name = "DW_UT_type"; break;
10364 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10365 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10366 default: gcc_unreachable ();
10367 }
10368 dw2_asm_output_data (1, ut, "%s", name);
10369 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10370 }
10371 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10372 debug_abbrev_section,
10373 "Offset Into Abbrev. Section");
10374 if (dwarf_version < 5)
10375 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10376 }
10377
10378 /* Output the compilation unit DIE and its children. */
10379
10380 static void
10381 output_comp_unit (dw_die_ref die, int output_if_empty,
10382 const unsigned char *dwo_id)
10383 {
10384 const char *secname, *oldsym;
10385 char *tmp;
10386
10387 /* Unless we are outputting main CU, we may throw away empty ones. */
10388 if (!output_if_empty && die->die_child == NULL)
10389 return;
10390
10391 /* Even if there are no children of this DIE, we must output the information
10392 about the compilation unit. Otherwise, on an empty translation unit, we
10393 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10394 will then complain when examining the file. First mark all the DIEs in
10395 this CU so we know which get local refs. */
10396 mark_dies (die);
10397
10398 external_ref_hash_type *extern_map = optimize_external_refs (die);
10399
10400 /* For now, optimize only the main CU, in order to optimize the rest
10401 we'd need to see all of them earlier. Leave the rest for post-linking
10402 tools like DWZ. */
10403 if (die == comp_unit_die ())
10404 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10405
10406 build_abbrev_table (die, extern_map);
10407
10408 optimize_abbrev_table ();
10409
10410 delete extern_map;
10411
10412 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10413 next_die_offset = (dwo_id
10414 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10415 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10416 calc_die_sizes (die);
10417
10418 oldsym = die->die_id.die_symbol;
10419 if (oldsym && die->comdat_type_p)
10420 {
10421 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10422
10423 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10424 secname = tmp;
10425 die->die_id.die_symbol = NULL;
10426 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10427 }
10428 else
10429 {
10430 switch_to_section (debug_info_section);
10431 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10432 info_section_emitted = true;
10433 }
10434
10435 /* For LTO cross unit DIE refs we want a symbol on the start of the
10436 debuginfo section, not on the CU DIE. */
10437 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10438 {
10439 /* ??? No way to get visibility assembled without a decl. */
10440 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10441 get_identifier (oldsym), char_type_node);
10442 TREE_PUBLIC (decl) = true;
10443 TREE_STATIC (decl) = true;
10444 DECL_ARTIFICIAL (decl) = true;
10445 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10446 DECL_VISIBILITY_SPECIFIED (decl) = true;
10447 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10448 #ifdef ASM_WEAKEN_LABEL
10449 /* We prefer a .weak because that handles duplicates from duplicate
10450 archive members in a graceful way. */
10451 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10452 #else
10453 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10454 #endif
10455 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10456 }
10457
10458 /* Output debugging information. */
10459 output_compilation_unit_header (dwo_id
10460 ? DW_UT_split_compile : DW_UT_compile);
10461 if (dwarf_version >= 5)
10462 {
10463 if (dwo_id != NULL)
10464 for (int i = 0; i < 8; i++)
10465 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10466 }
10467 output_die (die);
10468
10469 /* Leave the marks on the main CU, so we can check them in
10470 output_pubnames. */
10471 if (oldsym)
10472 {
10473 unmark_dies (die);
10474 die->die_id.die_symbol = oldsym;
10475 }
10476 }
10477
10478 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10479 and .debug_pubtypes. This is configured per-target, but can be
10480 overridden by the -gpubnames or -gno-pubnames options. */
10481
10482 static inline bool
10483 want_pubnames (void)
10484 {
10485 if (debug_info_level <= DINFO_LEVEL_TERSE)
10486 return false;
10487 if (debug_generate_pub_sections != -1)
10488 return debug_generate_pub_sections;
10489 return targetm.want_debug_pub_sections;
10490 }
10491
10492 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10493
10494 static void
10495 add_AT_pubnames (dw_die_ref die)
10496 {
10497 if (want_pubnames ())
10498 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10499 }
10500
10501 /* Add a string attribute value to a skeleton DIE. */
10502
10503 static inline void
10504 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10505 const char *str)
10506 {
10507 dw_attr_node attr;
10508 struct indirect_string_node *node;
10509
10510 if (! skeleton_debug_str_hash)
10511 skeleton_debug_str_hash
10512 = hash_table<indirect_string_hasher>::create_ggc (10);
10513
10514 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10515 find_string_form (node);
10516 if (node->form == DW_FORM_GNU_str_index)
10517 node->form = DW_FORM_strp;
10518
10519 attr.dw_attr = attr_kind;
10520 attr.dw_attr_val.val_class = dw_val_class_str;
10521 attr.dw_attr_val.val_entry = NULL;
10522 attr.dw_attr_val.v.val_str = node;
10523 add_dwarf_attr (die, &attr);
10524 }
10525
10526 /* Helper function to generate top-level dies for skeleton debug_info and
10527 debug_types. */
10528
10529 static void
10530 add_top_level_skeleton_die_attrs (dw_die_ref die)
10531 {
10532 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10533 const char *comp_dir = comp_dir_string ();
10534
10535 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10536 if (comp_dir != NULL)
10537 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10538 add_AT_pubnames (die);
10539 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10540 }
10541
10542 /* Output skeleton debug sections that point to the dwo file. */
10543
10544 static void
10545 output_skeleton_debug_sections (dw_die_ref comp_unit,
10546 const unsigned char *dwo_id)
10547 {
10548 /* These attributes will be found in the full debug_info section. */
10549 remove_AT (comp_unit, DW_AT_producer);
10550 remove_AT (comp_unit, DW_AT_language);
10551
10552 switch_to_section (debug_skeleton_info_section);
10553 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10554
10555 /* Produce the skeleton compilation-unit header. This one differs enough from
10556 a normal CU header that it's better not to call output_compilation_unit
10557 header. */
10558 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10559 dw2_asm_output_data (4, 0xffffffff,
10560 "Initial length escape value indicating 64-bit "
10561 "DWARF extension");
10562
10563 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10564 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10565 - DWARF_INITIAL_LENGTH_SIZE
10566 + size_of_die (comp_unit),
10567 "Length of Compilation Unit Info");
10568 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10569 if (dwarf_version >= 5)
10570 {
10571 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10572 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10573 }
10574 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10575 debug_skeleton_abbrev_section,
10576 "Offset Into Abbrev. Section");
10577 if (dwarf_version < 5)
10578 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10579 else
10580 for (int i = 0; i < 8; i++)
10581 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10582
10583 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10584 output_die (comp_unit);
10585
10586 /* Build the skeleton debug_abbrev section. */
10587 switch_to_section (debug_skeleton_abbrev_section);
10588 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10589
10590 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10591
10592 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10593 }
10594
10595 /* Output a comdat type unit DIE and its children. */
10596
10597 static void
10598 output_comdat_type_unit (comdat_type_node *node)
10599 {
10600 const char *secname;
10601 char *tmp;
10602 int i;
10603 #if defined (OBJECT_FORMAT_ELF)
10604 tree comdat_key;
10605 #endif
10606
10607 /* First mark all the DIEs in this CU so we know which get local refs. */
10608 mark_dies (node->root_die);
10609
10610 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10611
10612 build_abbrev_table (node->root_die, extern_map);
10613
10614 delete extern_map;
10615 extern_map = NULL;
10616
10617 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10618 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10619 calc_die_sizes (node->root_die);
10620
10621 #if defined (OBJECT_FORMAT_ELF)
10622 if (dwarf_version >= 5)
10623 {
10624 if (!dwarf_split_debug_info)
10625 secname = ".debug_info";
10626 else
10627 secname = ".debug_info.dwo";
10628 }
10629 else if (!dwarf_split_debug_info)
10630 secname = ".debug_types";
10631 else
10632 secname = ".debug_types.dwo";
10633
10634 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10635 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10636 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10637 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10638 comdat_key = get_identifier (tmp);
10639 targetm.asm_out.named_section (secname,
10640 SECTION_DEBUG | SECTION_LINKONCE,
10641 comdat_key);
10642 #else
10643 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10644 sprintf (tmp, (dwarf_version >= 5
10645 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10646 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10647 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10648 secname = tmp;
10649 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10650 #endif
10651
10652 /* Output debugging information. */
10653 output_compilation_unit_header (dwarf_split_debug_info
10654 ? DW_UT_split_type : DW_UT_type);
10655 output_signature (node->signature, "Type Signature");
10656 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10657 "Offset to Type DIE");
10658 output_die (node->root_die);
10659
10660 unmark_dies (node->root_die);
10661 }
10662
10663 /* Return the DWARF2/3 pubname associated with a decl. */
10664
10665 static const char *
10666 dwarf2_name (tree decl, int scope)
10667 {
10668 if (DECL_NAMELESS (decl))
10669 return NULL;
10670 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10671 }
10672
10673 /* Add a new entry to .debug_pubnames if appropriate. */
10674
10675 static void
10676 add_pubname_string (const char *str, dw_die_ref die)
10677 {
10678 pubname_entry e;
10679
10680 e.die = die;
10681 e.name = xstrdup (str);
10682 vec_safe_push (pubname_table, e);
10683 }
10684
10685 static void
10686 add_pubname (tree decl, dw_die_ref die)
10687 {
10688 if (!want_pubnames ())
10689 return;
10690
10691 /* Don't add items to the table when we expect that the consumer will have
10692 just read the enclosing die. For example, if the consumer is looking at a
10693 class_member, it will either be inside the class already, or will have just
10694 looked up the class to find the member. Either way, searching the class is
10695 faster than searching the index. */
10696 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10697 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10698 {
10699 const char *name = dwarf2_name (decl, 1);
10700
10701 if (name)
10702 add_pubname_string (name, die);
10703 }
10704 }
10705
10706 /* Add an enumerator to the pubnames section. */
10707
10708 static void
10709 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10710 {
10711 pubname_entry e;
10712
10713 gcc_assert (scope_name);
10714 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10715 e.die = die;
10716 vec_safe_push (pubname_table, e);
10717 }
10718
10719 /* Add a new entry to .debug_pubtypes if appropriate. */
10720
10721 static void
10722 add_pubtype (tree decl, dw_die_ref die)
10723 {
10724 pubname_entry e;
10725
10726 if (!want_pubnames ())
10727 return;
10728
10729 if ((TREE_PUBLIC (decl)
10730 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10731 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10732 {
10733 tree scope = NULL;
10734 const char *scope_name = "";
10735 const char *sep = is_cxx () ? "::" : ".";
10736 const char *name;
10737
10738 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10739 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10740 {
10741 scope_name = lang_hooks.dwarf_name (scope, 1);
10742 if (scope_name != NULL && scope_name[0] != '\0')
10743 scope_name = concat (scope_name, sep, NULL);
10744 else
10745 scope_name = "";
10746 }
10747
10748 if (TYPE_P (decl))
10749 name = type_tag (decl);
10750 else
10751 name = lang_hooks.dwarf_name (decl, 1);
10752
10753 /* If we don't have a name for the type, there's no point in adding
10754 it to the table. */
10755 if (name != NULL && name[0] != '\0')
10756 {
10757 e.die = die;
10758 e.name = concat (scope_name, name, NULL);
10759 vec_safe_push (pubtype_table, e);
10760 }
10761
10762 /* Although it might be more consistent to add the pubinfo for the
10763 enumerators as their dies are created, they should only be added if the
10764 enum type meets the criteria above. So rather than re-check the parent
10765 enum type whenever an enumerator die is created, just output them all
10766 here. This isn't protected by the name conditional because anonymous
10767 enums don't have names. */
10768 if (die->die_tag == DW_TAG_enumeration_type)
10769 {
10770 dw_die_ref c;
10771
10772 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10773 }
10774 }
10775 }
10776
10777 /* Output a single entry in the pubnames table. */
10778
10779 static void
10780 output_pubname (dw_offset die_offset, pubname_entry *entry)
10781 {
10782 dw_die_ref die = entry->die;
10783 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10784
10785 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10786
10787 if (debug_generate_pub_sections == 2)
10788 {
10789 /* This logic follows gdb's method for determining the value of the flag
10790 byte. */
10791 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10792 switch (die->die_tag)
10793 {
10794 case DW_TAG_typedef:
10795 case DW_TAG_base_type:
10796 case DW_TAG_subrange_type:
10797 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10798 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10799 break;
10800 case DW_TAG_enumerator:
10801 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10802 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10803 if (!is_cxx ())
10804 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10805 break;
10806 case DW_TAG_subprogram:
10807 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10808 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10809 if (!is_ada ())
10810 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10811 break;
10812 case DW_TAG_constant:
10813 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10814 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10815 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10816 break;
10817 case DW_TAG_variable:
10818 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10819 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10820 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10821 break;
10822 case DW_TAG_namespace:
10823 case DW_TAG_imported_declaration:
10824 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10825 break;
10826 case DW_TAG_class_type:
10827 case DW_TAG_interface_type:
10828 case DW_TAG_structure_type:
10829 case DW_TAG_union_type:
10830 case DW_TAG_enumeration_type:
10831 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10832 if (!is_cxx ())
10833 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10834 break;
10835 default:
10836 /* An unusual tag. Leave the flag-byte empty. */
10837 break;
10838 }
10839 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10840 "GDB-index flags");
10841 }
10842
10843 dw2_asm_output_nstring (entry->name, -1, "external name");
10844 }
10845
10846
10847 /* Output the public names table used to speed up access to externally
10848 visible names; or the public types table used to find type definitions. */
10849
10850 static void
10851 output_pubnames (vec<pubname_entry, va_gc> *names)
10852 {
10853 unsigned i;
10854 unsigned long pubnames_length = size_of_pubnames (names);
10855 pubname_entry *pub;
10856
10857 if (!XCOFF_DEBUGGING_INFO)
10858 {
10859 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10860 dw2_asm_output_data (4, 0xffffffff,
10861 "Initial length escape value indicating 64-bit DWARF extension");
10862 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10863 "Pub Info Length");
10864 }
10865
10866 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10867 dw2_asm_output_data (2, 2, "DWARF Version");
10868
10869 if (dwarf_split_debug_info)
10870 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10871 debug_skeleton_info_section,
10872 "Offset of Compilation Unit Info");
10873 else
10874 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10875 debug_info_section,
10876 "Offset of Compilation Unit Info");
10877 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10878 "Compilation Unit Length");
10879
10880 FOR_EACH_VEC_ELT (*names, i, pub)
10881 {
10882 if (include_pubname_in_output (names, pub))
10883 {
10884 dw_offset die_offset = pub->die->die_offset;
10885
10886 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10887 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10888 gcc_assert (pub->die->die_mark);
10889
10890 /* If we're putting types in their own .debug_types sections,
10891 the .debug_pubtypes table will still point to the compile
10892 unit (not the type unit), so we want to use the offset of
10893 the skeleton DIE (if there is one). */
10894 if (pub->die->comdat_type_p && names == pubtype_table)
10895 {
10896 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10897
10898 if (type_node != NULL)
10899 die_offset = (type_node->skeleton_die != NULL
10900 ? type_node->skeleton_die->die_offset
10901 : comp_unit_die ()->die_offset);
10902 }
10903
10904 output_pubname (die_offset, pub);
10905 }
10906 }
10907
10908 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10909 }
10910
10911 /* Output public names and types tables if necessary. */
10912
10913 static void
10914 output_pubtables (void)
10915 {
10916 if (!want_pubnames () || !info_section_emitted)
10917 return;
10918
10919 switch_to_section (debug_pubnames_section);
10920 output_pubnames (pubname_table);
10921 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10922 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10923 simply won't look for the section. */
10924 switch_to_section (debug_pubtypes_section);
10925 output_pubnames (pubtype_table);
10926 }
10927
10928
10929 /* Output the information that goes into the .debug_aranges table.
10930 Namely, define the beginning and ending address range of the
10931 text section generated for this compilation unit. */
10932
10933 static void
10934 output_aranges (void)
10935 {
10936 unsigned i;
10937 unsigned long aranges_length = size_of_aranges ();
10938
10939 if (!XCOFF_DEBUGGING_INFO)
10940 {
10941 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10942 dw2_asm_output_data (4, 0xffffffff,
10943 "Initial length escape value indicating 64-bit DWARF extension");
10944 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10945 "Length of Address Ranges Info");
10946 }
10947
10948 /* Version number for aranges is still 2, even up to DWARF5. */
10949 dw2_asm_output_data (2, 2, "DWARF Version");
10950 if (dwarf_split_debug_info)
10951 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10952 debug_skeleton_info_section,
10953 "Offset of Compilation Unit Info");
10954 else
10955 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10956 debug_info_section,
10957 "Offset of Compilation Unit Info");
10958 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10959 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10960
10961 /* We need to align to twice the pointer size here. */
10962 if (DWARF_ARANGES_PAD_SIZE)
10963 {
10964 /* Pad using a 2 byte words so that padding is correct for any
10965 pointer size. */
10966 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10967 2 * DWARF2_ADDR_SIZE);
10968 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10969 dw2_asm_output_data (2, 0, NULL);
10970 }
10971
10972 /* It is necessary not to output these entries if the sections were
10973 not used; if the sections were not used, the length will be 0 and
10974 the address may end up as 0 if the section is discarded by ld
10975 --gc-sections, leaving an invalid (0, 0) entry that can be
10976 confused with the terminator. */
10977 if (text_section_used)
10978 {
10979 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10980 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10981 text_section_label, "Length");
10982 }
10983 if (cold_text_section_used)
10984 {
10985 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10986 "Address");
10987 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10988 cold_text_section_label, "Length");
10989 }
10990
10991 if (have_multiple_function_sections)
10992 {
10993 unsigned fde_idx;
10994 dw_fde_ref fde;
10995
10996 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10997 {
10998 if (DECL_IGNORED_P (fde->decl))
10999 continue;
11000 if (!fde->in_std_section)
11001 {
11002 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11003 "Address");
11004 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11005 fde->dw_fde_begin, "Length");
11006 }
11007 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11008 {
11009 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11010 "Address");
11011 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11012 fde->dw_fde_second_begin, "Length");
11013 }
11014 }
11015 }
11016
11017 /* Output the terminator words. */
11018 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11019 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11020 }
11021
11022 /* Add a new entry to .debug_ranges. Return its index into
11023 ranges_table vector. */
11024
11025 static unsigned int
11026 add_ranges_num (int num, bool maybe_new_sec)
11027 {
11028 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11029 vec_safe_push (ranges_table, r);
11030 return vec_safe_length (ranges_table) - 1;
11031 }
11032
11033 /* Add a new entry to .debug_ranges corresponding to a block, or a
11034 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11035 this entry might be in a different section from previous range. */
11036
11037 static unsigned int
11038 add_ranges (const_tree block, bool maybe_new_sec)
11039 {
11040 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11041 }
11042
11043 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11044 chain, or middle entry of a chain that will be directly referred to. */
11045
11046 static void
11047 note_rnglist_head (unsigned int offset)
11048 {
11049 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11050 return;
11051 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11052 }
11053
11054 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11055 When using dwarf_split_debug_info, address attributes in dies destined
11056 for the final executable should be direct references--setting the
11057 parameter force_direct ensures this behavior. */
11058
11059 static void
11060 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11061 bool *added, bool force_direct)
11062 {
11063 unsigned int in_use = vec_safe_length (ranges_by_label);
11064 unsigned int offset;
11065 dw_ranges_by_label rbl = { begin, end };
11066 vec_safe_push (ranges_by_label, rbl);
11067 offset = add_ranges_num (-(int)in_use - 1, true);
11068 if (!*added)
11069 {
11070 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11071 *added = true;
11072 note_rnglist_head (offset);
11073 }
11074 }
11075
11076 /* Emit .debug_ranges section. */
11077
11078 static void
11079 output_ranges (void)
11080 {
11081 unsigned i;
11082 static const char *const start_fmt = "Offset %#x";
11083 const char *fmt = start_fmt;
11084 dw_ranges *r;
11085
11086 switch_to_section (debug_ranges_section);
11087 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11088 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11089 {
11090 int block_num = r->num;
11091
11092 if (block_num > 0)
11093 {
11094 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11095 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11096
11097 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11098 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11099
11100 /* If all code is in the text section, then the compilation
11101 unit base address defaults to DW_AT_low_pc, which is the
11102 base of the text section. */
11103 if (!have_multiple_function_sections)
11104 {
11105 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11106 text_section_label,
11107 fmt, i * 2 * DWARF2_ADDR_SIZE);
11108 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11109 text_section_label, NULL);
11110 }
11111
11112 /* Otherwise, the compilation unit base address is zero,
11113 which allows us to use absolute addresses, and not worry
11114 about whether the target supports cross-section
11115 arithmetic. */
11116 else
11117 {
11118 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11119 fmt, i * 2 * DWARF2_ADDR_SIZE);
11120 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11121 }
11122
11123 fmt = NULL;
11124 }
11125
11126 /* Negative block_num stands for an index into ranges_by_label. */
11127 else if (block_num < 0)
11128 {
11129 int lab_idx = - block_num - 1;
11130
11131 if (!have_multiple_function_sections)
11132 {
11133 gcc_unreachable ();
11134 #if 0
11135 /* If we ever use add_ranges_by_labels () for a single
11136 function section, all we have to do is to take out
11137 the #if 0 above. */
11138 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11139 (*ranges_by_label)[lab_idx].begin,
11140 text_section_label,
11141 fmt, i * 2 * DWARF2_ADDR_SIZE);
11142 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11143 (*ranges_by_label)[lab_idx].end,
11144 text_section_label, NULL);
11145 #endif
11146 }
11147 else
11148 {
11149 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11150 (*ranges_by_label)[lab_idx].begin,
11151 fmt, i * 2 * DWARF2_ADDR_SIZE);
11152 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11153 (*ranges_by_label)[lab_idx].end,
11154 NULL);
11155 }
11156 }
11157 else
11158 {
11159 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11160 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11161 fmt = start_fmt;
11162 }
11163 }
11164 }
11165
11166 /* Non-zero if .debug_line_str should be used for .debug_line section
11167 strings or strings that are likely shareable with those. */
11168 #define DWARF5_USE_DEBUG_LINE_STR \
11169 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11170 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11171 /* FIXME: there is no .debug_line_str.dwo section, \
11172 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11173 && !dwarf_split_debug_info)
11174
11175 /* Assign .debug_rnglists indexes. */
11176
11177 static void
11178 index_rnglists (void)
11179 {
11180 unsigned i;
11181 dw_ranges *r;
11182
11183 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11184 if (r->label)
11185 r->idx = rnglist_idx++;
11186 }
11187
11188 /* Emit .debug_rnglists section. */
11189
11190 static void
11191 output_rnglists (unsigned generation)
11192 {
11193 unsigned i;
11194 dw_ranges *r;
11195 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11196 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11197 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11198
11199 switch_to_section (debug_ranges_section);
11200 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11201 /* There are up to 4 unique ranges labels per generation.
11202 See also init_sections_and_labels. */
11203 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11204 2 + generation * 4);
11205 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11206 3 + generation * 4);
11207 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11208 dw2_asm_output_data (4, 0xffffffff,
11209 "Initial length escape value indicating "
11210 "64-bit DWARF extension");
11211 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11212 "Length of Range Lists");
11213 ASM_OUTPUT_LABEL (asm_out_file, l1);
11214 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11215 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11216 dw2_asm_output_data (1, 0, "Segment Size");
11217 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11218 about relocation sizes and primarily care about the size of .debug*
11219 sections in linked shared libraries and executables, then
11220 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11221 into it are usually larger than just DW_FORM_sec_offset offsets
11222 into the .debug_rnglists section. */
11223 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11224 "Offset Entry Count");
11225 if (dwarf_split_debug_info)
11226 {
11227 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11228 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11229 if (r->label)
11230 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11231 ranges_base_label, NULL);
11232 }
11233
11234 const char *lab = "";
11235 unsigned int len = vec_safe_length (ranges_table);
11236 const char *base = NULL;
11237 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11238 {
11239 int block_num = r->num;
11240
11241 if (r->label)
11242 {
11243 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11244 lab = r->label;
11245 }
11246 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11247 base = NULL;
11248 if (block_num > 0)
11249 {
11250 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11251 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11252
11253 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11254 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11255
11256 if (HAVE_AS_LEB128)
11257 {
11258 /* If all code is in the text section, then the compilation
11259 unit base address defaults to DW_AT_low_pc, which is the
11260 base of the text section. */
11261 if (!have_multiple_function_sections)
11262 {
11263 dw2_asm_output_data (1, DW_RLE_offset_pair,
11264 "DW_RLE_offset_pair (%s)", lab);
11265 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11266 "Range begin address (%s)", lab);
11267 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11268 "Range end address (%s)", lab);
11269 continue;
11270 }
11271 if (base == NULL)
11272 {
11273 dw_ranges *r2 = NULL;
11274 if (i < len - 1)
11275 r2 = &(*ranges_table)[i + 1];
11276 if (r2
11277 && r2->num != 0
11278 && r2->label == NULL
11279 && !r2->maybe_new_sec)
11280 {
11281 dw2_asm_output_data (1, DW_RLE_base_address,
11282 "DW_RLE_base_address (%s)", lab);
11283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11284 "Base address (%s)", lab);
11285 strcpy (basebuf, blabel);
11286 base = basebuf;
11287 }
11288 }
11289 if (base)
11290 {
11291 dw2_asm_output_data (1, DW_RLE_offset_pair,
11292 "DW_RLE_offset_pair (%s)", lab);
11293 dw2_asm_output_delta_uleb128 (blabel, base,
11294 "Range begin address (%s)", lab);
11295 dw2_asm_output_delta_uleb128 (elabel, base,
11296 "Range end address (%s)", lab);
11297 continue;
11298 }
11299 dw2_asm_output_data (1, DW_RLE_start_length,
11300 "DW_RLE_start_length (%s)", lab);
11301 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11302 "Range begin address (%s)", lab);
11303 dw2_asm_output_delta_uleb128 (elabel, blabel,
11304 "Range length (%s)", lab);
11305 }
11306 else
11307 {
11308 dw2_asm_output_data (1, DW_RLE_start_end,
11309 "DW_RLE_start_end (%s)", lab);
11310 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11311 "Range begin address (%s)", lab);
11312 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11313 "Range end address (%s)", lab);
11314 }
11315 }
11316
11317 /* Negative block_num stands for an index into ranges_by_label. */
11318 else if (block_num < 0)
11319 {
11320 int lab_idx = - block_num - 1;
11321 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11322 const char *elabel = (*ranges_by_label)[lab_idx].end;
11323
11324 if (!have_multiple_function_sections)
11325 gcc_unreachable ();
11326 if (HAVE_AS_LEB128)
11327 {
11328 dw2_asm_output_data (1, DW_RLE_start_length,
11329 "DW_RLE_start_length (%s)", lab);
11330 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11331 "Range begin address (%s)", lab);
11332 dw2_asm_output_delta_uleb128 (elabel, blabel,
11333 "Range length (%s)", lab);
11334 }
11335 else
11336 {
11337 dw2_asm_output_data (1, DW_RLE_start_end,
11338 "DW_RLE_start_end (%s)", lab);
11339 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11340 "Range begin address (%s)", lab);
11341 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11342 "Range end address (%s)", lab);
11343 }
11344 }
11345 else
11346 dw2_asm_output_data (1, DW_RLE_end_of_list,
11347 "DW_RLE_end_of_list (%s)", lab);
11348 }
11349 ASM_OUTPUT_LABEL (asm_out_file, l2);
11350 }
11351
11352 /* Data structure containing information about input files. */
11353 struct file_info
11354 {
11355 const char *path; /* Complete file name. */
11356 const char *fname; /* File name part. */
11357 int length; /* Length of entire string. */
11358 struct dwarf_file_data * file_idx; /* Index in input file table. */
11359 int dir_idx; /* Index in directory table. */
11360 };
11361
11362 /* Data structure containing information about directories with source
11363 files. */
11364 struct dir_info
11365 {
11366 const char *path; /* Path including directory name. */
11367 int length; /* Path length. */
11368 int prefix; /* Index of directory entry which is a prefix. */
11369 int count; /* Number of files in this directory. */
11370 int dir_idx; /* Index of directory used as base. */
11371 };
11372
11373 /* Callback function for file_info comparison. We sort by looking at
11374 the directories in the path. */
11375
11376 static int
11377 file_info_cmp (const void *p1, const void *p2)
11378 {
11379 const struct file_info *const s1 = (const struct file_info *) p1;
11380 const struct file_info *const s2 = (const struct file_info *) p2;
11381 const unsigned char *cp1;
11382 const unsigned char *cp2;
11383
11384 /* Take care of file names without directories. We need to make sure that
11385 we return consistent values to qsort since some will get confused if
11386 we return the same value when identical operands are passed in opposite
11387 orders. So if neither has a directory, return 0 and otherwise return
11388 1 or -1 depending on which one has the directory. */
11389 if ((s1->path == s1->fname || s2->path == s2->fname))
11390 return (s2->path == s2->fname) - (s1->path == s1->fname);
11391
11392 cp1 = (const unsigned char *) s1->path;
11393 cp2 = (const unsigned char *) s2->path;
11394
11395 while (1)
11396 {
11397 ++cp1;
11398 ++cp2;
11399 /* Reached the end of the first path? If so, handle like above. */
11400 if ((cp1 == (const unsigned char *) s1->fname)
11401 || (cp2 == (const unsigned char *) s2->fname))
11402 return ((cp2 == (const unsigned char *) s2->fname)
11403 - (cp1 == (const unsigned char *) s1->fname));
11404
11405 /* Character of current path component the same? */
11406 else if (*cp1 != *cp2)
11407 return *cp1 - *cp2;
11408 }
11409 }
11410
11411 struct file_name_acquire_data
11412 {
11413 struct file_info *files;
11414 int used_files;
11415 int max_files;
11416 };
11417
11418 /* Traversal function for the hash table. */
11419
11420 int
11421 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11422 {
11423 struct dwarf_file_data *d = *slot;
11424 struct file_info *fi;
11425 const char *f;
11426
11427 gcc_assert (fnad->max_files >= d->emitted_number);
11428
11429 if (! d->emitted_number)
11430 return 1;
11431
11432 gcc_assert (fnad->max_files != fnad->used_files);
11433
11434 fi = fnad->files + fnad->used_files++;
11435
11436 /* Skip all leading "./". */
11437 f = d->filename;
11438 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11439 f += 2;
11440
11441 /* Create a new array entry. */
11442 fi->path = f;
11443 fi->length = strlen (f);
11444 fi->file_idx = d;
11445
11446 /* Search for the file name part. */
11447 f = strrchr (f, DIR_SEPARATOR);
11448 #if defined (DIR_SEPARATOR_2)
11449 {
11450 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11451
11452 if (g != NULL)
11453 {
11454 if (f == NULL || f < g)
11455 f = g;
11456 }
11457 }
11458 #endif
11459
11460 fi->fname = f == NULL ? fi->path : f + 1;
11461 return 1;
11462 }
11463
11464 /* Helper function for output_file_names. Emit a FORM encoded
11465 string STR, with assembly comment start ENTRY_KIND and
11466 index IDX */
11467
11468 static void
11469 output_line_string (enum dwarf_form form, const char *str,
11470 const char *entry_kind, unsigned int idx)
11471 {
11472 switch (form)
11473 {
11474 case DW_FORM_string:
11475 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11476 break;
11477 case DW_FORM_line_strp:
11478 if (!debug_line_str_hash)
11479 debug_line_str_hash
11480 = hash_table<indirect_string_hasher>::create_ggc (10);
11481
11482 struct indirect_string_node *node;
11483 node = find_AT_string_in_table (str, debug_line_str_hash);
11484 set_indirect_string (node);
11485 node->form = form;
11486 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11487 debug_line_str_section, "%s: %#x: \"%s\"",
11488 entry_kind, 0, node->str);
11489 break;
11490 default:
11491 gcc_unreachable ();
11492 }
11493 }
11494
11495 /* Output the directory table and the file name table. We try to minimize
11496 the total amount of memory needed. A heuristic is used to avoid large
11497 slowdowns with many input files. */
11498
11499 static void
11500 output_file_names (void)
11501 {
11502 struct file_name_acquire_data fnad;
11503 int numfiles;
11504 struct file_info *files;
11505 struct dir_info *dirs;
11506 int *saved;
11507 int *savehere;
11508 int *backmap;
11509 int ndirs;
11510 int idx_offset;
11511 int i;
11512
11513 if (!last_emitted_file)
11514 {
11515 if (dwarf_version >= 5)
11516 {
11517 dw2_asm_output_data (1, 0, "Directory entry format count");
11518 dw2_asm_output_data_uleb128 (0, "Directories count");
11519 dw2_asm_output_data (1, 0, "File name entry format count");
11520 dw2_asm_output_data_uleb128 (0, "File names count");
11521 }
11522 else
11523 {
11524 dw2_asm_output_data (1, 0, "End directory table");
11525 dw2_asm_output_data (1, 0, "End file name table");
11526 }
11527 return;
11528 }
11529
11530 numfiles = last_emitted_file->emitted_number;
11531
11532 /* Allocate the various arrays we need. */
11533 files = XALLOCAVEC (struct file_info, numfiles);
11534 dirs = XALLOCAVEC (struct dir_info, numfiles);
11535
11536 fnad.files = files;
11537 fnad.used_files = 0;
11538 fnad.max_files = numfiles;
11539 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11540 gcc_assert (fnad.used_files == fnad.max_files);
11541
11542 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11543
11544 /* Find all the different directories used. */
11545 dirs[0].path = files[0].path;
11546 dirs[0].length = files[0].fname - files[0].path;
11547 dirs[0].prefix = -1;
11548 dirs[0].count = 1;
11549 dirs[0].dir_idx = 0;
11550 files[0].dir_idx = 0;
11551 ndirs = 1;
11552
11553 for (i = 1; i < numfiles; i++)
11554 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11555 && memcmp (dirs[ndirs - 1].path, files[i].path,
11556 dirs[ndirs - 1].length) == 0)
11557 {
11558 /* Same directory as last entry. */
11559 files[i].dir_idx = ndirs - 1;
11560 ++dirs[ndirs - 1].count;
11561 }
11562 else
11563 {
11564 int j;
11565
11566 /* This is a new directory. */
11567 dirs[ndirs].path = files[i].path;
11568 dirs[ndirs].length = files[i].fname - files[i].path;
11569 dirs[ndirs].count = 1;
11570 dirs[ndirs].dir_idx = ndirs;
11571 files[i].dir_idx = ndirs;
11572
11573 /* Search for a prefix. */
11574 dirs[ndirs].prefix = -1;
11575 for (j = 0; j < ndirs; j++)
11576 if (dirs[j].length < dirs[ndirs].length
11577 && dirs[j].length > 1
11578 && (dirs[ndirs].prefix == -1
11579 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11580 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11581 dirs[ndirs].prefix = j;
11582
11583 ++ndirs;
11584 }
11585
11586 /* Now to the actual work. We have to find a subset of the directories which
11587 allow expressing the file name using references to the directory table
11588 with the least amount of characters. We do not do an exhaustive search
11589 where we would have to check out every combination of every single
11590 possible prefix. Instead we use a heuristic which provides nearly optimal
11591 results in most cases and never is much off. */
11592 saved = XALLOCAVEC (int, ndirs);
11593 savehere = XALLOCAVEC (int, ndirs);
11594
11595 memset (saved, '\0', ndirs * sizeof (saved[0]));
11596 for (i = 0; i < ndirs; i++)
11597 {
11598 int j;
11599 int total;
11600
11601 /* We can always save some space for the current directory. But this
11602 does not mean it will be enough to justify adding the directory. */
11603 savehere[i] = dirs[i].length;
11604 total = (savehere[i] - saved[i]) * dirs[i].count;
11605
11606 for (j = i + 1; j < ndirs; j++)
11607 {
11608 savehere[j] = 0;
11609 if (saved[j] < dirs[i].length)
11610 {
11611 /* Determine whether the dirs[i] path is a prefix of the
11612 dirs[j] path. */
11613 int k;
11614
11615 k = dirs[j].prefix;
11616 while (k != -1 && k != (int) i)
11617 k = dirs[k].prefix;
11618
11619 if (k == (int) i)
11620 {
11621 /* Yes it is. We can possibly save some memory by
11622 writing the filenames in dirs[j] relative to
11623 dirs[i]. */
11624 savehere[j] = dirs[i].length;
11625 total += (savehere[j] - saved[j]) * dirs[j].count;
11626 }
11627 }
11628 }
11629
11630 /* Check whether we can save enough to justify adding the dirs[i]
11631 directory. */
11632 if (total > dirs[i].length + 1)
11633 {
11634 /* It's worthwhile adding. */
11635 for (j = i; j < ndirs; j++)
11636 if (savehere[j] > 0)
11637 {
11638 /* Remember how much we saved for this directory so far. */
11639 saved[j] = savehere[j];
11640
11641 /* Remember the prefix directory. */
11642 dirs[j].dir_idx = i;
11643 }
11644 }
11645 }
11646
11647 /* Emit the directory name table. */
11648 idx_offset = dirs[0].length > 0 ? 1 : 0;
11649 enum dwarf_form str_form = DW_FORM_string;
11650 enum dwarf_form idx_form = DW_FORM_udata;
11651 if (dwarf_version >= 5)
11652 {
11653 const char *comp_dir = comp_dir_string ();
11654 if (comp_dir == NULL)
11655 comp_dir = "";
11656 dw2_asm_output_data (1, 1, "Directory entry format count");
11657 if (DWARF5_USE_DEBUG_LINE_STR)
11658 str_form = DW_FORM_line_strp;
11659 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11660 dw2_asm_output_data_uleb128 (str_form, "%s",
11661 get_DW_FORM_name (str_form));
11662 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11663 if (str_form == DW_FORM_string)
11664 {
11665 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11666 for (i = 1 - idx_offset; i < ndirs; i++)
11667 dw2_asm_output_nstring (dirs[i].path,
11668 dirs[i].length
11669 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11670 "Directory Entry: %#x", i + idx_offset);
11671 }
11672 else
11673 {
11674 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11675 for (i = 1 - idx_offset; i < ndirs; i++)
11676 {
11677 const char *str
11678 = ggc_alloc_string (dirs[i].path,
11679 dirs[i].length
11680 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11681 output_line_string (str_form, str, "Directory Entry",
11682 (unsigned) i + idx_offset);
11683 }
11684 }
11685 }
11686 else
11687 {
11688 for (i = 1 - idx_offset; i < ndirs; i++)
11689 dw2_asm_output_nstring (dirs[i].path,
11690 dirs[i].length
11691 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11692 "Directory Entry: %#x", i + idx_offset);
11693
11694 dw2_asm_output_data (1, 0, "End directory table");
11695 }
11696
11697 /* We have to emit them in the order of emitted_number since that's
11698 used in the debug info generation. To do this efficiently we
11699 generate a back-mapping of the indices first. */
11700 backmap = XALLOCAVEC (int, numfiles);
11701 for (i = 0; i < numfiles; i++)
11702 backmap[files[i].file_idx->emitted_number - 1] = i;
11703
11704 if (dwarf_version >= 5)
11705 {
11706 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11707 if (filename0 == NULL)
11708 filename0 = "";
11709 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11710 DW_FORM_data2. Choose one based on the number of directories
11711 and how much space would they occupy in each encoding.
11712 If we have at most 256 directories, all indexes fit into
11713 a single byte, so DW_FORM_data1 is most compact (if there
11714 are at most 128 directories, DW_FORM_udata would be as
11715 compact as that, but not shorter and slower to decode). */
11716 if (ndirs + idx_offset <= 256)
11717 idx_form = DW_FORM_data1;
11718 /* If there are more than 65536 directories, we have to use
11719 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11720 Otherwise, compute what space would occupy if all the indexes
11721 used DW_FORM_udata - sum - and compare that to how large would
11722 be DW_FORM_data2 encoding, and pick the more efficient one. */
11723 else if (ndirs + idx_offset <= 65536)
11724 {
11725 unsigned HOST_WIDE_INT sum = 1;
11726 for (i = 0; i < numfiles; i++)
11727 {
11728 int file_idx = backmap[i];
11729 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11730 sum += size_of_uleb128 (dir_idx);
11731 }
11732 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11733 idx_form = DW_FORM_data2;
11734 }
11735 #ifdef VMS_DEBUGGING_INFO
11736 dw2_asm_output_data (1, 4, "File name entry format count");
11737 #else
11738 dw2_asm_output_data (1, 2, "File name entry format count");
11739 #endif
11740 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11741 dw2_asm_output_data_uleb128 (str_form, "%s",
11742 get_DW_FORM_name (str_form));
11743 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11744 "DW_LNCT_directory_index");
11745 dw2_asm_output_data_uleb128 (idx_form, "%s",
11746 get_DW_FORM_name (idx_form));
11747 #ifdef VMS_DEBUGGING_INFO
11748 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11749 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11750 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11751 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11752 #endif
11753 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11754
11755 output_line_string (str_form, filename0, "File Entry", 0);
11756
11757 /* Include directory index. */
11758 if (idx_form != DW_FORM_udata)
11759 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11760 0, NULL);
11761 else
11762 dw2_asm_output_data_uleb128 (0, NULL);
11763
11764 #ifdef VMS_DEBUGGING_INFO
11765 dw2_asm_output_data_uleb128 (0, NULL);
11766 dw2_asm_output_data_uleb128 (0, NULL);
11767 #endif
11768 }
11769
11770 /* Now write all the file names. */
11771 for (i = 0; i < numfiles; i++)
11772 {
11773 int file_idx = backmap[i];
11774 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11775
11776 #ifdef VMS_DEBUGGING_INFO
11777 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11778
11779 /* Setting these fields can lead to debugger miscomparisons,
11780 but VMS Debug requires them to be set correctly. */
11781
11782 int ver;
11783 long long cdt;
11784 long siz;
11785 int maxfilelen = (strlen (files[file_idx].path)
11786 + dirs[dir_idx].length
11787 + MAX_VMS_VERSION_LEN + 1);
11788 char *filebuf = XALLOCAVEC (char, maxfilelen);
11789
11790 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11791 snprintf (filebuf, maxfilelen, "%s;%d",
11792 files[file_idx].path + dirs[dir_idx].length, ver);
11793
11794 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11795
11796 /* Include directory index. */
11797 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11798 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11799 dir_idx + idx_offset, NULL);
11800 else
11801 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11802
11803 /* Modification time. */
11804 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11805 &cdt, 0, 0, 0) == 0)
11806 ? cdt : 0, NULL);
11807
11808 /* File length in bytes. */
11809 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11810 0, &siz, 0, 0) == 0)
11811 ? siz : 0, NULL);
11812 #else
11813 output_line_string (str_form,
11814 files[file_idx].path + dirs[dir_idx].length,
11815 "File Entry", (unsigned) i + 1);
11816
11817 /* Include directory index. */
11818 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11819 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11820 dir_idx + idx_offset, NULL);
11821 else
11822 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11823
11824 if (dwarf_version >= 5)
11825 continue;
11826
11827 /* Modification time. */
11828 dw2_asm_output_data_uleb128 (0, NULL);
11829
11830 /* File length in bytes. */
11831 dw2_asm_output_data_uleb128 (0, NULL);
11832 #endif /* VMS_DEBUGGING_INFO */
11833 }
11834
11835 if (dwarf_version < 5)
11836 dw2_asm_output_data (1, 0, "End file name table");
11837 }
11838
11839
11840 /* Output one line number table into the .debug_line section. */
11841
11842 static void
11843 output_one_line_info_table (dw_line_info_table *table)
11844 {
11845 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11846 unsigned int current_line = 1;
11847 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11848 dw_line_info_entry *ent;
11849 size_t i;
11850
11851 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11852 {
11853 switch (ent->opcode)
11854 {
11855 case LI_set_address:
11856 /* ??? Unfortunately, we have little choice here currently, and
11857 must always use the most general form. GCC does not know the
11858 address delta itself, so we can't use DW_LNS_advance_pc. Many
11859 ports do have length attributes which will give an upper bound
11860 on the address range. We could perhaps use length attributes
11861 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11862 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11863
11864 /* This can handle any delta. This takes
11865 4+DWARF2_ADDR_SIZE bytes. */
11866 dw2_asm_output_data (1, 0, "set address %s", line_label);
11867 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11868 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11869 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11870 break;
11871
11872 case LI_set_line:
11873 if (ent->val == current_line)
11874 {
11875 /* We still need to start a new row, so output a copy insn. */
11876 dw2_asm_output_data (1, DW_LNS_copy,
11877 "copy line %u", current_line);
11878 }
11879 else
11880 {
11881 int line_offset = ent->val - current_line;
11882 int line_delta = line_offset - DWARF_LINE_BASE;
11883
11884 current_line = ent->val;
11885 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11886 {
11887 /* This can handle deltas from -10 to 234, using the current
11888 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11889 This takes 1 byte. */
11890 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11891 "line %u", current_line);
11892 }
11893 else
11894 {
11895 /* This can handle any delta. This takes at least 4 bytes,
11896 depending on the value being encoded. */
11897 dw2_asm_output_data (1, DW_LNS_advance_line,
11898 "advance to line %u", current_line);
11899 dw2_asm_output_data_sleb128 (line_offset, NULL);
11900 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11901 }
11902 }
11903 break;
11904
11905 case LI_set_file:
11906 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11907 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11908 break;
11909
11910 case LI_set_column:
11911 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11912 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11913 break;
11914
11915 case LI_negate_stmt:
11916 current_is_stmt = !current_is_stmt;
11917 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11918 "is_stmt %d", current_is_stmt);
11919 break;
11920
11921 case LI_set_prologue_end:
11922 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11923 "set prologue end");
11924 break;
11925
11926 case LI_set_epilogue_begin:
11927 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11928 "set epilogue begin");
11929 break;
11930
11931 case LI_set_discriminator:
11932 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11933 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11934 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11935 dw2_asm_output_data_uleb128 (ent->val, NULL);
11936 break;
11937 }
11938 }
11939
11940 /* Emit debug info for the address of the end of the table. */
11941 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11942 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11943 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11944 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11945
11946 dw2_asm_output_data (1, 0, "end sequence");
11947 dw2_asm_output_data_uleb128 (1, NULL);
11948 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11949 }
11950
11951 /* Output the source line number correspondence information. This
11952 information goes into the .debug_line section. */
11953
11954 static void
11955 output_line_info (bool prologue_only)
11956 {
11957 static unsigned int generation;
11958 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11959 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11960 bool saw_one = false;
11961 int opc;
11962
11963 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11964 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11965 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11966 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11967
11968 if (!XCOFF_DEBUGGING_INFO)
11969 {
11970 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11971 dw2_asm_output_data (4, 0xffffffff,
11972 "Initial length escape value indicating 64-bit DWARF extension");
11973 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11974 "Length of Source Line Info");
11975 }
11976
11977 ASM_OUTPUT_LABEL (asm_out_file, l1);
11978
11979 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11980 if (dwarf_version >= 5)
11981 {
11982 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11983 dw2_asm_output_data (1, 0, "Segment Size");
11984 }
11985 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11986 ASM_OUTPUT_LABEL (asm_out_file, p1);
11987
11988 /* Define the architecture-dependent minimum instruction length (in bytes).
11989 In this implementation of DWARF, this field is used for information
11990 purposes only. Since GCC generates assembly language, we have no
11991 a priori knowledge of how many instruction bytes are generated for each
11992 source line, and therefore can use only the DW_LNE_set_address and
11993 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11994 this as '1', which is "correct enough" for all architectures,
11995 and don't let the target override. */
11996 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11997
11998 if (dwarf_version >= 4)
11999 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12000 "Maximum Operations Per Instruction");
12001 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12002 "Default is_stmt_start flag");
12003 dw2_asm_output_data (1, DWARF_LINE_BASE,
12004 "Line Base Value (Special Opcodes)");
12005 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12006 "Line Range Value (Special Opcodes)");
12007 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12008 "Special Opcode Base");
12009
12010 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12011 {
12012 int n_op_args;
12013 switch (opc)
12014 {
12015 case DW_LNS_advance_pc:
12016 case DW_LNS_advance_line:
12017 case DW_LNS_set_file:
12018 case DW_LNS_set_column:
12019 case DW_LNS_fixed_advance_pc:
12020 case DW_LNS_set_isa:
12021 n_op_args = 1;
12022 break;
12023 default:
12024 n_op_args = 0;
12025 break;
12026 }
12027
12028 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12029 opc, n_op_args);
12030 }
12031
12032 /* Write out the information about the files we use. */
12033 output_file_names ();
12034 ASM_OUTPUT_LABEL (asm_out_file, p2);
12035 if (prologue_only)
12036 {
12037 /* Output the marker for the end of the line number info. */
12038 ASM_OUTPUT_LABEL (asm_out_file, l2);
12039 return;
12040 }
12041
12042 if (separate_line_info)
12043 {
12044 dw_line_info_table *table;
12045 size_t i;
12046
12047 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12048 if (table->in_use)
12049 {
12050 output_one_line_info_table (table);
12051 saw_one = true;
12052 }
12053 }
12054 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12055 {
12056 output_one_line_info_table (cold_text_section_line_info);
12057 saw_one = true;
12058 }
12059
12060 /* ??? Some Darwin linkers crash on a .debug_line section with no
12061 sequences. Further, merely a DW_LNE_end_sequence entry is not
12062 sufficient -- the address column must also be initialized.
12063 Make sure to output at least one set_address/end_sequence pair,
12064 choosing .text since that section is always present. */
12065 if (text_section_line_info->in_use || !saw_one)
12066 output_one_line_info_table (text_section_line_info);
12067
12068 /* Output the marker for the end of the line number info. */
12069 ASM_OUTPUT_LABEL (asm_out_file, l2);
12070 }
12071 \f
12072 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12073
12074 static inline bool
12075 need_endianity_attribute_p (bool reverse)
12076 {
12077 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12078 }
12079
12080 /* Given a pointer to a tree node for some base type, return a pointer to
12081 a DIE that describes the given type. REVERSE is true if the type is
12082 to be interpreted in the reverse storage order wrt the target order.
12083
12084 This routine must only be called for GCC type nodes that correspond to
12085 Dwarf base (fundamental) types. */
12086
12087 static dw_die_ref
12088 base_type_die (tree type, bool reverse)
12089 {
12090 dw_die_ref base_type_result;
12091 enum dwarf_type encoding;
12092 bool fpt_used = false;
12093 struct fixed_point_type_info fpt_info;
12094 tree type_bias = NULL_TREE;
12095
12096 /* If this is a subtype that should not be emitted as a subrange type,
12097 use the base type. See subrange_type_for_debug_p. */
12098 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12099 type = TREE_TYPE (type);
12100
12101 switch (TREE_CODE (type))
12102 {
12103 case INTEGER_TYPE:
12104 if ((dwarf_version >= 4 || !dwarf_strict)
12105 && TYPE_NAME (type)
12106 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12107 && DECL_IS_BUILTIN (TYPE_NAME (type))
12108 && DECL_NAME (TYPE_NAME (type)))
12109 {
12110 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12111 if (strcmp (name, "char16_t") == 0
12112 || strcmp (name, "char32_t") == 0)
12113 {
12114 encoding = DW_ATE_UTF;
12115 break;
12116 }
12117 }
12118 if ((dwarf_version >= 3 || !dwarf_strict)
12119 && lang_hooks.types.get_fixed_point_type_info)
12120 {
12121 memset (&fpt_info, 0, sizeof (fpt_info));
12122 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12123 {
12124 fpt_used = true;
12125 encoding = ((TYPE_UNSIGNED (type))
12126 ? DW_ATE_unsigned_fixed
12127 : DW_ATE_signed_fixed);
12128 break;
12129 }
12130 }
12131 if (TYPE_STRING_FLAG (type))
12132 {
12133 if (TYPE_UNSIGNED (type))
12134 encoding = DW_ATE_unsigned_char;
12135 else
12136 encoding = DW_ATE_signed_char;
12137 }
12138 else if (TYPE_UNSIGNED (type))
12139 encoding = DW_ATE_unsigned;
12140 else
12141 encoding = DW_ATE_signed;
12142
12143 if (!dwarf_strict
12144 && lang_hooks.types.get_type_bias)
12145 type_bias = lang_hooks.types.get_type_bias (type);
12146 break;
12147
12148 case REAL_TYPE:
12149 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12150 {
12151 if (dwarf_version >= 3 || !dwarf_strict)
12152 encoding = DW_ATE_decimal_float;
12153 else
12154 encoding = DW_ATE_lo_user;
12155 }
12156 else
12157 encoding = DW_ATE_float;
12158 break;
12159
12160 case FIXED_POINT_TYPE:
12161 if (!(dwarf_version >= 3 || !dwarf_strict))
12162 encoding = DW_ATE_lo_user;
12163 else if (TYPE_UNSIGNED (type))
12164 encoding = DW_ATE_unsigned_fixed;
12165 else
12166 encoding = DW_ATE_signed_fixed;
12167 break;
12168
12169 /* Dwarf2 doesn't know anything about complex ints, so use
12170 a user defined type for it. */
12171 case COMPLEX_TYPE:
12172 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12173 encoding = DW_ATE_complex_float;
12174 else
12175 encoding = DW_ATE_lo_user;
12176 break;
12177
12178 case BOOLEAN_TYPE:
12179 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12180 encoding = DW_ATE_boolean;
12181 break;
12182
12183 default:
12184 /* No other TREE_CODEs are Dwarf fundamental types. */
12185 gcc_unreachable ();
12186 }
12187
12188 base_type_result = new_die_raw (DW_TAG_base_type);
12189
12190 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12191 int_size_in_bytes (type));
12192 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12193
12194 if (need_endianity_attribute_p (reverse))
12195 add_AT_unsigned (base_type_result, DW_AT_endianity,
12196 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12197
12198 add_alignment_attribute (base_type_result, type);
12199
12200 if (fpt_used)
12201 {
12202 switch (fpt_info.scale_factor_kind)
12203 {
12204 case fixed_point_scale_factor_binary:
12205 add_AT_int (base_type_result, DW_AT_binary_scale,
12206 fpt_info.scale_factor.binary);
12207 break;
12208
12209 case fixed_point_scale_factor_decimal:
12210 add_AT_int (base_type_result, DW_AT_decimal_scale,
12211 fpt_info.scale_factor.decimal);
12212 break;
12213
12214 case fixed_point_scale_factor_arbitrary:
12215 /* Arbitrary scale factors cannot be described in standard DWARF,
12216 yet. */
12217 if (!dwarf_strict)
12218 {
12219 /* Describe the scale factor as a rational constant. */
12220 const dw_die_ref scale_factor
12221 = new_die (DW_TAG_constant, comp_unit_die (), type);
12222
12223 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12224 fpt_info.scale_factor.arbitrary.numerator);
12225 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12226 fpt_info.scale_factor.arbitrary.denominator);
12227
12228 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12229 }
12230 break;
12231
12232 default:
12233 gcc_unreachable ();
12234 }
12235 }
12236
12237 if (type_bias)
12238 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12239 dw_scalar_form_constant
12240 | dw_scalar_form_exprloc
12241 | dw_scalar_form_reference,
12242 NULL);
12243
12244 return base_type_result;
12245 }
12246
12247 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12248 named 'auto' in its type: return true for it, false otherwise. */
12249
12250 static inline bool
12251 is_cxx_auto (tree type)
12252 {
12253 if (is_cxx ())
12254 {
12255 tree name = TYPE_IDENTIFIER (type);
12256 if (name == get_identifier ("auto")
12257 || name == get_identifier ("decltype(auto)"))
12258 return true;
12259 }
12260 return false;
12261 }
12262
12263 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12264 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12265
12266 static inline int
12267 is_base_type (tree type)
12268 {
12269 switch (TREE_CODE (type))
12270 {
12271 case INTEGER_TYPE:
12272 case REAL_TYPE:
12273 case FIXED_POINT_TYPE:
12274 case COMPLEX_TYPE:
12275 case BOOLEAN_TYPE:
12276 case POINTER_BOUNDS_TYPE:
12277 return 1;
12278
12279 case VOID_TYPE:
12280 case ARRAY_TYPE:
12281 case RECORD_TYPE:
12282 case UNION_TYPE:
12283 case QUAL_UNION_TYPE:
12284 case ENUMERAL_TYPE:
12285 case FUNCTION_TYPE:
12286 case METHOD_TYPE:
12287 case POINTER_TYPE:
12288 case REFERENCE_TYPE:
12289 case NULLPTR_TYPE:
12290 case OFFSET_TYPE:
12291 case LANG_TYPE:
12292 case VECTOR_TYPE:
12293 return 0;
12294
12295 default:
12296 if (is_cxx_auto (type))
12297 return 0;
12298 gcc_unreachable ();
12299 }
12300
12301 return 0;
12302 }
12303
12304 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12305 node, return the size in bits for the type if it is a constant, or else
12306 return the alignment for the type if the type's size is not constant, or
12307 else return BITS_PER_WORD if the type actually turns out to be an
12308 ERROR_MARK node. */
12309
12310 static inline unsigned HOST_WIDE_INT
12311 simple_type_size_in_bits (const_tree type)
12312 {
12313 if (TREE_CODE (type) == ERROR_MARK)
12314 return BITS_PER_WORD;
12315 else if (TYPE_SIZE (type) == NULL_TREE)
12316 return 0;
12317 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12318 return tree_to_uhwi (TYPE_SIZE (type));
12319 else
12320 return TYPE_ALIGN (type);
12321 }
12322
12323 /* Similarly, but return an offset_int instead of UHWI. */
12324
12325 static inline offset_int
12326 offset_int_type_size_in_bits (const_tree type)
12327 {
12328 if (TREE_CODE (type) == ERROR_MARK)
12329 return BITS_PER_WORD;
12330 else if (TYPE_SIZE (type) == NULL_TREE)
12331 return 0;
12332 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12333 return wi::to_offset (TYPE_SIZE (type));
12334 else
12335 return TYPE_ALIGN (type);
12336 }
12337
12338 /* Given a pointer to a tree node for a subrange type, return a pointer
12339 to a DIE that describes the given type. */
12340
12341 static dw_die_ref
12342 subrange_type_die (tree type, tree low, tree high, tree bias,
12343 dw_die_ref context_die)
12344 {
12345 dw_die_ref subrange_die;
12346 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12347
12348 if (context_die == NULL)
12349 context_die = comp_unit_die ();
12350
12351 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12352
12353 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12354 {
12355 /* The size of the subrange type and its base type do not match,
12356 so we need to generate a size attribute for the subrange type. */
12357 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12358 }
12359
12360 add_alignment_attribute (subrange_die, type);
12361
12362 if (low)
12363 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12364 if (high)
12365 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12366 if (bias && !dwarf_strict)
12367 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12368 dw_scalar_form_constant
12369 | dw_scalar_form_exprloc
12370 | dw_scalar_form_reference,
12371 NULL);
12372
12373 return subrange_die;
12374 }
12375
12376 /* Returns the (const and/or volatile) cv_qualifiers associated with
12377 the decl node. This will normally be augmented with the
12378 cv_qualifiers of the underlying type in add_type_attribute. */
12379
12380 static int
12381 decl_quals (const_tree decl)
12382 {
12383 return ((TREE_READONLY (decl)
12384 /* The C++ front-end correctly marks reference-typed
12385 variables as readonly, but from a language (and debug
12386 info) standpoint they are not const-qualified. */
12387 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12388 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12389 | (TREE_THIS_VOLATILE (decl)
12390 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12391 }
12392
12393 /* Determine the TYPE whose qualifiers match the largest strict subset
12394 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12395 qualifiers outside QUAL_MASK. */
12396
12397 static int
12398 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12399 {
12400 tree t;
12401 int best_rank = 0, best_qual = 0, max_rank;
12402
12403 type_quals &= qual_mask;
12404 max_rank = popcount_hwi (type_quals) - 1;
12405
12406 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12407 t = TYPE_NEXT_VARIANT (t))
12408 {
12409 int q = TYPE_QUALS (t) & qual_mask;
12410
12411 if ((q & type_quals) == q && q != type_quals
12412 && check_base_type (t, type))
12413 {
12414 int rank = popcount_hwi (q);
12415
12416 if (rank > best_rank)
12417 {
12418 best_rank = rank;
12419 best_qual = q;
12420 }
12421 }
12422 }
12423
12424 return best_qual;
12425 }
12426
12427 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12428 static const dwarf_qual_info_t dwarf_qual_info[] =
12429 {
12430 { TYPE_QUAL_CONST, DW_TAG_const_type },
12431 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12432 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12433 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12434 };
12435 static const unsigned int dwarf_qual_info_size
12436 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12437
12438 /* If DIE is a qualified DIE of some base DIE with the same parent,
12439 return the base DIE, otherwise return NULL. Set MASK to the
12440 qualifiers added compared to the returned DIE. */
12441
12442 static dw_die_ref
12443 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12444 {
12445 unsigned int i;
12446 for (i = 0; i < dwarf_qual_info_size; i++)
12447 if (die->die_tag == dwarf_qual_info[i].t)
12448 break;
12449 if (i == dwarf_qual_info_size)
12450 return NULL;
12451 if (vec_safe_length (die->die_attr) != 1)
12452 return NULL;
12453 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12454 if (type == NULL || type->die_parent != die->die_parent)
12455 return NULL;
12456 *mask |= dwarf_qual_info[i].q;
12457 if (depth)
12458 {
12459 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12460 if (ret)
12461 return ret;
12462 }
12463 return type;
12464 }
12465
12466 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12467 entry that chains the modifiers specified by CV_QUALS in front of the
12468 given type. REVERSE is true if the type is to be interpreted in the
12469 reverse storage order wrt the target order. */
12470
12471 static dw_die_ref
12472 modified_type_die (tree type, int cv_quals, bool reverse,
12473 dw_die_ref context_die)
12474 {
12475 enum tree_code code = TREE_CODE (type);
12476 dw_die_ref mod_type_die;
12477 dw_die_ref sub_die = NULL;
12478 tree item_type = NULL;
12479 tree qualified_type;
12480 tree name, low, high;
12481 dw_die_ref mod_scope;
12482 /* Only these cv-qualifiers are currently handled. */
12483 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12484 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12485 ENCODE_QUAL_ADDR_SPACE(~0U));
12486 const bool reverse_base_type
12487 = need_endianity_attribute_p (reverse) && is_base_type (type);
12488
12489 if (code == ERROR_MARK)
12490 return NULL;
12491
12492 if (lang_hooks.types.get_debug_type)
12493 {
12494 tree debug_type = lang_hooks.types.get_debug_type (type);
12495
12496 if (debug_type != NULL_TREE && debug_type != type)
12497 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12498 }
12499
12500 cv_quals &= cv_qual_mask;
12501
12502 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12503 tag modifier (and not an attribute) old consumers won't be able
12504 to handle it. */
12505 if (dwarf_version < 3)
12506 cv_quals &= ~TYPE_QUAL_RESTRICT;
12507
12508 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12509 if (dwarf_version < 5)
12510 cv_quals &= ~TYPE_QUAL_ATOMIC;
12511
12512 /* See if we already have the appropriately qualified variant of
12513 this type. */
12514 qualified_type = get_qualified_type (type, cv_quals);
12515
12516 if (qualified_type == sizetype)
12517 {
12518 /* Try not to expose the internal sizetype type's name. */
12519 if (TYPE_NAME (qualified_type)
12520 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12521 {
12522 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12523
12524 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12525 && (TYPE_PRECISION (t)
12526 == TYPE_PRECISION (qualified_type))
12527 && (TYPE_UNSIGNED (t)
12528 == TYPE_UNSIGNED (qualified_type)));
12529 qualified_type = t;
12530 }
12531 else if (qualified_type == sizetype
12532 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12533 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12534 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12535 qualified_type = size_type_node;
12536 }
12537
12538 /* If we do, then we can just use its DIE, if it exists. */
12539 if (qualified_type)
12540 {
12541 mod_type_die = lookup_type_die (qualified_type);
12542
12543 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12544 dealt with specially: the DIE with the attribute, if it exists, is
12545 placed immediately after the regular DIE for the same base type. */
12546 if (mod_type_die
12547 && (!reverse_base_type
12548 || ((mod_type_die = mod_type_die->die_sib) != NULL
12549 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12550 return mod_type_die;
12551 }
12552
12553 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12554
12555 /* Handle C typedef types. */
12556 if (name
12557 && TREE_CODE (name) == TYPE_DECL
12558 && DECL_ORIGINAL_TYPE (name)
12559 && !DECL_ARTIFICIAL (name))
12560 {
12561 tree dtype = TREE_TYPE (name);
12562
12563 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12564 if (qualified_type == dtype && !reverse_base_type)
12565 {
12566 tree origin = decl_ultimate_origin (name);
12567
12568 /* Typedef variants that have an abstract origin don't get their own
12569 type DIE (see gen_typedef_die), so fall back on the ultimate
12570 abstract origin instead. */
12571 if (origin != NULL && origin != name)
12572 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12573 context_die);
12574
12575 /* For a named type, use the typedef. */
12576 gen_type_die (qualified_type, context_die);
12577 return lookup_type_die (qualified_type);
12578 }
12579 else
12580 {
12581 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12582 dquals &= cv_qual_mask;
12583 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12584 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12585 /* cv-unqualified version of named type. Just use
12586 the unnamed type to which it refers. */
12587 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12588 reverse, context_die);
12589 /* Else cv-qualified version of named type; fall through. */
12590 }
12591 }
12592
12593 mod_scope = scope_die_for (type, context_die);
12594
12595 if (cv_quals)
12596 {
12597 int sub_quals = 0, first_quals = 0;
12598 unsigned i;
12599 dw_die_ref first = NULL, last = NULL;
12600
12601 /* Determine a lesser qualified type that most closely matches
12602 this one. Then generate DW_TAG_* entries for the remaining
12603 qualifiers. */
12604 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12605 cv_qual_mask);
12606 if (sub_quals && use_debug_types)
12607 {
12608 bool needed = false;
12609 /* If emitting type units, make sure the order of qualifiers
12610 is canonical. Thus, start from unqualified type if
12611 an earlier qualifier is missing in sub_quals, but some later
12612 one is present there. */
12613 for (i = 0; i < dwarf_qual_info_size; i++)
12614 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12615 needed = true;
12616 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12617 {
12618 sub_quals = 0;
12619 break;
12620 }
12621 }
12622 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12623 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12624 {
12625 /* As not all intermediate qualified DIEs have corresponding
12626 tree types, ensure that qualified DIEs in the same scope
12627 as their DW_AT_type are emitted after their DW_AT_type,
12628 only with other qualified DIEs for the same type possibly
12629 in between them. Determine the range of such qualified
12630 DIEs now (first being the base type, last being corresponding
12631 last qualified DIE for it). */
12632 unsigned int count = 0;
12633 first = qualified_die_p (mod_type_die, &first_quals,
12634 dwarf_qual_info_size);
12635 if (first == NULL)
12636 first = mod_type_die;
12637 gcc_assert ((first_quals & ~sub_quals) == 0);
12638 for (count = 0, last = first;
12639 count < (1U << dwarf_qual_info_size);
12640 count++, last = last->die_sib)
12641 {
12642 int quals = 0;
12643 if (last == mod_scope->die_child)
12644 break;
12645 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12646 != first)
12647 break;
12648 }
12649 }
12650
12651 for (i = 0; i < dwarf_qual_info_size; i++)
12652 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12653 {
12654 dw_die_ref d;
12655 if (first && first != last)
12656 {
12657 for (d = first->die_sib; ; d = d->die_sib)
12658 {
12659 int quals = 0;
12660 qualified_die_p (d, &quals, dwarf_qual_info_size);
12661 if (quals == (first_quals | dwarf_qual_info[i].q))
12662 break;
12663 if (d == last)
12664 {
12665 d = NULL;
12666 break;
12667 }
12668 }
12669 if (d)
12670 {
12671 mod_type_die = d;
12672 continue;
12673 }
12674 }
12675 if (first)
12676 {
12677 d = new_die_raw (dwarf_qual_info[i].t);
12678 add_child_die_after (mod_scope, d, last);
12679 last = d;
12680 }
12681 else
12682 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12683 if (mod_type_die)
12684 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12685 mod_type_die = d;
12686 first_quals |= dwarf_qual_info[i].q;
12687 }
12688 }
12689 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12690 {
12691 dwarf_tag tag = DW_TAG_pointer_type;
12692 if (code == REFERENCE_TYPE)
12693 {
12694 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12695 tag = DW_TAG_rvalue_reference_type;
12696 else
12697 tag = DW_TAG_reference_type;
12698 }
12699 mod_type_die = new_die (tag, mod_scope, type);
12700
12701 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12702 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12703 add_alignment_attribute (mod_type_die, type);
12704 item_type = TREE_TYPE (type);
12705
12706 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12707 if (!ADDR_SPACE_GENERIC_P (as))
12708 {
12709 int action = targetm.addr_space.debug (as);
12710 if (action >= 0)
12711 {
12712 /* Positive values indicate an address_class. */
12713 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12714 }
12715 else
12716 {
12717 /* Negative values indicate an (inverted) segment base reg. */
12718 dw_loc_descr_ref d
12719 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12720 add_AT_loc (mod_type_die, DW_AT_segment, d);
12721 }
12722 }
12723 }
12724 else if (code == INTEGER_TYPE
12725 && TREE_TYPE (type) != NULL_TREE
12726 && subrange_type_for_debug_p (type, &low, &high))
12727 {
12728 tree bias = NULL_TREE;
12729 if (lang_hooks.types.get_type_bias)
12730 bias = lang_hooks.types.get_type_bias (type);
12731 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12732 item_type = TREE_TYPE (type);
12733 }
12734 else if (is_base_type (type))
12735 {
12736 mod_type_die = base_type_die (type, reverse);
12737
12738 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12739 if (reverse_base_type)
12740 {
12741 dw_die_ref after_die
12742 = modified_type_die (type, cv_quals, false, context_die);
12743 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12744 }
12745 else
12746 add_child_die (comp_unit_die (), mod_type_die);
12747
12748 add_pubtype (type, mod_type_die);
12749 }
12750 else
12751 {
12752 gen_type_die (type, context_die);
12753
12754 /* We have to get the type_main_variant here (and pass that to the
12755 `lookup_type_die' routine) because the ..._TYPE node we have
12756 might simply be a *copy* of some original type node (where the
12757 copy was created to help us keep track of typedef names) and
12758 that copy might have a different TYPE_UID from the original
12759 ..._TYPE node. */
12760 if (TREE_CODE (type) == FUNCTION_TYPE
12761 || TREE_CODE (type) == METHOD_TYPE)
12762 {
12763 /* For function/method types, can't just use type_main_variant here,
12764 because that can have different ref-qualifiers for C++,
12765 but try to canonicalize. */
12766 tree main = TYPE_MAIN_VARIANT (type);
12767 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12768 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12769 && check_base_type (t, main)
12770 && check_lang_type (t, type))
12771 return lookup_type_die (t);
12772 return lookup_type_die (type);
12773 }
12774 else if (TREE_CODE (type) != VECTOR_TYPE
12775 && TREE_CODE (type) != ARRAY_TYPE)
12776 return lookup_type_die (type_main_variant (type));
12777 else
12778 /* Vectors have the debugging information in the type,
12779 not the main variant. */
12780 return lookup_type_die (type);
12781 }
12782
12783 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12784 don't output a DW_TAG_typedef, since there isn't one in the
12785 user's program; just attach a DW_AT_name to the type.
12786 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12787 if the base type already has the same name. */
12788 if (name
12789 && ((TREE_CODE (name) != TYPE_DECL
12790 && (qualified_type == TYPE_MAIN_VARIANT (type)
12791 || (cv_quals == TYPE_UNQUALIFIED)))
12792 || (TREE_CODE (name) == TYPE_DECL
12793 && TREE_TYPE (name) == qualified_type
12794 && DECL_NAME (name))))
12795 {
12796 if (TREE_CODE (name) == TYPE_DECL)
12797 /* Could just call add_name_and_src_coords_attributes here,
12798 but since this is a builtin type it doesn't have any
12799 useful source coordinates anyway. */
12800 name = DECL_NAME (name);
12801 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12802 }
12803 /* This probably indicates a bug. */
12804 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12805 {
12806 name = TYPE_IDENTIFIER (type);
12807 add_name_attribute (mod_type_die,
12808 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12809 }
12810
12811 if (qualified_type && !reverse_base_type)
12812 equate_type_number_to_die (qualified_type, mod_type_die);
12813
12814 if (item_type)
12815 /* We must do this after the equate_type_number_to_die call, in case
12816 this is a recursive type. This ensures that the modified_type_die
12817 recursion will terminate even if the type is recursive. Recursive
12818 types are possible in Ada. */
12819 sub_die = modified_type_die (item_type,
12820 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12821 reverse,
12822 context_die);
12823
12824 if (sub_die != NULL)
12825 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12826
12827 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12828 if (TYPE_ARTIFICIAL (type))
12829 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12830
12831 return mod_type_die;
12832 }
12833
12834 /* Generate DIEs for the generic parameters of T.
12835 T must be either a generic type or a generic function.
12836 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12837
12838 static void
12839 gen_generic_params_dies (tree t)
12840 {
12841 tree parms, args;
12842 int parms_num, i;
12843 dw_die_ref die = NULL;
12844 int non_default;
12845
12846 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12847 return;
12848
12849 if (TYPE_P (t))
12850 die = lookup_type_die (t);
12851 else if (DECL_P (t))
12852 die = lookup_decl_die (t);
12853
12854 gcc_assert (die);
12855
12856 parms = lang_hooks.get_innermost_generic_parms (t);
12857 if (!parms)
12858 /* T has no generic parameter. It means T is neither a generic type
12859 or function. End of story. */
12860 return;
12861
12862 parms_num = TREE_VEC_LENGTH (parms);
12863 args = lang_hooks.get_innermost_generic_args (t);
12864 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12865 non_default = int_cst_value (TREE_CHAIN (args));
12866 else
12867 non_default = TREE_VEC_LENGTH (args);
12868 for (i = 0; i < parms_num; i++)
12869 {
12870 tree parm, arg, arg_pack_elems;
12871 dw_die_ref parm_die;
12872
12873 parm = TREE_VEC_ELT (parms, i);
12874 arg = TREE_VEC_ELT (args, i);
12875 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12876 gcc_assert (parm && TREE_VALUE (parm) && arg);
12877
12878 if (parm && TREE_VALUE (parm) && arg)
12879 {
12880 /* If PARM represents a template parameter pack,
12881 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12882 by DW_TAG_template_*_parameter DIEs for the argument
12883 pack elements of ARG. Note that ARG would then be
12884 an argument pack. */
12885 if (arg_pack_elems)
12886 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12887 arg_pack_elems,
12888 die);
12889 else
12890 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12891 true /* emit name */, die);
12892 if (i >= non_default)
12893 add_AT_flag (parm_die, DW_AT_default_value, 1);
12894 }
12895 }
12896 }
12897
12898 /* Create and return a DIE for PARM which should be
12899 the representation of a generic type parameter.
12900 For instance, in the C++ front end, PARM would be a template parameter.
12901 ARG is the argument to PARM.
12902 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12903 name of the PARM.
12904 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12905 as a child node. */
12906
12907 static dw_die_ref
12908 generic_parameter_die (tree parm, tree arg,
12909 bool emit_name_p,
12910 dw_die_ref parent_die)
12911 {
12912 dw_die_ref tmpl_die = NULL;
12913 const char *name = NULL;
12914
12915 if (!parm || !DECL_NAME (parm) || !arg)
12916 return NULL;
12917
12918 /* We support non-type generic parameters and arguments,
12919 type generic parameters and arguments, as well as
12920 generic generic parameters (a.k.a. template template parameters in C++)
12921 and arguments. */
12922 if (TREE_CODE (parm) == PARM_DECL)
12923 /* PARM is a nontype generic parameter */
12924 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12925 else if (TREE_CODE (parm) == TYPE_DECL)
12926 /* PARM is a type generic parameter. */
12927 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12928 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12929 /* PARM is a generic generic parameter.
12930 Its DIE is a GNU extension. It shall have a
12931 DW_AT_name attribute to represent the name of the template template
12932 parameter, and a DW_AT_GNU_template_name attribute to represent the
12933 name of the template template argument. */
12934 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12935 parent_die, parm);
12936 else
12937 gcc_unreachable ();
12938
12939 if (tmpl_die)
12940 {
12941 tree tmpl_type;
12942
12943 /* If PARM is a generic parameter pack, it means we are
12944 emitting debug info for a template argument pack element.
12945 In other terms, ARG is a template argument pack element.
12946 In that case, we don't emit any DW_AT_name attribute for
12947 the die. */
12948 if (emit_name_p)
12949 {
12950 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12951 gcc_assert (name);
12952 add_AT_string (tmpl_die, DW_AT_name, name);
12953 }
12954
12955 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12956 {
12957 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12958 TMPL_DIE should have a child DW_AT_type attribute that is set
12959 to the type of the argument to PARM, which is ARG.
12960 If PARM is a type generic parameter, TMPL_DIE should have a
12961 child DW_AT_type that is set to ARG. */
12962 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12963 add_type_attribute (tmpl_die, tmpl_type,
12964 (TREE_THIS_VOLATILE (tmpl_type)
12965 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12966 false, parent_die);
12967 }
12968 else
12969 {
12970 /* So TMPL_DIE is a DIE representing a
12971 a generic generic template parameter, a.k.a template template
12972 parameter in C++ and arg is a template. */
12973
12974 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12975 to the name of the argument. */
12976 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12977 if (name)
12978 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12979 }
12980
12981 if (TREE_CODE (parm) == PARM_DECL)
12982 /* So PARM is a non-type generic parameter.
12983 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12984 attribute of TMPL_DIE which value represents the value
12985 of ARG.
12986 We must be careful here:
12987 The value of ARG might reference some function decls.
12988 We might currently be emitting debug info for a generic
12989 type and types are emitted before function decls, we don't
12990 know if the function decls referenced by ARG will actually be
12991 emitted after cgraph computations.
12992 So must defer the generation of the DW_AT_const_value to
12993 after cgraph is ready. */
12994 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12995 }
12996
12997 return tmpl_die;
12998 }
12999
13000 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13001 PARM_PACK must be a template parameter pack. The returned DIE
13002 will be child DIE of PARENT_DIE. */
13003
13004 static dw_die_ref
13005 template_parameter_pack_die (tree parm_pack,
13006 tree parm_pack_args,
13007 dw_die_ref parent_die)
13008 {
13009 dw_die_ref die;
13010 int j;
13011
13012 gcc_assert (parent_die && parm_pack);
13013
13014 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13015 add_name_and_src_coords_attributes (die, parm_pack);
13016 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13017 generic_parameter_die (parm_pack,
13018 TREE_VEC_ELT (parm_pack_args, j),
13019 false /* Don't emit DW_AT_name */,
13020 die);
13021 return die;
13022 }
13023
13024 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13025 an enumerated type. */
13026
13027 static inline int
13028 type_is_enum (const_tree type)
13029 {
13030 return TREE_CODE (type) == ENUMERAL_TYPE;
13031 }
13032
13033 /* Return the DBX register number described by a given RTL node. */
13034
13035 static unsigned int
13036 dbx_reg_number (const_rtx rtl)
13037 {
13038 unsigned regno = REGNO (rtl);
13039
13040 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13041
13042 #ifdef LEAF_REG_REMAP
13043 if (crtl->uses_only_leaf_regs)
13044 {
13045 int leaf_reg = LEAF_REG_REMAP (regno);
13046 if (leaf_reg != -1)
13047 regno = (unsigned) leaf_reg;
13048 }
13049 #endif
13050
13051 regno = DBX_REGISTER_NUMBER (regno);
13052 gcc_assert (regno != INVALID_REGNUM);
13053 return regno;
13054 }
13055
13056 /* Optionally add a DW_OP_piece term to a location description expression.
13057 DW_OP_piece is only added if the location description expression already
13058 doesn't end with DW_OP_piece. */
13059
13060 static void
13061 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13062 {
13063 dw_loc_descr_ref loc;
13064
13065 if (*list_head != NULL)
13066 {
13067 /* Find the end of the chain. */
13068 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13069 ;
13070
13071 if (loc->dw_loc_opc != DW_OP_piece)
13072 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13073 }
13074 }
13075
13076 /* Return a location descriptor that designates a machine register or
13077 zero if there is none. */
13078
13079 static dw_loc_descr_ref
13080 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13081 {
13082 rtx regs;
13083
13084 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13085 return 0;
13086
13087 /* We only use "frame base" when we're sure we're talking about the
13088 post-prologue local stack frame. We do this by *not* running
13089 register elimination until this point, and recognizing the special
13090 argument pointer and soft frame pointer rtx's.
13091 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13092 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13093 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13094 {
13095 dw_loc_descr_ref result = NULL;
13096
13097 if (dwarf_version >= 4 || !dwarf_strict)
13098 {
13099 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13100 initialized);
13101 if (result)
13102 add_loc_descr (&result,
13103 new_loc_descr (DW_OP_stack_value, 0, 0));
13104 }
13105 return result;
13106 }
13107
13108 regs = targetm.dwarf_register_span (rtl);
13109
13110 if (REG_NREGS (rtl) > 1 || regs)
13111 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13112 else
13113 {
13114 unsigned int dbx_regnum = dbx_reg_number (rtl);
13115 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13116 return 0;
13117 return one_reg_loc_descriptor (dbx_regnum, initialized);
13118 }
13119 }
13120
13121 /* Return a location descriptor that designates a machine register for
13122 a given hard register number. */
13123
13124 static dw_loc_descr_ref
13125 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13126 {
13127 dw_loc_descr_ref reg_loc_descr;
13128
13129 if (regno <= 31)
13130 reg_loc_descr
13131 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13132 else
13133 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13134
13135 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13136 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13137
13138 return reg_loc_descr;
13139 }
13140
13141 /* Given an RTL of a register, return a location descriptor that
13142 designates a value that spans more than one register. */
13143
13144 static dw_loc_descr_ref
13145 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13146 enum var_init_status initialized)
13147 {
13148 int size, i;
13149 dw_loc_descr_ref loc_result = NULL;
13150
13151 /* Simple, contiguous registers. */
13152 if (regs == NULL_RTX)
13153 {
13154 unsigned reg = REGNO (rtl);
13155 int nregs;
13156
13157 #ifdef LEAF_REG_REMAP
13158 if (crtl->uses_only_leaf_regs)
13159 {
13160 int leaf_reg = LEAF_REG_REMAP (reg);
13161 if (leaf_reg != -1)
13162 reg = (unsigned) leaf_reg;
13163 }
13164 #endif
13165
13166 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13167 nregs = REG_NREGS (rtl);
13168
13169 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13170
13171 loc_result = NULL;
13172 while (nregs--)
13173 {
13174 dw_loc_descr_ref t;
13175
13176 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13177 VAR_INIT_STATUS_INITIALIZED);
13178 add_loc_descr (&loc_result, t);
13179 add_loc_descr_op_piece (&loc_result, size);
13180 ++reg;
13181 }
13182 return loc_result;
13183 }
13184
13185 /* Now onto stupid register sets in non contiguous locations. */
13186
13187 gcc_assert (GET_CODE (regs) == PARALLEL);
13188
13189 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13190 loc_result = NULL;
13191
13192 for (i = 0; i < XVECLEN (regs, 0); ++i)
13193 {
13194 dw_loc_descr_ref t;
13195
13196 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13197 VAR_INIT_STATUS_INITIALIZED);
13198 add_loc_descr (&loc_result, t);
13199 add_loc_descr_op_piece (&loc_result, size);
13200 }
13201
13202 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13203 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13204 return loc_result;
13205 }
13206
13207 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13208
13209 /* Return a location descriptor that designates a constant i,
13210 as a compound operation from constant (i >> shift), constant shift
13211 and DW_OP_shl. */
13212
13213 static dw_loc_descr_ref
13214 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13215 {
13216 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13217 add_loc_descr (&ret, int_loc_descriptor (shift));
13218 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13219 return ret;
13220 }
13221
13222 /* Return a location descriptor that designates a constant. */
13223
13224 static dw_loc_descr_ref
13225 int_loc_descriptor (HOST_WIDE_INT i)
13226 {
13227 enum dwarf_location_atom op;
13228
13229 /* Pick the smallest representation of a constant, rather than just
13230 defaulting to the LEB encoding. */
13231 if (i >= 0)
13232 {
13233 int clz = clz_hwi (i);
13234 int ctz = ctz_hwi (i);
13235 if (i <= 31)
13236 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13237 else if (i <= 0xff)
13238 op = DW_OP_const1u;
13239 else if (i <= 0xffff)
13240 op = DW_OP_const2u;
13241 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13242 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13243 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13244 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13245 while DW_OP_const4u is 5 bytes. */
13246 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13247 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13248 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13249 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13250 while DW_OP_const4u is 5 bytes. */
13251 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13252
13253 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13254 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13255 <= 4)
13256 {
13257 /* As i >= 2**31, the double cast above will yield a negative number.
13258 Since wrapping is defined in DWARF expressions we can output big
13259 positive integers as small negative ones, regardless of the size
13260 of host wide ints.
13261
13262 Here, since the evaluator will handle 32-bit values and since i >=
13263 2**31, we know it's going to be interpreted as a negative literal:
13264 store it this way if we can do better than 5 bytes this way. */
13265 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13266 }
13267 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13268 op = DW_OP_const4u;
13269
13270 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13271 least 6 bytes: see if we can do better before falling back to it. */
13272 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13273 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13274 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13275 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13276 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13277 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13278 >= HOST_BITS_PER_WIDE_INT)
13279 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13280 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13281 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13282 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13283 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13284 && size_of_uleb128 (i) > 6)
13285 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13286 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13287 else
13288 op = DW_OP_constu;
13289 }
13290 else
13291 {
13292 if (i >= -0x80)
13293 op = DW_OP_const1s;
13294 else if (i >= -0x8000)
13295 op = DW_OP_const2s;
13296 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13297 {
13298 if (size_of_int_loc_descriptor (i) < 5)
13299 {
13300 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13301 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13302 return ret;
13303 }
13304 op = DW_OP_const4s;
13305 }
13306 else
13307 {
13308 if (size_of_int_loc_descriptor (i)
13309 < (unsigned long) 1 + size_of_sleb128 (i))
13310 {
13311 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13312 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13313 return ret;
13314 }
13315 op = DW_OP_consts;
13316 }
13317 }
13318
13319 return new_loc_descr (op, i, 0);
13320 }
13321
13322 /* Likewise, for unsigned constants. */
13323
13324 static dw_loc_descr_ref
13325 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13326 {
13327 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13328 const unsigned HOST_WIDE_INT max_uint
13329 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13330
13331 /* If possible, use the clever signed constants handling. */
13332 if (i <= max_int)
13333 return int_loc_descriptor ((HOST_WIDE_INT) i);
13334
13335 /* Here, we are left with positive numbers that cannot be represented as
13336 HOST_WIDE_INT, i.e.:
13337 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13338
13339 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13340 whereas may be better to output a negative integer: thanks to integer
13341 wrapping, we know that:
13342 x = x - 2 ** DWARF2_ADDR_SIZE
13343 = x - 2 * (max (HOST_WIDE_INT) + 1)
13344 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13345 small negative integers. Let's try that in cases it will clearly improve
13346 the encoding: there is no gain turning DW_OP_const4u into
13347 DW_OP_const4s. */
13348 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13349 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13350 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13351 {
13352 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13353
13354 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13355 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13356 const HOST_WIDE_INT second_shift
13357 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13358
13359 /* So we finally have:
13360 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13361 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13362 return int_loc_descriptor (second_shift);
13363 }
13364
13365 /* Last chance: fallback to a simple constant operation. */
13366 return new_loc_descr
13367 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13368 ? DW_OP_const4u
13369 : DW_OP_const8u,
13370 i, 0);
13371 }
13372
13373 /* Generate and return a location description that computes the unsigned
13374 comparison of the two stack top entries (a OP b where b is the top-most
13375 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13376 LE_EXPR, GT_EXPR or GE_EXPR. */
13377
13378 static dw_loc_descr_ref
13379 uint_comparison_loc_list (enum tree_code kind)
13380 {
13381 enum dwarf_location_atom op, flip_op;
13382 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13383
13384 switch (kind)
13385 {
13386 case LT_EXPR:
13387 op = DW_OP_lt;
13388 break;
13389 case LE_EXPR:
13390 op = DW_OP_le;
13391 break;
13392 case GT_EXPR:
13393 op = DW_OP_gt;
13394 break;
13395 case GE_EXPR:
13396 op = DW_OP_ge;
13397 break;
13398 default:
13399 gcc_unreachable ();
13400 }
13401
13402 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13403 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13404
13405 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13406 possible to perform unsigned comparisons: we just have to distinguish
13407 three cases:
13408
13409 1. when a and b have the same sign (as signed integers); then we should
13410 return: a OP(signed) b;
13411
13412 2. when a is a negative signed integer while b is a positive one, then a
13413 is a greater unsigned integer than b; likewise when a and b's roles
13414 are flipped.
13415
13416 So first, compare the sign of the two operands. */
13417 ret = new_loc_descr (DW_OP_over, 0, 0);
13418 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13419 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13420 /* If they have different signs (i.e. they have different sign bits), then
13421 the stack top value has now the sign bit set and thus it's smaller than
13422 zero. */
13423 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13424 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13425 add_loc_descr (&ret, bra_node);
13426
13427 /* We are in case 1. At this point, we know both operands have the same
13428 sign, to it's safe to use the built-in signed comparison. */
13429 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13430 add_loc_descr (&ret, jmp_node);
13431
13432 /* We are in case 2. Here, we know both operands do not have the same sign,
13433 so we have to flip the signed comparison. */
13434 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13435 tmp = new_loc_descr (flip_op, 0, 0);
13436 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13437 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13438 add_loc_descr (&ret, tmp);
13439
13440 /* This dummy operation is necessary to make the two branches join. */
13441 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13442 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13443 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13444 add_loc_descr (&ret, tmp);
13445
13446 return ret;
13447 }
13448
13449 /* Likewise, but takes the location description lists (might be destructive on
13450 them). Return NULL if either is NULL or if concatenation fails. */
13451
13452 static dw_loc_list_ref
13453 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13454 enum tree_code kind)
13455 {
13456 if (left == NULL || right == NULL)
13457 return NULL;
13458
13459 add_loc_list (&left, right);
13460 if (left == NULL)
13461 return NULL;
13462
13463 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13464 return left;
13465 }
13466
13467 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13468 without actually allocating it. */
13469
13470 static unsigned long
13471 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13472 {
13473 return size_of_int_loc_descriptor (i >> shift)
13474 + size_of_int_loc_descriptor (shift)
13475 + 1;
13476 }
13477
13478 /* Return size_of_locs (int_loc_descriptor (i)) without
13479 actually allocating it. */
13480
13481 static unsigned long
13482 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13483 {
13484 unsigned long s;
13485
13486 if (i >= 0)
13487 {
13488 int clz, ctz;
13489 if (i <= 31)
13490 return 1;
13491 else if (i <= 0xff)
13492 return 2;
13493 else if (i <= 0xffff)
13494 return 3;
13495 clz = clz_hwi (i);
13496 ctz = ctz_hwi (i);
13497 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13498 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13499 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13500 - clz - 5);
13501 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13502 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13503 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13504 - clz - 8);
13505 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13506 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13507 <= 4)
13508 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13509 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13510 return 5;
13511 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13512 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13513 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13514 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13515 - clz - 8);
13516 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13517 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13518 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13519 - clz - 16);
13520 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13521 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13522 && s > 6)
13523 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13524 - clz - 32);
13525 else
13526 return 1 + s;
13527 }
13528 else
13529 {
13530 if (i >= -0x80)
13531 return 2;
13532 else if (i >= -0x8000)
13533 return 3;
13534 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13535 {
13536 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13537 {
13538 s = size_of_int_loc_descriptor (-i) + 1;
13539 if (s < 5)
13540 return s;
13541 }
13542 return 5;
13543 }
13544 else
13545 {
13546 unsigned long r = 1 + size_of_sleb128 (i);
13547 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13548 {
13549 s = size_of_int_loc_descriptor (-i) + 1;
13550 if (s < r)
13551 return s;
13552 }
13553 return r;
13554 }
13555 }
13556 }
13557
13558 /* Return loc description representing "address" of integer value.
13559 This can appear only as toplevel expression. */
13560
13561 static dw_loc_descr_ref
13562 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13563 {
13564 int litsize;
13565 dw_loc_descr_ref loc_result = NULL;
13566
13567 if (!(dwarf_version >= 4 || !dwarf_strict))
13568 return NULL;
13569
13570 litsize = size_of_int_loc_descriptor (i);
13571 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13572 is more compact. For DW_OP_stack_value we need:
13573 litsize + 1 (DW_OP_stack_value)
13574 and for DW_OP_implicit_value:
13575 1 (DW_OP_implicit_value) + 1 (length) + size. */
13576 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13577 {
13578 loc_result = int_loc_descriptor (i);
13579 add_loc_descr (&loc_result,
13580 new_loc_descr (DW_OP_stack_value, 0, 0));
13581 return loc_result;
13582 }
13583
13584 loc_result = new_loc_descr (DW_OP_implicit_value,
13585 size, 0);
13586 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13587 loc_result->dw_loc_oprnd2.v.val_int = i;
13588 return loc_result;
13589 }
13590
13591 /* Return a location descriptor that designates a base+offset location. */
13592
13593 static dw_loc_descr_ref
13594 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13595 enum var_init_status initialized)
13596 {
13597 unsigned int regno;
13598 dw_loc_descr_ref result;
13599 dw_fde_ref fde = cfun->fde;
13600
13601 /* We only use "frame base" when we're sure we're talking about the
13602 post-prologue local stack frame. We do this by *not* running
13603 register elimination until this point, and recognizing the special
13604 argument pointer and soft frame pointer rtx's. */
13605 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13606 {
13607 rtx elim = (ira_use_lra_p
13608 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13609 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13610
13611 if (elim != reg)
13612 {
13613 if (GET_CODE (elim) == PLUS)
13614 {
13615 offset += INTVAL (XEXP (elim, 1));
13616 elim = XEXP (elim, 0);
13617 }
13618 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13619 && (elim == hard_frame_pointer_rtx
13620 || elim == stack_pointer_rtx))
13621 || elim == (frame_pointer_needed
13622 ? hard_frame_pointer_rtx
13623 : stack_pointer_rtx));
13624
13625 /* If drap register is used to align stack, use frame
13626 pointer + offset to access stack variables. If stack
13627 is aligned without drap, use stack pointer + offset to
13628 access stack variables. */
13629 if (crtl->stack_realign_tried
13630 && reg == frame_pointer_rtx)
13631 {
13632 int base_reg
13633 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13634 ? HARD_FRAME_POINTER_REGNUM
13635 : REGNO (elim));
13636 return new_reg_loc_descr (base_reg, offset);
13637 }
13638
13639 gcc_assert (frame_pointer_fb_offset_valid);
13640 offset += frame_pointer_fb_offset;
13641 return new_loc_descr (DW_OP_fbreg, offset, 0);
13642 }
13643 }
13644
13645 regno = REGNO (reg);
13646 #ifdef LEAF_REG_REMAP
13647 if (crtl->uses_only_leaf_regs)
13648 {
13649 int leaf_reg = LEAF_REG_REMAP (regno);
13650 if (leaf_reg != -1)
13651 regno = (unsigned) leaf_reg;
13652 }
13653 #endif
13654 regno = DWARF_FRAME_REGNUM (regno);
13655
13656 if (!optimize && fde
13657 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13658 {
13659 /* Use cfa+offset to represent the location of arguments passed
13660 on the stack when drap is used to align stack.
13661 Only do this when not optimizing, for optimized code var-tracking
13662 is supposed to track where the arguments live and the register
13663 used as vdrap or drap in some spot might be used for something
13664 else in other part of the routine. */
13665 return new_loc_descr (DW_OP_fbreg, offset, 0);
13666 }
13667
13668 if (regno <= 31)
13669 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13670 offset, 0);
13671 else
13672 result = new_loc_descr (DW_OP_bregx, regno, offset);
13673
13674 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13675 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13676
13677 return result;
13678 }
13679
13680 /* Return true if this RTL expression describes a base+offset calculation. */
13681
13682 static inline int
13683 is_based_loc (const_rtx rtl)
13684 {
13685 return (GET_CODE (rtl) == PLUS
13686 && ((REG_P (XEXP (rtl, 0))
13687 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13688 && CONST_INT_P (XEXP (rtl, 1)))));
13689 }
13690
13691 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13692 failed. */
13693
13694 static dw_loc_descr_ref
13695 tls_mem_loc_descriptor (rtx mem)
13696 {
13697 tree base;
13698 dw_loc_descr_ref loc_result;
13699
13700 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13701 return NULL;
13702
13703 base = get_base_address (MEM_EXPR (mem));
13704 if (base == NULL
13705 || !VAR_P (base)
13706 || !DECL_THREAD_LOCAL_P (base))
13707 return NULL;
13708
13709 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13710 if (loc_result == NULL)
13711 return NULL;
13712
13713 if (MEM_OFFSET (mem))
13714 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13715
13716 return loc_result;
13717 }
13718
13719 /* Output debug info about reason why we failed to expand expression as dwarf
13720 expression. */
13721
13722 static void
13723 expansion_failed (tree expr, rtx rtl, char const *reason)
13724 {
13725 if (dump_file && (dump_flags & TDF_DETAILS))
13726 {
13727 fprintf (dump_file, "Failed to expand as dwarf: ");
13728 if (expr)
13729 print_generic_expr (dump_file, expr, dump_flags);
13730 if (rtl)
13731 {
13732 fprintf (dump_file, "\n");
13733 print_rtl (dump_file, rtl);
13734 }
13735 fprintf (dump_file, "\nReason: %s\n", reason);
13736 }
13737 }
13738
13739 /* Helper function for const_ok_for_output. */
13740
13741 static bool
13742 const_ok_for_output_1 (rtx rtl)
13743 {
13744 if (targetm.const_not_ok_for_debug_p (rtl))
13745 {
13746 if (GET_CODE (rtl) != UNSPEC)
13747 {
13748 expansion_failed (NULL_TREE, rtl,
13749 "Expression rejected for debug by the backend.\n");
13750 return false;
13751 }
13752
13753 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13754 the target hook doesn't explicitly allow it in debug info, assume
13755 we can't express it in the debug info. */
13756 /* Don't complain about TLS UNSPECs, those are just too hard to
13757 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13758 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13759 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13760 if (flag_checking
13761 && (XVECLEN (rtl, 0) == 0
13762 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13763 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13764 inform (current_function_decl
13765 ? DECL_SOURCE_LOCATION (current_function_decl)
13766 : UNKNOWN_LOCATION,
13767 #if NUM_UNSPEC_VALUES > 0
13768 "non-delegitimized UNSPEC %s (%d) found in variable location",
13769 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13770 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13771 XINT (rtl, 1));
13772 #else
13773 "non-delegitimized UNSPEC %d found in variable location",
13774 XINT (rtl, 1));
13775 #endif
13776 expansion_failed (NULL_TREE, rtl,
13777 "UNSPEC hasn't been delegitimized.\n");
13778 return false;
13779 }
13780
13781 /* FIXME: Refer to PR60655. It is possible for simplification
13782 of rtl expressions in var tracking to produce such expressions.
13783 We should really identify / validate expressions
13784 enclosed in CONST that can be handled by assemblers on various
13785 targets and only handle legitimate cases here. */
13786 switch (GET_CODE (rtl))
13787 {
13788 case SYMBOL_REF:
13789 break;
13790 case NOT:
13791 case NEG:
13792 return false;
13793 default:
13794 return true;
13795 }
13796
13797 if (CONSTANT_POOL_ADDRESS_P (rtl))
13798 {
13799 bool marked;
13800 get_pool_constant_mark (rtl, &marked);
13801 /* If all references to this pool constant were optimized away,
13802 it was not output and thus we can't represent it. */
13803 if (!marked)
13804 {
13805 expansion_failed (NULL_TREE, rtl,
13806 "Constant was removed from constant pool.\n");
13807 return false;
13808 }
13809 }
13810
13811 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13812 return false;
13813
13814 /* Avoid references to external symbols in debug info, on several targets
13815 the linker might even refuse to link when linking a shared library,
13816 and in many other cases the relocations for .debug_info/.debug_loc are
13817 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13818 to be defined within the same shared library or executable are fine. */
13819 if (SYMBOL_REF_EXTERNAL_P (rtl))
13820 {
13821 tree decl = SYMBOL_REF_DECL (rtl);
13822
13823 if (decl == NULL || !targetm.binds_local_p (decl))
13824 {
13825 expansion_failed (NULL_TREE, rtl,
13826 "Symbol not defined in current TU.\n");
13827 return false;
13828 }
13829 }
13830
13831 return true;
13832 }
13833
13834 /* Return true if constant RTL can be emitted in DW_OP_addr or
13835 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13836 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13837
13838 static bool
13839 const_ok_for_output (rtx rtl)
13840 {
13841 if (GET_CODE (rtl) == SYMBOL_REF)
13842 return const_ok_for_output_1 (rtl);
13843
13844 if (GET_CODE (rtl) == CONST)
13845 {
13846 subrtx_var_iterator::array_type array;
13847 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13848 if (!const_ok_for_output_1 (*iter))
13849 return false;
13850 return true;
13851 }
13852
13853 return true;
13854 }
13855
13856 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13857 if possible, NULL otherwise. */
13858
13859 static dw_die_ref
13860 base_type_for_mode (machine_mode mode, bool unsignedp)
13861 {
13862 dw_die_ref type_die;
13863 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13864
13865 if (type == NULL)
13866 return NULL;
13867 switch (TREE_CODE (type))
13868 {
13869 case INTEGER_TYPE:
13870 case REAL_TYPE:
13871 break;
13872 default:
13873 return NULL;
13874 }
13875 type_die = lookup_type_die (type);
13876 if (!type_die)
13877 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13878 comp_unit_die ());
13879 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13880 return NULL;
13881 return type_die;
13882 }
13883
13884 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13885 type matching MODE, or, if MODE is narrower than or as wide as
13886 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13887 possible. */
13888
13889 static dw_loc_descr_ref
13890 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13891 {
13892 machine_mode outer_mode = mode;
13893 dw_die_ref type_die;
13894 dw_loc_descr_ref cvt;
13895
13896 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13897 {
13898 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13899 return op;
13900 }
13901 type_die = base_type_for_mode (outer_mode, 1);
13902 if (type_die == NULL)
13903 return NULL;
13904 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13905 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13906 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13907 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13908 add_loc_descr (&op, cvt);
13909 return op;
13910 }
13911
13912 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13913
13914 static dw_loc_descr_ref
13915 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13916 dw_loc_descr_ref op1)
13917 {
13918 dw_loc_descr_ref ret = op0;
13919 add_loc_descr (&ret, op1);
13920 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13921 if (STORE_FLAG_VALUE != 1)
13922 {
13923 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13924 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13925 }
13926 return ret;
13927 }
13928
13929 /* Subroutine of scompare_loc_descriptor for the case in which we're
13930 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13931 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13932
13933 static dw_loc_descr_ref
13934 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13935 scalar_int_mode op_mode,
13936 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13937 {
13938 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13939 dw_loc_descr_ref cvt;
13940
13941 if (type_die == NULL)
13942 return NULL;
13943 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13944 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13945 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13946 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13947 add_loc_descr (&op0, cvt);
13948 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13949 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13950 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13951 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13952 add_loc_descr (&op1, cvt);
13953 return compare_loc_descriptor (op, op0, op1);
13954 }
13955
13956 /* Subroutine of scompare_loc_descriptor for the case in which we're
13957 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13958 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13959
13960 static dw_loc_descr_ref
13961 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13962 scalar_int_mode op_mode,
13963 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13964 {
13965 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13966 /* For eq/ne, if the operands are known to be zero-extended,
13967 there is no need to do the fancy shifting up. */
13968 if (op == DW_OP_eq || op == DW_OP_ne)
13969 {
13970 dw_loc_descr_ref last0, last1;
13971 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13972 ;
13973 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13974 ;
13975 /* deref_size zero extends, and for constants we can check
13976 whether they are zero extended or not. */
13977 if (((last0->dw_loc_opc == DW_OP_deref_size
13978 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13979 || (CONST_INT_P (XEXP (rtl, 0))
13980 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13981 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13982 && ((last1->dw_loc_opc == DW_OP_deref_size
13983 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13984 || (CONST_INT_P (XEXP (rtl, 1))
13985 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13986 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13987 return compare_loc_descriptor (op, op0, op1);
13988
13989 /* EQ/NE comparison against constant in narrower type than
13990 DWARF2_ADDR_SIZE can be performed either as
13991 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13992 DW_OP_{eq,ne}
13993 or
13994 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13995 DW_OP_{eq,ne}. Pick whatever is shorter. */
13996 if (CONST_INT_P (XEXP (rtl, 1))
13997 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13998 && (size_of_int_loc_descriptor (shift) + 1
13999 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14000 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14001 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14002 & GET_MODE_MASK (op_mode))))
14003 {
14004 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14005 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14006 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14007 & GET_MODE_MASK (op_mode));
14008 return compare_loc_descriptor (op, op0, op1);
14009 }
14010 }
14011 add_loc_descr (&op0, int_loc_descriptor (shift));
14012 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14013 if (CONST_INT_P (XEXP (rtl, 1)))
14014 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14015 else
14016 {
14017 add_loc_descr (&op1, int_loc_descriptor (shift));
14018 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14019 }
14020 return compare_loc_descriptor (op, op0, op1);
14021 }
14022
14023 /* Return location descriptor for unsigned comparison OP RTL. */
14024
14025 static dw_loc_descr_ref
14026 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14027 machine_mode mem_mode)
14028 {
14029 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14030 dw_loc_descr_ref op0, op1;
14031
14032 if (op_mode == VOIDmode)
14033 op_mode = GET_MODE (XEXP (rtl, 1));
14034 if (op_mode == VOIDmode)
14035 return NULL;
14036
14037 scalar_int_mode int_op_mode;
14038 if (dwarf_strict
14039 && dwarf_version < 5
14040 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14041 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14042 return NULL;
14043
14044 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14045 VAR_INIT_STATUS_INITIALIZED);
14046 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14047 VAR_INIT_STATUS_INITIALIZED);
14048
14049 if (op0 == NULL || op1 == NULL)
14050 return NULL;
14051
14052 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14053 {
14054 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14055 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14056
14057 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14058 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14059 }
14060 return compare_loc_descriptor (op, op0, op1);
14061 }
14062
14063 /* Return location descriptor for unsigned comparison OP RTL. */
14064
14065 static dw_loc_descr_ref
14066 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14067 machine_mode mem_mode)
14068 {
14069 dw_loc_descr_ref op0, op1;
14070
14071 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14072 if (test_op_mode == VOIDmode)
14073 test_op_mode = GET_MODE (XEXP (rtl, 1));
14074
14075 scalar_int_mode op_mode;
14076 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14077 return NULL;
14078
14079 if (dwarf_strict
14080 && dwarf_version < 5
14081 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14082 return NULL;
14083
14084 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14085 VAR_INIT_STATUS_INITIALIZED);
14086 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14087 VAR_INIT_STATUS_INITIALIZED);
14088
14089 if (op0 == NULL || op1 == NULL)
14090 return NULL;
14091
14092 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14093 {
14094 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14095 dw_loc_descr_ref last0, last1;
14096 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14097 ;
14098 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14099 ;
14100 if (CONST_INT_P (XEXP (rtl, 0)))
14101 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14102 /* deref_size zero extends, so no need to mask it again. */
14103 else if (last0->dw_loc_opc != DW_OP_deref_size
14104 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14105 {
14106 add_loc_descr (&op0, int_loc_descriptor (mask));
14107 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14108 }
14109 if (CONST_INT_P (XEXP (rtl, 1)))
14110 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14111 /* deref_size zero extends, so no need to mask it again. */
14112 else if (last1->dw_loc_opc != DW_OP_deref_size
14113 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14114 {
14115 add_loc_descr (&op1, int_loc_descriptor (mask));
14116 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14117 }
14118 }
14119 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14120 {
14121 HOST_WIDE_INT bias = 1;
14122 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14123 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14124 if (CONST_INT_P (XEXP (rtl, 1)))
14125 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14126 + INTVAL (XEXP (rtl, 1)));
14127 else
14128 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14129 bias, 0));
14130 }
14131 return compare_loc_descriptor (op, op0, op1);
14132 }
14133
14134 /* Return location descriptor for {U,S}{MIN,MAX}. */
14135
14136 static dw_loc_descr_ref
14137 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14138 machine_mode mem_mode)
14139 {
14140 enum dwarf_location_atom op;
14141 dw_loc_descr_ref op0, op1, ret;
14142 dw_loc_descr_ref bra_node, drop_node;
14143
14144 scalar_int_mode int_mode;
14145 if (dwarf_strict
14146 && dwarf_version < 5
14147 && (!is_a <scalar_int_mode> (mode, &int_mode)
14148 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14149 return NULL;
14150
14151 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14152 VAR_INIT_STATUS_INITIALIZED);
14153 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14154 VAR_INIT_STATUS_INITIALIZED);
14155
14156 if (op0 == NULL || op1 == NULL)
14157 return NULL;
14158
14159 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14160 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14161 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14162 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14163 {
14164 /* Checked by the caller. */
14165 int_mode = as_a <scalar_int_mode> (mode);
14166 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14167 {
14168 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14169 add_loc_descr (&op0, int_loc_descriptor (mask));
14170 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14171 add_loc_descr (&op1, int_loc_descriptor (mask));
14172 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14173 }
14174 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14175 {
14176 HOST_WIDE_INT bias = 1;
14177 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14178 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14179 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14180 }
14181 }
14182 else if (is_a <scalar_int_mode> (mode, &int_mode)
14183 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14184 {
14185 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14186 add_loc_descr (&op0, int_loc_descriptor (shift));
14187 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14188 add_loc_descr (&op1, int_loc_descriptor (shift));
14189 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14190 }
14191 else if (is_a <scalar_int_mode> (mode, &int_mode)
14192 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14193 {
14194 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14195 dw_loc_descr_ref cvt;
14196 if (type_die == NULL)
14197 return NULL;
14198 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14199 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14200 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14201 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14202 add_loc_descr (&op0, cvt);
14203 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14204 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14205 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14206 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14207 add_loc_descr (&op1, cvt);
14208 }
14209
14210 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14211 op = DW_OP_lt;
14212 else
14213 op = DW_OP_gt;
14214 ret = op0;
14215 add_loc_descr (&ret, op1);
14216 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14217 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14218 add_loc_descr (&ret, bra_node);
14219 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14220 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14221 add_loc_descr (&ret, drop_node);
14222 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14223 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14224 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14225 && is_a <scalar_int_mode> (mode, &int_mode)
14226 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14227 ret = convert_descriptor_to_mode (int_mode, ret);
14228 return ret;
14229 }
14230
14231 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14232 but after converting arguments to type_die, afterwards
14233 convert back to unsigned. */
14234
14235 static dw_loc_descr_ref
14236 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14237 scalar_int_mode mode, machine_mode mem_mode)
14238 {
14239 dw_loc_descr_ref cvt, op0, op1;
14240
14241 if (type_die == NULL)
14242 return NULL;
14243 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14244 VAR_INIT_STATUS_INITIALIZED);
14245 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14246 VAR_INIT_STATUS_INITIALIZED);
14247 if (op0 == NULL || op1 == NULL)
14248 return NULL;
14249 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14250 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14251 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14252 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14253 add_loc_descr (&op0, cvt);
14254 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14255 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14256 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14257 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14258 add_loc_descr (&op1, cvt);
14259 add_loc_descr (&op0, op1);
14260 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14261 return convert_descriptor_to_mode (mode, op0);
14262 }
14263
14264 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14265 const0 is DW_OP_lit0 or corresponding typed constant,
14266 const1 is DW_OP_lit1 or corresponding typed constant
14267 and constMSB is constant with just the MSB bit set
14268 for the mode):
14269 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14270 L1: const0 DW_OP_swap
14271 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14272 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14273 L3: DW_OP_drop
14274 L4: DW_OP_nop
14275
14276 CTZ is similar:
14277 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14278 L1: const0 DW_OP_swap
14279 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14280 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14281 L3: DW_OP_drop
14282 L4: DW_OP_nop
14283
14284 FFS is similar:
14285 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14286 L1: const1 DW_OP_swap
14287 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14288 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14289 L3: DW_OP_drop
14290 L4: DW_OP_nop */
14291
14292 static dw_loc_descr_ref
14293 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14294 machine_mode mem_mode)
14295 {
14296 dw_loc_descr_ref op0, ret, tmp;
14297 HOST_WIDE_INT valv;
14298 dw_loc_descr_ref l1jump, l1label;
14299 dw_loc_descr_ref l2jump, l2label;
14300 dw_loc_descr_ref l3jump, l3label;
14301 dw_loc_descr_ref l4jump, l4label;
14302 rtx msb;
14303
14304 if (GET_MODE (XEXP (rtl, 0)) != mode)
14305 return NULL;
14306
14307 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14308 VAR_INIT_STATUS_INITIALIZED);
14309 if (op0 == NULL)
14310 return NULL;
14311 ret = op0;
14312 if (GET_CODE (rtl) == CLZ)
14313 {
14314 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14315 valv = GET_MODE_BITSIZE (mode);
14316 }
14317 else if (GET_CODE (rtl) == FFS)
14318 valv = 0;
14319 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14320 valv = GET_MODE_BITSIZE (mode);
14321 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14322 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14323 add_loc_descr (&ret, l1jump);
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14325 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14326 VAR_INIT_STATUS_INITIALIZED);
14327 if (tmp == NULL)
14328 return NULL;
14329 add_loc_descr (&ret, tmp);
14330 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14331 add_loc_descr (&ret, l4jump);
14332 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14333 ? const1_rtx : const0_rtx,
14334 mode, mem_mode,
14335 VAR_INIT_STATUS_INITIALIZED);
14336 if (l1label == NULL)
14337 return NULL;
14338 add_loc_descr (&ret, l1label);
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14340 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14341 add_loc_descr (&ret, l2label);
14342 if (GET_CODE (rtl) != CLZ)
14343 msb = const1_rtx;
14344 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14345 msb = GEN_INT (HOST_WIDE_INT_1U
14346 << (GET_MODE_BITSIZE (mode) - 1));
14347 else
14348 msb = immed_wide_int_const
14349 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14350 GET_MODE_PRECISION (mode)), mode);
14351 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14352 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14353 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14354 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14355 else
14356 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14357 VAR_INIT_STATUS_INITIALIZED);
14358 if (tmp == NULL)
14359 return NULL;
14360 add_loc_descr (&ret, tmp);
14361 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14362 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14363 add_loc_descr (&ret, l3jump);
14364 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14365 VAR_INIT_STATUS_INITIALIZED);
14366 if (tmp == NULL)
14367 return NULL;
14368 add_loc_descr (&ret, tmp);
14369 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14370 ? DW_OP_shl : DW_OP_shr, 0, 0));
14371 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14372 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14373 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14374 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14375 add_loc_descr (&ret, l2jump);
14376 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14377 add_loc_descr (&ret, l3label);
14378 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14379 add_loc_descr (&ret, l4label);
14380 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14381 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14382 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14383 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14384 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14385 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14386 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14387 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14388 return ret;
14389 }
14390
14391 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14392 const1 is DW_OP_lit1 or corresponding typed constant):
14393 const0 DW_OP_swap
14394 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14395 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14396 L2: DW_OP_drop
14397
14398 PARITY is similar:
14399 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14400 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14401 L2: DW_OP_drop */
14402
14403 static dw_loc_descr_ref
14404 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14405 machine_mode mem_mode)
14406 {
14407 dw_loc_descr_ref op0, ret, tmp;
14408 dw_loc_descr_ref l1jump, l1label;
14409 dw_loc_descr_ref l2jump, l2label;
14410
14411 if (GET_MODE (XEXP (rtl, 0)) != mode)
14412 return NULL;
14413
14414 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14415 VAR_INIT_STATUS_INITIALIZED);
14416 if (op0 == NULL)
14417 return NULL;
14418 ret = op0;
14419 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14420 VAR_INIT_STATUS_INITIALIZED);
14421 if (tmp == NULL)
14422 return NULL;
14423 add_loc_descr (&ret, tmp);
14424 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14425 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14426 add_loc_descr (&ret, l1label);
14427 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14428 add_loc_descr (&ret, l2jump);
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14430 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14431 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14432 VAR_INIT_STATUS_INITIALIZED);
14433 if (tmp == NULL)
14434 return NULL;
14435 add_loc_descr (&ret, tmp);
14436 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14437 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14438 ? DW_OP_plus : DW_OP_xor, 0, 0));
14439 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14440 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14441 VAR_INIT_STATUS_INITIALIZED);
14442 add_loc_descr (&ret, tmp);
14443 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14444 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14445 add_loc_descr (&ret, l1jump);
14446 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14447 add_loc_descr (&ret, l2label);
14448 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14449 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14450 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14451 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14452 return ret;
14453 }
14454
14455 /* BSWAP (constS is initial shift count, either 56 or 24):
14456 constS const0
14457 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14458 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14459 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14460 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14461 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14462
14463 static dw_loc_descr_ref
14464 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14465 machine_mode mem_mode)
14466 {
14467 dw_loc_descr_ref op0, ret, tmp;
14468 dw_loc_descr_ref l1jump, l1label;
14469 dw_loc_descr_ref l2jump, l2label;
14470
14471 if (BITS_PER_UNIT != 8
14472 || (GET_MODE_BITSIZE (mode) != 32
14473 && GET_MODE_BITSIZE (mode) != 64))
14474 return NULL;
14475
14476 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14477 VAR_INIT_STATUS_INITIALIZED);
14478 if (op0 == NULL)
14479 return NULL;
14480
14481 ret = op0;
14482 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14483 mode, mem_mode,
14484 VAR_INIT_STATUS_INITIALIZED);
14485 if (tmp == NULL)
14486 return NULL;
14487 add_loc_descr (&ret, tmp);
14488 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14489 VAR_INIT_STATUS_INITIALIZED);
14490 if (tmp == NULL)
14491 return NULL;
14492 add_loc_descr (&ret, tmp);
14493 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14494 add_loc_descr (&ret, l1label);
14495 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14496 mode, mem_mode,
14497 VAR_INIT_STATUS_INITIALIZED);
14498 add_loc_descr (&ret, tmp);
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14500 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14502 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14503 VAR_INIT_STATUS_INITIALIZED);
14504 if (tmp == NULL)
14505 return NULL;
14506 add_loc_descr (&ret, tmp);
14507 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14508 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14509 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14511 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14512 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14513 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14514 VAR_INIT_STATUS_INITIALIZED);
14515 add_loc_descr (&ret, tmp);
14516 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14517 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14518 add_loc_descr (&ret, l2jump);
14519 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14520 VAR_INIT_STATUS_INITIALIZED);
14521 add_loc_descr (&ret, tmp);
14522 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14523 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14524 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14525 add_loc_descr (&ret, l1jump);
14526 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14527 add_loc_descr (&ret, l2label);
14528 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14529 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14530 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14531 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14532 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14533 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14534 return ret;
14535 }
14536
14537 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14538 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14539 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14540 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14541
14542 ROTATERT is similar:
14543 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14544 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14545 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14546
14547 static dw_loc_descr_ref
14548 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14549 machine_mode mem_mode)
14550 {
14551 rtx rtlop1 = XEXP (rtl, 1);
14552 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14553 int i;
14554
14555 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14556 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14557 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14558 VAR_INIT_STATUS_INITIALIZED);
14559 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14560 VAR_INIT_STATUS_INITIALIZED);
14561 if (op0 == NULL || op1 == NULL)
14562 return NULL;
14563 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14564 for (i = 0; i < 2; i++)
14565 {
14566 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14567 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14568 mode, mem_mode,
14569 VAR_INIT_STATUS_INITIALIZED);
14570 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14571 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14572 ? DW_OP_const4u
14573 : HOST_BITS_PER_WIDE_INT == 64
14574 ? DW_OP_const8u : DW_OP_constu,
14575 GET_MODE_MASK (mode), 0);
14576 else
14577 mask[i] = NULL;
14578 if (mask[i] == NULL)
14579 return NULL;
14580 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14581 }
14582 ret = op0;
14583 add_loc_descr (&ret, op1);
14584 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14585 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14586 if (GET_CODE (rtl) == ROTATERT)
14587 {
14588 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14589 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14590 GET_MODE_BITSIZE (mode), 0));
14591 }
14592 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14593 if (mask[0] != NULL)
14594 add_loc_descr (&ret, mask[0]);
14595 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14596 if (mask[1] != NULL)
14597 {
14598 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14599 add_loc_descr (&ret, mask[1]);
14600 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14601 }
14602 if (GET_CODE (rtl) == ROTATE)
14603 {
14604 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14605 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14606 GET_MODE_BITSIZE (mode), 0));
14607 }
14608 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14609 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14610 return ret;
14611 }
14612
14613 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14614 for DEBUG_PARAMETER_REF RTL. */
14615
14616 static dw_loc_descr_ref
14617 parameter_ref_descriptor (rtx rtl)
14618 {
14619 dw_loc_descr_ref ret;
14620 dw_die_ref ref;
14621
14622 if (dwarf_strict)
14623 return NULL;
14624 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14625 /* With LTO during LTRANS we get the late DIE that refers to the early
14626 DIE, thus we add another indirection here. This seems to confuse
14627 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14628 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14629 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14630 if (ref)
14631 {
14632 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14633 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14634 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14635 }
14636 else
14637 {
14638 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14639 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14640 }
14641 return ret;
14642 }
14643
14644 /* The following routine converts the RTL for a variable or parameter
14645 (resident in memory) into an equivalent Dwarf representation of a
14646 mechanism for getting the address of that same variable onto the top of a
14647 hypothetical "address evaluation" stack.
14648
14649 When creating memory location descriptors, we are effectively transforming
14650 the RTL for a memory-resident object into its Dwarf postfix expression
14651 equivalent. This routine recursively descends an RTL tree, turning
14652 it into Dwarf postfix code as it goes.
14653
14654 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14655
14656 MEM_MODE is the mode of the memory reference, needed to handle some
14657 autoincrement addressing modes.
14658
14659 Return 0 if we can't represent the location. */
14660
14661 dw_loc_descr_ref
14662 mem_loc_descriptor (rtx rtl, machine_mode mode,
14663 machine_mode mem_mode,
14664 enum var_init_status initialized)
14665 {
14666 dw_loc_descr_ref mem_loc_result = NULL;
14667 enum dwarf_location_atom op;
14668 dw_loc_descr_ref op0, op1;
14669 rtx inner = NULL_RTX;
14670
14671 if (mode == VOIDmode)
14672 mode = GET_MODE (rtl);
14673
14674 /* Note that for a dynamically sized array, the location we will generate a
14675 description of here will be the lowest numbered location which is
14676 actually within the array. That's *not* necessarily the same as the
14677 zeroth element of the array. */
14678
14679 rtl = targetm.delegitimize_address (rtl);
14680
14681 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14682 return NULL;
14683
14684 scalar_int_mode int_mode, inner_mode, op1_mode;
14685 switch (GET_CODE (rtl))
14686 {
14687 case POST_INC:
14688 case POST_DEC:
14689 case POST_MODIFY:
14690 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14691
14692 case SUBREG:
14693 /* The case of a subreg may arise when we have a local (register)
14694 variable or a formal (register) parameter which doesn't quite fill
14695 up an entire register. For now, just assume that it is
14696 legitimate to make the Dwarf info refer to the whole register which
14697 contains the given subreg. */
14698 if (!subreg_lowpart_p (rtl))
14699 break;
14700 inner = SUBREG_REG (rtl);
14701 /* FALLTHRU */
14702 case TRUNCATE:
14703 if (inner == NULL_RTX)
14704 inner = XEXP (rtl, 0);
14705 if (is_a <scalar_int_mode> (mode, &int_mode)
14706 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14707 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14708 #ifdef POINTERS_EXTEND_UNSIGNED
14709 || (int_mode == Pmode && mem_mode != VOIDmode)
14710 #endif
14711 )
14712 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14713 {
14714 mem_loc_result = mem_loc_descriptor (inner,
14715 inner_mode,
14716 mem_mode, initialized);
14717 break;
14718 }
14719 if (dwarf_strict && dwarf_version < 5)
14720 break;
14721 if (is_a <scalar_int_mode> (mode, &int_mode)
14722 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14723 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14724 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14725 {
14726 dw_die_ref type_die;
14727 dw_loc_descr_ref cvt;
14728
14729 mem_loc_result = mem_loc_descriptor (inner,
14730 GET_MODE (inner),
14731 mem_mode, initialized);
14732 if (mem_loc_result == NULL)
14733 break;
14734 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14735 if (type_die == NULL)
14736 {
14737 mem_loc_result = NULL;
14738 break;
14739 }
14740 if (GET_MODE_SIZE (mode)
14741 != GET_MODE_SIZE (GET_MODE (inner)))
14742 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14743 else
14744 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14745 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14746 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14747 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14748 add_loc_descr (&mem_loc_result, cvt);
14749 if (is_a <scalar_int_mode> (mode, &int_mode)
14750 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14751 {
14752 /* Convert it to untyped afterwards. */
14753 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14754 add_loc_descr (&mem_loc_result, cvt);
14755 }
14756 }
14757 break;
14758
14759 case REG:
14760 if (!is_a <scalar_int_mode> (mode, &int_mode)
14761 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14762 && rtl != arg_pointer_rtx
14763 && rtl != frame_pointer_rtx
14764 #ifdef POINTERS_EXTEND_UNSIGNED
14765 && (int_mode != Pmode || mem_mode == VOIDmode)
14766 #endif
14767 ))
14768 {
14769 dw_die_ref type_die;
14770 unsigned int dbx_regnum;
14771
14772 if (dwarf_strict && dwarf_version < 5)
14773 break;
14774 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14775 break;
14776 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14777 if (type_die == NULL)
14778 break;
14779
14780 dbx_regnum = dbx_reg_number (rtl);
14781 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14782 break;
14783 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14784 dbx_regnum, 0);
14785 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14786 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14787 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14788 break;
14789 }
14790 /* Whenever a register number forms a part of the description of the
14791 method for calculating the (dynamic) address of a memory resident
14792 object, DWARF rules require the register number be referred to as
14793 a "base register". This distinction is not based in any way upon
14794 what category of register the hardware believes the given register
14795 belongs to. This is strictly DWARF terminology we're dealing with
14796 here. Note that in cases where the location of a memory-resident
14797 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14798 OP_CONST (0)) the actual DWARF location descriptor that we generate
14799 may just be OP_BASEREG (basereg). This may look deceptively like
14800 the object in question was allocated to a register (rather than in
14801 memory) so DWARF consumers need to be aware of the subtle
14802 distinction between OP_REG and OP_BASEREG. */
14803 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14804 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14805 else if (stack_realign_drap
14806 && crtl->drap_reg
14807 && crtl->args.internal_arg_pointer == rtl
14808 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14809 {
14810 /* If RTL is internal_arg_pointer, which has been optimized
14811 out, use DRAP instead. */
14812 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14813 VAR_INIT_STATUS_INITIALIZED);
14814 }
14815 break;
14816
14817 case SIGN_EXTEND:
14818 case ZERO_EXTEND:
14819 if (!is_a <scalar_int_mode> (mode, &int_mode)
14820 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14821 break;
14822 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14823 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14824 if (op0 == 0)
14825 break;
14826 else if (GET_CODE (rtl) == ZERO_EXTEND
14827 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14828 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14829 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14830 to expand zero extend as two shifts instead of
14831 masking. */
14832 && GET_MODE_SIZE (inner_mode) <= 4)
14833 {
14834 mem_loc_result = op0;
14835 add_loc_descr (&mem_loc_result,
14836 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14837 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14838 }
14839 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14840 {
14841 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14842 shift *= BITS_PER_UNIT;
14843 if (GET_CODE (rtl) == SIGN_EXTEND)
14844 op = DW_OP_shra;
14845 else
14846 op = DW_OP_shr;
14847 mem_loc_result = op0;
14848 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14849 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14850 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14851 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14852 }
14853 else if (!dwarf_strict || dwarf_version >= 5)
14854 {
14855 dw_die_ref type_die1, type_die2;
14856 dw_loc_descr_ref cvt;
14857
14858 type_die1 = base_type_for_mode (inner_mode,
14859 GET_CODE (rtl) == ZERO_EXTEND);
14860 if (type_die1 == NULL)
14861 break;
14862 type_die2 = base_type_for_mode (int_mode, 1);
14863 if (type_die2 == NULL)
14864 break;
14865 mem_loc_result = op0;
14866 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14867 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14868 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14869 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14870 add_loc_descr (&mem_loc_result, cvt);
14871 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14872 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14873 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14874 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14875 add_loc_descr (&mem_loc_result, cvt);
14876 }
14877 break;
14878
14879 case MEM:
14880 {
14881 rtx new_rtl = avoid_constant_pool_reference (rtl);
14882 if (new_rtl != rtl)
14883 {
14884 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14885 initialized);
14886 if (mem_loc_result != NULL)
14887 return mem_loc_result;
14888 }
14889 }
14890 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14891 get_address_mode (rtl), mode,
14892 VAR_INIT_STATUS_INITIALIZED);
14893 if (mem_loc_result == NULL)
14894 mem_loc_result = tls_mem_loc_descriptor (rtl);
14895 if (mem_loc_result != NULL)
14896 {
14897 if (!is_a <scalar_int_mode> (mode, &int_mode)
14898 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14899 {
14900 dw_die_ref type_die;
14901 dw_loc_descr_ref deref;
14902
14903 if (dwarf_strict && dwarf_version < 5)
14904 return NULL;
14905 type_die
14906 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14907 if (type_die == NULL)
14908 return NULL;
14909 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14910 GET_MODE_SIZE (mode), 0);
14911 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14912 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14913 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14914 add_loc_descr (&mem_loc_result, deref);
14915 }
14916 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14917 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14918 else
14919 add_loc_descr (&mem_loc_result,
14920 new_loc_descr (DW_OP_deref_size,
14921 GET_MODE_SIZE (int_mode), 0));
14922 }
14923 break;
14924
14925 case LO_SUM:
14926 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14927
14928 case LABEL_REF:
14929 /* Some ports can transform a symbol ref into a label ref, because
14930 the symbol ref is too far away and has to be dumped into a constant
14931 pool. */
14932 case CONST:
14933 case SYMBOL_REF:
14934 if (!is_a <scalar_int_mode> (mode, &int_mode)
14935 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14936 #ifdef POINTERS_EXTEND_UNSIGNED
14937 && (int_mode != Pmode || mem_mode == VOIDmode)
14938 #endif
14939 ))
14940 break;
14941 if (GET_CODE (rtl) == SYMBOL_REF
14942 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14943 {
14944 dw_loc_descr_ref temp;
14945
14946 /* If this is not defined, we have no way to emit the data. */
14947 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14948 break;
14949
14950 temp = new_addr_loc_descr (rtl, dtprel_true);
14951
14952 /* We check for DWARF 5 here because gdb did not implement
14953 DW_OP_form_tls_address until after 7.12. */
14954 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14955 ? DW_OP_form_tls_address
14956 : DW_OP_GNU_push_tls_address),
14957 0, 0);
14958 add_loc_descr (&mem_loc_result, temp);
14959
14960 break;
14961 }
14962
14963 if (!const_ok_for_output (rtl))
14964 {
14965 if (GET_CODE (rtl) == CONST)
14966 switch (GET_CODE (XEXP (rtl, 0)))
14967 {
14968 case NOT:
14969 op = DW_OP_not;
14970 goto try_const_unop;
14971 case NEG:
14972 op = DW_OP_neg;
14973 goto try_const_unop;
14974 try_const_unop:
14975 rtx arg;
14976 arg = XEXP (XEXP (rtl, 0), 0);
14977 if (!CONSTANT_P (arg))
14978 arg = gen_rtx_CONST (int_mode, arg);
14979 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
14980 initialized);
14981 if (op0)
14982 {
14983 mem_loc_result = op0;
14984 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14985 }
14986 break;
14987 default:
14988 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14989 mem_mode, initialized);
14990 break;
14991 }
14992 break;
14993 }
14994
14995 symref:
14996 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14997 vec_safe_push (used_rtx_array, rtl);
14998 break;
14999
15000 case CONCAT:
15001 case CONCATN:
15002 case VAR_LOCATION:
15003 case DEBUG_IMPLICIT_PTR:
15004 expansion_failed (NULL_TREE, rtl,
15005 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15006 return 0;
15007
15008 case ENTRY_VALUE:
15009 if (dwarf_strict && dwarf_version < 5)
15010 return NULL;
15011 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15012 {
15013 if (!is_a <scalar_int_mode> (mode, &int_mode)
15014 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15015 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15016 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15017 else
15018 {
15019 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15020 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15021 return NULL;
15022 op0 = one_reg_loc_descriptor (dbx_regnum,
15023 VAR_INIT_STATUS_INITIALIZED);
15024 }
15025 }
15026 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15027 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15028 {
15029 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15030 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15031 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15032 return NULL;
15033 }
15034 else
15035 gcc_unreachable ();
15036 if (op0 == NULL)
15037 return NULL;
15038 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15039 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15040 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15041 break;
15042
15043 case DEBUG_PARAMETER_REF:
15044 mem_loc_result = parameter_ref_descriptor (rtl);
15045 break;
15046
15047 case PRE_MODIFY:
15048 /* Extract the PLUS expression nested inside and fall into
15049 PLUS code below. */
15050 rtl = XEXP (rtl, 1);
15051 goto plus;
15052
15053 case PRE_INC:
15054 case PRE_DEC:
15055 /* Turn these into a PLUS expression and fall into the PLUS code
15056 below. */
15057 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15058 gen_int_mode (GET_CODE (rtl) == PRE_INC
15059 ? GET_MODE_UNIT_SIZE (mem_mode)
15060 : -GET_MODE_UNIT_SIZE (mem_mode),
15061 mode));
15062
15063 /* fall through */
15064
15065 case PLUS:
15066 plus:
15067 if (is_based_loc (rtl)
15068 && is_a <scalar_int_mode> (mode, &int_mode)
15069 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15070 || XEXP (rtl, 0) == arg_pointer_rtx
15071 || XEXP (rtl, 0) == frame_pointer_rtx))
15072 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15073 INTVAL (XEXP (rtl, 1)),
15074 VAR_INIT_STATUS_INITIALIZED);
15075 else
15076 {
15077 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15078 VAR_INIT_STATUS_INITIALIZED);
15079 if (mem_loc_result == 0)
15080 break;
15081
15082 if (CONST_INT_P (XEXP (rtl, 1))
15083 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15084 <= DWARF2_ADDR_SIZE))
15085 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15086 else
15087 {
15088 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15089 VAR_INIT_STATUS_INITIALIZED);
15090 if (op1 == 0)
15091 return NULL;
15092 add_loc_descr (&mem_loc_result, op1);
15093 add_loc_descr (&mem_loc_result,
15094 new_loc_descr (DW_OP_plus, 0, 0));
15095 }
15096 }
15097 break;
15098
15099 /* If a pseudo-reg is optimized away, it is possible for it to
15100 be replaced with a MEM containing a multiply or shift. */
15101 case MINUS:
15102 op = DW_OP_minus;
15103 goto do_binop;
15104
15105 case MULT:
15106 op = DW_OP_mul;
15107 goto do_binop;
15108
15109 case DIV:
15110 if ((!dwarf_strict || dwarf_version >= 5)
15111 && is_a <scalar_int_mode> (mode, &int_mode)
15112 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15113 {
15114 mem_loc_result = typed_binop (DW_OP_div, rtl,
15115 base_type_for_mode (mode, 0),
15116 int_mode, mem_mode);
15117 break;
15118 }
15119 op = DW_OP_div;
15120 goto do_binop;
15121
15122 case UMOD:
15123 op = DW_OP_mod;
15124 goto do_binop;
15125
15126 case ASHIFT:
15127 op = DW_OP_shl;
15128 goto do_shift;
15129
15130 case ASHIFTRT:
15131 op = DW_OP_shra;
15132 goto do_shift;
15133
15134 case LSHIFTRT:
15135 op = DW_OP_shr;
15136 goto do_shift;
15137
15138 do_shift:
15139 if (!is_a <scalar_int_mode> (mode, &int_mode))
15140 break;
15141 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15142 VAR_INIT_STATUS_INITIALIZED);
15143 {
15144 rtx rtlop1 = XEXP (rtl, 1);
15145 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15146 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15147 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15148 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15149 VAR_INIT_STATUS_INITIALIZED);
15150 }
15151
15152 if (op0 == 0 || op1 == 0)
15153 break;
15154
15155 mem_loc_result = op0;
15156 add_loc_descr (&mem_loc_result, op1);
15157 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15158 break;
15159
15160 case AND:
15161 op = DW_OP_and;
15162 goto do_binop;
15163
15164 case IOR:
15165 op = DW_OP_or;
15166 goto do_binop;
15167
15168 case XOR:
15169 op = DW_OP_xor;
15170 goto do_binop;
15171
15172 do_binop:
15173 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15174 VAR_INIT_STATUS_INITIALIZED);
15175 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15176 VAR_INIT_STATUS_INITIALIZED);
15177
15178 if (op0 == 0 || op1 == 0)
15179 break;
15180
15181 mem_loc_result = op0;
15182 add_loc_descr (&mem_loc_result, op1);
15183 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15184 break;
15185
15186 case MOD:
15187 if ((!dwarf_strict || dwarf_version >= 5)
15188 && is_a <scalar_int_mode> (mode, &int_mode)
15189 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15190 {
15191 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15192 base_type_for_mode (mode, 0),
15193 int_mode, mem_mode);
15194 break;
15195 }
15196
15197 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15198 VAR_INIT_STATUS_INITIALIZED);
15199 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15200 VAR_INIT_STATUS_INITIALIZED);
15201
15202 if (op0 == 0 || op1 == 0)
15203 break;
15204
15205 mem_loc_result = op0;
15206 add_loc_descr (&mem_loc_result, op1);
15207 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15208 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15209 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15210 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15211 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15212 break;
15213
15214 case UDIV:
15215 if ((!dwarf_strict || dwarf_version >= 5)
15216 && is_a <scalar_int_mode> (mode, &int_mode))
15217 {
15218 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15219 {
15220 op = DW_OP_div;
15221 goto do_binop;
15222 }
15223 mem_loc_result = typed_binop (DW_OP_div, rtl,
15224 base_type_for_mode (int_mode, 1),
15225 int_mode, mem_mode);
15226 }
15227 break;
15228
15229 case NOT:
15230 op = DW_OP_not;
15231 goto do_unop;
15232
15233 case ABS:
15234 op = DW_OP_abs;
15235 goto do_unop;
15236
15237 case NEG:
15238 op = DW_OP_neg;
15239 goto do_unop;
15240
15241 do_unop:
15242 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15243 VAR_INIT_STATUS_INITIALIZED);
15244
15245 if (op0 == 0)
15246 break;
15247
15248 mem_loc_result = op0;
15249 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15250 break;
15251
15252 case CONST_INT:
15253 if (!is_a <scalar_int_mode> (mode, &int_mode)
15254 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15255 #ifdef POINTERS_EXTEND_UNSIGNED
15256 || (int_mode == Pmode
15257 && mem_mode != VOIDmode
15258 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15259 #endif
15260 )
15261 {
15262 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15263 break;
15264 }
15265 if ((!dwarf_strict || dwarf_version >= 5)
15266 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15267 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15268 {
15269 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15270 scalar_int_mode amode;
15271 if (type_die == NULL)
15272 return NULL;
15273 if (INTVAL (rtl) >= 0
15274 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15275 .exists (&amode))
15276 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15277 /* const DW_OP_convert <XXX> vs.
15278 DW_OP_const_type <XXX, 1, const>. */
15279 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15280 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15281 {
15282 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15283 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15284 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15285 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15286 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15287 add_loc_descr (&mem_loc_result, op0);
15288 return mem_loc_result;
15289 }
15290 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15291 INTVAL (rtl));
15292 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15293 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15294 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15295 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15296 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15297 else
15298 {
15299 mem_loc_result->dw_loc_oprnd2.val_class
15300 = dw_val_class_const_double;
15301 mem_loc_result->dw_loc_oprnd2.v.val_double
15302 = double_int::from_shwi (INTVAL (rtl));
15303 }
15304 }
15305 break;
15306
15307 case CONST_DOUBLE:
15308 if (!dwarf_strict || dwarf_version >= 5)
15309 {
15310 dw_die_ref type_die;
15311
15312 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15313 CONST_DOUBLE rtx could represent either a large integer
15314 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15315 the value is always a floating point constant.
15316
15317 When it is an integer, a CONST_DOUBLE is used whenever
15318 the constant requires 2 HWIs to be adequately represented.
15319 We output CONST_DOUBLEs as blocks. */
15320 if (mode == VOIDmode
15321 || (GET_MODE (rtl) == VOIDmode
15322 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15323 break;
15324 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15325 if (type_die == NULL)
15326 return NULL;
15327 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15328 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15329 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15330 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15331 #if TARGET_SUPPORTS_WIDE_INT == 0
15332 if (!SCALAR_FLOAT_MODE_P (mode))
15333 {
15334 mem_loc_result->dw_loc_oprnd2.val_class
15335 = dw_val_class_const_double;
15336 mem_loc_result->dw_loc_oprnd2.v.val_double
15337 = rtx_to_double_int (rtl);
15338 }
15339 else
15340 #endif
15341 {
15342 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15343 unsigned int length = GET_MODE_SIZE (float_mode);
15344 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15345
15346 insert_float (rtl, array);
15347 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15348 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15349 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15350 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15351 }
15352 }
15353 break;
15354
15355 case CONST_WIDE_INT:
15356 if (!dwarf_strict || dwarf_version >= 5)
15357 {
15358 dw_die_ref type_die;
15359
15360 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15361 if (type_die == NULL)
15362 return NULL;
15363 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15364 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15365 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15366 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15367 mem_loc_result->dw_loc_oprnd2.val_class
15368 = dw_val_class_wide_int;
15369 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15370 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15371 }
15372 break;
15373
15374 case EQ:
15375 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15376 break;
15377
15378 case GE:
15379 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15380 break;
15381
15382 case GT:
15383 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15384 break;
15385
15386 case LE:
15387 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15388 break;
15389
15390 case LT:
15391 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15392 break;
15393
15394 case NE:
15395 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15396 break;
15397
15398 case GEU:
15399 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15400 break;
15401
15402 case GTU:
15403 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15404 break;
15405
15406 case LEU:
15407 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15408 break;
15409
15410 case LTU:
15411 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15412 break;
15413
15414 case UMIN:
15415 case UMAX:
15416 if (!SCALAR_INT_MODE_P (mode))
15417 break;
15418 /* FALLTHRU */
15419 case SMIN:
15420 case SMAX:
15421 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15422 break;
15423
15424 case ZERO_EXTRACT:
15425 case SIGN_EXTRACT:
15426 if (CONST_INT_P (XEXP (rtl, 1))
15427 && CONST_INT_P (XEXP (rtl, 2))
15428 && is_a <scalar_int_mode> (mode, &int_mode)
15429 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15430 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15431 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15432 && ((unsigned) INTVAL (XEXP (rtl, 1))
15433 + (unsigned) INTVAL (XEXP (rtl, 2))
15434 <= GET_MODE_BITSIZE (int_mode)))
15435 {
15436 int shift, size;
15437 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15438 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15439 if (op0 == 0)
15440 break;
15441 if (GET_CODE (rtl) == SIGN_EXTRACT)
15442 op = DW_OP_shra;
15443 else
15444 op = DW_OP_shr;
15445 mem_loc_result = op0;
15446 size = INTVAL (XEXP (rtl, 1));
15447 shift = INTVAL (XEXP (rtl, 2));
15448 if (BITS_BIG_ENDIAN)
15449 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15450 if (shift + size != (int) DWARF2_ADDR_SIZE)
15451 {
15452 add_loc_descr (&mem_loc_result,
15453 int_loc_descriptor (DWARF2_ADDR_SIZE
15454 - shift - size));
15455 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15456 }
15457 if (size != (int) DWARF2_ADDR_SIZE)
15458 {
15459 add_loc_descr (&mem_loc_result,
15460 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15461 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15462 }
15463 }
15464 break;
15465
15466 case IF_THEN_ELSE:
15467 {
15468 dw_loc_descr_ref op2, bra_node, drop_node;
15469 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15470 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15471 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15472 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15473 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15474 VAR_INIT_STATUS_INITIALIZED);
15475 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15476 VAR_INIT_STATUS_INITIALIZED);
15477 if (op0 == NULL || op1 == NULL || op2 == NULL)
15478 break;
15479
15480 mem_loc_result = op1;
15481 add_loc_descr (&mem_loc_result, op2);
15482 add_loc_descr (&mem_loc_result, op0);
15483 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15484 add_loc_descr (&mem_loc_result, bra_node);
15485 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15486 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15487 add_loc_descr (&mem_loc_result, drop_node);
15488 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15489 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15490 }
15491 break;
15492
15493 case FLOAT_EXTEND:
15494 case FLOAT_TRUNCATE:
15495 case FLOAT:
15496 case UNSIGNED_FLOAT:
15497 case FIX:
15498 case UNSIGNED_FIX:
15499 if (!dwarf_strict || dwarf_version >= 5)
15500 {
15501 dw_die_ref type_die;
15502 dw_loc_descr_ref cvt;
15503
15504 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15505 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15506 if (op0 == NULL)
15507 break;
15508 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15509 && (GET_CODE (rtl) == FLOAT
15510 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15511 {
15512 type_die = base_type_for_mode (int_mode,
15513 GET_CODE (rtl) == UNSIGNED_FLOAT);
15514 if (type_die == NULL)
15515 break;
15516 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15517 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15518 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15519 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15520 add_loc_descr (&op0, cvt);
15521 }
15522 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15523 if (type_die == NULL)
15524 break;
15525 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15526 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15527 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15528 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15529 add_loc_descr (&op0, cvt);
15530 if (is_a <scalar_int_mode> (mode, &int_mode)
15531 && (GET_CODE (rtl) == FIX
15532 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15533 {
15534 op0 = convert_descriptor_to_mode (int_mode, op0);
15535 if (op0 == NULL)
15536 break;
15537 }
15538 mem_loc_result = op0;
15539 }
15540 break;
15541
15542 case CLZ:
15543 case CTZ:
15544 case FFS:
15545 if (is_a <scalar_int_mode> (mode, &int_mode))
15546 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15547 break;
15548
15549 case POPCOUNT:
15550 case PARITY:
15551 if (is_a <scalar_int_mode> (mode, &int_mode))
15552 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15553 break;
15554
15555 case BSWAP:
15556 if (is_a <scalar_int_mode> (mode, &int_mode))
15557 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15558 break;
15559
15560 case ROTATE:
15561 case ROTATERT:
15562 if (is_a <scalar_int_mode> (mode, &int_mode))
15563 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15564 break;
15565
15566 case COMPARE:
15567 /* In theory, we could implement the above. */
15568 /* DWARF cannot represent the unsigned compare operations
15569 natively. */
15570 case SS_MULT:
15571 case US_MULT:
15572 case SS_DIV:
15573 case US_DIV:
15574 case SS_PLUS:
15575 case US_PLUS:
15576 case SS_MINUS:
15577 case US_MINUS:
15578 case SS_NEG:
15579 case US_NEG:
15580 case SS_ABS:
15581 case SS_ASHIFT:
15582 case US_ASHIFT:
15583 case SS_TRUNCATE:
15584 case US_TRUNCATE:
15585 case UNORDERED:
15586 case ORDERED:
15587 case UNEQ:
15588 case UNGE:
15589 case UNGT:
15590 case UNLE:
15591 case UNLT:
15592 case LTGT:
15593 case FRACT_CONVERT:
15594 case UNSIGNED_FRACT_CONVERT:
15595 case SAT_FRACT:
15596 case UNSIGNED_SAT_FRACT:
15597 case SQRT:
15598 case ASM_OPERANDS:
15599 case VEC_MERGE:
15600 case VEC_SELECT:
15601 case VEC_CONCAT:
15602 case VEC_DUPLICATE:
15603 case VEC_SERIES:
15604 case UNSPEC:
15605 case HIGH:
15606 case FMA:
15607 case STRICT_LOW_PART:
15608 case CONST_VECTOR:
15609 case CONST_FIXED:
15610 case CLRSB:
15611 case CLOBBER:
15612 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15613 can't express it in the debug info. This can happen e.g. with some
15614 TLS UNSPECs. */
15615 break;
15616
15617 case CONST_STRING:
15618 resolve_one_addr (&rtl);
15619 goto symref;
15620
15621 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15622 the expression. An UNSPEC rtx represents a raw DWARF operation,
15623 new_loc_descr is called for it to build the operation directly.
15624 Otherwise mem_loc_descriptor is called recursively. */
15625 case PARALLEL:
15626 {
15627 int index = 0;
15628 dw_loc_descr_ref exp_result = NULL;
15629
15630 for (; index < XVECLEN (rtl, 0); index++)
15631 {
15632 rtx elem = XVECEXP (rtl, 0, index);
15633 if (GET_CODE (elem) == UNSPEC)
15634 {
15635 /* Each DWARF operation UNSPEC contain two operands, if
15636 one operand is not used for the operation, const0_rtx is
15637 passed. */
15638 gcc_assert (XVECLEN (elem, 0) == 2);
15639
15640 HOST_WIDE_INT dw_op = XINT (elem, 1);
15641 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15642 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15643 exp_result
15644 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15645 oprnd2);
15646 }
15647 else
15648 exp_result
15649 = mem_loc_descriptor (elem, mode, mem_mode,
15650 VAR_INIT_STATUS_INITIALIZED);
15651
15652 if (!mem_loc_result)
15653 mem_loc_result = exp_result;
15654 else
15655 add_loc_descr (&mem_loc_result, exp_result);
15656 }
15657
15658 break;
15659 }
15660
15661 default:
15662 if (flag_checking)
15663 {
15664 print_rtl (stderr, rtl);
15665 gcc_unreachable ();
15666 }
15667 break;
15668 }
15669
15670 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15671 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15672
15673 return mem_loc_result;
15674 }
15675
15676 /* Return a descriptor that describes the concatenation of two locations.
15677 This is typically a complex variable. */
15678
15679 static dw_loc_descr_ref
15680 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15681 {
15682 dw_loc_descr_ref cc_loc_result = NULL;
15683 dw_loc_descr_ref x0_ref
15684 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15685 dw_loc_descr_ref x1_ref
15686 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15687
15688 if (x0_ref == 0 || x1_ref == 0)
15689 return 0;
15690
15691 cc_loc_result = x0_ref;
15692 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15693
15694 add_loc_descr (&cc_loc_result, x1_ref);
15695 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15696
15697 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15698 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15699
15700 return cc_loc_result;
15701 }
15702
15703 /* Return a descriptor that describes the concatenation of N
15704 locations. */
15705
15706 static dw_loc_descr_ref
15707 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15708 {
15709 unsigned int i;
15710 dw_loc_descr_ref cc_loc_result = NULL;
15711 unsigned int n = XVECLEN (concatn, 0);
15712
15713 for (i = 0; i < n; ++i)
15714 {
15715 dw_loc_descr_ref ref;
15716 rtx x = XVECEXP (concatn, 0, i);
15717
15718 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15719 if (ref == NULL)
15720 return NULL;
15721
15722 add_loc_descr (&cc_loc_result, ref);
15723 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15724 }
15725
15726 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15727 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15728
15729 return cc_loc_result;
15730 }
15731
15732 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15733 for DEBUG_IMPLICIT_PTR RTL. */
15734
15735 static dw_loc_descr_ref
15736 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15737 {
15738 dw_loc_descr_ref ret;
15739 dw_die_ref ref;
15740
15741 if (dwarf_strict && dwarf_version < 5)
15742 return NULL;
15743 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15744 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15745 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15746 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15747 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15748 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15749 if (ref)
15750 {
15751 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15752 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15753 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15754 }
15755 else
15756 {
15757 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15758 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15759 }
15760 return ret;
15761 }
15762
15763 /* Output a proper Dwarf location descriptor for a variable or parameter
15764 which is either allocated in a register or in a memory location. For a
15765 register, we just generate an OP_REG and the register number. For a
15766 memory location we provide a Dwarf postfix expression describing how to
15767 generate the (dynamic) address of the object onto the address stack.
15768
15769 MODE is mode of the decl if this loc_descriptor is going to be used in
15770 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15771 allowed, VOIDmode otherwise.
15772
15773 If we don't know how to describe it, return 0. */
15774
15775 static dw_loc_descr_ref
15776 loc_descriptor (rtx rtl, machine_mode mode,
15777 enum var_init_status initialized)
15778 {
15779 dw_loc_descr_ref loc_result = NULL;
15780 scalar_int_mode int_mode;
15781
15782 switch (GET_CODE (rtl))
15783 {
15784 case SUBREG:
15785 /* The case of a subreg may arise when we have a local (register)
15786 variable or a formal (register) parameter which doesn't quite fill
15787 up an entire register. For now, just assume that it is
15788 legitimate to make the Dwarf info refer to the whole register which
15789 contains the given subreg. */
15790 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15791 loc_result = loc_descriptor (SUBREG_REG (rtl),
15792 GET_MODE (SUBREG_REG (rtl)), initialized);
15793 else
15794 goto do_default;
15795 break;
15796
15797 case REG:
15798 loc_result = reg_loc_descriptor (rtl, initialized);
15799 break;
15800
15801 case MEM:
15802 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15803 GET_MODE (rtl), initialized);
15804 if (loc_result == NULL)
15805 loc_result = tls_mem_loc_descriptor (rtl);
15806 if (loc_result == NULL)
15807 {
15808 rtx new_rtl = avoid_constant_pool_reference (rtl);
15809 if (new_rtl != rtl)
15810 loc_result = loc_descriptor (new_rtl, mode, initialized);
15811 }
15812 break;
15813
15814 case CONCAT:
15815 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15816 initialized);
15817 break;
15818
15819 case CONCATN:
15820 loc_result = concatn_loc_descriptor (rtl, initialized);
15821 break;
15822
15823 case VAR_LOCATION:
15824 /* Single part. */
15825 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15826 {
15827 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15828 if (GET_CODE (loc) == EXPR_LIST)
15829 loc = XEXP (loc, 0);
15830 loc_result = loc_descriptor (loc, mode, initialized);
15831 break;
15832 }
15833
15834 rtl = XEXP (rtl, 1);
15835 /* FALLTHRU */
15836
15837 case PARALLEL:
15838 {
15839 rtvec par_elems = XVEC (rtl, 0);
15840 int num_elem = GET_NUM_ELEM (par_elems);
15841 machine_mode mode;
15842 int i;
15843
15844 /* Create the first one, so we have something to add to. */
15845 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15846 VOIDmode, initialized);
15847 if (loc_result == NULL)
15848 return NULL;
15849 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15850 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15851 for (i = 1; i < num_elem; i++)
15852 {
15853 dw_loc_descr_ref temp;
15854
15855 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15856 VOIDmode, initialized);
15857 if (temp == NULL)
15858 return NULL;
15859 add_loc_descr (&loc_result, temp);
15860 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15861 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15862 }
15863 }
15864 break;
15865
15866 case CONST_INT:
15867 if (mode != VOIDmode && mode != BLKmode)
15868 {
15869 int_mode = as_a <scalar_int_mode> (mode);
15870 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15871 INTVAL (rtl));
15872 }
15873 break;
15874
15875 case CONST_DOUBLE:
15876 if (mode == VOIDmode)
15877 mode = GET_MODE (rtl);
15878
15879 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15880 {
15881 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15882
15883 /* Note that a CONST_DOUBLE rtx could represent either an integer
15884 or a floating-point constant. A CONST_DOUBLE is used whenever
15885 the constant requires more than one word in order to be
15886 adequately represented. We output CONST_DOUBLEs as blocks. */
15887 scalar_mode smode = as_a <scalar_mode> (mode);
15888 loc_result = new_loc_descr (DW_OP_implicit_value,
15889 GET_MODE_SIZE (smode), 0);
15890 #if TARGET_SUPPORTS_WIDE_INT == 0
15891 if (!SCALAR_FLOAT_MODE_P (smode))
15892 {
15893 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15894 loc_result->dw_loc_oprnd2.v.val_double
15895 = rtx_to_double_int (rtl);
15896 }
15897 else
15898 #endif
15899 {
15900 unsigned int length = GET_MODE_SIZE (smode);
15901 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15902
15903 insert_float (rtl, array);
15904 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15905 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15906 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15907 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15908 }
15909 }
15910 break;
15911
15912 case CONST_WIDE_INT:
15913 if (mode == VOIDmode)
15914 mode = GET_MODE (rtl);
15915
15916 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15917 {
15918 int_mode = as_a <scalar_int_mode> (mode);
15919 loc_result = new_loc_descr (DW_OP_implicit_value,
15920 GET_MODE_SIZE (int_mode), 0);
15921 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15922 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15923 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15924 }
15925 break;
15926
15927 case CONST_VECTOR:
15928 if (mode == VOIDmode)
15929 mode = GET_MODE (rtl);
15930
15931 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15932 {
15933 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15934 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15935 unsigned char *array
15936 = ggc_vec_alloc<unsigned char> (length * elt_size);
15937 unsigned int i;
15938 unsigned char *p;
15939 machine_mode imode = GET_MODE_INNER (mode);
15940
15941 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15942 switch (GET_MODE_CLASS (mode))
15943 {
15944 case MODE_VECTOR_INT:
15945 for (i = 0, p = array; i < length; i++, p += elt_size)
15946 {
15947 rtx elt = CONST_VECTOR_ELT (rtl, i);
15948 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15949 }
15950 break;
15951
15952 case MODE_VECTOR_FLOAT:
15953 for (i = 0, p = array; i < length; i++, p += elt_size)
15954 {
15955 rtx elt = CONST_VECTOR_ELT (rtl, i);
15956 insert_float (elt, p);
15957 }
15958 break;
15959
15960 default:
15961 gcc_unreachable ();
15962 }
15963
15964 loc_result = new_loc_descr (DW_OP_implicit_value,
15965 length * elt_size, 0);
15966 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15967 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15968 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15969 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15970 }
15971 break;
15972
15973 case CONST:
15974 if (mode == VOIDmode
15975 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15976 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15977 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15978 {
15979 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15980 break;
15981 }
15982 /* FALLTHROUGH */
15983 case SYMBOL_REF:
15984 if (!const_ok_for_output (rtl))
15985 break;
15986 /* FALLTHROUGH */
15987 case LABEL_REF:
15988 if (is_a <scalar_int_mode> (mode, &int_mode)
15989 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15990 && (dwarf_version >= 4 || !dwarf_strict))
15991 {
15992 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15993 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15994 vec_safe_push (used_rtx_array, rtl);
15995 }
15996 break;
15997
15998 case DEBUG_IMPLICIT_PTR:
15999 loc_result = implicit_ptr_descriptor (rtl, 0);
16000 break;
16001
16002 case PLUS:
16003 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16004 && CONST_INT_P (XEXP (rtl, 1)))
16005 {
16006 loc_result
16007 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16008 break;
16009 }
16010 /* FALLTHRU */
16011 do_default:
16012 default:
16013 if ((is_a <scalar_int_mode> (mode, &int_mode)
16014 && GET_MODE (rtl) == int_mode
16015 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16016 && dwarf_version >= 4)
16017 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16018 {
16019 /* Value expression. */
16020 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16021 if (loc_result)
16022 add_loc_descr (&loc_result,
16023 new_loc_descr (DW_OP_stack_value, 0, 0));
16024 }
16025 break;
16026 }
16027
16028 return loc_result;
16029 }
16030
16031 /* We need to figure out what section we should use as the base for the
16032 address ranges where a given location is valid.
16033 1. If this particular DECL has a section associated with it, use that.
16034 2. If this function has a section associated with it, use that.
16035 3. Otherwise, use the text section.
16036 XXX: If you split a variable across multiple sections, we won't notice. */
16037
16038 static const char *
16039 secname_for_decl (const_tree decl)
16040 {
16041 const char *secname;
16042
16043 if (VAR_OR_FUNCTION_DECL_P (decl)
16044 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16045 && DECL_SECTION_NAME (decl))
16046 secname = DECL_SECTION_NAME (decl);
16047 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16048 secname = DECL_SECTION_NAME (current_function_decl);
16049 else if (cfun && in_cold_section_p)
16050 secname = crtl->subsections.cold_section_label;
16051 else
16052 secname = text_section_label;
16053
16054 return secname;
16055 }
16056
16057 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16058
16059 static bool
16060 decl_by_reference_p (tree decl)
16061 {
16062 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16063 || VAR_P (decl))
16064 && DECL_BY_REFERENCE (decl));
16065 }
16066
16067 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16068 for VARLOC. */
16069
16070 static dw_loc_descr_ref
16071 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16072 enum var_init_status initialized)
16073 {
16074 int have_address = 0;
16075 dw_loc_descr_ref descr;
16076 machine_mode mode;
16077
16078 if (want_address != 2)
16079 {
16080 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16081 /* Single part. */
16082 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16083 {
16084 varloc = PAT_VAR_LOCATION_LOC (varloc);
16085 if (GET_CODE (varloc) == EXPR_LIST)
16086 varloc = XEXP (varloc, 0);
16087 mode = GET_MODE (varloc);
16088 if (MEM_P (varloc))
16089 {
16090 rtx addr = XEXP (varloc, 0);
16091 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16092 mode, initialized);
16093 if (descr)
16094 have_address = 1;
16095 else
16096 {
16097 rtx x = avoid_constant_pool_reference (varloc);
16098 if (x != varloc)
16099 descr = mem_loc_descriptor (x, mode, VOIDmode,
16100 initialized);
16101 }
16102 }
16103 else
16104 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16105 }
16106 else
16107 return 0;
16108 }
16109 else
16110 {
16111 if (GET_CODE (varloc) == VAR_LOCATION)
16112 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16113 else
16114 mode = DECL_MODE (loc);
16115 descr = loc_descriptor (varloc, mode, initialized);
16116 have_address = 1;
16117 }
16118
16119 if (!descr)
16120 return 0;
16121
16122 if (want_address == 2 && !have_address
16123 && (dwarf_version >= 4 || !dwarf_strict))
16124 {
16125 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16126 {
16127 expansion_failed (loc, NULL_RTX,
16128 "DWARF address size mismatch");
16129 return 0;
16130 }
16131 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16132 have_address = 1;
16133 }
16134 /* Show if we can't fill the request for an address. */
16135 if (want_address && !have_address)
16136 {
16137 expansion_failed (loc, NULL_RTX,
16138 "Want address and only have value");
16139 return 0;
16140 }
16141
16142 /* If we've got an address and don't want one, dereference. */
16143 if (!want_address && have_address)
16144 {
16145 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16146 enum dwarf_location_atom op;
16147
16148 if (size > DWARF2_ADDR_SIZE || size == -1)
16149 {
16150 expansion_failed (loc, NULL_RTX,
16151 "DWARF address size mismatch");
16152 return 0;
16153 }
16154 else if (size == DWARF2_ADDR_SIZE)
16155 op = DW_OP_deref;
16156 else
16157 op = DW_OP_deref_size;
16158
16159 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16160 }
16161
16162 return descr;
16163 }
16164
16165 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16166 if it is not possible. */
16167
16168 static dw_loc_descr_ref
16169 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16170 {
16171 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16172 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16173 else if (dwarf_version >= 3 || !dwarf_strict)
16174 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16175 else
16176 return NULL;
16177 }
16178
16179 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16180 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16181
16182 static dw_loc_descr_ref
16183 dw_sra_loc_expr (tree decl, rtx loc)
16184 {
16185 rtx p;
16186 unsigned HOST_WIDE_INT padsize = 0;
16187 dw_loc_descr_ref descr, *descr_tail;
16188 unsigned HOST_WIDE_INT decl_size;
16189 rtx varloc;
16190 enum var_init_status initialized;
16191
16192 if (DECL_SIZE (decl) == NULL
16193 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16194 return NULL;
16195
16196 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16197 descr = NULL;
16198 descr_tail = &descr;
16199
16200 for (p = loc; p; p = XEXP (p, 1))
16201 {
16202 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16203 rtx loc_note = *decl_piece_varloc_ptr (p);
16204 dw_loc_descr_ref cur_descr;
16205 dw_loc_descr_ref *tail, last = NULL;
16206 unsigned HOST_WIDE_INT opsize = 0;
16207
16208 if (loc_note == NULL_RTX
16209 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16210 {
16211 padsize += bitsize;
16212 continue;
16213 }
16214 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16215 varloc = NOTE_VAR_LOCATION (loc_note);
16216 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16217 if (cur_descr == NULL)
16218 {
16219 padsize += bitsize;
16220 continue;
16221 }
16222
16223 /* Check that cur_descr either doesn't use
16224 DW_OP_*piece operations, or their sum is equal
16225 to bitsize. Otherwise we can't embed it. */
16226 for (tail = &cur_descr; *tail != NULL;
16227 tail = &(*tail)->dw_loc_next)
16228 if ((*tail)->dw_loc_opc == DW_OP_piece)
16229 {
16230 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16231 * BITS_PER_UNIT;
16232 last = *tail;
16233 }
16234 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16235 {
16236 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16237 last = *tail;
16238 }
16239
16240 if (last != NULL && opsize != bitsize)
16241 {
16242 padsize += bitsize;
16243 /* Discard the current piece of the descriptor and release any
16244 addr_table entries it uses. */
16245 remove_loc_list_addr_table_entries (cur_descr);
16246 continue;
16247 }
16248
16249 /* If there is a hole, add DW_OP_*piece after empty DWARF
16250 expression, which means that those bits are optimized out. */
16251 if (padsize)
16252 {
16253 if (padsize > decl_size)
16254 {
16255 remove_loc_list_addr_table_entries (cur_descr);
16256 goto discard_descr;
16257 }
16258 decl_size -= padsize;
16259 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16260 if (*descr_tail == NULL)
16261 {
16262 remove_loc_list_addr_table_entries (cur_descr);
16263 goto discard_descr;
16264 }
16265 descr_tail = &(*descr_tail)->dw_loc_next;
16266 padsize = 0;
16267 }
16268 *descr_tail = cur_descr;
16269 descr_tail = tail;
16270 if (bitsize > decl_size)
16271 goto discard_descr;
16272 decl_size -= bitsize;
16273 if (last == NULL)
16274 {
16275 HOST_WIDE_INT offset = 0;
16276 if (GET_CODE (varloc) == VAR_LOCATION
16277 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16278 {
16279 varloc = PAT_VAR_LOCATION_LOC (varloc);
16280 if (GET_CODE (varloc) == EXPR_LIST)
16281 varloc = XEXP (varloc, 0);
16282 }
16283 do
16284 {
16285 if (GET_CODE (varloc) == CONST
16286 || GET_CODE (varloc) == SIGN_EXTEND
16287 || GET_CODE (varloc) == ZERO_EXTEND)
16288 varloc = XEXP (varloc, 0);
16289 else if (GET_CODE (varloc) == SUBREG)
16290 varloc = SUBREG_REG (varloc);
16291 else
16292 break;
16293 }
16294 while (1);
16295 /* DW_OP_bit_size offset should be zero for register
16296 or implicit location descriptions and empty location
16297 descriptions, but for memory addresses needs big endian
16298 adjustment. */
16299 if (MEM_P (varloc))
16300 {
16301 unsigned HOST_WIDE_INT memsize
16302 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16303 if (memsize != bitsize)
16304 {
16305 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16306 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16307 goto discard_descr;
16308 if (memsize < bitsize)
16309 goto discard_descr;
16310 if (BITS_BIG_ENDIAN)
16311 offset = memsize - bitsize;
16312 }
16313 }
16314
16315 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16316 if (*descr_tail == NULL)
16317 goto discard_descr;
16318 descr_tail = &(*descr_tail)->dw_loc_next;
16319 }
16320 }
16321
16322 /* If there were any non-empty expressions, add padding till the end of
16323 the decl. */
16324 if (descr != NULL && decl_size != 0)
16325 {
16326 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16327 if (*descr_tail == NULL)
16328 goto discard_descr;
16329 }
16330 return descr;
16331
16332 discard_descr:
16333 /* Discard the descriptor and release any addr_table entries it uses. */
16334 remove_loc_list_addr_table_entries (descr);
16335 return NULL;
16336 }
16337
16338 /* Return the dwarf representation of the location list LOC_LIST of
16339 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16340 function. */
16341
16342 static dw_loc_list_ref
16343 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16344 {
16345 const char *endname, *secname;
16346 rtx varloc;
16347 enum var_init_status initialized;
16348 struct var_loc_node *node;
16349 dw_loc_descr_ref descr;
16350 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16351 dw_loc_list_ref list = NULL;
16352 dw_loc_list_ref *listp = &list;
16353
16354 /* Now that we know what section we are using for a base,
16355 actually construct the list of locations.
16356 The first location information is what is passed to the
16357 function that creates the location list, and the remaining
16358 locations just get added on to that list.
16359 Note that we only know the start address for a location
16360 (IE location changes), so to build the range, we use
16361 the range [current location start, next location start].
16362 This means we have to special case the last node, and generate
16363 a range of [last location start, end of function label]. */
16364
16365 if (cfun && crtl->has_bb_partition)
16366 {
16367 bool save_in_cold_section_p = in_cold_section_p;
16368 in_cold_section_p = first_function_block_is_cold;
16369 if (loc_list->last_before_switch == NULL)
16370 in_cold_section_p = !in_cold_section_p;
16371 secname = secname_for_decl (decl);
16372 in_cold_section_p = save_in_cold_section_p;
16373 }
16374 else
16375 secname = secname_for_decl (decl);
16376
16377 for (node = loc_list->first; node; node = node->next)
16378 {
16379 bool range_across_switch = false;
16380 if (GET_CODE (node->loc) == EXPR_LIST
16381 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16382 {
16383 if (GET_CODE (node->loc) == EXPR_LIST)
16384 {
16385 descr = NULL;
16386 /* This requires DW_OP_{,bit_}piece, which is not usable
16387 inside DWARF expressions. */
16388 if (want_address == 2)
16389 descr = dw_sra_loc_expr (decl, node->loc);
16390 }
16391 else
16392 {
16393 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16394 varloc = NOTE_VAR_LOCATION (node->loc);
16395 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16396 }
16397 if (descr)
16398 {
16399 /* If section switch happens in between node->label
16400 and node->next->label (or end of function) and
16401 we can't emit it as a single entry list,
16402 emit two ranges, first one ending at the end
16403 of first partition and second one starting at the
16404 beginning of second partition. */
16405 if (node == loc_list->last_before_switch
16406 && (node != loc_list->first || loc_list->first->next)
16407 && current_function_decl)
16408 {
16409 endname = cfun->fde->dw_fde_end;
16410 range_across_switch = true;
16411 }
16412 /* The variable has a location between NODE->LABEL and
16413 NODE->NEXT->LABEL. */
16414 else if (node->next)
16415 endname = node->next->label;
16416 /* If the variable has a location at the last label
16417 it keeps its location until the end of function. */
16418 else if (!current_function_decl)
16419 endname = text_end_label;
16420 else
16421 {
16422 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16423 current_function_funcdef_no);
16424 endname = ggc_strdup (label_id);
16425 }
16426
16427 *listp = new_loc_list (descr, node->label, endname, secname);
16428 if (TREE_CODE (decl) == PARM_DECL
16429 && node == loc_list->first
16430 && NOTE_P (node->loc)
16431 && strcmp (node->label, endname) == 0)
16432 (*listp)->force = true;
16433 listp = &(*listp)->dw_loc_next;
16434 }
16435 }
16436
16437 if (cfun
16438 && crtl->has_bb_partition
16439 && node == loc_list->last_before_switch)
16440 {
16441 bool save_in_cold_section_p = in_cold_section_p;
16442 in_cold_section_p = !first_function_block_is_cold;
16443 secname = secname_for_decl (decl);
16444 in_cold_section_p = save_in_cold_section_p;
16445 }
16446
16447 if (range_across_switch)
16448 {
16449 if (GET_CODE (node->loc) == EXPR_LIST)
16450 descr = dw_sra_loc_expr (decl, node->loc);
16451 else
16452 {
16453 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16454 varloc = NOTE_VAR_LOCATION (node->loc);
16455 descr = dw_loc_list_1 (decl, varloc, want_address,
16456 initialized);
16457 }
16458 gcc_assert (descr);
16459 /* The variable has a location between NODE->LABEL and
16460 NODE->NEXT->LABEL. */
16461 if (node->next)
16462 endname = node->next->label;
16463 else
16464 endname = cfun->fde->dw_fde_second_end;
16465 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin,
16466 endname, secname);
16467 listp = &(*listp)->dw_loc_next;
16468 }
16469 }
16470
16471 /* Try to avoid the overhead of a location list emitting a location
16472 expression instead, but only if we didn't have more than one
16473 location entry in the first place. If some entries were not
16474 representable, we don't want to pretend a single entry that was
16475 applies to the entire scope in which the variable is
16476 available. */
16477 if (list && loc_list->first->next)
16478 gen_llsym (list);
16479
16480 return list;
16481 }
16482
16483 /* Return if the loc_list has only single element and thus can be represented
16484 as location description. */
16485
16486 static bool
16487 single_element_loc_list_p (dw_loc_list_ref list)
16488 {
16489 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16490 return !list->ll_symbol;
16491 }
16492
16493 /* Duplicate a single element of location list. */
16494
16495 static inline dw_loc_descr_ref
16496 copy_loc_descr (dw_loc_descr_ref ref)
16497 {
16498 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16499 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16500 return copy;
16501 }
16502
16503 /* To each location in list LIST append loc descr REF. */
16504
16505 static void
16506 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16507 {
16508 dw_loc_descr_ref copy;
16509 add_loc_descr (&list->expr, ref);
16510 list = list->dw_loc_next;
16511 while (list)
16512 {
16513 copy = copy_loc_descr (ref);
16514 add_loc_descr (&list->expr, copy);
16515 while (copy->dw_loc_next)
16516 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16517 list = list->dw_loc_next;
16518 }
16519 }
16520
16521 /* To each location in list LIST prepend loc descr REF. */
16522
16523 static void
16524 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16525 {
16526 dw_loc_descr_ref copy;
16527 dw_loc_descr_ref ref_end = list->expr;
16528 add_loc_descr (&ref, list->expr);
16529 list->expr = ref;
16530 list = list->dw_loc_next;
16531 while (list)
16532 {
16533 dw_loc_descr_ref end = list->expr;
16534 list->expr = copy = copy_loc_descr (ref);
16535 while (copy->dw_loc_next != ref_end)
16536 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16537 copy->dw_loc_next = end;
16538 list = list->dw_loc_next;
16539 }
16540 }
16541
16542 /* Given two lists RET and LIST
16543 produce location list that is result of adding expression in LIST
16544 to expression in RET on each position in program.
16545 Might be destructive on both RET and LIST.
16546
16547 TODO: We handle only simple cases of RET or LIST having at most one
16548 element. General case would involve sorting the lists in program order
16549 and merging them that will need some additional work.
16550 Adding that will improve quality of debug info especially for SRA-ed
16551 structures. */
16552
16553 static void
16554 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16555 {
16556 if (!list)
16557 return;
16558 if (!*ret)
16559 {
16560 *ret = list;
16561 return;
16562 }
16563 if (!list->dw_loc_next)
16564 {
16565 add_loc_descr_to_each (*ret, list->expr);
16566 return;
16567 }
16568 if (!(*ret)->dw_loc_next)
16569 {
16570 prepend_loc_descr_to_each (list, (*ret)->expr);
16571 *ret = list;
16572 return;
16573 }
16574 expansion_failed (NULL_TREE, NULL_RTX,
16575 "Don't know how to merge two non-trivial"
16576 " location lists.\n");
16577 *ret = NULL;
16578 return;
16579 }
16580
16581 /* LOC is constant expression. Try a luck, look it up in constant
16582 pool and return its loc_descr of its address. */
16583
16584 static dw_loc_descr_ref
16585 cst_pool_loc_descr (tree loc)
16586 {
16587 /* Get an RTL for this, if something has been emitted. */
16588 rtx rtl = lookup_constant_def (loc);
16589
16590 if (!rtl || !MEM_P (rtl))
16591 {
16592 gcc_assert (!rtl);
16593 return 0;
16594 }
16595 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16596
16597 /* TODO: We might get more coverage if we was actually delaying expansion
16598 of all expressions till end of compilation when constant pools are fully
16599 populated. */
16600 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16601 {
16602 expansion_failed (loc, NULL_RTX,
16603 "CST value in contant pool but not marked.");
16604 return 0;
16605 }
16606 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16607 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16608 }
16609
16610 /* Return dw_loc_list representing address of addr_expr LOC
16611 by looking for inner INDIRECT_REF expression and turning
16612 it into simple arithmetics.
16613
16614 See loc_list_from_tree for the meaning of CONTEXT. */
16615
16616 static dw_loc_list_ref
16617 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16618 loc_descr_context *context)
16619 {
16620 tree obj, offset;
16621 HOST_WIDE_INT bitsize, bitpos, bytepos;
16622 machine_mode mode;
16623 int unsignedp, reversep, volatilep = 0;
16624 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16625
16626 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16627 &bitsize, &bitpos, &offset, &mode,
16628 &unsignedp, &reversep, &volatilep);
16629 STRIP_NOPS (obj);
16630 if (bitpos % BITS_PER_UNIT)
16631 {
16632 expansion_failed (loc, NULL_RTX, "bitfield access");
16633 return 0;
16634 }
16635 if (!INDIRECT_REF_P (obj))
16636 {
16637 expansion_failed (obj,
16638 NULL_RTX, "no indirect ref in inner refrence");
16639 return 0;
16640 }
16641 if (!offset && !bitpos)
16642 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16643 context);
16644 else if (toplev
16645 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16646 && (dwarf_version >= 4 || !dwarf_strict))
16647 {
16648 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16649 if (!list_ret)
16650 return 0;
16651 if (offset)
16652 {
16653 /* Variable offset. */
16654 list_ret1 = loc_list_from_tree (offset, 0, context);
16655 if (list_ret1 == 0)
16656 return 0;
16657 add_loc_list (&list_ret, list_ret1);
16658 if (!list_ret)
16659 return 0;
16660 add_loc_descr_to_each (list_ret,
16661 new_loc_descr (DW_OP_plus, 0, 0));
16662 }
16663 bytepos = bitpos / BITS_PER_UNIT;
16664 if (bytepos > 0)
16665 add_loc_descr_to_each (list_ret,
16666 new_loc_descr (DW_OP_plus_uconst,
16667 bytepos, 0));
16668 else if (bytepos < 0)
16669 loc_list_plus_const (list_ret, bytepos);
16670 add_loc_descr_to_each (list_ret,
16671 new_loc_descr (DW_OP_stack_value, 0, 0));
16672 }
16673 return list_ret;
16674 }
16675
16676 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16677 all operations from LOC are nops, move to the last one. Insert in NOPS all
16678 operations that are skipped. */
16679
16680 static void
16681 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16682 hash_set<dw_loc_descr_ref> &nops)
16683 {
16684 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16685 {
16686 nops.add (loc);
16687 loc = loc->dw_loc_next;
16688 }
16689 }
16690
16691 /* Helper for loc_descr_without_nops: free the location description operation
16692 P. */
16693
16694 bool
16695 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16696 {
16697 ggc_free (loc);
16698 return true;
16699 }
16700
16701 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16702 finishes LOC. */
16703
16704 static void
16705 loc_descr_without_nops (dw_loc_descr_ref &loc)
16706 {
16707 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16708 return;
16709
16710 /* Set of all DW_OP_nop operations we remove. */
16711 hash_set<dw_loc_descr_ref> nops;
16712
16713 /* First, strip all prefix NOP operations in order to keep the head of the
16714 operations list. */
16715 loc_descr_to_next_no_nop (loc, nops);
16716
16717 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16718 {
16719 /* For control flow operations: strip "prefix" nops in destination
16720 labels. */
16721 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16722 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16723 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16724 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16725
16726 /* Do the same for the operations that follow, then move to the next
16727 iteration. */
16728 if (cur->dw_loc_next != NULL)
16729 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16730 cur = cur->dw_loc_next;
16731 }
16732
16733 nops.traverse<void *, free_loc_descr> (NULL);
16734 }
16735
16736
16737 struct dwarf_procedure_info;
16738
16739 /* Helper structure for location descriptions generation. */
16740 struct loc_descr_context
16741 {
16742 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16743 NULL_TREE if DW_OP_push_object_address in invalid for this location
16744 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16745 tree context_type;
16746 /* The ..._DECL node that should be translated as a
16747 DW_OP_push_object_address operation. */
16748 tree base_decl;
16749 /* Information about the DWARF procedure we are currently generating. NULL if
16750 we are not generating a DWARF procedure. */
16751 struct dwarf_procedure_info *dpi;
16752 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16753 by consumer. Used for DW_TAG_generic_subrange attributes. */
16754 bool placeholder_arg;
16755 /* True if PLACEHOLDER_EXPR has been seen. */
16756 bool placeholder_seen;
16757 };
16758
16759 /* DWARF procedures generation
16760
16761 DWARF expressions (aka. location descriptions) are used to encode variable
16762 things such as sizes or offsets. Such computations can have redundant parts
16763 that can be factorized in order to reduce the size of the output debug
16764 information. This is the whole point of DWARF procedures.
16765
16766 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16767 already factorized into functions ("size functions") in order to handle very
16768 big and complex types. Such functions are quite simple: they have integral
16769 arguments, they return an integral result and their body contains only a
16770 return statement with arithmetic expressions. This is the only kind of
16771 function we are interested in translating into DWARF procedures, here.
16772
16773 DWARF expressions and DWARF procedure are executed using a stack, so we have
16774 to define some calling convention for them to interact. Let's say that:
16775
16776 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16777 all arguments in reverse order (right-to-left) so that when the DWARF
16778 procedure execution starts, the first argument is the top of the stack.
16779
16780 - Then, when returning, the DWARF procedure must have consumed all arguments
16781 on the stack, must have pushed the result and touched nothing else.
16782
16783 - Each integral argument and the result are integral types can be hold in a
16784 single stack slot.
16785
16786 - We call "frame offset" the number of stack slots that are "under DWARF
16787 procedure control": it includes the arguments slots, the temporaries and
16788 the result slot. Thus, it is equal to the number of arguments when the
16789 procedure execution starts and must be equal to one (the result) when it
16790 returns. */
16791
16792 /* Helper structure used when generating operations for a DWARF procedure. */
16793 struct dwarf_procedure_info
16794 {
16795 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16796 currently translated. */
16797 tree fndecl;
16798 /* The number of arguments FNDECL takes. */
16799 unsigned args_count;
16800 };
16801
16802 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16803 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16804 equate it to this DIE. */
16805
16806 static dw_die_ref
16807 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16808 dw_die_ref parent_die)
16809 {
16810 dw_die_ref dwarf_proc_die;
16811
16812 if ((dwarf_version < 3 && dwarf_strict)
16813 || location == NULL)
16814 return NULL;
16815
16816 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16817 if (fndecl)
16818 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16819 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16820 return dwarf_proc_die;
16821 }
16822
16823 /* Return whether TYPE is a supported type as a DWARF procedure argument
16824 type or return type (we handle only scalar types and pointer types that
16825 aren't wider than the DWARF expression evaluation stack. */
16826
16827 static bool
16828 is_handled_procedure_type (tree type)
16829 {
16830 return ((INTEGRAL_TYPE_P (type)
16831 || TREE_CODE (type) == OFFSET_TYPE
16832 || TREE_CODE (type) == POINTER_TYPE)
16833 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16834 }
16835
16836 /* Helper for resolve_args_picking: do the same but stop when coming across
16837 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16838 offset *before* evaluating the corresponding operation. */
16839
16840 static bool
16841 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16842 struct dwarf_procedure_info *dpi,
16843 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16844 {
16845 /* The "frame_offset" identifier is already used to name a macro... */
16846 unsigned frame_offset_ = initial_frame_offset;
16847 dw_loc_descr_ref l;
16848
16849 for (l = loc; l != NULL;)
16850 {
16851 bool existed;
16852 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16853
16854 /* If we already met this node, there is nothing to compute anymore. */
16855 if (existed)
16856 {
16857 /* Make sure that the stack size is consistent wherever the execution
16858 flow comes from. */
16859 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16860 break;
16861 }
16862 l_frame_offset = frame_offset_;
16863
16864 /* If needed, relocate the picking offset with respect to the frame
16865 offset. */
16866 if (l->frame_offset_rel)
16867 {
16868 unsigned HOST_WIDE_INT off;
16869 switch (l->dw_loc_opc)
16870 {
16871 case DW_OP_pick:
16872 off = l->dw_loc_oprnd1.v.val_unsigned;
16873 break;
16874 case DW_OP_dup:
16875 off = 0;
16876 break;
16877 case DW_OP_over:
16878 off = 1;
16879 break;
16880 default:
16881 gcc_unreachable ();
16882 }
16883 /* frame_offset_ is the size of the current stack frame, including
16884 incoming arguments. Besides, the arguments are pushed
16885 right-to-left. Thus, in order to access the Nth argument from
16886 this operation node, the picking has to skip temporaries *plus*
16887 one stack slot per argument (0 for the first one, 1 for the second
16888 one, etc.).
16889
16890 The targetted argument number (N) is already set as the operand,
16891 and the number of temporaries can be computed with:
16892 frame_offsets_ - dpi->args_count */
16893 off += frame_offset_ - dpi->args_count;
16894
16895 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16896 if (off > 255)
16897 return false;
16898
16899 if (off == 0)
16900 {
16901 l->dw_loc_opc = DW_OP_dup;
16902 l->dw_loc_oprnd1.v.val_unsigned = 0;
16903 }
16904 else if (off == 1)
16905 {
16906 l->dw_loc_opc = DW_OP_over;
16907 l->dw_loc_oprnd1.v.val_unsigned = 0;
16908 }
16909 else
16910 {
16911 l->dw_loc_opc = DW_OP_pick;
16912 l->dw_loc_oprnd1.v.val_unsigned = off;
16913 }
16914 }
16915
16916 /* Update frame_offset according to the effect the current operation has
16917 on the stack. */
16918 switch (l->dw_loc_opc)
16919 {
16920 case DW_OP_deref:
16921 case DW_OP_swap:
16922 case DW_OP_rot:
16923 case DW_OP_abs:
16924 case DW_OP_neg:
16925 case DW_OP_not:
16926 case DW_OP_plus_uconst:
16927 case DW_OP_skip:
16928 case DW_OP_reg0:
16929 case DW_OP_reg1:
16930 case DW_OP_reg2:
16931 case DW_OP_reg3:
16932 case DW_OP_reg4:
16933 case DW_OP_reg5:
16934 case DW_OP_reg6:
16935 case DW_OP_reg7:
16936 case DW_OP_reg8:
16937 case DW_OP_reg9:
16938 case DW_OP_reg10:
16939 case DW_OP_reg11:
16940 case DW_OP_reg12:
16941 case DW_OP_reg13:
16942 case DW_OP_reg14:
16943 case DW_OP_reg15:
16944 case DW_OP_reg16:
16945 case DW_OP_reg17:
16946 case DW_OP_reg18:
16947 case DW_OP_reg19:
16948 case DW_OP_reg20:
16949 case DW_OP_reg21:
16950 case DW_OP_reg22:
16951 case DW_OP_reg23:
16952 case DW_OP_reg24:
16953 case DW_OP_reg25:
16954 case DW_OP_reg26:
16955 case DW_OP_reg27:
16956 case DW_OP_reg28:
16957 case DW_OP_reg29:
16958 case DW_OP_reg30:
16959 case DW_OP_reg31:
16960 case DW_OP_bregx:
16961 case DW_OP_piece:
16962 case DW_OP_deref_size:
16963 case DW_OP_nop:
16964 case DW_OP_bit_piece:
16965 case DW_OP_implicit_value:
16966 case DW_OP_stack_value:
16967 break;
16968
16969 case DW_OP_addr:
16970 case DW_OP_const1u:
16971 case DW_OP_const1s:
16972 case DW_OP_const2u:
16973 case DW_OP_const2s:
16974 case DW_OP_const4u:
16975 case DW_OP_const4s:
16976 case DW_OP_const8u:
16977 case DW_OP_const8s:
16978 case DW_OP_constu:
16979 case DW_OP_consts:
16980 case DW_OP_dup:
16981 case DW_OP_over:
16982 case DW_OP_pick:
16983 case DW_OP_lit0:
16984 case DW_OP_lit1:
16985 case DW_OP_lit2:
16986 case DW_OP_lit3:
16987 case DW_OP_lit4:
16988 case DW_OP_lit5:
16989 case DW_OP_lit6:
16990 case DW_OP_lit7:
16991 case DW_OP_lit8:
16992 case DW_OP_lit9:
16993 case DW_OP_lit10:
16994 case DW_OP_lit11:
16995 case DW_OP_lit12:
16996 case DW_OP_lit13:
16997 case DW_OP_lit14:
16998 case DW_OP_lit15:
16999 case DW_OP_lit16:
17000 case DW_OP_lit17:
17001 case DW_OP_lit18:
17002 case DW_OP_lit19:
17003 case DW_OP_lit20:
17004 case DW_OP_lit21:
17005 case DW_OP_lit22:
17006 case DW_OP_lit23:
17007 case DW_OP_lit24:
17008 case DW_OP_lit25:
17009 case DW_OP_lit26:
17010 case DW_OP_lit27:
17011 case DW_OP_lit28:
17012 case DW_OP_lit29:
17013 case DW_OP_lit30:
17014 case DW_OP_lit31:
17015 case DW_OP_breg0:
17016 case DW_OP_breg1:
17017 case DW_OP_breg2:
17018 case DW_OP_breg3:
17019 case DW_OP_breg4:
17020 case DW_OP_breg5:
17021 case DW_OP_breg6:
17022 case DW_OP_breg7:
17023 case DW_OP_breg8:
17024 case DW_OP_breg9:
17025 case DW_OP_breg10:
17026 case DW_OP_breg11:
17027 case DW_OP_breg12:
17028 case DW_OP_breg13:
17029 case DW_OP_breg14:
17030 case DW_OP_breg15:
17031 case DW_OP_breg16:
17032 case DW_OP_breg17:
17033 case DW_OP_breg18:
17034 case DW_OP_breg19:
17035 case DW_OP_breg20:
17036 case DW_OP_breg21:
17037 case DW_OP_breg22:
17038 case DW_OP_breg23:
17039 case DW_OP_breg24:
17040 case DW_OP_breg25:
17041 case DW_OP_breg26:
17042 case DW_OP_breg27:
17043 case DW_OP_breg28:
17044 case DW_OP_breg29:
17045 case DW_OP_breg30:
17046 case DW_OP_breg31:
17047 case DW_OP_fbreg:
17048 case DW_OP_push_object_address:
17049 case DW_OP_call_frame_cfa:
17050 case DW_OP_GNU_variable_value:
17051 ++frame_offset_;
17052 break;
17053
17054 case DW_OP_drop:
17055 case DW_OP_xderef:
17056 case DW_OP_and:
17057 case DW_OP_div:
17058 case DW_OP_minus:
17059 case DW_OP_mod:
17060 case DW_OP_mul:
17061 case DW_OP_or:
17062 case DW_OP_plus:
17063 case DW_OP_shl:
17064 case DW_OP_shr:
17065 case DW_OP_shra:
17066 case DW_OP_xor:
17067 case DW_OP_bra:
17068 case DW_OP_eq:
17069 case DW_OP_ge:
17070 case DW_OP_gt:
17071 case DW_OP_le:
17072 case DW_OP_lt:
17073 case DW_OP_ne:
17074 case DW_OP_regx:
17075 case DW_OP_xderef_size:
17076 --frame_offset_;
17077 break;
17078
17079 case DW_OP_call2:
17080 case DW_OP_call4:
17081 case DW_OP_call_ref:
17082 {
17083 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17084 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17085
17086 if (stack_usage == NULL)
17087 return false;
17088 frame_offset_ += *stack_usage;
17089 break;
17090 }
17091
17092 case DW_OP_implicit_pointer:
17093 case DW_OP_entry_value:
17094 case DW_OP_const_type:
17095 case DW_OP_regval_type:
17096 case DW_OP_deref_type:
17097 case DW_OP_convert:
17098 case DW_OP_reinterpret:
17099 case DW_OP_form_tls_address:
17100 case DW_OP_GNU_push_tls_address:
17101 case DW_OP_GNU_uninit:
17102 case DW_OP_GNU_encoded_addr:
17103 case DW_OP_GNU_implicit_pointer:
17104 case DW_OP_GNU_entry_value:
17105 case DW_OP_GNU_const_type:
17106 case DW_OP_GNU_regval_type:
17107 case DW_OP_GNU_deref_type:
17108 case DW_OP_GNU_convert:
17109 case DW_OP_GNU_reinterpret:
17110 case DW_OP_GNU_parameter_ref:
17111 /* loc_list_from_tree will probably not output these operations for
17112 size functions, so assume they will not appear here. */
17113 /* Fall through... */
17114
17115 default:
17116 gcc_unreachable ();
17117 }
17118
17119 /* Now, follow the control flow (except subroutine calls). */
17120 switch (l->dw_loc_opc)
17121 {
17122 case DW_OP_bra:
17123 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17124 frame_offsets))
17125 return false;
17126 /* Fall through. */
17127
17128 case DW_OP_skip:
17129 l = l->dw_loc_oprnd1.v.val_loc;
17130 break;
17131
17132 case DW_OP_stack_value:
17133 return true;
17134
17135 default:
17136 l = l->dw_loc_next;
17137 break;
17138 }
17139 }
17140
17141 return true;
17142 }
17143
17144 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17145 operations) in order to resolve the operand of DW_OP_pick operations that
17146 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17147 offset *before* LOC is executed. Return if all relocations were
17148 successful. */
17149
17150 static bool
17151 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17152 struct dwarf_procedure_info *dpi)
17153 {
17154 /* Associate to all visited operations the frame offset *before* evaluating
17155 this operation. */
17156 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17157
17158 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17159 frame_offsets);
17160 }
17161
17162 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17163 Return NULL if it is not possible. */
17164
17165 static dw_die_ref
17166 function_to_dwarf_procedure (tree fndecl)
17167 {
17168 struct loc_descr_context ctx;
17169 struct dwarf_procedure_info dpi;
17170 dw_die_ref dwarf_proc_die;
17171 tree tree_body = DECL_SAVED_TREE (fndecl);
17172 dw_loc_descr_ref loc_body, epilogue;
17173
17174 tree cursor;
17175 unsigned i;
17176
17177 /* Do not generate multiple DWARF procedures for the same function
17178 declaration. */
17179 dwarf_proc_die = lookup_decl_die (fndecl);
17180 if (dwarf_proc_die != NULL)
17181 return dwarf_proc_die;
17182
17183 /* DWARF procedures are available starting with the DWARFv3 standard. */
17184 if (dwarf_version < 3 && dwarf_strict)
17185 return NULL;
17186
17187 /* We handle only functions for which we still have a body, that return a
17188 supported type and that takes arguments with supported types. Note that
17189 there is no point translating functions that return nothing. */
17190 if (tree_body == NULL_TREE
17191 || DECL_RESULT (fndecl) == NULL_TREE
17192 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17193 return NULL;
17194
17195 for (cursor = DECL_ARGUMENTS (fndecl);
17196 cursor != NULL_TREE;
17197 cursor = TREE_CHAIN (cursor))
17198 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17199 return NULL;
17200
17201 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17202 if (TREE_CODE (tree_body) != RETURN_EXPR)
17203 return NULL;
17204 tree_body = TREE_OPERAND (tree_body, 0);
17205 if (TREE_CODE (tree_body) != MODIFY_EXPR
17206 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17207 return NULL;
17208 tree_body = TREE_OPERAND (tree_body, 1);
17209
17210 /* Try to translate the body expression itself. Note that this will probably
17211 cause an infinite recursion if its call graph has a cycle. This is very
17212 unlikely for size functions, however, so don't bother with such things at
17213 the moment. */
17214 ctx.context_type = NULL_TREE;
17215 ctx.base_decl = NULL_TREE;
17216 ctx.dpi = &dpi;
17217 ctx.placeholder_arg = false;
17218 ctx.placeholder_seen = false;
17219 dpi.fndecl = fndecl;
17220 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17221 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17222 if (!loc_body)
17223 return NULL;
17224
17225 /* After evaluating all operands in "loc_body", we should still have on the
17226 stack all arguments plus the desired function result (top of the stack).
17227 Generate code in order to keep only the result in our stack frame. */
17228 epilogue = NULL;
17229 for (i = 0; i < dpi.args_count; ++i)
17230 {
17231 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17232 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17233 op_couple->dw_loc_next->dw_loc_next = epilogue;
17234 epilogue = op_couple;
17235 }
17236 add_loc_descr (&loc_body, epilogue);
17237 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17238 return NULL;
17239
17240 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17241 because they are considered useful. Now there is an epilogue, they are
17242 not anymore, so give it another try. */
17243 loc_descr_without_nops (loc_body);
17244
17245 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17246 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17247 though, given that size functions do not come from source, so they should
17248 not have a dedicated DW_TAG_subprogram DIE. */
17249 dwarf_proc_die
17250 = new_dwarf_proc_die (loc_body, fndecl,
17251 get_context_die (DECL_CONTEXT (fndecl)));
17252
17253 /* The called DWARF procedure consumes one stack slot per argument and
17254 returns one stack slot. */
17255 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17256
17257 return dwarf_proc_die;
17258 }
17259
17260
17261 /* Generate Dwarf location list representing LOC.
17262 If WANT_ADDRESS is false, expression computing LOC will be computed
17263 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17264 if WANT_ADDRESS is 2, expression computing address useable in location
17265 will be returned (i.e. DW_OP_reg can be used
17266 to refer to register values).
17267
17268 CONTEXT provides information to customize the location descriptions
17269 generation. Its context_type field specifies what type is implicitly
17270 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17271 will not be generated.
17272
17273 Its DPI field determines whether we are generating a DWARF expression for a
17274 DWARF procedure, so PARM_DECL references are processed specifically.
17275
17276 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17277 and dpi fields were null. */
17278
17279 static dw_loc_list_ref
17280 loc_list_from_tree_1 (tree loc, int want_address,
17281 struct loc_descr_context *context)
17282 {
17283 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17284 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17285 int have_address = 0;
17286 enum dwarf_location_atom op;
17287
17288 /* ??? Most of the time we do not take proper care for sign/zero
17289 extending the values properly. Hopefully this won't be a real
17290 problem... */
17291
17292 if (context != NULL
17293 && context->base_decl == loc
17294 && want_address == 0)
17295 {
17296 if (dwarf_version >= 3 || !dwarf_strict)
17297 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17298 NULL, NULL, NULL);
17299 else
17300 return NULL;
17301 }
17302
17303 switch (TREE_CODE (loc))
17304 {
17305 case ERROR_MARK:
17306 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17307 return 0;
17308
17309 case PLACEHOLDER_EXPR:
17310 /* This case involves extracting fields from an object to determine the
17311 position of other fields. It is supposed to appear only as the first
17312 operand of COMPONENT_REF nodes and to reference precisely the type
17313 that the context allows. */
17314 if (context != NULL
17315 && TREE_TYPE (loc) == context->context_type
17316 && want_address >= 1)
17317 {
17318 if (dwarf_version >= 3 || !dwarf_strict)
17319 {
17320 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17321 have_address = 1;
17322 break;
17323 }
17324 else
17325 return NULL;
17326 }
17327 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17328 the single argument passed by consumer. */
17329 else if (context != NULL
17330 && context->placeholder_arg
17331 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17332 && want_address == 0)
17333 {
17334 ret = new_loc_descr (DW_OP_pick, 0, 0);
17335 ret->frame_offset_rel = 1;
17336 context->placeholder_seen = true;
17337 break;
17338 }
17339 else
17340 expansion_failed (loc, NULL_RTX,
17341 "PLACEHOLDER_EXPR for an unexpected type");
17342 break;
17343
17344 case CALL_EXPR:
17345 {
17346 const int nargs = call_expr_nargs (loc);
17347 tree callee = get_callee_fndecl (loc);
17348 int i;
17349 dw_die_ref dwarf_proc;
17350
17351 if (callee == NULL_TREE)
17352 goto call_expansion_failed;
17353
17354 /* We handle only functions that return an integer. */
17355 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17356 goto call_expansion_failed;
17357
17358 dwarf_proc = function_to_dwarf_procedure (callee);
17359 if (dwarf_proc == NULL)
17360 goto call_expansion_failed;
17361
17362 /* Evaluate arguments right-to-left so that the first argument will
17363 be the top-most one on the stack. */
17364 for (i = nargs - 1; i >= 0; --i)
17365 {
17366 dw_loc_descr_ref loc_descr
17367 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17368 context);
17369
17370 if (loc_descr == NULL)
17371 goto call_expansion_failed;
17372
17373 add_loc_descr (&ret, loc_descr);
17374 }
17375
17376 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17377 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17378 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17379 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17380 add_loc_descr (&ret, ret1);
17381 break;
17382
17383 call_expansion_failed:
17384 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17385 /* There are no opcodes for these operations. */
17386 return 0;
17387 }
17388
17389 case PREINCREMENT_EXPR:
17390 case PREDECREMENT_EXPR:
17391 case POSTINCREMENT_EXPR:
17392 case POSTDECREMENT_EXPR:
17393 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17394 /* There are no opcodes for these operations. */
17395 return 0;
17396
17397 case ADDR_EXPR:
17398 /* If we already want an address, see if there is INDIRECT_REF inside
17399 e.g. for &this->field. */
17400 if (want_address)
17401 {
17402 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17403 (loc, want_address == 2, context);
17404 if (list_ret)
17405 have_address = 1;
17406 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17407 && (ret = cst_pool_loc_descr (loc)))
17408 have_address = 1;
17409 }
17410 /* Otherwise, process the argument and look for the address. */
17411 if (!list_ret && !ret)
17412 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17413 else
17414 {
17415 if (want_address)
17416 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17417 return NULL;
17418 }
17419 break;
17420
17421 case VAR_DECL:
17422 if (DECL_THREAD_LOCAL_P (loc))
17423 {
17424 rtx rtl;
17425 enum dwarf_location_atom tls_op;
17426 enum dtprel_bool dtprel = dtprel_false;
17427
17428 if (targetm.have_tls)
17429 {
17430 /* If this is not defined, we have no way to emit the
17431 data. */
17432 if (!targetm.asm_out.output_dwarf_dtprel)
17433 return 0;
17434
17435 /* The way DW_OP_GNU_push_tls_address is specified, we
17436 can only look up addresses of objects in the current
17437 module. We used DW_OP_addr as first op, but that's
17438 wrong, because DW_OP_addr is relocated by the debug
17439 info consumer, while DW_OP_GNU_push_tls_address
17440 operand shouldn't be. */
17441 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17442 return 0;
17443 dtprel = dtprel_true;
17444 /* We check for DWARF 5 here because gdb did not implement
17445 DW_OP_form_tls_address until after 7.12. */
17446 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17447 : DW_OP_GNU_push_tls_address);
17448 }
17449 else
17450 {
17451 if (!targetm.emutls.debug_form_tls_address
17452 || !(dwarf_version >= 3 || !dwarf_strict))
17453 return 0;
17454 /* We stuffed the control variable into the DECL_VALUE_EXPR
17455 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17456 no longer appear in gimple code. We used the control
17457 variable in specific so that we could pick it up here. */
17458 loc = DECL_VALUE_EXPR (loc);
17459 tls_op = DW_OP_form_tls_address;
17460 }
17461
17462 rtl = rtl_for_decl_location (loc);
17463 if (rtl == NULL_RTX)
17464 return 0;
17465
17466 if (!MEM_P (rtl))
17467 return 0;
17468 rtl = XEXP (rtl, 0);
17469 if (! CONSTANT_P (rtl))
17470 return 0;
17471
17472 ret = new_addr_loc_descr (rtl, dtprel);
17473 ret1 = new_loc_descr (tls_op, 0, 0);
17474 add_loc_descr (&ret, ret1);
17475
17476 have_address = 1;
17477 break;
17478 }
17479 /* FALLTHRU */
17480
17481 case PARM_DECL:
17482 if (context != NULL && context->dpi != NULL
17483 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17484 {
17485 /* We are generating code for a DWARF procedure and we want to access
17486 one of its arguments: find the appropriate argument offset and let
17487 the resolve_args_picking pass compute the offset that complies
17488 with the stack frame size. */
17489 unsigned i = 0;
17490 tree cursor;
17491
17492 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17493 cursor != NULL_TREE && cursor != loc;
17494 cursor = TREE_CHAIN (cursor), ++i)
17495 ;
17496 /* If we are translating a DWARF procedure, all referenced parameters
17497 must belong to the current function. */
17498 gcc_assert (cursor != NULL_TREE);
17499
17500 ret = new_loc_descr (DW_OP_pick, i, 0);
17501 ret->frame_offset_rel = 1;
17502 break;
17503 }
17504 /* FALLTHRU */
17505
17506 case RESULT_DECL:
17507 if (DECL_HAS_VALUE_EXPR_P (loc))
17508 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17509 want_address, context);
17510 /* FALLTHRU */
17511
17512 case FUNCTION_DECL:
17513 {
17514 rtx rtl;
17515 var_loc_list *loc_list = lookup_decl_loc (loc);
17516
17517 if (loc_list && loc_list->first)
17518 {
17519 list_ret = dw_loc_list (loc_list, loc, want_address);
17520 have_address = want_address != 0;
17521 break;
17522 }
17523 rtl = rtl_for_decl_location (loc);
17524 if (rtl == NULL_RTX)
17525 {
17526 if (TREE_CODE (loc) != FUNCTION_DECL
17527 && early_dwarf
17528 && current_function_decl
17529 && want_address != 1
17530 && ! DECL_IGNORED_P (loc)
17531 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17532 || POINTER_TYPE_P (TREE_TYPE (loc)))
17533 && DECL_CONTEXT (loc) == current_function_decl
17534 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17535 <= DWARF2_ADDR_SIZE))
17536 {
17537 dw_die_ref ref = lookup_decl_die (loc);
17538 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17539 if (ref)
17540 {
17541 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17542 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17543 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17544 }
17545 else
17546 {
17547 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17548 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17549 }
17550 break;
17551 }
17552 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17553 return 0;
17554 }
17555 else if (CONST_INT_P (rtl))
17556 {
17557 HOST_WIDE_INT val = INTVAL (rtl);
17558 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17559 val &= GET_MODE_MASK (DECL_MODE (loc));
17560 ret = int_loc_descriptor (val);
17561 }
17562 else if (GET_CODE (rtl) == CONST_STRING)
17563 {
17564 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17565 return 0;
17566 }
17567 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17568 ret = new_addr_loc_descr (rtl, dtprel_false);
17569 else
17570 {
17571 machine_mode mode, mem_mode;
17572
17573 /* Certain constructs can only be represented at top-level. */
17574 if (want_address == 2)
17575 {
17576 ret = loc_descriptor (rtl, VOIDmode,
17577 VAR_INIT_STATUS_INITIALIZED);
17578 have_address = 1;
17579 }
17580 else
17581 {
17582 mode = GET_MODE (rtl);
17583 mem_mode = VOIDmode;
17584 if (MEM_P (rtl))
17585 {
17586 mem_mode = mode;
17587 mode = get_address_mode (rtl);
17588 rtl = XEXP (rtl, 0);
17589 have_address = 1;
17590 }
17591 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17592 VAR_INIT_STATUS_INITIALIZED);
17593 }
17594 if (!ret)
17595 expansion_failed (loc, rtl,
17596 "failed to produce loc descriptor for rtl");
17597 }
17598 }
17599 break;
17600
17601 case MEM_REF:
17602 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17603 {
17604 have_address = 1;
17605 goto do_plus;
17606 }
17607 /* Fallthru. */
17608 case INDIRECT_REF:
17609 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17610 have_address = 1;
17611 break;
17612
17613 case TARGET_MEM_REF:
17614 case SSA_NAME:
17615 case DEBUG_EXPR_DECL:
17616 return NULL;
17617
17618 case COMPOUND_EXPR:
17619 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17620 context);
17621
17622 CASE_CONVERT:
17623 case VIEW_CONVERT_EXPR:
17624 case SAVE_EXPR:
17625 case MODIFY_EXPR:
17626 case NON_LVALUE_EXPR:
17627 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17628 context);
17629
17630 case COMPONENT_REF:
17631 case BIT_FIELD_REF:
17632 case ARRAY_REF:
17633 case ARRAY_RANGE_REF:
17634 case REALPART_EXPR:
17635 case IMAGPART_EXPR:
17636 {
17637 tree obj, offset;
17638 HOST_WIDE_INT bitsize, bitpos, bytepos;
17639 machine_mode mode;
17640 int unsignedp, reversep, volatilep = 0;
17641
17642 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17643 &unsignedp, &reversep, &volatilep);
17644
17645 gcc_assert (obj != loc);
17646
17647 list_ret = loc_list_from_tree_1 (obj,
17648 want_address == 2
17649 && !bitpos && !offset ? 2 : 1,
17650 context);
17651 /* TODO: We can extract value of the small expression via shifting even
17652 for nonzero bitpos. */
17653 if (list_ret == 0)
17654 return 0;
17655 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17656 {
17657 expansion_failed (loc, NULL_RTX,
17658 "bitfield access");
17659 return 0;
17660 }
17661
17662 if (offset != NULL_TREE)
17663 {
17664 /* Variable offset. */
17665 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17666 if (list_ret1 == 0)
17667 return 0;
17668 add_loc_list (&list_ret, list_ret1);
17669 if (!list_ret)
17670 return 0;
17671 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17672 }
17673
17674 bytepos = bitpos / BITS_PER_UNIT;
17675 if (bytepos > 0)
17676 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17677 else if (bytepos < 0)
17678 loc_list_plus_const (list_ret, bytepos);
17679
17680 have_address = 1;
17681 break;
17682 }
17683
17684 case INTEGER_CST:
17685 if ((want_address || !tree_fits_shwi_p (loc))
17686 && (ret = cst_pool_loc_descr (loc)))
17687 have_address = 1;
17688 else if (want_address == 2
17689 && tree_fits_shwi_p (loc)
17690 && (ret = address_of_int_loc_descriptor
17691 (int_size_in_bytes (TREE_TYPE (loc)),
17692 tree_to_shwi (loc))))
17693 have_address = 1;
17694 else if (tree_fits_shwi_p (loc))
17695 ret = int_loc_descriptor (tree_to_shwi (loc));
17696 else if (tree_fits_uhwi_p (loc))
17697 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17698 else
17699 {
17700 expansion_failed (loc, NULL_RTX,
17701 "Integer operand is not host integer");
17702 return 0;
17703 }
17704 break;
17705
17706 case CONSTRUCTOR:
17707 case REAL_CST:
17708 case STRING_CST:
17709 case COMPLEX_CST:
17710 if ((ret = cst_pool_loc_descr (loc)))
17711 have_address = 1;
17712 else if (TREE_CODE (loc) == CONSTRUCTOR)
17713 {
17714 tree type = TREE_TYPE (loc);
17715 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17716 unsigned HOST_WIDE_INT offset = 0;
17717 unsigned HOST_WIDE_INT cnt;
17718 constructor_elt *ce;
17719
17720 if (TREE_CODE (type) == RECORD_TYPE)
17721 {
17722 /* This is very limited, but it's enough to output
17723 pointers to member functions, as long as the
17724 referenced function is defined in the current
17725 translation unit. */
17726 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17727 {
17728 tree val = ce->value;
17729
17730 tree field = ce->index;
17731
17732 if (val)
17733 STRIP_NOPS (val);
17734
17735 if (!field || DECL_BIT_FIELD (field))
17736 {
17737 expansion_failed (loc, NULL_RTX,
17738 "bitfield in record type constructor");
17739 size = offset = (unsigned HOST_WIDE_INT)-1;
17740 ret = NULL;
17741 break;
17742 }
17743
17744 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17745 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17746 gcc_assert (pos + fieldsize <= size);
17747 if (pos < offset)
17748 {
17749 expansion_failed (loc, NULL_RTX,
17750 "out-of-order fields in record constructor");
17751 size = offset = (unsigned HOST_WIDE_INT)-1;
17752 ret = NULL;
17753 break;
17754 }
17755 if (pos > offset)
17756 {
17757 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17758 add_loc_descr (&ret, ret1);
17759 offset = pos;
17760 }
17761 if (val && fieldsize != 0)
17762 {
17763 ret1 = loc_descriptor_from_tree (val, want_address, context);
17764 if (!ret1)
17765 {
17766 expansion_failed (loc, NULL_RTX,
17767 "unsupported expression in field");
17768 size = offset = (unsigned HOST_WIDE_INT)-1;
17769 ret = NULL;
17770 break;
17771 }
17772 add_loc_descr (&ret, ret1);
17773 }
17774 if (fieldsize)
17775 {
17776 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17777 add_loc_descr (&ret, ret1);
17778 offset = pos + fieldsize;
17779 }
17780 }
17781
17782 if (offset != size)
17783 {
17784 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17785 add_loc_descr (&ret, ret1);
17786 offset = size;
17787 }
17788
17789 have_address = !!want_address;
17790 }
17791 else
17792 expansion_failed (loc, NULL_RTX,
17793 "constructor of non-record type");
17794 }
17795 else
17796 /* We can construct small constants here using int_loc_descriptor. */
17797 expansion_failed (loc, NULL_RTX,
17798 "constructor or constant not in constant pool");
17799 break;
17800
17801 case TRUTH_AND_EXPR:
17802 case TRUTH_ANDIF_EXPR:
17803 case BIT_AND_EXPR:
17804 op = DW_OP_and;
17805 goto do_binop;
17806
17807 case TRUTH_XOR_EXPR:
17808 case BIT_XOR_EXPR:
17809 op = DW_OP_xor;
17810 goto do_binop;
17811
17812 case TRUTH_OR_EXPR:
17813 case TRUTH_ORIF_EXPR:
17814 case BIT_IOR_EXPR:
17815 op = DW_OP_or;
17816 goto do_binop;
17817
17818 case FLOOR_DIV_EXPR:
17819 case CEIL_DIV_EXPR:
17820 case ROUND_DIV_EXPR:
17821 case TRUNC_DIV_EXPR:
17822 case EXACT_DIV_EXPR:
17823 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17824 return 0;
17825 op = DW_OP_div;
17826 goto do_binop;
17827
17828 case MINUS_EXPR:
17829 op = DW_OP_minus;
17830 goto do_binop;
17831
17832 case FLOOR_MOD_EXPR:
17833 case CEIL_MOD_EXPR:
17834 case ROUND_MOD_EXPR:
17835 case TRUNC_MOD_EXPR:
17836 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17837 {
17838 op = DW_OP_mod;
17839 goto do_binop;
17840 }
17841 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17842 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17843 if (list_ret == 0 || list_ret1 == 0)
17844 return 0;
17845
17846 add_loc_list (&list_ret, list_ret1);
17847 if (list_ret == 0)
17848 return 0;
17849 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17850 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17851 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17852 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17853 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17854 break;
17855
17856 case MULT_EXPR:
17857 op = DW_OP_mul;
17858 goto do_binop;
17859
17860 case LSHIFT_EXPR:
17861 op = DW_OP_shl;
17862 goto do_binop;
17863
17864 case RSHIFT_EXPR:
17865 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17866 goto do_binop;
17867
17868 case POINTER_PLUS_EXPR:
17869 case PLUS_EXPR:
17870 do_plus:
17871 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17872 {
17873 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17874 smarter to encode their opposite. The DW_OP_plus_uconst operation
17875 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17876 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17877 bytes, Y being the size of the operation that pushes the opposite
17878 of the addend. So let's choose the smallest representation. */
17879 const tree tree_addend = TREE_OPERAND (loc, 1);
17880 offset_int wi_addend;
17881 HOST_WIDE_INT shwi_addend;
17882 dw_loc_descr_ref loc_naddend;
17883
17884 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17885 if (list_ret == 0)
17886 return 0;
17887
17888 /* Try to get the literal to push. It is the opposite of the addend,
17889 so as we rely on wrapping during DWARF evaluation, first decode
17890 the literal as a "DWARF-sized" signed number. */
17891 wi_addend = wi::to_offset (tree_addend);
17892 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17893 shwi_addend = wi_addend.to_shwi ();
17894 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17895 ? int_loc_descriptor (-shwi_addend)
17896 : NULL;
17897
17898 if (loc_naddend != NULL
17899 && ((unsigned) size_of_uleb128 (shwi_addend)
17900 > size_of_loc_descr (loc_naddend)))
17901 {
17902 add_loc_descr_to_each (list_ret, loc_naddend);
17903 add_loc_descr_to_each (list_ret,
17904 new_loc_descr (DW_OP_minus, 0, 0));
17905 }
17906 else
17907 {
17908 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17909 {
17910 loc_naddend = loc_cur;
17911 loc_cur = loc_cur->dw_loc_next;
17912 ggc_free (loc_naddend);
17913 }
17914 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17915 }
17916 break;
17917 }
17918
17919 op = DW_OP_plus;
17920 goto do_binop;
17921
17922 case LE_EXPR:
17923 op = DW_OP_le;
17924 goto do_comp_binop;
17925
17926 case GE_EXPR:
17927 op = DW_OP_ge;
17928 goto do_comp_binop;
17929
17930 case LT_EXPR:
17931 op = DW_OP_lt;
17932 goto do_comp_binop;
17933
17934 case GT_EXPR:
17935 op = DW_OP_gt;
17936 goto do_comp_binop;
17937
17938 do_comp_binop:
17939 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17940 {
17941 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17942 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17943 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17944 TREE_CODE (loc));
17945 break;
17946 }
17947 else
17948 goto do_binop;
17949
17950 case EQ_EXPR:
17951 op = DW_OP_eq;
17952 goto do_binop;
17953
17954 case NE_EXPR:
17955 op = DW_OP_ne;
17956 goto do_binop;
17957
17958 do_binop:
17959 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17960 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17961 if (list_ret == 0 || list_ret1 == 0)
17962 return 0;
17963
17964 add_loc_list (&list_ret, list_ret1);
17965 if (list_ret == 0)
17966 return 0;
17967 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17968 break;
17969
17970 case TRUTH_NOT_EXPR:
17971 case BIT_NOT_EXPR:
17972 op = DW_OP_not;
17973 goto do_unop;
17974
17975 case ABS_EXPR:
17976 op = DW_OP_abs;
17977 goto do_unop;
17978
17979 case NEGATE_EXPR:
17980 op = DW_OP_neg;
17981 goto do_unop;
17982
17983 do_unop:
17984 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17985 if (list_ret == 0)
17986 return 0;
17987
17988 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17989 break;
17990
17991 case MIN_EXPR:
17992 case MAX_EXPR:
17993 {
17994 const enum tree_code code =
17995 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17996
17997 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17998 build2 (code, integer_type_node,
17999 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18000 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18001 }
18002
18003 /* fall through */
18004
18005 case COND_EXPR:
18006 {
18007 dw_loc_descr_ref lhs
18008 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18009 dw_loc_list_ref rhs
18010 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18011 dw_loc_descr_ref bra_node, jump_node, tmp;
18012
18013 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18014 if (list_ret == 0 || lhs == 0 || rhs == 0)
18015 return 0;
18016
18017 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18018 add_loc_descr_to_each (list_ret, bra_node);
18019
18020 add_loc_list (&list_ret, rhs);
18021 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18022 add_loc_descr_to_each (list_ret, jump_node);
18023
18024 add_loc_descr_to_each (list_ret, lhs);
18025 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18026 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18027
18028 /* ??? Need a node to point the skip at. Use a nop. */
18029 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18030 add_loc_descr_to_each (list_ret, tmp);
18031 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18032 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18033 }
18034 break;
18035
18036 case FIX_TRUNC_EXPR:
18037 return 0;
18038
18039 default:
18040 /* Leave front-end specific codes as simply unknown. This comes
18041 up, for instance, with the C STMT_EXPR. */
18042 if ((unsigned int) TREE_CODE (loc)
18043 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18044 {
18045 expansion_failed (loc, NULL_RTX,
18046 "language specific tree node");
18047 return 0;
18048 }
18049
18050 /* Otherwise this is a generic code; we should just lists all of
18051 these explicitly. We forgot one. */
18052 if (flag_checking)
18053 gcc_unreachable ();
18054
18055 /* In a release build, we want to degrade gracefully: better to
18056 generate incomplete debugging information than to crash. */
18057 return NULL;
18058 }
18059
18060 if (!ret && !list_ret)
18061 return 0;
18062
18063 if (want_address == 2 && !have_address
18064 && (dwarf_version >= 4 || !dwarf_strict))
18065 {
18066 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18067 {
18068 expansion_failed (loc, NULL_RTX,
18069 "DWARF address size mismatch");
18070 return 0;
18071 }
18072 if (ret)
18073 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18074 else
18075 add_loc_descr_to_each (list_ret,
18076 new_loc_descr (DW_OP_stack_value, 0, 0));
18077 have_address = 1;
18078 }
18079 /* Show if we can't fill the request for an address. */
18080 if (want_address && !have_address)
18081 {
18082 expansion_failed (loc, NULL_RTX,
18083 "Want address and only have value");
18084 return 0;
18085 }
18086
18087 gcc_assert (!ret || !list_ret);
18088
18089 /* If we've got an address and don't want one, dereference. */
18090 if (!want_address && have_address)
18091 {
18092 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18093
18094 if (size > DWARF2_ADDR_SIZE || size == -1)
18095 {
18096 expansion_failed (loc, NULL_RTX,
18097 "DWARF address size mismatch");
18098 return 0;
18099 }
18100 else if (size == DWARF2_ADDR_SIZE)
18101 op = DW_OP_deref;
18102 else
18103 op = DW_OP_deref_size;
18104
18105 if (ret)
18106 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18107 else
18108 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18109 }
18110 if (ret)
18111 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18112
18113 return list_ret;
18114 }
18115
18116 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18117 expressions. */
18118
18119 static dw_loc_list_ref
18120 loc_list_from_tree (tree loc, int want_address,
18121 struct loc_descr_context *context)
18122 {
18123 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18124
18125 for (dw_loc_list_ref loc_cur = result;
18126 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18127 loc_descr_without_nops (loc_cur->expr);
18128 return result;
18129 }
18130
18131 /* Same as above but return only single location expression. */
18132 static dw_loc_descr_ref
18133 loc_descriptor_from_tree (tree loc, int want_address,
18134 struct loc_descr_context *context)
18135 {
18136 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18137 if (!ret)
18138 return NULL;
18139 if (ret->dw_loc_next)
18140 {
18141 expansion_failed (loc, NULL_RTX,
18142 "Location list where only loc descriptor needed");
18143 return NULL;
18144 }
18145 return ret->expr;
18146 }
18147
18148 /* Given a value, round it up to the lowest multiple of `boundary'
18149 which is not less than the value itself. */
18150
18151 static inline HOST_WIDE_INT
18152 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18153 {
18154 return (((value + boundary - 1) / boundary) * boundary);
18155 }
18156
18157 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18158 pointer to the declared type for the relevant field variable, or return
18159 `integer_type_node' if the given node turns out to be an
18160 ERROR_MARK node. */
18161
18162 static inline tree
18163 field_type (const_tree decl)
18164 {
18165 tree type;
18166
18167 if (TREE_CODE (decl) == ERROR_MARK)
18168 return integer_type_node;
18169
18170 type = DECL_BIT_FIELD_TYPE (decl);
18171 if (type == NULL_TREE)
18172 type = TREE_TYPE (decl);
18173
18174 return type;
18175 }
18176
18177 /* Given a pointer to a tree node, return the alignment in bits for
18178 it, or else return BITS_PER_WORD if the node actually turns out to
18179 be an ERROR_MARK node. */
18180
18181 static inline unsigned
18182 simple_type_align_in_bits (const_tree type)
18183 {
18184 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18185 }
18186
18187 static inline unsigned
18188 simple_decl_align_in_bits (const_tree decl)
18189 {
18190 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18191 }
18192
18193 /* Return the result of rounding T up to ALIGN. */
18194
18195 static inline offset_int
18196 round_up_to_align (const offset_int &t, unsigned int align)
18197 {
18198 return wi::udiv_trunc (t + align - 1, align) * align;
18199 }
18200
18201 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18202 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18203 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18204 if we fail to return the size in one of these two forms. */
18205
18206 static dw_loc_descr_ref
18207 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18208 {
18209 tree tree_size;
18210 struct loc_descr_context ctx;
18211
18212 /* Return a constant integer in priority, if possible. */
18213 *cst_size = int_size_in_bytes (type);
18214 if (*cst_size != -1)
18215 return NULL;
18216
18217 ctx.context_type = const_cast<tree> (type);
18218 ctx.base_decl = NULL_TREE;
18219 ctx.dpi = NULL;
18220 ctx.placeholder_arg = false;
18221 ctx.placeholder_seen = false;
18222
18223 type = TYPE_MAIN_VARIANT (type);
18224 tree_size = TYPE_SIZE_UNIT (type);
18225 return ((tree_size != NULL_TREE)
18226 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18227 : NULL);
18228 }
18229
18230 /* Helper structure for RECORD_TYPE processing. */
18231 struct vlr_context
18232 {
18233 /* Root RECORD_TYPE. It is needed to generate data member location
18234 descriptions in variable-length records (VLR), but also to cope with
18235 variants, which are composed of nested structures multiplexed with
18236 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18237 function processing a FIELD_DECL, it is required to be non null. */
18238 tree struct_type;
18239 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18240 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18241 this variant part as part of the root record (in storage units). For
18242 regular records, it must be NULL_TREE. */
18243 tree variant_part_offset;
18244 };
18245
18246 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18247 addressed byte of the "containing object" for the given FIELD_DECL. If
18248 possible, return a native constant through CST_OFFSET (in which case NULL is
18249 returned); otherwise return a DWARF expression that computes the offset.
18250
18251 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18252 that offset is, either because the argument turns out to be a pointer to an
18253 ERROR_MARK node, or because the offset expression is too complex for us.
18254
18255 CTX is required: see the comment for VLR_CONTEXT. */
18256
18257 static dw_loc_descr_ref
18258 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18259 HOST_WIDE_INT *cst_offset)
18260 {
18261 tree tree_result;
18262 dw_loc_list_ref loc_result;
18263
18264 *cst_offset = 0;
18265
18266 if (TREE_CODE (decl) == ERROR_MARK)
18267 return NULL;
18268 else
18269 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18270
18271 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18272 case. */
18273 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18274 return NULL;
18275
18276 #ifdef PCC_BITFIELD_TYPE_MATTERS
18277 /* We used to handle only constant offsets in all cases. Now, we handle
18278 properly dynamic byte offsets only when PCC bitfield type doesn't
18279 matter. */
18280 if (PCC_BITFIELD_TYPE_MATTERS
18281 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18282 {
18283 offset_int object_offset_in_bits;
18284 offset_int object_offset_in_bytes;
18285 offset_int bitpos_int;
18286 tree type;
18287 tree field_size_tree;
18288 offset_int deepest_bitpos;
18289 offset_int field_size_in_bits;
18290 unsigned int type_align_in_bits;
18291 unsigned int decl_align_in_bits;
18292 offset_int type_size_in_bits;
18293
18294 bitpos_int = wi::to_offset (bit_position (decl));
18295 type = field_type (decl);
18296 type_size_in_bits = offset_int_type_size_in_bits (type);
18297 type_align_in_bits = simple_type_align_in_bits (type);
18298
18299 field_size_tree = DECL_SIZE (decl);
18300
18301 /* The size could be unspecified if there was an error, or for
18302 a flexible array member. */
18303 if (!field_size_tree)
18304 field_size_tree = bitsize_zero_node;
18305
18306 /* If the size of the field is not constant, use the type size. */
18307 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18308 field_size_in_bits = wi::to_offset (field_size_tree);
18309 else
18310 field_size_in_bits = type_size_in_bits;
18311
18312 decl_align_in_bits = simple_decl_align_in_bits (decl);
18313
18314 /* The GCC front-end doesn't make any attempt to keep track of the
18315 starting bit offset (relative to the start of the containing
18316 structure type) of the hypothetical "containing object" for a
18317 bit-field. Thus, when computing the byte offset value for the
18318 start of the "containing object" of a bit-field, we must deduce
18319 this information on our own. This can be rather tricky to do in
18320 some cases. For example, handling the following structure type
18321 definition when compiling for an i386/i486 target (which only
18322 aligns long long's to 32-bit boundaries) can be very tricky:
18323
18324 struct S { int field1; long long field2:31; };
18325
18326 Fortunately, there is a simple rule-of-thumb which can be used
18327 in such cases. When compiling for an i386/i486, GCC will
18328 allocate 8 bytes for the structure shown above. It decides to
18329 do this based upon one simple rule for bit-field allocation.
18330 GCC allocates each "containing object" for each bit-field at
18331 the first (i.e. lowest addressed) legitimate alignment boundary
18332 (based upon the required minimum alignment for the declared
18333 type of the field) which it can possibly use, subject to the
18334 condition that there is still enough available space remaining
18335 in the containing object (when allocated at the selected point)
18336 to fully accommodate all of the bits of the bit-field itself.
18337
18338 This simple rule makes it obvious why GCC allocates 8 bytes for
18339 each object of the structure type shown above. When looking
18340 for a place to allocate the "containing object" for `field2',
18341 the compiler simply tries to allocate a 64-bit "containing
18342 object" at each successive 32-bit boundary (starting at zero)
18343 until it finds a place to allocate that 64- bit field such that
18344 at least 31 contiguous (and previously unallocated) bits remain
18345 within that selected 64 bit field. (As it turns out, for the
18346 example above, the compiler finds it is OK to allocate the
18347 "containing object" 64-bit field at bit-offset zero within the
18348 structure type.)
18349
18350 Here we attempt to work backwards from the limited set of facts
18351 we're given, and we try to deduce from those facts, where GCC
18352 must have believed that the containing object started (within
18353 the structure type). The value we deduce is then used (by the
18354 callers of this routine) to generate DW_AT_location and
18355 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18356 the case of DW_AT_location, regular fields as well). */
18357
18358 /* Figure out the bit-distance from the start of the structure to
18359 the "deepest" bit of the bit-field. */
18360 deepest_bitpos = bitpos_int + field_size_in_bits;
18361
18362 /* This is the tricky part. Use some fancy footwork to deduce
18363 where the lowest addressed bit of the containing object must
18364 be. */
18365 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18366
18367 /* Round up to type_align by default. This works best for
18368 bitfields. */
18369 object_offset_in_bits
18370 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18371
18372 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18373 {
18374 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18375
18376 /* Round up to decl_align instead. */
18377 object_offset_in_bits
18378 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18379 }
18380
18381 object_offset_in_bytes
18382 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18383 if (ctx->variant_part_offset == NULL_TREE)
18384 {
18385 *cst_offset = object_offset_in_bytes.to_shwi ();
18386 return NULL;
18387 }
18388 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18389 }
18390 else
18391 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18392 tree_result = byte_position (decl);
18393
18394 if (ctx->variant_part_offset != NULL_TREE)
18395 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18396 ctx->variant_part_offset, tree_result);
18397
18398 /* If the byte offset is a constant, it's simplier to handle a native
18399 constant rather than a DWARF expression. */
18400 if (TREE_CODE (tree_result) == INTEGER_CST)
18401 {
18402 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18403 return NULL;
18404 }
18405 struct loc_descr_context loc_ctx = {
18406 ctx->struct_type, /* context_type */
18407 NULL_TREE, /* base_decl */
18408 NULL, /* dpi */
18409 false, /* placeholder_arg */
18410 false /* placeholder_seen */
18411 };
18412 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18413
18414 /* We want a DWARF expression: abort if we only have a location list with
18415 multiple elements. */
18416 if (!loc_result || !single_element_loc_list_p (loc_result))
18417 return NULL;
18418 else
18419 return loc_result->expr;
18420 }
18421 \f
18422 /* The following routines define various Dwarf attributes and any data
18423 associated with them. */
18424
18425 /* Add a location description attribute value to a DIE.
18426
18427 This emits location attributes suitable for whole variables and
18428 whole parameters. Note that the location attributes for struct fields are
18429 generated by the routine `data_member_location_attribute' below. */
18430
18431 static inline void
18432 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18433 dw_loc_list_ref descr)
18434 {
18435 if (descr == 0)
18436 return;
18437 if (single_element_loc_list_p (descr))
18438 add_AT_loc (die, attr_kind, descr->expr);
18439 else
18440 add_AT_loc_list (die, attr_kind, descr);
18441 }
18442
18443 /* Add DW_AT_accessibility attribute to DIE if needed. */
18444
18445 static void
18446 add_accessibility_attribute (dw_die_ref die, tree decl)
18447 {
18448 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18449 children, otherwise the default is DW_ACCESS_public. In DWARF2
18450 the default has always been DW_ACCESS_public. */
18451 if (TREE_PROTECTED (decl))
18452 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18453 else if (TREE_PRIVATE (decl))
18454 {
18455 if (dwarf_version == 2
18456 || die->die_parent == NULL
18457 || die->die_parent->die_tag != DW_TAG_class_type)
18458 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18459 }
18460 else if (dwarf_version > 2
18461 && die->die_parent
18462 && die->die_parent->die_tag == DW_TAG_class_type)
18463 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18464 }
18465
18466 /* Attach the specialized form of location attribute used for data members of
18467 struct and union types. In the special case of a FIELD_DECL node which
18468 represents a bit-field, the "offset" part of this special location
18469 descriptor must indicate the distance in bytes from the lowest-addressed
18470 byte of the containing struct or union type to the lowest-addressed byte of
18471 the "containing object" for the bit-field. (See the `field_byte_offset'
18472 function above).
18473
18474 For any given bit-field, the "containing object" is a hypothetical object
18475 (of some integral or enum type) within which the given bit-field lives. The
18476 type of this hypothetical "containing object" is always the same as the
18477 declared type of the individual bit-field itself (for GCC anyway... the
18478 DWARF spec doesn't actually mandate this). Note that it is the size (in
18479 bytes) of the hypothetical "containing object" which will be given in the
18480 DW_AT_byte_size attribute for this bit-field. (See the
18481 `byte_size_attribute' function below.) It is also used when calculating the
18482 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18483 function below.)
18484
18485 CTX is required: see the comment for VLR_CONTEXT. */
18486
18487 static void
18488 add_data_member_location_attribute (dw_die_ref die,
18489 tree decl,
18490 struct vlr_context *ctx)
18491 {
18492 HOST_WIDE_INT offset;
18493 dw_loc_descr_ref loc_descr = 0;
18494
18495 if (TREE_CODE (decl) == TREE_BINFO)
18496 {
18497 /* We're working on the TAG_inheritance for a base class. */
18498 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18499 {
18500 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18501 aren't at a fixed offset from all (sub)objects of the same
18502 type. We need to extract the appropriate offset from our
18503 vtable. The following dwarf expression means
18504
18505 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18506
18507 This is specific to the V3 ABI, of course. */
18508
18509 dw_loc_descr_ref tmp;
18510
18511 /* Make a copy of the object address. */
18512 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18513 add_loc_descr (&loc_descr, tmp);
18514
18515 /* Extract the vtable address. */
18516 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18517 add_loc_descr (&loc_descr, tmp);
18518
18519 /* Calculate the address of the offset. */
18520 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18521 gcc_assert (offset < 0);
18522
18523 tmp = int_loc_descriptor (-offset);
18524 add_loc_descr (&loc_descr, tmp);
18525 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18526 add_loc_descr (&loc_descr, tmp);
18527
18528 /* Extract the offset. */
18529 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18530 add_loc_descr (&loc_descr, tmp);
18531
18532 /* Add it to the object address. */
18533 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18534 add_loc_descr (&loc_descr, tmp);
18535 }
18536 else
18537 offset = tree_to_shwi (BINFO_OFFSET (decl));
18538 }
18539 else
18540 {
18541 loc_descr = field_byte_offset (decl, ctx, &offset);
18542
18543 /* If loc_descr is available then we know the field offset is dynamic.
18544 However, GDB does not handle dynamic field offsets very well at the
18545 moment. */
18546 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18547 {
18548 loc_descr = NULL;
18549 offset = 0;
18550 }
18551
18552 /* Data member location evalutation starts with the base address on the
18553 stack. Compute the field offset and add it to this base address. */
18554 else if (loc_descr != NULL)
18555 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18556 }
18557
18558 if (! loc_descr)
18559 {
18560 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18561 e.g. GDB only added support to it in November 2016. For DWARF5
18562 we need newer debug info consumers anyway. We might change this
18563 to dwarf_version >= 4 once most consumers catched up. */
18564 if (dwarf_version >= 5
18565 && TREE_CODE (decl) == FIELD_DECL
18566 && DECL_BIT_FIELD_TYPE (decl))
18567 {
18568 tree off = bit_position (decl);
18569 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18570 {
18571 remove_AT (die, DW_AT_byte_size);
18572 remove_AT (die, DW_AT_bit_offset);
18573 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18574 return;
18575 }
18576 }
18577 if (dwarf_version > 2)
18578 {
18579 /* Don't need to output a location expression, just the constant. */
18580 if (offset < 0)
18581 add_AT_int (die, DW_AT_data_member_location, offset);
18582 else
18583 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18584 return;
18585 }
18586 else
18587 {
18588 enum dwarf_location_atom op;
18589
18590 /* The DWARF2 standard says that we should assume that the structure
18591 address is already on the stack, so we can specify a structure
18592 field address by using DW_OP_plus_uconst. */
18593 op = DW_OP_plus_uconst;
18594 loc_descr = new_loc_descr (op, offset, 0);
18595 }
18596 }
18597
18598 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18599 }
18600
18601 /* Writes integer values to dw_vec_const array. */
18602
18603 static void
18604 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18605 {
18606 while (size != 0)
18607 {
18608 *dest++ = val & 0xff;
18609 val >>= 8;
18610 --size;
18611 }
18612 }
18613
18614 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18615
18616 static HOST_WIDE_INT
18617 extract_int (const unsigned char *src, unsigned int size)
18618 {
18619 HOST_WIDE_INT val = 0;
18620
18621 src += size;
18622 while (size != 0)
18623 {
18624 val <<= 8;
18625 val |= *--src & 0xff;
18626 --size;
18627 }
18628 return val;
18629 }
18630
18631 /* Writes wide_int values to dw_vec_const array. */
18632
18633 static void
18634 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18635 {
18636 int i;
18637
18638 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18639 {
18640 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18641 return;
18642 }
18643
18644 /* We'd have to extend this code to support odd sizes. */
18645 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18646
18647 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18648
18649 if (WORDS_BIG_ENDIAN)
18650 for (i = n - 1; i >= 0; i--)
18651 {
18652 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18653 dest += sizeof (HOST_WIDE_INT);
18654 }
18655 else
18656 for (i = 0; i < n; i++)
18657 {
18658 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18659 dest += sizeof (HOST_WIDE_INT);
18660 }
18661 }
18662
18663 /* Writes floating point values to dw_vec_const array. */
18664
18665 static void
18666 insert_float (const_rtx rtl, unsigned char *array)
18667 {
18668 long val[4];
18669 int i;
18670 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18671
18672 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18673
18674 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18675 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18676 {
18677 insert_int (val[i], 4, array);
18678 array += 4;
18679 }
18680 }
18681
18682 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18683 does not have a "location" either in memory or in a register. These
18684 things can arise in GNU C when a constant is passed as an actual parameter
18685 to an inlined function. They can also arise in C++ where declared
18686 constants do not necessarily get memory "homes". */
18687
18688 static bool
18689 add_const_value_attribute (dw_die_ref die, rtx rtl)
18690 {
18691 switch (GET_CODE (rtl))
18692 {
18693 case CONST_INT:
18694 {
18695 HOST_WIDE_INT val = INTVAL (rtl);
18696
18697 if (val < 0)
18698 add_AT_int (die, DW_AT_const_value, val);
18699 else
18700 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18701 }
18702 return true;
18703
18704 case CONST_WIDE_INT:
18705 {
18706 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18707 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18708 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18709 wide_int w = wi::zext (w1, prec);
18710 add_AT_wide (die, DW_AT_const_value, w);
18711 }
18712 return true;
18713
18714 case CONST_DOUBLE:
18715 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18716 floating-point constant. A CONST_DOUBLE is used whenever the
18717 constant requires more than one word in order to be adequately
18718 represented. */
18719 if (TARGET_SUPPORTS_WIDE_INT == 0
18720 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18721 add_AT_double (die, DW_AT_const_value,
18722 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18723 else
18724 {
18725 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18726 unsigned int length = GET_MODE_SIZE (mode);
18727 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18728
18729 insert_float (rtl, array);
18730 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18731 }
18732 return true;
18733
18734 case CONST_VECTOR:
18735 {
18736 machine_mode mode = GET_MODE (rtl);
18737 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18738 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18739 unsigned char *array
18740 = ggc_vec_alloc<unsigned char> (length * elt_size);
18741 unsigned int i;
18742 unsigned char *p;
18743 machine_mode imode = GET_MODE_INNER (mode);
18744
18745 switch (GET_MODE_CLASS (mode))
18746 {
18747 case MODE_VECTOR_INT:
18748 for (i = 0, p = array; i < length; i++, p += elt_size)
18749 {
18750 rtx elt = CONST_VECTOR_ELT (rtl, i);
18751 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18752 }
18753 break;
18754
18755 case MODE_VECTOR_FLOAT:
18756 for (i = 0, p = array; i < length; i++, p += elt_size)
18757 {
18758 rtx elt = CONST_VECTOR_ELT (rtl, i);
18759 insert_float (elt, p);
18760 }
18761 break;
18762
18763 default:
18764 gcc_unreachable ();
18765 }
18766
18767 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18768 }
18769 return true;
18770
18771 case CONST_STRING:
18772 if (dwarf_version >= 4 || !dwarf_strict)
18773 {
18774 dw_loc_descr_ref loc_result;
18775 resolve_one_addr (&rtl);
18776 rtl_addr:
18777 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18778 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18779 add_AT_loc (die, DW_AT_location, loc_result);
18780 vec_safe_push (used_rtx_array, rtl);
18781 return true;
18782 }
18783 return false;
18784
18785 case CONST:
18786 if (CONSTANT_P (XEXP (rtl, 0)))
18787 return add_const_value_attribute (die, XEXP (rtl, 0));
18788 /* FALLTHROUGH */
18789 case SYMBOL_REF:
18790 if (!const_ok_for_output (rtl))
18791 return false;
18792 /* FALLTHROUGH */
18793 case LABEL_REF:
18794 if (dwarf_version >= 4 || !dwarf_strict)
18795 goto rtl_addr;
18796 return false;
18797
18798 case PLUS:
18799 /* In cases where an inlined instance of an inline function is passed
18800 the address of an `auto' variable (which is local to the caller) we
18801 can get a situation where the DECL_RTL of the artificial local
18802 variable (for the inlining) which acts as a stand-in for the
18803 corresponding formal parameter (of the inline function) will look
18804 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18805 exactly a compile-time constant expression, but it isn't the address
18806 of the (artificial) local variable either. Rather, it represents the
18807 *value* which the artificial local variable always has during its
18808 lifetime. We currently have no way to represent such quasi-constant
18809 values in Dwarf, so for now we just punt and generate nothing. */
18810 return false;
18811
18812 case HIGH:
18813 case CONST_FIXED:
18814 return false;
18815
18816 case MEM:
18817 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18818 && MEM_READONLY_P (rtl)
18819 && GET_MODE (rtl) == BLKmode)
18820 {
18821 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18822 return true;
18823 }
18824 return false;
18825
18826 default:
18827 /* No other kinds of rtx should be possible here. */
18828 gcc_unreachable ();
18829 }
18830 return false;
18831 }
18832
18833 /* Determine whether the evaluation of EXPR references any variables
18834 or functions which aren't otherwise used (and therefore may not be
18835 output). */
18836 static tree
18837 reference_to_unused (tree * tp, int * walk_subtrees,
18838 void * data ATTRIBUTE_UNUSED)
18839 {
18840 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18841 *walk_subtrees = 0;
18842
18843 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18844 && ! TREE_ASM_WRITTEN (*tp))
18845 return *tp;
18846 /* ??? The C++ FE emits debug information for using decls, so
18847 putting gcc_unreachable here falls over. See PR31899. For now
18848 be conservative. */
18849 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18850 return *tp;
18851 else if (VAR_P (*tp))
18852 {
18853 varpool_node *node = varpool_node::get (*tp);
18854 if (!node || !node->definition)
18855 return *tp;
18856 }
18857 else if (TREE_CODE (*tp) == FUNCTION_DECL
18858 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18859 {
18860 /* The call graph machinery must have finished analyzing,
18861 optimizing and gimplifying the CU by now.
18862 So if *TP has no call graph node associated
18863 to it, it means *TP will not be emitted. */
18864 if (!cgraph_node::get (*tp))
18865 return *tp;
18866 }
18867 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18868 return *tp;
18869
18870 return NULL_TREE;
18871 }
18872
18873 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18874 for use in a later add_const_value_attribute call. */
18875
18876 static rtx
18877 rtl_for_decl_init (tree init, tree type)
18878 {
18879 rtx rtl = NULL_RTX;
18880
18881 STRIP_NOPS (init);
18882
18883 /* If a variable is initialized with a string constant without embedded
18884 zeros, build CONST_STRING. */
18885 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18886 {
18887 tree enttype = TREE_TYPE (type);
18888 tree domain = TYPE_DOMAIN (type);
18889 scalar_int_mode mode;
18890
18891 if (is_int_mode (TYPE_MODE (enttype), &mode)
18892 && GET_MODE_SIZE (mode) == 1
18893 && domain
18894 && integer_zerop (TYPE_MIN_VALUE (domain))
18895 && compare_tree_int (TYPE_MAX_VALUE (domain),
18896 TREE_STRING_LENGTH (init) - 1) == 0
18897 && ((size_t) TREE_STRING_LENGTH (init)
18898 == strlen (TREE_STRING_POINTER (init)) + 1))
18899 {
18900 rtl = gen_rtx_CONST_STRING (VOIDmode,
18901 ggc_strdup (TREE_STRING_POINTER (init)));
18902 rtl = gen_rtx_MEM (BLKmode, rtl);
18903 MEM_READONLY_P (rtl) = 1;
18904 }
18905 }
18906 /* Other aggregates, and complex values, could be represented using
18907 CONCAT: FIXME! */
18908 else if (AGGREGATE_TYPE_P (type)
18909 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18910 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18911 || TREE_CODE (type) == COMPLEX_TYPE)
18912 ;
18913 /* Vectors only work if their mode is supported by the target.
18914 FIXME: generic vectors ought to work too. */
18915 else if (TREE_CODE (type) == VECTOR_TYPE
18916 && !VECTOR_MODE_P (TYPE_MODE (type)))
18917 ;
18918 /* If the initializer is something that we know will expand into an
18919 immediate RTL constant, expand it now. We must be careful not to
18920 reference variables which won't be output. */
18921 else if (initializer_constant_valid_p (init, type)
18922 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18923 {
18924 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18925 possible. */
18926 if (TREE_CODE (type) == VECTOR_TYPE)
18927 switch (TREE_CODE (init))
18928 {
18929 case VECTOR_CST:
18930 break;
18931 case CONSTRUCTOR:
18932 if (TREE_CONSTANT (init))
18933 {
18934 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18935 bool constant_p = true;
18936 tree value;
18937 unsigned HOST_WIDE_INT ix;
18938
18939 /* Even when ctor is constant, it might contain non-*_CST
18940 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18941 belong into VECTOR_CST nodes. */
18942 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18943 if (!CONSTANT_CLASS_P (value))
18944 {
18945 constant_p = false;
18946 break;
18947 }
18948
18949 if (constant_p)
18950 {
18951 init = build_vector_from_ctor (type, elts);
18952 break;
18953 }
18954 }
18955 /* FALLTHRU */
18956
18957 default:
18958 return NULL;
18959 }
18960
18961 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18962
18963 /* If expand_expr returns a MEM, it wasn't immediate. */
18964 gcc_assert (!rtl || !MEM_P (rtl));
18965 }
18966
18967 return rtl;
18968 }
18969
18970 /* Generate RTL for the variable DECL to represent its location. */
18971
18972 static rtx
18973 rtl_for_decl_location (tree decl)
18974 {
18975 rtx rtl;
18976
18977 /* Here we have to decide where we are going to say the parameter "lives"
18978 (as far as the debugger is concerned). We only have a couple of
18979 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18980
18981 DECL_RTL normally indicates where the parameter lives during most of the
18982 activation of the function. If optimization is enabled however, this
18983 could be either NULL or else a pseudo-reg. Both of those cases indicate
18984 that the parameter doesn't really live anywhere (as far as the code
18985 generation parts of GCC are concerned) during most of the function's
18986 activation. That will happen (for example) if the parameter is never
18987 referenced within the function.
18988
18989 We could just generate a location descriptor here for all non-NULL
18990 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18991 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18992 where DECL_RTL is NULL or is a pseudo-reg.
18993
18994 Note however that we can only get away with using DECL_INCOMING_RTL as
18995 a backup substitute for DECL_RTL in certain limited cases. In cases
18996 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18997 we can be sure that the parameter was passed using the same type as it is
18998 declared to have within the function, and that its DECL_INCOMING_RTL
18999 points us to a place where a value of that type is passed.
19000
19001 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19002 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19003 because in these cases DECL_INCOMING_RTL points us to a value of some
19004 type which is *different* from the type of the parameter itself. Thus,
19005 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19006 such cases, the debugger would end up (for example) trying to fetch a
19007 `float' from a place which actually contains the first part of a
19008 `double'. That would lead to really incorrect and confusing
19009 output at debug-time.
19010
19011 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19012 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19013 are a couple of exceptions however. On little-endian machines we can
19014 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19015 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19016 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19017 when (on a little-endian machine) a non-prototyped function has a
19018 parameter declared to be of type `short' or `char'. In such cases,
19019 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19020 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19021 passed `int' value. If the debugger then uses that address to fetch
19022 a `short' or a `char' (on a little-endian machine) the result will be
19023 the correct data, so we allow for such exceptional cases below.
19024
19025 Note that our goal here is to describe the place where the given formal
19026 parameter lives during most of the function's activation (i.e. between the
19027 end of the prologue and the start of the epilogue). We'll do that as best
19028 as we can. Note however that if the given formal parameter is modified
19029 sometime during the execution of the function, then a stack backtrace (at
19030 debug-time) will show the function as having been called with the *new*
19031 value rather than the value which was originally passed in. This happens
19032 rarely enough that it is not a major problem, but it *is* a problem, and
19033 I'd like to fix it.
19034
19035 A future version of dwarf2out.c may generate two additional attributes for
19036 any given DW_TAG_formal_parameter DIE which will describe the "passed
19037 type" and the "passed location" for the given formal parameter in addition
19038 to the attributes we now generate to indicate the "declared type" and the
19039 "active location" for each parameter. This additional set of attributes
19040 could be used by debuggers for stack backtraces. Separately, note that
19041 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19042 This happens (for example) for inlined-instances of inline function formal
19043 parameters which are never referenced. This really shouldn't be
19044 happening. All PARM_DECL nodes should get valid non-NULL
19045 DECL_INCOMING_RTL values. FIXME. */
19046
19047 /* Use DECL_RTL as the "location" unless we find something better. */
19048 rtl = DECL_RTL_IF_SET (decl);
19049
19050 /* When generating abstract instances, ignore everything except
19051 constants, symbols living in memory, and symbols living in
19052 fixed registers. */
19053 if (! reload_completed)
19054 {
19055 if (rtl
19056 && (CONSTANT_P (rtl)
19057 || (MEM_P (rtl)
19058 && CONSTANT_P (XEXP (rtl, 0)))
19059 || (REG_P (rtl)
19060 && VAR_P (decl)
19061 && TREE_STATIC (decl))))
19062 {
19063 rtl = targetm.delegitimize_address (rtl);
19064 return rtl;
19065 }
19066 rtl = NULL_RTX;
19067 }
19068 else if (TREE_CODE (decl) == PARM_DECL)
19069 {
19070 if (rtl == NULL_RTX
19071 || is_pseudo_reg (rtl)
19072 || (MEM_P (rtl)
19073 && is_pseudo_reg (XEXP (rtl, 0))
19074 && DECL_INCOMING_RTL (decl)
19075 && MEM_P (DECL_INCOMING_RTL (decl))
19076 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19077 {
19078 tree declared_type = TREE_TYPE (decl);
19079 tree passed_type = DECL_ARG_TYPE (decl);
19080 machine_mode dmode = TYPE_MODE (declared_type);
19081 machine_mode pmode = TYPE_MODE (passed_type);
19082
19083 /* This decl represents a formal parameter which was optimized out.
19084 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19085 all cases where (rtl == NULL_RTX) just below. */
19086 if (dmode == pmode)
19087 rtl = DECL_INCOMING_RTL (decl);
19088 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19089 && SCALAR_INT_MODE_P (dmode)
19090 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19091 && DECL_INCOMING_RTL (decl))
19092 {
19093 rtx inc = DECL_INCOMING_RTL (decl);
19094 if (REG_P (inc))
19095 rtl = inc;
19096 else if (MEM_P (inc))
19097 {
19098 if (BYTES_BIG_ENDIAN)
19099 rtl = adjust_address_nv (inc, dmode,
19100 GET_MODE_SIZE (pmode)
19101 - GET_MODE_SIZE (dmode));
19102 else
19103 rtl = inc;
19104 }
19105 }
19106 }
19107
19108 /* If the parm was passed in registers, but lives on the stack, then
19109 make a big endian correction if the mode of the type of the
19110 parameter is not the same as the mode of the rtl. */
19111 /* ??? This is the same series of checks that are made in dbxout.c before
19112 we reach the big endian correction code there. It isn't clear if all
19113 of these checks are necessary here, but keeping them all is the safe
19114 thing to do. */
19115 else if (MEM_P (rtl)
19116 && XEXP (rtl, 0) != const0_rtx
19117 && ! CONSTANT_P (XEXP (rtl, 0))
19118 /* Not passed in memory. */
19119 && !MEM_P (DECL_INCOMING_RTL (decl))
19120 /* Not passed by invisible reference. */
19121 && (!REG_P (XEXP (rtl, 0))
19122 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19123 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19124 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19125 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19126 #endif
19127 )
19128 /* Big endian correction check. */
19129 && BYTES_BIG_ENDIAN
19130 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19131 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19132 < UNITS_PER_WORD))
19133 {
19134 machine_mode addr_mode = get_address_mode (rtl);
19135 int offset = (UNITS_PER_WORD
19136 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19137
19138 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19139 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19140 }
19141 }
19142 else if (VAR_P (decl)
19143 && rtl
19144 && MEM_P (rtl)
19145 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19146 {
19147 machine_mode addr_mode = get_address_mode (rtl);
19148 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19149 GET_MODE (rtl));
19150
19151 /* If a variable is declared "register" yet is smaller than
19152 a register, then if we store the variable to memory, it
19153 looks like we're storing a register-sized value, when in
19154 fact we are not. We need to adjust the offset of the
19155 storage location to reflect the actual value's bytes,
19156 else gdb will not be able to display it. */
19157 if (offset != 0)
19158 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19159 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19160 }
19161
19162 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19163 and will have been substituted directly into all expressions that use it.
19164 C does not have such a concept, but C++ and other languages do. */
19165 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19166 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19167
19168 if (rtl)
19169 rtl = targetm.delegitimize_address (rtl);
19170
19171 /* If we don't look past the constant pool, we risk emitting a
19172 reference to a constant pool entry that isn't referenced from
19173 code, and thus is not emitted. */
19174 if (rtl)
19175 rtl = avoid_constant_pool_reference (rtl);
19176
19177 /* Try harder to get a rtl. If this symbol ends up not being emitted
19178 in the current CU, resolve_addr will remove the expression referencing
19179 it. */
19180 if (rtl == NULL_RTX
19181 && VAR_P (decl)
19182 && !DECL_EXTERNAL (decl)
19183 && TREE_STATIC (decl)
19184 && DECL_NAME (decl)
19185 && !DECL_HARD_REGISTER (decl)
19186 && DECL_MODE (decl) != VOIDmode)
19187 {
19188 rtl = make_decl_rtl_for_debug (decl);
19189 if (!MEM_P (rtl)
19190 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19191 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19192 rtl = NULL_RTX;
19193 }
19194
19195 return rtl;
19196 }
19197
19198 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19199 returned. If so, the decl for the COMMON block is returned, and the
19200 value is the offset into the common block for the symbol. */
19201
19202 static tree
19203 fortran_common (tree decl, HOST_WIDE_INT *value)
19204 {
19205 tree val_expr, cvar;
19206 machine_mode mode;
19207 HOST_WIDE_INT bitsize, bitpos;
19208 tree offset;
19209 int unsignedp, reversep, volatilep = 0;
19210
19211 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19212 it does not have a value (the offset into the common area), or if it
19213 is thread local (as opposed to global) then it isn't common, and shouldn't
19214 be handled as such. */
19215 if (!VAR_P (decl)
19216 || !TREE_STATIC (decl)
19217 || !DECL_HAS_VALUE_EXPR_P (decl)
19218 || !is_fortran ())
19219 return NULL_TREE;
19220
19221 val_expr = DECL_VALUE_EXPR (decl);
19222 if (TREE_CODE (val_expr) != COMPONENT_REF)
19223 return NULL_TREE;
19224
19225 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19226 &unsignedp, &reversep, &volatilep);
19227
19228 if (cvar == NULL_TREE
19229 || !VAR_P (cvar)
19230 || DECL_ARTIFICIAL (cvar)
19231 || !TREE_PUBLIC (cvar))
19232 return NULL_TREE;
19233
19234 *value = 0;
19235 if (offset != NULL)
19236 {
19237 if (!tree_fits_shwi_p (offset))
19238 return NULL_TREE;
19239 *value = tree_to_shwi (offset);
19240 }
19241 if (bitpos != 0)
19242 *value += bitpos / BITS_PER_UNIT;
19243
19244 return cvar;
19245 }
19246
19247 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19248 data attribute for a variable or a parameter. We generate the
19249 DW_AT_const_value attribute only in those cases where the given variable
19250 or parameter does not have a true "location" either in memory or in a
19251 register. This can happen (for example) when a constant is passed as an
19252 actual argument in a call to an inline function. (It's possible that
19253 these things can crop up in other ways also.) Note that one type of
19254 constant value which can be passed into an inlined function is a constant
19255 pointer. This can happen for example if an actual argument in an inlined
19256 function call evaluates to a compile-time constant address.
19257
19258 CACHE_P is true if it is worth caching the location list for DECL,
19259 so that future calls can reuse it rather than regenerate it from scratch.
19260 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19261 since we will need to refer to them each time the function is inlined. */
19262
19263 static bool
19264 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19265 {
19266 rtx rtl;
19267 dw_loc_list_ref list;
19268 var_loc_list *loc_list;
19269 cached_dw_loc_list *cache;
19270
19271 if (early_dwarf)
19272 return false;
19273
19274 if (TREE_CODE (decl) == ERROR_MARK)
19275 return false;
19276
19277 if (get_AT (die, DW_AT_location)
19278 || get_AT (die, DW_AT_const_value))
19279 return true;
19280
19281 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19282 || TREE_CODE (decl) == RESULT_DECL);
19283
19284 /* Try to get some constant RTL for this decl, and use that as the value of
19285 the location. */
19286
19287 rtl = rtl_for_decl_location (decl);
19288 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19289 && add_const_value_attribute (die, rtl))
19290 return true;
19291
19292 /* See if we have single element location list that is equivalent to
19293 a constant value. That way we are better to use add_const_value_attribute
19294 rather than expanding constant value equivalent. */
19295 loc_list = lookup_decl_loc (decl);
19296 if (loc_list
19297 && loc_list->first
19298 && loc_list->first->next == NULL
19299 && NOTE_P (loc_list->first->loc)
19300 && NOTE_VAR_LOCATION (loc_list->first->loc)
19301 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19302 {
19303 struct var_loc_node *node;
19304
19305 node = loc_list->first;
19306 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19307 if (GET_CODE (rtl) == EXPR_LIST)
19308 rtl = XEXP (rtl, 0);
19309 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19310 && add_const_value_attribute (die, rtl))
19311 return true;
19312 }
19313 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19314 list several times. See if we've already cached the contents. */
19315 list = NULL;
19316 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19317 cache_p = false;
19318 if (cache_p)
19319 {
19320 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19321 if (cache)
19322 list = cache->loc_list;
19323 }
19324 if (list == NULL)
19325 {
19326 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19327 NULL);
19328 /* It is usually worth caching this result if the decl is from
19329 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19330 if (cache_p && list && list->dw_loc_next)
19331 {
19332 cached_dw_loc_list **slot
19333 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19334 DECL_UID (decl),
19335 INSERT);
19336 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19337 cache->decl_id = DECL_UID (decl);
19338 cache->loc_list = list;
19339 *slot = cache;
19340 }
19341 }
19342 if (list)
19343 {
19344 add_AT_location_description (die, DW_AT_location, list);
19345 return true;
19346 }
19347 /* None of that worked, so it must not really have a location;
19348 try adding a constant value attribute from the DECL_INITIAL. */
19349 return tree_add_const_value_attribute_for_decl (die, decl);
19350 }
19351
19352 /* Helper function for tree_add_const_value_attribute. Natively encode
19353 initializer INIT into an array. Return true if successful. */
19354
19355 static bool
19356 native_encode_initializer (tree init, unsigned char *array, int size)
19357 {
19358 tree type;
19359
19360 if (init == NULL_TREE)
19361 return false;
19362
19363 STRIP_NOPS (init);
19364 switch (TREE_CODE (init))
19365 {
19366 case STRING_CST:
19367 type = TREE_TYPE (init);
19368 if (TREE_CODE (type) == ARRAY_TYPE)
19369 {
19370 tree enttype = TREE_TYPE (type);
19371 scalar_int_mode mode;
19372
19373 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19374 || GET_MODE_SIZE (mode) != 1)
19375 return false;
19376 if (int_size_in_bytes (type) != size)
19377 return false;
19378 if (size > TREE_STRING_LENGTH (init))
19379 {
19380 memcpy (array, TREE_STRING_POINTER (init),
19381 TREE_STRING_LENGTH (init));
19382 memset (array + TREE_STRING_LENGTH (init),
19383 '\0', size - TREE_STRING_LENGTH (init));
19384 }
19385 else
19386 memcpy (array, TREE_STRING_POINTER (init), size);
19387 return true;
19388 }
19389 return false;
19390 case CONSTRUCTOR:
19391 type = TREE_TYPE (init);
19392 if (int_size_in_bytes (type) != size)
19393 return false;
19394 if (TREE_CODE (type) == ARRAY_TYPE)
19395 {
19396 HOST_WIDE_INT min_index;
19397 unsigned HOST_WIDE_INT cnt;
19398 int curpos = 0, fieldsize;
19399 constructor_elt *ce;
19400
19401 if (TYPE_DOMAIN (type) == NULL_TREE
19402 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19403 return false;
19404
19405 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19406 if (fieldsize <= 0)
19407 return false;
19408
19409 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19410 memset (array, '\0', size);
19411 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19412 {
19413 tree val = ce->value;
19414 tree index = ce->index;
19415 int pos = curpos;
19416 if (index && TREE_CODE (index) == RANGE_EXPR)
19417 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19418 * fieldsize;
19419 else if (index)
19420 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19421
19422 if (val)
19423 {
19424 STRIP_NOPS (val);
19425 if (!native_encode_initializer (val, array + pos, fieldsize))
19426 return false;
19427 }
19428 curpos = pos + fieldsize;
19429 if (index && TREE_CODE (index) == RANGE_EXPR)
19430 {
19431 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19432 - tree_to_shwi (TREE_OPERAND (index, 0));
19433 while (count-- > 0)
19434 {
19435 if (val)
19436 memcpy (array + curpos, array + pos, fieldsize);
19437 curpos += fieldsize;
19438 }
19439 }
19440 gcc_assert (curpos <= size);
19441 }
19442 return true;
19443 }
19444 else if (TREE_CODE (type) == RECORD_TYPE
19445 || TREE_CODE (type) == UNION_TYPE)
19446 {
19447 tree field = NULL_TREE;
19448 unsigned HOST_WIDE_INT cnt;
19449 constructor_elt *ce;
19450
19451 if (int_size_in_bytes (type) != size)
19452 return false;
19453
19454 if (TREE_CODE (type) == RECORD_TYPE)
19455 field = TYPE_FIELDS (type);
19456
19457 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19458 {
19459 tree val = ce->value;
19460 int pos, fieldsize;
19461
19462 if (ce->index != 0)
19463 field = ce->index;
19464
19465 if (val)
19466 STRIP_NOPS (val);
19467
19468 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19469 return false;
19470
19471 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19472 && TYPE_DOMAIN (TREE_TYPE (field))
19473 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19474 return false;
19475 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19476 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19477 return false;
19478 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19479 pos = int_byte_position (field);
19480 gcc_assert (pos + fieldsize <= size);
19481 if (val && fieldsize != 0
19482 && !native_encode_initializer (val, array + pos, fieldsize))
19483 return false;
19484 }
19485 return true;
19486 }
19487 return false;
19488 case VIEW_CONVERT_EXPR:
19489 case NON_LVALUE_EXPR:
19490 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19491 default:
19492 return native_encode_expr (init, array, size) == size;
19493 }
19494 }
19495
19496 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19497 attribute is the const value T. */
19498
19499 static bool
19500 tree_add_const_value_attribute (dw_die_ref die, tree t)
19501 {
19502 tree init;
19503 tree type = TREE_TYPE (t);
19504 rtx rtl;
19505
19506 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19507 return false;
19508
19509 init = t;
19510 gcc_assert (!DECL_P (init));
19511
19512 if (TREE_CODE (init) == INTEGER_CST)
19513 {
19514 if (tree_fits_uhwi_p (init))
19515 {
19516 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19517 return true;
19518 }
19519 if (tree_fits_shwi_p (init))
19520 {
19521 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19522 return true;
19523 }
19524 }
19525 if (! early_dwarf)
19526 {
19527 rtl = rtl_for_decl_init (init, type);
19528 if (rtl)
19529 return add_const_value_attribute (die, rtl);
19530 }
19531 /* If the host and target are sane, try harder. */
19532 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19533 && initializer_constant_valid_p (init, type))
19534 {
19535 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19536 if (size > 0 && (int) size == size)
19537 {
19538 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19539
19540 if (native_encode_initializer (init, array, size))
19541 {
19542 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19543 return true;
19544 }
19545 ggc_free (array);
19546 }
19547 }
19548 return false;
19549 }
19550
19551 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19552 attribute is the const value of T, where T is an integral constant
19553 variable with static storage duration
19554 (so it can't be a PARM_DECL or a RESULT_DECL). */
19555
19556 static bool
19557 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19558 {
19559
19560 if (!decl
19561 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19562 || (VAR_P (decl) && !TREE_STATIC (decl)))
19563 return false;
19564
19565 if (TREE_READONLY (decl)
19566 && ! TREE_THIS_VOLATILE (decl)
19567 && DECL_INITIAL (decl))
19568 /* OK */;
19569 else
19570 return false;
19571
19572 /* Don't add DW_AT_const_value if abstract origin already has one. */
19573 if (get_AT (var_die, DW_AT_const_value))
19574 return false;
19575
19576 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19577 }
19578
19579 /* Convert the CFI instructions for the current function into a
19580 location list. This is used for DW_AT_frame_base when we targeting
19581 a dwarf2 consumer that does not support the dwarf3
19582 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19583 expressions. */
19584
19585 static dw_loc_list_ref
19586 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19587 {
19588 int ix;
19589 dw_fde_ref fde;
19590 dw_loc_list_ref list, *list_tail;
19591 dw_cfi_ref cfi;
19592 dw_cfa_location last_cfa, next_cfa;
19593 const char *start_label, *last_label, *section;
19594 dw_cfa_location remember;
19595
19596 fde = cfun->fde;
19597 gcc_assert (fde != NULL);
19598
19599 section = secname_for_decl (current_function_decl);
19600 list_tail = &list;
19601 list = NULL;
19602
19603 memset (&next_cfa, 0, sizeof (next_cfa));
19604 next_cfa.reg = INVALID_REGNUM;
19605 remember = next_cfa;
19606
19607 start_label = fde->dw_fde_begin;
19608
19609 /* ??? Bald assumption that the CIE opcode list does not contain
19610 advance opcodes. */
19611 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19612 lookup_cfa_1 (cfi, &next_cfa, &remember);
19613
19614 last_cfa = next_cfa;
19615 last_label = start_label;
19616
19617 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19618 {
19619 /* If the first partition contained no CFI adjustments, the
19620 CIE opcodes apply to the whole first partition. */
19621 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19622 fde->dw_fde_begin, fde->dw_fde_end, section);
19623 list_tail =&(*list_tail)->dw_loc_next;
19624 start_label = last_label = fde->dw_fde_second_begin;
19625 }
19626
19627 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19628 {
19629 switch (cfi->dw_cfi_opc)
19630 {
19631 case DW_CFA_set_loc:
19632 case DW_CFA_advance_loc1:
19633 case DW_CFA_advance_loc2:
19634 case DW_CFA_advance_loc4:
19635 if (!cfa_equal_p (&last_cfa, &next_cfa))
19636 {
19637 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19638 start_label, last_label, section);
19639
19640 list_tail = &(*list_tail)->dw_loc_next;
19641 last_cfa = next_cfa;
19642 start_label = last_label;
19643 }
19644 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19645 break;
19646
19647 case DW_CFA_advance_loc:
19648 /* The encoding is complex enough that we should never emit this. */
19649 gcc_unreachable ();
19650
19651 default:
19652 lookup_cfa_1 (cfi, &next_cfa, &remember);
19653 break;
19654 }
19655 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19656 {
19657 if (!cfa_equal_p (&last_cfa, &next_cfa))
19658 {
19659 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19660 start_label, last_label, section);
19661
19662 list_tail = &(*list_tail)->dw_loc_next;
19663 last_cfa = next_cfa;
19664 start_label = last_label;
19665 }
19666 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19667 start_label, fde->dw_fde_end, section);
19668 list_tail = &(*list_tail)->dw_loc_next;
19669 start_label = last_label = fde->dw_fde_second_begin;
19670 }
19671 }
19672
19673 if (!cfa_equal_p (&last_cfa, &next_cfa))
19674 {
19675 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19676 start_label, last_label, section);
19677 list_tail = &(*list_tail)->dw_loc_next;
19678 start_label = last_label;
19679 }
19680
19681 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19682 start_label,
19683 fde->dw_fde_second_begin
19684 ? fde->dw_fde_second_end : fde->dw_fde_end,
19685 section);
19686
19687 if (list && list->dw_loc_next)
19688 gen_llsym (list);
19689
19690 return list;
19691 }
19692
19693 /* Compute a displacement from the "steady-state frame pointer" to the
19694 frame base (often the same as the CFA), and store it in
19695 frame_pointer_fb_offset. OFFSET is added to the displacement
19696 before the latter is negated. */
19697
19698 static void
19699 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19700 {
19701 rtx reg, elim;
19702
19703 #ifdef FRAME_POINTER_CFA_OFFSET
19704 reg = frame_pointer_rtx;
19705 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19706 #else
19707 reg = arg_pointer_rtx;
19708 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19709 #endif
19710
19711 elim = (ira_use_lra_p
19712 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19713 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19714 if (GET_CODE (elim) == PLUS)
19715 {
19716 offset += INTVAL (XEXP (elim, 1));
19717 elim = XEXP (elim, 0);
19718 }
19719
19720 frame_pointer_fb_offset = -offset;
19721
19722 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19723 in which to eliminate. This is because it's stack pointer isn't
19724 directly accessible as a register within the ISA. To work around
19725 this, assume that while we cannot provide a proper value for
19726 frame_pointer_fb_offset, we won't need one either. */
19727 frame_pointer_fb_offset_valid
19728 = ((SUPPORTS_STACK_ALIGNMENT
19729 && (elim == hard_frame_pointer_rtx
19730 || elim == stack_pointer_rtx))
19731 || elim == (frame_pointer_needed
19732 ? hard_frame_pointer_rtx
19733 : stack_pointer_rtx));
19734 }
19735
19736 /* Generate a DW_AT_name attribute given some string value to be included as
19737 the value of the attribute. */
19738
19739 static void
19740 add_name_attribute (dw_die_ref die, const char *name_string)
19741 {
19742 if (name_string != NULL && *name_string != 0)
19743 {
19744 if (demangle_name_func)
19745 name_string = (*demangle_name_func) (name_string);
19746
19747 add_AT_string (die, DW_AT_name, name_string);
19748 }
19749 }
19750
19751 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19752 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19753 of TYPE accordingly.
19754
19755 ??? This is a temporary measure until after we're able to generate
19756 regular DWARF for the complex Ada type system. */
19757
19758 static void
19759 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19760 dw_die_ref context_die)
19761 {
19762 tree dtype;
19763 dw_die_ref dtype_die;
19764
19765 if (!lang_hooks.types.descriptive_type)
19766 return;
19767
19768 dtype = lang_hooks.types.descriptive_type (type);
19769 if (!dtype)
19770 return;
19771
19772 dtype_die = lookup_type_die (dtype);
19773 if (!dtype_die)
19774 {
19775 gen_type_die (dtype, context_die);
19776 dtype_die = lookup_type_die (dtype);
19777 gcc_assert (dtype_die);
19778 }
19779
19780 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19781 }
19782
19783 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19784
19785 static const char *
19786 comp_dir_string (void)
19787 {
19788 const char *wd;
19789 char *wd1;
19790 static const char *cached_wd = NULL;
19791
19792 if (cached_wd != NULL)
19793 return cached_wd;
19794
19795 wd = get_src_pwd ();
19796 if (wd == NULL)
19797 return NULL;
19798
19799 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19800 {
19801 int wdlen;
19802
19803 wdlen = strlen (wd);
19804 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19805 strcpy (wd1, wd);
19806 wd1 [wdlen] = DIR_SEPARATOR;
19807 wd1 [wdlen + 1] = 0;
19808 wd = wd1;
19809 }
19810
19811 cached_wd = remap_debug_filename (wd);
19812 return cached_wd;
19813 }
19814
19815 /* Generate a DW_AT_comp_dir attribute for DIE. */
19816
19817 static void
19818 add_comp_dir_attribute (dw_die_ref die)
19819 {
19820 const char * wd = comp_dir_string ();
19821 if (wd != NULL)
19822 add_AT_string (die, DW_AT_comp_dir, wd);
19823 }
19824
19825 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19826 pointer computation, ...), output a representation for that bound according
19827 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19828 loc_list_from_tree for the meaning of CONTEXT. */
19829
19830 static void
19831 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19832 int forms, struct loc_descr_context *context)
19833 {
19834 dw_die_ref context_die, decl_die;
19835 dw_loc_list_ref list;
19836 bool strip_conversions = true;
19837 bool placeholder_seen = false;
19838
19839 while (strip_conversions)
19840 switch (TREE_CODE (value))
19841 {
19842 case ERROR_MARK:
19843 case SAVE_EXPR:
19844 return;
19845
19846 CASE_CONVERT:
19847 case VIEW_CONVERT_EXPR:
19848 value = TREE_OPERAND (value, 0);
19849 break;
19850
19851 default:
19852 strip_conversions = false;
19853 break;
19854 }
19855
19856 /* If possible and permitted, output the attribute as a constant. */
19857 if ((forms & dw_scalar_form_constant) != 0
19858 && TREE_CODE (value) == INTEGER_CST)
19859 {
19860 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19861
19862 /* If HOST_WIDE_INT is big enough then represent the bound as
19863 a constant value. We need to choose a form based on
19864 whether the type is signed or unsigned. We cannot just
19865 call add_AT_unsigned if the value itself is positive
19866 (add_AT_unsigned might add the unsigned value encoded as
19867 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19868 bounds type and then sign extend any unsigned values found
19869 for signed types. This is needed only for
19870 DW_AT_{lower,upper}_bound, since for most other attributes,
19871 consumers will treat DW_FORM_data[1248] as unsigned values,
19872 regardless of the underlying type. */
19873 if (prec <= HOST_BITS_PER_WIDE_INT
19874 || tree_fits_uhwi_p (value))
19875 {
19876 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19877 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19878 else
19879 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19880 }
19881 else
19882 /* Otherwise represent the bound as an unsigned value with
19883 the precision of its type. The precision and signedness
19884 of the type will be necessary to re-interpret it
19885 unambiguously. */
19886 add_AT_wide (die, attr, wi::to_wide (value));
19887 return;
19888 }
19889
19890 /* Otherwise, if it's possible and permitted too, output a reference to
19891 another DIE. */
19892 if ((forms & dw_scalar_form_reference) != 0)
19893 {
19894 tree decl = NULL_TREE;
19895
19896 /* Some type attributes reference an outer type. For instance, the upper
19897 bound of an array may reference an embedding record (this happens in
19898 Ada). */
19899 if (TREE_CODE (value) == COMPONENT_REF
19900 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19901 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19902 decl = TREE_OPERAND (value, 1);
19903
19904 else if (VAR_P (value)
19905 || TREE_CODE (value) == PARM_DECL
19906 || TREE_CODE (value) == RESULT_DECL)
19907 decl = value;
19908
19909 if (decl != NULL_TREE)
19910 {
19911 dw_die_ref decl_die = lookup_decl_die (decl);
19912
19913 /* ??? Can this happen, or should the variable have been bound
19914 first? Probably it can, since I imagine that we try to create
19915 the types of parameters in the order in which they exist in
19916 the list, and won't have created a forward reference to a
19917 later parameter. */
19918 if (decl_die != NULL)
19919 {
19920 add_AT_die_ref (die, attr, decl_die);
19921 return;
19922 }
19923 }
19924 }
19925
19926 /* Last chance: try to create a stack operation procedure to evaluate the
19927 value. Do nothing if even that is not possible or permitted. */
19928 if ((forms & dw_scalar_form_exprloc) == 0)
19929 return;
19930
19931 list = loc_list_from_tree (value, 2, context);
19932 if (context && context->placeholder_arg)
19933 {
19934 placeholder_seen = context->placeholder_seen;
19935 context->placeholder_seen = false;
19936 }
19937 if (list == NULL || single_element_loc_list_p (list))
19938 {
19939 /* If this attribute is not a reference nor constant, it is
19940 a DWARF expression rather than location description. For that
19941 loc_list_from_tree (value, 0, &context) is needed. */
19942 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19943 if (list2 && single_element_loc_list_p (list2))
19944 {
19945 if (placeholder_seen)
19946 {
19947 struct dwarf_procedure_info dpi;
19948 dpi.fndecl = NULL_TREE;
19949 dpi.args_count = 1;
19950 if (!resolve_args_picking (list2->expr, 1, &dpi))
19951 return;
19952 }
19953 add_AT_loc (die, attr, list2->expr);
19954 return;
19955 }
19956 }
19957
19958 /* If that failed to give a single element location list, fall back to
19959 outputting this as a reference... still if permitted. */
19960 if (list == NULL
19961 || (forms & dw_scalar_form_reference) == 0
19962 || placeholder_seen)
19963 return;
19964
19965 if (current_function_decl == 0)
19966 context_die = comp_unit_die ();
19967 else
19968 context_die = lookup_decl_die (current_function_decl);
19969
19970 decl_die = new_die (DW_TAG_variable, context_die, value);
19971 add_AT_flag (decl_die, DW_AT_artificial, 1);
19972 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19973 context_die);
19974 add_AT_location_description (decl_die, DW_AT_location, list);
19975 add_AT_die_ref (die, attr, decl_die);
19976 }
19977
19978 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19979 default. */
19980
19981 static int
19982 lower_bound_default (void)
19983 {
19984 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19985 {
19986 case DW_LANG_C:
19987 case DW_LANG_C89:
19988 case DW_LANG_C99:
19989 case DW_LANG_C11:
19990 case DW_LANG_C_plus_plus:
19991 case DW_LANG_C_plus_plus_11:
19992 case DW_LANG_C_plus_plus_14:
19993 case DW_LANG_ObjC:
19994 case DW_LANG_ObjC_plus_plus:
19995 return 0;
19996 case DW_LANG_Fortran77:
19997 case DW_LANG_Fortran90:
19998 case DW_LANG_Fortran95:
19999 case DW_LANG_Fortran03:
20000 case DW_LANG_Fortran08:
20001 return 1;
20002 case DW_LANG_UPC:
20003 case DW_LANG_D:
20004 case DW_LANG_Python:
20005 return dwarf_version >= 4 ? 0 : -1;
20006 case DW_LANG_Ada95:
20007 case DW_LANG_Ada83:
20008 case DW_LANG_Cobol74:
20009 case DW_LANG_Cobol85:
20010 case DW_LANG_Modula2:
20011 case DW_LANG_PLI:
20012 return dwarf_version >= 4 ? 1 : -1;
20013 default:
20014 return -1;
20015 }
20016 }
20017
20018 /* Given a tree node describing an array bound (either lower or upper) output
20019 a representation for that bound. */
20020
20021 static void
20022 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20023 tree bound, struct loc_descr_context *context)
20024 {
20025 int dflt;
20026
20027 while (1)
20028 switch (TREE_CODE (bound))
20029 {
20030 /* Strip all conversions. */
20031 CASE_CONVERT:
20032 case VIEW_CONVERT_EXPR:
20033 bound = TREE_OPERAND (bound, 0);
20034 break;
20035
20036 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20037 are even omitted when they are the default. */
20038 case INTEGER_CST:
20039 /* If the value for this bound is the default one, we can even omit the
20040 attribute. */
20041 if (bound_attr == DW_AT_lower_bound
20042 && tree_fits_shwi_p (bound)
20043 && (dflt = lower_bound_default ()) != -1
20044 && tree_to_shwi (bound) == dflt)
20045 return;
20046
20047 /* FALLTHRU */
20048
20049 default:
20050 /* Because of the complex interaction there can be with other GNAT
20051 encodings, GDB isn't ready yet to handle proper DWARF description
20052 for self-referencial subrange bounds: let GNAT encodings do the
20053 magic in such a case. */
20054 if (is_ada ()
20055 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20056 && contains_placeholder_p (bound))
20057 return;
20058
20059 add_scalar_info (subrange_die, bound_attr, bound,
20060 dw_scalar_form_constant
20061 | dw_scalar_form_exprloc
20062 | dw_scalar_form_reference,
20063 context);
20064 return;
20065 }
20066 }
20067
20068 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20069 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20070 Note that the block of subscript information for an array type also
20071 includes information about the element type of the given array type.
20072
20073 This function reuses previously set type and bound information if
20074 available. */
20075
20076 static void
20077 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20078 {
20079 unsigned dimension_number;
20080 tree lower, upper;
20081 dw_die_ref child = type_die->die_child;
20082
20083 for (dimension_number = 0;
20084 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20085 type = TREE_TYPE (type), dimension_number++)
20086 {
20087 tree domain = TYPE_DOMAIN (type);
20088
20089 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20090 break;
20091
20092 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20093 and (in GNU C only) variable bounds. Handle all three forms
20094 here. */
20095
20096 /* Find and reuse a previously generated DW_TAG_subrange_type if
20097 available.
20098
20099 For multi-dimensional arrays, as we iterate through the
20100 various dimensions in the enclosing for loop above, we also
20101 iterate through the DIE children and pick at each
20102 DW_TAG_subrange_type previously generated (if available).
20103 Each child DW_TAG_subrange_type DIE describes the range of
20104 the current dimension. At this point we should have as many
20105 DW_TAG_subrange_type's as we have dimensions in the
20106 array. */
20107 dw_die_ref subrange_die = NULL;
20108 if (child)
20109 while (1)
20110 {
20111 child = child->die_sib;
20112 if (child->die_tag == DW_TAG_subrange_type)
20113 subrange_die = child;
20114 if (child == type_die->die_child)
20115 {
20116 /* If we wrapped around, stop looking next time. */
20117 child = NULL;
20118 break;
20119 }
20120 if (child->die_tag == DW_TAG_subrange_type)
20121 break;
20122 }
20123 if (!subrange_die)
20124 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20125
20126 if (domain)
20127 {
20128 /* We have an array type with specified bounds. */
20129 lower = TYPE_MIN_VALUE (domain);
20130 upper = TYPE_MAX_VALUE (domain);
20131
20132 /* Define the index type. */
20133 if (TREE_TYPE (domain)
20134 && !get_AT (subrange_die, DW_AT_type))
20135 {
20136 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20137 TREE_TYPE field. We can't emit debug info for this
20138 because it is an unnamed integral type. */
20139 if (TREE_CODE (domain) == INTEGER_TYPE
20140 && TYPE_NAME (domain) == NULL_TREE
20141 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20142 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20143 ;
20144 else
20145 add_type_attribute (subrange_die, TREE_TYPE (domain),
20146 TYPE_UNQUALIFIED, false, type_die);
20147 }
20148
20149 /* ??? If upper is NULL, the array has unspecified length,
20150 but it does have a lower bound. This happens with Fortran
20151 dimension arr(N:*)
20152 Since the debugger is definitely going to need to know N
20153 to produce useful results, go ahead and output the lower
20154 bound solo, and hope the debugger can cope. */
20155
20156 if (!get_AT (subrange_die, DW_AT_lower_bound))
20157 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20158 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20159 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20160 }
20161
20162 /* Otherwise we have an array type with an unspecified length. The
20163 DWARF-2 spec does not say how to handle this; let's just leave out the
20164 bounds. */
20165 }
20166 }
20167
20168 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20169
20170 static void
20171 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20172 {
20173 dw_die_ref decl_die;
20174 HOST_WIDE_INT size;
20175 dw_loc_descr_ref size_expr = NULL;
20176
20177 switch (TREE_CODE (tree_node))
20178 {
20179 case ERROR_MARK:
20180 size = 0;
20181 break;
20182 case ENUMERAL_TYPE:
20183 case RECORD_TYPE:
20184 case UNION_TYPE:
20185 case QUAL_UNION_TYPE:
20186 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20187 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20188 {
20189 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20190 return;
20191 }
20192 size_expr = type_byte_size (tree_node, &size);
20193 break;
20194 case FIELD_DECL:
20195 /* For a data member of a struct or union, the DW_AT_byte_size is
20196 generally given as the number of bytes normally allocated for an
20197 object of the *declared* type of the member itself. This is true
20198 even for bit-fields. */
20199 size = int_size_in_bytes (field_type (tree_node));
20200 break;
20201 default:
20202 gcc_unreachable ();
20203 }
20204
20205 /* Support for dynamically-sized objects was introduced by DWARFv3.
20206 At the moment, GDB does not handle variable byte sizes very well,
20207 though. */
20208 if ((dwarf_version >= 3 || !dwarf_strict)
20209 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20210 && size_expr != NULL)
20211 add_AT_loc (die, DW_AT_byte_size, size_expr);
20212
20213 /* Note that `size' might be -1 when we get to this point. If it is, that
20214 indicates that the byte size of the entity in question is variable and
20215 that we could not generate a DWARF expression that computes it. */
20216 if (size >= 0)
20217 add_AT_unsigned (die, DW_AT_byte_size, size);
20218 }
20219
20220 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20221 alignment. */
20222
20223 static void
20224 add_alignment_attribute (dw_die_ref die, tree tree_node)
20225 {
20226 if (dwarf_version < 5 && dwarf_strict)
20227 return;
20228
20229 unsigned align;
20230
20231 if (DECL_P (tree_node))
20232 {
20233 if (!DECL_USER_ALIGN (tree_node))
20234 return;
20235
20236 align = DECL_ALIGN_UNIT (tree_node);
20237 }
20238 else if (TYPE_P (tree_node))
20239 {
20240 if (!TYPE_USER_ALIGN (tree_node))
20241 return;
20242
20243 align = TYPE_ALIGN_UNIT (tree_node);
20244 }
20245 else
20246 gcc_unreachable ();
20247
20248 add_AT_unsigned (die, DW_AT_alignment, align);
20249 }
20250
20251 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20252 which specifies the distance in bits from the highest order bit of the
20253 "containing object" for the bit-field to the highest order bit of the
20254 bit-field itself.
20255
20256 For any given bit-field, the "containing object" is a hypothetical object
20257 (of some integral or enum type) within which the given bit-field lives. The
20258 type of this hypothetical "containing object" is always the same as the
20259 declared type of the individual bit-field itself. The determination of the
20260 exact location of the "containing object" for a bit-field is rather
20261 complicated. It's handled by the `field_byte_offset' function (above).
20262
20263 CTX is required: see the comment for VLR_CONTEXT.
20264
20265 Note that it is the size (in bytes) of the hypothetical "containing object"
20266 which will be given in the DW_AT_byte_size attribute for this bit-field.
20267 (See `byte_size_attribute' above). */
20268
20269 static inline void
20270 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20271 {
20272 HOST_WIDE_INT object_offset_in_bytes;
20273 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20274 HOST_WIDE_INT bitpos_int;
20275 HOST_WIDE_INT highest_order_object_bit_offset;
20276 HOST_WIDE_INT highest_order_field_bit_offset;
20277 HOST_WIDE_INT bit_offset;
20278
20279 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20280
20281 /* Must be a field and a bit field. */
20282 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20283
20284 /* We can't yet handle bit-fields whose offsets are variable, so if we
20285 encounter such things, just return without generating any attribute
20286 whatsoever. Likewise for variable or too large size. */
20287 if (! tree_fits_shwi_p (bit_position (decl))
20288 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20289 return;
20290
20291 bitpos_int = int_bit_position (decl);
20292
20293 /* Note that the bit offset is always the distance (in bits) from the
20294 highest-order bit of the "containing object" to the highest-order bit of
20295 the bit-field itself. Since the "high-order end" of any object or field
20296 is different on big-endian and little-endian machines, the computation
20297 below must take account of these differences. */
20298 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20299 highest_order_field_bit_offset = bitpos_int;
20300
20301 if (! BYTES_BIG_ENDIAN)
20302 {
20303 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20304 highest_order_object_bit_offset +=
20305 simple_type_size_in_bits (original_type);
20306 }
20307
20308 bit_offset
20309 = (! BYTES_BIG_ENDIAN
20310 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20311 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20312
20313 if (bit_offset < 0)
20314 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20315 else
20316 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20317 }
20318
20319 /* For a FIELD_DECL node which represents a bit field, output an attribute
20320 which specifies the length in bits of the given field. */
20321
20322 static inline void
20323 add_bit_size_attribute (dw_die_ref die, tree decl)
20324 {
20325 /* Must be a field and a bit field. */
20326 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20327 && DECL_BIT_FIELD_TYPE (decl));
20328
20329 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20330 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20331 }
20332
20333 /* If the compiled language is ANSI C, then add a 'prototyped'
20334 attribute, if arg types are given for the parameters of a function. */
20335
20336 static inline void
20337 add_prototyped_attribute (dw_die_ref die, tree func_type)
20338 {
20339 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20340 {
20341 case DW_LANG_C:
20342 case DW_LANG_C89:
20343 case DW_LANG_C99:
20344 case DW_LANG_C11:
20345 case DW_LANG_ObjC:
20346 if (prototype_p (func_type))
20347 add_AT_flag (die, DW_AT_prototyped, 1);
20348 break;
20349 default:
20350 break;
20351 }
20352 }
20353
20354 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20355 by looking in the type declaration, the object declaration equate table or
20356 the block mapping. */
20357
20358 static inline dw_die_ref
20359 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20360 {
20361 dw_die_ref origin_die = NULL;
20362
20363 if (DECL_P (origin))
20364 {
20365 dw_die_ref c;
20366 origin_die = lookup_decl_die (origin);
20367 /* "Unwrap" the decls DIE which we put in the imported unit context.
20368 We are looking for the abstract copy here. */
20369 if (in_lto_p
20370 && origin_die
20371 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20372 /* ??? Identify this better. */
20373 && c->with_offset)
20374 origin_die = c;
20375 }
20376 else if (TYPE_P (origin))
20377 origin_die = lookup_type_die (origin);
20378 else if (TREE_CODE (origin) == BLOCK)
20379 origin_die = BLOCK_DIE (origin);
20380
20381 /* XXX: Functions that are never lowered don't always have correct block
20382 trees (in the case of java, they simply have no block tree, in some other
20383 languages). For these functions, there is nothing we can really do to
20384 output correct debug info for inlined functions in all cases. Rather
20385 than die, we'll just produce deficient debug info now, in that we will
20386 have variables without a proper abstract origin. In the future, when all
20387 functions are lowered, we should re-add a gcc_assert (origin_die)
20388 here. */
20389
20390 if (origin_die)
20391 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20392 return origin_die;
20393 }
20394
20395 /* We do not currently support the pure_virtual attribute. */
20396
20397 static inline void
20398 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20399 {
20400 if (DECL_VINDEX (func_decl))
20401 {
20402 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20403
20404 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20405 add_AT_loc (die, DW_AT_vtable_elem_location,
20406 new_loc_descr (DW_OP_constu,
20407 tree_to_shwi (DECL_VINDEX (func_decl)),
20408 0));
20409
20410 /* GNU extension: Record what type this method came from originally. */
20411 if (debug_info_level > DINFO_LEVEL_TERSE
20412 && DECL_CONTEXT (func_decl))
20413 add_AT_die_ref (die, DW_AT_containing_type,
20414 lookup_type_die (DECL_CONTEXT (func_decl)));
20415 }
20416 }
20417 \f
20418 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20419 given decl. This used to be a vendor extension until after DWARF 4
20420 standardized it. */
20421
20422 static void
20423 add_linkage_attr (dw_die_ref die, tree decl)
20424 {
20425 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20426
20427 /* Mimic what assemble_name_raw does with a leading '*'. */
20428 if (name[0] == '*')
20429 name = &name[1];
20430
20431 if (dwarf_version >= 4)
20432 add_AT_string (die, DW_AT_linkage_name, name);
20433 else
20434 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20435 }
20436
20437 /* Add source coordinate attributes for the given decl. */
20438
20439 static void
20440 add_src_coords_attributes (dw_die_ref die, tree decl)
20441 {
20442 expanded_location s;
20443
20444 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20445 return;
20446 s = expand_location (DECL_SOURCE_LOCATION (decl));
20447 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20448 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20449 if (debug_column_info && s.column)
20450 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20451 }
20452
20453 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20454
20455 static void
20456 add_linkage_name_raw (dw_die_ref die, tree decl)
20457 {
20458 /* Defer until we have an assembler name set. */
20459 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20460 {
20461 limbo_die_node *asm_name;
20462
20463 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20464 asm_name->die = die;
20465 asm_name->created_for = decl;
20466 asm_name->next = deferred_asm_name;
20467 deferred_asm_name = asm_name;
20468 }
20469 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20470 add_linkage_attr (die, decl);
20471 }
20472
20473 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20474
20475 static void
20476 add_linkage_name (dw_die_ref die, tree decl)
20477 {
20478 if (debug_info_level > DINFO_LEVEL_NONE
20479 && VAR_OR_FUNCTION_DECL_P (decl)
20480 && TREE_PUBLIC (decl)
20481 && !(VAR_P (decl) && DECL_REGISTER (decl))
20482 && die->die_tag != DW_TAG_member)
20483 add_linkage_name_raw (die, decl);
20484 }
20485
20486 /* Add a DW_AT_name attribute and source coordinate attribute for the
20487 given decl, but only if it actually has a name. */
20488
20489 static void
20490 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20491 bool no_linkage_name)
20492 {
20493 tree decl_name;
20494
20495 decl_name = DECL_NAME (decl);
20496 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20497 {
20498 const char *name = dwarf2_name (decl, 0);
20499 if (name)
20500 add_name_attribute (die, name);
20501 if (! DECL_ARTIFICIAL (decl))
20502 add_src_coords_attributes (die, decl);
20503
20504 if (!no_linkage_name)
20505 add_linkage_name (die, decl);
20506 }
20507
20508 #ifdef VMS_DEBUGGING_INFO
20509 /* Get the function's name, as described by its RTL. This may be different
20510 from the DECL_NAME name used in the source file. */
20511 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20512 {
20513 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20514 XEXP (DECL_RTL (decl), 0), false);
20515 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20516 }
20517 #endif /* VMS_DEBUGGING_INFO */
20518 }
20519
20520 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20521
20522 static void
20523 add_discr_value (dw_die_ref die, dw_discr_value *value)
20524 {
20525 dw_attr_node attr;
20526
20527 attr.dw_attr = DW_AT_discr_value;
20528 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20529 attr.dw_attr_val.val_entry = NULL;
20530 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20531 if (value->pos)
20532 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20533 else
20534 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20535 add_dwarf_attr (die, &attr);
20536 }
20537
20538 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20539
20540 static void
20541 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20542 {
20543 dw_attr_node attr;
20544
20545 attr.dw_attr = DW_AT_discr_list;
20546 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20547 attr.dw_attr_val.val_entry = NULL;
20548 attr.dw_attr_val.v.val_discr_list = discr_list;
20549 add_dwarf_attr (die, &attr);
20550 }
20551
20552 static inline dw_discr_list_ref
20553 AT_discr_list (dw_attr_node *attr)
20554 {
20555 return attr->dw_attr_val.v.val_discr_list;
20556 }
20557
20558 #ifdef VMS_DEBUGGING_INFO
20559 /* Output the debug main pointer die for VMS */
20560
20561 void
20562 dwarf2out_vms_debug_main_pointer (void)
20563 {
20564 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20565 dw_die_ref die;
20566
20567 /* Allocate the VMS debug main subprogram die. */
20568 die = new_die_raw (DW_TAG_subprogram);
20569 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20570 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20571 current_function_funcdef_no);
20572 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20573
20574 /* Make it the first child of comp_unit_die (). */
20575 die->die_parent = comp_unit_die ();
20576 if (comp_unit_die ()->die_child)
20577 {
20578 die->die_sib = comp_unit_die ()->die_child->die_sib;
20579 comp_unit_die ()->die_child->die_sib = die;
20580 }
20581 else
20582 {
20583 die->die_sib = die;
20584 comp_unit_die ()->die_child = die;
20585 }
20586 }
20587 #endif /* VMS_DEBUGGING_INFO */
20588
20589 /* Push a new declaration scope. */
20590
20591 static void
20592 push_decl_scope (tree scope)
20593 {
20594 vec_safe_push (decl_scope_table, scope);
20595 }
20596
20597 /* Pop a declaration scope. */
20598
20599 static inline void
20600 pop_decl_scope (void)
20601 {
20602 decl_scope_table->pop ();
20603 }
20604
20605 /* walk_tree helper function for uses_local_type, below. */
20606
20607 static tree
20608 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20609 {
20610 if (!TYPE_P (*tp))
20611 *walk_subtrees = 0;
20612 else
20613 {
20614 tree name = TYPE_NAME (*tp);
20615 if (name && DECL_P (name) && decl_function_context (name))
20616 return *tp;
20617 }
20618 return NULL_TREE;
20619 }
20620
20621 /* If TYPE involves a function-local type (including a local typedef to a
20622 non-local type), returns that type; otherwise returns NULL_TREE. */
20623
20624 static tree
20625 uses_local_type (tree type)
20626 {
20627 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20628 return used;
20629 }
20630
20631 /* Return the DIE for the scope that immediately contains this type.
20632 Non-named types that do not involve a function-local type get global
20633 scope. Named types nested in namespaces or other types get their
20634 containing scope. All other types (i.e. function-local named types) get
20635 the current active scope. */
20636
20637 static dw_die_ref
20638 scope_die_for (tree t, dw_die_ref context_die)
20639 {
20640 dw_die_ref scope_die = NULL;
20641 tree containing_scope;
20642
20643 /* Non-types always go in the current scope. */
20644 gcc_assert (TYPE_P (t));
20645
20646 /* Use the scope of the typedef, rather than the scope of the type
20647 it refers to. */
20648 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20649 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20650 else
20651 containing_scope = TYPE_CONTEXT (t);
20652
20653 /* Use the containing namespace if there is one. */
20654 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20655 {
20656 if (context_die == lookup_decl_die (containing_scope))
20657 /* OK */;
20658 else if (debug_info_level > DINFO_LEVEL_TERSE)
20659 context_die = get_context_die (containing_scope);
20660 else
20661 containing_scope = NULL_TREE;
20662 }
20663
20664 /* Ignore function type "scopes" from the C frontend. They mean that
20665 a tagged type is local to a parmlist of a function declarator, but
20666 that isn't useful to DWARF. */
20667 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20668 containing_scope = NULL_TREE;
20669
20670 if (SCOPE_FILE_SCOPE_P (containing_scope))
20671 {
20672 /* If T uses a local type keep it local as well, to avoid references
20673 to function-local DIEs from outside the function. */
20674 if (current_function_decl && uses_local_type (t))
20675 scope_die = context_die;
20676 else
20677 scope_die = comp_unit_die ();
20678 }
20679 else if (TYPE_P (containing_scope))
20680 {
20681 /* For types, we can just look up the appropriate DIE. */
20682 if (debug_info_level > DINFO_LEVEL_TERSE)
20683 scope_die = get_context_die (containing_scope);
20684 else
20685 {
20686 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20687 if (scope_die == NULL)
20688 scope_die = comp_unit_die ();
20689 }
20690 }
20691 else
20692 scope_die = context_die;
20693
20694 return scope_die;
20695 }
20696
20697 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20698
20699 static inline int
20700 local_scope_p (dw_die_ref context_die)
20701 {
20702 for (; context_die; context_die = context_die->die_parent)
20703 if (context_die->die_tag == DW_TAG_inlined_subroutine
20704 || context_die->die_tag == DW_TAG_subprogram)
20705 return 1;
20706
20707 return 0;
20708 }
20709
20710 /* Returns nonzero if CONTEXT_DIE is a class. */
20711
20712 static inline int
20713 class_scope_p (dw_die_ref context_die)
20714 {
20715 return (context_die
20716 && (context_die->die_tag == DW_TAG_structure_type
20717 || context_die->die_tag == DW_TAG_class_type
20718 || context_die->die_tag == DW_TAG_interface_type
20719 || context_die->die_tag == DW_TAG_union_type));
20720 }
20721
20722 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20723 whether or not to treat a DIE in this context as a declaration. */
20724
20725 static inline int
20726 class_or_namespace_scope_p (dw_die_ref context_die)
20727 {
20728 return (class_scope_p (context_die)
20729 || (context_die && context_die->die_tag == DW_TAG_namespace));
20730 }
20731
20732 /* Many forms of DIEs require a "type description" attribute. This
20733 routine locates the proper "type descriptor" die for the type given
20734 by 'type' plus any additional qualifiers given by 'cv_quals', and
20735 adds a DW_AT_type attribute below the given die. */
20736
20737 static void
20738 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20739 bool reverse, dw_die_ref context_die)
20740 {
20741 enum tree_code code = TREE_CODE (type);
20742 dw_die_ref type_die = NULL;
20743
20744 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20745 or fixed-point type, use the inner type. This is because we have no
20746 support for unnamed types in base_type_die. This can happen if this is
20747 an Ada subrange type. Correct solution is emit a subrange type die. */
20748 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20749 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20750 type = TREE_TYPE (type), code = TREE_CODE (type);
20751
20752 if (code == ERROR_MARK
20753 /* Handle a special case. For functions whose return type is void, we
20754 generate *no* type attribute. (Note that no object may have type
20755 `void', so this only applies to function return types). */
20756 || code == VOID_TYPE)
20757 return;
20758
20759 type_die = modified_type_die (type,
20760 cv_quals | TYPE_QUALS (type),
20761 reverse,
20762 context_die);
20763
20764 if (type_die != NULL)
20765 add_AT_die_ref (object_die, DW_AT_type, type_die);
20766 }
20767
20768 /* Given an object die, add the calling convention attribute for the
20769 function call type. */
20770 static void
20771 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20772 {
20773 enum dwarf_calling_convention value = DW_CC_normal;
20774
20775 value = ((enum dwarf_calling_convention)
20776 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20777
20778 if (is_fortran ()
20779 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20780 {
20781 /* DWARF 2 doesn't provide a way to identify a program's source-level
20782 entry point. DW_AT_calling_convention attributes are only meant
20783 to describe functions' calling conventions. However, lacking a
20784 better way to signal the Fortran main program, we used this for
20785 a long time, following existing custom. Now, DWARF 4 has
20786 DW_AT_main_subprogram, which we add below, but some tools still
20787 rely on the old way, which we thus keep. */
20788 value = DW_CC_program;
20789
20790 if (dwarf_version >= 4 || !dwarf_strict)
20791 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20792 }
20793
20794 /* Only add the attribute if the backend requests it, and
20795 is not DW_CC_normal. */
20796 if (value && (value != DW_CC_normal))
20797 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20798 }
20799
20800 /* Given a tree pointer to a struct, class, union, or enum type node, return
20801 a pointer to the (string) tag name for the given type, or zero if the type
20802 was declared without a tag. */
20803
20804 static const char *
20805 type_tag (const_tree type)
20806 {
20807 const char *name = 0;
20808
20809 if (TYPE_NAME (type) != 0)
20810 {
20811 tree t = 0;
20812
20813 /* Find the IDENTIFIER_NODE for the type name. */
20814 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20815 && !TYPE_NAMELESS (type))
20816 t = TYPE_NAME (type);
20817
20818 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20819 a TYPE_DECL node, regardless of whether or not a `typedef' was
20820 involved. */
20821 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20822 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20823 {
20824 /* We want to be extra verbose. Don't call dwarf_name if
20825 DECL_NAME isn't set. The default hook for decl_printable_name
20826 doesn't like that, and in this context it's correct to return
20827 0, instead of "<anonymous>" or the like. */
20828 if (DECL_NAME (TYPE_NAME (type))
20829 && !DECL_NAMELESS (TYPE_NAME (type)))
20830 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20831 }
20832
20833 /* Now get the name as a string, or invent one. */
20834 if (!name && t != 0)
20835 name = IDENTIFIER_POINTER (t);
20836 }
20837
20838 return (name == 0 || *name == '\0') ? 0 : name;
20839 }
20840
20841 /* Return the type associated with a data member, make a special check
20842 for bit field types. */
20843
20844 static inline tree
20845 member_declared_type (const_tree member)
20846 {
20847 return (DECL_BIT_FIELD_TYPE (member)
20848 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20849 }
20850
20851 /* Get the decl's label, as described by its RTL. This may be different
20852 from the DECL_NAME name used in the source file. */
20853
20854 #if 0
20855 static const char *
20856 decl_start_label (tree decl)
20857 {
20858 rtx x;
20859 const char *fnname;
20860
20861 x = DECL_RTL (decl);
20862 gcc_assert (MEM_P (x));
20863
20864 x = XEXP (x, 0);
20865 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20866
20867 fnname = XSTR (x, 0);
20868 return fnname;
20869 }
20870 #endif
20871 \f
20872 /* For variable-length arrays that have been previously generated, but
20873 may be incomplete due to missing subscript info, fill the subscript
20874 info. Return TRUE if this is one of those cases. */
20875 static bool
20876 fill_variable_array_bounds (tree type)
20877 {
20878 if (TREE_ASM_WRITTEN (type)
20879 && TREE_CODE (type) == ARRAY_TYPE
20880 && variably_modified_type_p (type, NULL))
20881 {
20882 dw_die_ref array_die = lookup_type_die (type);
20883 if (!array_die)
20884 return false;
20885 add_subscript_info (array_die, type, !is_ada ());
20886 return true;
20887 }
20888 return false;
20889 }
20890
20891 /* These routines generate the internal representation of the DIE's for
20892 the compilation unit. Debugging information is collected by walking
20893 the declaration trees passed in from dwarf2out_decl(). */
20894
20895 static void
20896 gen_array_type_die (tree type, dw_die_ref context_die)
20897 {
20898 dw_die_ref array_die;
20899
20900 /* GNU compilers represent multidimensional array types as sequences of one
20901 dimensional array types whose element types are themselves array types.
20902 We sometimes squish that down to a single array_type DIE with multiple
20903 subscripts in the Dwarf debugging info. The draft Dwarf specification
20904 say that we are allowed to do this kind of compression in C, because
20905 there is no difference between an array of arrays and a multidimensional
20906 array. We don't do this for Ada to remain as close as possible to the
20907 actual representation, which is especially important against the language
20908 flexibilty wrt arrays of variable size. */
20909
20910 bool collapse_nested_arrays = !is_ada ();
20911
20912 if (fill_variable_array_bounds (type))
20913 return;
20914
20915 dw_die_ref scope_die = scope_die_for (type, context_die);
20916 tree element_type;
20917
20918 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20919 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20920 if (TYPE_STRING_FLAG (type)
20921 && TREE_CODE (type) == ARRAY_TYPE
20922 && is_fortran ()
20923 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20924 {
20925 HOST_WIDE_INT size;
20926
20927 array_die = new_die (DW_TAG_string_type, scope_die, type);
20928 add_name_attribute (array_die, type_tag (type));
20929 equate_type_number_to_die (type, array_die);
20930 size = int_size_in_bytes (type);
20931 if (size >= 0)
20932 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20933 /* ??? We can't annotate types late, but for LTO we may not
20934 generate a location early either (gfortran.dg/save_6.f90). */
20935 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20936 && TYPE_DOMAIN (type) != NULL_TREE
20937 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20938 {
20939 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20940 tree rszdecl = szdecl;
20941
20942 size = int_size_in_bytes (TREE_TYPE (szdecl));
20943 if (!DECL_P (szdecl))
20944 {
20945 if (TREE_CODE (szdecl) == INDIRECT_REF
20946 && DECL_P (TREE_OPERAND (szdecl, 0)))
20947 {
20948 rszdecl = TREE_OPERAND (szdecl, 0);
20949 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20950 != DWARF2_ADDR_SIZE)
20951 size = 0;
20952 }
20953 else
20954 size = 0;
20955 }
20956 if (size > 0)
20957 {
20958 dw_loc_list_ref loc
20959 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20960 NULL);
20961 if (loc)
20962 {
20963 add_AT_location_description (array_die, DW_AT_string_length,
20964 loc);
20965 if (size != DWARF2_ADDR_SIZE)
20966 add_AT_unsigned (array_die, dwarf_version >= 5
20967 ? DW_AT_string_length_byte_size
20968 : DW_AT_byte_size, size);
20969 }
20970 }
20971 }
20972 return;
20973 }
20974
20975 array_die = new_die (DW_TAG_array_type, scope_die, type);
20976 add_name_attribute (array_die, type_tag (type));
20977 equate_type_number_to_die (type, array_die);
20978
20979 if (TREE_CODE (type) == VECTOR_TYPE)
20980 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20981
20982 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20983 if (is_fortran ()
20984 && TREE_CODE (type) == ARRAY_TYPE
20985 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20986 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20987 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20988
20989 #if 0
20990 /* We default the array ordering. Debuggers will probably do the right
20991 things even if DW_AT_ordering is not present. It's not even an issue
20992 until we start to get into multidimensional arrays anyway. If a debugger
20993 is ever caught doing the Wrong Thing for multi-dimensional arrays,
20994 then we'll have to put the DW_AT_ordering attribute back in. (But if
20995 and when we find out that we need to put these in, we will only do so
20996 for multidimensional arrays. */
20997 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20998 #endif
20999
21000 if (TREE_CODE (type) == VECTOR_TYPE)
21001 {
21002 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21003 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21004 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21005 add_bound_info (subrange_die, DW_AT_upper_bound,
21006 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21007 }
21008 else
21009 add_subscript_info (array_die, type, collapse_nested_arrays);
21010
21011 /* Add representation of the type of the elements of this array type and
21012 emit the corresponding DIE if we haven't done it already. */
21013 element_type = TREE_TYPE (type);
21014 if (collapse_nested_arrays)
21015 while (TREE_CODE (element_type) == ARRAY_TYPE)
21016 {
21017 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21018 break;
21019 element_type = TREE_TYPE (element_type);
21020 }
21021
21022 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21023 TREE_CODE (type) == ARRAY_TYPE
21024 && TYPE_REVERSE_STORAGE_ORDER (type),
21025 context_die);
21026
21027 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21028 if (TYPE_ARTIFICIAL (type))
21029 add_AT_flag (array_die, DW_AT_artificial, 1);
21030
21031 if (get_AT (array_die, DW_AT_name))
21032 add_pubtype (type, array_die);
21033
21034 add_alignment_attribute (array_die, type);
21035 }
21036
21037 /* This routine generates DIE for array with hidden descriptor, details
21038 are filled into *info by a langhook. */
21039
21040 static void
21041 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21042 dw_die_ref context_die)
21043 {
21044 const dw_die_ref scope_die = scope_die_for (type, context_die);
21045 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21046 struct loc_descr_context context = { type, info->base_decl, NULL,
21047 false, false };
21048 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21049 int dim;
21050
21051 add_name_attribute (array_die, type_tag (type));
21052 equate_type_number_to_die (type, array_die);
21053
21054 if (info->ndimensions > 1)
21055 switch (info->ordering)
21056 {
21057 case array_descr_ordering_row_major:
21058 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21059 break;
21060 case array_descr_ordering_column_major:
21061 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21062 break;
21063 default:
21064 break;
21065 }
21066
21067 if (dwarf_version >= 3 || !dwarf_strict)
21068 {
21069 if (info->data_location)
21070 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21071 dw_scalar_form_exprloc, &context);
21072 if (info->associated)
21073 add_scalar_info (array_die, DW_AT_associated, info->associated,
21074 dw_scalar_form_constant
21075 | dw_scalar_form_exprloc
21076 | dw_scalar_form_reference, &context);
21077 if (info->allocated)
21078 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21079 dw_scalar_form_constant
21080 | dw_scalar_form_exprloc
21081 | dw_scalar_form_reference, &context);
21082 if (info->stride)
21083 {
21084 const enum dwarf_attribute attr
21085 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21086 const int forms
21087 = (info->stride_in_bits)
21088 ? dw_scalar_form_constant
21089 : (dw_scalar_form_constant
21090 | dw_scalar_form_exprloc
21091 | dw_scalar_form_reference);
21092
21093 add_scalar_info (array_die, attr, info->stride, forms, &context);
21094 }
21095 }
21096 if (dwarf_version >= 5)
21097 {
21098 if (info->rank)
21099 {
21100 add_scalar_info (array_die, DW_AT_rank, info->rank,
21101 dw_scalar_form_constant
21102 | dw_scalar_form_exprloc, &context);
21103 subrange_tag = DW_TAG_generic_subrange;
21104 context.placeholder_arg = true;
21105 }
21106 }
21107
21108 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21109
21110 for (dim = 0; dim < info->ndimensions; dim++)
21111 {
21112 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21113
21114 if (info->dimen[dim].bounds_type)
21115 add_type_attribute (subrange_die,
21116 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21117 false, context_die);
21118 if (info->dimen[dim].lower_bound)
21119 add_bound_info (subrange_die, DW_AT_lower_bound,
21120 info->dimen[dim].lower_bound, &context);
21121 if (info->dimen[dim].upper_bound)
21122 add_bound_info (subrange_die, DW_AT_upper_bound,
21123 info->dimen[dim].upper_bound, &context);
21124 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21125 add_scalar_info (subrange_die, DW_AT_byte_stride,
21126 info->dimen[dim].stride,
21127 dw_scalar_form_constant
21128 | dw_scalar_form_exprloc
21129 | dw_scalar_form_reference,
21130 &context);
21131 }
21132
21133 gen_type_die (info->element_type, context_die);
21134 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21135 TREE_CODE (type) == ARRAY_TYPE
21136 && TYPE_REVERSE_STORAGE_ORDER (type),
21137 context_die);
21138
21139 if (get_AT (array_die, DW_AT_name))
21140 add_pubtype (type, array_die);
21141
21142 add_alignment_attribute (array_die, type);
21143 }
21144
21145 #if 0
21146 static void
21147 gen_entry_point_die (tree decl, dw_die_ref context_die)
21148 {
21149 tree origin = decl_ultimate_origin (decl);
21150 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21151
21152 if (origin != NULL)
21153 add_abstract_origin_attribute (decl_die, origin);
21154 else
21155 {
21156 add_name_and_src_coords_attributes (decl_die, decl);
21157 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21158 TYPE_UNQUALIFIED, false, context_die);
21159 }
21160
21161 if (DECL_ABSTRACT_P (decl))
21162 equate_decl_number_to_die (decl, decl_die);
21163 else
21164 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21165 }
21166 #endif
21167
21168 /* Walk through the list of incomplete types again, trying once more to
21169 emit full debugging info for them. */
21170
21171 static void
21172 retry_incomplete_types (void)
21173 {
21174 set_early_dwarf s;
21175 int i;
21176
21177 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21178 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21179 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21180 vec_safe_truncate (incomplete_types, 0);
21181 }
21182
21183 /* Determine what tag to use for a record type. */
21184
21185 static enum dwarf_tag
21186 record_type_tag (tree type)
21187 {
21188 if (! lang_hooks.types.classify_record)
21189 return DW_TAG_structure_type;
21190
21191 switch (lang_hooks.types.classify_record (type))
21192 {
21193 case RECORD_IS_STRUCT:
21194 return DW_TAG_structure_type;
21195
21196 case RECORD_IS_CLASS:
21197 return DW_TAG_class_type;
21198
21199 case RECORD_IS_INTERFACE:
21200 if (dwarf_version >= 3 || !dwarf_strict)
21201 return DW_TAG_interface_type;
21202 return DW_TAG_structure_type;
21203
21204 default:
21205 gcc_unreachable ();
21206 }
21207 }
21208
21209 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21210 include all of the information about the enumeration values also. Each
21211 enumerated type name/value is listed as a child of the enumerated type
21212 DIE. */
21213
21214 static dw_die_ref
21215 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21216 {
21217 dw_die_ref type_die = lookup_type_die (type);
21218
21219 if (type_die == NULL)
21220 {
21221 type_die = new_die (DW_TAG_enumeration_type,
21222 scope_die_for (type, context_die), type);
21223 equate_type_number_to_die (type, type_die);
21224 add_name_attribute (type_die, type_tag (type));
21225 if (dwarf_version >= 4 || !dwarf_strict)
21226 {
21227 if (ENUM_IS_SCOPED (type))
21228 add_AT_flag (type_die, DW_AT_enum_class, 1);
21229 if (ENUM_IS_OPAQUE (type))
21230 add_AT_flag (type_die, DW_AT_declaration, 1);
21231 }
21232 if (!dwarf_strict)
21233 add_AT_unsigned (type_die, DW_AT_encoding,
21234 TYPE_UNSIGNED (type)
21235 ? DW_ATE_unsigned
21236 : DW_ATE_signed);
21237 }
21238 else if (! TYPE_SIZE (type))
21239 return type_die;
21240 else
21241 remove_AT (type_die, DW_AT_declaration);
21242
21243 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21244 given enum type is incomplete, do not generate the DW_AT_byte_size
21245 attribute or the DW_AT_element_list attribute. */
21246 if (TYPE_SIZE (type))
21247 {
21248 tree link;
21249
21250 TREE_ASM_WRITTEN (type) = 1;
21251 add_byte_size_attribute (type_die, type);
21252 add_alignment_attribute (type_die, type);
21253 if (dwarf_version >= 3 || !dwarf_strict)
21254 {
21255 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21256 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21257 context_die);
21258 }
21259 if (TYPE_STUB_DECL (type) != NULL_TREE)
21260 {
21261 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21262 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21263 }
21264
21265 /* If the first reference to this type was as the return type of an
21266 inline function, then it may not have a parent. Fix this now. */
21267 if (type_die->die_parent == NULL)
21268 add_child_die (scope_die_for (type, context_die), type_die);
21269
21270 for (link = TYPE_VALUES (type);
21271 link != NULL; link = TREE_CHAIN (link))
21272 {
21273 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21274 tree value = TREE_VALUE (link);
21275
21276 add_name_attribute (enum_die,
21277 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21278
21279 if (TREE_CODE (value) == CONST_DECL)
21280 value = DECL_INITIAL (value);
21281
21282 if (simple_type_size_in_bits (TREE_TYPE (value))
21283 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21284 {
21285 /* For constant forms created by add_AT_unsigned DWARF
21286 consumers (GDB, elfutils, etc.) always zero extend
21287 the value. Only when the actual value is negative
21288 do we need to use add_AT_int to generate a constant
21289 form that can represent negative values. */
21290 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21291 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21292 add_AT_unsigned (enum_die, DW_AT_const_value,
21293 (unsigned HOST_WIDE_INT) val);
21294 else
21295 add_AT_int (enum_die, DW_AT_const_value, val);
21296 }
21297 else
21298 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21299 that here. TODO: This should be re-worked to use correct
21300 signed/unsigned double tags for all cases. */
21301 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21302 }
21303
21304 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21305 if (TYPE_ARTIFICIAL (type))
21306 add_AT_flag (type_die, DW_AT_artificial, 1);
21307 }
21308 else
21309 add_AT_flag (type_die, DW_AT_declaration, 1);
21310
21311 add_pubtype (type, type_die);
21312
21313 return type_die;
21314 }
21315
21316 /* Generate a DIE to represent either a real live formal parameter decl or to
21317 represent just the type of some formal parameter position in some function
21318 type.
21319
21320 Note that this routine is a bit unusual because its argument may be a
21321 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21322 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21323 node. If it's the former then this function is being called to output a
21324 DIE to represent a formal parameter object (or some inlining thereof). If
21325 it's the latter, then this function is only being called to output a
21326 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21327 argument type of some subprogram type.
21328 If EMIT_NAME_P is true, name and source coordinate attributes
21329 are emitted. */
21330
21331 static dw_die_ref
21332 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21333 dw_die_ref context_die)
21334 {
21335 tree node_or_origin = node ? node : origin;
21336 tree ultimate_origin;
21337 dw_die_ref parm_die = NULL;
21338
21339 if (DECL_P (node_or_origin))
21340 {
21341 parm_die = lookup_decl_die (node);
21342
21343 /* If the contexts differ, we may not be talking about the same
21344 thing.
21345 ??? When in LTO the DIE parent is the "abstract" copy and the
21346 context_die is the specification "copy". But this whole block
21347 should eventually be no longer needed. */
21348 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21349 {
21350 if (!DECL_ABSTRACT_P (node))
21351 {
21352 /* This can happen when creating an inlined instance, in
21353 which case we need to create a new DIE that will get
21354 annotated with DW_AT_abstract_origin. */
21355 parm_die = NULL;
21356 }
21357 else
21358 gcc_unreachable ();
21359 }
21360
21361 if (parm_die && parm_die->die_parent == NULL)
21362 {
21363 /* Check that parm_die already has the right attributes that
21364 we would have added below. If any attributes are
21365 missing, fall through to add them. */
21366 if (! DECL_ABSTRACT_P (node_or_origin)
21367 && !get_AT (parm_die, DW_AT_location)
21368 && !get_AT (parm_die, DW_AT_const_value))
21369 /* We are missing location info, and are about to add it. */
21370 ;
21371 else
21372 {
21373 add_child_die (context_die, parm_die);
21374 return parm_die;
21375 }
21376 }
21377 }
21378
21379 /* If we have a previously generated DIE, use it, unless this is an
21380 concrete instance (origin != NULL), in which case we need a new
21381 DIE with a corresponding DW_AT_abstract_origin. */
21382 bool reusing_die;
21383 if (parm_die && origin == NULL)
21384 reusing_die = true;
21385 else
21386 {
21387 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21388 reusing_die = false;
21389 }
21390
21391 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21392 {
21393 case tcc_declaration:
21394 ultimate_origin = decl_ultimate_origin (node_or_origin);
21395 if (node || ultimate_origin)
21396 origin = ultimate_origin;
21397
21398 if (reusing_die)
21399 goto add_location;
21400
21401 if (origin != NULL)
21402 add_abstract_origin_attribute (parm_die, origin);
21403 else if (emit_name_p)
21404 add_name_and_src_coords_attributes (parm_die, node);
21405 if (origin == NULL
21406 || (! DECL_ABSTRACT_P (node_or_origin)
21407 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21408 decl_function_context
21409 (node_or_origin))))
21410 {
21411 tree type = TREE_TYPE (node_or_origin);
21412 if (decl_by_reference_p (node_or_origin))
21413 add_type_attribute (parm_die, TREE_TYPE (type),
21414 TYPE_UNQUALIFIED,
21415 false, context_die);
21416 else
21417 add_type_attribute (parm_die, type,
21418 decl_quals (node_or_origin),
21419 false, context_die);
21420 }
21421 if (origin == NULL && DECL_ARTIFICIAL (node))
21422 add_AT_flag (parm_die, DW_AT_artificial, 1);
21423 add_location:
21424 if (node && node != origin)
21425 equate_decl_number_to_die (node, parm_die);
21426 if (! DECL_ABSTRACT_P (node_or_origin))
21427 add_location_or_const_value_attribute (parm_die, node_or_origin,
21428 node == NULL);
21429
21430 break;
21431
21432 case tcc_type:
21433 /* We were called with some kind of a ..._TYPE node. */
21434 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21435 context_die);
21436 break;
21437
21438 default:
21439 gcc_unreachable ();
21440 }
21441
21442 return parm_die;
21443 }
21444
21445 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21446 children DW_TAG_formal_parameter DIEs representing the arguments of the
21447 parameter pack.
21448
21449 PARM_PACK must be a function parameter pack.
21450 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21451 must point to the subsequent arguments of the function PACK_ARG belongs to.
21452 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21453 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21454 following the last one for which a DIE was generated. */
21455
21456 static dw_die_ref
21457 gen_formal_parameter_pack_die (tree parm_pack,
21458 tree pack_arg,
21459 dw_die_ref subr_die,
21460 tree *next_arg)
21461 {
21462 tree arg;
21463 dw_die_ref parm_pack_die;
21464
21465 gcc_assert (parm_pack
21466 && lang_hooks.function_parameter_pack_p (parm_pack)
21467 && subr_die);
21468
21469 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21470 add_src_coords_attributes (parm_pack_die, parm_pack);
21471
21472 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21473 {
21474 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21475 parm_pack))
21476 break;
21477 gen_formal_parameter_die (arg, NULL,
21478 false /* Don't emit name attribute. */,
21479 parm_pack_die);
21480 }
21481 if (next_arg)
21482 *next_arg = arg;
21483 return parm_pack_die;
21484 }
21485
21486 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21487 at the end of an (ANSI prototyped) formal parameters list. */
21488
21489 static void
21490 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21491 {
21492 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21493 }
21494
21495 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21496 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21497 parameters as specified in some function type specification (except for
21498 those which appear as part of a function *definition*). */
21499
21500 static void
21501 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21502 {
21503 tree link;
21504 tree formal_type = NULL;
21505 tree first_parm_type;
21506 tree arg;
21507
21508 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21509 {
21510 arg = DECL_ARGUMENTS (function_or_method_type);
21511 function_or_method_type = TREE_TYPE (function_or_method_type);
21512 }
21513 else
21514 arg = NULL_TREE;
21515
21516 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21517
21518 /* Make our first pass over the list of formal parameter types and output a
21519 DW_TAG_formal_parameter DIE for each one. */
21520 for (link = first_parm_type; link; )
21521 {
21522 dw_die_ref parm_die;
21523
21524 formal_type = TREE_VALUE (link);
21525 if (formal_type == void_type_node)
21526 break;
21527
21528 /* Output a (nameless) DIE to represent the formal parameter itself. */
21529 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21530 {
21531 parm_die = gen_formal_parameter_die (formal_type, NULL,
21532 true /* Emit name attribute. */,
21533 context_die);
21534 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21535 && link == first_parm_type)
21536 {
21537 add_AT_flag (parm_die, DW_AT_artificial, 1);
21538 if (dwarf_version >= 3 || !dwarf_strict)
21539 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21540 }
21541 else if (arg && DECL_ARTIFICIAL (arg))
21542 add_AT_flag (parm_die, DW_AT_artificial, 1);
21543 }
21544
21545 link = TREE_CHAIN (link);
21546 if (arg)
21547 arg = DECL_CHAIN (arg);
21548 }
21549
21550 /* If this function type has an ellipsis, add a
21551 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21552 if (formal_type != void_type_node)
21553 gen_unspecified_parameters_die (function_or_method_type, context_die);
21554
21555 /* Make our second (and final) pass over the list of formal parameter types
21556 and output DIEs to represent those types (as necessary). */
21557 for (link = TYPE_ARG_TYPES (function_or_method_type);
21558 link && TREE_VALUE (link);
21559 link = TREE_CHAIN (link))
21560 gen_type_die (TREE_VALUE (link), context_die);
21561 }
21562
21563 /* We want to generate the DIE for TYPE so that we can generate the
21564 die for MEMBER, which has been defined; we will need to refer back
21565 to the member declaration nested within TYPE. If we're trying to
21566 generate minimal debug info for TYPE, processing TYPE won't do the
21567 trick; we need to attach the member declaration by hand. */
21568
21569 static void
21570 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21571 {
21572 gen_type_die (type, context_die);
21573
21574 /* If we're trying to avoid duplicate debug info, we may not have
21575 emitted the member decl for this function. Emit it now. */
21576 if (TYPE_STUB_DECL (type)
21577 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21578 && ! lookup_decl_die (member))
21579 {
21580 dw_die_ref type_die;
21581 gcc_assert (!decl_ultimate_origin (member));
21582
21583 push_decl_scope (type);
21584 type_die = lookup_type_die_strip_naming_typedef (type);
21585 if (TREE_CODE (member) == FUNCTION_DECL)
21586 gen_subprogram_die (member, type_die);
21587 else if (TREE_CODE (member) == FIELD_DECL)
21588 {
21589 /* Ignore the nameless fields that are used to skip bits but handle
21590 C++ anonymous unions and structs. */
21591 if (DECL_NAME (member) != NULL_TREE
21592 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21593 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21594 {
21595 struct vlr_context vlr_ctx = {
21596 DECL_CONTEXT (member), /* struct_type */
21597 NULL_TREE /* variant_part_offset */
21598 };
21599 gen_type_die (member_declared_type (member), type_die);
21600 gen_field_die (member, &vlr_ctx, type_die);
21601 }
21602 }
21603 else
21604 gen_variable_die (member, NULL_TREE, type_die);
21605
21606 pop_decl_scope ();
21607 }
21608 }
21609 \f
21610 /* Forward declare these functions, because they are mutually recursive
21611 with their set_block_* pairing functions. */
21612 static void set_decl_origin_self (tree);
21613
21614 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21615 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21616 that it points to the node itself, thus indicating that the node is its
21617 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21618 the given node is NULL, recursively descend the decl/block tree which
21619 it is the root of, and for each other ..._DECL or BLOCK node contained
21620 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21621 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21622 values to point to themselves. */
21623
21624 static void
21625 set_block_origin_self (tree stmt)
21626 {
21627 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21628 {
21629 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21630
21631 {
21632 tree local_decl;
21633
21634 for (local_decl = BLOCK_VARS (stmt);
21635 local_decl != NULL_TREE;
21636 local_decl = DECL_CHAIN (local_decl))
21637 /* Do not recurse on nested functions since the inlining status
21638 of parent and child can be different as per the DWARF spec. */
21639 if (TREE_CODE (local_decl) != FUNCTION_DECL
21640 && !DECL_EXTERNAL (local_decl))
21641 set_decl_origin_self (local_decl);
21642 }
21643
21644 {
21645 tree subblock;
21646
21647 for (subblock = BLOCK_SUBBLOCKS (stmt);
21648 subblock != NULL_TREE;
21649 subblock = BLOCK_CHAIN (subblock))
21650 set_block_origin_self (subblock); /* Recurse. */
21651 }
21652 }
21653 }
21654
21655 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21656 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21657 node to so that it points to the node itself, thus indicating that the
21658 node represents its own (abstract) origin. Additionally, if the
21659 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21660 the decl/block tree of which the given node is the root of, and for
21661 each other ..._DECL or BLOCK node contained therein whose
21662 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21663 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21664 point to themselves. */
21665
21666 static void
21667 set_decl_origin_self (tree decl)
21668 {
21669 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21670 {
21671 DECL_ABSTRACT_ORIGIN (decl) = decl;
21672 if (TREE_CODE (decl) == FUNCTION_DECL)
21673 {
21674 tree arg;
21675
21676 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21677 DECL_ABSTRACT_ORIGIN (arg) = arg;
21678 if (DECL_INITIAL (decl) != NULL_TREE
21679 && DECL_INITIAL (decl) != error_mark_node)
21680 set_block_origin_self (DECL_INITIAL (decl));
21681 }
21682 }
21683 }
21684 \f
21685 /* Mark the early DIE for DECL as the abstract instance. */
21686
21687 static void
21688 dwarf2out_abstract_function (tree decl)
21689 {
21690 dw_die_ref old_die;
21691
21692 /* Make sure we have the actual abstract inline, not a clone. */
21693 decl = DECL_ORIGIN (decl);
21694
21695 if (DECL_IGNORED_P (decl))
21696 return;
21697
21698 old_die = lookup_decl_die (decl);
21699 /* With early debug we always have an old DIE unless we are in LTO
21700 and the user did not compile but only link with debug. */
21701 if (in_lto_p && ! old_die)
21702 return;
21703 gcc_assert (old_die != NULL);
21704 if (get_AT (old_die, DW_AT_inline)
21705 || get_AT (old_die, DW_AT_abstract_origin))
21706 /* We've already generated the abstract instance. */
21707 return;
21708
21709 /* Go ahead and put DW_AT_inline on the DIE. */
21710 if (DECL_DECLARED_INLINE_P (decl))
21711 {
21712 if (cgraph_function_possibly_inlined_p (decl))
21713 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21714 else
21715 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21716 }
21717 else
21718 {
21719 if (cgraph_function_possibly_inlined_p (decl))
21720 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21721 else
21722 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21723 }
21724
21725 if (DECL_DECLARED_INLINE_P (decl)
21726 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21727 add_AT_flag (old_die, DW_AT_artificial, 1);
21728
21729 set_decl_origin_self (decl);
21730 }
21731
21732 /* Helper function of premark_used_types() which gets called through
21733 htab_traverse.
21734
21735 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21736 marked as unused by prune_unused_types. */
21737
21738 bool
21739 premark_used_types_helper (tree const &type, void *)
21740 {
21741 dw_die_ref die;
21742
21743 die = lookup_type_die (type);
21744 if (die != NULL)
21745 die->die_perennial_p = 1;
21746 return true;
21747 }
21748
21749 /* Helper function of premark_types_used_by_global_vars which gets called
21750 through htab_traverse.
21751
21752 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21753 marked as unused by prune_unused_types. The DIE of the type is marked
21754 only if the global variable using the type will actually be emitted. */
21755
21756 int
21757 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21758 void *)
21759 {
21760 struct types_used_by_vars_entry *entry;
21761 dw_die_ref die;
21762
21763 entry = (struct types_used_by_vars_entry *) *slot;
21764 gcc_assert (entry->type != NULL
21765 && entry->var_decl != NULL);
21766 die = lookup_type_die (entry->type);
21767 if (die)
21768 {
21769 /* Ask cgraph if the global variable really is to be emitted.
21770 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21771 varpool_node *node = varpool_node::get (entry->var_decl);
21772 if (node && node->definition)
21773 {
21774 die->die_perennial_p = 1;
21775 /* Keep the parent DIEs as well. */
21776 while ((die = die->die_parent) && die->die_perennial_p == 0)
21777 die->die_perennial_p = 1;
21778 }
21779 }
21780 return 1;
21781 }
21782
21783 /* Mark all members of used_types_hash as perennial. */
21784
21785 static void
21786 premark_used_types (struct function *fun)
21787 {
21788 if (fun && fun->used_types_hash)
21789 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21790 }
21791
21792 /* Mark all members of types_used_by_vars_entry as perennial. */
21793
21794 static void
21795 premark_types_used_by_global_vars (void)
21796 {
21797 if (types_used_by_vars_hash)
21798 types_used_by_vars_hash
21799 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21800 }
21801
21802 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21803 for CA_LOC call arg loc node. */
21804
21805 static dw_die_ref
21806 gen_call_site_die (tree decl, dw_die_ref subr_die,
21807 struct call_arg_loc_node *ca_loc)
21808 {
21809 dw_die_ref stmt_die = NULL, die;
21810 tree block = ca_loc->block;
21811
21812 while (block
21813 && block != DECL_INITIAL (decl)
21814 && TREE_CODE (block) == BLOCK)
21815 {
21816 stmt_die = BLOCK_DIE (block);
21817 if (stmt_die)
21818 break;
21819 block = BLOCK_SUPERCONTEXT (block);
21820 }
21821 if (stmt_die == NULL)
21822 stmt_die = subr_die;
21823 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21824 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21825 if (ca_loc->tail_call_p)
21826 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21827 if (ca_loc->symbol_ref)
21828 {
21829 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21830 if (tdie)
21831 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21832 else
21833 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21834 false);
21835 }
21836 return die;
21837 }
21838
21839 /* Generate a DIE to represent a declared function (either file-scope or
21840 block-local). */
21841
21842 static void
21843 gen_subprogram_die (tree decl, dw_die_ref context_die)
21844 {
21845 tree origin = decl_ultimate_origin (decl);
21846 dw_die_ref subr_die;
21847 dw_die_ref old_die = lookup_decl_die (decl);
21848
21849 /* This function gets called multiple times for different stages of
21850 the debug process. For example, for func() in this code:
21851
21852 namespace S
21853 {
21854 void func() { ... }
21855 }
21856
21857 ...we get called 4 times. Twice in early debug and twice in
21858 late debug:
21859
21860 Early debug
21861 -----------
21862
21863 1. Once while generating func() within the namespace. This is
21864 the declaration. The declaration bit below is set, as the
21865 context is the namespace.
21866
21867 A new DIE will be generated with DW_AT_declaration set.
21868
21869 2. Once for func() itself. This is the specification. The
21870 declaration bit below is clear as the context is the CU.
21871
21872 We will use the cached DIE from (1) to create a new DIE with
21873 DW_AT_specification pointing to the declaration in (1).
21874
21875 Late debug via rest_of_handle_final()
21876 -------------------------------------
21877
21878 3. Once generating func() within the namespace. This is also the
21879 declaration, as in (1), but this time we will early exit below
21880 as we have a cached DIE and a declaration needs no additional
21881 annotations (no locations), as the source declaration line
21882 info is enough.
21883
21884 4. Once for func() itself. As in (2), this is the specification,
21885 but this time we will re-use the cached DIE, and just annotate
21886 it with the location information that should now be available.
21887
21888 For something without namespaces, but with abstract instances, we
21889 are also called a multiple times:
21890
21891 class Base
21892 {
21893 public:
21894 Base (); // constructor declaration (1)
21895 };
21896
21897 Base::Base () { } // constructor specification (2)
21898
21899 Early debug
21900 -----------
21901
21902 1. Once for the Base() constructor by virtue of it being a
21903 member of the Base class. This is done via
21904 rest_of_type_compilation.
21905
21906 This is a declaration, so a new DIE will be created with
21907 DW_AT_declaration.
21908
21909 2. Once for the Base() constructor definition, but this time
21910 while generating the abstract instance of the base
21911 constructor (__base_ctor) which is being generated via early
21912 debug of reachable functions.
21913
21914 Even though we have a cached version of the declaration (1),
21915 we will create a DW_AT_specification of the declaration DIE
21916 in (1).
21917
21918 3. Once for the __base_ctor itself, but this time, we generate
21919 an DW_AT_abstract_origin version of the DW_AT_specification in
21920 (2).
21921
21922 Late debug via rest_of_handle_final
21923 -----------------------------------
21924
21925 4. One final time for the __base_ctor (which will have a cached
21926 DIE with DW_AT_abstract_origin created in (3). This time,
21927 we will just annotate the location information now
21928 available.
21929 */
21930 int declaration = (current_function_decl != decl
21931 || class_or_namespace_scope_p (context_die));
21932
21933 /* Now that the C++ front end lazily declares artificial member fns, we
21934 might need to retrofit the declaration into its class. */
21935 if (!declaration && !origin && !old_die
21936 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21937 && !class_or_namespace_scope_p (context_die)
21938 && debug_info_level > DINFO_LEVEL_TERSE)
21939 old_die = force_decl_die (decl);
21940
21941 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21942 if (origin != NULL)
21943 {
21944 gcc_assert (!declaration || local_scope_p (context_die));
21945
21946 /* Fixup die_parent for the abstract instance of a nested
21947 inline function. */
21948 if (old_die && old_die->die_parent == NULL)
21949 add_child_die (context_die, old_die);
21950
21951 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21952 {
21953 /* If we have a DW_AT_abstract_origin we have a working
21954 cached version. */
21955 subr_die = old_die;
21956 }
21957 else
21958 {
21959 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21960 add_abstract_origin_attribute (subr_die, origin);
21961 /* This is where the actual code for a cloned function is.
21962 Let's emit linkage name attribute for it. This helps
21963 debuggers to e.g, set breakpoints into
21964 constructors/destructors when the user asks "break
21965 K::K". */
21966 add_linkage_name (subr_die, decl);
21967 }
21968 }
21969 /* A cached copy, possibly from early dwarf generation. Reuse as
21970 much as possible. */
21971 else if (old_die)
21972 {
21973 /* A declaration that has been previously dumped needs no
21974 additional information. */
21975 if (declaration)
21976 return;
21977
21978 if (!get_AT_flag (old_die, DW_AT_declaration)
21979 /* We can have a normal definition following an inline one in the
21980 case of redefinition of GNU C extern inlines.
21981 It seems reasonable to use AT_specification in this case. */
21982 && !get_AT (old_die, DW_AT_inline))
21983 {
21984 /* Detect and ignore this case, where we are trying to output
21985 something we have already output. */
21986 if (get_AT (old_die, DW_AT_low_pc)
21987 || get_AT (old_die, DW_AT_ranges))
21988 return;
21989
21990 /* If we have no location information, this must be a
21991 partially generated DIE from early dwarf generation.
21992 Fall through and generate it. */
21993 }
21994
21995 /* If the definition comes from the same place as the declaration,
21996 maybe use the old DIE. We always want the DIE for this function
21997 that has the *_pc attributes to be under comp_unit_die so the
21998 debugger can find it. We also need to do this for abstract
21999 instances of inlines, since the spec requires the out-of-line copy
22000 to have the same parent. For local class methods, this doesn't
22001 apply; we just use the old DIE. */
22002 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22003 struct dwarf_file_data * file_index = lookup_filename (s.file);
22004 if ((is_cu_die (old_die->die_parent)
22005 /* This condition fixes the inconsistency/ICE with the
22006 following Fortran test (or some derivative thereof) while
22007 building libgfortran:
22008
22009 module some_m
22010 contains
22011 logical function funky (FLAG)
22012 funky = .true.
22013 end function
22014 end module
22015 */
22016 || (old_die->die_parent
22017 && old_die->die_parent->die_tag == DW_TAG_module)
22018 || context_die == NULL)
22019 && (DECL_ARTIFICIAL (decl)
22020 /* The location attributes may be in the abstract origin
22021 which in the case of LTO might be not available to
22022 look at. */
22023 || get_AT (old_die, DW_AT_abstract_origin)
22024 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22025 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22026 == (unsigned) s.line)
22027 && (!debug_column_info
22028 || s.column == 0
22029 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22030 == (unsigned) s.column)))))
22031 {
22032 subr_die = old_die;
22033
22034 /* Clear out the declaration attribute, but leave the
22035 parameters so they can be augmented with location
22036 information later. Unless this was a declaration, in
22037 which case, wipe out the nameless parameters and recreate
22038 them further down. */
22039 if (remove_AT (subr_die, DW_AT_declaration))
22040 {
22041
22042 remove_AT (subr_die, DW_AT_object_pointer);
22043 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22044 }
22045 }
22046 /* Make a specification pointing to the previously built
22047 declaration. */
22048 else
22049 {
22050 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22051 add_AT_specification (subr_die, old_die);
22052 add_pubname (decl, subr_die);
22053 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22054 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22055 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22056 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22057 if (debug_column_info
22058 && s.column
22059 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22060 != (unsigned) s.column))
22061 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22062
22063 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22064 emit the real type on the definition die. */
22065 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22066 {
22067 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22068 if (die == auto_die || die == decltype_auto_die)
22069 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22070 TYPE_UNQUALIFIED, false, context_die);
22071 }
22072
22073 /* When we process the method declaration, we haven't seen
22074 the out-of-class defaulted definition yet, so we have to
22075 recheck now. */
22076 if ((dwarf_version >= 5 || ! dwarf_strict)
22077 && !get_AT (subr_die, DW_AT_defaulted))
22078 {
22079 int defaulted
22080 = lang_hooks.decls.decl_dwarf_attribute (decl,
22081 DW_AT_defaulted);
22082 if (defaulted != -1)
22083 {
22084 /* Other values must have been handled before. */
22085 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22086 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22087 }
22088 }
22089 }
22090 }
22091 /* Create a fresh DIE for anything else. */
22092 else
22093 {
22094 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22095
22096 if (TREE_PUBLIC (decl))
22097 add_AT_flag (subr_die, DW_AT_external, 1);
22098
22099 add_name_and_src_coords_attributes (subr_die, decl);
22100 add_pubname (decl, subr_die);
22101 if (debug_info_level > DINFO_LEVEL_TERSE)
22102 {
22103 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22104 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22105 TYPE_UNQUALIFIED, false, context_die);
22106 }
22107
22108 add_pure_or_virtual_attribute (subr_die, decl);
22109 if (DECL_ARTIFICIAL (decl))
22110 add_AT_flag (subr_die, DW_AT_artificial, 1);
22111
22112 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22113 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22114
22115 add_alignment_attribute (subr_die, decl);
22116
22117 add_accessibility_attribute (subr_die, decl);
22118 }
22119
22120 /* Unless we have an existing non-declaration DIE, equate the new
22121 DIE. */
22122 if (!old_die || is_declaration_die (old_die))
22123 equate_decl_number_to_die (decl, subr_die);
22124
22125 if (declaration)
22126 {
22127 if (!old_die || !get_AT (old_die, DW_AT_inline))
22128 {
22129 add_AT_flag (subr_die, DW_AT_declaration, 1);
22130
22131 /* If this is an explicit function declaration then generate
22132 a DW_AT_explicit attribute. */
22133 if ((dwarf_version >= 3 || !dwarf_strict)
22134 && lang_hooks.decls.decl_dwarf_attribute (decl,
22135 DW_AT_explicit) == 1)
22136 add_AT_flag (subr_die, DW_AT_explicit, 1);
22137
22138 /* If this is a C++11 deleted special function member then generate
22139 a DW_AT_deleted attribute. */
22140 if ((dwarf_version >= 5 || !dwarf_strict)
22141 && lang_hooks.decls.decl_dwarf_attribute (decl,
22142 DW_AT_deleted) == 1)
22143 add_AT_flag (subr_die, DW_AT_deleted, 1);
22144
22145 /* If this is a C++11 defaulted special function member then
22146 generate a DW_AT_defaulted attribute. */
22147 if (dwarf_version >= 5 || !dwarf_strict)
22148 {
22149 int defaulted
22150 = lang_hooks.decls.decl_dwarf_attribute (decl,
22151 DW_AT_defaulted);
22152 if (defaulted != -1)
22153 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22154 }
22155
22156 /* If this is a C++11 non-static member function with & ref-qualifier
22157 then generate a DW_AT_reference attribute. */
22158 if ((dwarf_version >= 5 || !dwarf_strict)
22159 && lang_hooks.decls.decl_dwarf_attribute (decl,
22160 DW_AT_reference) == 1)
22161 add_AT_flag (subr_die, DW_AT_reference, 1);
22162
22163 /* If this is a C++11 non-static member function with &&
22164 ref-qualifier then generate a DW_AT_reference attribute. */
22165 if ((dwarf_version >= 5 || !dwarf_strict)
22166 && lang_hooks.decls.decl_dwarf_attribute (decl,
22167 DW_AT_rvalue_reference)
22168 == 1)
22169 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22170 }
22171 }
22172 /* For non DECL_EXTERNALs, if range information is available, fill
22173 the DIE with it. */
22174 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22175 {
22176 HOST_WIDE_INT cfa_fb_offset;
22177
22178 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22179
22180 if (!crtl->has_bb_partition)
22181 {
22182 dw_fde_ref fde = fun->fde;
22183 if (fde->dw_fde_begin)
22184 {
22185 /* We have already generated the labels. */
22186 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22187 fde->dw_fde_end, false);
22188 }
22189 else
22190 {
22191 /* Create start/end labels and add the range. */
22192 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22193 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22194 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22195 current_function_funcdef_no);
22196 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22197 current_function_funcdef_no);
22198 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22199 false);
22200 }
22201
22202 #if VMS_DEBUGGING_INFO
22203 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22204 Section 2.3 Prologue and Epilogue Attributes:
22205 When a breakpoint is set on entry to a function, it is generally
22206 desirable for execution to be suspended, not on the very first
22207 instruction of the function, but rather at a point after the
22208 function's frame has been set up, after any language defined local
22209 declaration processing has been completed, and before execution of
22210 the first statement of the function begins. Debuggers generally
22211 cannot properly determine where this point is. Similarly for a
22212 breakpoint set on exit from a function. The prologue and epilogue
22213 attributes allow a compiler to communicate the location(s) to use. */
22214
22215 {
22216 if (fde->dw_fde_vms_end_prologue)
22217 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22218 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22219
22220 if (fde->dw_fde_vms_begin_epilogue)
22221 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22222 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22223 }
22224 #endif
22225
22226 }
22227 else
22228 {
22229 /* Generate pubnames entries for the split function code ranges. */
22230 dw_fde_ref fde = fun->fde;
22231
22232 if (fde->dw_fde_second_begin)
22233 {
22234 if (dwarf_version >= 3 || !dwarf_strict)
22235 {
22236 /* We should use ranges for non-contiguous code section
22237 addresses. Use the actual code range for the initial
22238 section, since the HOT/COLD labels might precede an
22239 alignment offset. */
22240 bool range_list_added = false;
22241 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22242 fde->dw_fde_end, &range_list_added,
22243 false);
22244 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22245 fde->dw_fde_second_end,
22246 &range_list_added, false);
22247 if (range_list_added)
22248 add_ranges (NULL);
22249 }
22250 else
22251 {
22252 /* There is no real support in DW2 for this .. so we make
22253 a work-around. First, emit the pub name for the segment
22254 containing the function label. Then make and emit a
22255 simplified subprogram DIE for the second segment with the
22256 name pre-fixed by __hot/cold_sect_of_. We use the same
22257 linkage name for the second die so that gdb will find both
22258 sections when given "b foo". */
22259 const char *name = NULL;
22260 tree decl_name = DECL_NAME (decl);
22261 dw_die_ref seg_die;
22262
22263 /* Do the 'primary' section. */
22264 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22265 fde->dw_fde_end, false);
22266
22267 /* Build a minimal DIE for the secondary section. */
22268 seg_die = new_die (DW_TAG_subprogram,
22269 subr_die->die_parent, decl);
22270
22271 if (TREE_PUBLIC (decl))
22272 add_AT_flag (seg_die, DW_AT_external, 1);
22273
22274 if (decl_name != NULL
22275 && IDENTIFIER_POINTER (decl_name) != NULL)
22276 {
22277 name = dwarf2_name (decl, 1);
22278 if (! DECL_ARTIFICIAL (decl))
22279 add_src_coords_attributes (seg_die, decl);
22280
22281 add_linkage_name (seg_die, decl);
22282 }
22283 gcc_assert (name != NULL);
22284 add_pure_or_virtual_attribute (seg_die, decl);
22285 if (DECL_ARTIFICIAL (decl))
22286 add_AT_flag (seg_die, DW_AT_artificial, 1);
22287
22288 name = concat ("__second_sect_of_", name, NULL);
22289 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22290 fde->dw_fde_second_end, false);
22291 add_name_attribute (seg_die, name);
22292 if (want_pubnames ())
22293 add_pubname_string (name, seg_die);
22294 }
22295 }
22296 else
22297 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22298 false);
22299 }
22300
22301 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22302
22303 /* We define the "frame base" as the function's CFA. This is more
22304 convenient for several reasons: (1) It's stable across the prologue
22305 and epilogue, which makes it better than just a frame pointer,
22306 (2) With dwarf3, there exists a one-byte encoding that allows us
22307 to reference the .debug_frame data by proxy, but failing that,
22308 (3) We can at least reuse the code inspection and interpretation
22309 code that determines the CFA position at various points in the
22310 function. */
22311 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22312 {
22313 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22314 add_AT_loc (subr_die, DW_AT_frame_base, op);
22315 }
22316 else
22317 {
22318 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22319 if (list->dw_loc_next)
22320 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22321 else
22322 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22323 }
22324
22325 /* Compute a displacement from the "steady-state frame pointer" to
22326 the CFA. The former is what all stack slots and argument slots
22327 will reference in the rtl; the latter is what we've told the
22328 debugger about. We'll need to adjust all frame_base references
22329 by this displacement. */
22330 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22331
22332 if (fun->static_chain_decl)
22333 {
22334 /* DWARF requires here a location expression that computes the
22335 address of the enclosing subprogram's frame base. The machinery
22336 in tree-nested.c is supposed to store this specific address in the
22337 last field of the FRAME record. */
22338 const tree frame_type
22339 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22340 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22341
22342 tree fb_expr
22343 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22344 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22345 fb_expr, fb_decl, NULL_TREE);
22346
22347 add_AT_location_description (subr_die, DW_AT_static_link,
22348 loc_list_from_tree (fb_expr, 0, NULL));
22349 }
22350
22351 resolve_variable_values ();
22352 }
22353
22354 /* Generate child dies for template paramaters. */
22355 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22356 gen_generic_params_dies (decl);
22357
22358 /* Now output descriptions of the arguments for this function. This gets
22359 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22360 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22361 `...' at the end of the formal parameter list. In order to find out if
22362 there was a trailing ellipsis or not, we must instead look at the type
22363 associated with the FUNCTION_DECL. This will be a node of type
22364 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22365 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22366 an ellipsis at the end. */
22367
22368 /* In the case where we are describing a mere function declaration, all we
22369 need to do here (and all we *can* do here) is to describe the *types* of
22370 its formal parameters. */
22371 if (debug_info_level <= DINFO_LEVEL_TERSE)
22372 ;
22373 else if (declaration)
22374 gen_formal_types_die (decl, subr_die);
22375 else
22376 {
22377 /* Generate DIEs to represent all known formal parameters. */
22378 tree parm = DECL_ARGUMENTS (decl);
22379 tree generic_decl = early_dwarf
22380 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22381 tree generic_decl_parm = generic_decl
22382 ? DECL_ARGUMENTS (generic_decl)
22383 : NULL;
22384
22385 /* Now we want to walk the list of parameters of the function and
22386 emit their relevant DIEs.
22387
22388 We consider the case of DECL being an instance of a generic function
22389 as well as it being a normal function.
22390
22391 If DECL is an instance of a generic function we walk the
22392 parameters of the generic function declaration _and_ the parameters of
22393 DECL itself. This is useful because we want to emit specific DIEs for
22394 function parameter packs and those are declared as part of the
22395 generic function declaration. In that particular case,
22396 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22397 That DIE has children DIEs representing the set of arguments
22398 of the pack. Note that the set of pack arguments can be empty.
22399 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22400 children DIE.
22401
22402 Otherwise, we just consider the parameters of DECL. */
22403 while (generic_decl_parm || parm)
22404 {
22405 if (generic_decl_parm
22406 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22407 gen_formal_parameter_pack_die (generic_decl_parm,
22408 parm, subr_die,
22409 &parm);
22410 else if (parm && !POINTER_BOUNDS_P (parm))
22411 {
22412 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22413
22414 if (early_dwarf
22415 && parm == DECL_ARGUMENTS (decl)
22416 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22417 && parm_die
22418 && (dwarf_version >= 3 || !dwarf_strict))
22419 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22420
22421 parm = DECL_CHAIN (parm);
22422 }
22423 else if (parm)
22424 parm = DECL_CHAIN (parm);
22425
22426 if (generic_decl_parm)
22427 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22428 }
22429
22430 /* Decide whether we need an unspecified_parameters DIE at the end.
22431 There are 2 more cases to do this for: 1) the ansi ... declaration -
22432 this is detectable when the end of the arg list is not a
22433 void_type_node 2) an unprototyped function declaration (not a
22434 definition). This just means that we have no info about the
22435 parameters at all. */
22436 if (early_dwarf)
22437 {
22438 if (prototype_p (TREE_TYPE (decl)))
22439 {
22440 /* This is the prototyped case, check for.... */
22441 if (stdarg_p (TREE_TYPE (decl)))
22442 gen_unspecified_parameters_die (decl, subr_die);
22443 }
22444 else if (DECL_INITIAL (decl) == NULL_TREE)
22445 gen_unspecified_parameters_die (decl, subr_die);
22446 }
22447 }
22448
22449 if (subr_die != old_die)
22450 /* Add the calling convention attribute if requested. */
22451 add_calling_convention_attribute (subr_die, decl);
22452
22453 /* Output Dwarf info for all of the stuff within the body of the function
22454 (if it has one - it may be just a declaration).
22455
22456 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22457 a function. This BLOCK actually represents the outermost binding contour
22458 for the function, i.e. the contour in which the function's formal
22459 parameters and labels get declared. Curiously, it appears that the front
22460 end doesn't actually put the PARM_DECL nodes for the current function onto
22461 the BLOCK_VARS list for this outer scope, but are strung off of the
22462 DECL_ARGUMENTS list for the function instead.
22463
22464 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22465 the LABEL_DECL nodes for the function however, and we output DWARF info
22466 for those in decls_for_scope. Just within the `outer_scope' there will be
22467 a BLOCK node representing the function's outermost pair of curly braces,
22468 and any blocks used for the base and member initializers of a C++
22469 constructor function. */
22470 tree outer_scope = DECL_INITIAL (decl);
22471 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22472 {
22473 int call_site_note_count = 0;
22474 int tail_call_site_note_count = 0;
22475
22476 /* Emit a DW_TAG_variable DIE for a named return value. */
22477 if (DECL_NAME (DECL_RESULT (decl)))
22478 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22479
22480 /* The first time through decls_for_scope we will generate the
22481 DIEs for the locals. The second time, we fill in the
22482 location info. */
22483 decls_for_scope (outer_scope, subr_die);
22484
22485 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22486 {
22487 struct call_arg_loc_node *ca_loc;
22488 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22489 {
22490 dw_die_ref die = NULL;
22491 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22492 rtx arg, next_arg;
22493
22494 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22495 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22496 : NULL_RTX);
22497 arg; arg = next_arg)
22498 {
22499 dw_loc_descr_ref reg, val;
22500 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22501 dw_die_ref cdie, tdie = NULL;
22502
22503 next_arg = XEXP (arg, 1);
22504 if (REG_P (XEXP (XEXP (arg, 0), 0))
22505 && next_arg
22506 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22507 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22508 && REGNO (XEXP (XEXP (arg, 0), 0))
22509 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22510 next_arg = XEXP (next_arg, 1);
22511 if (mode == VOIDmode)
22512 {
22513 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22514 if (mode == VOIDmode)
22515 mode = GET_MODE (XEXP (arg, 0));
22516 }
22517 if (mode == VOIDmode || mode == BLKmode)
22518 continue;
22519 /* Get dynamic information about call target only if we
22520 have no static information: we cannot generate both
22521 DW_AT_call_origin and DW_AT_call_target
22522 attributes. */
22523 if (ca_loc->symbol_ref == NULL_RTX)
22524 {
22525 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22526 {
22527 tloc = XEXP (XEXP (arg, 0), 1);
22528 continue;
22529 }
22530 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22531 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22532 {
22533 tlocc = XEXP (XEXP (arg, 0), 1);
22534 continue;
22535 }
22536 }
22537 reg = NULL;
22538 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22539 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22540 VAR_INIT_STATUS_INITIALIZED);
22541 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22542 {
22543 rtx mem = XEXP (XEXP (arg, 0), 0);
22544 reg = mem_loc_descriptor (XEXP (mem, 0),
22545 get_address_mode (mem),
22546 GET_MODE (mem),
22547 VAR_INIT_STATUS_INITIALIZED);
22548 }
22549 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22550 == DEBUG_PARAMETER_REF)
22551 {
22552 tree tdecl
22553 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22554 tdie = lookup_decl_die (tdecl);
22555 if (tdie == NULL)
22556 continue;
22557 }
22558 else
22559 continue;
22560 if (reg == NULL
22561 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22562 != DEBUG_PARAMETER_REF)
22563 continue;
22564 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22565 VOIDmode,
22566 VAR_INIT_STATUS_INITIALIZED);
22567 if (val == NULL)
22568 continue;
22569 if (die == NULL)
22570 die = gen_call_site_die (decl, subr_die, ca_loc);
22571 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22572 NULL_TREE);
22573 if (reg != NULL)
22574 add_AT_loc (cdie, DW_AT_location, reg);
22575 else if (tdie != NULL)
22576 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22577 tdie);
22578 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22579 if (next_arg != XEXP (arg, 1))
22580 {
22581 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22582 if (mode == VOIDmode)
22583 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22584 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22585 0), 1),
22586 mode, VOIDmode,
22587 VAR_INIT_STATUS_INITIALIZED);
22588 if (val != NULL)
22589 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22590 val);
22591 }
22592 }
22593 if (die == NULL
22594 && (ca_loc->symbol_ref || tloc))
22595 die = gen_call_site_die (decl, subr_die, ca_loc);
22596 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22597 {
22598 dw_loc_descr_ref tval = NULL;
22599
22600 if (tloc != NULL_RTX)
22601 tval = mem_loc_descriptor (tloc,
22602 GET_MODE (tloc) == VOIDmode
22603 ? Pmode : GET_MODE (tloc),
22604 VOIDmode,
22605 VAR_INIT_STATUS_INITIALIZED);
22606 if (tval)
22607 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22608 else if (tlocc != NULL_RTX)
22609 {
22610 tval = mem_loc_descriptor (tlocc,
22611 GET_MODE (tlocc) == VOIDmode
22612 ? Pmode : GET_MODE (tlocc),
22613 VOIDmode,
22614 VAR_INIT_STATUS_INITIALIZED);
22615 if (tval)
22616 add_AT_loc (die,
22617 dwarf_AT (DW_AT_call_target_clobbered),
22618 tval);
22619 }
22620 }
22621 if (die != NULL)
22622 {
22623 call_site_note_count++;
22624 if (ca_loc->tail_call_p)
22625 tail_call_site_note_count++;
22626 }
22627 }
22628 }
22629 call_arg_locations = NULL;
22630 call_arg_loc_last = NULL;
22631 if (tail_call_site_count >= 0
22632 && tail_call_site_count == tail_call_site_note_count
22633 && (!dwarf_strict || dwarf_version >= 5))
22634 {
22635 if (call_site_count >= 0
22636 && call_site_count == call_site_note_count)
22637 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22638 else
22639 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22640 }
22641 call_site_count = -1;
22642 tail_call_site_count = -1;
22643 }
22644
22645 /* Mark used types after we have created DIEs for the functions scopes. */
22646 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22647 }
22648
22649 /* Returns a hash value for X (which really is a die_struct). */
22650
22651 hashval_t
22652 block_die_hasher::hash (die_struct *d)
22653 {
22654 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22655 }
22656
22657 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22658 as decl_id and die_parent of die_struct Y. */
22659
22660 bool
22661 block_die_hasher::equal (die_struct *x, die_struct *y)
22662 {
22663 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22664 }
22665
22666 /* Return TRUE if DECL, which may have been previously generated as
22667 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22668 true if decl (or its origin) is either an extern declaration or a
22669 class/namespace scoped declaration.
22670
22671 The declare_in_namespace support causes us to get two DIEs for one
22672 variable, both of which are declarations. We want to avoid
22673 considering one to be a specification, so we must test for
22674 DECLARATION and DW_AT_declaration. */
22675 static inline bool
22676 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22677 {
22678 return (old_die && TREE_STATIC (decl) && !declaration
22679 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22680 }
22681
22682 /* Return true if DECL is a local static. */
22683
22684 static inline bool
22685 local_function_static (tree decl)
22686 {
22687 gcc_assert (VAR_P (decl));
22688 return TREE_STATIC (decl)
22689 && DECL_CONTEXT (decl)
22690 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22691 }
22692
22693 /* Generate a DIE to represent a declared data object.
22694 Either DECL or ORIGIN must be non-null. */
22695
22696 static void
22697 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22698 {
22699 HOST_WIDE_INT off = 0;
22700 tree com_decl;
22701 tree decl_or_origin = decl ? decl : origin;
22702 tree ultimate_origin;
22703 dw_die_ref var_die;
22704 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22705 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22706 || class_or_namespace_scope_p (context_die));
22707 bool specialization_p = false;
22708 bool no_linkage_name = false;
22709
22710 /* While C++ inline static data members have definitions inside of the
22711 class, force the first DIE to be a declaration, then let gen_member_die
22712 reparent it to the class context and call gen_variable_die again
22713 to create the outside of the class DIE for the definition. */
22714 if (!declaration
22715 && old_die == NULL
22716 && decl
22717 && DECL_CONTEXT (decl)
22718 && TYPE_P (DECL_CONTEXT (decl))
22719 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22720 {
22721 declaration = true;
22722 if (dwarf_version < 5)
22723 no_linkage_name = true;
22724 }
22725
22726 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22727 if (decl || ultimate_origin)
22728 origin = ultimate_origin;
22729 com_decl = fortran_common (decl_or_origin, &off);
22730
22731 /* Symbol in common gets emitted as a child of the common block, in the form
22732 of a data member. */
22733 if (com_decl)
22734 {
22735 dw_die_ref com_die;
22736 dw_loc_list_ref loc = NULL;
22737 die_node com_die_arg;
22738
22739 var_die = lookup_decl_die (decl_or_origin);
22740 if (var_die)
22741 {
22742 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22743 {
22744 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22745 if (loc)
22746 {
22747 if (off)
22748 {
22749 /* Optimize the common case. */
22750 if (single_element_loc_list_p (loc)
22751 && loc->expr->dw_loc_opc == DW_OP_addr
22752 && loc->expr->dw_loc_next == NULL
22753 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22754 == SYMBOL_REF)
22755 {
22756 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22757 loc->expr->dw_loc_oprnd1.v.val_addr
22758 = plus_constant (GET_MODE (x), x , off);
22759 }
22760 else
22761 loc_list_plus_const (loc, off);
22762 }
22763 add_AT_location_description (var_die, DW_AT_location, loc);
22764 remove_AT (var_die, DW_AT_declaration);
22765 }
22766 }
22767 return;
22768 }
22769
22770 if (common_block_die_table == NULL)
22771 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22772
22773 com_die_arg.decl_id = DECL_UID (com_decl);
22774 com_die_arg.die_parent = context_die;
22775 com_die = common_block_die_table->find (&com_die_arg);
22776 if (! early_dwarf)
22777 loc = loc_list_from_tree (com_decl, 2, NULL);
22778 if (com_die == NULL)
22779 {
22780 const char *cnam
22781 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22782 die_node **slot;
22783
22784 com_die = new_die (DW_TAG_common_block, context_die, decl);
22785 add_name_and_src_coords_attributes (com_die, com_decl);
22786 if (loc)
22787 {
22788 add_AT_location_description (com_die, DW_AT_location, loc);
22789 /* Avoid sharing the same loc descriptor between
22790 DW_TAG_common_block and DW_TAG_variable. */
22791 loc = loc_list_from_tree (com_decl, 2, NULL);
22792 }
22793 else if (DECL_EXTERNAL (decl_or_origin))
22794 add_AT_flag (com_die, DW_AT_declaration, 1);
22795 if (want_pubnames ())
22796 add_pubname_string (cnam, com_die); /* ??? needed? */
22797 com_die->decl_id = DECL_UID (com_decl);
22798 slot = common_block_die_table->find_slot (com_die, INSERT);
22799 *slot = com_die;
22800 }
22801 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22802 {
22803 add_AT_location_description (com_die, DW_AT_location, loc);
22804 loc = loc_list_from_tree (com_decl, 2, NULL);
22805 remove_AT (com_die, DW_AT_declaration);
22806 }
22807 var_die = new_die (DW_TAG_variable, com_die, decl);
22808 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22809 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22810 decl_quals (decl_or_origin), false,
22811 context_die);
22812 add_alignment_attribute (var_die, decl);
22813 add_AT_flag (var_die, DW_AT_external, 1);
22814 if (loc)
22815 {
22816 if (off)
22817 {
22818 /* Optimize the common case. */
22819 if (single_element_loc_list_p (loc)
22820 && loc->expr->dw_loc_opc == DW_OP_addr
22821 && loc->expr->dw_loc_next == NULL
22822 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22823 {
22824 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22825 loc->expr->dw_loc_oprnd1.v.val_addr
22826 = plus_constant (GET_MODE (x), x, off);
22827 }
22828 else
22829 loc_list_plus_const (loc, off);
22830 }
22831 add_AT_location_description (var_die, DW_AT_location, loc);
22832 }
22833 else if (DECL_EXTERNAL (decl_or_origin))
22834 add_AT_flag (var_die, DW_AT_declaration, 1);
22835 if (decl)
22836 equate_decl_number_to_die (decl, var_die);
22837 return;
22838 }
22839
22840 if (old_die)
22841 {
22842 if (declaration)
22843 {
22844 /* A declaration that has been previously dumped, needs no
22845 further annotations, since it doesn't need location on
22846 the second pass. */
22847 return;
22848 }
22849 else if (decl_will_get_specification_p (old_die, decl, declaration)
22850 && !get_AT (old_die, DW_AT_specification))
22851 {
22852 /* Fall-thru so we can make a new variable die along with a
22853 DW_AT_specification. */
22854 }
22855 else if (origin && old_die->die_parent != context_die)
22856 {
22857 /* If we will be creating an inlined instance, we need a
22858 new DIE that will get annotated with
22859 DW_AT_abstract_origin. Clear things so we can get a
22860 new DIE. */
22861 gcc_assert (!DECL_ABSTRACT_P (decl));
22862 old_die = NULL;
22863 }
22864 else
22865 {
22866 /* If a DIE was dumped early, it still needs location info.
22867 Skip to where we fill the location bits. */
22868 var_die = old_die;
22869
22870 /* ??? In LTRANS we cannot annotate early created variably
22871 modified type DIEs without copying them and adjusting all
22872 references to them. Thus we dumped them again, also add a
22873 reference to them. */
22874 tree type = TREE_TYPE (decl_or_origin);
22875 if (in_lto_p
22876 && variably_modified_type_p
22877 (type, decl_function_context (decl_or_origin)))
22878 {
22879 if (decl_by_reference_p (decl_or_origin))
22880 add_type_attribute (var_die, TREE_TYPE (type),
22881 TYPE_UNQUALIFIED, false, context_die);
22882 else
22883 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22884 false, context_die);
22885 }
22886
22887 goto gen_variable_die_location;
22888 }
22889 }
22890
22891 /* For static data members, the declaration in the class is supposed
22892 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22893 also in DWARF2; the specification should still be DW_TAG_variable
22894 referencing the DW_TAG_member DIE. */
22895 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22896 var_die = new_die (DW_TAG_member, context_die, decl);
22897 else
22898 var_die = new_die (DW_TAG_variable, context_die, decl);
22899
22900 if (origin != NULL)
22901 add_abstract_origin_attribute (var_die, origin);
22902
22903 /* Loop unrolling can create multiple blocks that refer to the same
22904 static variable, so we must test for the DW_AT_declaration flag.
22905
22906 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22907 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22908 sharing them.
22909
22910 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22911 else if (decl_will_get_specification_p (old_die, decl, declaration))
22912 {
22913 /* This is a definition of a C++ class level static. */
22914 add_AT_specification (var_die, old_die);
22915 specialization_p = true;
22916 if (DECL_NAME (decl))
22917 {
22918 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22919 struct dwarf_file_data * file_index = lookup_filename (s.file);
22920
22921 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22922 add_AT_file (var_die, DW_AT_decl_file, file_index);
22923
22924 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22925 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22926
22927 if (debug_column_info
22928 && s.column
22929 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22930 != (unsigned) s.column))
22931 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22932
22933 if (old_die->die_tag == DW_TAG_member)
22934 add_linkage_name (var_die, decl);
22935 }
22936 }
22937 else
22938 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22939
22940 if ((origin == NULL && !specialization_p)
22941 || (origin != NULL
22942 && !DECL_ABSTRACT_P (decl_or_origin)
22943 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22944 decl_function_context
22945 (decl_or_origin))))
22946 {
22947 tree type = TREE_TYPE (decl_or_origin);
22948
22949 if (decl_by_reference_p (decl_or_origin))
22950 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22951 context_die);
22952 else
22953 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22954 context_die);
22955 }
22956
22957 if (origin == NULL && !specialization_p)
22958 {
22959 if (TREE_PUBLIC (decl))
22960 add_AT_flag (var_die, DW_AT_external, 1);
22961
22962 if (DECL_ARTIFICIAL (decl))
22963 add_AT_flag (var_die, DW_AT_artificial, 1);
22964
22965 add_alignment_attribute (var_die, decl);
22966
22967 add_accessibility_attribute (var_die, decl);
22968 }
22969
22970 if (declaration)
22971 add_AT_flag (var_die, DW_AT_declaration, 1);
22972
22973 if (decl && (DECL_ABSTRACT_P (decl)
22974 || !old_die || is_declaration_die (old_die)))
22975 equate_decl_number_to_die (decl, var_die);
22976
22977 gen_variable_die_location:
22978 if (! declaration
22979 && (! DECL_ABSTRACT_P (decl_or_origin)
22980 /* Local static vars are shared between all clones/inlines,
22981 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22982 already set. */
22983 || (VAR_P (decl_or_origin)
22984 && TREE_STATIC (decl_or_origin)
22985 && DECL_RTL_SET_P (decl_or_origin))))
22986 {
22987 if (early_dwarf)
22988 add_pubname (decl_or_origin, var_die);
22989 else
22990 add_location_or_const_value_attribute (var_die, decl_or_origin,
22991 decl == NULL);
22992 }
22993 else
22994 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22995
22996 if ((dwarf_version >= 4 || !dwarf_strict)
22997 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22998 DW_AT_const_expr) == 1
22999 && !get_AT (var_die, DW_AT_const_expr)
23000 && !specialization_p)
23001 add_AT_flag (var_die, DW_AT_const_expr, 1);
23002
23003 if (!dwarf_strict)
23004 {
23005 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23006 DW_AT_inline);
23007 if (inl != -1
23008 && !get_AT (var_die, DW_AT_inline)
23009 && !specialization_p)
23010 add_AT_unsigned (var_die, DW_AT_inline, inl);
23011 }
23012 }
23013
23014 /* Generate a DIE to represent a named constant. */
23015
23016 static void
23017 gen_const_die (tree decl, dw_die_ref context_die)
23018 {
23019 dw_die_ref const_die;
23020 tree type = TREE_TYPE (decl);
23021
23022 const_die = lookup_decl_die (decl);
23023 if (const_die)
23024 return;
23025
23026 const_die = new_die (DW_TAG_constant, context_die, decl);
23027 equate_decl_number_to_die (decl, const_die);
23028 add_name_and_src_coords_attributes (const_die, decl);
23029 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23030 if (TREE_PUBLIC (decl))
23031 add_AT_flag (const_die, DW_AT_external, 1);
23032 if (DECL_ARTIFICIAL (decl))
23033 add_AT_flag (const_die, DW_AT_artificial, 1);
23034 tree_add_const_value_attribute_for_decl (const_die, decl);
23035 }
23036
23037 /* Generate a DIE to represent a label identifier. */
23038
23039 static void
23040 gen_label_die (tree decl, dw_die_ref context_die)
23041 {
23042 tree origin = decl_ultimate_origin (decl);
23043 dw_die_ref lbl_die = lookup_decl_die (decl);
23044 rtx insn;
23045 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23046
23047 if (!lbl_die)
23048 {
23049 lbl_die = new_die (DW_TAG_label, context_die, decl);
23050 equate_decl_number_to_die (decl, lbl_die);
23051
23052 if (origin != NULL)
23053 add_abstract_origin_attribute (lbl_die, origin);
23054 else
23055 add_name_and_src_coords_attributes (lbl_die, decl);
23056 }
23057
23058 if (DECL_ABSTRACT_P (decl))
23059 equate_decl_number_to_die (decl, lbl_die);
23060 else if (! early_dwarf)
23061 {
23062 insn = DECL_RTL_IF_SET (decl);
23063
23064 /* Deleted labels are programmer specified labels which have been
23065 eliminated because of various optimizations. We still emit them
23066 here so that it is possible to put breakpoints on them. */
23067 if (insn
23068 && (LABEL_P (insn)
23069 || ((NOTE_P (insn)
23070 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23071 {
23072 /* When optimization is enabled (via -O) some parts of the compiler
23073 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23074 represent source-level labels which were explicitly declared by
23075 the user. This really shouldn't be happening though, so catch
23076 it if it ever does happen. */
23077 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23078
23079 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23080 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23081 }
23082 else if (insn
23083 && NOTE_P (insn)
23084 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23085 && CODE_LABEL_NUMBER (insn) != -1)
23086 {
23087 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23088 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23089 }
23090 }
23091 }
23092
23093 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23094 attributes to the DIE for a block STMT, to describe where the inlined
23095 function was called from. This is similar to add_src_coords_attributes. */
23096
23097 static inline void
23098 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23099 {
23100 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23101
23102 if (dwarf_version >= 3 || !dwarf_strict)
23103 {
23104 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23105 add_AT_unsigned (die, DW_AT_call_line, s.line);
23106 if (debug_column_info && s.column)
23107 add_AT_unsigned (die, DW_AT_call_column, s.column);
23108 }
23109 }
23110
23111
23112 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23113 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23114
23115 static inline void
23116 add_high_low_attributes (tree stmt, dw_die_ref die)
23117 {
23118 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23119
23120 if (BLOCK_FRAGMENT_CHAIN (stmt)
23121 && (dwarf_version >= 3 || !dwarf_strict))
23122 {
23123 tree chain, superblock = NULL_TREE;
23124 dw_die_ref pdie;
23125 dw_attr_node *attr = NULL;
23126
23127 if (inlined_function_outer_scope_p (stmt))
23128 {
23129 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23130 BLOCK_NUMBER (stmt));
23131 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23132 }
23133
23134 /* Optimize duplicate .debug_ranges lists or even tails of
23135 lists. If this BLOCK has same ranges as its supercontext,
23136 lookup DW_AT_ranges attribute in the supercontext (and
23137 recursively so), verify that the ranges_table contains the
23138 right values and use it instead of adding a new .debug_range. */
23139 for (chain = stmt, pdie = die;
23140 BLOCK_SAME_RANGE (chain);
23141 chain = BLOCK_SUPERCONTEXT (chain))
23142 {
23143 dw_attr_node *new_attr;
23144
23145 pdie = pdie->die_parent;
23146 if (pdie == NULL)
23147 break;
23148 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23149 break;
23150 new_attr = get_AT (pdie, DW_AT_ranges);
23151 if (new_attr == NULL
23152 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23153 break;
23154 attr = new_attr;
23155 superblock = BLOCK_SUPERCONTEXT (chain);
23156 }
23157 if (attr != NULL
23158 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23159 == BLOCK_NUMBER (superblock))
23160 && BLOCK_FRAGMENT_CHAIN (superblock))
23161 {
23162 unsigned long off = attr->dw_attr_val.v.val_offset;
23163 unsigned long supercnt = 0, thiscnt = 0;
23164 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23165 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23166 {
23167 ++supercnt;
23168 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23169 == BLOCK_NUMBER (chain));
23170 }
23171 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23172 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23173 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23174 ++thiscnt;
23175 gcc_assert (supercnt >= thiscnt);
23176 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23177 false);
23178 note_rnglist_head (off + supercnt - thiscnt);
23179 return;
23180 }
23181
23182 unsigned int offset = add_ranges (stmt, true);
23183 add_AT_range_list (die, DW_AT_ranges, offset, false);
23184 note_rnglist_head (offset);
23185
23186 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23187 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23188 do
23189 {
23190 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23191 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23192 chain = BLOCK_FRAGMENT_CHAIN (chain);
23193 }
23194 while (chain);
23195 add_ranges (NULL);
23196 }
23197 else
23198 {
23199 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23200 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23201 BLOCK_NUMBER (stmt));
23202 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23203 BLOCK_NUMBER (stmt));
23204 add_AT_low_high_pc (die, label, label_high, false);
23205 }
23206 }
23207
23208 /* Generate a DIE for a lexical block. */
23209
23210 static void
23211 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23212 {
23213 dw_die_ref old_die = BLOCK_DIE (stmt);
23214 dw_die_ref stmt_die = NULL;
23215 if (!old_die)
23216 {
23217 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23218 BLOCK_DIE (stmt) = stmt_die;
23219 }
23220
23221 if (BLOCK_ABSTRACT (stmt))
23222 {
23223 if (old_die)
23224 {
23225 /* This must have been generated early and it won't even
23226 need location information since it's a DW_AT_inline
23227 function. */
23228 if (flag_checking)
23229 for (dw_die_ref c = context_die; c; c = c->die_parent)
23230 if (c->die_tag == DW_TAG_inlined_subroutine
23231 || c->die_tag == DW_TAG_subprogram)
23232 {
23233 gcc_assert (get_AT (c, DW_AT_inline));
23234 break;
23235 }
23236 return;
23237 }
23238 }
23239 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23240 {
23241 /* If this is an inlined instance, create a new lexical die for
23242 anything below to attach DW_AT_abstract_origin to. */
23243 if (old_die)
23244 {
23245 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23246 BLOCK_DIE (stmt) = stmt_die;
23247 old_die = NULL;
23248 }
23249
23250 tree origin = block_ultimate_origin (stmt);
23251 if (origin != NULL_TREE && origin != stmt)
23252 add_abstract_origin_attribute (stmt_die, origin);
23253 }
23254
23255 if (old_die)
23256 stmt_die = old_die;
23257
23258 /* A non abstract block whose blocks have already been reordered
23259 should have the instruction range for this block. If so, set the
23260 high/low attributes. */
23261 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23262 {
23263 gcc_assert (stmt_die);
23264 add_high_low_attributes (stmt, stmt_die);
23265 }
23266
23267 decls_for_scope (stmt, stmt_die);
23268 }
23269
23270 /* Generate a DIE for an inlined subprogram. */
23271
23272 static void
23273 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23274 {
23275 tree decl;
23276
23277 /* The instance of function that is effectively being inlined shall not
23278 be abstract. */
23279 gcc_assert (! BLOCK_ABSTRACT (stmt));
23280
23281 decl = block_ultimate_origin (stmt);
23282
23283 /* Make sure any inlined functions are known to be inlineable. */
23284 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23285 || cgraph_function_possibly_inlined_p (decl));
23286
23287 if (! BLOCK_ABSTRACT (stmt))
23288 {
23289 dw_die_ref subr_die
23290 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23291
23292 if (call_arg_locations)
23293 BLOCK_DIE (stmt) = subr_die;
23294 add_abstract_origin_attribute (subr_die, decl);
23295 if (TREE_ASM_WRITTEN (stmt))
23296 add_high_low_attributes (stmt, subr_die);
23297 add_call_src_coords_attributes (stmt, subr_die);
23298
23299 decls_for_scope (stmt, subr_die);
23300 }
23301 }
23302
23303 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23304 the comment for VLR_CONTEXT. */
23305
23306 static void
23307 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23308 {
23309 dw_die_ref decl_die;
23310
23311 if (TREE_TYPE (decl) == error_mark_node)
23312 return;
23313
23314 decl_die = new_die (DW_TAG_member, context_die, decl);
23315 add_name_and_src_coords_attributes (decl_die, decl);
23316 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23317 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23318 context_die);
23319
23320 if (DECL_BIT_FIELD_TYPE (decl))
23321 {
23322 add_byte_size_attribute (decl_die, decl);
23323 add_bit_size_attribute (decl_die, decl);
23324 add_bit_offset_attribute (decl_die, decl, ctx);
23325 }
23326
23327 add_alignment_attribute (decl_die, decl);
23328
23329 /* If we have a variant part offset, then we are supposed to process a member
23330 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23331 trees. */
23332 gcc_assert (ctx->variant_part_offset == NULL_TREE
23333 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23334 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23335 add_data_member_location_attribute (decl_die, decl, ctx);
23336
23337 if (DECL_ARTIFICIAL (decl))
23338 add_AT_flag (decl_die, DW_AT_artificial, 1);
23339
23340 add_accessibility_attribute (decl_die, decl);
23341
23342 /* Equate decl number to die, so that we can look up this decl later on. */
23343 equate_decl_number_to_die (decl, decl_die);
23344 }
23345
23346 /* Generate a DIE for a pointer to a member type. TYPE can be an
23347 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23348 pointer to member function. */
23349
23350 static void
23351 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23352 {
23353 if (lookup_type_die (type))
23354 return;
23355
23356 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23357 scope_die_for (type, context_die), type);
23358
23359 equate_type_number_to_die (type, ptr_die);
23360 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23361 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23362 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23363 context_die);
23364 add_alignment_attribute (ptr_die, type);
23365
23366 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23367 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23368 {
23369 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23370 add_AT_loc (ptr_die, DW_AT_use_location, op);
23371 }
23372 }
23373
23374 static char *producer_string;
23375
23376 /* Return a heap allocated producer string including command line options
23377 if -grecord-gcc-switches. */
23378
23379 static char *
23380 gen_producer_string (void)
23381 {
23382 size_t j;
23383 auto_vec<const char *> switches;
23384 const char *language_string = lang_hooks.name;
23385 char *producer, *tail;
23386 const char *p;
23387 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23388 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23389
23390 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23391 switch (save_decoded_options[j].opt_index)
23392 {
23393 case OPT_o:
23394 case OPT_d:
23395 case OPT_dumpbase:
23396 case OPT_dumpdir:
23397 case OPT_auxbase:
23398 case OPT_auxbase_strip:
23399 case OPT_quiet:
23400 case OPT_version:
23401 case OPT_v:
23402 case OPT_w:
23403 case OPT_L:
23404 case OPT_D:
23405 case OPT_I:
23406 case OPT_U:
23407 case OPT_SPECIAL_unknown:
23408 case OPT_SPECIAL_ignore:
23409 case OPT_SPECIAL_program_name:
23410 case OPT_SPECIAL_input_file:
23411 case OPT_grecord_gcc_switches:
23412 case OPT__output_pch_:
23413 case OPT_fdiagnostics_show_location_:
23414 case OPT_fdiagnostics_show_option:
23415 case OPT_fdiagnostics_show_caret:
23416 case OPT_fdiagnostics_color_:
23417 case OPT_fverbose_asm:
23418 case OPT____:
23419 case OPT__sysroot_:
23420 case OPT_nostdinc:
23421 case OPT_nostdinc__:
23422 case OPT_fpreprocessed:
23423 case OPT_fltrans_output_list_:
23424 case OPT_fresolution_:
23425 case OPT_fdebug_prefix_map_:
23426 case OPT_fcompare_debug:
23427 /* Ignore these. */
23428 continue;
23429 default:
23430 if (cl_options[save_decoded_options[j].opt_index].flags
23431 & CL_NO_DWARF_RECORD)
23432 continue;
23433 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23434 == '-');
23435 switch (save_decoded_options[j].canonical_option[0][1])
23436 {
23437 case 'M':
23438 case 'i':
23439 case 'W':
23440 continue;
23441 case 'f':
23442 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23443 "dump", 4) == 0)
23444 continue;
23445 break;
23446 default:
23447 break;
23448 }
23449 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23450 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23451 break;
23452 }
23453
23454 producer = XNEWVEC (char, plen + 1 + len + 1);
23455 tail = producer;
23456 sprintf (tail, "%s %s", language_string, version_string);
23457 tail += plen;
23458
23459 FOR_EACH_VEC_ELT (switches, j, p)
23460 {
23461 len = strlen (p);
23462 *tail = ' ';
23463 memcpy (tail + 1, p, len);
23464 tail += len + 1;
23465 }
23466
23467 *tail = '\0';
23468 return producer;
23469 }
23470
23471 /* Given a C and/or C++ language/version string return the "highest".
23472 C++ is assumed to be "higher" than C in this case. Used for merging
23473 LTO translation unit languages. */
23474 static const char *
23475 highest_c_language (const char *lang1, const char *lang2)
23476 {
23477 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23478 return "GNU C++17";
23479 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23480 return "GNU C++14";
23481 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23482 return "GNU C++11";
23483 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23484 return "GNU C++98";
23485
23486 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23487 return "GNU C17";
23488 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23489 return "GNU C11";
23490 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23491 return "GNU C99";
23492 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23493 return "GNU C89";
23494
23495 gcc_unreachable ();
23496 }
23497
23498
23499 /* Generate the DIE for the compilation unit. */
23500
23501 static dw_die_ref
23502 gen_compile_unit_die (const char *filename)
23503 {
23504 dw_die_ref die;
23505 const char *language_string = lang_hooks.name;
23506 int language;
23507
23508 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23509
23510 if (filename)
23511 {
23512 add_name_attribute (die, filename);
23513 /* Don't add cwd for <built-in>. */
23514 if (filename[0] != '<')
23515 add_comp_dir_attribute (die);
23516 }
23517
23518 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23519
23520 /* If our producer is LTO try to figure out a common language to use
23521 from the global list of translation units. */
23522 if (strcmp (language_string, "GNU GIMPLE") == 0)
23523 {
23524 unsigned i;
23525 tree t;
23526 const char *common_lang = NULL;
23527
23528 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23529 {
23530 if (!TRANSLATION_UNIT_LANGUAGE (t))
23531 continue;
23532 if (!common_lang)
23533 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23534 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23535 ;
23536 else if (strncmp (common_lang, "GNU C", 5) == 0
23537 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23538 /* Mixing C and C++ is ok, use C++ in that case. */
23539 common_lang = highest_c_language (common_lang,
23540 TRANSLATION_UNIT_LANGUAGE (t));
23541 else
23542 {
23543 /* Fall back to C. */
23544 common_lang = NULL;
23545 break;
23546 }
23547 }
23548
23549 if (common_lang)
23550 language_string = common_lang;
23551 }
23552
23553 language = DW_LANG_C;
23554 if (strncmp (language_string, "GNU C", 5) == 0
23555 && ISDIGIT (language_string[5]))
23556 {
23557 language = DW_LANG_C89;
23558 if (dwarf_version >= 3 || !dwarf_strict)
23559 {
23560 if (strcmp (language_string, "GNU C89") != 0)
23561 language = DW_LANG_C99;
23562
23563 if (dwarf_version >= 5 /* || !dwarf_strict */)
23564 if (strcmp (language_string, "GNU C11") == 0
23565 || strcmp (language_string, "GNU C17") == 0)
23566 language = DW_LANG_C11;
23567 }
23568 }
23569 else if (strncmp (language_string, "GNU C++", 7) == 0)
23570 {
23571 language = DW_LANG_C_plus_plus;
23572 if (dwarf_version >= 5 /* || !dwarf_strict */)
23573 {
23574 if (strcmp (language_string, "GNU C++11") == 0)
23575 language = DW_LANG_C_plus_plus_11;
23576 else if (strcmp (language_string, "GNU C++14") == 0)
23577 language = DW_LANG_C_plus_plus_14;
23578 else if (strcmp (language_string, "GNU C++17") == 0)
23579 /* For now. */
23580 language = DW_LANG_C_plus_plus_14;
23581 }
23582 }
23583 else if (strcmp (language_string, "GNU F77") == 0)
23584 language = DW_LANG_Fortran77;
23585 else if (dwarf_version >= 3 || !dwarf_strict)
23586 {
23587 if (strcmp (language_string, "GNU Ada") == 0)
23588 language = DW_LANG_Ada95;
23589 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23590 {
23591 language = DW_LANG_Fortran95;
23592 if (dwarf_version >= 5 /* || !dwarf_strict */)
23593 {
23594 if (strcmp (language_string, "GNU Fortran2003") == 0)
23595 language = DW_LANG_Fortran03;
23596 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23597 language = DW_LANG_Fortran08;
23598 }
23599 }
23600 else if (strcmp (language_string, "GNU Objective-C") == 0)
23601 language = DW_LANG_ObjC;
23602 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23603 language = DW_LANG_ObjC_plus_plus;
23604 else if (dwarf_version >= 5 || !dwarf_strict)
23605 {
23606 if (strcmp (language_string, "GNU Go") == 0)
23607 language = DW_LANG_Go;
23608 }
23609 }
23610 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23611 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23612 language = DW_LANG_Fortran90;
23613
23614 add_AT_unsigned (die, DW_AT_language, language);
23615
23616 switch (language)
23617 {
23618 case DW_LANG_Fortran77:
23619 case DW_LANG_Fortran90:
23620 case DW_LANG_Fortran95:
23621 case DW_LANG_Fortran03:
23622 case DW_LANG_Fortran08:
23623 /* Fortran has case insensitive identifiers and the front-end
23624 lowercases everything. */
23625 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23626 break;
23627 default:
23628 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23629 break;
23630 }
23631 return die;
23632 }
23633
23634 /* Generate the DIE for a base class. */
23635
23636 static void
23637 gen_inheritance_die (tree binfo, tree access, tree type,
23638 dw_die_ref context_die)
23639 {
23640 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23641 struct vlr_context ctx = { type, NULL };
23642
23643 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23644 context_die);
23645 add_data_member_location_attribute (die, binfo, &ctx);
23646
23647 if (BINFO_VIRTUAL_P (binfo))
23648 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23649
23650 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23651 children, otherwise the default is DW_ACCESS_public. In DWARF2
23652 the default has always been DW_ACCESS_private. */
23653 if (access == access_public_node)
23654 {
23655 if (dwarf_version == 2
23656 || context_die->die_tag == DW_TAG_class_type)
23657 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23658 }
23659 else if (access == access_protected_node)
23660 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23661 else if (dwarf_version > 2
23662 && context_die->die_tag != DW_TAG_class_type)
23663 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23664 }
23665
23666 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23667 structure. */
23668 static bool
23669 is_variant_part (tree decl)
23670 {
23671 return (TREE_CODE (decl) == FIELD_DECL
23672 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23673 }
23674
23675 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23676 return the FIELD_DECL. Return NULL_TREE otherwise. */
23677
23678 static tree
23679 analyze_discr_in_predicate (tree operand, tree struct_type)
23680 {
23681 bool continue_stripping = true;
23682 while (continue_stripping)
23683 switch (TREE_CODE (operand))
23684 {
23685 CASE_CONVERT:
23686 operand = TREE_OPERAND (operand, 0);
23687 break;
23688 default:
23689 continue_stripping = false;
23690 break;
23691 }
23692
23693 /* Match field access to members of struct_type only. */
23694 if (TREE_CODE (operand) == COMPONENT_REF
23695 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23696 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23697 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23698 return TREE_OPERAND (operand, 1);
23699 else
23700 return NULL_TREE;
23701 }
23702
23703 /* Check that SRC is a constant integer that can be represented as a native
23704 integer constant (either signed or unsigned). If so, store it into DEST and
23705 return true. Return false otherwise. */
23706
23707 static bool
23708 get_discr_value (tree src, dw_discr_value *dest)
23709 {
23710 tree discr_type = TREE_TYPE (src);
23711
23712 if (lang_hooks.types.get_debug_type)
23713 {
23714 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23715 if (debug_type != NULL)
23716 discr_type = debug_type;
23717 }
23718
23719 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23720 return false;
23721
23722 /* Signedness can vary between the original type and the debug type. This
23723 can happen for character types in Ada for instance: the character type
23724 used for code generation can be signed, to be compatible with the C one,
23725 but from a debugger point of view, it must be unsigned. */
23726 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23727 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23728
23729 if (is_orig_unsigned != is_debug_unsigned)
23730 src = fold_convert (discr_type, src);
23731
23732 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23733 return false;
23734
23735 dest->pos = is_debug_unsigned;
23736 if (is_debug_unsigned)
23737 dest->v.uval = tree_to_uhwi (src);
23738 else
23739 dest->v.sval = tree_to_shwi (src);
23740
23741 return true;
23742 }
23743
23744 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23745 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23746 store NULL_TREE in DISCR_DECL. Otherwise:
23747
23748 - store the discriminant field in STRUCT_TYPE that controls the variant
23749 part to *DISCR_DECL
23750
23751 - put in *DISCR_LISTS_P an array where for each variant, the item
23752 represents the corresponding matching list of discriminant values.
23753
23754 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23755 the above array.
23756
23757 Note that when the array is allocated (i.e. when the analysis is
23758 successful), it is up to the caller to free the array. */
23759
23760 static void
23761 analyze_variants_discr (tree variant_part_decl,
23762 tree struct_type,
23763 tree *discr_decl,
23764 dw_discr_list_ref **discr_lists_p,
23765 unsigned *discr_lists_length)
23766 {
23767 tree variant_part_type = TREE_TYPE (variant_part_decl);
23768 tree variant;
23769 dw_discr_list_ref *discr_lists;
23770 unsigned i;
23771
23772 /* Compute how many variants there are in this variant part. */
23773 *discr_lists_length = 0;
23774 for (variant = TYPE_FIELDS (variant_part_type);
23775 variant != NULL_TREE;
23776 variant = DECL_CHAIN (variant))
23777 ++*discr_lists_length;
23778
23779 *discr_decl = NULL_TREE;
23780 *discr_lists_p
23781 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23782 sizeof (**discr_lists_p));
23783 discr_lists = *discr_lists_p;
23784
23785 /* And then analyze all variants to extract discriminant information for all
23786 of them. This analysis is conservative: as soon as we detect something we
23787 do not support, abort everything and pretend we found nothing. */
23788 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23789 variant != NULL_TREE;
23790 variant = DECL_CHAIN (variant), ++i)
23791 {
23792 tree match_expr = DECL_QUALIFIER (variant);
23793
23794 /* Now, try to analyze the predicate and deduce a discriminant for
23795 it. */
23796 if (match_expr == boolean_true_node)
23797 /* Typically happens for the default variant: it matches all cases that
23798 previous variants rejected. Don't output any matching value for
23799 this one. */
23800 continue;
23801
23802 /* The following loop tries to iterate over each discriminant
23803 possibility: single values or ranges. */
23804 while (match_expr != NULL_TREE)
23805 {
23806 tree next_round_match_expr;
23807 tree candidate_discr = NULL_TREE;
23808 dw_discr_list_ref new_node = NULL;
23809
23810 /* Possibilities are matched one after the other by nested
23811 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23812 continue with the rest at next iteration. */
23813 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23814 {
23815 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23816 match_expr = TREE_OPERAND (match_expr, 1);
23817 }
23818 else
23819 next_round_match_expr = NULL_TREE;
23820
23821 if (match_expr == boolean_false_node)
23822 /* This sub-expression matches nothing: just wait for the next
23823 one. */
23824 ;
23825
23826 else if (TREE_CODE (match_expr) == EQ_EXPR)
23827 {
23828 /* We are matching: <discr_field> == <integer_cst>
23829 This sub-expression matches a single value. */
23830 tree integer_cst = TREE_OPERAND (match_expr, 1);
23831
23832 candidate_discr
23833 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23834 struct_type);
23835
23836 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23837 if (!get_discr_value (integer_cst,
23838 &new_node->dw_discr_lower_bound))
23839 goto abort;
23840 new_node->dw_discr_range = false;
23841 }
23842
23843 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23844 {
23845 /* We are matching:
23846 <discr_field> > <integer_cst>
23847 && <discr_field> < <integer_cst>.
23848 This sub-expression matches the range of values between the
23849 two matched integer constants. Note that comparisons can be
23850 inclusive or exclusive. */
23851 tree candidate_discr_1, candidate_discr_2;
23852 tree lower_cst, upper_cst;
23853 bool lower_cst_included, upper_cst_included;
23854 tree lower_op = TREE_OPERAND (match_expr, 0);
23855 tree upper_op = TREE_OPERAND (match_expr, 1);
23856
23857 /* When the comparison is exclusive, the integer constant is not
23858 the discriminant range bound we are looking for: we will have
23859 to increment or decrement it. */
23860 if (TREE_CODE (lower_op) == GE_EXPR)
23861 lower_cst_included = true;
23862 else if (TREE_CODE (lower_op) == GT_EXPR)
23863 lower_cst_included = false;
23864 else
23865 goto abort;
23866
23867 if (TREE_CODE (upper_op) == LE_EXPR)
23868 upper_cst_included = true;
23869 else if (TREE_CODE (upper_op) == LT_EXPR)
23870 upper_cst_included = false;
23871 else
23872 goto abort;
23873
23874 /* Extract the discriminant from the first operand and check it
23875 is consistant with the same analysis in the second
23876 operand. */
23877 candidate_discr_1
23878 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23879 struct_type);
23880 candidate_discr_2
23881 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23882 struct_type);
23883 if (candidate_discr_1 == candidate_discr_2)
23884 candidate_discr = candidate_discr_1;
23885 else
23886 goto abort;
23887
23888 /* Extract bounds from both. */
23889 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23890 lower_cst = TREE_OPERAND (lower_op, 1);
23891 upper_cst = TREE_OPERAND (upper_op, 1);
23892
23893 if (!lower_cst_included)
23894 lower_cst
23895 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23896 build_int_cst (TREE_TYPE (lower_cst), 1));
23897 if (!upper_cst_included)
23898 upper_cst
23899 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23900 build_int_cst (TREE_TYPE (upper_cst), 1));
23901
23902 if (!get_discr_value (lower_cst,
23903 &new_node->dw_discr_lower_bound)
23904 || !get_discr_value (upper_cst,
23905 &new_node->dw_discr_upper_bound))
23906 goto abort;
23907
23908 new_node->dw_discr_range = true;
23909 }
23910
23911 else
23912 /* Unsupported sub-expression: we cannot determine the set of
23913 matching discriminant values. Abort everything. */
23914 goto abort;
23915
23916 /* If the discriminant info is not consistant with what we saw so
23917 far, consider the analysis failed and abort everything. */
23918 if (candidate_discr == NULL_TREE
23919 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23920 goto abort;
23921 else
23922 *discr_decl = candidate_discr;
23923
23924 if (new_node != NULL)
23925 {
23926 new_node->dw_discr_next = discr_lists[i];
23927 discr_lists[i] = new_node;
23928 }
23929 match_expr = next_round_match_expr;
23930 }
23931 }
23932
23933 /* If we reach this point, we could match everything we were interested
23934 in. */
23935 return;
23936
23937 abort:
23938 /* Clean all data structure and return no result. */
23939 free (*discr_lists_p);
23940 *discr_lists_p = NULL;
23941 *discr_decl = NULL_TREE;
23942 }
23943
23944 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23945 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23946 under CONTEXT_DIE.
23947
23948 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23949 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23950 this type, which are record types, represent the available variants and each
23951 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23952 values are inferred from these attributes.
23953
23954 In trees, the offsets for the fields inside these sub-records are relative
23955 to the variant part itself, whereas the corresponding DIEs should have
23956 offset attributes that are relative to the embedding record base address.
23957 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23958 must be an expression that computes the offset of the variant part to
23959 describe in DWARF. */
23960
23961 static void
23962 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23963 dw_die_ref context_die)
23964 {
23965 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23966 tree variant_part_offset = vlr_ctx->variant_part_offset;
23967 struct loc_descr_context ctx = {
23968 vlr_ctx->struct_type, /* context_type */
23969 NULL_TREE, /* base_decl */
23970 NULL, /* dpi */
23971 false, /* placeholder_arg */
23972 false /* placeholder_seen */
23973 };
23974
23975 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23976 NULL_TREE if there is no such field. */
23977 tree discr_decl = NULL_TREE;
23978 dw_discr_list_ref *discr_lists;
23979 unsigned discr_lists_length = 0;
23980 unsigned i;
23981
23982 dw_die_ref dwarf_proc_die = NULL;
23983 dw_die_ref variant_part_die
23984 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23985
23986 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23987
23988 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23989 &discr_decl, &discr_lists, &discr_lists_length);
23990
23991 if (discr_decl != NULL_TREE)
23992 {
23993 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23994
23995 if (discr_die)
23996 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23997 else
23998 /* We have no DIE for the discriminant, so just discard all
23999 discrimimant information in the output. */
24000 discr_decl = NULL_TREE;
24001 }
24002
24003 /* If the offset for this variant part is more complex than a constant,
24004 create a DWARF procedure for it so that we will not have to generate DWARF
24005 expressions for it for each member. */
24006 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24007 && (dwarf_version >= 3 || !dwarf_strict))
24008 {
24009 const tree dwarf_proc_fndecl
24010 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24011 build_function_type (TREE_TYPE (variant_part_offset),
24012 NULL_TREE));
24013 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24014 const dw_loc_descr_ref dwarf_proc_body
24015 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24016
24017 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24018 dwarf_proc_fndecl, context_die);
24019 if (dwarf_proc_die != NULL)
24020 variant_part_offset = dwarf_proc_call;
24021 }
24022
24023 /* Output DIEs for all variants. */
24024 i = 0;
24025 for (tree variant = TYPE_FIELDS (variant_part_type);
24026 variant != NULL_TREE;
24027 variant = DECL_CHAIN (variant), ++i)
24028 {
24029 tree variant_type = TREE_TYPE (variant);
24030 dw_die_ref variant_die;
24031
24032 /* All variants (i.e. members of a variant part) are supposed to be
24033 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24034 under these records. */
24035 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24036
24037 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24038 equate_decl_number_to_die (variant, variant_die);
24039
24040 /* Output discriminant values this variant matches, if any. */
24041 if (discr_decl == NULL || discr_lists[i] == NULL)
24042 /* In the case we have discriminant information at all, this is
24043 probably the default variant: as the standard says, don't
24044 output any discriminant value/list attribute. */
24045 ;
24046 else if (discr_lists[i]->dw_discr_next == NULL
24047 && !discr_lists[i]->dw_discr_range)
24048 /* If there is only one accepted value, don't bother outputting a
24049 list. */
24050 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24051 else
24052 add_discr_list (variant_die, discr_lists[i]);
24053
24054 for (tree member = TYPE_FIELDS (variant_type);
24055 member != NULL_TREE;
24056 member = DECL_CHAIN (member))
24057 {
24058 struct vlr_context vlr_sub_ctx = {
24059 vlr_ctx->struct_type, /* struct_type */
24060 NULL /* variant_part_offset */
24061 };
24062 if (is_variant_part (member))
24063 {
24064 /* All offsets for fields inside variant parts are relative to
24065 the top-level embedding RECORD_TYPE's base address. On the
24066 other hand, offsets in GCC's types are relative to the
24067 nested-most variant part. So we have to sum offsets each time
24068 we recurse. */
24069
24070 vlr_sub_ctx.variant_part_offset
24071 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24072 variant_part_offset, byte_position (member));
24073 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24074 }
24075 else
24076 {
24077 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24078 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24079 }
24080 }
24081 }
24082
24083 free (discr_lists);
24084 }
24085
24086 /* Generate a DIE for a class member. */
24087
24088 static void
24089 gen_member_die (tree type, dw_die_ref context_die)
24090 {
24091 tree member;
24092 tree binfo = TYPE_BINFO (type);
24093
24094 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24095
24096 /* If this is not an incomplete type, output descriptions of each of its
24097 members. Note that as we output the DIEs necessary to represent the
24098 members of this record or union type, we will also be trying to output
24099 DIEs to represent the *types* of those members. However the `type'
24100 function (above) will specifically avoid generating type DIEs for member
24101 types *within* the list of member DIEs for this (containing) type except
24102 for those types (of members) which are explicitly marked as also being
24103 members of this (containing) type themselves. The g++ front- end can
24104 force any given type to be treated as a member of some other (containing)
24105 type by setting the TYPE_CONTEXT of the given (member) type to point to
24106 the TREE node representing the appropriate (containing) type. */
24107
24108 /* First output info about the base classes. */
24109 if (binfo)
24110 {
24111 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24112 int i;
24113 tree base;
24114
24115 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24116 gen_inheritance_die (base,
24117 (accesses ? (*accesses)[i] : access_public_node),
24118 type,
24119 context_die);
24120 }
24121
24122 /* Now output info about the data members and type members. */
24123 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24124 {
24125 struct vlr_context vlr_ctx = { type, NULL_TREE };
24126 bool static_inline_p
24127 = (TREE_STATIC (member)
24128 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24129 != -1));
24130
24131 /* Ignore clones. */
24132 if (DECL_ABSTRACT_ORIGIN (member))
24133 continue;
24134
24135 /* If we thought we were generating minimal debug info for TYPE
24136 and then changed our minds, some of the member declarations
24137 may have already been defined. Don't define them again, but
24138 do put them in the right order. */
24139
24140 if (dw_die_ref child = lookup_decl_die (member))
24141 {
24142 /* Handle inline static data members, which only have in-class
24143 declarations. */
24144 dw_die_ref ref = NULL;
24145 if (child->die_tag == DW_TAG_variable
24146 && child->die_parent == comp_unit_die ())
24147 {
24148 ref = get_AT_ref (child, DW_AT_specification);
24149 /* For C++17 inline static data members followed by redundant
24150 out of class redeclaration, we might get here with
24151 child being the DIE created for the out of class
24152 redeclaration and with its DW_AT_specification being
24153 the DIE created for in-class definition. We want to
24154 reparent the latter, and don't want to create another
24155 DIE with DW_AT_specification in that case, because
24156 we already have one. */
24157 if (ref
24158 && static_inline_p
24159 && ref->die_tag == DW_TAG_variable
24160 && ref->die_parent == comp_unit_die ()
24161 && get_AT (ref, DW_AT_specification) == NULL)
24162 {
24163 child = ref;
24164 ref = NULL;
24165 static_inline_p = false;
24166 }
24167 }
24168
24169 if (child->die_tag == DW_TAG_variable
24170 && child->die_parent == comp_unit_die ()
24171 && ref == NULL)
24172 {
24173 reparent_child (child, context_die);
24174 if (dwarf_version < 5)
24175 child->die_tag = DW_TAG_member;
24176 }
24177 else
24178 splice_child_die (context_die, child);
24179 }
24180
24181 /* Do not generate standard DWARF for variant parts if we are generating
24182 the corresponding GNAT encodings: DIEs generated for both would
24183 conflict in our mappings. */
24184 else if (is_variant_part (member)
24185 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24186 {
24187 vlr_ctx.variant_part_offset = byte_position (member);
24188 gen_variant_part (member, &vlr_ctx, context_die);
24189 }
24190 else
24191 {
24192 vlr_ctx.variant_part_offset = NULL_TREE;
24193 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24194 }
24195
24196 /* For C++ inline static data members emit immediately a DW_TAG_variable
24197 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24198 DW_AT_specification. */
24199 if (static_inline_p)
24200 {
24201 int old_extern = DECL_EXTERNAL (member);
24202 DECL_EXTERNAL (member) = 0;
24203 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24204 DECL_EXTERNAL (member) = old_extern;
24205 }
24206 }
24207 }
24208
24209 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24210 is set, we pretend that the type was never defined, so we only get the
24211 member DIEs needed by later specification DIEs. */
24212
24213 static void
24214 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24215 enum debug_info_usage usage)
24216 {
24217 if (TREE_ASM_WRITTEN (type))
24218 {
24219 /* Fill in the bound of variable-length fields in late dwarf if
24220 still incomplete. */
24221 if (!early_dwarf && variably_modified_type_p (type, NULL))
24222 for (tree member = TYPE_FIELDS (type);
24223 member;
24224 member = DECL_CHAIN (member))
24225 fill_variable_array_bounds (TREE_TYPE (member));
24226 return;
24227 }
24228
24229 dw_die_ref type_die = lookup_type_die (type);
24230 dw_die_ref scope_die = 0;
24231 int nested = 0;
24232 int complete = (TYPE_SIZE (type)
24233 && (! TYPE_STUB_DECL (type)
24234 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24235 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24236 complete = complete && should_emit_struct_debug (type, usage);
24237
24238 if (type_die && ! complete)
24239 return;
24240
24241 if (TYPE_CONTEXT (type) != NULL_TREE
24242 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24243 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24244 nested = 1;
24245
24246 scope_die = scope_die_for (type, context_die);
24247
24248 /* Generate child dies for template paramaters. */
24249 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24250 schedule_generic_params_dies_gen (type);
24251
24252 if (! type_die || (nested && is_cu_die (scope_die)))
24253 /* First occurrence of type or toplevel definition of nested class. */
24254 {
24255 dw_die_ref old_die = type_die;
24256
24257 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24258 ? record_type_tag (type) : DW_TAG_union_type,
24259 scope_die, type);
24260 equate_type_number_to_die (type, type_die);
24261 if (old_die)
24262 add_AT_specification (type_die, old_die);
24263 else
24264 add_name_attribute (type_die, type_tag (type));
24265 }
24266 else
24267 remove_AT (type_die, DW_AT_declaration);
24268
24269 /* If this type has been completed, then give it a byte_size attribute and
24270 then give a list of members. */
24271 if (complete && !ns_decl)
24272 {
24273 /* Prevent infinite recursion in cases where the type of some member of
24274 this type is expressed in terms of this type itself. */
24275 TREE_ASM_WRITTEN (type) = 1;
24276 add_byte_size_attribute (type_die, type);
24277 add_alignment_attribute (type_die, type);
24278 if (TYPE_STUB_DECL (type) != NULL_TREE)
24279 {
24280 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24281 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24282 }
24283
24284 /* If the first reference to this type was as the return type of an
24285 inline function, then it may not have a parent. Fix this now. */
24286 if (type_die->die_parent == NULL)
24287 add_child_die (scope_die, type_die);
24288
24289 push_decl_scope (type);
24290 gen_member_die (type, type_die);
24291 pop_decl_scope ();
24292
24293 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24294 if (TYPE_ARTIFICIAL (type))
24295 add_AT_flag (type_die, DW_AT_artificial, 1);
24296
24297 /* GNU extension: Record what type our vtable lives in. */
24298 if (TYPE_VFIELD (type))
24299 {
24300 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24301
24302 gen_type_die (vtype, context_die);
24303 add_AT_die_ref (type_die, DW_AT_containing_type,
24304 lookup_type_die (vtype));
24305 }
24306 }
24307 else
24308 {
24309 add_AT_flag (type_die, DW_AT_declaration, 1);
24310
24311 /* We don't need to do this for function-local types. */
24312 if (TYPE_STUB_DECL (type)
24313 && ! decl_function_context (TYPE_STUB_DECL (type)))
24314 vec_safe_push (incomplete_types, type);
24315 }
24316
24317 if (get_AT (type_die, DW_AT_name))
24318 add_pubtype (type, type_die);
24319 }
24320
24321 /* Generate a DIE for a subroutine _type_. */
24322
24323 static void
24324 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24325 {
24326 tree return_type = TREE_TYPE (type);
24327 dw_die_ref subr_die
24328 = new_die (DW_TAG_subroutine_type,
24329 scope_die_for (type, context_die), type);
24330
24331 equate_type_number_to_die (type, subr_die);
24332 add_prototyped_attribute (subr_die, type);
24333 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24334 context_die);
24335 add_alignment_attribute (subr_die, type);
24336 gen_formal_types_die (type, subr_die);
24337
24338 if (get_AT (subr_die, DW_AT_name))
24339 add_pubtype (type, subr_die);
24340 if ((dwarf_version >= 5 || !dwarf_strict)
24341 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24342 add_AT_flag (subr_die, DW_AT_reference, 1);
24343 if ((dwarf_version >= 5 || !dwarf_strict)
24344 && lang_hooks.types.type_dwarf_attribute (type,
24345 DW_AT_rvalue_reference) != -1)
24346 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24347 }
24348
24349 /* Generate a DIE for a type definition. */
24350
24351 static void
24352 gen_typedef_die (tree decl, dw_die_ref context_die)
24353 {
24354 dw_die_ref type_die;
24355 tree type;
24356
24357 if (TREE_ASM_WRITTEN (decl))
24358 {
24359 if (DECL_ORIGINAL_TYPE (decl))
24360 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24361 return;
24362 }
24363
24364 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24365 checks in process_scope_var and modified_type_die), this should be called
24366 only for original types. */
24367 gcc_assert (decl_ultimate_origin (decl) == NULL
24368 || decl_ultimate_origin (decl) == decl);
24369
24370 TREE_ASM_WRITTEN (decl) = 1;
24371 type_die = new_die (DW_TAG_typedef, context_die, decl);
24372
24373 add_name_and_src_coords_attributes (type_die, decl);
24374 if (DECL_ORIGINAL_TYPE (decl))
24375 {
24376 type = DECL_ORIGINAL_TYPE (decl);
24377 if (type == error_mark_node)
24378 return;
24379
24380 gcc_assert (type != TREE_TYPE (decl));
24381 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24382 }
24383 else
24384 {
24385 type = TREE_TYPE (decl);
24386 if (type == error_mark_node)
24387 return;
24388
24389 if (is_naming_typedef_decl (TYPE_NAME (type)))
24390 {
24391 /* Here, we are in the case of decl being a typedef naming
24392 an anonymous type, e.g:
24393 typedef struct {...} foo;
24394 In that case TREE_TYPE (decl) is not a typedef variant
24395 type and TYPE_NAME of the anonymous type is set to the
24396 TYPE_DECL of the typedef. This construct is emitted by
24397 the C++ FE.
24398
24399 TYPE is the anonymous struct named by the typedef
24400 DECL. As we need the DW_AT_type attribute of the
24401 DW_TAG_typedef to point to the DIE of TYPE, let's
24402 generate that DIE right away. add_type_attribute
24403 called below will then pick (via lookup_type_die) that
24404 anonymous struct DIE. */
24405 if (!TREE_ASM_WRITTEN (type))
24406 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24407
24408 /* This is a GNU Extension. We are adding a
24409 DW_AT_linkage_name attribute to the DIE of the
24410 anonymous struct TYPE. The value of that attribute
24411 is the name of the typedef decl naming the anonymous
24412 struct. This greatly eases the work of consumers of
24413 this debug info. */
24414 add_linkage_name_raw (lookup_type_die (type), decl);
24415 }
24416 }
24417
24418 add_type_attribute (type_die, type, decl_quals (decl), false,
24419 context_die);
24420
24421 if (is_naming_typedef_decl (decl))
24422 /* We want that all subsequent calls to lookup_type_die with
24423 TYPE in argument yield the DW_TAG_typedef we have just
24424 created. */
24425 equate_type_number_to_die (type, type_die);
24426
24427 add_alignment_attribute (type_die, TREE_TYPE (decl));
24428
24429 add_accessibility_attribute (type_die, decl);
24430
24431 if (DECL_ABSTRACT_P (decl))
24432 equate_decl_number_to_die (decl, type_die);
24433
24434 if (get_AT (type_die, DW_AT_name))
24435 add_pubtype (decl, type_die);
24436 }
24437
24438 /* Generate a DIE for a struct, class, enum or union type. */
24439
24440 static void
24441 gen_tagged_type_die (tree type,
24442 dw_die_ref context_die,
24443 enum debug_info_usage usage)
24444 {
24445 int need_pop;
24446
24447 if (type == NULL_TREE
24448 || !is_tagged_type (type))
24449 return;
24450
24451 if (TREE_ASM_WRITTEN (type))
24452 need_pop = 0;
24453 /* If this is a nested type whose containing class hasn't been written
24454 out yet, writing it out will cover this one, too. This does not apply
24455 to instantiations of member class templates; they need to be added to
24456 the containing class as they are generated. FIXME: This hurts the
24457 idea of combining type decls from multiple TUs, since we can't predict
24458 what set of template instantiations we'll get. */
24459 else if (TYPE_CONTEXT (type)
24460 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24461 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24462 {
24463 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24464
24465 if (TREE_ASM_WRITTEN (type))
24466 return;
24467
24468 /* If that failed, attach ourselves to the stub. */
24469 push_decl_scope (TYPE_CONTEXT (type));
24470 context_die = lookup_type_die (TYPE_CONTEXT (type));
24471 need_pop = 1;
24472 }
24473 else if (TYPE_CONTEXT (type) != NULL_TREE
24474 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24475 {
24476 /* If this type is local to a function that hasn't been written
24477 out yet, use a NULL context for now; it will be fixed up in
24478 decls_for_scope. */
24479 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24480 /* A declaration DIE doesn't count; nested types need to go in the
24481 specification. */
24482 if (context_die && is_declaration_die (context_die))
24483 context_die = NULL;
24484 need_pop = 0;
24485 }
24486 else
24487 {
24488 context_die = declare_in_namespace (type, context_die);
24489 need_pop = 0;
24490 }
24491
24492 if (TREE_CODE (type) == ENUMERAL_TYPE)
24493 {
24494 /* This might have been written out by the call to
24495 declare_in_namespace. */
24496 if (!TREE_ASM_WRITTEN (type))
24497 gen_enumeration_type_die (type, context_die);
24498 }
24499 else
24500 gen_struct_or_union_type_die (type, context_die, usage);
24501
24502 if (need_pop)
24503 pop_decl_scope ();
24504
24505 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24506 it up if it is ever completed. gen_*_type_die will set it for us
24507 when appropriate. */
24508 }
24509
24510 /* Generate a type description DIE. */
24511
24512 static void
24513 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24514 enum debug_info_usage usage)
24515 {
24516 struct array_descr_info info;
24517
24518 if (type == NULL_TREE || type == error_mark_node)
24519 return;
24520
24521 if (flag_checking && type)
24522 verify_type (type);
24523
24524 if (TYPE_NAME (type) != NULL_TREE
24525 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24526 && is_redundant_typedef (TYPE_NAME (type))
24527 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24528 /* The DECL of this type is a typedef we don't want to emit debug
24529 info for but we want debug info for its underlying typedef.
24530 This can happen for e.g, the injected-class-name of a C++
24531 type. */
24532 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24533
24534 /* If TYPE is a typedef type variant, let's generate debug info
24535 for the parent typedef which TYPE is a type of. */
24536 if (typedef_variant_p (type))
24537 {
24538 if (TREE_ASM_WRITTEN (type))
24539 return;
24540
24541 tree name = TYPE_NAME (type);
24542 tree origin = decl_ultimate_origin (name);
24543 if (origin != NULL && origin != name)
24544 {
24545 gen_decl_die (origin, NULL, NULL, context_die);
24546 return;
24547 }
24548
24549 /* Prevent broken recursion; we can't hand off to the same type. */
24550 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24551
24552 /* Give typedefs the right scope. */
24553 context_die = scope_die_for (type, context_die);
24554
24555 TREE_ASM_WRITTEN (type) = 1;
24556
24557 gen_decl_die (name, NULL, NULL, context_die);
24558 return;
24559 }
24560
24561 /* If type is an anonymous tagged type named by a typedef, let's
24562 generate debug info for the typedef. */
24563 if (is_naming_typedef_decl (TYPE_NAME (type)))
24564 {
24565 /* Use the DIE of the containing namespace as the parent DIE of
24566 the type description DIE we want to generate. */
24567 if (DECL_CONTEXT (TYPE_NAME (type))
24568 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24569 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24570
24571 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24572 return;
24573 }
24574
24575 if (lang_hooks.types.get_debug_type)
24576 {
24577 tree debug_type = lang_hooks.types.get_debug_type (type);
24578
24579 if (debug_type != NULL_TREE && debug_type != type)
24580 {
24581 gen_type_die_with_usage (debug_type, context_die, usage);
24582 return;
24583 }
24584 }
24585
24586 /* We are going to output a DIE to represent the unqualified version
24587 of this type (i.e. without any const or volatile qualifiers) so
24588 get the main variant (i.e. the unqualified version) of this type
24589 now. (Vectors and arrays are special because the debugging info is in the
24590 cloned type itself. Similarly function/method types can contain extra
24591 ref-qualification). */
24592 if (TREE_CODE (type) == FUNCTION_TYPE
24593 || TREE_CODE (type) == METHOD_TYPE)
24594 {
24595 /* For function/method types, can't use type_main_variant here,
24596 because that can have different ref-qualifiers for C++,
24597 but try to canonicalize. */
24598 tree main = TYPE_MAIN_VARIANT (type);
24599 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24600 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24601 && check_base_type (t, main)
24602 && check_lang_type (t, type))
24603 {
24604 type = t;
24605 break;
24606 }
24607 }
24608 else if (TREE_CODE (type) != VECTOR_TYPE
24609 && TREE_CODE (type) != ARRAY_TYPE)
24610 type = type_main_variant (type);
24611
24612 /* If this is an array type with hidden descriptor, handle it first. */
24613 if (!TREE_ASM_WRITTEN (type)
24614 && lang_hooks.types.get_array_descr_info)
24615 {
24616 memset (&info, 0, sizeof (info));
24617 if (lang_hooks.types.get_array_descr_info (type, &info))
24618 {
24619 /* Fortran sometimes emits array types with no dimension. */
24620 gcc_assert (info.ndimensions >= 0
24621 && (info.ndimensions
24622 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24623 gen_descr_array_type_die (type, &info, context_die);
24624 TREE_ASM_WRITTEN (type) = 1;
24625 return;
24626 }
24627 }
24628
24629 if (TREE_ASM_WRITTEN (type))
24630 {
24631 /* Variable-length types may be incomplete even if
24632 TREE_ASM_WRITTEN. For such types, fall through to
24633 gen_array_type_die() and possibly fill in
24634 DW_AT_{upper,lower}_bound attributes. */
24635 if ((TREE_CODE (type) != ARRAY_TYPE
24636 && TREE_CODE (type) != RECORD_TYPE
24637 && TREE_CODE (type) != UNION_TYPE
24638 && TREE_CODE (type) != QUAL_UNION_TYPE)
24639 || !variably_modified_type_p (type, NULL))
24640 return;
24641 }
24642
24643 switch (TREE_CODE (type))
24644 {
24645 case ERROR_MARK:
24646 break;
24647
24648 case POINTER_TYPE:
24649 case REFERENCE_TYPE:
24650 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24651 ensures that the gen_type_die recursion will terminate even if the
24652 type is recursive. Recursive types are possible in Ada. */
24653 /* ??? We could perhaps do this for all types before the switch
24654 statement. */
24655 TREE_ASM_WRITTEN (type) = 1;
24656
24657 /* For these types, all that is required is that we output a DIE (or a
24658 set of DIEs) to represent the "basis" type. */
24659 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24660 DINFO_USAGE_IND_USE);
24661 break;
24662
24663 case OFFSET_TYPE:
24664 /* This code is used for C++ pointer-to-data-member types.
24665 Output a description of the relevant class type. */
24666 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24667 DINFO_USAGE_IND_USE);
24668
24669 /* Output a description of the type of the object pointed to. */
24670 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24671 DINFO_USAGE_IND_USE);
24672
24673 /* Now output a DIE to represent this pointer-to-data-member type
24674 itself. */
24675 gen_ptr_to_mbr_type_die (type, context_die);
24676 break;
24677
24678 case FUNCTION_TYPE:
24679 /* Force out return type (in case it wasn't forced out already). */
24680 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24681 DINFO_USAGE_DIR_USE);
24682 gen_subroutine_type_die (type, context_die);
24683 break;
24684
24685 case METHOD_TYPE:
24686 /* Force out return type (in case it wasn't forced out already). */
24687 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24688 DINFO_USAGE_DIR_USE);
24689 gen_subroutine_type_die (type, context_die);
24690 break;
24691
24692 case ARRAY_TYPE:
24693 case VECTOR_TYPE:
24694 gen_array_type_die (type, context_die);
24695 break;
24696
24697 case ENUMERAL_TYPE:
24698 case RECORD_TYPE:
24699 case UNION_TYPE:
24700 case QUAL_UNION_TYPE:
24701 gen_tagged_type_die (type, context_die, usage);
24702 return;
24703
24704 case VOID_TYPE:
24705 case INTEGER_TYPE:
24706 case REAL_TYPE:
24707 case FIXED_POINT_TYPE:
24708 case COMPLEX_TYPE:
24709 case BOOLEAN_TYPE:
24710 case POINTER_BOUNDS_TYPE:
24711 /* No DIEs needed for fundamental types. */
24712 break;
24713
24714 case NULLPTR_TYPE:
24715 case LANG_TYPE:
24716 /* Just use DW_TAG_unspecified_type. */
24717 {
24718 dw_die_ref type_die = lookup_type_die (type);
24719 if (type_die == NULL)
24720 {
24721 tree name = TYPE_IDENTIFIER (type);
24722 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24723 type);
24724 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24725 equate_type_number_to_die (type, type_die);
24726 }
24727 }
24728 break;
24729
24730 default:
24731 if (is_cxx_auto (type))
24732 {
24733 tree name = TYPE_IDENTIFIER (type);
24734 dw_die_ref *die = (name == get_identifier ("auto")
24735 ? &auto_die : &decltype_auto_die);
24736 if (!*die)
24737 {
24738 *die = new_die (DW_TAG_unspecified_type,
24739 comp_unit_die (), NULL_TREE);
24740 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24741 }
24742 equate_type_number_to_die (type, *die);
24743 break;
24744 }
24745 gcc_unreachable ();
24746 }
24747
24748 TREE_ASM_WRITTEN (type) = 1;
24749 }
24750
24751 static void
24752 gen_type_die (tree type, dw_die_ref context_die)
24753 {
24754 if (type != error_mark_node)
24755 {
24756 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24757 if (flag_checking)
24758 {
24759 dw_die_ref die = lookup_type_die (type);
24760 if (die)
24761 check_die (die);
24762 }
24763 }
24764 }
24765
24766 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24767 things which are local to the given block. */
24768
24769 static void
24770 gen_block_die (tree stmt, dw_die_ref context_die)
24771 {
24772 int must_output_die = 0;
24773 bool inlined_func;
24774
24775 /* Ignore blocks that are NULL. */
24776 if (stmt == NULL_TREE)
24777 return;
24778
24779 inlined_func = inlined_function_outer_scope_p (stmt);
24780
24781 /* If the block is one fragment of a non-contiguous block, do not
24782 process the variables, since they will have been done by the
24783 origin block. Do process subblocks. */
24784 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24785 {
24786 tree sub;
24787
24788 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24789 gen_block_die (sub, context_die);
24790
24791 return;
24792 }
24793
24794 /* Determine if we need to output any Dwarf DIEs at all to represent this
24795 block. */
24796 if (inlined_func)
24797 /* The outer scopes for inlinings *must* always be represented. We
24798 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24799 must_output_die = 1;
24800 else
24801 {
24802 /* Determine if this block directly contains any "significant"
24803 local declarations which we will need to output DIEs for. */
24804 if (debug_info_level > DINFO_LEVEL_TERSE)
24805 /* We are not in terse mode so *any* local declaration counts
24806 as being a "significant" one. */
24807 must_output_die = ((BLOCK_VARS (stmt) != NULL
24808 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24809 && (TREE_USED (stmt)
24810 || TREE_ASM_WRITTEN (stmt)
24811 || BLOCK_ABSTRACT (stmt)));
24812 else if ((TREE_USED (stmt)
24813 || TREE_ASM_WRITTEN (stmt)
24814 || BLOCK_ABSTRACT (stmt))
24815 && !dwarf2out_ignore_block (stmt))
24816 must_output_die = 1;
24817 }
24818
24819 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24820 DIE for any block which contains no significant local declarations at
24821 all. Rather, in such cases we just call `decls_for_scope' so that any
24822 needed Dwarf info for any sub-blocks will get properly generated. Note
24823 that in terse mode, our definition of what constitutes a "significant"
24824 local declaration gets restricted to include only inlined function
24825 instances and local (nested) function definitions. */
24826 if (must_output_die)
24827 {
24828 if (inlined_func)
24829 {
24830 /* If STMT block is abstract, that means we have been called
24831 indirectly from dwarf2out_abstract_function.
24832 That function rightfully marks the descendent blocks (of
24833 the abstract function it is dealing with) as being abstract,
24834 precisely to prevent us from emitting any
24835 DW_TAG_inlined_subroutine DIE as a descendent
24836 of an abstract function instance. So in that case, we should
24837 not call gen_inlined_subroutine_die.
24838
24839 Later though, when cgraph asks dwarf2out to emit info
24840 for the concrete instance of the function decl into which
24841 the concrete instance of STMT got inlined, the later will lead
24842 to the generation of a DW_TAG_inlined_subroutine DIE. */
24843 if (! BLOCK_ABSTRACT (stmt))
24844 gen_inlined_subroutine_die (stmt, context_die);
24845 }
24846 else
24847 gen_lexical_block_die (stmt, context_die);
24848 }
24849 else
24850 decls_for_scope (stmt, context_die);
24851 }
24852
24853 /* Process variable DECL (or variable with origin ORIGIN) within
24854 block STMT and add it to CONTEXT_DIE. */
24855 static void
24856 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24857 {
24858 dw_die_ref die;
24859 tree decl_or_origin = decl ? decl : origin;
24860
24861 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24862 die = lookup_decl_die (decl_or_origin);
24863 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24864 {
24865 if (TYPE_DECL_IS_STUB (decl_or_origin))
24866 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24867 else
24868 die = lookup_decl_die (decl_or_origin);
24869 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24870 if (! die && ! early_dwarf)
24871 return;
24872 }
24873 else
24874 die = NULL;
24875
24876 /* Avoid creating DIEs for local typedefs and concrete static variables that
24877 will only be pruned later. */
24878 if ((origin || decl_ultimate_origin (decl))
24879 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24880 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24881 {
24882 origin = decl_ultimate_origin (decl_or_origin);
24883 if (decl && VAR_P (decl) && die != NULL)
24884 {
24885 die = lookup_decl_die (origin);
24886 if (die != NULL)
24887 equate_decl_number_to_die (decl, die);
24888 }
24889 return;
24890 }
24891
24892 if (die != NULL && die->die_parent == NULL)
24893 add_child_die (context_die, die);
24894 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24895 {
24896 if (early_dwarf)
24897 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24898 stmt, context_die);
24899 }
24900 else
24901 {
24902 if (decl && DECL_P (decl))
24903 {
24904 die = lookup_decl_die (decl);
24905
24906 /* Early created DIEs do not have a parent as the decls refer
24907 to the function as DECL_CONTEXT rather than the BLOCK. */
24908 if (die && die->die_parent == NULL)
24909 {
24910 gcc_assert (in_lto_p);
24911 add_child_die (context_die, die);
24912 }
24913 }
24914
24915 gen_decl_die (decl, origin, NULL, context_die);
24916 }
24917 }
24918
24919 /* Generate all of the decls declared within a given scope and (recursively)
24920 all of its sub-blocks. */
24921
24922 static void
24923 decls_for_scope (tree stmt, dw_die_ref context_die)
24924 {
24925 tree decl;
24926 unsigned int i;
24927 tree subblocks;
24928
24929 /* Ignore NULL blocks. */
24930 if (stmt == NULL_TREE)
24931 return;
24932
24933 /* Output the DIEs to represent all of the data objects and typedefs
24934 declared directly within this block but not within any nested
24935 sub-blocks. Also, nested function and tag DIEs have been
24936 generated with a parent of NULL; fix that up now. We don't
24937 have to do this if we're at -g1. */
24938 if (debug_info_level > DINFO_LEVEL_TERSE)
24939 {
24940 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24941 process_scope_var (stmt, decl, NULL_TREE, context_die);
24942 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24943 origin - avoid doing this twice as we have no good way to see
24944 if we've done it once already. */
24945 if (! early_dwarf)
24946 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24947 {
24948 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24949 if (decl == current_function_decl)
24950 /* Ignore declarations of the current function, while they
24951 are declarations, gen_subprogram_die would treat them
24952 as definitions again, because they are equal to
24953 current_function_decl and endlessly recurse. */;
24954 else if (TREE_CODE (decl) == FUNCTION_DECL)
24955 process_scope_var (stmt, decl, NULL_TREE, context_die);
24956 else
24957 process_scope_var (stmt, NULL_TREE, decl, context_die);
24958 }
24959 }
24960
24961 /* Even if we're at -g1, we need to process the subblocks in order to get
24962 inlined call information. */
24963
24964 /* Output the DIEs to represent all sub-blocks (and the items declared
24965 therein) of this block. */
24966 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24967 subblocks != NULL;
24968 subblocks = BLOCK_CHAIN (subblocks))
24969 gen_block_die (subblocks, context_die);
24970 }
24971
24972 /* Is this a typedef we can avoid emitting? */
24973
24974 bool
24975 is_redundant_typedef (const_tree decl)
24976 {
24977 if (TYPE_DECL_IS_STUB (decl))
24978 return true;
24979
24980 if (DECL_ARTIFICIAL (decl)
24981 && DECL_CONTEXT (decl)
24982 && is_tagged_type (DECL_CONTEXT (decl))
24983 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24984 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24985 /* Also ignore the artificial member typedef for the class name. */
24986 return true;
24987
24988 return false;
24989 }
24990
24991 /* Return TRUE if TYPE is a typedef that names a type for linkage
24992 purposes. This kind of typedefs is produced by the C++ FE for
24993 constructs like:
24994
24995 typedef struct {...} foo;
24996
24997 In that case, there is no typedef variant type produced for foo.
24998 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24999 struct type. */
25000
25001 static bool
25002 is_naming_typedef_decl (const_tree decl)
25003 {
25004 if (decl == NULL_TREE
25005 || TREE_CODE (decl) != TYPE_DECL
25006 || DECL_NAMELESS (decl)
25007 || !is_tagged_type (TREE_TYPE (decl))
25008 || DECL_IS_BUILTIN (decl)
25009 || is_redundant_typedef (decl)
25010 /* It looks like Ada produces TYPE_DECLs that are very similar
25011 to C++ naming typedefs but that have different
25012 semantics. Let's be specific to c++ for now. */
25013 || !is_cxx (decl))
25014 return FALSE;
25015
25016 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25017 && TYPE_NAME (TREE_TYPE (decl)) == decl
25018 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25019 != TYPE_NAME (TREE_TYPE (decl))));
25020 }
25021
25022 /* Looks up the DIE for a context. */
25023
25024 static inline dw_die_ref
25025 lookup_context_die (tree context)
25026 {
25027 if (context)
25028 {
25029 /* Find die that represents this context. */
25030 if (TYPE_P (context))
25031 {
25032 context = TYPE_MAIN_VARIANT (context);
25033 dw_die_ref ctx = lookup_type_die (context);
25034 if (!ctx)
25035 return NULL;
25036 return strip_naming_typedef (context, ctx);
25037 }
25038 else
25039 return lookup_decl_die (context);
25040 }
25041 return comp_unit_die ();
25042 }
25043
25044 /* Returns the DIE for a context. */
25045
25046 static inline dw_die_ref
25047 get_context_die (tree context)
25048 {
25049 if (context)
25050 {
25051 /* Find die that represents this context. */
25052 if (TYPE_P (context))
25053 {
25054 context = TYPE_MAIN_VARIANT (context);
25055 return strip_naming_typedef (context, force_type_die (context));
25056 }
25057 else
25058 return force_decl_die (context);
25059 }
25060 return comp_unit_die ();
25061 }
25062
25063 /* Returns the DIE for decl. A DIE will always be returned. */
25064
25065 static dw_die_ref
25066 force_decl_die (tree decl)
25067 {
25068 dw_die_ref decl_die;
25069 unsigned saved_external_flag;
25070 tree save_fn = NULL_TREE;
25071 decl_die = lookup_decl_die (decl);
25072 if (!decl_die)
25073 {
25074 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25075
25076 decl_die = lookup_decl_die (decl);
25077 if (decl_die)
25078 return decl_die;
25079
25080 switch (TREE_CODE (decl))
25081 {
25082 case FUNCTION_DECL:
25083 /* Clear current_function_decl, so that gen_subprogram_die thinks
25084 that this is a declaration. At this point, we just want to force
25085 declaration die. */
25086 save_fn = current_function_decl;
25087 current_function_decl = NULL_TREE;
25088 gen_subprogram_die (decl, context_die);
25089 current_function_decl = save_fn;
25090 break;
25091
25092 case VAR_DECL:
25093 /* Set external flag to force declaration die. Restore it after
25094 gen_decl_die() call. */
25095 saved_external_flag = DECL_EXTERNAL (decl);
25096 DECL_EXTERNAL (decl) = 1;
25097 gen_decl_die (decl, NULL, NULL, context_die);
25098 DECL_EXTERNAL (decl) = saved_external_flag;
25099 break;
25100
25101 case NAMESPACE_DECL:
25102 if (dwarf_version >= 3 || !dwarf_strict)
25103 dwarf2out_decl (decl);
25104 else
25105 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25106 decl_die = comp_unit_die ();
25107 break;
25108
25109 case TRANSLATION_UNIT_DECL:
25110 decl_die = comp_unit_die ();
25111 break;
25112
25113 default:
25114 gcc_unreachable ();
25115 }
25116
25117 /* We should be able to find the DIE now. */
25118 if (!decl_die)
25119 decl_die = lookup_decl_die (decl);
25120 gcc_assert (decl_die);
25121 }
25122
25123 return decl_die;
25124 }
25125
25126 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25127 always returned. */
25128
25129 static dw_die_ref
25130 force_type_die (tree type)
25131 {
25132 dw_die_ref type_die;
25133
25134 type_die = lookup_type_die (type);
25135 if (!type_die)
25136 {
25137 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25138
25139 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25140 false, context_die);
25141 gcc_assert (type_die);
25142 }
25143 return type_die;
25144 }
25145
25146 /* Force out any required namespaces to be able to output DECL,
25147 and return the new context_die for it, if it's changed. */
25148
25149 static dw_die_ref
25150 setup_namespace_context (tree thing, dw_die_ref context_die)
25151 {
25152 tree context = (DECL_P (thing)
25153 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25154 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25155 /* Force out the namespace. */
25156 context_die = force_decl_die (context);
25157
25158 return context_die;
25159 }
25160
25161 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25162 type) within its namespace, if appropriate.
25163
25164 For compatibility with older debuggers, namespace DIEs only contain
25165 declarations; all definitions are emitted at CU scope, with
25166 DW_AT_specification pointing to the declaration (like with class
25167 members). */
25168
25169 static dw_die_ref
25170 declare_in_namespace (tree thing, dw_die_ref context_die)
25171 {
25172 dw_die_ref ns_context;
25173
25174 if (debug_info_level <= DINFO_LEVEL_TERSE)
25175 return context_die;
25176
25177 /* External declarations in the local scope only need to be emitted
25178 once, not once in the namespace and once in the scope.
25179
25180 This avoids declaring the `extern' below in the
25181 namespace DIE as well as in the innermost scope:
25182
25183 namespace S
25184 {
25185 int i=5;
25186 int foo()
25187 {
25188 int i=8;
25189 extern int i;
25190 return i;
25191 }
25192 }
25193 */
25194 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25195 return context_die;
25196
25197 /* If this decl is from an inlined function, then don't try to emit it in its
25198 namespace, as we will get confused. It would have already been emitted
25199 when the abstract instance of the inline function was emitted anyways. */
25200 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25201 return context_die;
25202
25203 ns_context = setup_namespace_context (thing, context_die);
25204
25205 if (ns_context != context_die)
25206 {
25207 if (is_fortran ())
25208 return ns_context;
25209 if (DECL_P (thing))
25210 gen_decl_die (thing, NULL, NULL, ns_context);
25211 else
25212 gen_type_die (thing, ns_context);
25213 }
25214 return context_die;
25215 }
25216
25217 /* Generate a DIE for a namespace or namespace alias. */
25218
25219 static void
25220 gen_namespace_die (tree decl, dw_die_ref context_die)
25221 {
25222 dw_die_ref namespace_die;
25223
25224 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25225 they are an alias of. */
25226 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25227 {
25228 /* Output a real namespace or module. */
25229 context_die = setup_namespace_context (decl, comp_unit_die ());
25230 namespace_die = new_die (is_fortran ()
25231 ? DW_TAG_module : DW_TAG_namespace,
25232 context_die, decl);
25233 /* For Fortran modules defined in different CU don't add src coords. */
25234 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25235 {
25236 const char *name = dwarf2_name (decl, 0);
25237 if (name)
25238 add_name_attribute (namespace_die, name);
25239 }
25240 else
25241 add_name_and_src_coords_attributes (namespace_die, decl);
25242 if (DECL_EXTERNAL (decl))
25243 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25244 equate_decl_number_to_die (decl, namespace_die);
25245 }
25246 else
25247 {
25248 /* Output a namespace alias. */
25249
25250 /* Force out the namespace we are an alias of, if necessary. */
25251 dw_die_ref origin_die
25252 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25253
25254 if (DECL_FILE_SCOPE_P (decl)
25255 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25256 context_die = setup_namespace_context (decl, comp_unit_die ());
25257 /* Now create the namespace alias DIE. */
25258 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25259 add_name_and_src_coords_attributes (namespace_die, decl);
25260 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25261 equate_decl_number_to_die (decl, namespace_die);
25262 }
25263 if ((dwarf_version >= 5 || !dwarf_strict)
25264 && lang_hooks.decls.decl_dwarf_attribute (decl,
25265 DW_AT_export_symbols) == 1)
25266 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25267
25268 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25269 if (want_pubnames ())
25270 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25271 }
25272
25273 /* Generate Dwarf debug information for a decl described by DECL.
25274 The return value is currently only meaningful for PARM_DECLs,
25275 for all other decls it returns NULL.
25276
25277 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25278 It can be NULL otherwise. */
25279
25280 static dw_die_ref
25281 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25282 dw_die_ref context_die)
25283 {
25284 tree decl_or_origin = decl ? decl : origin;
25285 tree class_origin = NULL, ultimate_origin;
25286
25287 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25288 return NULL;
25289
25290 /* Ignore pointer bounds decls. */
25291 if (DECL_P (decl_or_origin)
25292 && TREE_TYPE (decl_or_origin)
25293 && POINTER_BOUNDS_P (decl_or_origin))
25294 return NULL;
25295
25296 switch (TREE_CODE (decl_or_origin))
25297 {
25298 case ERROR_MARK:
25299 break;
25300
25301 case CONST_DECL:
25302 if (!is_fortran () && !is_ada ())
25303 {
25304 /* The individual enumerators of an enum type get output when we output
25305 the Dwarf representation of the relevant enum type itself. */
25306 break;
25307 }
25308
25309 /* Emit its type. */
25310 gen_type_die (TREE_TYPE (decl), context_die);
25311
25312 /* And its containing namespace. */
25313 context_die = declare_in_namespace (decl, context_die);
25314
25315 gen_const_die (decl, context_die);
25316 break;
25317
25318 case FUNCTION_DECL:
25319 #if 0
25320 /* FIXME */
25321 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25322 on local redeclarations of global functions. That seems broken. */
25323 if (current_function_decl != decl)
25324 /* This is only a declaration. */;
25325 #endif
25326
25327 /* We should have abstract copies already and should not generate
25328 stray type DIEs in late LTO dumping. */
25329 if (! early_dwarf)
25330 ;
25331
25332 /* If we're emitting a clone, emit info for the abstract instance. */
25333 else if (origin || DECL_ORIGIN (decl) != decl)
25334 dwarf2out_abstract_function (origin
25335 ? DECL_ORIGIN (origin)
25336 : DECL_ABSTRACT_ORIGIN (decl));
25337
25338 /* If we're emitting a possibly inlined function emit it as
25339 abstract instance. */
25340 else if (cgraph_function_possibly_inlined_p (decl)
25341 && ! DECL_ABSTRACT_P (decl)
25342 && ! class_or_namespace_scope_p (context_die)
25343 /* dwarf2out_abstract_function won't emit a die if this is just
25344 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25345 that case, because that works only if we have a die. */
25346 && DECL_INITIAL (decl) != NULL_TREE)
25347 dwarf2out_abstract_function (decl);
25348
25349 /* Otherwise we're emitting the primary DIE for this decl. */
25350 else if (debug_info_level > DINFO_LEVEL_TERSE)
25351 {
25352 /* Before we describe the FUNCTION_DECL itself, make sure that we
25353 have its containing type. */
25354 if (!origin)
25355 origin = decl_class_context (decl);
25356 if (origin != NULL_TREE)
25357 gen_type_die (origin, context_die);
25358
25359 /* And its return type. */
25360 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25361
25362 /* And its virtual context. */
25363 if (DECL_VINDEX (decl) != NULL_TREE)
25364 gen_type_die (DECL_CONTEXT (decl), context_die);
25365
25366 /* Make sure we have a member DIE for decl. */
25367 if (origin != NULL_TREE)
25368 gen_type_die_for_member (origin, decl, context_die);
25369
25370 /* And its containing namespace. */
25371 context_die = declare_in_namespace (decl, context_die);
25372 }
25373
25374 /* Now output a DIE to represent the function itself. */
25375 if (decl)
25376 gen_subprogram_die (decl, context_die);
25377 break;
25378
25379 case TYPE_DECL:
25380 /* If we are in terse mode, don't generate any DIEs to represent any
25381 actual typedefs. */
25382 if (debug_info_level <= DINFO_LEVEL_TERSE)
25383 break;
25384
25385 /* In the special case of a TYPE_DECL node representing the declaration
25386 of some type tag, if the given TYPE_DECL is marked as having been
25387 instantiated from some other (original) TYPE_DECL node (e.g. one which
25388 was generated within the original definition of an inline function) we
25389 used to generate a special (abbreviated) DW_TAG_structure_type,
25390 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25391 should be actually referencing those DIEs, as variable DIEs with that
25392 type would be emitted already in the abstract origin, so it was always
25393 removed during unused type prunning. Don't add anything in this
25394 case. */
25395 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25396 break;
25397
25398 if (is_redundant_typedef (decl))
25399 gen_type_die (TREE_TYPE (decl), context_die);
25400 else
25401 /* Output a DIE to represent the typedef itself. */
25402 gen_typedef_die (decl, context_die);
25403 break;
25404
25405 case LABEL_DECL:
25406 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25407 gen_label_die (decl, context_die);
25408 break;
25409
25410 case VAR_DECL:
25411 case RESULT_DECL:
25412 /* If we are in terse mode, don't generate any DIEs to represent any
25413 variable declarations or definitions. */
25414 if (debug_info_level <= DINFO_LEVEL_TERSE)
25415 break;
25416
25417 /* Avoid generating stray type DIEs during late dwarf dumping.
25418 All types have been dumped early. */
25419 if (early_dwarf
25420 /* ??? But in LTRANS we cannot annotate early created variably
25421 modified type DIEs without copying them and adjusting all
25422 references to them. Dump them again as happens for inlining
25423 which copies both the decl and the types. */
25424 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25425 in VLA bound information for example. */
25426 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25427 current_function_decl)))
25428 {
25429 /* Output any DIEs that are needed to specify the type of this data
25430 object. */
25431 if (decl_by_reference_p (decl_or_origin))
25432 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25433 else
25434 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25435 }
25436
25437 if (early_dwarf)
25438 {
25439 /* And its containing type. */
25440 class_origin = decl_class_context (decl_or_origin);
25441 if (class_origin != NULL_TREE)
25442 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25443
25444 /* And its containing namespace. */
25445 context_die = declare_in_namespace (decl_or_origin, context_die);
25446 }
25447
25448 /* Now output the DIE to represent the data object itself. This gets
25449 complicated because of the possibility that the VAR_DECL really
25450 represents an inlined instance of a formal parameter for an inline
25451 function. */
25452 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25453 if (ultimate_origin != NULL_TREE
25454 && TREE_CODE (ultimate_origin) == PARM_DECL)
25455 gen_formal_parameter_die (decl, origin,
25456 true /* Emit name attribute. */,
25457 context_die);
25458 else
25459 gen_variable_die (decl, origin, context_die);
25460 break;
25461
25462 case FIELD_DECL:
25463 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25464 /* Ignore the nameless fields that are used to skip bits but handle C++
25465 anonymous unions and structs. */
25466 if (DECL_NAME (decl) != NULL_TREE
25467 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25468 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25469 {
25470 gen_type_die (member_declared_type (decl), context_die);
25471 gen_field_die (decl, ctx, context_die);
25472 }
25473 break;
25474
25475 case PARM_DECL:
25476 /* Avoid generating stray type DIEs during late dwarf dumping.
25477 All types have been dumped early. */
25478 if (early_dwarf
25479 /* ??? But in LTRANS we cannot annotate early created variably
25480 modified type DIEs without copying them and adjusting all
25481 references to them. Dump them again as happens for inlining
25482 which copies both the decl and the types. */
25483 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25484 in VLA bound information for example. */
25485 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25486 current_function_decl)))
25487 {
25488 if (DECL_BY_REFERENCE (decl_or_origin))
25489 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25490 else
25491 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25492 }
25493 return gen_formal_parameter_die (decl, origin,
25494 true /* Emit name attribute. */,
25495 context_die);
25496
25497 case NAMESPACE_DECL:
25498 if (dwarf_version >= 3 || !dwarf_strict)
25499 gen_namespace_die (decl, context_die);
25500 break;
25501
25502 case IMPORTED_DECL:
25503 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25504 DECL_CONTEXT (decl), context_die);
25505 break;
25506
25507 case NAMELIST_DECL:
25508 gen_namelist_decl (DECL_NAME (decl), context_die,
25509 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25510 break;
25511
25512 default:
25513 /* Probably some frontend-internal decl. Assume we don't care. */
25514 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25515 break;
25516 }
25517
25518 return NULL;
25519 }
25520 \f
25521 /* Output initial debug information for global DECL. Called at the
25522 end of the parsing process.
25523
25524 This is the initial debug generation process. As such, the DIEs
25525 generated may be incomplete. A later debug generation pass
25526 (dwarf2out_late_global_decl) will augment the information generated
25527 in this pass (e.g., with complete location info). */
25528
25529 static void
25530 dwarf2out_early_global_decl (tree decl)
25531 {
25532 set_early_dwarf s;
25533
25534 /* gen_decl_die() will set DECL_ABSTRACT because
25535 cgraph_function_possibly_inlined_p() returns true. This is in
25536 turn will cause DW_AT_inline attributes to be set.
25537
25538 This happens because at early dwarf generation, there is no
25539 cgraph information, causing cgraph_function_possibly_inlined_p()
25540 to return true. Trick cgraph_function_possibly_inlined_p()
25541 while we generate dwarf early. */
25542 bool save = symtab->global_info_ready;
25543 symtab->global_info_ready = true;
25544
25545 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25546 other DECLs and they can point to template types or other things
25547 that dwarf2out can't handle when done via dwarf2out_decl. */
25548 if (TREE_CODE (decl) != TYPE_DECL
25549 && TREE_CODE (decl) != PARM_DECL)
25550 {
25551 if (TREE_CODE (decl) == FUNCTION_DECL)
25552 {
25553 tree save_fndecl = current_function_decl;
25554
25555 /* For nested functions, make sure we have DIEs for the parents first
25556 so that all nested DIEs are generated at the proper scope in the
25557 first shot. */
25558 tree context = decl_function_context (decl);
25559 if (context != NULL)
25560 {
25561 dw_die_ref context_die = lookup_decl_die (context);
25562 current_function_decl = context;
25563
25564 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25565 enough so that it lands in its own context. This avoids type
25566 pruning issues later on. */
25567 if (context_die == NULL || is_declaration_die (context_die))
25568 dwarf2out_decl (context);
25569 }
25570
25571 /* Emit an abstract origin of a function first. This happens
25572 with C++ constructor clones for example and makes
25573 dwarf2out_abstract_function happy which requires the early
25574 DIE of the abstract instance to be present. */
25575 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25576 dw_die_ref origin_die;
25577 if (origin != NULL
25578 /* Do not emit the DIE multiple times but make sure to
25579 process it fully here in case we just saw a declaration. */
25580 && ((origin_die = lookup_decl_die (origin)) == NULL
25581 || is_declaration_die (origin_die)))
25582 {
25583 current_function_decl = origin;
25584 dwarf2out_decl (origin);
25585 }
25586
25587 /* Emit the DIE for decl but avoid doing that multiple times. */
25588 dw_die_ref old_die;
25589 if ((old_die = lookup_decl_die (decl)) == NULL
25590 || is_declaration_die (old_die))
25591 {
25592 current_function_decl = decl;
25593 dwarf2out_decl (decl);
25594 }
25595
25596 current_function_decl = save_fndecl;
25597 }
25598 else
25599 dwarf2out_decl (decl);
25600 }
25601 symtab->global_info_ready = save;
25602 }
25603
25604 /* Output debug information for global decl DECL. Called from
25605 toplev.c after compilation proper has finished. */
25606
25607 static void
25608 dwarf2out_late_global_decl (tree decl)
25609 {
25610 /* Fill-in any location information we were unable to determine
25611 on the first pass. */
25612 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25613 {
25614 dw_die_ref die = lookup_decl_die (decl);
25615
25616 /* We may have to generate early debug late for LTO in case debug
25617 was not enabled at compile-time or the target doesn't support
25618 the LTO early debug scheme. */
25619 if (! die && in_lto_p)
25620 {
25621 dwarf2out_decl (decl);
25622 die = lookup_decl_die (decl);
25623 }
25624
25625 if (die)
25626 {
25627 /* We get called via the symtab code invoking late_global_decl
25628 for symbols that are optimized out. Do not add locations
25629 for those, except if they have a DECL_VALUE_EXPR, in which case
25630 they are relevant for debuggers. */
25631 varpool_node *node = varpool_node::get (decl);
25632 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25633 tree_add_const_value_attribute_for_decl (die, decl);
25634 else
25635 add_location_or_const_value_attribute (die, decl, false);
25636 }
25637 }
25638 }
25639
25640 /* Output debug information for type decl DECL. Called from toplev.c
25641 and from language front ends (to record built-in types). */
25642 static void
25643 dwarf2out_type_decl (tree decl, int local)
25644 {
25645 if (!local)
25646 {
25647 set_early_dwarf s;
25648 dwarf2out_decl (decl);
25649 }
25650 }
25651
25652 /* Output debug information for imported module or decl DECL.
25653 NAME is non-NULL name in the lexical block if the decl has been renamed.
25654 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25655 that DECL belongs to.
25656 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25657 static void
25658 dwarf2out_imported_module_or_decl_1 (tree decl,
25659 tree name,
25660 tree lexical_block,
25661 dw_die_ref lexical_block_die)
25662 {
25663 expanded_location xloc;
25664 dw_die_ref imported_die = NULL;
25665 dw_die_ref at_import_die;
25666
25667 if (TREE_CODE (decl) == IMPORTED_DECL)
25668 {
25669 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25670 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25671 gcc_assert (decl);
25672 }
25673 else
25674 xloc = expand_location (input_location);
25675
25676 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25677 {
25678 at_import_die = force_type_die (TREE_TYPE (decl));
25679 /* For namespace N { typedef void T; } using N::T; base_type_die
25680 returns NULL, but DW_TAG_imported_declaration requires
25681 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25682 if (!at_import_die)
25683 {
25684 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25685 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25686 at_import_die = lookup_type_die (TREE_TYPE (decl));
25687 gcc_assert (at_import_die);
25688 }
25689 }
25690 else
25691 {
25692 at_import_die = lookup_decl_die (decl);
25693 if (!at_import_die)
25694 {
25695 /* If we're trying to avoid duplicate debug info, we may not have
25696 emitted the member decl for this field. Emit it now. */
25697 if (TREE_CODE (decl) == FIELD_DECL)
25698 {
25699 tree type = DECL_CONTEXT (decl);
25700
25701 if (TYPE_CONTEXT (type)
25702 && TYPE_P (TYPE_CONTEXT (type))
25703 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25704 DINFO_USAGE_DIR_USE))
25705 return;
25706 gen_type_die_for_member (type, decl,
25707 get_context_die (TYPE_CONTEXT (type)));
25708 }
25709 if (TREE_CODE (decl) == NAMELIST_DECL)
25710 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25711 get_context_die (DECL_CONTEXT (decl)),
25712 NULL_TREE);
25713 else
25714 at_import_die = force_decl_die (decl);
25715 }
25716 }
25717
25718 if (TREE_CODE (decl) == NAMESPACE_DECL)
25719 {
25720 if (dwarf_version >= 3 || !dwarf_strict)
25721 imported_die = new_die (DW_TAG_imported_module,
25722 lexical_block_die,
25723 lexical_block);
25724 else
25725 return;
25726 }
25727 else
25728 imported_die = new_die (DW_TAG_imported_declaration,
25729 lexical_block_die,
25730 lexical_block);
25731
25732 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25733 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25734 if (debug_column_info && xloc.column)
25735 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25736 if (name)
25737 add_AT_string (imported_die, DW_AT_name,
25738 IDENTIFIER_POINTER (name));
25739 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25740 }
25741
25742 /* Output debug information for imported module or decl DECL.
25743 NAME is non-NULL name in context if the decl has been renamed.
25744 CHILD is true if decl is one of the renamed decls as part of
25745 importing whole module.
25746 IMPLICIT is set if this hook is called for an implicit import
25747 such as inline namespace. */
25748
25749 static void
25750 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25751 bool child, bool implicit)
25752 {
25753 /* dw_die_ref at_import_die; */
25754 dw_die_ref scope_die;
25755
25756 if (debug_info_level <= DINFO_LEVEL_TERSE)
25757 return;
25758
25759 gcc_assert (decl);
25760
25761 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25762 should be enough, for DWARF4 and older even if we emit as extension
25763 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25764 for the benefit of consumers unaware of DW_AT_export_symbols. */
25765 if (implicit
25766 && dwarf_version >= 5
25767 && lang_hooks.decls.decl_dwarf_attribute (decl,
25768 DW_AT_export_symbols) == 1)
25769 return;
25770
25771 set_early_dwarf s;
25772
25773 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25774 We need decl DIE for reference and scope die. First, get DIE for the decl
25775 itself. */
25776
25777 /* Get the scope die for decl context. Use comp_unit_die for global module
25778 or decl. If die is not found for non globals, force new die. */
25779 if (context
25780 && TYPE_P (context)
25781 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25782 return;
25783
25784 scope_die = get_context_die (context);
25785
25786 if (child)
25787 {
25788 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25789 there is nothing we can do, here. */
25790 if (dwarf_version < 3 && dwarf_strict)
25791 return;
25792
25793 gcc_assert (scope_die->die_child);
25794 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25795 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25796 scope_die = scope_die->die_child;
25797 }
25798
25799 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25800 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25801 }
25802
25803 /* Output debug information for namelists. */
25804
25805 static dw_die_ref
25806 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25807 {
25808 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25809 tree value;
25810 unsigned i;
25811
25812 if (debug_info_level <= DINFO_LEVEL_TERSE)
25813 return NULL;
25814
25815 gcc_assert (scope_die != NULL);
25816 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25817 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25818
25819 /* If there are no item_decls, we have a nondefining namelist, e.g.
25820 with USE association; hence, set DW_AT_declaration. */
25821 if (item_decls == NULL_TREE)
25822 {
25823 add_AT_flag (nml_die, DW_AT_declaration, 1);
25824 return nml_die;
25825 }
25826
25827 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25828 {
25829 nml_item_ref_die = lookup_decl_die (value);
25830 if (!nml_item_ref_die)
25831 nml_item_ref_die = force_decl_die (value);
25832
25833 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25834 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25835 }
25836 return nml_die;
25837 }
25838
25839
25840 /* Write the debugging output for DECL and return the DIE. */
25841
25842 static void
25843 dwarf2out_decl (tree decl)
25844 {
25845 dw_die_ref context_die = comp_unit_die ();
25846
25847 switch (TREE_CODE (decl))
25848 {
25849 case ERROR_MARK:
25850 return;
25851
25852 case FUNCTION_DECL:
25853 /* If we're a nested function, initially use a parent of NULL; if we're
25854 a plain function, this will be fixed up in decls_for_scope. If
25855 we're a method, it will be ignored, since we already have a DIE. */
25856 if (decl_function_context (decl)
25857 /* But if we're in terse mode, we don't care about scope. */
25858 && debug_info_level > DINFO_LEVEL_TERSE)
25859 context_die = NULL;
25860 break;
25861
25862 case VAR_DECL:
25863 /* For local statics lookup proper context die. */
25864 if (local_function_static (decl))
25865 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25866
25867 /* If we are in terse mode, don't generate any DIEs to represent any
25868 variable declarations or definitions. */
25869 if (debug_info_level <= DINFO_LEVEL_TERSE)
25870 return;
25871 break;
25872
25873 case CONST_DECL:
25874 if (debug_info_level <= DINFO_LEVEL_TERSE)
25875 return;
25876 if (!is_fortran () && !is_ada ())
25877 return;
25878 if (TREE_STATIC (decl) && decl_function_context (decl))
25879 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25880 break;
25881
25882 case NAMESPACE_DECL:
25883 case IMPORTED_DECL:
25884 if (debug_info_level <= DINFO_LEVEL_TERSE)
25885 return;
25886 if (lookup_decl_die (decl) != NULL)
25887 return;
25888 break;
25889
25890 case TYPE_DECL:
25891 /* Don't emit stubs for types unless they are needed by other DIEs. */
25892 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25893 return;
25894
25895 /* Don't bother trying to generate any DIEs to represent any of the
25896 normal built-in types for the language we are compiling. */
25897 if (DECL_IS_BUILTIN (decl))
25898 return;
25899
25900 /* If we are in terse mode, don't generate any DIEs for types. */
25901 if (debug_info_level <= DINFO_LEVEL_TERSE)
25902 return;
25903
25904 /* If we're a function-scope tag, initially use a parent of NULL;
25905 this will be fixed up in decls_for_scope. */
25906 if (decl_function_context (decl))
25907 context_die = NULL;
25908
25909 break;
25910
25911 case NAMELIST_DECL:
25912 break;
25913
25914 default:
25915 return;
25916 }
25917
25918 gen_decl_die (decl, NULL, NULL, context_die);
25919
25920 if (flag_checking)
25921 {
25922 dw_die_ref die = lookup_decl_die (decl);
25923 if (die)
25924 check_die (die);
25925 }
25926 }
25927
25928 /* Write the debugging output for DECL. */
25929
25930 static void
25931 dwarf2out_function_decl (tree decl)
25932 {
25933 dwarf2out_decl (decl);
25934 call_arg_locations = NULL;
25935 call_arg_loc_last = NULL;
25936 call_site_count = -1;
25937 tail_call_site_count = -1;
25938 decl_loc_table->empty ();
25939 cached_dw_loc_list_table->empty ();
25940 }
25941
25942 /* Output a marker (i.e. a label) for the beginning of the generated code for
25943 a lexical block. */
25944
25945 static void
25946 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25947 unsigned int blocknum)
25948 {
25949 switch_to_section (current_function_section ());
25950 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25951 }
25952
25953 /* Output a marker (i.e. a label) for the end of the generated code for a
25954 lexical block. */
25955
25956 static void
25957 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25958 {
25959 switch_to_section (current_function_section ());
25960 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25961 }
25962
25963 /* Returns nonzero if it is appropriate not to emit any debugging
25964 information for BLOCK, because it doesn't contain any instructions.
25965
25966 Don't allow this for blocks with nested functions or local classes
25967 as we would end up with orphans, and in the presence of scheduling
25968 we may end up calling them anyway. */
25969
25970 static bool
25971 dwarf2out_ignore_block (const_tree block)
25972 {
25973 tree decl;
25974 unsigned int i;
25975
25976 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25977 if (TREE_CODE (decl) == FUNCTION_DECL
25978 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25979 return 0;
25980 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25981 {
25982 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25983 if (TREE_CODE (decl) == FUNCTION_DECL
25984 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25985 return 0;
25986 }
25987
25988 return 1;
25989 }
25990
25991 /* Hash table routines for file_hash. */
25992
25993 bool
25994 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25995 {
25996 return filename_cmp (p1->filename, p2) == 0;
25997 }
25998
25999 hashval_t
26000 dwarf_file_hasher::hash (dwarf_file_data *p)
26001 {
26002 return htab_hash_string (p->filename);
26003 }
26004
26005 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26006 dwarf2out.c) and return its "index". The index of each (known) filename is
26007 just a unique number which is associated with only that one filename. We
26008 need such numbers for the sake of generating labels (in the .debug_sfnames
26009 section) and references to those files numbers (in the .debug_srcinfo
26010 and .debug_macinfo sections). If the filename given as an argument is not
26011 found in our current list, add it to the list and assign it the next
26012 available unique index number. */
26013
26014 static struct dwarf_file_data *
26015 lookup_filename (const char *file_name)
26016 {
26017 struct dwarf_file_data * created;
26018
26019 if (!file_name)
26020 return NULL;
26021
26022 dwarf_file_data **slot
26023 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26024 INSERT);
26025 if (*slot)
26026 return *slot;
26027
26028 created = ggc_alloc<dwarf_file_data> ();
26029 created->filename = file_name;
26030 created->emitted_number = 0;
26031 *slot = created;
26032 return created;
26033 }
26034
26035 /* If the assembler will construct the file table, then translate the compiler
26036 internal file table number into the assembler file table number, and emit
26037 a .file directive if we haven't already emitted one yet. The file table
26038 numbers are different because we prune debug info for unused variables and
26039 types, which may include filenames. */
26040
26041 static int
26042 maybe_emit_file (struct dwarf_file_data * fd)
26043 {
26044 if (! fd->emitted_number)
26045 {
26046 if (last_emitted_file)
26047 fd->emitted_number = last_emitted_file->emitted_number + 1;
26048 else
26049 fd->emitted_number = 1;
26050 last_emitted_file = fd;
26051
26052 if (DWARF2_ASM_LINE_DEBUG_INFO)
26053 {
26054 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26055 output_quoted_string (asm_out_file,
26056 remap_debug_filename (fd->filename));
26057 fputc ('\n', asm_out_file);
26058 }
26059 }
26060
26061 return fd->emitted_number;
26062 }
26063
26064 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26065 That generation should happen after function debug info has been
26066 generated. The value of the attribute is the constant value of ARG. */
26067
26068 static void
26069 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26070 {
26071 die_arg_entry entry;
26072
26073 if (!die || !arg)
26074 return;
26075
26076 gcc_assert (early_dwarf);
26077
26078 if (!tmpl_value_parm_die_table)
26079 vec_alloc (tmpl_value_parm_die_table, 32);
26080
26081 entry.die = die;
26082 entry.arg = arg;
26083 vec_safe_push (tmpl_value_parm_die_table, entry);
26084 }
26085
26086 /* Return TRUE if T is an instance of generic type, FALSE
26087 otherwise. */
26088
26089 static bool
26090 generic_type_p (tree t)
26091 {
26092 if (t == NULL_TREE || !TYPE_P (t))
26093 return false;
26094 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26095 }
26096
26097 /* Schedule the generation of the generic parameter dies for the
26098 instance of generic type T. The proper generation itself is later
26099 done by gen_scheduled_generic_parms_dies. */
26100
26101 static void
26102 schedule_generic_params_dies_gen (tree t)
26103 {
26104 if (!generic_type_p (t))
26105 return;
26106
26107 gcc_assert (early_dwarf);
26108
26109 if (!generic_type_instances)
26110 vec_alloc (generic_type_instances, 256);
26111
26112 vec_safe_push (generic_type_instances, t);
26113 }
26114
26115 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26116 by append_entry_to_tmpl_value_parm_die_table. This function must
26117 be called after function DIEs have been generated. */
26118
26119 static void
26120 gen_remaining_tmpl_value_param_die_attribute (void)
26121 {
26122 if (tmpl_value_parm_die_table)
26123 {
26124 unsigned i, j;
26125 die_arg_entry *e;
26126
26127 /* We do this in two phases - first get the cases we can
26128 handle during early-finish, preserving those we cannot
26129 (containing symbolic constants where we don't yet know
26130 whether we are going to output the referenced symbols).
26131 For those we try again at late-finish. */
26132 j = 0;
26133 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26134 {
26135 if (!e->die->removed
26136 && !tree_add_const_value_attribute (e->die, e->arg))
26137 {
26138 dw_loc_descr_ref loc = NULL;
26139 if (! early_dwarf
26140 && (dwarf_version >= 5 || !dwarf_strict))
26141 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26142 if (loc)
26143 add_AT_loc (e->die, DW_AT_location, loc);
26144 else
26145 (*tmpl_value_parm_die_table)[j++] = *e;
26146 }
26147 }
26148 tmpl_value_parm_die_table->truncate (j);
26149 }
26150 }
26151
26152 /* Generate generic parameters DIEs for instances of generic types
26153 that have been previously scheduled by
26154 schedule_generic_params_dies_gen. This function must be called
26155 after all the types of the CU have been laid out. */
26156
26157 static void
26158 gen_scheduled_generic_parms_dies (void)
26159 {
26160 unsigned i;
26161 tree t;
26162
26163 if (!generic_type_instances)
26164 return;
26165
26166 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26167 if (COMPLETE_TYPE_P (t))
26168 gen_generic_params_dies (t);
26169
26170 generic_type_instances = NULL;
26171 }
26172
26173
26174 /* Replace DW_AT_name for the decl with name. */
26175
26176 static void
26177 dwarf2out_set_name (tree decl, tree name)
26178 {
26179 dw_die_ref die;
26180 dw_attr_node *attr;
26181 const char *dname;
26182
26183 die = TYPE_SYMTAB_DIE (decl);
26184 if (!die)
26185 return;
26186
26187 dname = dwarf2_name (name, 0);
26188 if (!dname)
26189 return;
26190
26191 attr = get_AT (die, DW_AT_name);
26192 if (attr)
26193 {
26194 struct indirect_string_node *node;
26195
26196 node = find_AT_string (dname);
26197 /* replace the string. */
26198 attr->dw_attr_val.v.val_str = node;
26199 }
26200
26201 else
26202 add_name_attribute (die, dname);
26203 }
26204
26205 /* True if before or during processing of the first function being emitted. */
26206 static bool in_first_function_p = true;
26207 /* True if loc_note during dwarf2out_var_location call might still be
26208 before first real instruction at address equal to .Ltext0. */
26209 static bool maybe_at_text_label_p = true;
26210 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26211 static unsigned int first_loclabel_num_not_at_text_label;
26212
26213 /* Called by the final INSN scan whenever we see a var location. We
26214 use it to drop labels in the right places, and throw the location in
26215 our lookup table. */
26216
26217 static void
26218 dwarf2out_var_location (rtx_insn *loc_note)
26219 {
26220 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26221 struct var_loc_node *newloc;
26222 rtx_insn *next_real, *next_note;
26223 rtx_insn *call_insn = NULL;
26224 static const char *last_label;
26225 static const char *last_postcall_label;
26226 static bool last_in_cold_section_p;
26227 static rtx_insn *expected_next_loc_note;
26228 tree decl;
26229 bool var_loc_p;
26230
26231 if (!NOTE_P (loc_note))
26232 {
26233 if (CALL_P (loc_note))
26234 {
26235 call_site_count++;
26236 if (SIBLING_CALL_P (loc_note))
26237 tail_call_site_count++;
26238 if (optimize == 0 && !flag_var_tracking)
26239 {
26240 /* When the var-tracking pass is not running, there is no note
26241 for indirect calls whose target is compile-time known. In this
26242 case, process such calls specifically so that we generate call
26243 sites for them anyway. */
26244 rtx x = PATTERN (loc_note);
26245 if (GET_CODE (x) == PARALLEL)
26246 x = XVECEXP (x, 0, 0);
26247 if (GET_CODE (x) == SET)
26248 x = SET_SRC (x);
26249 if (GET_CODE (x) == CALL)
26250 x = XEXP (x, 0);
26251 if (!MEM_P (x)
26252 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26253 || !SYMBOL_REF_DECL (XEXP (x, 0))
26254 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26255 != FUNCTION_DECL))
26256 {
26257 call_insn = loc_note;
26258 loc_note = NULL;
26259 var_loc_p = false;
26260
26261 next_real = next_real_insn (call_insn);
26262 next_note = NULL;
26263 cached_next_real_insn = NULL;
26264 goto create_label;
26265 }
26266 }
26267 }
26268 return;
26269 }
26270
26271 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26272 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26273 return;
26274
26275 /* Optimize processing a large consecutive sequence of location
26276 notes so we don't spend too much time in next_real_insn. If the
26277 next insn is another location note, remember the next_real_insn
26278 calculation for next time. */
26279 next_real = cached_next_real_insn;
26280 if (next_real)
26281 {
26282 if (expected_next_loc_note != loc_note)
26283 next_real = NULL;
26284 }
26285
26286 next_note = NEXT_INSN (loc_note);
26287 if (! next_note
26288 || next_note->deleted ()
26289 || ! NOTE_P (next_note)
26290 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26291 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26292 next_note = NULL;
26293
26294 if (! next_real)
26295 next_real = next_real_insn (loc_note);
26296
26297 if (next_note)
26298 {
26299 expected_next_loc_note = next_note;
26300 cached_next_real_insn = next_real;
26301 }
26302 else
26303 cached_next_real_insn = NULL;
26304
26305 /* If there are no instructions which would be affected by this note,
26306 don't do anything. */
26307 if (var_loc_p
26308 && next_real == NULL_RTX
26309 && !NOTE_DURING_CALL_P (loc_note))
26310 return;
26311
26312 create_label:
26313
26314 if (next_real == NULL_RTX)
26315 next_real = get_last_insn ();
26316
26317 /* If there were any real insns between note we processed last time
26318 and this note (or if it is the first note), clear
26319 last_{,postcall_}label so that they are not reused this time. */
26320 if (last_var_location_insn == NULL_RTX
26321 || last_var_location_insn != next_real
26322 || last_in_cold_section_p != in_cold_section_p)
26323 {
26324 last_label = NULL;
26325 last_postcall_label = NULL;
26326 }
26327
26328 if (var_loc_p)
26329 {
26330 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26331 newloc = add_var_loc_to_decl (decl, loc_note,
26332 NOTE_DURING_CALL_P (loc_note)
26333 ? last_postcall_label : last_label);
26334 if (newloc == NULL)
26335 return;
26336 }
26337 else
26338 {
26339 decl = NULL_TREE;
26340 newloc = NULL;
26341 }
26342
26343 /* If there were no real insns between note we processed last time
26344 and this note, use the label we emitted last time. Otherwise
26345 create a new label and emit it. */
26346 if (last_label == NULL)
26347 {
26348 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26349 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26350 loclabel_num++;
26351 last_label = ggc_strdup (loclabel);
26352 /* See if loclabel might be equal to .Ltext0. If yes,
26353 bump first_loclabel_num_not_at_text_label. */
26354 if (!have_multiple_function_sections
26355 && in_first_function_p
26356 && maybe_at_text_label_p)
26357 {
26358 static rtx_insn *last_start;
26359 rtx_insn *insn;
26360 for (insn = loc_note; insn; insn = previous_insn (insn))
26361 if (insn == last_start)
26362 break;
26363 else if (!NONDEBUG_INSN_P (insn))
26364 continue;
26365 else
26366 {
26367 rtx body = PATTERN (insn);
26368 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26369 continue;
26370 /* Inline asm could occupy zero bytes. */
26371 else if (GET_CODE (body) == ASM_INPUT
26372 || asm_noperands (body) >= 0)
26373 continue;
26374 #ifdef HAVE_attr_length
26375 else if (get_attr_min_length (insn) == 0)
26376 continue;
26377 #endif
26378 else
26379 {
26380 /* Assume insn has non-zero length. */
26381 maybe_at_text_label_p = false;
26382 break;
26383 }
26384 }
26385 if (maybe_at_text_label_p)
26386 {
26387 last_start = loc_note;
26388 first_loclabel_num_not_at_text_label = loclabel_num;
26389 }
26390 }
26391 }
26392
26393 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26394 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26395
26396 if (!var_loc_p)
26397 {
26398 struct call_arg_loc_node *ca_loc
26399 = ggc_cleared_alloc<call_arg_loc_node> ();
26400 rtx_insn *prev
26401 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26402
26403 ca_loc->call_arg_loc_note = loc_note;
26404 ca_loc->next = NULL;
26405 ca_loc->label = last_label;
26406 gcc_assert (prev
26407 && (CALL_P (prev)
26408 || (NONJUMP_INSN_P (prev)
26409 && GET_CODE (PATTERN (prev)) == SEQUENCE
26410 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26411 if (!CALL_P (prev))
26412 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26413 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26414
26415 /* Look for a SYMBOL_REF in the "prev" instruction. */
26416 rtx x = get_call_rtx_from (PATTERN (prev));
26417 if (x)
26418 {
26419 /* Try to get the call symbol, if any. */
26420 if (MEM_P (XEXP (x, 0)))
26421 x = XEXP (x, 0);
26422 /* First, look for a memory access to a symbol_ref. */
26423 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26424 && SYMBOL_REF_DECL (XEXP (x, 0))
26425 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26426 ca_loc->symbol_ref = XEXP (x, 0);
26427 /* Otherwise, look at a compile-time known user-level function
26428 declaration. */
26429 else if (MEM_P (x)
26430 && MEM_EXPR (x)
26431 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26432 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26433 }
26434
26435 ca_loc->block = insn_scope (prev);
26436 if (call_arg_locations)
26437 call_arg_loc_last->next = ca_loc;
26438 else
26439 call_arg_locations = ca_loc;
26440 call_arg_loc_last = ca_loc;
26441 }
26442 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26443 newloc->label = last_label;
26444 else
26445 {
26446 if (!last_postcall_label)
26447 {
26448 sprintf (loclabel, "%s-1", last_label);
26449 last_postcall_label = ggc_strdup (loclabel);
26450 }
26451 newloc->label = last_postcall_label;
26452 }
26453
26454 last_var_location_insn = next_real;
26455 last_in_cold_section_p = in_cold_section_p;
26456 }
26457
26458 /* Called from finalize_size_functions for size functions so that their body
26459 can be encoded in the debug info to describe the layout of variable-length
26460 structures. */
26461
26462 static void
26463 dwarf2out_size_function (tree decl)
26464 {
26465 function_to_dwarf_procedure (decl);
26466 }
26467
26468 /* Note in one location list that text section has changed. */
26469
26470 int
26471 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26472 {
26473 var_loc_list *list = *slot;
26474 if (list->first)
26475 list->last_before_switch
26476 = list->last->next ? list->last->next : list->last;
26477 return 1;
26478 }
26479
26480 /* Note in all location lists that text section has changed. */
26481
26482 static void
26483 var_location_switch_text_section (void)
26484 {
26485 if (decl_loc_table == NULL)
26486 return;
26487
26488 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26489 }
26490
26491 /* Create a new line number table. */
26492
26493 static dw_line_info_table *
26494 new_line_info_table (void)
26495 {
26496 dw_line_info_table *table;
26497
26498 table = ggc_cleared_alloc<dw_line_info_table> ();
26499 table->file_num = 1;
26500 table->line_num = 1;
26501 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26502
26503 return table;
26504 }
26505
26506 /* Lookup the "current" table into which we emit line info, so
26507 that we don't have to do it for every source line. */
26508
26509 static void
26510 set_cur_line_info_table (section *sec)
26511 {
26512 dw_line_info_table *table;
26513
26514 if (sec == text_section)
26515 table = text_section_line_info;
26516 else if (sec == cold_text_section)
26517 {
26518 table = cold_text_section_line_info;
26519 if (!table)
26520 {
26521 cold_text_section_line_info = table = new_line_info_table ();
26522 table->end_label = cold_end_label;
26523 }
26524 }
26525 else
26526 {
26527 const char *end_label;
26528
26529 if (crtl->has_bb_partition)
26530 {
26531 if (in_cold_section_p)
26532 end_label = crtl->subsections.cold_section_end_label;
26533 else
26534 end_label = crtl->subsections.hot_section_end_label;
26535 }
26536 else
26537 {
26538 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26539 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26540 current_function_funcdef_no);
26541 end_label = ggc_strdup (label);
26542 }
26543
26544 table = new_line_info_table ();
26545 table->end_label = end_label;
26546
26547 vec_safe_push (separate_line_info, table);
26548 }
26549
26550 if (DWARF2_ASM_LINE_DEBUG_INFO)
26551 table->is_stmt = (cur_line_info_table
26552 ? cur_line_info_table->is_stmt
26553 : DWARF_LINE_DEFAULT_IS_STMT_START);
26554 cur_line_info_table = table;
26555 }
26556
26557
26558 /* We need to reset the locations at the beginning of each
26559 function. We can't do this in the end_function hook, because the
26560 declarations that use the locations won't have been output when
26561 that hook is called. Also compute have_multiple_function_sections here. */
26562
26563 static void
26564 dwarf2out_begin_function (tree fun)
26565 {
26566 section *sec = function_section (fun);
26567
26568 if (sec != text_section)
26569 have_multiple_function_sections = true;
26570
26571 if (crtl->has_bb_partition && !cold_text_section)
26572 {
26573 gcc_assert (current_function_decl == fun);
26574 cold_text_section = unlikely_text_section ();
26575 switch_to_section (cold_text_section);
26576 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26577 switch_to_section (sec);
26578 }
26579
26580 dwarf2out_note_section_used ();
26581 call_site_count = 0;
26582 tail_call_site_count = 0;
26583
26584 set_cur_line_info_table (sec);
26585 }
26586
26587 /* Helper function of dwarf2out_end_function, called only after emitting
26588 the very first function into assembly. Check if some .debug_loc range
26589 might end with a .LVL* label that could be equal to .Ltext0.
26590 In that case we must force using absolute addresses in .debug_loc ranges,
26591 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26592 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26593 list terminator.
26594 Set have_multiple_function_sections to true in that case and
26595 terminate htab traversal. */
26596
26597 int
26598 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26599 {
26600 var_loc_list *entry = *slot;
26601 struct var_loc_node *node;
26602
26603 node = entry->first;
26604 if (node && node->next && node->next->label)
26605 {
26606 unsigned int i;
26607 const char *label = node->next->label;
26608 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26609
26610 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26611 {
26612 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26613 if (strcmp (label, loclabel) == 0)
26614 {
26615 have_multiple_function_sections = true;
26616 return 0;
26617 }
26618 }
26619 }
26620 return 1;
26621 }
26622
26623 /* Hook called after emitting a function into assembly.
26624 This does something only for the very first function emitted. */
26625
26626 static void
26627 dwarf2out_end_function (unsigned int)
26628 {
26629 if (in_first_function_p
26630 && !have_multiple_function_sections
26631 && first_loclabel_num_not_at_text_label
26632 && decl_loc_table)
26633 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26634 in_first_function_p = false;
26635 maybe_at_text_label_p = false;
26636 }
26637
26638 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26639 front-ends register a translation unit even before dwarf2out_init is
26640 called. */
26641 static tree main_translation_unit = NULL_TREE;
26642
26643 /* Hook called by front-ends after they built their main translation unit.
26644 Associate comp_unit_die to UNIT. */
26645
26646 static void
26647 dwarf2out_register_main_translation_unit (tree unit)
26648 {
26649 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26650 && main_translation_unit == NULL_TREE);
26651 main_translation_unit = unit;
26652 /* If dwarf2out_init has not been called yet, it will perform the association
26653 itself looking at main_translation_unit. */
26654 if (decl_die_table != NULL)
26655 equate_decl_number_to_die (unit, comp_unit_die ());
26656 }
26657
26658 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26659
26660 static void
26661 push_dw_line_info_entry (dw_line_info_table *table,
26662 enum dw_line_info_opcode opcode, unsigned int val)
26663 {
26664 dw_line_info_entry e;
26665 e.opcode = opcode;
26666 e.val = val;
26667 vec_safe_push (table->entries, e);
26668 }
26669
26670 /* Output a label to mark the beginning of a source code line entry
26671 and record information relating to this source line, in
26672 'line_info_table' for later output of the .debug_line section. */
26673 /* ??? The discriminator parameter ought to be unsigned. */
26674
26675 static void
26676 dwarf2out_source_line (unsigned int line, unsigned int column,
26677 const char *filename,
26678 int discriminator, bool is_stmt)
26679 {
26680 unsigned int file_num;
26681 dw_line_info_table *table;
26682
26683 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26684 return;
26685
26686 /* The discriminator column was added in dwarf4. Simplify the below
26687 by simply removing it if we're not supposed to output it. */
26688 if (dwarf_version < 4 && dwarf_strict)
26689 discriminator = 0;
26690
26691 if (!debug_column_info)
26692 column = 0;
26693
26694 table = cur_line_info_table;
26695 file_num = maybe_emit_file (lookup_filename (filename));
26696
26697 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26698 the debugger has used the second (possibly duplicate) line number
26699 at the beginning of the function to mark the end of the prologue.
26700 We could eliminate any other duplicates within the function. For
26701 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26702 that second line number entry. */
26703 /* Recall that this end-of-prologue indication is *not* the same thing
26704 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26705 to which the hook corresponds, follows the last insn that was
26706 emitted by gen_prologue. What we need is to precede the first insn
26707 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26708 insn that corresponds to something the user wrote. These may be
26709 very different locations once scheduling is enabled. */
26710
26711 if (0 && file_num == table->file_num
26712 && line == table->line_num
26713 && column == table->column_num
26714 && discriminator == table->discrim_num
26715 && is_stmt == table->is_stmt)
26716 return;
26717
26718 switch_to_section (current_function_section ());
26719
26720 /* If requested, emit something human-readable. */
26721 if (flag_debug_asm)
26722 {
26723 if (debug_column_info)
26724 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26725 filename, line, column);
26726 else
26727 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26728 filename, line);
26729 }
26730
26731 if (DWARF2_ASM_LINE_DEBUG_INFO)
26732 {
26733 /* Emit the .loc directive understood by GNU as. */
26734 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26735 file_num, line, is_stmt, discriminator */
26736 fputs ("\t.loc ", asm_out_file);
26737 fprint_ul (asm_out_file, file_num);
26738 putc (' ', asm_out_file);
26739 fprint_ul (asm_out_file, line);
26740 putc (' ', asm_out_file);
26741 fprint_ul (asm_out_file, column);
26742
26743 if (is_stmt != table->is_stmt)
26744 {
26745 fputs (" is_stmt ", asm_out_file);
26746 putc (is_stmt ? '1' : '0', asm_out_file);
26747 }
26748 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26749 {
26750 gcc_assert (discriminator > 0);
26751 fputs (" discriminator ", asm_out_file);
26752 fprint_ul (asm_out_file, (unsigned long) discriminator);
26753 }
26754 putc ('\n', asm_out_file);
26755 }
26756 else
26757 {
26758 unsigned int label_num = ++line_info_label_num;
26759
26760 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26761
26762 push_dw_line_info_entry (table, LI_set_address, label_num);
26763 if (file_num != table->file_num)
26764 push_dw_line_info_entry (table, LI_set_file, file_num);
26765 if (discriminator != table->discrim_num)
26766 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26767 if (is_stmt != table->is_stmt)
26768 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26769 push_dw_line_info_entry (table, LI_set_line, line);
26770 if (debug_column_info)
26771 push_dw_line_info_entry (table, LI_set_column, column);
26772 }
26773
26774 table->file_num = file_num;
26775 table->line_num = line;
26776 table->column_num = column;
26777 table->discrim_num = discriminator;
26778 table->is_stmt = is_stmt;
26779 table->in_use = true;
26780 }
26781
26782 /* Record the beginning of a new source file. */
26783
26784 static void
26785 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26786 {
26787 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26788 {
26789 macinfo_entry e;
26790 e.code = DW_MACINFO_start_file;
26791 e.lineno = lineno;
26792 e.info = ggc_strdup (filename);
26793 vec_safe_push (macinfo_table, e);
26794 }
26795 }
26796
26797 /* Record the end of a source file. */
26798
26799 static void
26800 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26801 {
26802 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26803 {
26804 macinfo_entry e;
26805 e.code = DW_MACINFO_end_file;
26806 e.lineno = lineno;
26807 e.info = NULL;
26808 vec_safe_push (macinfo_table, e);
26809 }
26810 }
26811
26812 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26813 the tail part of the directive line, i.e. the part which is past the
26814 initial whitespace, #, whitespace, directive-name, whitespace part. */
26815
26816 static void
26817 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26818 const char *buffer ATTRIBUTE_UNUSED)
26819 {
26820 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26821 {
26822 macinfo_entry e;
26823 /* Insert a dummy first entry to be able to optimize the whole
26824 predefined macro block using DW_MACRO_import. */
26825 if (macinfo_table->is_empty () && lineno <= 1)
26826 {
26827 e.code = 0;
26828 e.lineno = 0;
26829 e.info = NULL;
26830 vec_safe_push (macinfo_table, e);
26831 }
26832 e.code = DW_MACINFO_define;
26833 e.lineno = lineno;
26834 e.info = ggc_strdup (buffer);
26835 vec_safe_push (macinfo_table, e);
26836 }
26837 }
26838
26839 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26840 the tail part of the directive line, i.e. the part which is past the
26841 initial whitespace, #, whitespace, directive-name, whitespace part. */
26842
26843 static void
26844 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26845 const char *buffer ATTRIBUTE_UNUSED)
26846 {
26847 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26848 {
26849 macinfo_entry e;
26850 /* Insert a dummy first entry to be able to optimize the whole
26851 predefined macro block using DW_MACRO_import. */
26852 if (macinfo_table->is_empty () && lineno <= 1)
26853 {
26854 e.code = 0;
26855 e.lineno = 0;
26856 e.info = NULL;
26857 vec_safe_push (macinfo_table, e);
26858 }
26859 e.code = DW_MACINFO_undef;
26860 e.lineno = lineno;
26861 e.info = ggc_strdup (buffer);
26862 vec_safe_push (macinfo_table, e);
26863 }
26864 }
26865
26866 /* Helpers to manipulate hash table of CUs. */
26867
26868 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26869 {
26870 static inline hashval_t hash (const macinfo_entry *);
26871 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26872 };
26873
26874 inline hashval_t
26875 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26876 {
26877 return htab_hash_string (entry->info);
26878 }
26879
26880 inline bool
26881 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26882 const macinfo_entry *entry2)
26883 {
26884 return !strcmp (entry1->info, entry2->info);
26885 }
26886
26887 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26888
26889 /* Output a single .debug_macinfo entry. */
26890
26891 static void
26892 output_macinfo_op (macinfo_entry *ref)
26893 {
26894 int file_num;
26895 size_t len;
26896 struct indirect_string_node *node;
26897 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26898 struct dwarf_file_data *fd;
26899
26900 switch (ref->code)
26901 {
26902 case DW_MACINFO_start_file:
26903 fd = lookup_filename (ref->info);
26904 file_num = maybe_emit_file (fd);
26905 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26906 dw2_asm_output_data_uleb128 (ref->lineno,
26907 "Included from line number %lu",
26908 (unsigned long) ref->lineno);
26909 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26910 break;
26911 case DW_MACINFO_end_file:
26912 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26913 break;
26914 case DW_MACINFO_define:
26915 case DW_MACINFO_undef:
26916 len = strlen (ref->info) + 1;
26917 if (!dwarf_strict
26918 && len > DWARF_OFFSET_SIZE
26919 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26920 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26921 {
26922 ref->code = ref->code == DW_MACINFO_define
26923 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26924 output_macinfo_op (ref);
26925 return;
26926 }
26927 dw2_asm_output_data (1, ref->code,
26928 ref->code == DW_MACINFO_define
26929 ? "Define macro" : "Undefine macro");
26930 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26931 (unsigned long) ref->lineno);
26932 dw2_asm_output_nstring (ref->info, -1, "The macro");
26933 break;
26934 case DW_MACRO_define_strp:
26935 case DW_MACRO_undef_strp:
26936 node = find_AT_string (ref->info);
26937 gcc_assert (node
26938 && (node->form == DW_FORM_strp
26939 || node->form == DW_FORM_GNU_str_index));
26940 dw2_asm_output_data (1, ref->code,
26941 ref->code == DW_MACRO_define_strp
26942 ? "Define macro strp"
26943 : "Undefine macro strp");
26944 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26945 (unsigned long) ref->lineno);
26946 if (node->form == DW_FORM_strp)
26947 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26948 debug_str_section, "The macro: \"%s\"",
26949 ref->info);
26950 else
26951 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26952 ref->info);
26953 break;
26954 case DW_MACRO_import:
26955 dw2_asm_output_data (1, ref->code, "Import");
26956 ASM_GENERATE_INTERNAL_LABEL (label,
26957 DEBUG_MACRO_SECTION_LABEL,
26958 ref->lineno + macinfo_label_base);
26959 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26960 break;
26961 default:
26962 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26963 ASM_COMMENT_START, (unsigned long) ref->code);
26964 break;
26965 }
26966 }
26967
26968 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26969 other compilation unit .debug_macinfo sections. IDX is the first
26970 index of a define/undef, return the number of ops that should be
26971 emitted in a comdat .debug_macinfo section and emit
26972 a DW_MACRO_import entry referencing it.
26973 If the define/undef entry should be emitted normally, return 0. */
26974
26975 static unsigned
26976 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26977 macinfo_hash_type **macinfo_htab)
26978 {
26979 macinfo_entry *first, *second, *cur, *inc;
26980 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26981 unsigned char checksum[16];
26982 struct md5_ctx ctx;
26983 char *grp_name, *tail;
26984 const char *base;
26985 unsigned int i, count, encoded_filename_len, linebuf_len;
26986 macinfo_entry **slot;
26987
26988 first = &(*macinfo_table)[idx];
26989 second = &(*macinfo_table)[idx + 1];
26990
26991 /* Optimize only if there are at least two consecutive define/undef ops,
26992 and either all of them are before first DW_MACINFO_start_file
26993 with lineno {0,1} (i.e. predefined macro block), or all of them are
26994 in some included header file. */
26995 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26996 return 0;
26997 if (vec_safe_is_empty (files))
26998 {
26999 if (first->lineno > 1 || second->lineno > 1)
27000 return 0;
27001 }
27002 else if (first->lineno == 0)
27003 return 0;
27004
27005 /* Find the last define/undef entry that can be grouped together
27006 with first and at the same time compute md5 checksum of their
27007 codes, linenumbers and strings. */
27008 md5_init_ctx (&ctx);
27009 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27010 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27011 break;
27012 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27013 break;
27014 else
27015 {
27016 unsigned char code = cur->code;
27017 md5_process_bytes (&code, 1, &ctx);
27018 checksum_uleb128 (cur->lineno, &ctx);
27019 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27020 }
27021 md5_finish_ctx (&ctx, checksum);
27022 count = i - idx;
27023
27024 /* From the containing include filename (if any) pick up just
27025 usable characters from its basename. */
27026 if (vec_safe_is_empty (files))
27027 base = "";
27028 else
27029 base = lbasename (files->last ().info);
27030 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27031 if (ISIDNUM (base[i]) || base[i] == '.')
27032 encoded_filename_len++;
27033 /* Count . at the end. */
27034 if (encoded_filename_len)
27035 encoded_filename_len++;
27036
27037 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27038 linebuf_len = strlen (linebuf);
27039
27040 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27041 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27042 + 16 * 2 + 1);
27043 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27044 tail = grp_name + 4;
27045 if (encoded_filename_len)
27046 {
27047 for (i = 0; base[i]; i++)
27048 if (ISIDNUM (base[i]) || base[i] == '.')
27049 *tail++ = base[i];
27050 *tail++ = '.';
27051 }
27052 memcpy (tail, linebuf, linebuf_len);
27053 tail += linebuf_len;
27054 *tail++ = '.';
27055 for (i = 0; i < 16; i++)
27056 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27057
27058 /* Construct a macinfo_entry for DW_MACRO_import
27059 in the empty vector entry before the first define/undef. */
27060 inc = &(*macinfo_table)[idx - 1];
27061 inc->code = DW_MACRO_import;
27062 inc->lineno = 0;
27063 inc->info = ggc_strdup (grp_name);
27064 if (!*macinfo_htab)
27065 *macinfo_htab = new macinfo_hash_type (10);
27066 /* Avoid emitting duplicates. */
27067 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27068 if (*slot != NULL)
27069 {
27070 inc->code = 0;
27071 inc->info = NULL;
27072 /* If such an entry has been used before, just emit
27073 a DW_MACRO_import op. */
27074 inc = *slot;
27075 output_macinfo_op (inc);
27076 /* And clear all macinfo_entry in the range to avoid emitting them
27077 in the second pass. */
27078 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27079 {
27080 cur->code = 0;
27081 cur->info = NULL;
27082 }
27083 }
27084 else
27085 {
27086 *slot = inc;
27087 inc->lineno = (*macinfo_htab)->elements ();
27088 output_macinfo_op (inc);
27089 }
27090 return count;
27091 }
27092
27093 /* Save any strings needed by the macinfo table in the debug str
27094 table. All strings must be collected into the table by the time
27095 index_string is called. */
27096
27097 static void
27098 save_macinfo_strings (void)
27099 {
27100 unsigned len;
27101 unsigned i;
27102 macinfo_entry *ref;
27103
27104 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27105 {
27106 switch (ref->code)
27107 {
27108 /* Match the logic in output_macinfo_op to decide on
27109 indirect strings. */
27110 case DW_MACINFO_define:
27111 case DW_MACINFO_undef:
27112 len = strlen (ref->info) + 1;
27113 if (!dwarf_strict
27114 && len > DWARF_OFFSET_SIZE
27115 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27116 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27117 set_indirect_string (find_AT_string (ref->info));
27118 break;
27119 case DW_MACRO_define_strp:
27120 case DW_MACRO_undef_strp:
27121 set_indirect_string (find_AT_string (ref->info));
27122 break;
27123 default:
27124 break;
27125 }
27126 }
27127 }
27128
27129 /* Output macinfo section(s). */
27130
27131 static void
27132 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27133 {
27134 unsigned i;
27135 unsigned long length = vec_safe_length (macinfo_table);
27136 macinfo_entry *ref;
27137 vec<macinfo_entry, va_gc> *files = NULL;
27138 macinfo_hash_type *macinfo_htab = NULL;
27139 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27140
27141 if (! length)
27142 return;
27143
27144 /* output_macinfo* uses these interchangeably. */
27145 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27146 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27147 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27148 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27149
27150 /* AIX Assembler inserts the length, so adjust the reference to match the
27151 offset expected by debuggers. */
27152 strcpy (dl_section_ref, debug_line_label);
27153 if (XCOFF_DEBUGGING_INFO)
27154 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27155
27156 /* For .debug_macro emit the section header. */
27157 if (!dwarf_strict || dwarf_version >= 5)
27158 {
27159 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27160 "DWARF macro version number");
27161 if (DWARF_OFFSET_SIZE == 8)
27162 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27163 else
27164 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27165 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27166 debug_line_section, NULL);
27167 }
27168
27169 /* In the first loop, it emits the primary .debug_macinfo section
27170 and after each emitted op the macinfo_entry is cleared.
27171 If a longer range of define/undef ops can be optimized using
27172 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27173 the vector before the first define/undef in the range and the
27174 whole range of define/undef ops is not emitted and kept. */
27175 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27176 {
27177 switch (ref->code)
27178 {
27179 case DW_MACINFO_start_file:
27180 vec_safe_push (files, *ref);
27181 break;
27182 case DW_MACINFO_end_file:
27183 if (!vec_safe_is_empty (files))
27184 files->pop ();
27185 break;
27186 case DW_MACINFO_define:
27187 case DW_MACINFO_undef:
27188 if ((!dwarf_strict || dwarf_version >= 5)
27189 && HAVE_COMDAT_GROUP
27190 && vec_safe_length (files) != 1
27191 && i > 0
27192 && i + 1 < length
27193 && (*macinfo_table)[i - 1].code == 0)
27194 {
27195 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27196 if (count)
27197 {
27198 i += count - 1;
27199 continue;
27200 }
27201 }
27202 break;
27203 case 0:
27204 /* A dummy entry may be inserted at the beginning to be able
27205 to optimize the whole block of predefined macros. */
27206 if (i == 0)
27207 continue;
27208 default:
27209 break;
27210 }
27211 output_macinfo_op (ref);
27212 ref->info = NULL;
27213 ref->code = 0;
27214 }
27215
27216 if (!macinfo_htab)
27217 return;
27218
27219 /* Save the number of transparent includes so we can adjust the
27220 label number for the fat LTO object DWARF. */
27221 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27222
27223 delete macinfo_htab;
27224 macinfo_htab = NULL;
27225
27226 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27227 terminate the current chain and switch to a new comdat .debug_macinfo
27228 section and emit the define/undef entries within it. */
27229 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27230 switch (ref->code)
27231 {
27232 case 0:
27233 continue;
27234 case DW_MACRO_import:
27235 {
27236 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27237 tree comdat_key = get_identifier (ref->info);
27238 /* Terminate the previous .debug_macinfo section. */
27239 dw2_asm_output_data (1, 0, "End compilation unit");
27240 targetm.asm_out.named_section (debug_macinfo_section_name,
27241 SECTION_DEBUG
27242 | SECTION_LINKONCE
27243 | (early_lto_debug
27244 ? SECTION_EXCLUDE : 0),
27245 comdat_key);
27246 ASM_GENERATE_INTERNAL_LABEL (label,
27247 DEBUG_MACRO_SECTION_LABEL,
27248 ref->lineno + macinfo_label_base);
27249 ASM_OUTPUT_LABEL (asm_out_file, label);
27250 ref->code = 0;
27251 ref->info = NULL;
27252 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27253 "DWARF macro version number");
27254 if (DWARF_OFFSET_SIZE == 8)
27255 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27256 else
27257 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27258 }
27259 break;
27260 case DW_MACINFO_define:
27261 case DW_MACINFO_undef:
27262 output_macinfo_op (ref);
27263 ref->code = 0;
27264 ref->info = NULL;
27265 break;
27266 default:
27267 gcc_unreachable ();
27268 }
27269
27270 macinfo_label_base += macinfo_label_base_adj;
27271 }
27272
27273 /* Initialize the various sections and labels for dwarf output and prefix
27274 them with PREFIX if non-NULL. Returns the generation (zero based
27275 number of times function was called). */
27276
27277 static unsigned
27278 init_sections_and_labels (bool early_lto_debug)
27279 {
27280 /* As we may get called multiple times have a generation count for
27281 labels. */
27282 static unsigned generation = 0;
27283
27284 if (early_lto_debug)
27285 {
27286 if (!dwarf_split_debug_info)
27287 {
27288 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27289 SECTION_DEBUG | SECTION_EXCLUDE,
27290 NULL);
27291 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27292 SECTION_DEBUG | SECTION_EXCLUDE,
27293 NULL);
27294 debug_macinfo_section_name
27295 = ((dwarf_strict && dwarf_version < 5)
27296 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27297 debug_macinfo_section = get_section (debug_macinfo_section_name,
27298 SECTION_DEBUG
27299 | SECTION_EXCLUDE, NULL);
27300 /* For macro info we have to refer to a debug_line section, so
27301 similar to split-dwarf emit a skeleton one for early debug. */
27302 debug_skeleton_line_section
27303 = get_section (DEBUG_LTO_LINE_SECTION,
27304 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27305 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27306 DEBUG_SKELETON_LINE_SECTION_LABEL,
27307 generation);
27308 }
27309 else
27310 {
27311 /* ??? Which of the following do we need early? */
27312 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27313 SECTION_DEBUG | SECTION_EXCLUDE,
27314 NULL);
27315 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27316 SECTION_DEBUG | SECTION_EXCLUDE,
27317 NULL);
27318 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27319 SECTION_DEBUG
27320 | SECTION_EXCLUDE, NULL);
27321 debug_skeleton_abbrev_section
27322 = get_section (DEBUG_LTO_ABBREV_SECTION,
27323 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27324 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27325 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27326 generation);
27327
27328 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27329 stay in the main .o, but the skeleton_line goes into the split
27330 off dwo. */
27331 debug_skeleton_line_section
27332 = get_section (DEBUG_LTO_LINE_SECTION,
27333 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27334 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27335 DEBUG_SKELETON_LINE_SECTION_LABEL,
27336 generation);
27337 debug_str_offsets_section
27338 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27339 SECTION_DEBUG | SECTION_EXCLUDE,
27340 NULL);
27341 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27342 DEBUG_SKELETON_INFO_SECTION_LABEL,
27343 generation);
27344 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27345 DEBUG_STR_DWO_SECTION_FLAGS,
27346 NULL);
27347 debug_macinfo_section_name
27348 = ((dwarf_strict && dwarf_version < 5)
27349 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27350 debug_macinfo_section = get_section (debug_macinfo_section_name,
27351 SECTION_DEBUG | SECTION_EXCLUDE,
27352 NULL);
27353 }
27354 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27355 DEBUG_STR_SECTION_FLAGS
27356 | SECTION_EXCLUDE, NULL);
27357 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27358 debug_line_str_section
27359 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27360 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27361 }
27362 else
27363 {
27364 if (!dwarf_split_debug_info)
27365 {
27366 debug_info_section = get_section (DEBUG_INFO_SECTION,
27367 SECTION_DEBUG, NULL);
27368 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27369 SECTION_DEBUG, NULL);
27370 debug_loc_section = get_section (dwarf_version >= 5
27371 ? DEBUG_LOCLISTS_SECTION
27372 : DEBUG_LOC_SECTION,
27373 SECTION_DEBUG, NULL);
27374 debug_macinfo_section_name
27375 = ((dwarf_strict && dwarf_version < 5)
27376 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27377 debug_macinfo_section = get_section (debug_macinfo_section_name,
27378 SECTION_DEBUG, NULL);
27379 }
27380 else
27381 {
27382 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27383 SECTION_DEBUG | SECTION_EXCLUDE,
27384 NULL);
27385 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27386 SECTION_DEBUG | SECTION_EXCLUDE,
27387 NULL);
27388 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27389 SECTION_DEBUG, NULL);
27390 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27391 SECTION_DEBUG, NULL);
27392 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27393 SECTION_DEBUG, NULL);
27394 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27395 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27396 generation);
27397
27398 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27399 stay in the main .o, but the skeleton_line goes into the
27400 split off dwo. */
27401 debug_skeleton_line_section
27402 = get_section (DEBUG_DWO_LINE_SECTION,
27403 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27404 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27405 DEBUG_SKELETON_LINE_SECTION_LABEL,
27406 generation);
27407 debug_str_offsets_section
27408 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27409 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27410 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27411 DEBUG_SKELETON_INFO_SECTION_LABEL,
27412 generation);
27413 debug_loc_section = get_section (dwarf_version >= 5
27414 ? DEBUG_DWO_LOCLISTS_SECTION
27415 : DEBUG_DWO_LOC_SECTION,
27416 SECTION_DEBUG | SECTION_EXCLUDE,
27417 NULL);
27418 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27419 DEBUG_STR_DWO_SECTION_FLAGS,
27420 NULL);
27421 debug_macinfo_section_name
27422 = ((dwarf_strict && dwarf_version < 5)
27423 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27424 debug_macinfo_section = get_section (debug_macinfo_section_name,
27425 SECTION_DEBUG | SECTION_EXCLUDE,
27426 NULL);
27427 }
27428 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27429 SECTION_DEBUG, NULL);
27430 debug_line_section = get_section (DEBUG_LINE_SECTION,
27431 SECTION_DEBUG, NULL);
27432 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27433 SECTION_DEBUG, NULL);
27434 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27435 SECTION_DEBUG, NULL);
27436 debug_str_section = get_section (DEBUG_STR_SECTION,
27437 DEBUG_STR_SECTION_FLAGS, NULL);
27438 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27439 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27440 DEBUG_STR_SECTION_FLAGS, NULL);
27441 debug_ranges_section = get_section (dwarf_version >= 5
27442 ? DEBUG_RNGLISTS_SECTION
27443 : DEBUG_RANGES_SECTION,
27444 SECTION_DEBUG, NULL);
27445 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27446 SECTION_DEBUG, NULL);
27447 }
27448
27449 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27450 DEBUG_ABBREV_SECTION_LABEL, generation);
27451 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27452 DEBUG_INFO_SECTION_LABEL, generation);
27453 info_section_emitted = false;
27454 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27455 DEBUG_LINE_SECTION_LABEL, generation);
27456 /* There are up to 4 unique ranges labels per generation.
27457 See also output_rnglists. */
27458 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27459 DEBUG_RANGES_SECTION_LABEL, generation * 4);
27460 if (dwarf_version >= 5 && dwarf_split_debug_info)
27461 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27462 DEBUG_RANGES_SECTION_LABEL,
27463 1 + generation * 4);
27464 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27465 DEBUG_ADDR_SECTION_LABEL, generation);
27466 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27467 (dwarf_strict && dwarf_version < 5)
27468 ? DEBUG_MACINFO_SECTION_LABEL
27469 : DEBUG_MACRO_SECTION_LABEL, generation);
27470 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27471 generation);
27472
27473 ++generation;
27474 return generation - 1;
27475 }
27476
27477 /* Set up for Dwarf output at the start of compilation. */
27478
27479 static void
27480 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27481 {
27482 /* Allocate the file_table. */
27483 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27484
27485 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27486 /* Allocate the decl_die_table. */
27487 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27488
27489 /* Allocate the decl_loc_table. */
27490 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27491
27492 /* Allocate the cached_dw_loc_list_table. */
27493 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27494
27495 /* Allocate the initial hunk of the decl_scope_table. */
27496 vec_alloc (decl_scope_table, 256);
27497
27498 /* Allocate the initial hunk of the abbrev_die_table. */
27499 vec_alloc (abbrev_die_table, 256);
27500 /* Zero-th entry is allocated, but unused. */
27501 abbrev_die_table->quick_push (NULL);
27502
27503 /* Allocate the dwarf_proc_stack_usage_map. */
27504 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27505
27506 /* Allocate the pubtypes and pubnames vectors. */
27507 vec_alloc (pubname_table, 32);
27508 vec_alloc (pubtype_table, 32);
27509
27510 vec_alloc (incomplete_types, 64);
27511
27512 vec_alloc (used_rtx_array, 32);
27513
27514 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27515 vec_alloc (macinfo_table, 64);
27516 #endif
27517
27518 /* If front-ends already registered a main translation unit but we were not
27519 ready to perform the association, do this now. */
27520 if (main_translation_unit != NULL_TREE)
27521 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27522 }
27523
27524 /* Called before compile () starts outputtting functions, variables
27525 and toplevel asms into assembly. */
27526
27527 static void
27528 dwarf2out_assembly_start (void)
27529 {
27530 if (text_section_line_info)
27531 return;
27532
27533 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27534 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27535 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27536 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27537 COLD_TEXT_SECTION_LABEL, 0);
27538 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27539
27540 switch_to_section (text_section);
27541 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27542 #endif
27543
27544 /* Make sure the line number table for .text always exists. */
27545 text_section_line_info = new_line_info_table ();
27546 text_section_line_info->end_label = text_end_label;
27547
27548 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27549 cur_line_info_table = text_section_line_info;
27550 #endif
27551
27552 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27553 && dwarf2out_do_cfi_asm ()
27554 && !dwarf2out_do_eh_frame ())
27555 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27556 }
27557
27558 /* A helper function for dwarf2out_finish called through
27559 htab_traverse. Assign a string its index. All strings must be
27560 collected into the table by the time index_string is called,
27561 because the indexing code relies on htab_traverse to traverse nodes
27562 in the same order for each run. */
27563
27564 int
27565 index_string (indirect_string_node **h, unsigned int *index)
27566 {
27567 indirect_string_node *node = *h;
27568
27569 find_string_form (node);
27570 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27571 {
27572 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27573 node->index = *index;
27574 *index += 1;
27575 }
27576 return 1;
27577 }
27578
27579 /* A helper function for output_indirect_strings called through
27580 htab_traverse. Output the offset to a string and update the
27581 current offset. */
27582
27583 int
27584 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27585 {
27586 indirect_string_node *node = *h;
27587
27588 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27589 {
27590 /* Assert that this node has been assigned an index. */
27591 gcc_assert (node->index != NO_INDEX_ASSIGNED
27592 && node->index != NOT_INDEXED);
27593 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27594 "indexed string 0x%x: %s", node->index, node->str);
27595 *offset += strlen (node->str) + 1;
27596 }
27597 return 1;
27598 }
27599
27600 /* A helper function for dwarf2out_finish called through
27601 htab_traverse. Output the indexed string. */
27602
27603 int
27604 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27605 {
27606 struct indirect_string_node *node = *h;
27607
27608 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27609 {
27610 /* Assert that the strings are output in the same order as their
27611 indexes were assigned. */
27612 gcc_assert (*cur_idx == node->index);
27613 assemble_string (node->str, strlen (node->str) + 1);
27614 *cur_idx += 1;
27615 }
27616 return 1;
27617 }
27618
27619 /* A helper function for dwarf2out_finish called through
27620 htab_traverse. Emit one queued .debug_str string. */
27621
27622 int
27623 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27624 {
27625 struct indirect_string_node *node = *h;
27626
27627 node->form = find_string_form (node);
27628 if (node->form == form && node->refcount > 0)
27629 {
27630 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27631 assemble_string (node->str, strlen (node->str) + 1);
27632 }
27633
27634 return 1;
27635 }
27636
27637 /* Output the indexed string table. */
27638
27639 static void
27640 output_indirect_strings (void)
27641 {
27642 switch_to_section (debug_str_section);
27643 if (!dwarf_split_debug_info)
27644 debug_str_hash->traverse<enum dwarf_form,
27645 output_indirect_string> (DW_FORM_strp);
27646 else
27647 {
27648 unsigned int offset = 0;
27649 unsigned int cur_idx = 0;
27650
27651 skeleton_debug_str_hash->traverse<enum dwarf_form,
27652 output_indirect_string> (DW_FORM_strp);
27653
27654 switch_to_section (debug_str_offsets_section);
27655 debug_str_hash->traverse_noresize
27656 <unsigned int *, output_index_string_offset> (&offset);
27657 switch_to_section (debug_str_dwo_section);
27658 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27659 (&cur_idx);
27660 }
27661 }
27662
27663 /* Callback for htab_traverse to assign an index to an entry in the
27664 table, and to write that entry to the .debug_addr section. */
27665
27666 int
27667 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27668 {
27669 addr_table_entry *entry = *slot;
27670
27671 if (entry->refcount == 0)
27672 {
27673 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27674 || entry->index == NOT_INDEXED);
27675 return 1;
27676 }
27677
27678 gcc_assert (entry->index == *cur_index);
27679 (*cur_index)++;
27680
27681 switch (entry->kind)
27682 {
27683 case ate_kind_rtx:
27684 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27685 "0x%x", entry->index);
27686 break;
27687 case ate_kind_rtx_dtprel:
27688 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27689 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27690 DWARF2_ADDR_SIZE,
27691 entry->addr.rtl);
27692 fputc ('\n', asm_out_file);
27693 break;
27694 case ate_kind_label:
27695 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27696 "0x%x", entry->index);
27697 break;
27698 default:
27699 gcc_unreachable ();
27700 }
27701 return 1;
27702 }
27703
27704 /* Produce the .debug_addr section. */
27705
27706 static void
27707 output_addr_table (void)
27708 {
27709 unsigned int index = 0;
27710 if (addr_index_table == NULL || addr_index_table->size () == 0)
27711 return;
27712
27713 switch_to_section (debug_addr_section);
27714 addr_index_table
27715 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27716 }
27717
27718 #if ENABLE_ASSERT_CHECKING
27719 /* Verify that all marks are clear. */
27720
27721 static void
27722 verify_marks_clear (dw_die_ref die)
27723 {
27724 dw_die_ref c;
27725
27726 gcc_assert (! die->die_mark);
27727 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27728 }
27729 #endif /* ENABLE_ASSERT_CHECKING */
27730
27731 /* Clear the marks for a die and its children.
27732 Be cool if the mark isn't set. */
27733
27734 static void
27735 prune_unmark_dies (dw_die_ref die)
27736 {
27737 dw_die_ref c;
27738
27739 if (die->die_mark)
27740 die->die_mark = 0;
27741 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27742 }
27743
27744 /* Given LOC that is referenced by a DIE we're marking as used, find all
27745 referenced DWARF procedures it references and mark them as used. */
27746
27747 static void
27748 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27749 {
27750 for (; loc != NULL; loc = loc->dw_loc_next)
27751 switch (loc->dw_loc_opc)
27752 {
27753 case DW_OP_implicit_pointer:
27754 case DW_OP_convert:
27755 case DW_OP_reinterpret:
27756 case DW_OP_GNU_implicit_pointer:
27757 case DW_OP_GNU_convert:
27758 case DW_OP_GNU_reinterpret:
27759 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27760 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27761 break;
27762 case DW_OP_GNU_variable_value:
27763 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27764 {
27765 dw_die_ref ref
27766 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27767 if (ref == NULL)
27768 break;
27769 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27770 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27771 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27772 }
27773 /* FALLTHRU */
27774 case DW_OP_call2:
27775 case DW_OP_call4:
27776 case DW_OP_call_ref:
27777 case DW_OP_const_type:
27778 case DW_OP_GNU_const_type:
27779 case DW_OP_GNU_parameter_ref:
27780 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27781 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27782 break;
27783 case DW_OP_regval_type:
27784 case DW_OP_deref_type:
27785 case DW_OP_GNU_regval_type:
27786 case DW_OP_GNU_deref_type:
27787 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27788 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27789 break;
27790 case DW_OP_entry_value:
27791 case DW_OP_GNU_entry_value:
27792 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27793 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27794 break;
27795 default:
27796 break;
27797 }
27798 }
27799
27800 /* Given DIE that we're marking as used, find any other dies
27801 it references as attributes and mark them as used. */
27802
27803 static void
27804 prune_unused_types_walk_attribs (dw_die_ref die)
27805 {
27806 dw_attr_node *a;
27807 unsigned ix;
27808
27809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27810 {
27811 switch (AT_class (a))
27812 {
27813 /* Make sure DWARF procedures referenced by location descriptions will
27814 get emitted. */
27815 case dw_val_class_loc:
27816 prune_unused_types_walk_loc_descr (AT_loc (a));
27817 break;
27818 case dw_val_class_loc_list:
27819 for (dw_loc_list_ref list = AT_loc_list (a);
27820 list != NULL;
27821 list = list->dw_loc_next)
27822 prune_unused_types_walk_loc_descr (list->expr);
27823 break;
27824
27825 case dw_val_class_die_ref:
27826 /* A reference to another DIE.
27827 Make sure that it will get emitted.
27828 If it was broken out into a comdat group, don't follow it. */
27829 if (! AT_ref (a)->comdat_type_p
27830 || a->dw_attr == DW_AT_specification)
27831 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27832 break;
27833
27834 case dw_val_class_str:
27835 /* Set the string's refcount to 0 so that prune_unused_types_mark
27836 accounts properly for it. */
27837 a->dw_attr_val.v.val_str->refcount = 0;
27838 break;
27839
27840 default:
27841 break;
27842 }
27843 }
27844 }
27845
27846 /* Mark the generic parameters and arguments children DIEs of DIE. */
27847
27848 static void
27849 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27850 {
27851 dw_die_ref c;
27852
27853 if (die == NULL || die->die_child == NULL)
27854 return;
27855 c = die->die_child;
27856 do
27857 {
27858 if (is_template_parameter (c))
27859 prune_unused_types_mark (c, 1);
27860 c = c->die_sib;
27861 } while (c && c != die->die_child);
27862 }
27863
27864 /* Mark DIE as being used. If DOKIDS is true, then walk down
27865 to DIE's children. */
27866
27867 static void
27868 prune_unused_types_mark (dw_die_ref die, int dokids)
27869 {
27870 dw_die_ref c;
27871
27872 if (die->die_mark == 0)
27873 {
27874 /* We haven't done this node yet. Mark it as used. */
27875 die->die_mark = 1;
27876 /* If this is the DIE of a generic type instantiation,
27877 mark the children DIEs that describe its generic parms and
27878 args. */
27879 prune_unused_types_mark_generic_parms_dies (die);
27880
27881 /* We also have to mark its parents as used.
27882 (But we don't want to mark our parent's kids due to this,
27883 unless it is a class.) */
27884 if (die->die_parent)
27885 prune_unused_types_mark (die->die_parent,
27886 class_scope_p (die->die_parent));
27887
27888 /* Mark any referenced nodes. */
27889 prune_unused_types_walk_attribs (die);
27890
27891 /* If this node is a specification,
27892 also mark the definition, if it exists. */
27893 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27894 prune_unused_types_mark (die->die_definition, 1);
27895 }
27896
27897 if (dokids && die->die_mark != 2)
27898 {
27899 /* We need to walk the children, but haven't done so yet.
27900 Remember that we've walked the kids. */
27901 die->die_mark = 2;
27902
27903 /* If this is an array type, we need to make sure our
27904 kids get marked, even if they're types. If we're
27905 breaking out types into comdat sections, do this
27906 for all type definitions. */
27907 if (die->die_tag == DW_TAG_array_type
27908 || (use_debug_types
27909 && is_type_die (die) && ! is_declaration_die (die)))
27910 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27911 else
27912 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27913 }
27914 }
27915
27916 /* For local classes, look if any static member functions were emitted
27917 and if so, mark them. */
27918
27919 static void
27920 prune_unused_types_walk_local_classes (dw_die_ref die)
27921 {
27922 dw_die_ref c;
27923
27924 if (die->die_mark == 2)
27925 return;
27926
27927 switch (die->die_tag)
27928 {
27929 case DW_TAG_structure_type:
27930 case DW_TAG_union_type:
27931 case DW_TAG_class_type:
27932 break;
27933
27934 case DW_TAG_subprogram:
27935 if (!get_AT_flag (die, DW_AT_declaration)
27936 || die->die_definition != NULL)
27937 prune_unused_types_mark (die, 1);
27938 return;
27939
27940 default:
27941 return;
27942 }
27943
27944 /* Mark children. */
27945 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27946 }
27947
27948 /* Walk the tree DIE and mark types that we actually use. */
27949
27950 static void
27951 prune_unused_types_walk (dw_die_ref die)
27952 {
27953 dw_die_ref c;
27954
27955 /* Don't do anything if this node is already marked and
27956 children have been marked as well. */
27957 if (die->die_mark == 2)
27958 return;
27959
27960 switch (die->die_tag)
27961 {
27962 case DW_TAG_structure_type:
27963 case DW_TAG_union_type:
27964 case DW_TAG_class_type:
27965 if (die->die_perennial_p)
27966 break;
27967
27968 for (c = die->die_parent; c; c = c->die_parent)
27969 if (c->die_tag == DW_TAG_subprogram)
27970 break;
27971
27972 /* Finding used static member functions inside of classes
27973 is needed just for local classes, because for other classes
27974 static member function DIEs with DW_AT_specification
27975 are emitted outside of the DW_TAG_*_type. If we ever change
27976 it, we'd need to call this even for non-local classes. */
27977 if (c)
27978 prune_unused_types_walk_local_classes (die);
27979
27980 /* It's a type node --- don't mark it. */
27981 return;
27982
27983 case DW_TAG_const_type:
27984 case DW_TAG_packed_type:
27985 case DW_TAG_pointer_type:
27986 case DW_TAG_reference_type:
27987 case DW_TAG_rvalue_reference_type:
27988 case DW_TAG_volatile_type:
27989 case DW_TAG_typedef:
27990 case DW_TAG_array_type:
27991 case DW_TAG_interface_type:
27992 case DW_TAG_friend:
27993 case DW_TAG_enumeration_type:
27994 case DW_TAG_subroutine_type:
27995 case DW_TAG_string_type:
27996 case DW_TAG_set_type:
27997 case DW_TAG_subrange_type:
27998 case DW_TAG_ptr_to_member_type:
27999 case DW_TAG_file_type:
28000 /* Type nodes are useful only when other DIEs reference them --- don't
28001 mark them. */
28002 /* FALLTHROUGH */
28003
28004 case DW_TAG_dwarf_procedure:
28005 /* Likewise for DWARF procedures. */
28006
28007 if (die->die_perennial_p)
28008 break;
28009
28010 return;
28011
28012 default:
28013 /* Mark everything else. */
28014 break;
28015 }
28016
28017 if (die->die_mark == 0)
28018 {
28019 die->die_mark = 1;
28020
28021 /* Now, mark any dies referenced from here. */
28022 prune_unused_types_walk_attribs (die);
28023 }
28024
28025 die->die_mark = 2;
28026
28027 /* Mark children. */
28028 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28029 }
28030
28031 /* Increment the string counts on strings referred to from DIE's
28032 attributes. */
28033
28034 static void
28035 prune_unused_types_update_strings (dw_die_ref die)
28036 {
28037 dw_attr_node *a;
28038 unsigned ix;
28039
28040 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28041 if (AT_class (a) == dw_val_class_str)
28042 {
28043 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28044 s->refcount++;
28045 /* Avoid unnecessarily putting strings that are used less than
28046 twice in the hash table. */
28047 if (s->refcount
28048 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28049 {
28050 indirect_string_node **slot
28051 = debug_str_hash->find_slot_with_hash (s->str,
28052 htab_hash_string (s->str),
28053 INSERT);
28054 gcc_assert (*slot == NULL);
28055 *slot = s;
28056 }
28057 }
28058 }
28059
28060 /* Mark DIE and its children as removed. */
28061
28062 static void
28063 mark_removed (dw_die_ref die)
28064 {
28065 dw_die_ref c;
28066 die->removed = true;
28067 FOR_EACH_CHILD (die, c, mark_removed (c));
28068 }
28069
28070 /* Remove from the tree DIE any dies that aren't marked. */
28071
28072 static void
28073 prune_unused_types_prune (dw_die_ref die)
28074 {
28075 dw_die_ref c;
28076
28077 gcc_assert (die->die_mark);
28078 prune_unused_types_update_strings (die);
28079
28080 if (! die->die_child)
28081 return;
28082
28083 c = die->die_child;
28084 do {
28085 dw_die_ref prev = c, next;
28086 for (c = c->die_sib; ! c->die_mark; c = next)
28087 if (c == die->die_child)
28088 {
28089 /* No marked children between 'prev' and the end of the list. */
28090 if (prev == c)
28091 /* No marked children at all. */
28092 die->die_child = NULL;
28093 else
28094 {
28095 prev->die_sib = c->die_sib;
28096 die->die_child = prev;
28097 }
28098 c->die_sib = NULL;
28099 mark_removed (c);
28100 return;
28101 }
28102 else
28103 {
28104 next = c->die_sib;
28105 c->die_sib = NULL;
28106 mark_removed (c);
28107 }
28108
28109 if (c != prev->die_sib)
28110 prev->die_sib = c;
28111 prune_unused_types_prune (c);
28112 } while (c != die->die_child);
28113 }
28114
28115 /* Remove dies representing declarations that we never use. */
28116
28117 static void
28118 prune_unused_types (void)
28119 {
28120 unsigned int i;
28121 limbo_die_node *node;
28122 comdat_type_node *ctnode;
28123 pubname_entry *pub;
28124 dw_die_ref base_type;
28125
28126 #if ENABLE_ASSERT_CHECKING
28127 /* All the marks should already be clear. */
28128 verify_marks_clear (comp_unit_die ());
28129 for (node = limbo_die_list; node; node = node->next)
28130 verify_marks_clear (node->die);
28131 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28132 verify_marks_clear (ctnode->root_die);
28133 #endif /* ENABLE_ASSERT_CHECKING */
28134
28135 /* Mark types that are used in global variables. */
28136 premark_types_used_by_global_vars ();
28137
28138 /* Set the mark on nodes that are actually used. */
28139 prune_unused_types_walk (comp_unit_die ());
28140 for (node = limbo_die_list; node; node = node->next)
28141 prune_unused_types_walk (node->die);
28142 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28143 {
28144 prune_unused_types_walk (ctnode->root_die);
28145 prune_unused_types_mark (ctnode->type_die, 1);
28146 }
28147
28148 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28149 are unusual in that they are pubnames that are the children of pubtypes.
28150 They should only be marked via their parent DW_TAG_enumeration_type die,
28151 not as roots in themselves. */
28152 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28153 if (pub->die->die_tag != DW_TAG_enumerator)
28154 prune_unused_types_mark (pub->die, 1);
28155 for (i = 0; base_types.iterate (i, &base_type); i++)
28156 prune_unused_types_mark (base_type, 1);
28157
28158 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28159 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28160 callees). */
28161 cgraph_node *cnode;
28162 FOR_EACH_FUNCTION (cnode)
28163 if (cnode->referred_to_p (false))
28164 {
28165 dw_die_ref die = lookup_decl_die (cnode->decl);
28166 if (die == NULL || die->die_mark)
28167 continue;
28168 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28169 if (e->caller != cnode
28170 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28171 {
28172 prune_unused_types_mark (die, 1);
28173 break;
28174 }
28175 }
28176
28177 if (debug_str_hash)
28178 debug_str_hash->empty ();
28179 if (skeleton_debug_str_hash)
28180 skeleton_debug_str_hash->empty ();
28181 prune_unused_types_prune (comp_unit_die ());
28182 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28183 {
28184 node = *pnode;
28185 if (!node->die->die_mark)
28186 *pnode = node->next;
28187 else
28188 {
28189 prune_unused_types_prune (node->die);
28190 pnode = &node->next;
28191 }
28192 }
28193 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28194 prune_unused_types_prune (ctnode->root_die);
28195
28196 /* Leave the marks clear. */
28197 prune_unmark_dies (comp_unit_die ());
28198 for (node = limbo_die_list; node; node = node->next)
28199 prune_unmark_dies (node->die);
28200 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28201 prune_unmark_dies (ctnode->root_die);
28202 }
28203
28204 /* Helpers to manipulate hash table of comdat type units. */
28205
28206 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28207 {
28208 static inline hashval_t hash (const comdat_type_node *);
28209 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28210 };
28211
28212 inline hashval_t
28213 comdat_type_hasher::hash (const comdat_type_node *type_node)
28214 {
28215 hashval_t h;
28216 memcpy (&h, type_node->signature, sizeof (h));
28217 return h;
28218 }
28219
28220 inline bool
28221 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28222 const comdat_type_node *type_node_2)
28223 {
28224 return (! memcmp (type_node_1->signature, type_node_2->signature,
28225 DWARF_TYPE_SIGNATURE_SIZE));
28226 }
28227
28228 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28229 to the location it would have been added, should we know its
28230 DECL_ASSEMBLER_NAME when we added other attributes. This will
28231 probably improve compactness of debug info, removing equivalent
28232 abbrevs, and hide any differences caused by deferring the
28233 computation of the assembler name, triggered by e.g. PCH. */
28234
28235 static inline void
28236 move_linkage_attr (dw_die_ref die)
28237 {
28238 unsigned ix = vec_safe_length (die->die_attr);
28239 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28240
28241 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28242 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28243
28244 while (--ix > 0)
28245 {
28246 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28247
28248 if (prev->dw_attr == DW_AT_decl_line
28249 || prev->dw_attr == DW_AT_decl_column
28250 || prev->dw_attr == DW_AT_name)
28251 break;
28252 }
28253
28254 if (ix != vec_safe_length (die->die_attr) - 1)
28255 {
28256 die->die_attr->pop ();
28257 die->die_attr->quick_insert (ix, linkage);
28258 }
28259 }
28260
28261 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28262 referenced from typed stack ops and count how often they are used. */
28263
28264 static void
28265 mark_base_types (dw_loc_descr_ref loc)
28266 {
28267 dw_die_ref base_type = NULL;
28268
28269 for (; loc; loc = loc->dw_loc_next)
28270 {
28271 switch (loc->dw_loc_opc)
28272 {
28273 case DW_OP_regval_type:
28274 case DW_OP_deref_type:
28275 case DW_OP_GNU_regval_type:
28276 case DW_OP_GNU_deref_type:
28277 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28278 break;
28279 case DW_OP_convert:
28280 case DW_OP_reinterpret:
28281 case DW_OP_GNU_convert:
28282 case DW_OP_GNU_reinterpret:
28283 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28284 continue;
28285 /* FALLTHRU */
28286 case DW_OP_const_type:
28287 case DW_OP_GNU_const_type:
28288 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28289 break;
28290 case DW_OP_entry_value:
28291 case DW_OP_GNU_entry_value:
28292 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28293 continue;
28294 default:
28295 continue;
28296 }
28297 gcc_assert (base_type->die_parent == comp_unit_die ());
28298 if (base_type->die_mark)
28299 base_type->die_mark++;
28300 else
28301 {
28302 base_types.safe_push (base_type);
28303 base_type->die_mark = 1;
28304 }
28305 }
28306 }
28307
28308 /* Comparison function for sorting marked base types. */
28309
28310 static int
28311 base_type_cmp (const void *x, const void *y)
28312 {
28313 dw_die_ref dx = *(const dw_die_ref *) x;
28314 dw_die_ref dy = *(const dw_die_ref *) y;
28315 unsigned int byte_size1, byte_size2;
28316 unsigned int encoding1, encoding2;
28317 unsigned int align1, align2;
28318 if (dx->die_mark > dy->die_mark)
28319 return -1;
28320 if (dx->die_mark < dy->die_mark)
28321 return 1;
28322 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28323 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28324 if (byte_size1 < byte_size2)
28325 return 1;
28326 if (byte_size1 > byte_size2)
28327 return -1;
28328 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28329 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28330 if (encoding1 < encoding2)
28331 return 1;
28332 if (encoding1 > encoding2)
28333 return -1;
28334 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28335 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28336 if (align1 < align2)
28337 return 1;
28338 if (align1 > align2)
28339 return -1;
28340 return 0;
28341 }
28342
28343 /* Move base types marked by mark_base_types as early as possible
28344 in the CU, sorted by decreasing usage count both to make the
28345 uleb128 references as small as possible and to make sure they
28346 will have die_offset already computed by calc_die_sizes when
28347 sizes of typed stack loc ops is computed. */
28348
28349 static void
28350 move_marked_base_types (void)
28351 {
28352 unsigned int i;
28353 dw_die_ref base_type, die, c;
28354
28355 if (base_types.is_empty ())
28356 return;
28357
28358 /* Sort by decreasing usage count, they will be added again in that
28359 order later on. */
28360 base_types.qsort (base_type_cmp);
28361 die = comp_unit_die ();
28362 c = die->die_child;
28363 do
28364 {
28365 dw_die_ref prev = c;
28366 c = c->die_sib;
28367 while (c->die_mark)
28368 {
28369 remove_child_with_prev (c, prev);
28370 /* As base types got marked, there must be at least
28371 one node other than DW_TAG_base_type. */
28372 gcc_assert (die->die_child != NULL);
28373 c = prev->die_sib;
28374 }
28375 }
28376 while (c != die->die_child);
28377 gcc_assert (die->die_child);
28378 c = die->die_child;
28379 for (i = 0; base_types.iterate (i, &base_type); i++)
28380 {
28381 base_type->die_mark = 0;
28382 base_type->die_sib = c->die_sib;
28383 c->die_sib = base_type;
28384 c = base_type;
28385 }
28386 }
28387
28388 /* Helper function for resolve_addr, attempt to resolve
28389 one CONST_STRING, return true if successful. Similarly verify that
28390 SYMBOL_REFs refer to variables emitted in the current CU. */
28391
28392 static bool
28393 resolve_one_addr (rtx *addr)
28394 {
28395 rtx rtl = *addr;
28396
28397 if (GET_CODE (rtl) == CONST_STRING)
28398 {
28399 size_t len = strlen (XSTR (rtl, 0)) + 1;
28400 tree t = build_string (len, XSTR (rtl, 0));
28401 tree tlen = size_int (len - 1);
28402 TREE_TYPE (t)
28403 = build_array_type (char_type_node, build_index_type (tlen));
28404 rtl = lookup_constant_def (t);
28405 if (!rtl || !MEM_P (rtl))
28406 return false;
28407 rtl = XEXP (rtl, 0);
28408 if (GET_CODE (rtl) == SYMBOL_REF
28409 && SYMBOL_REF_DECL (rtl)
28410 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28411 return false;
28412 vec_safe_push (used_rtx_array, rtl);
28413 *addr = rtl;
28414 return true;
28415 }
28416
28417 if (GET_CODE (rtl) == SYMBOL_REF
28418 && SYMBOL_REF_DECL (rtl))
28419 {
28420 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28421 {
28422 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28423 return false;
28424 }
28425 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28426 return false;
28427 }
28428
28429 if (GET_CODE (rtl) == CONST)
28430 {
28431 subrtx_ptr_iterator::array_type array;
28432 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28433 if (!resolve_one_addr (*iter))
28434 return false;
28435 }
28436
28437 return true;
28438 }
28439
28440 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28441 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28442 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28443
28444 static rtx
28445 string_cst_pool_decl (tree t)
28446 {
28447 rtx rtl = output_constant_def (t, 1);
28448 unsigned char *array;
28449 dw_loc_descr_ref l;
28450 tree decl;
28451 size_t len;
28452 dw_die_ref ref;
28453
28454 if (!rtl || !MEM_P (rtl))
28455 return NULL_RTX;
28456 rtl = XEXP (rtl, 0);
28457 if (GET_CODE (rtl) != SYMBOL_REF
28458 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28459 return NULL_RTX;
28460
28461 decl = SYMBOL_REF_DECL (rtl);
28462 if (!lookup_decl_die (decl))
28463 {
28464 len = TREE_STRING_LENGTH (t);
28465 vec_safe_push (used_rtx_array, rtl);
28466 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28467 array = ggc_vec_alloc<unsigned char> (len);
28468 memcpy (array, TREE_STRING_POINTER (t), len);
28469 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28470 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28471 l->dw_loc_oprnd2.v.val_vec.length = len;
28472 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28473 l->dw_loc_oprnd2.v.val_vec.array = array;
28474 add_AT_loc (ref, DW_AT_location, l);
28475 equate_decl_number_to_die (decl, ref);
28476 }
28477 return rtl;
28478 }
28479
28480 /* Helper function of resolve_addr_in_expr. LOC is
28481 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28482 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28483 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28484 with DW_OP_implicit_pointer if possible
28485 and return true, if unsuccessful, return false. */
28486
28487 static bool
28488 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28489 {
28490 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28491 HOST_WIDE_INT offset = 0;
28492 dw_die_ref ref = NULL;
28493 tree decl;
28494
28495 if (GET_CODE (rtl) == CONST
28496 && GET_CODE (XEXP (rtl, 0)) == PLUS
28497 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28498 {
28499 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28500 rtl = XEXP (XEXP (rtl, 0), 0);
28501 }
28502 if (GET_CODE (rtl) == CONST_STRING)
28503 {
28504 size_t len = strlen (XSTR (rtl, 0)) + 1;
28505 tree t = build_string (len, XSTR (rtl, 0));
28506 tree tlen = size_int (len - 1);
28507
28508 TREE_TYPE (t)
28509 = build_array_type (char_type_node, build_index_type (tlen));
28510 rtl = string_cst_pool_decl (t);
28511 if (!rtl)
28512 return false;
28513 }
28514 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28515 {
28516 decl = SYMBOL_REF_DECL (rtl);
28517 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28518 {
28519 ref = lookup_decl_die (decl);
28520 if (ref && (get_AT (ref, DW_AT_location)
28521 || get_AT (ref, DW_AT_const_value)))
28522 {
28523 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28524 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28525 loc->dw_loc_oprnd1.val_entry = NULL;
28526 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28527 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28528 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28529 loc->dw_loc_oprnd2.v.val_int = offset;
28530 return true;
28531 }
28532 }
28533 }
28534 return false;
28535 }
28536
28537 /* Helper function for resolve_addr, handle one location
28538 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28539 the location list couldn't be resolved. */
28540
28541 static bool
28542 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28543 {
28544 dw_loc_descr_ref keep = NULL;
28545 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28546 switch (loc->dw_loc_opc)
28547 {
28548 case DW_OP_addr:
28549 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28550 {
28551 if ((prev == NULL
28552 || prev->dw_loc_opc == DW_OP_piece
28553 || prev->dw_loc_opc == DW_OP_bit_piece)
28554 && loc->dw_loc_next
28555 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28556 && (!dwarf_strict || dwarf_version >= 5)
28557 && optimize_one_addr_into_implicit_ptr (loc))
28558 break;
28559 return false;
28560 }
28561 break;
28562 case DW_OP_GNU_addr_index:
28563 case DW_OP_GNU_const_index:
28564 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28565 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28566 {
28567 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28568 if (!resolve_one_addr (&rtl))
28569 return false;
28570 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28571 loc->dw_loc_oprnd1.val_entry
28572 = add_addr_table_entry (rtl, ate_kind_rtx);
28573 }
28574 break;
28575 case DW_OP_const4u:
28576 case DW_OP_const8u:
28577 if (loc->dtprel
28578 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28579 return false;
28580 break;
28581 case DW_OP_plus_uconst:
28582 if (size_of_loc_descr (loc)
28583 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28584 + 1
28585 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28586 {
28587 dw_loc_descr_ref repl
28588 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28589 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28590 add_loc_descr (&repl, loc->dw_loc_next);
28591 *loc = *repl;
28592 }
28593 break;
28594 case DW_OP_implicit_value:
28595 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28596 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28597 return false;
28598 break;
28599 case DW_OP_implicit_pointer:
28600 case DW_OP_GNU_implicit_pointer:
28601 case DW_OP_GNU_parameter_ref:
28602 case DW_OP_GNU_variable_value:
28603 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28604 {
28605 dw_die_ref ref
28606 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28607 if (ref == NULL)
28608 return false;
28609 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28610 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28611 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28612 }
28613 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28614 {
28615 if (prev == NULL
28616 && loc->dw_loc_next == NULL
28617 && AT_class (a) == dw_val_class_loc)
28618 switch (a->dw_attr)
28619 {
28620 /* Following attributes allow both exprloc and reference,
28621 so if the whole expression is DW_OP_GNU_variable_value
28622 alone we could transform it into reference. */
28623 case DW_AT_byte_size:
28624 case DW_AT_bit_size:
28625 case DW_AT_lower_bound:
28626 case DW_AT_upper_bound:
28627 case DW_AT_bit_stride:
28628 case DW_AT_count:
28629 case DW_AT_allocated:
28630 case DW_AT_associated:
28631 case DW_AT_byte_stride:
28632 a->dw_attr_val.val_class = dw_val_class_die_ref;
28633 a->dw_attr_val.val_entry = NULL;
28634 a->dw_attr_val.v.val_die_ref.die
28635 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28636 a->dw_attr_val.v.val_die_ref.external = 0;
28637 return true;
28638 default:
28639 break;
28640 }
28641 if (dwarf_strict)
28642 return false;
28643 }
28644 break;
28645 case DW_OP_const_type:
28646 case DW_OP_regval_type:
28647 case DW_OP_deref_type:
28648 case DW_OP_convert:
28649 case DW_OP_reinterpret:
28650 case DW_OP_GNU_const_type:
28651 case DW_OP_GNU_regval_type:
28652 case DW_OP_GNU_deref_type:
28653 case DW_OP_GNU_convert:
28654 case DW_OP_GNU_reinterpret:
28655 while (loc->dw_loc_next
28656 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28657 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28658 {
28659 dw_die_ref base1, base2;
28660 unsigned enc1, enc2, size1, size2;
28661 if (loc->dw_loc_opc == DW_OP_regval_type
28662 || loc->dw_loc_opc == DW_OP_deref_type
28663 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28664 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28665 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28666 else if (loc->dw_loc_oprnd1.val_class
28667 == dw_val_class_unsigned_const)
28668 break;
28669 else
28670 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28671 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28672 == dw_val_class_unsigned_const)
28673 break;
28674 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28675 gcc_assert (base1->die_tag == DW_TAG_base_type
28676 && base2->die_tag == DW_TAG_base_type);
28677 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28678 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28679 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28680 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28681 if (size1 == size2
28682 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28683 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28684 && loc != keep)
28685 || enc1 == enc2))
28686 {
28687 /* Optimize away next DW_OP_convert after
28688 adjusting LOC's base type die reference. */
28689 if (loc->dw_loc_opc == DW_OP_regval_type
28690 || loc->dw_loc_opc == DW_OP_deref_type
28691 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28692 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28693 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28694 else
28695 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28696 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28697 continue;
28698 }
28699 /* Don't change integer DW_OP_convert after e.g. floating
28700 point typed stack entry. */
28701 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28702 keep = loc->dw_loc_next;
28703 break;
28704 }
28705 break;
28706 default:
28707 break;
28708 }
28709 return true;
28710 }
28711
28712 /* Helper function of resolve_addr. DIE had DW_AT_location of
28713 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28714 and DW_OP_addr couldn't be resolved. resolve_addr has already
28715 removed the DW_AT_location attribute. This function attempts to
28716 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28717 to it or DW_AT_const_value attribute, if possible. */
28718
28719 static void
28720 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28721 {
28722 if (!VAR_P (decl)
28723 || lookup_decl_die (decl) != die
28724 || DECL_EXTERNAL (decl)
28725 || !TREE_STATIC (decl)
28726 || DECL_INITIAL (decl) == NULL_TREE
28727 || DECL_P (DECL_INITIAL (decl))
28728 || get_AT (die, DW_AT_const_value))
28729 return;
28730
28731 tree init = DECL_INITIAL (decl);
28732 HOST_WIDE_INT offset = 0;
28733 /* For variables that have been optimized away and thus
28734 don't have a memory location, see if we can emit
28735 DW_AT_const_value instead. */
28736 if (tree_add_const_value_attribute (die, init))
28737 return;
28738 if (dwarf_strict && dwarf_version < 5)
28739 return;
28740 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28741 and ADDR_EXPR refers to a decl that has DW_AT_location or
28742 DW_AT_const_value (but isn't addressable, otherwise
28743 resolving the original DW_OP_addr wouldn't fail), see if
28744 we can add DW_OP_implicit_pointer. */
28745 STRIP_NOPS (init);
28746 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28747 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28748 {
28749 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28750 init = TREE_OPERAND (init, 0);
28751 STRIP_NOPS (init);
28752 }
28753 if (TREE_CODE (init) != ADDR_EXPR)
28754 return;
28755 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28756 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28757 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28758 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28759 && TREE_OPERAND (init, 0) != decl))
28760 {
28761 dw_die_ref ref;
28762 dw_loc_descr_ref l;
28763
28764 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28765 {
28766 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28767 if (!rtl)
28768 return;
28769 decl = SYMBOL_REF_DECL (rtl);
28770 }
28771 else
28772 decl = TREE_OPERAND (init, 0);
28773 ref = lookup_decl_die (decl);
28774 if (ref == NULL
28775 || (!get_AT (ref, DW_AT_location)
28776 && !get_AT (ref, DW_AT_const_value)))
28777 return;
28778 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28779 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28780 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28781 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28782 add_AT_loc (die, DW_AT_location, l);
28783 }
28784 }
28785
28786 /* Return NULL if l is a DWARF expression, or first op that is not
28787 valid DWARF expression. */
28788
28789 static dw_loc_descr_ref
28790 non_dwarf_expression (dw_loc_descr_ref l)
28791 {
28792 while (l)
28793 {
28794 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28795 return l;
28796 switch (l->dw_loc_opc)
28797 {
28798 case DW_OP_regx:
28799 case DW_OP_implicit_value:
28800 case DW_OP_stack_value:
28801 case DW_OP_implicit_pointer:
28802 case DW_OP_GNU_implicit_pointer:
28803 case DW_OP_GNU_parameter_ref:
28804 case DW_OP_piece:
28805 case DW_OP_bit_piece:
28806 return l;
28807 default:
28808 break;
28809 }
28810 l = l->dw_loc_next;
28811 }
28812 return NULL;
28813 }
28814
28815 /* Return adjusted copy of EXPR:
28816 If it is empty DWARF expression, return it.
28817 If it is valid non-empty DWARF expression,
28818 return copy of EXPR with DW_OP_deref appended to it.
28819 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28820 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28821 If it is DWARF expression followed by DW_OP_stack_value, return
28822 copy of the DWARF expression without anything appended.
28823 Otherwise, return NULL. */
28824
28825 static dw_loc_descr_ref
28826 copy_deref_exprloc (dw_loc_descr_ref expr)
28827 {
28828 dw_loc_descr_ref tail = NULL;
28829
28830 if (expr == NULL)
28831 return NULL;
28832
28833 dw_loc_descr_ref l = non_dwarf_expression (expr);
28834 if (l && l->dw_loc_next)
28835 return NULL;
28836
28837 if (l)
28838 {
28839 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28840 tail = new_loc_descr ((enum dwarf_location_atom)
28841 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28842 0, 0);
28843 else
28844 switch (l->dw_loc_opc)
28845 {
28846 case DW_OP_regx:
28847 tail = new_loc_descr (DW_OP_bregx,
28848 l->dw_loc_oprnd1.v.val_unsigned, 0);
28849 break;
28850 case DW_OP_stack_value:
28851 break;
28852 default:
28853 return NULL;
28854 }
28855 }
28856 else
28857 tail = new_loc_descr (DW_OP_deref, 0, 0);
28858
28859 dw_loc_descr_ref ret = NULL, *p = &ret;
28860 while (expr != l)
28861 {
28862 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28863 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28864 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28865 p = &(*p)->dw_loc_next;
28866 expr = expr->dw_loc_next;
28867 }
28868 *p = tail;
28869 return ret;
28870 }
28871
28872 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28873 reference to a variable or argument, adjust it if needed and return:
28874 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28875 attribute if present should be removed
28876 0 keep the attribute perhaps with minor modifications, no need to rescan
28877 1 if the attribute has been successfully adjusted. */
28878
28879 static int
28880 optimize_string_length (dw_attr_node *a)
28881 {
28882 dw_loc_descr_ref l = AT_loc (a), lv;
28883 dw_die_ref die;
28884 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28885 {
28886 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28887 die = lookup_decl_die (decl);
28888 if (die)
28889 {
28890 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28891 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28892 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28893 }
28894 else
28895 return -1;
28896 }
28897 else
28898 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28899
28900 /* DWARF5 allows reference class, so we can then reference the DIE.
28901 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28902 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28903 {
28904 a->dw_attr_val.val_class = dw_val_class_die_ref;
28905 a->dw_attr_val.val_entry = NULL;
28906 a->dw_attr_val.v.val_die_ref.die = die;
28907 a->dw_attr_val.v.val_die_ref.external = 0;
28908 return 0;
28909 }
28910
28911 dw_attr_node *av = get_AT (die, DW_AT_location);
28912 dw_loc_list_ref d;
28913 bool non_dwarf_expr = false;
28914
28915 if (av == NULL)
28916 return dwarf_strict ? -1 : 0;
28917 switch (AT_class (av))
28918 {
28919 case dw_val_class_loc_list:
28920 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28921 if (d->expr && non_dwarf_expression (d->expr))
28922 non_dwarf_expr = true;
28923 break;
28924 case dw_val_class_loc:
28925 lv = AT_loc (av);
28926 if (lv == NULL)
28927 return dwarf_strict ? -1 : 0;
28928 if (non_dwarf_expression (lv))
28929 non_dwarf_expr = true;
28930 break;
28931 default:
28932 return dwarf_strict ? -1 : 0;
28933 }
28934
28935 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28936 into DW_OP_call4 or DW_OP_GNU_variable_value into
28937 DW_OP_call4 DW_OP_deref, do so. */
28938 if (!non_dwarf_expr
28939 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28940 {
28941 l->dw_loc_opc = DW_OP_call4;
28942 if (l->dw_loc_next)
28943 l->dw_loc_next = NULL;
28944 else
28945 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28946 return 0;
28947 }
28948
28949 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28950 copy over the DW_AT_location attribute from die to a. */
28951 if (l->dw_loc_next != NULL)
28952 {
28953 a->dw_attr_val = av->dw_attr_val;
28954 return 1;
28955 }
28956
28957 dw_loc_list_ref list, *p;
28958 switch (AT_class (av))
28959 {
28960 case dw_val_class_loc_list:
28961 p = &list;
28962 list = NULL;
28963 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28964 {
28965 lv = copy_deref_exprloc (d->expr);
28966 if (lv)
28967 {
28968 *p = new_loc_list (lv, d->begin, d->end, d->section);
28969 p = &(*p)->dw_loc_next;
28970 }
28971 else if (!dwarf_strict && d->expr)
28972 return 0;
28973 }
28974 if (list == NULL)
28975 return dwarf_strict ? -1 : 0;
28976 a->dw_attr_val.val_class = dw_val_class_loc_list;
28977 gen_llsym (list);
28978 *AT_loc_list_ptr (a) = list;
28979 return 1;
28980 case dw_val_class_loc:
28981 lv = copy_deref_exprloc (AT_loc (av));
28982 if (lv == NULL)
28983 return dwarf_strict ? -1 : 0;
28984 a->dw_attr_val.v.val_loc = lv;
28985 return 1;
28986 default:
28987 gcc_unreachable ();
28988 }
28989 }
28990
28991 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28992 an address in .rodata section if the string literal is emitted there,
28993 or remove the containing location list or replace DW_AT_const_value
28994 with DW_AT_location and empty location expression, if it isn't found
28995 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28996 to something that has been emitted in the current CU. */
28997
28998 static void
28999 resolve_addr (dw_die_ref die)
29000 {
29001 dw_die_ref c;
29002 dw_attr_node *a;
29003 dw_loc_list_ref *curr, *start, loc;
29004 unsigned ix;
29005 bool remove_AT_byte_size = false;
29006
29007 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29008 switch (AT_class (a))
29009 {
29010 case dw_val_class_loc_list:
29011 start = curr = AT_loc_list_ptr (a);
29012 loc = *curr;
29013 gcc_assert (loc);
29014 /* The same list can be referenced more than once. See if we have
29015 already recorded the result from a previous pass. */
29016 if (loc->replaced)
29017 *curr = loc->dw_loc_next;
29018 else if (!loc->resolved_addr)
29019 {
29020 /* As things stand, we do not expect or allow one die to
29021 reference a suffix of another die's location list chain.
29022 References must be identical or completely separate.
29023 There is therefore no need to cache the result of this
29024 pass on any list other than the first; doing so
29025 would lead to unnecessary writes. */
29026 while (*curr)
29027 {
29028 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29029 if (!resolve_addr_in_expr (a, (*curr)->expr))
29030 {
29031 dw_loc_list_ref next = (*curr)->dw_loc_next;
29032 dw_loc_descr_ref l = (*curr)->expr;
29033
29034 if (next && (*curr)->ll_symbol)
29035 {
29036 gcc_assert (!next->ll_symbol);
29037 next->ll_symbol = (*curr)->ll_symbol;
29038 }
29039 if (dwarf_split_debug_info)
29040 remove_loc_list_addr_table_entries (l);
29041 *curr = next;
29042 }
29043 else
29044 {
29045 mark_base_types ((*curr)->expr);
29046 curr = &(*curr)->dw_loc_next;
29047 }
29048 }
29049 if (loc == *start)
29050 loc->resolved_addr = 1;
29051 else
29052 {
29053 loc->replaced = 1;
29054 loc->dw_loc_next = *start;
29055 }
29056 }
29057 if (!*start)
29058 {
29059 remove_AT (die, a->dw_attr);
29060 ix--;
29061 }
29062 break;
29063 case dw_val_class_loc:
29064 {
29065 dw_loc_descr_ref l = AT_loc (a);
29066 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29067 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29068 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29069 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29070 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29071 with DW_FORM_ref referencing the same DIE as
29072 DW_OP_GNU_variable_value used to reference. */
29073 if (a->dw_attr == DW_AT_string_length
29074 && l
29075 && l->dw_loc_opc == DW_OP_GNU_variable_value
29076 && (l->dw_loc_next == NULL
29077 || (l->dw_loc_next->dw_loc_next == NULL
29078 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29079 {
29080 switch (optimize_string_length (a))
29081 {
29082 case -1:
29083 remove_AT (die, a->dw_attr);
29084 ix--;
29085 /* If we drop DW_AT_string_length, we need to drop also
29086 DW_AT_{string_length_,}byte_size. */
29087 remove_AT_byte_size = true;
29088 continue;
29089 default:
29090 break;
29091 case 1:
29092 /* Even if we keep the optimized DW_AT_string_length,
29093 it might have changed AT_class, so process it again. */
29094 ix--;
29095 continue;
29096 }
29097 }
29098 /* For -gdwarf-2 don't attempt to optimize
29099 DW_AT_data_member_location containing
29100 DW_OP_plus_uconst - older consumers might
29101 rely on it being that op instead of a more complex,
29102 but shorter, location description. */
29103 if ((dwarf_version > 2
29104 || a->dw_attr != DW_AT_data_member_location
29105 || l == NULL
29106 || l->dw_loc_opc != DW_OP_plus_uconst
29107 || l->dw_loc_next != NULL)
29108 && !resolve_addr_in_expr (a, l))
29109 {
29110 if (dwarf_split_debug_info)
29111 remove_loc_list_addr_table_entries (l);
29112 if (l != NULL
29113 && l->dw_loc_next == NULL
29114 && l->dw_loc_opc == DW_OP_addr
29115 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29116 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29117 && a->dw_attr == DW_AT_location)
29118 {
29119 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29120 remove_AT (die, a->dw_attr);
29121 ix--;
29122 optimize_location_into_implicit_ptr (die, decl);
29123 break;
29124 }
29125 if (a->dw_attr == DW_AT_string_length)
29126 /* If we drop DW_AT_string_length, we need to drop also
29127 DW_AT_{string_length_,}byte_size. */
29128 remove_AT_byte_size = true;
29129 remove_AT (die, a->dw_attr);
29130 ix--;
29131 }
29132 else
29133 mark_base_types (l);
29134 }
29135 break;
29136 case dw_val_class_addr:
29137 if (a->dw_attr == DW_AT_const_value
29138 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29139 {
29140 if (AT_index (a) != NOT_INDEXED)
29141 remove_addr_table_entry (a->dw_attr_val.val_entry);
29142 remove_AT (die, a->dw_attr);
29143 ix--;
29144 }
29145 if ((die->die_tag == DW_TAG_call_site
29146 && a->dw_attr == DW_AT_call_origin)
29147 || (die->die_tag == DW_TAG_GNU_call_site
29148 && a->dw_attr == DW_AT_abstract_origin))
29149 {
29150 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29151 dw_die_ref tdie = lookup_decl_die (tdecl);
29152 dw_die_ref cdie;
29153 if (tdie == NULL
29154 && DECL_EXTERNAL (tdecl)
29155 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29156 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29157 {
29158 dw_die_ref pdie = cdie;
29159 /* Make sure we don't add these DIEs into type units.
29160 We could emit skeleton DIEs for context (namespaces,
29161 outer structs/classes) and a skeleton DIE for the
29162 innermost context with DW_AT_signature pointing to the
29163 type unit. See PR78835. */
29164 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29165 pdie = pdie->die_parent;
29166 if (pdie == NULL)
29167 {
29168 /* Creating a full DIE for tdecl is overly expensive and
29169 at this point even wrong when in the LTO phase
29170 as it can end up generating new type DIEs we didn't
29171 output and thus optimize_external_refs will crash. */
29172 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29173 add_AT_flag (tdie, DW_AT_external, 1);
29174 add_AT_flag (tdie, DW_AT_declaration, 1);
29175 add_linkage_attr (tdie, tdecl);
29176 add_name_and_src_coords_attributes (tdie, tdecl, true);
29177 equate_decl_number_to_die (tdecl, tdie);
29178 }
29179 }
29180 if (tdie)
29181 {
29182 a->dw_attr_val.val_class = dw_val_class_die_ref;
29183 a->dw_attr_val.v.val_die_ref.die = tdie;
29184 a->dw_attr_val.v.val_die_ref.external = 0;
29185 }
29186 else
29187 {
29188 if (AT_index (a) != NOT_INDEXED)
29189 remove_addr_table_entry (a->dw_attr_val.val_entry);
29190 remove_AT (die, a->dw_attr);
29191 ix--;
29192 }
29193 }
29194 break;
29195 default:
29196 break;
29197 }
29198
29199 if (remove_AT_byte_size)
29200 remove_AT (die, dwarf_version >= 5
29201 ? DW_AT_string_length_byte_size
29202 : DW_AT_byte_size);
29203
29204 FOR_EACH_CHILD (die, c, resolve_addr (c));
29205 }
29206 \f
29207 /* Helper routines for optimize_location_lists.
29208 This pass tries to share identical local lists in .debug_loc
29209 section. */
29210
29211 /* Iteratively hash operands of LOC opcode into HSTATE. */
29212
29213 static void
29214 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29215 {
29216 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29217 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29218
29219 switch (loc->dw_loc_opc)
29220 {
29221 case DW_OP_const4u:
29222 case DW_OP_const8u:
29223 if (loc->dtprel)
29224 goto hash_addr;
29225 /* FALLTHRU */
29226 case DW_OP_const1u:
29227 case DW_OP_const1s:
29228 case DW_OP_const2u:
29229 case DW_OP_const2s:
29230 case DW_OP_const4s:
29231 case DW_OP_const8s:
29232 case DW_OP_constu:
29233 case DW_OP_consts:
29234 case DW_OP_pick:
29235 case DW_OP_plus_uconst:
29236 case DW_OP_breg0:
29237 case DW_OP_breg1:
29238 case DW_OP_breg2:
29239 case DW_OP_breg3:
29240 case DW_OP_breg4:
29241 case DW_OP_breg5:
29242 case DW_OP_breg6:
29243 case DW_OP_breg7:
29244 case DW_OP_breg8:
29245 case DW_OP_breg9:
29246 case DW_OP_breg10:
29247 case DW_OP_breg11:
29248 case DW_OP_breg12:
29249 case DW_OP_breg13:
29250 case DW_OP_breg14:
29251 case DW_OP_breg15:
29252 case DW_OP_breg16:
29253 case DW_OP_breg17:
29254 case DW_OP_breg18:
29255 case DW_OP_breg19:
29256 case DW_OP_breg20:
29257 case DW_OP_breg21:
29258 case DW_OP_breg22:
29259 case DW_OP_breg23:
29260 case DW_OP_breg24:
29261 case DW_OP_breg25:
29262 case DW_OP_breg26:
29263 case DW_OP_breg27:
29264 case DW_OP_breg28:
29265 case DW_OP_breg29:
29266 case DW_OP_breg30:
29267 case DW_OP_breg31:
29268 case DW_OP_regx:
29269 case DW_OP_fbreg:
29270 case DW_OP_piece:
29271 case DW_OP_deref_size:
29272 case DW_OP_xderef_size:
29273 hstate.add_object (val1->v.val_int);
29274 break;
29275 case DW_OP_skip:
29276 case DW_OP_bra:
29277 {
29278 int offset;
29279
29280 gcc_assert (val1->val_class == dw_val_class_loc);
29281 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29282 hstate.add_object (offset);
29283 }
29284 break;
29285 case DW_OP_implicit_value:
29286 hstate.add_object (val1->v.val_unsigned);
29287 switch (val2->val_class)
29288 {
29289 case dw_val_class_const:
29290 hstate.add_object (val2->v.val_int);
29291 break;
29292 case dw_val_class_vec:
29293 {
29294 unsigned int elt_size = val2->v.val_vec.elt_size;
29295 unsigned int len = val2->v.val_vec.length;
29296
29297 hstate.add_int (elt_size);
29298 hstate.add_int (len);
29299 hstate.add (val2->v.val_vec.array, len * elt_size);
29300 }
29301 break;
29302 case dw_val_class_const_double:
29303 hstate.add_object (val2->v.val_double.low);
29304 hstate.add_object (val2->v.val_double.high);
29305 break;
29306 case dw_val_class_wide_int:
29307 hstate.add (val2->v.val_wide->get_val (),
29308 get_full_len (*val2->v.val_wide)
29309 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29310 break;
29311 case dw_val_class_addr:
29312 inchash::add_rtx (val2->v.val_addr, hstate);
29313 break;
29314 default:
29315 gcc_unreachable ();
29316 }
29317 break;
29318 case DW_OP_bregx:
29319 case DW_OP_bit_piece:
29320 hstate.add_object (val1->v.val_int);
29321 hstate.add_object (val2->v.val_int);
29322 break;
29323 case DW_OP_addr:
29324 hash_addr:
29325 if (loc->dtprel)
29326 {
29327 unsigned char dtprel = 0xd1;
29328 hstate.add_object (dtprel);
29329 }
29330 inchash::add_rtx (val1->v.val_addr, hstate);
29331 break;
29332 case DW_OP_GNU_addr_index:
29333 case DW_OP_GNU_const_index:
29334 {
29335 if (loc->dtprel)
29336 {
29337 unsigned char dtprel = 0xd1;
29338 hstate.add_object (dtprel);
29339 }
29340 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29341 }
29342 break;
29343 case DW_OP_implicit_pointer:
29344 case DW_OP_GNU_implicit_pointer:
29345 hstate.add_int (val2->v.val_int);
29346 break;
29347 case DW_OP_entry_value:
29348 case DW_OP_GNU_entry_value:
29349 hstate.add_object (val1->v.val_loc);
29350 break;
29351 case DW_OP_regval_type:
29352 case DW_OP_deref_type:
29353 case DW_OP_GNU_regval_type:
29354 case DW_OP_GNU_deref_type:
29355 {
29356 unsigned int byte_size
29357 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29358 unsigned int encoding
29359 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29360 hstate.add_object (val1->v.val_int);
29361 hstate.add_object (byte_size);
29362 hstate.add_object (encoding);
29363 }
29364 break;
29365 case DW_OP_convert:
29366 case DW_OP_reinterpret:
29367 case DW_OP_GNU_convert:
29368 case DW_OP_GNU_reinterpret:
29369 if (val1->val_class == dw_val_class_unsigned_const)
29370 {
29371 hstate.add_object (val1->v.val_unsigned);
29372 break;
29373 }
29374 /* FALLTHRU */
29375 case DW_OP_const_type:
29376 case DW_OP_GNU_const_type:
29377 {
29378 unsigned int byte_size
29379 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29380 unsigned int encoding
29381 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29382 hstate.add_object (byte_size);
29383 hstate.add_object (encoding);
29384 if (loc->dw_loc_opc != DW_OP_const_type
29385 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29386 break;
29387 hstate.add_object (val2->val_class);
29388 switch (val2->val_class)
29389 {
29390 case dw_val_class_const:
29391 hstate.add_object (val2->v.val_int);
29392 break;
29393 case dw_val_class_vec:
29394 {
29395 unsigned int elt_size = val2->v.val_vec.elt_size;
29396 unsigned int len = val2->v.val_vec.length;
29397
29398 hstate.add_object (elt_size);
29399 hstate.add_object (len);
29400 hstate.add (val2->v.val_vec.array, len * elt_size);
29401 }
29402 break;
29403 case dw_val_class_const_double:
29404 hstate.add_object (val2->v.val_double.low);
29405 hstate.add_object (val2->v.val_double.high);
29406 break;
29407 case dw_val_class_wide_int:
29408 hstate.add (val2->v.val_wide->get_val (),
29409 get_full_len (*val2->v.val_wide)
29410 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29411 break;
29412 default:
29413 gcc_unreachable ();
29414 }
29415 }
29416 break;
29417
29418 default:
29419 /* Other codes have no operands. */
29420 break;
29421 }
29422 }
29423
29424 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29425
29426 static inline void
29427 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29428 {
29429 dw_loc_descr_ref l;
29430 bool sizes_computed = false;
29431 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29432 size_of_locs (loc);
29433
29434 for (l = loc; l != NULL; l = l->dw_loc_next)
29435 {
29436 enum dwarf_location_atom opc = l->dw_loc_opc;
29437 hstate.add_object (opc);
29438 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29439 {
29440 size_of_locs (loc);
29441 sizes_computed = true;
29442 }
29443 hash_loc_operands (l, hstate);
29444 }
29445 }
29446
29447 /* Compute hash of the whole location list LIST_HEAD. */
29448
29449 static inline void
29450 hash_loc_list (dw_loc_list_ref list_head)
29451 {
29452 dw_loc_list_ref curr = list_head;
29453 inchash::hash hstate;
29454
29455 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29456 {
29457 hstate.add (curr->begin, strlen (curr->begin) + 1);
29458 hstate.add (curr->end, strlen (curr->end) + 1);
29459 if (curr->section)
29460 hstate.add (curr->section, strlen (curr->section) + 1);
29461 hash_locs (curr->expr, hstate);
29462 }
29463 list_head->hash = hstate.end ();
29464 }
29465
29466 /* Return true if X and Y opcodes have the same operands. */
29467
29468 static inline bool
29469 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29470 {
29471 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29472 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29473 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29474 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29475
29476 switch (x->dw_loc_opc)
29477 {
29478 case DW_OP_const4u:
29479 case DW_OP_const8u:
29480 if (x->dtprel)
29481 goto hash_addr;
29482 /* FALLTHRU */
29483 case DW_OP_const1u:
29484 case DW_OP_const1s:
29485 case DW_OP_const2u:
29486 case DW_OP_const2s:
29487 case DW_OP_const4s:
29488 case DW_OP_const8s:
29489 case DW_OP_constu:
29490 case DW_OP_consts:
29491 case DW_OP_pick:
29492 case DW_OP_plus_uconst:
29493 case DW_OP_breg0:
29494 case DW_OP_breg1:
29495 case DW_OP_breg2:
29496 case DW_OP_breg3:
29497 case DW_OP_breg4:
29498 case DW_OP_breg5:
29499 case DW_OP_breg6:
29500 case DW_OP_breg7:
29501 case DW_OP_breg8:
29502 case DW_OP_breg9:
29503 case DW_OP_breg10:
29504 case DW_OP_breg11:
29505 case DW_OP_breg12:
29506 case DW_OP_breg13:
29507 case DW_OP_breg14:
29508 case DW_OP_breg15:
29509 case DW_OP_breg16:
29510 case DW_OP_breg17:
29511 case DW_OP_breg18:
29512 case DW_OP_breg19:
29513 case DW_OP_breg20:
29514 case DW_OP_breg21:
29515 case DW_OP_breg22:
29516 case DW_OP_breg23:
29517 case DW_OP_breg24:
29518 case DW_OP_breg25:
29519 case DW_OP_breg26:
29520 case DW_OP_breg27:
29521 case DW_OP_breg28:
29522 case DW_OP_breg29:
29523 case DW_OP_breg30:
29524 case DW_OP_breg31:
29525 case DW_OP_regx:
29526 case DW_OP_fbreg:
29527 case DW_OP_piece:
29528 case DW_OP_deref_size:
29529 case DW_OP_xderef_size:
29530 return valx1->v.val_int == valy1->v.val_int;
29531 case DW_OP_skip:
29532 case DW_OP_bra:
29533 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29534 can cause irrelevant differences in dw_loc_addr. */
29535 gcc_assert (valx1->val_class == dw_val_class_loc
29536 && valy1->val_class == dw_val_class_loc
29537 && (dwarf_split_debug_info
29538 || x->dw_loc_addr == y->dw_loc_addr));
29539 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29540 case DW_OP_implicit_value:
29541 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29542 || valx2->val_class != valy2->val_class)
29543 return false;
29544 switch (valx2->val_class)
29545 {
29546 case dw_val_class_const:
29547 return valx2->v.val_int == valy2->v.val_int;
29548 case dw_val_class_vec:
29549 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29550 && valx2->v.val_vec.length == valy2->v.val_vec.length
29551 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29552 valx2->v.val_vec.elt_size
29553 * valx2->v.val_vec.length) == 0;
29554 case dw_val_class_const_double:
29555 return valx2->v.val_double.low == valy2->v.val_double.low
29556 && valx2->v.val_double.high == valy2->v.val_double.high;
29557 case dw_val_class_wide_int:
29558 return *valx2->v.val_wide == *valy2->v.val_wide;
29559 case dw_val_class_addr:
29560 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29561 default:
29562 gcc_unreachable ();
29563 }
29564 case DW_OP_bregx:
29565 case DW_OP_bit_piece:
29566 return valx1->v.val_int == valy1->v.val_int
29567 && valx2->v.val_int == valy2->v.val_int;
29568 case DW_OP_addr:
29569 hash_addr:
29570 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29571 case DW_OP_GNU_addr_index:
29572 case DW_OP_GNU_const_index:
29573 {
29574 rtx ax1 = valx1->val_entry->addr.rtl;
29575 rtx ay1 = valy1->val_entry->addr.rtl;
29576 return rtx_equal_p (ax1, ay1);
29577 }
29578 case DW_OP_implicit_pointer:
29579 case DW_OP_GNU_implicit_pointer:
29580 return valx1->val_class == dw_val_class_die_ref
29581 && valx1->val_class == valy1->val_class
29582 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29583 && valx2->v.val_int == valy2->v.val_int;
29584 case DW_OP_entry_value:
29585 case DW_OP_GNU_entry_value:
29586 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29587 case DW_OP_const_type:
29588 case DW_OP_GNU_const_type:
29589 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29590 || valx2->val_class != valy2->val_class)
29591 return false;
29592 switch (valx2->val_class)
29593 {
29594 case dw_val_class_const:
29595 return valx2->v.val_int == valy2->v.val_int;
29596 case dw_val_class_vec:
29597 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29598 && valx2->v.val_vec.length == valy2->v.val_vec.length
29599 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29600 valx2->v.val_vec.elt_size
29601 * valx2->v.val_vec.length) == 0;
29602 case dw_val_class_const_double:
29603 return valx2->v.val_double.low == valy2->v.val_double.low
29604 && valx2->v.val_double.high == valy2->v.val_double.high;
29605 case dw_val_class_wide_int:
29606 return *valx2->v.val_wide == *valy2->v.val_wide;
29607 default:
29608 gcc_unreachable ();
29609 }
29610 case DW_OP_regval_type:
29611 case DW_OP_deref_type:
29612 case DW_OP_GNU_regval_type:
29613 case DW_OP_GNU_deref_type:
29614 return valx1->v.val_int == valy1->v.val_int
29615 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29616 case DW_OP_convert:
29617 case DW_OP_reinterpret:
29618 case DW_OP_GNU_convert:
29619 case DW_OP_GNU_reinterpret:
29620 if (valx1->val_class != valy1->val_class)
29621 return false;
29622 if (valx1->val_class == dw_val_class_unsigned_const)
29623 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29624 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29625 case DW_OP_GNU_parameter_ref:
29626 return valx1->val_class == dw_val_class_die_ref
29627 && valx1->val_class == valy1->val_class
29628 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29629 default:
29630 /* Other codes have no operands. */
29631 return true;
29632 }
29633 }
29634
29635 /* Return true if DWARF location expressions X and Y are the same. */
29636
29637 static inline bool
29638 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29639 {
29640 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29641 if (x->dw_loc_opc != y->dw_loc_opc
29642 || x->dtprel != y->dtprel
29643 || !compare_loc_operands (x, y))
29644 break;
29645 return x == NULL && y == NULL;
29646 }
29647
29648 /* Hashtable helpers. */
29649
29650 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29651 {
29652 static inline hashval_t hash (const dw_loc_list_struct *);
29653 static inline bool equal (const dw_loc_list_struct *,
29654 const dw_loc_list_struct *);
29655 };
29656
29657 /* Return precomputed hash of location list X. */
29658
29659 inline hashval_t
29660 loc_list_hasher::hash (const dw_loc_list_struct *x)
29661 {
29662 return x->hash;
29663 }
29664
29665 /* Return true if location lists A and B are the same. */
29666
29667 inline bool
29668 loc_list_hasher::equal (const dw_loc_list_struct *a,
29669 const dw_loc_list_struct *b)
29670 {
29671 if (a == b)
29672 return 1;
29673 if (a->hash != b->hash)
29674 return 0;
29675 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29676 if (strcmp (a->begin, b->begin) != 0
29677 || strcmp (a->end, b->end) != 0
29678 || (a->section == NULL) != (b->section == NULL)
29679 || (a->section && strcmp (a->section, b->section) != 0)
29680 || !compare_locs (a->expr, b->expr))
29681 break;
29682 return a == NULL && b == NULL;
29683 }
29684
29685 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29686
29687
29688 /* Recursively optimize location lists referenced from DIE
29689 children and share them whenever possible. */
29690
29691 static void
29692 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29693 {
29694 dw_die_ref c;
29695 dw_attr_node *a;
29696 unsigned ix;
29697 dw_loc_list_struct **slot;
29698
29699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29700 if (AT_class (a) == dw_val_class_loc_list)
29701 {
29702 dw_loc_list_ref list = AT_loc_list (a);
29703 /* TODO: perform some optimizations here, before hashing
29704 it and storing into the hash table. */
29705 hash_loc_list (list);
29706 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29707 if (*slot == NULL)
29708 *slot = list;
29709 else
29710 a->dw_attr_val.v.val_loc_list = *slot;
29711 }
29712
29713 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29714 }
29715
29716
29717 /* Recursively assign each location list a unique index into the debug_addr
29718 section. */
29719
29720 static void
29721 index_location_lists (dw_die_ref die)
29722 {
29723 dw_die_ref c;
29724 dw_attr_node *a;
29725 unsigned ix;
29726
29727 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29728 if (AT_class (a) == dw_val_class_loc_list)
29729 {
29730 dw_loc_list_ref list = AT_loc_list (a);
29731 dw_loc_list_ref curr;
29732 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29733 {
29734 /* Don't index an entry that has already been indexed
29735 or won't be output. */
29736 if (curr->begin_entry != NULL
29737 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29738 continue;
29739
29740 curr->begin_entry
29741 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29742 }
29743 }
29744
29745 FOR_EACH_CHILD (die, c, index_location_lists (c));
29746 }
29747
29748 /* Optimize location lists referenced from DIE
29749 children and share them whenever possible. */
29750
29751 static void
29752 optimize_location_lists (dw_die_ref die)
29753 {
29754 loc_list_hash_type htab (500);
29755 optimize_location_lists_1 (die, &htab);
29756 }
29757 \f
29758 /* Traverse the limbo die list, and add parent/child links. The only
29759 dies without parents that should be here are concrete instances of
29760 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29761 For concrete instances, we can get the parent die from the abstract
29762 instance. */
29763
29764 static void
29765 flush_limbo_die_list (void)
29766 {
29767 limbo_die_node *node;
29768
29769 /* get_context_die calls force_decl_die, which can put new DIEs on the
29770 limbo list in LTO mode when nested functions are put in a different
29771 partition than that of their parent function. */
29772 while ((node = limbo_die_list))
29773 {
29774 dw_die_ref die = node->die;
29775 limbo_die_list = node->next;
29776
29777 if (die->die_parent == NULL)
29778 {
29779 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29780
29781 if (origin && origin->die_parent)
29782 add_child_die (origin->die_parent, die);
29783 else if (is_cu_die (die))
29784 ;
29785 else if (seen_error ())
29786 /* It's OK to be confused by errors in the input. */
29787 add_child_die (comp_unit_die (), die);
29788 else
29789 {
29790 /* In certain situations, the lexical block containing a
29791 nested function can be optimized away, which results
29792 in the nested function die being orphaned. Likewise
29793 with the return type of that nested function. Force
29794 this to be a child of the containing function.
29795
29796 It may happen that even the containing function got fully
29797 inlined and optimized out. In that case we are lost and
29798 assign the empty child. This should not be big issue as
29799 the function is likely unreachable too. */
29800 gcc_assert (node->created_for);
29801
29802 if (DECL_P (node->created_for))
29803 origin = get_context_die (DECL_CONTEXT (node->created_for));
29804 else if (TYPE_P (node->created_for))
29805 origin = scope_die_for (node->created_for, comp_unit_die ());
29806 else
29807 origin = comp_unit_die ();
29808
29809 add_child_die (origin, die);
29810 }
29811 }
29812 }
29813 }
29814
29815 /* Reset DIEs so we can output them again. */
29816
29817 static void
29818 reset_dies (dw_die_ref die)
29819 {
29820 dw_die_ref c;
29821
29822 /* Remove stuff we re-generate. */
29823 die->die_mark = 0;
29824 die->die_offset = 0;
29825 die->die_abbrev = 0;
29826 remove_AT (die, DW_AT_sibling);
29827
29828 FOR_EACH_CHILD (die, c, reset_dies (c));
29829 }
29830
29831 /* Output stuff that dwarf requires at the end of every file,
29832 and generate the DWARF-2 debugging info. */
29833
29834 static void
29835 dwarf2out_finish (const char *)
29836 {
29837 comdat_type_node *ctnode;
29838 dw_die_ref main_comp_unit_die;
29839 unsigned char checksum[16];
29840 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29841
29842 /* Flush out any latecomers to the limbo party. */
29843 flush_limbo_die_list ();
29844
29845 if (flag_checking)
29846 {
29847 verify_die (comp_unit_die ());
29848 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29849 verify_die (node->die);
29850 }
29851
29852 /* We shouldn't have any symbols with delayed asm names for
29853 DIEs generated after early finish. */
29854 gcc_assert (deferred_asm_name == NULL);
29855
29856 gen_remaining_tmpl_value_param_die_attribute ();
29857
29858 if (flag_generate_lto || flag_generate_offload)
29859 {
29860 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29861
29862 /* Prune stuff so that dwarf2out_finish runs successfully
29863 for the fat part of the object. */
29864 reset_dies (comp_unit_die ());
29865 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29866 reset_dies (node->die);
29867
29868 hash_table<comdat_type_hasher> comdat_type_table (100);
29869 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29870 {
29871 comdat_type_node **slot
29872 = comdat_type_table.find_slot (ctnode, INSERT);
29873
29874 /* Don't reset types twice. */
29875 if (*slot != HTAB_EMPTY_ENTRY)
29876 continue;
29877
29878 /* Add a pointer to the line table for the main compilation unit
29879 so that the debugger can make sense of DW_AT_decl_file
29880 attributes. */
29881 if (debug_info_level >= DINFO_LEVEL_TERSE)
29882 reset_dies (ctnode->root_die);
29883
29884 *slot = ctnode;
29885 }
29886
29887 /* Reset die CU symbol so we don't output it twice. */
29888 comp_unit_die ()->die_id.die_symbol = NULL;
29889
29890 /* Remove DW_AT_macro from the early output. */
29891 if (have_macinfo)
29892 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29893
29894 /* Remove indirect string decisions. */
29895 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29896 }
29897
29898 #if ENABLE_ASSERT_CHECKING
29899 {
29900 dw_die_ref die = comp_unit_die (), c;
29901 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29902 }
29903 #endif
29904 resolve_addr (comp_unit_die ());
29905 move_marked_base_types ();
29906
29907 /* Initialize sections and labels used for actual assembler output. */
29908 unsigned generation = init_sections_and_labels (false);
29909
29910 /* Traverse the DIE's and add sibling attributes to those DIE's that
29911 have children. */
29912 add_sibling_attributes (comp_unit_die ());
29913 limbo_die_node *node;
29914 for (node = cu_die_list; node; node = node->next)
29915 add_sibling_attributes (node->die);
29916 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29917 add_sibling_attributes (ctnode->root_die);
29918
29919 /* When splitting DWARF info, we put some attributes in the
29920 skeleton compile_unit DIE that remains in the .o, while
29921 most attributes go in the DWO compile_unit_die. */
29922 if (dwarf_split_debug_info)
29923 {
29924 limbo_die_node *cu;
29925 main_comp_unit_die = gen_compile_unit_die (NULL);
29926 if (dwarf_version >= 5)
29927 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29928 cu = limbo_die_list;
29929 gcc_assert (cu->die == main_comp_unit_die);
29930 limbo_die_list = limbo_die_list->next;
29931 cu->next = cu_die_list;
29932 cu_die_list = cu;
29933 }
29934 else
29935 main_comp_unit_die = comp_unit_die ();
29936
29937 /* Output a terminator label for the .text section. */
29938 switch_to_section (text_section);
29939 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29940 if (cold_text_section)
29941 {
29942 switch_to_section (cold_text_section);
29943 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29944 }
29945
29946 /* We can only use the low/high_pc attributes if all of the code was
29947 in .text. */
29948 if (!have_multiple_function_sections
29949 || (dwarf_version < 3 && dwarf_strict))
29950 {
29951 /* Don't add if the CU has no associated code. */
29952 if (text_section_used)
29953 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29954 text_end_label, true);
29955 }
29956 else
29957 {
29958 unsigned fde_idx;
29959 dw_fde_ref fde;
29960 bool range_list_added = false;
29961
29962 if (text_section_used)
29963 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29964 text_end_label, &range_list_added, true);
29965 if (cold_text_section_used)
29966 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29967 cold_end_label, &range_list_added, true);
29968
29969 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29970 {
29971 if (DECL_IGNORED_P (fde->decl))
29972 continue;
29973 if (!fde->in_std_section)
29974 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29975 fde->dw_fde_end, &range_list_added,
29976 true);
29977 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29978 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29979 fde->dw_fde_second_end, &range_list_added,
29980 true);
29981 }
29982
29983 if (range_list_added)
29984 {
29985 /* We need to give .debug_loc and .debug_ranges an appropriate
29986 "base address". Use zero so that these addresses become
29987 absolute. Historically, we've emitted the unexpected
29988 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29989 Emit both to give time for other tools to adapt. */
29990 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29991 if (! dwarf_strict && dwarf_version < 4)
29992 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29993
29994 add_ranges (NULL);
29995 }
29996 }
29997
29998 /* AIX Assembler inserts the length, so adjust the reference to match the
29999 offset expected by debuggers. */
30000 strcpy (dl_section_ref, debug_line_section_label);
30001 if (XCOFF_DEBUGGING_INFO)
30002 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
30003
30004 if (debug_info_level >= DINFO_LEVEL_TERSE)
30005 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
30006 dl_section_ref);
30007
30008 if (have_macinfo)
30009 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30010 macinfo_section_label);
30011
30012 if (dwarf_split_debug_info)
30013 {
30014 if (have_location_lists)
30015 {
30016 if (dwarf_version >= 5)
30017 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30018 loc_section_label);
30019 /* optimize_location_lists calculates the size of the lists,
30020 so index them first, and assign indices to the entries.
30021 Although optimize_location_lists will remove entries from
30022 the table, it only does so for duplicates, and therefore
30023 only reduces ref_counts to 1. */
30024 index_location_lists (comp_unit_die ());
30025 }
30026
30027 if (addr_index_table != NULL)
30028 {
30029 unsigned int index = 0;
30030 addr_index_table
30031 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30032 (&index);
30033 }
30034 }
30035
30036 loc_list_idx = 0;
30037 if (have_location_lists)
30038 {
30039 optimize_location_lists (comp_unit_die ());
30040 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30041 if (dwarf_version >= 5 && dwarf_split_debug_info)
30042 assign_location_list_indexes (comp_unit_die ());
30043 }
30044
30045 save_macinfo_strings ();
30046
30047 if (dwarf_split_debug_info)
30048 {
30049 unsigned int index = 0;
30050
30051 /* Add attributes common to skeleton compile_units and
30052 type_units. Because these attributes include strings, it
30053 must be done before freezing the string table. Top-level
30054 skeleton die attrs are added when the skeleton type unit is
30055 created, so ensure it is created by this point. */
30056 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30057 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30058 }
30059
30060 /* Output all of the compilation units. We put the main one last so that
30061 the offsets are available to output_pubnames. */
30062 for (node = cu_die_list; node; node = node->next)
30063 output_comp_unit (node->die, 0, NULL);
30064
30065 hash_table<comdat_type_hasher> comdat_type_table (100);
30066 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30067 {
30068 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30069
30070 /* Don't output duplicate types. */
30071 if (*slot != HTAB_EMPTY_ENTRY)
30072 continue;
30073
30074 /* Add a pointer to the line table for the main compilation unit
30075 so that the debugger can make sense of DW_AT_decl_file
30076 attributes. */
30077 if (debug_info_level >= DINFO_LEVEL_TERSE)
30078 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30079 (!dwarf_split_debug_info
30080 ? dl_section_ref
30081 : debug_skeleton_line_section_label));
30082
30083 output_comdat_type_unit (ctnode);
30084 *slot = ctnode;
30085 }
30086
30087 if (dwarf_split_debug_info)
30088 {
30089 int mark;
30090 struct md5_ctx ctx;
30091
30092 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30093 index_rnglists ();
30094
30095 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30096 md5_init_ctx (&ctx);
30097 mark = 0;
30098 die_checksum (comp_unit_die (), &ctx, &mark);
30099 unmark_all_dies (comp_unit_die ());
30100 md5_finish_ctx (&ctx, checksum);
30101
30102 if (dwarf_version < 5)
30103 {
30104 /* Use the first 8 bytes of the checksum as the dwo_id,
30105 and add it to both comp-unit DIEs. */
30106 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30107 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30108 }
30109
30110 /* Add the base offset of the ranges table to the skeleton
30111 comp-unit DIE. */
30112 if (!vec_safe_is_empty (ranges_table))
30113 {
30114 if (dwarf_version >= 5)
30115 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30116 ranges_base_label);
30117 else
30118 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30119 ranges_section_label);
30120 }
30121
30122 switch_to_section (debug_addr_section);
30123 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30124 output_addr_table ();
30125 }
30126
30127 /* Output the main compilation unit if non-empty or if .debug_macinfo
30128 or .debug_macro will be emitted. */
30129 output_comp_unit (comp_unit_die (), have_macinfo,
30130 dwarf_split_debug_info ? checksum : NULL);
30131
30132 if (dwarf_split_debug_info && info_section_emitted)
30133 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30134
30135 /* Output the abbreviation table. */
30136 if (vec_safe_length (abbrev_die_table) != 1)
30137 {
30138 switch_to_section (debug_abbrev_section);
30139 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30140 output_abbrev_section ();
30141 }
30142
30143 /* Output location list section if necessary. */
30144 if (have_location_lists)
30145 {
30146 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30147 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30148 /* Output the location lists info. */
30149 switch_to_section (debug_loc_section);
30150 if (dwarf_version >= 5)
30151 {
30152 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30153 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30154 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30155 dw2_asm_output_data (4, 0xffffffff,
30156 "Initial length escape value indicating "
30157 "64-bit DWARF extension");
30158 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30159 "Length of Location Lists");
30160 ASM_OUTPUT_LABEL (asm_out_file, l1);
30161 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30162 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30163 dw2_asm_output_data (1, 0, "Segment Size");
30164 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30165 "Offset Entry Count");
30166 }
30167 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30168 if (dwarf_version >= 5 && dwarf_split_debug_info)
30169 {
30170 unsigned int save_loc_list_idx = loc_list_idx;
30171 loc_list_idx = 0;
30172 output_loclists_offsets (comp_unit_die ());
30173 gcc_assert (save_loc_list_idx == loc_list_idx);
30174 }
30175 output_location_lists (comp_unit_die ());
30176 if (dwarf_version >= 5)
30177 ASM_OUTPUT_LABEL (asm_out_file, l2);
30178 }
30179
30180 output_pubtables ();
30181
30182 /* Output the address range information if a CU (.debug_info section)
30183 was emitted. We output an empty table even if we had no functions
30184 to put in it. This because the consumer has no way to tell the
30185 difference between an empty table that we omitted and failure to
30186 generate a table that would have contained data. */
30187 if (info_section_emitted)
30188 {
30189 switch_to_section (debug_aranges_section);
30190 output_aranges ();
30191 }
30192
30193 /* Output ranges section if necessary. */
30194 if (!vec_safe_is_empty (ranges_table))
30195 {
30196 if (dwarf_version >= 5)
30197 output_rnglists (generation);
30198 else
30199 output_ranges ();
30200 }
30201
30202 /* Have to end the macro section. */
30203 if (have_macinfo)
30204 {
30205 switch_to_section (debug_macinfo_section);
30206 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30207 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30208 : debug_skeleton_line_section_label, false);
30209 dw2_asm_output_data (1, 0, "End compilation unit");
30210 }
30211
30212 /* Output the source line correspondence table. We must do this
30213 even if there is no line information. Otherwise, on an empty
30214 translation unit, we will generate a present, but empty,
30215 .debug_info section. IRIX 6.5 `nm' will then complain when
30216 examining the file. This is done late so that any filenames
30217 used by the debug_info section are marked as 'used'. */
30218 switch_to_section (debug_line_section);
30219 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30220 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30221 output_line_info (false);
30222
30223 if (dwarf_split_debug_info && info_section_emitted)
30224 {
30225 switch_to_section (debug_skeleton_line_section);
30226 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30227 output_line_info (true);
30228 }
30229
30230 /* If we emitted any indirect strings, output the string table too. */
30231 if (debug_str_hash || skeleton_debug_str_hash)
30232 output_indirect_strings ();
30233 if (debug_line_str_hash)
30234 {
30235 switch_to_section (debug_line_str_section);
30236 const enum dwarf_form form = DW_FORM_line_strp;
30237 debug_line_str_hash->traverse<enum dwarf_form,
30238 output_indirect_string> (form);
30239 }
30240 }
30241
30242 /* Returns a hash value for X (which really is a variable_value_struct). */
30243
30244 inline hashval_t
30245 variable_value_hasher::hash (variable_value_struct *x)
30246 {
30247 return (hashval_t) x->decl_id;
30248 }
30249
30250 /* Return nonzero if decl_id of variable_value_struct X is the same as
30251 UID of decl Y. */
30252
30253 inline bool
30254 variable_value_hasher::equal (variable_value_struct *x, tree y)
30255 {
30256 return x->decl_id == DECL_UID (y);
30257 }
30258
30259 /* Helper function for resolve_variable_value, handle
30260 DW_OP_GNU_variable_value in one location expression.
30261 Return true if exprloc has been changed into loclist. */
30262
30263 static bool
30264 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30265 {
30266 dw_loc_descr_ref next;
30267 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30268 {
30269 next = loc->dw_loc_next;
30270 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30271 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30272 continue;
30273
30274 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30275 if (DECL_CONTEXT (decl) != current_function_decl)
30276 continue;
30277
30278 dw_die_ref ref = lookup_decl_die (decl);
30279 if (ref)
30280 {
30281 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30282 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30283 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30284 continue;
30285 }
30286 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30287 if (l == NULL)
30288 continue;
30289 if (l->dw_loc_next)
30290 {
30291 if (AT_class (a) != dw_val_class_loc)
30292 continue;
30293 switch (a->dw_attr)
30294 {
30295 /* Following attributes allow both exprloc and loclist
30296 classes, so we can change them into a loclist. */
30297 case DW_AT_location:
30298 case DW_AT_string_length:
30299 case DW_AT_return_addr:
30300 case DW_AT_data_member_location:
30301 case DW_AT_frame_base:
30302 case DW_AT_segment:
30303 case DW_AT_static_link:
30304 case DW_AT_use_location:
30305 case DW_AT_vtable_elem_location:
30306 if (prev)
30307 {
30308 prev->dw_loc_next = NULL;
30309 prepend_loc_descr_to_each (l, AT_loc (a));
30310 }
30311 if (next)
30312 add_loc_descr_to_each (l, next);
30313 a->dw_attr_val.val_class = dw_val_class_loc_list;
30314 a->dw_attr_val.val_entry = NULL;
30315 a->dw_attr_val.v.val_loc_list = l;
30316 have_location_lists = true;
30317 return true;
30318 /* Following attributes allow both exprloc and reference,
30319 so if the whole expression is DW_OP_GNU_variable_value alone
30320 we could transform it into reference. */
30321 case DW_AT_byte_size:
30322 case DW_AT_bit_size:
30323 case DW_AT_lower_bound:
30324 case DW_AT_upper_bound:
30325 case DW_AT_bit_stride:
30326 case DW_AT_count:
30327 case DW_AT_allocated:
30328 case DW_AT_associated:
30329 case DW_AT_byte_stride:
30330 if (prev == NULL && next == NULL)
30331 break;
30332 /* FALLTHRU */
30333 default:
30334 if (dwarf_strict)
30335 continue;
30336 break;
30337 }
30338 /* Create DW_TAG_variable that we can refer to. */
30339 gen_decl_die (decl, NULL_TREE, NULL,
30340 lookup_decl_die (current_function_decl));
30341 ref = lookup_decl_die (decl);
30342 if (ref)
30343 {
30344 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30345 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30346 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30347 }
30348 continue;
30349 }
30350 if (prev)
30351 {
30352 prev->dw_loc_next = l->expr;
30353 add_loc_descr (&prev->dw_loc_next, next);
30354 free_loc_descr (loc, NULL);
30355 next = prev->dw_loc_next;
30356 }
30357 else
30358 {
30359 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30360 add_loc_descr (&loc, next);
30361 next = loc;
30362 }
30363 loc = prev;
30364 }
30365 return false;
30366 }
30367
30368 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30369
30370 static void
30371 resolve_variable_value (dw_die_ref die)
30372 {
30373 dw_attr_node *a;
30374 dw_loc_list_ref loc;
30375 unsigned ix;
30376
30377 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30378 switch (AT_class (a))
30379 {
30380 case dw_val_class_loc:
30381 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30382 break;
30383 /* FALLTHRU */
30384 case dw_val_class_loc_list:
30385 loc = AT_loc_list (a);
30386 gcc_assert (loc);
30387 for (; loc; loc = loc->dw_loc_next)
30388 resolve_variable_value_in_expr (a, loc->expr);
30389 break;
30390 default:
30391 break;
30392 }
30393 }
30394
30395 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30396 temporaries in the current function. */
30397
30398 static void
30399 resolve_variable_values (void)
30400 {
30401 if (!variable_value_hash || !current_function_decl)
30402 return;
30403
30404 struct variable_value_struct *node
30405 = variable_value_hash->find_with_hash (current_function_decl,
30406 DECL_UID (current_function_decl));
30407
30408 if (node == NULL)
30409 return;
30410
30411 unsigned int i;
30412 dw_die_ref die;
30413 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30414 resolve_variable_value (die);
30415 }
30416
30417 /* Helper function for note_variable_value, handle one location
30418 expression. */
30419
30420 static void
30421 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30422 {
30423 for (; loc; loc = loc->dw_loc_next)
30424 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30425 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30426 {
30427 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30428 dw_die_ref ref = lookup_decl_die (decl);
30429 if (! ref && (flag_generate_lto || flag_generate_offload))
30430 {
30431 /* ??? This is somewhat a hack because we do not create DIEs
30432 for variables not in BLOCK trees early but when generating
30433 early LTO output we need the dw_val_class_decl_ref to be
30434 fully resolved. For fat LTO objects we'd also like to
30435 undo this after LTO dwarf output. */
30436 gcc_assert (DECL_CONTEXT (decl));
30437 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30438 gcc_assert (ctx != NULL);
30439 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30440 ref = lookup_decl_die (decl);
30441 gcc_assert (ref != NULL);
30442 }
30443 if (ref)
30444 {
30445 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30446 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30447 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30448 continue;
30449 }
30450 if (VAR_P (decl)
30451 && DECL_CONTEXT (decl)
30452 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30453 && lookup_decl_die (DECL_CONTEXT (decl)))
30454 {
30455 if (!variable_value_hash)
30456 variable_value_hash
30457 = hash_table<variable_value_hasher>::create_ggc (10);
30458
30459 tree fndecl = DECL_CONTEXT (decl);
30460 struct variable_value_struct *node;
30461 struct variable_value_struct **slot
30462 = variable_value_hash->find_slot_with_hash (fndecl,
30463 DECL_UID (fndecl),
30464 INSERT);
30465 if (*slot == NULL)
30466 {
30467 node = ggc_cleared_alloc<variable_value_struct> ();
30468 node->decl_id = DECL_UID (fndecl);
30469 *slot = node;
30470 }
30471 else
30472 node = *slot;
30473
30474 vec_safe_push (node->dies, die);
30475 }
30476 }
30477 }
30478
30479 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30480 with dw_val_class_decl_ref operand. */
30481
30482 static void
30483 note_variable_value (dw_die_ref die)
30484 {
30485 dw_die_ref c;
30486 dw_attr_node *a;
30487 dw_loc_list_ref loc;
30488 unsigned ix;
30489
30490 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30491 switch (AT_class (a))
30492 {
30493 case dw_val_class_loc_list:
30494 loc = AT_loc_list (a);
30495 gcc_assert (loc);
30496 if (!loc->noted_variable_value)
30497 {
30498 loc->noted_variable_value = 1;
30499 for (; loc; loc = loc->dw_loc_next)
30500 note_variable_value_in_expr (die, loc->expr);
30501 }
30502 break;
30503 case dw_val_class_loc:
30504 note_variable_value_in_expr (die, AT_loc (a));
30505 break;
30506 default:
30507 break;
30508 }
30509
30510 /* Mark children. */
30511 FOR_EACH_CHILD (die, c, note_variable_value (c));
30512 }
30513
30514 /* Perform any cleanups needed after the early debug generation pass
30515 has run. */
30516
30517 static void
30518 dwarf2out_early_finish (const char *filename)
30519 {
30520 set_early_dwarf s;
30521
30522 /* PCH might result in DW_AT_producer string being restored from the
30523 header compilation, so always fill it with empty string initially
30524 and overwrite only here. */
30525 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30526 producer_string = gen_producer_string ();
30527 producer->dw_attr_val.v.val_str->refcount--;
30528 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30529
30530 /* Add the name for the main input file now. We delayed this from
30531 dwarf2out_init to avoid complications with PCH. */
30532 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30533 add_comp_dir_attribute (comp_unit_die ());
30534
30535 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30536 DW_AT_comp_dir into .debug_line_str section. */
30537 if (!DWARF2_ASM_LINE_DEBUG_INFO
30538 && dwarf_version >= 5
30539 && DWARF5_USE_DEBUG_LINE_STR)
30540 {
30541 for (int i = 0; i < 2; i++)
30542 {
30543 dw_attr_node *a = get_AT (comp_unit_die (),
30544 i ? DW_AT_comp_dir : DW_AT_name);
30545 if (a == NULL
30546 || AT_class (a) != dw_val_class_str
30547 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30548 continue;
30549
30550 if (! debug_line_str_hash)
30551 debug_line_str_hash
30552 = hash_table<indirect_string_hasher>::create_ggc (10);
30553
30554 struct indirect_string_node *node
30555 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30556 set_indirect_string (node);
30557 node->form = DW_FORM_line_strp;
30558 a->dw_attr_val.v.val_str->refcount--;
30559 a->dw_attr_val.v.val_str = node;
30560 }
30561 }
30562
30563 /* With LTO early dwarf was really finished at compile-time, so make
30564 sure to adjust the phase after annotating the LTRANS CU DIE. */
30565 if (in_lto_p)
30566 {
30567 early_dwarf_finished = true;
30568 return;
30569 }
30570
30571 /* Walk through the list of incomplete types again, trying once more to
30572 emit full debugging info for them. */
30573 retry_incomplete_types ();
30574
30575 /* The point here is to flush out the limbo list so that it is empty
30576 and we don't need to stream it for LTO. */
30577 flush_limbo_die_list ();
30578
30579 gen_scheduled_generic_parms_dies ();
30580 gen_remaining_tmpl_value_param_die_attribute ();
30581
30582 /* Add DW_AT_linkage_name for all deferred DIEs. */
30583 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30584 {
30585 tree decl = node->created_for;
30586 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30587 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30588 ended up in deferred_asm_name before we knew it was
30589 constant and never written to disk. */
30590 && DECL_ASSEMBLER_NAME (decl))
30591 {
30592 add_linkage_attr (node->die, decl);
30593 move_linkage_attr (node->die);
30594 }
30595 }
30596 deferred_asm_name = NULL;
30597
30598 if (flag_eliminate_unused_debug_types)
30599 prune_unused_types ();
30600
30601 /* Generate separate COMDAT sections for type DIEs. */
30602 if (use_debug_types)
30603 {
30604 break_out_comdat_types (comp_unit_die ());
30605
30606 /* Each new type_unit DIE was added to the limbo die list when created.
30607 Since these have all been added to comdat_type_list, clear the
30608 limbo die list. */
30609 limbo_die_list = NULL;
30610
30611 /* For each new comdat type unit, copy declarations for incomplete
30612 types to make the new unit self-contained (i.e., no direct
30613 references to the main compile unit). */
30614 for (comdat_type_node *ctnode = comdat_type_list;
30615 ctnode != NULL; ctnode = ctnode->next)
30616 copy_decls_for_unworthy_types (ctnode->root_die);
30617 copy_decls_for_unworthy_types (comp_unit_die ());
30618
30619 /* In the process of copying declarations from one unit to another,
30620 we may have left some declarations behind that are no longer
30621 referenced. Prune them. */
30622 prune_unused_types ();
30623 }
30624
30625 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30626 with dw_val_class_decl_ref operand. */
30627 note_variable_value (comp_unit_die ());
30628 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30629 note_variable_value (node->die);
30630 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30631 ctnode = ctnode->next)
30632 note_variable_value (ctnode->root_die);
30633 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30634 note_variable_value (node->die);
30635
30636 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30637 both the main_cu and all skeleton TUs. Making this call unconditional
30638 would end up either adding a second copy of the AT_pubnames attribute, or
30639 requiring a special case in add_top_level_skeleton_die_attrs. */
30640 if (!dwarf_split_debug_info)
30641 add_AT_pubnames (comp_unit_die ());
30642
30643 /* The early debug phase is now finished. */
30644 early_dwarf_finished = true;
30645
30646 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30647 if (!flag_generate_lto && !flag_generate_offload)
30648 return;
30649
30650 /* Now as we are going to output for LTO initialize sections and labels
30651 to the LTO variants. We don't need a random-seed postfix as other
30652 LTO sections as linking the LTO debug sections into one in a partial
30653 link is fine. */
30654 init_sections_and_labels (true);
30655
30656 /* The output below is modeled after dwarf2out_finish with all
30657 location related output removed and some LTO specific changes.
30658 Some refactoring might make both smaller and easier to match up. */
30659
30660 /* Traverse the DIE's and add add sibling attributes to those DIE's
30661 that have children. */
30662 add_sibling_attributes (comp_unit_die ());
30663 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30664 add_sibling_attributes (node->die);
30665 for (comdat_type_node *ctnode = comdat_type_list;
30666 ctnode != NULL; ctnode = ctnode->next)
30667 add_sibling_attributes (ctnode->root_die);
30668
30669 if (have_macinfo)
30670 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30671 macinfo_section_label);
30672
30673 save_macinfo_strings ();
30674
30675 /* Output all of the compilation units. We put the main one last so that
30676 the offsets are available to output_pubnames. */
30677 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30678 output_comp_unit (node->die, 0, NULL);
30679
30680 hash_table<comdat_type_hasher> comdat_type_table (100);
30681 for (comdat_type_node *ctnode = comdat_type_list;
30682 ctnode != NULL; ctnode = ctnode->next)
30683 {
30684 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30685
30686 /* Don't output duplicate types. */
30687 if (*slot != HTAB_EMPTY_ENTRY)
30688 continue;
30689
30690 /* Add a pointer to the line table for the main compilation unit
30691 so that the debugger can make sense of DW_AT_decl_file
30692 attributes. */
30693 if (debug_info_level >= DINFO_LEVEL_TERSE)
30694 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30695 (!dwarf_split_debug_info
30696 ? debug_line_section_label
30697 : debug_skeleton_line_section_label));
30698
30699 output_comdat_type_unit (ctnode);
30700 *slot = ctnode;
30701 }
30702
30703 /* Stick a unique symbol to the main debuginfo section. */
30704 compute_comp_unit_symbol (comp_unit_die ());
30705
30706 /* Output the main compilation unit. We always need it if only for
30707 the CU symbol. */
30708 output_comp_unit (comp_unit_die (), true, NULL);
30709
30710 /* Output the abbreviation table. */
30711 if (vec_safe_length (abbrev_die_table) != 1)
30712 {
30713 switch_to_section (debug_abbrev_section);
30714 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30715 output_abbrev_section ();
30716 }
30717
30718 /* Have to end the macro section. */
30719 if (have_macinfo)
30720 {
30721 /* We have to save macinfo state if we need to output it again
30722 for the FAT part of the object. */
30723 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30724 if (flag_fat_lto_objects)
30725 macinfo_table = macinfo_table->copy ();
30726
30727 switch_to_section (debug_macinfo_section);
30728 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30729 output_macinfo (debug_skeleton_line_section_label, true);
30730 dw2_asm_output_data (1, 0, "End compilation unit");
30731
30732 /* Emit a skeleton debug_line section. */
30733 switch_to_section (debug_skeleton_line_section);
30734 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30735 output_line_info (true);
30736
30737 if (flag_fat_lto_objects)
30738 {
30739 vec_free (macinfo_table);
30740 macinfo_table = saved_macinfo_table;
30741 }
30742 }
30743
30744
30745 /* If we emitted any indirect strings, output the string table too. */
30746 if (debug_str_hash || skeleton_debug_str_hash)
30747 output_indirect_strings ();
30748
30749 /* Switch back to the text section. */
30750 switch_to_section (text_section);
30751 }
30752
30753 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30754 within the same process. For use by toplev::finalize. */
30755
30756 void
30757 dwarf2out_c_finalize (void)
30758 {
30759 last_var_location_insn = NULL;
30760 cached_next_real_insn = NULL;
30761 used_rtx_array = NULL;
30762 incomplete_types = NULL;
30763 decl_scope_table = NULL;
30764 debug_info_section = NULL;
30765 debug_skeleton_info_section = NULL;
30766 debug_abbrev_section = NULL;
30767 debug_skeleton_abbrev_section = NULL;
30768 debug_aranges_section = NULL;
30769 debug_addr_section = NULL;
30770 debug_macinfo_section = NULL;
30771 debug_line_section = NULL;
30772 debug_skeleton_line_section = NULL;
30773 debug_loc_section = NULL;
30774 debug_pubnames_section = NULL;
30775 debug_pubtypes_section = NULL;
30776 debug_str_section = NULL;
30777 debug_line_str_section = NULL;
30778 debug_str_dwo_section = NULL;
30779 debug_str_offsets_section = NULL;
30780 debug_ranges_section = NULL;
30781 debug_frame_section = NULL;
30782 fde_vec = NULL;
30783 debug_str_hash = NULL;
30784 debug_line_str_hash = NULL;
30785 skeleton_debug_str_hash = NULL;
30786 dw2_string_counter = 0;
30787 have_multiple_function_sections = false;
30788 text_section_used = false;
30789 cold_text_section_used = false;
30790 cold_text_section = NULL;
30791 current_unit_personality = NULL;
30792
30793 early_dwarf = false;
30794 early_dwarf_finished = false;
30795
30796 next_die_offset = 0;
30797 single_comp_unit_die = NULL;
30798 comdat_type_list = NULL;
30799 limbo_die_list = NULL;
30800 file_table = NULL;
30801 decl_die_table = NULL;
30802 common_block_die_table = NULL;
30803 decl_loc_table = NULL;
30804 call_arg_locations = NULL;
30805 call_arg_loc_last = NULL;
30806 call_site_count = -1;
30807 tail_call_site_count = -1;
30808 cached_dw_loc_list_table = NULL;
30809 abbrev_die_table = NULL;
30810 delete dwarf_proc_stack_usage_map;
30811 dwarf_proc_stack_usage_map = NULL;
30812 line_info_label_num = 0;
30813 cur_line_info_table = NULL;
30814 text_section_line_info = NULL;
30815 cold_text_section_line_info = NULL;
30816 separate_line_info = NULL;
30817 info_section_emitted = false;
30818 pubname_table = NULL;
30819 pubtype_table = NULL;
30820 macinfo_table = NULL;
30821 ranges_table = NULL;
30822 ranges_by_label = NULL;
30823 rnglist_idx = 0;
30824 have_location_lists = false;
30825 loclabel_num = 0;
30826 poc_label_num = 0;
30827 last_emitted_file = NULL;
30828 label_num = 0;
30829 tmpl_value_parm_die_table = NULL;
30830 generic_type_instances = NULL;
30831 frame_pointer_fb_offset = 0;
30832 frame_pointer_fb_offset_valid = false;
30833 base_types.release ();
30834 XDELETEVEC (producer_string);
30835 producer_string = NULL;
30836 }
30837
30838 #include "gt-dwarf2out.h"