poly_int: MEM_OFFSET and MEM_SIZE
[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 "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98
99 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
100 int, bool);
101 static rtx_insn *last_var_location_insn;
102 static rtx_insn *cached_next_real_insn;
103 static void dwarf2out_decl (tree);
104
105 #ifndef XCOFF_DEBUGGING_INFO
106 #define XCOFF_DEBUGGING_INFO 0
107 #endif
108
109 #ifndef HAVE_XCOFF_DWARF_EXTRAS
110 #define HAVE_XCOFF_DWARF_EXTRAS 0
111 #endif
112
113 #ifdef VMS_DEBUGGING_INFO
114 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
115
116 /* Define this macro to be a nonzero value if the directory specifications
117 which are output in the debug info should end with a separator. */
118 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
119 /* Define this macro to evaluate to a nonzero value if GCC should refrain
120 from generating indirect strings in DWARF2 debug information, for instance
121 if your target is stuck with an old version of GDB that is unable to
122 process them properly or uses VMS Debug. */
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
124 #else
125 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
127 #endif
128
129 /* ??? Poison these here until it can be done generically. They've been
130 totally replaced in this file; make sure it stays that way. */
131 #undef DWARF2_UNWIND_INFO
132 #undef DWARF2_FRAME_INFO
133 #if (GCC_VERSION >= 3000)
134 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
135 #endif
136
137 /* The size of the target's pointer type. */
138 #ifndef PTR_SIZE
139 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
140 #endif
141
142 /* Array of RTXes referenced by the debugging information, which therefore
143 must be kept around forever. */
144 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
145
146 /* A pointer to the base of a list of incomplete types which might be
147 completed at some later time. incomplete_types_list needs to be a
148 vec<tree, va_gc> *because we want to tell the garbage collector about
149 it. */
150 static GTY(()) vec<tree, va_gc> *incomplete_types;
151
152 /* A pointer to the base of a table of references to declaration
153 scopes. This table is a display which tracks the nesting
154 of declaration scopes at the current scope and containing
155 scopes. This table is used to find the proper place to
156 define type declaration DIE's. */
157 static GTY(()) vec<tree, va_gc> *decl_scope_table;
158
159 /* Pointers to various DWARF2 sections. */
160 static GTY(()) section *debug_info_section;
161 static GTY(()) section *debug_skeleton_info_section;
162 static GTY(()) section *debug_abbrev_section;
163 static GTY(()) section *debug_skeleton_abbrev_section;
164 static GTY(()) section *debug_aranges_section;
165 static GTY(()) section *debug_addr_section;
166 static GTY(()) section *debug_macinfo_section;
167 static const char *debug_macinfo_section_name;
168 static unsigned macinfo_label_base = 1;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_line_str_section;
176 static GTY(()) section *debug_str_dwo_section;
177 static GTY(()) section *debug_str_offsets_section;
178 static GTY(()) section *debug_ranges_section;
179 static GTY(()) section *debug_frame_section;
180
181 /* Maximum size (in bytes) of an artificially generated label. */
182 #define MAX_ARTIFICIAL_LABEL_BYTES 40
183
184 /* According to the (draft) DWARF 3 specification, the initial length
185 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
186 bytes are 0xffffffff, followed by the length stored in the next 8
187 bytes.
188
189 However, the SGI/MIPS ABI uses an initial length which is equal to
190 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191
192 #ifndef DWARF_INITIAL_LENGTH_SIZE
193 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
194 #endif
195
196 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
197 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
198 #endif
199
200 /* Round SIZE up to the nearest BOUNDARY. */
201 #define DWARF_ROUND(SIZE,BOUNDARY) \
202 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
203
204 /* CIE identifier. */
205 #if HOST_BITS_PER_WIDE_INT >= 64
206 #define DWARF_CIE_ID \
207 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
208 #else
209 #define DWARF_CIE_ID DW_CIE_ID
210 #endif
211
212
213 /* A vector for a table that contains frame description
214 information for each routine. */
215 #define NOT_INDEXED (-1U)
216 #define NO_INDEX_ASSIGNED (-2U)
217
218 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
219
220 struct GTY((for_user)) indirect_string_node {
221 const char *str;
222 unsigned int refcount;
223 enum dwarf_form form;
224 char *label;
225 unsigned int index;
226 };
227
228 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
229 {
230 typedef const char *compare_type;
231
232 static hashval_t hash (indirect_string_node *);
233 static bool equal (indirect_string_node *, const char *);
234 };
235
236 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
237
238 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
239
240 /* With split_debug_info, both the comp_dir and dwo_name go in the
241 main object file, rather than the dwo, similar to the force_direct
242 parameter elsewhere but with additional complications:
243
244 1) The string is needed in both the main object file and the dwo.
245 That is, the comp_dir and dwo_name will appear in both places.
246
247 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
248 DW_FORM_line_strp or DW_FORM_GNU_str_index.
249
250 3) GCC chooses the form to use late, depending on the size and
251 reference count.
252
253 Rather than forcing the all debug string handling functions and
254 callers to deal with these complications, simply use a separate,
255 special-cased string table for any attribute that should go in the
256 main object file. This limits the complexity to just the places
257 that need it. */
258
259 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
260
261 static GTY(()) int dw2_string_counter;
262
263 /* True if the compilation unit places functions in more than one section. */
264 static GTY(()) bool have_multiple_function_sections = false;
265
266 /* Whether the default text and cold text sections have been used at all. */
267 static GTY(()) bool text_section_used = false;
268 static GTY(()) bool cold_text_section_used = false;
269
270 /* The default cold text section. */
271 static GTY(()) section *cold_text_section;
272
273 /* The DIE for C++14 'auto' in a function return type. */
274 static GTY(()) dw_die_ref auto_die;
275
276 /* The DIE for C++14 'decltype(auto)' in a function return type. */
277 static GTY(()) dw_die_ref decltype_auto_die;
278
279 /* Forward declarations for functions defined in this file. */
280
281 static void output_call_frame_info (int);
282 static void dwarf2out_note_section_used (void);
283
284 /* Personality decl of current unit. Used only when assembler does not support
285 personality CFI. */
286 static GTY(()) rtx current_unit_personality;
287
288 /* Whether an eh_frame section is required. */
289 static GTY(()) bool do_eh_frame = false;
290
291 /* .debug_rnglists next index. */
292 static unsigned int rnglist_idx;
293
294 /* Data and reference forms for relocatable data. */
295 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
296 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
297
298 #ifndef DEBUG_FRAME_SECTION
299 #define DEBUG_FRAME_SECTION ".debug_frame"
300 #endif
301
302 #ifndef FUNC_BEGIN_LABEL
303 #define FUNC_BEGIN_LABEL "LFB"
304 #endif
305
306 #ifndef FUNC_END_LABEL
307 #define FUNC_END_LABEL "LFE"
308 #endif
309
310 #ifndef PROLOGUE_END_LABEL
311 #define PROLOGUE_END_LABEL "LPE"
312 #endif
313
314 #ifndef EPILOGUE_BEGIN_LABEL
315 #define EPILOGUE_BEGIN_LABEL "LEB"
316 #endif
317
318 #ifndef FRAME_BEGIN_LABEL
319 #define FRAME_BEGIN_LABEL "Lframe"
320 #endif
321 #define CIE_AFTER_SIZE_LABEL "LSCIE"
322 #define CIE_END_LABEL "LECIE"
323 #define FDE_LABEL "LSFDE"
324 #define FDE_AFTER_SIZE_LABEL "LASFDE"
325 #define FDE_END_LABEL "LEFDE"
326 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
327 #define LINE_NUMBER_END_LABEL "LELT"
328 #define LN_PROLOG_AS_LABEL "LASLTP"
329 #define LN_PROLOG_END_LABEL "LELTP"
330 #define DIE_LABEL_PREFIX "DW"
331 \f
332 /* Match the base name of a file to the base name of a compilation unit. */
333
334 static int
335 matches_main_base (const char *path)
336 {
337 /* Cache the last query. */
338 static const char *last_path = NULL;
339 static int last_match = 0;
340 if (path != last_path)
341 {
342 const char *base;
343 int length = base_of_path (path, &base);
344 last_path = path;
345 last_match = (length == main_input_baselength
346 && memcmp (base, main_input_basename, length) == 0);
347 }
348 return last_match;
349 }
350
351 #ifdef DEBUG_DEBUG_STRUCT
352
353 static int
354 dump_struct_debug (tree type, enum debug_info_usage usage,
355 enum debug_struct_file criterion, int generic,
356 int matches, int result)
357 {
358 /* Find the type name. */
359 tree type_decl = TYPE_STUB_DECL (type);
360 tree t = type_decl;
361 const char *name = 0;
362 if (TREE_CODE (t) == TYPE_DECL)
363 t = DECL_NAME (t);
364 if (t)
365 name = IDENTIFIER_POINTER (t);
366
367 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
368 criterion,
369 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
370 matches ? "bas" : "hdr",
371 generic ? "gen" : "ord",
372 usage == DINFO_USAGE_DFN ? ";" :
373 usage == DINFO_USAGE_DIR_USE ? "." : "*",
374 result,
375 (void*) type_decl, name);
376 return result;
377 }
378 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
379 dump_struct_debug (type, usage, criterion, generic, matches, result)
380
381 #else
382
383 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
384 (result)
385
386 #endif
387
388 /* Get the number of HOST_WIDE_INTs needed to represent the precision
389 of the number. Some constants have a large uniform precision, so
390 we get the precision needed for the actual value of the number. */
391
392 static unsigned int
393 get_full_len (const wide_int &op)
394 {
395 int prec = wi::min_precision (op, UNSIGNED);
396 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
397 / HOST_BITS_PER_WIDE_INT);
398 }
399
400 static bool
401 should_emit_struct_debug (tree type, enum debug_info_usage usage)
402 {
403 enum debug_struct_file criterion;
404 tree type_decl;
405 bool generic = lang_hooks.types.generic_p (type);
406
407 if (generic)
408 criterion = debug_struct_generic[usage];
409 else
410 criterion = debug_struct_ordinary[usage];
411
412 if (criterion == DINFO_STRUCT_FILE_NONE)
413 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
414 if (criterion == DINFO_STRUCT_FILE_ANY)
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
416
417 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
418
419 if (type_decl != NULL)
420 {
421 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
422 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
423
424 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
425 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
426 }
427
428 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
429 }
430 \f
431 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
432 switch to the data section instead, and write out a synthetic start label
433 for collect2 the first time around. */
434
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
437 {
438 if (eh_frame_section == 0)
439 {
440 int flags;
441
442 if (EH_TABLES_CAN_BE_READ_ONLY)
443 {
444 int fde_encoding;
445 int per_encoding;
446 int lsda_encoding;
447
448 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449 /*global=*/0);
450 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451 /*global=*/1);
452 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453 /*global=*/0);
454 flags = ((! flag_pic
455 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456 && (fde_encoding & 0x70) != DW_EH_PE_aligned
457 && (per_encoding & 0x70) != DW_EH_PE_absptr
458 && (per_encoding & 0x70) != DW_EH_PE_aligned
459 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461 ? 0 : SECTION_WRITE);
462 }
463 else
464 flags = SECTION_WRITE;
465
466 #ifdef EH_FRAME_SECTION_NAME
467 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469 eh_frame_section = ((flags == SECTION_WRITE)
470 ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
472 }
473
474 switch_to_section (eh_frame_section);
475
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477 /* We have no special eh_frame section. Emit special labels to guide
478 collect2. */
479 if (!back)
480 {
481 tree label = get_file_function_name ("F");
482 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483 targetm.asm_out.globalize_label (asm_out_file,
484 IDENTIFIER_POINTER (label));
485 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
486 }
487 #endif
488 }
489
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491 FOR_EH. */
492
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
495 {
496 if (for_eh)
497 switch_to_eh_frame_section (back);
498 else
499 {
500 if (!debug_frame_section)
501 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502 SECTION_DEBUG, NULL);
503 switch_to_section (debug_frame_section);
504 }
505 }
506
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
508
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
511 {
512 switch (cfi)
513 {
514 case DW_CFA_nop:
515 case DW_CFA_GNU_window_save:
516 case DW_CFA_remember_state:
517 case DW_CFA_restore_state:
518 return dw_cfi_oprnd_unused;
519
520 case DW_CFA_set_loc:
521 case DW_CFA_advance_loc1:
522 case DW_CFA_advance_loc2:
523 case DW_CFA_advance_loc4:
524 case DW_CFA_MIPS_advance_loc8:
525 return dw_cfi_oprnd_addr;
526
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended:
529 case DW_CFA_def_cfa:
530 case DW_CFA_offset_extended_sf:
531 case DW_CFA_def_cfa_sf:
532 case DW_CFA_restore:
533 case DW_CFA_restore_extended:
534 case DW_CFA_undefined:
535 case DW_CFA_same_value:
536 case DW_CFA_def_cfa_register:
537 case DW_CFA_register:
538 case DW_CFA_expression:
539 case DW_CFA_val_expression:
540 return dw_cfi_oprnd_reg_num;
541
542 case DW_CFA_def_cfa_offset:
543 case DW_CFA_GNU_args_size:
544 case DW_CFA_def_cfa_offset_sf:
545 return dw_cfi_oprnd_offset;
546
547 case DW_CFA_def_cfa_expression:
548 return dw_cfi_oprnd_loc;
549
550 default:
551 gcc_unreachable ();
552 }
553 }
554
555 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
556
557 enum dw_cfi_oprnd_type
558 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
559 {
560 switch (cfi)
561 {
562 case DW_CFA_def_cfa:
563 case DW_CFA_def_cfa_sf:
564 case DW_CFA_offset:
565 case DW_CFA_offset_extended_sf:
566 case DW_CFA_offset_extended:
567 return dw_cfi_oprnd_offset;
568
569 case DW_CFA_register:
570 return dw_cfi_oprnd_reg_num;
571
572 case DW_CFA_expression:
573 case DW_CFA_val_expression:
574 return dw_cfi_oprnd_loc;
575
576 default:
577 return dw_cfi_oprnd_unused;
578 }
579 }
580
581 /* Output one FDE. */
582
583 static void
584 output_fde (dw_fde_ref fde, bool for_eh, bool second,
585 char *section_start_label, int fde_encoding, char *augmentation,
586 bool any_lsda_needed, int lsda_encoding)
587 {
588 const char *begin, *end;
589 static unsigned int j;
590 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
591
592 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
593 /* empty */ 0);
594 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
595 for_eh + j);
596 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
597 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
598 if (!XCOFF_DEBUGGING_INFO || for_eh)
599 {
600 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
601 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
602 " indicating 64-bit DWARF extension");
603 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
604 "FDE Length");
605 }
606 ASM_OUTPUT_LABEL (asm_out_file, l1);
607
608 if (for_eh)
609 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
610 else
611 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
612 debug_frame_section, "FDE CIE offset");
613
614 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
615 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
616
617 if (for_eh)
618 {
619 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
620 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
621 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
622 "FDE initial location");
623 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
624 end, begin, "FDE address range");
625 }
626 else
627 {
628 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
629 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
630 }
631
632 if (augmentation[0])
633 {
634 if (any_lsda_needed)
635 {
636 int size = size_of_encoded_value (lsda_encoding);
637
638 if (lsda_encoding == DW_EH_PE_aligned)
639 {
640 int offset = ( 4 /* Length */
641 + 4 /* CIE offset */
642 + 2 * size_of_encoded_value (fde_encoding)
643 + 1 /* Augmentation size */ );
644 int pad = -offset & (PTR_SIZE - 1);
645
646 size += pad;
647 gcc_assert (size_of_uleb128 (size) == 1);
648 }
649
650 dw2_asm_output_data_uleb128 (size, "Augmentation size");
651
652 if (fde->uses_eh_lsda)
653 {
654 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
655 fde->funcdef_number);
656 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
657 gen_rtx_SYMBOL_REF (Pmode, l1),
658 false,
659 "Language Specific Data Area");
660 }
661 else
662 {
663 if (lsda_encoding == DW_EH_PE_aligned)
664 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
665 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
666 "Language Specific Data Area (none)");
667 }
668 }
669 else
670 dw2_asm_output_data_uleb128 (0, "Augmentation size");
671 }
672
673 /* Loop through the Call Frame Instructions associated with this FDE. */
674 fde->dw_fde_current_label = begin;
675 {
676 size_t from, until, i;
677
678 from = 0;
679 until = vec_safe_length (fde->dw_fde_cfi);
680
681 if (fde->dw_fde_second_begin == NULL)
682 ;
683 else if (!second)
684 until = fde->dw_fde_switch_cfi_index;
685 else
686 from = fde->dw_fde_switch_cfi_index;
687
688 for (i = from; i < until; i++)
689 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
690 }
691
692 /* If we are to emit a ref/link from function bodies to their frame tables,
693 do it now. This is typically performed to make sure that tables
694 associated with functions are dragged with them and not discarded in
695 garbage collecting links. We need to do this on a per function basis to
696 cope with -ffunction-sections. */
697
698 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
699 /* Switch to the function section, emit the ref to the tables, and
700 switch *back* into the table section. */
701 switch_to_section (function_section (fde->decl));
702 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
703 switch_to_frame_table_section (for_eh, true);
704 #endif
705
706 /* Pad the FDE out to an address sized boundary. */
707 ASM_OUTPUT_ALIGN (asm_out_file,
708 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
709 ASM_OUTPUT_LABEL (asm_out_file, l2);
710
711 j += 2;
712 }
713
714 /* Return true if frame description entry FDE is needed for EH. */
715
716 static bool
717 fde_needed_for_eh_p (dw_fde_ref fde)
718 {
719 if (flag_asynchronous_unwind_tables)
720 return true;
721
722 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
723 return true;
724
725 if (fde->uses_eh_lsda)
726 return true;
727
728 /* If exceptions are enabled, we have collected nothrow info. */
729 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
730 return false;
731
732 return true;
733 }
734
735 /* Output the call frame information used to record information
736 that relates to calculating the frame pointer, and records the
737 location of saved registers. */
738
739 static void
740 output_call_frame_info (int for_eh)
741 {
742 unsigned int i;
743 dw_fde_ref fde;
744 dw_cfi_ref cfi;
745 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
746 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
756
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
760
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
764
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
772 {
773 bool any_eh_needed = false;
774
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
776 {
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
783 }
784
785 if (!any_eh_needed)
786 return;
787 }
788
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
792
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
795
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
798
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (!XCOFF_DEBUGGING_INFO || for_eh)
803 {
804 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
805 dw2_asm_output_data (4, 0xffffffff,
806 "Initial length escape value indicating 64-bit DWARF extension");
807 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
808 "Length of Common Information Entry");
809 }
810 ASM_OUTPUT_LABEL (asm_out_file, l1);
811
812 /* Now that the CIE pointer is PC-relative for EH,
813 use 0 to identify the CIE. */
814 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
815 (for_eh ? 0 : DWARF_CIE_ID),
816 "CIE Identifier Tag");
817
818 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
819 use CIE version 1, unless that would produce incorrect results
820 due to overflowing the return register column. */
821 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
822 dw_cie_version = 1;
823 if (return_reg >= 256 || dwarf_version > 2)
824 dw_cie_version = 3;
825 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
826
827 augmentation[0] = 0;
828 augmentation_size = 0;
829
830 personality = current_unit_personality;
831 if (for_eh)
832 {
833 char *p;
834
835 /* Augmentation:
836 z Indicates that a uleb128 is present to size the
837 augmentation section.
838 L Indicates the encoding (and thus presence) of
839 an LSDA pointer in the FDE augmentation.
840 R Indicates a non-default pointer encoding for
841 FDE code pointers.
842 P Indicates the presence of an encoding + language
843 personality routine in the CIE augmentation. */
844
845 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
846 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
847 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
848
849 p = augmentation + 1;
850 if (personality)
851 {
852 *p++ = 'P';
853 augmentation_size += 1 + size_of_encoded_value (per_encoding);
854 assemble_external_libcall (personality);
855 }
856 if (any_lsda_needed)
857 {
858 *p++ = 'L';
859 augmentation_size += 1;
860 }
861 if (fde_encoding != DW_EH_PE_absptr)
862 {
863 *p++ = 'R';
864 augmentation_size += 1;
865 }
866 if (p > augmentation + 1)
867 {
868 augmentation[0] = 'z';
869 *p = '\0';
870 }
871
872 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
873 if (personality && per_encoding == DW_EH_PE_aligned)
874 {
875 int offset = ( 4 /* Length */
876 + 4 /* CIE Id */
877 + 1 /* CIE version */
878 + strlen (augmentation) + 1 /* Augmentation */
879 + size_of_uleb128 (1) /* Code alignment */
880 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
881 + 1 /* RA column */
882 + 1 /* Augmentation size */
883 + 1 /* Personality encoding */ );
884 int pad = -offset & (PTR_SIZE - 1);
885
886 augmentation_size += pad;
887
888 /* Augmentations should be small, so there's scarce need to
889 iterate for a solution. Die if we exceed one uleb128 byte. */
890 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 }
892 }
893
894 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
895 if (dw_cie_version >= 4)
896 {
897 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
898 dw2_asm_output_data (1, 0, "CIE Segment Size");
899 }
900 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
901 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
902 "CIE Data Alignment Factor");
903
904 if (dw_cie_version == 1)
905 dw2_asm_output_data (1, return_reg, "CIE RA Column");
906 else
907 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
908
909 if (augmentation[0])
910 {
911 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
912 if (personality)
913 {
914 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
915 eh_data_format_name (per_encoding));
916 dw2_asm_output_encoded_addr_rtx (per_encoding,
917 personality,
918 true, NULL);
919 }
920
921 if (any_lsda_needed)
922 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
923 eh_data_format_name (lsda_encoding));
924
925 if (fde_encoding != DW_EH_PE_absptr)
926 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
927 eh_data_format_name (fde_encoding));
928 }
929
930 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
931 output_cfi (cfi, NULL, for_eh);
932
933 /* Pad the CIE out to an address sized boundary. */
934 ASM_OUTPUT_ALIGN (asm_out_file,
935 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
936 ASM_OUTPUT_LABEL (asm_out_file, l2);
937
938 /* Loop through all of the FDE's. */
939 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
940 {
941 unsigned int k;
942
943 /* Don't emit EH unwind info for leaf functions that don't need it. */
944 if (for_eh && !fde_needed_for_eh_p (fde))
945 continue;
946
947 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
948 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
949 augmentation, any_lsda_needed, lsda_encoding);
950 }
951
952 if (for_eh && targetm.terminate_dw2_eh_frame_info)
953 dw2_asm_output_data (4, 0, "End of Table");
954
955 /* Turn off app to make assembly quicker. */
956 if (flag_debug_asm)
957 app_disable ();
958 }
959
960 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
961
962 static void
963 dwarf2out_do_cfi_startproc (bool second)
964 {
965 int enc;
966 rtx ref;
967
968 fprintf (asm_out_file, "\t.cfi_startproc\n");
969
970 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
971 eh unwinders. */
972 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
973 return;
974
975 rtx personality = get_personality_function (current_function_decl);
976
977 if (personality)
978 {
979 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
980 ref = personality;
981
982 /* ??? The GAS support isn't entirely consistent. We have to
983 handle indirect support ourselves, but PC-relative is done
984 in the assembler. Further, the assembler can't handle any
985 of the weirder relocation types. */
986 if (enc & DW_EH_PE_indirect)
987 ref = dw2_force_const_mem (ref, true);
988
989 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
990 output_addr_const (asm_out_file, ref);
991 fputc ('\n', asm_out_file);
992 }
993
994 if (crtl->uses_eh_lsda)
995 {
996 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
997
998 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
999 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1000 current_function_funcdef_no);
1001 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1002 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1003
1004 if (enc & DW_EH_PE_indirect)
1005 ref = dw2_force_const_mem (ref, true);
1006
1007 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1008 output_addr_const (asm_out_file, ref);
1009 fputc ('\n', asm_out_file);
1010 }
1011 }
1012
1013 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1014 this allocation may be done before pass_final. */
1015
1016 dw_fde_ref
1017 dwarf2out_alloc_current_fde (void)
1018 {
1019 dw_fde_ref fde;
1020
1021 fde = ggc_cleared_alloc<dw_fde_node> ();
1022 fde->decl = current_function_decl;
1023 fde->funcdef_number = current_function_funcdef_no;
1024 fde->fde_index = vec_safe_length (fde_vec);
1025 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1026 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1027 fde->nothrow = crtl->nothrow;
1028 fde->drap_reg = INVALID_REGNUM;
1029 fde->vdrap_reg = INVALID_REGNUM;
1030
1031 /* Record the FDE associated with this function. */
1032 cfun->fde = fde;
1033 vec_safe_push (fde_vec, fde);
1034
1035 return fde;
1036 }
1037
1038 /* Output a marker (i.e. a label) for the beginning of a function, before
1039 the prologue. */
1040
1041 void
1042 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1043 unsigned int column ATTRIBUTE_UNUSED,
1044 const char *file ATTRIBUTE_UNUSED)
1045 {
1046 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1047 char * dup_label;
1048 dw_fde_ref fde;
1049 section *fnsec;
1050 bool do_frame;
1051
1052 current_function_func_begin_label = NULL;
1053
1054 do_frame = dwarf2out_do_frame ();
1055
1056 /* ??? current_function_func_begin_label is also used by except.c for
1057 call-site information. We must emit this label if it might be used. */
1058 if (!do_frame
1059 && (!flag_exceptions
1060 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1061 return;
1062
1063 fnsec = function_section (current_function_decl);
1064 switch_to_section (fnsec);
1065 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1066 current_function_funcdef_no);
1067 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1068 current_function_funcdef_no);
1069 dup_label = xstrdup (label);
1070 current_function_func_begin_label = dup_label;
1071
1072 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1073 if (!do_frame)
1074 return;
1075
1076 /* Unlike the debug version, the EH version of frame unwind info is a per-
1077 function setting so we need to record whether we need it for the unit. */
1078 do_eh_frame |= dwarf2out_do_eh_frame ();
1079
1080 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1081 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1082 would include pass_dwarf2_frame. If we've not created the FDE yet,
1083 do so now. */
1084 fde = cfun->fde;
1085 if (fde == NULL)
1086 fde = dwarf2out_alloc_current_fde ();
1087
1088 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1089 fde->dw_fde_begin = dup_label;
1090 fde->dw_fde_current_label = dup_label;
1091 fde->in_std_section = (fnsec == text_section
1092 || (cold_text_section && fnsec == cold_text_section));
1093
1094 /* We only want to output line number information for the genuine dwarf2
1095 prologue case, not the eh frame case. */
1096 #ifdef DWARF2_DEBUGGING_INFO
1097 if (file)
1098 dwarf2out_source_line (line, column, file, 0, true);
1099 #endif
1100
1101 if (dwarf2out_do_cfi_asm ())
1102 dwarf2out_do_cfi_startproc (false);
1103 else
1104 {
1105 rtx personality = get_personality_function (current_function_decl);
1106 if (!current_unit_personality)
1107 current_unit_personality = personality;
1108
1109 /* We cannot keep a current personality per function as without CFI
1110 asm, at the point where we emit the CFI data, there is no current
1111 function anymore. */
1112 if (personality && current_unit_personality != personality)
1113 sorry ("multiple EH personalities are supported only with assemblers "
1114 "supporting .cfi_personality directive");
1115 }
1116 }
1117
1118 /* Output a marker (i.e. a label) for the end of the generated code
1119 for a function prologue. This gets called *after* the prologue code has
1120 been generated. */
1121
1122 void
1123 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1124 const char *file ATTRIBUTE_UNUSED)
1125 {
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127
1128 /* Output a label to mark the endpoint of the code generated for this
1129 function. */
1130 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1131 current_function_funcdef_no);
1132 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1133 current_function_funcdef_no);
1134 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1135 }
1136
1137 /* Output a marker (i.e. a label) for the beginning of the generated code
1138 for a function epilogue. This gets called *before* the prologue code has
1139 been generated. */
1140
1141 void
1142 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1143 const char *file ATTRIBUTE_UNUSED)
1144 {
1145 dw_fde_ref fde = cfun->fde;
1146 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1147
1148 if (fde->dw_fde_vms_begin_epilogue)
1149 return;
1150
1151 /* Output a label to mark the endpoint of the code generated for this
1152 function. */
1153 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1154 current_function_funcdef_no);
1155 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1156 current_function_funcdef_no);
1157 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1158 }
1159
1160 /* Output a marker (i.e. a label) for the absolute end of the generated code
1161 for a function definition. This gets called *after* the epilogue code has
1162 been generated. */
1163
1164 void
1165 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1166 const char *file ATTRIBUTE_UNUSED)
1167 {
1168 dw_fde_ref fde;
1169 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1170
1171 last_var_location_insn = NULL;
1172 cached_next_real_insn = NULL;
1173
1174 if (dwarf2out_do_cfi_asm ())
1175 fprintf (asm_out_file, "\t.cfi_endproc\n");
1176
1177 /* Output a label to mark the endpoint of the code generated for this
1178 function. */
1179 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1180 current_function_funcdef_no);
1181 ASM_OUTPUT_LABEL (asm_out_file, label);
1182 fde = cfun->fde;
1183 gcc_assert (fde != NULL);
1184 if (fde->dw_fde_second_begin == NULL)
1185 fde->dw_fde_end = xstrdup (label);
1186 }
1187
1188 void
1189 dwarf2out_frame_finish (void)
1190 {
1191 /* Output call frame information. */
1192 if (targetm.debug_unwind_info () == UI_DWARF2)
1193 output_call_frame_info (0);
1194
1195 /* Output another copy for the unwinder. */
1196 if (do_eh_frame)
1197 output_call_frame_info (1);
1198 }
1199
1200 /* Note that the current function section is being used for code. */
1201
1202 static void
1203 dwarf2out_note_section_used (void)
1204 {
1205 section *sec = current_function_section ();
1206 if (sec == text_section)
1207 text_section_used = true;
1208 else if (sec == cold_text_section)
1209 cold_text_section_used = true;
1210 }
1211
1212 static void var_location_switch_text_section (void);
1213 static void set_cur_line_info_table (section *);
1214
1215 void
1216 dwarf2out_switch_text_section (void)
1217 {
1218 section *sect;
1219 dw_fde_ref fde = cfun->fde;
1220
1221 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1222
1223 if (!in_cold_section_p)
1224 {
1225 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1226 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1227 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1228 }
1229 else
1230 {
1231 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1232 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1233 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1234 }
1235 have_multiple_function_sections = true;
1236
1237 /* There is no need to mark used sections when not debugging. */
1238 if (cold_text_section != NULL)
1239 dwarf2out_note_section_used ();
1240
1241 if (dwarf2out_do_cfi_asm ())
1242 fprintf (asm_out_file, "\t.cfi_endproc\n");
1243
1244 /* Now do the real section switch. */
1245 sect = current_function_section ();
1246 switch_to_section (sect);
1247
1248 fde->second_in_std_section
1249 = (sect == text_section
1250 || (cold_text_section && sect == cold_text_section));
1251
1252 if (dwarf2out_do_cfi_asm ())
1253 dwarf2out_do_cfi_startproc (true);
1254
1255 var_location_switch_text_section ();
1256
1257 if (cold_text_section != NULL)
1258 set_cur_line_info_table (sect);
1259 }
1260 \f
1261 /* And now, the subset of the debugging information support code necessary
1262 for emitting location expressions. */
1263
1264 /* Data about a single source file. */
1265 struct GTY((for_user)) dwarf_file_data {
1266 const char * filename;
1267 int emitted_number;
1268 };
1269
1270 /* Describe an entry into the .debug_addr section. */
1271
1272 enum ate_kind {
1273 ate_kind_rtx,
1274 ate_kind_rtx_dtprel,
1275 ate_kind_label
1276 };
1277
1278 struct GTY((for_user)) addr_table_entry {
1279 enum ate_kind kind;
1280 unsigned int refcount;
1281 unsigned int index;
1282 union addr_table_entry_struct_union
1283 {
1284 rtx GTY ((tag ("0"))) rtl;
1285 char * GTY ((tag ("1"))) label;
1286 }
1287 GTY ((desc ("%1.kind"))) addr;
1288 };
1289
1290 /* Location lists are ranges + location descriptions for that range,
1291 so you can track variables that are in different places over
1292 their entire life. */
1293 typedef struct GTY(()) dw_loc_list_struct {
1294 dw_loc_list_ref dw_loc_next;
1295 const char *begin; /* Label and addr_entry for start of range */
1296 addr_table_entry *begin_entry;
1297 const char *end; /* Label for end of range */
1298 char *ll_symbol; /* Label for beginning of location list.
1299 Only on head of list */
1300 const char *section; /* Section this loclist is relative to */
1301 dw_loc_descr_ref expr;
1302 hashval_t hash;
1303 /* True if all addresses in this and subsequent lists are known to be
1304 resolved. */
1305 bool resolved_addr;
1306 /* True if this list has been replaced by dw_loc_next. */
1307 bool replaced;
1308 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1309 section. */
1310 unsigned char emitted : 1;
1311 /* True if hash field is index rather than hash value. */
1312 unsigned char num_assigned : 1;
1313 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1314 unsigned char offset_emitted : 1;
1315 /* True if note_variable_value_in_expr has been called on it. */
1316 unsigned char noted_variable_value : 1;
1317 /* True if the range should be emitted even if begin and end
1318 are the same. */
1319 bool force;
1320 } dw_loc_list_node;
1321
1322 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1323 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1324
1325 /* Convert a DWARF stack opcode into its string name. */
1326
1327 static const char *
1328 dwarf_stack_op_name (unsigned int op)
1329 {
1330 const char *name = get_DW_OP_name (op);
1331
1332 if (name != NULL)
1333 return name;
1334
1335 return "OP_<unknown>";
1336 }
1337
1338 /* Return a pointer to a newly allocated location description. Location
1339 descriptions are simple expression terms that can be strung
1340 together to form more complicated location (address) descriptions. */
1341
1342 static inline dw_loc_descr_ref
1343 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1344 unsigned HOST_WIDE_INT oprnd2)
1345 {
1346 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1347
1348 descr->dw_loc_opc = op;
1349 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1350 descr->dw_loc_oprnd1.val_entry = NULL;
1351 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1352 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1353 descr->dw_loc_oprnd2.val_entry = NULL;
1354 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1355
1356 return descr;
1357 }
1358
1359 /* Add a location description term to a location description expression. */
1360
1361 static inline void
1362 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1363 {
1364 dw_loc_descr_ref *d;
1365
1366 /* Find the end of the chain. */
1367 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1368 ;
1369
1370 *d = descr;
1371 }
1372
1373 /* Compare two location operands for exact equality. */
1374
1375 static bool
1376 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1377 {
1378 if (a->val_class != b->val_class)
1379 return false;
1380 switch (a->val_class)
1381 {
1382 case dw_val_class_none:
1383 return true;
1384 case dw_val_class_addr:
1385 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1386
1387 case dw_val_class_offset:
1388 case dw_val_class_unsigned_const:
1389 case dw_val_class_const:
1390 case dw_val_class_unsigned_const_implicit:
1391 case dw_val_class_const_implicit:
1392 case dw_val_class_range_list:
1393 /* These are all HOST_WIDE_INT, signed or unsigned. */
1394 return a->v.val_unsigned == b->v.val_unsigned;
1395
1396 case dw_val_class_loc:
1397 return a->v.val_loc == b->v.val_loc;
1398 case dw_val_class_loc_list:
1399 return a->v.val_loc_list == b->v.val_loc_list;
1400 case dw_val_class_die_ref:
1401 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1402 case dw_val_class_fde_ref:
1403 return a->v.val_fde_index == b->v.val_fde_index;
1404 case dw_val_class_lbl_id:
1405 case dw_val_class_lineptr:
1406 case dw_val_class_macptr:
1407 case dw_val_class_loclistsptr:
1408 case dw_val_class_high_pc:
1409 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1410 case dw_val_class_str:
1411 return a->v.val_str == b->v.val_str;
1412 case dw_val_class_flag:
1413 return a->v.val_flag == b->v.val_flag;
1414 case dw_val_class_file:
1415 case dw_val_class_file_implicit:
1416 return a->v.val_file == b->v.val_file;
1417 case dw_val_class_decl_ref:
1418 return a->v.val_decl_ref == b->v.val_decl_ref;
1419
1420 case dw_val_class_const_double:
1421 return (a->v.val_double.high == b->v.val_double.high
1422 && a->v.val_double.low == b->v.val_double.low);
1423
1424 case dw_val_class_wide_int:
1425 return *a->v.val_wide == *b->v.val_wide;
1426
1427 case dw_val_class_vec:
1428 {
1429 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1430 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1431
1432 return (a_len == b_len
1433 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1434 }
1435
1436 case dw_val_class_data8:
1437 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1438
1439 case dw_val_class_vms_delta:
1440 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1441 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1442
1443 case dw_val_class_discr_value:
1444 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1445 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1446 case dw_val_class_discr_list:
1447 /* It makes no sense comparing two discriminant value lists. */
1448 return false;
1449 }
1450 gcc_unreachable ();
1451 }
1452
1453 /* Compare two location atoms for exact equality. */
1454
1455 static bool
1456 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1457 {
1458 if (a->dw_loc_opc != b->dw_loc_opc)
1459 return false;
1460
1461 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1462 address size, but since we always allocate cleared storage it
1463 should be zero for other types of locations. */
1464 if (a->dtprel != b->dtprel)
1465 return false;
1466
1467 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1468 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1469 }
1470
1471 /* Compare two complete location expressions for exact equality. */
1472
1473 bool
1474 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1475 {
1476 while (1)
1477 {
1478 if (a == b)
1479 return true;
1480 if (a == NULL || b == NULL)
1481 return false;
1482 if (!loc_descr_equal_p_1 (a, b))
1483 return false;
1484
1485 a = a->dw_loc_next;
1486 b = b->dw_loc_next;
1487 }
1488 }
1489
1490
1491 /* Add a constant POLY_OFFSET to a location expression. */
1492
1493 static void
1494 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1495 {
1496 dw_loc_descr_ref loc;
1497 HOST_WIDE_INT *p;
1498
1499 gcc_assert (*list_head != NULL);
1500
1501 if (known_eq (poly_offset, 0))
1502 return;
1503
1504 /* Find the end of the chain. */
1505 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1506 ;
1507
1508 HOST_WIDE_INT offset;
1509 if (!poly_offset.is_constant (&offset))
1510 {
1511 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1512 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1513 return;
1514 }
1515
1516 p = NULL;
1517 if (loc->dw_loc_opc == DW_OP_fbreg
1518 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1519 p = &loc->dw_loc_oprnd1.v.val_int;
1520 else if (loc->dw_loc_opc == DW_OP_bregx)
1521 p = &loc->dw_loc_oprnd2.v.val_int;
1522
1523 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1524 offset. Don't optimize if an signed integer overflow would happen. */
1525 if (p != NULL
1526 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1527 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1528 *p += offset;
1529
1530 else if (offset > 0)
1531 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1532
1533 else
1534 {
1535 loc->dw_loc_next
1536 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1537 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1538 }
1539 }
1540
1541 /* Return a pointer to a newly allocated location description for
1542 REG and OFFSET. */
1543
1544 static inline dw_loc_descr_ref
1545 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1546 {
1547 HOST_WIDE_INT const_offset;
1548 if (offset.is_constant (&const_offset))
1549 {
1550 if (reg <= 31)
1551 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1552 const_offset, 0);
1553 else
1554 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1555 }
1556 else
1557 {
1558 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1559 loc_descr_plus_const (&ret, offset);
1560 return ret;
1561 }
1562 }
1563
1564 /* Add a constant OFFSET to a location list. */
1565
1566 static void
1567 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1568 {
1569 dw_loc_list_ref d;
1570 for (d = list_head; d != NULL; d = d->dw_loc_next)
1571 loc_descr_plus_const (&d->expr, offset);
1572 }
1573
1574 #define DWARF_REF_SIZE \
1575 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1576
1577 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1578 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1579 DW_FORM_data16 with 128 bits. */
1580 #define DWARF_LARGEST_DATA_FORM_BITS \
1581 (dwarf_version >= 5 ? 128 : 64)
1582
1583 /* Utility inline function for construction of ops that were GNU extension
1584 before DWARF 5. */
1585 static inline enum dwarf_location_atom
1586 dwarf_OP (enum dwarf_location_atom op)
1587 {
1588 switch (op)
1589 {
1590 case DW_OP_implicit_pointer:
1591 if (dwarf_version < 5)
1592 return DW_OP_GNU_implicit_pointer;
1593 break;
1594
1595 case DW_OP_entry_value:
1596 if (dwarf_version < 5)
1597 return DW_OP_GNU_entry_value;
1598 break;
1599
1600 case DW_OP_const_type:
1601 if (dwarf_version < 5)
1602 return DW_OP_GNU_const_type;
1603 break;
1604
1605 case DW_OP_regval_type:
1606 if (dwarf_version < 5)
1607 return DW_OP_GNU_regval_type;
1608 break;
1609
1610 case DW_OP_deref_type:
1611 if (dwarf_version < 5)
1612 return DW_OP_GNU_deref_type;
1613 break;
1614
1615 case DW_OP_convert:
1616 if (dwarf_version < 5)
1617 return DW_OP_GNU_convert;
1618 break;
1619
1620 case DW_OP_reinterpret:
1621 if (dwarf_version < 5)
1622 return DW_OP_GNU_reinterpret;
1623 break;
1624
1625 default:
1626 break;
1627 }
1628 return op;
1629 }
1630
1631 /* Similarly for attributes. */
1632 static inline enum dwarf_attribute
1633 dwarf_AT (enum dwarf_attribute at)
1634 {
1635 switch (at)
1636 {
1637 case DW_AT_call_return_pc:
1638 if (dwarf_version < 5)
1639 return DW_AT_low_pc;
1640 break;
1641
1642 case DW_AT_call_tail_call:
1643 if (dwarf_version < 5)
1644 return DW_AT_GNU_tail_call;
1645 break;
1646
1647 case DW_AT_call_origin:
1648 if (dwarf_version < 5)
1649 return DW_AT_abstract_origin;
1650 break;
1651
1652 case DW_AT_call_target:
1653 if (dwarf_version < 5)
1654 return DW_AT_GNU_call_site_target;
1655 break;
1656
1657 case DW_AT_call_target_clobbered:
1658 if (dwarf_version < 5)
1659 return DW_AT_GNU_call_site_target_clobbered;
1660 break;
1661
1662 case DW_AT_call_parameter:
1663 if (dwarf_version < 5)
1664 return DW_AT_abstract_origin;
1665 break;
1666
1667 case DW_AT_call_value:
1668 if (dwarf_version < 5)
1669 return DW_AT_GNU_call_site_value;
1670 break;
1671
1672 case DW_AT_call_data_value:
1673 if (dwarf_version < 5)
1674 return DW_AT_GNU_call_site_data_value;
1675 break;
1676
1677 case DW_AT_call_all_calls:
1678 if (dwarf_version < 5)
1679 return DW_AT_GNU_all_call_sites;
1680 break;
1681
1682 case DW_AT_call_all_tail_calls:
1683 if (dwarf_version < 5)
1684 return DW_AT_GNU_all_tail_call_sites;
1685 break;
1686
1687 case DW_AT_dwo_name:
1688 if (dwarf_version < 5)
1689 return DW_AT_GNU_dwo_name;
1690 break;
1691
1692 default:
1693 break;
1694 }
1695 return at;
1696 }
1697
1698 /* And similarly for tags. */
1699 static inline enum dwarf_tag
1700 dwarf_TAG (enum dwarf_tag tag)
1701 {
1702 switch (tag)
1703 {
1704 case DW_TAG_call_site:
1705 if (dwarf_version < 5)
1706 return DW_TAG_GNU_call_site;
1707 break;
1708
1709 case DW_TAG_call_site_parameter:
1710 if (dwarf_version < 5)
1711 return DW_TAG_GNU_call_site_parameter;
1712 break;
1713
1714 default:
1715 break;
1716 }
1717 return tag;
1718 }
1719
1720 static unsigned long int get_base_type_offset (dw_die_ref);
1721
1722 /* Return the size of a location descriptor. */
1723
1724 static unsigned long
1725 size_of_loc_descr (dw_loc_descr_ref loc)
1726 {
1727 unsigned long size = 1;
1728
1729 switch (loc->dw_loc_opc)
1730 {
1731 case DW_OP_addr:
1732 size += DWARF2_ADDR_SIZE;
1733 break;
1734 case DW_OP_GNU_addr_index:
1735 case DW_OP_GNU_const_index:
1736 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1737 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1738 break;
1739 case DW_OP_const1u:
1740 case DW_OP_const1s:
1741 size += 1;
1742 break;
1743 case DW_OP_const2u:
1744 case DW_OP_const2s:
1745 size += 2;
1746 break;
1747 case DW_OP_const4u:
1748 case DW_OP_const4s:
1749 size += 4;
1750 break;
1751 case DW_OP_const8u:
1752 case DW_OP_const8s:
1753 size += 8;
1754 break;
1755 case DW_OP_constu:
1756 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1757 break;
1758 case DW_OP_consts:
1759 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1760 break;
1761 case DW_OP_pick:
1762 size += 1;
1763 break;
1764 case DW_OP_plus_uconst:
1765 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1766 break;
1767 case DW_OP_skip:
1768 case DW_OP_bra:
1769 size += 2;
1770 break;
1771 case DW_OP_breg0:
1772 case DW_OP_breg1:
1773 case DW_OP_breg2:
1774 case DW_OP_breg3:
1775 case DW_OP_breg4:
1776 case DW_OP_breg5:
1777 case DW_OP_breg6:
1778 case DW_OP_breg7:
1779 case DW_OP_breg8:
1780 case DW_OP_breg9:
1781 case DW_OP_breg10:
1782 case DW_OP_breg11:
1783 case DW_OP_breg12:
1784 case DW_OP_breg13:
1785 case DW_OP_breg14:
1786 case DW_OP_breg15:
1787 case DW_OP_breg16:
1788 case DW_OP_breg17:
1789 case DW_OP_breg18:
1790 case DW_OP_breg19:
1791 case DW_OP_breg20:
1792 case DW_OP_breg21:
1793 case DW_OP_breg22:
1794 case DW_OP_breg23:
1795 case DW_OP_breg24:
1796 case DW_OP_breg25:
1797 case DW_OP_breg26:
1798 case DW_OP_breg27:
1799 case DW_OP_breg28:
1800 case DW_OP_breg29:
1801 case DW_OP_breg30:
1802 case DW_OP_breg31:
1803 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1804 break;
1805 case DW_OP_regx:
1806 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1807 break;
1808 case DW_OP_fbreg:
1809 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1810 break;
1811 case DW_OP_bregx:
1812 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1813 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1814 break;
1815 case DW_OP_piece:
1816 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1817 break;
1818 case DW_OP_bit_piece:
1819 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1820 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1821 break;
1822 case DW_OP_deref_size:
1823 case DW_OP_xderef_size:
1824 size += 1;
1825 break;
1826 case DW_OP_call2:
1827 size += 2;
1828 break;
1829 case DW_OP_call4:
1830 size += 4;
1831 break;
1832 case DW_OP_call_ref:
1833 case DW_OP_GNU_variable_value:
1834 size += DWARF_REF_SIZE;
1835 break;
1836 case DW_OP_implicit_value:
1837 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1838 + loc->dw_loc_oprnd1.v.val_unsigned;
1839 break;
1840 case DW_OP_implicit_pointer:
1841 case DW_OP_GNU_implicit_pointer:
1842 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1843 break;
1844 case DW_OP_entry_value:
1845 case DW_OP_GNU_entry_value:
1846 {
1847 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1848 size += size_of_uleb128 (op_size) + op_size;
1849 break;
1850 }
1851 case DW_OP_const_type:
1852 case DW_OP_GNU_const_type:
1853 {
1854 unsigned long o
1855 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1856 size += size_of_uleb128 (o) + 1;
1857 switch (loc->dw_loc_oprnd2.val_class)
1858 {
1859 case dw_val_class_vec:
1860 size += loc->dw_loc_oprnd2.v.val_vec.length
1861 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1862 break;
1863 case dw_val_class_const:
1864 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1865 break;
1866 case dw_val_class_const_double:
1867 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1868 break;
1869 case dw_val_class_wide_int:
1870 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1871 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1872 break;
1873 default:
1874 gcc_unreachable ();
1875 }
1876 break;
1877 }
1878 case DW_OP_regval_type:
1879 case DW_OP_GNU_regval_type:
1880 {
1881 unsigned long o
1882 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1883 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1884 + size_of_uleb128 (o);
1885 }
1886 break;
1887 case DW_OP_deref_type:
1888 case DW_OP_GNU_deref_type:
1889 {
1890 unsigned long o
1891 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1892 size += 1 + size_of_uleb128 (o);
1893 }
1894 break;
1895 case DW_OP_convert:
1896 case DW_OP_reinterpret:
1897 case DW_OP_GNU_convert:
1898 case DW_OP_GNU_reinterpret:
1899 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1900 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1901 else
1902 {
1903 unsigned long o
1904 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1905 size += size_of_uleb128 (o);
1906 }
1907 break;
1908 case DW_OP_GNU_parameter_ref:
1909 size += 4;
1910 break;
1911 default:
1912 break;
1913 }
1914
1915 return size;
1916 }
1917
1918 /* Return the size of a series of location descriptors. */
1919
1920 unsigned long
1921 size_of_locs (dw_loc_descr_ref loc)
1922 {
1923 dw_loc_descr_ref l;
1924 unsigned long size;
1925
1926 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1927 field, to avoid writing to a PCH file. */
1928 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1929 {
1930 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1931 break;
1932 size += size_of_loc_descr (l);
1933 }
1934 if (! l)
1935 return size;
1936
1937 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1938 {
1939 l->dw_loc_addr = size;
1940 size += size_of_loc_descr (l);
1941 }
1942
1943 return size;
1944 }
1945
1946 /* Return the size of the value in a DW_AT_discr_value attribute. */
1947
1948 static int
1949 size_of_discr_value (dw_discr_value *discr_value)
1950 {
1951 if (discr_value->pos)
1952 return size_of_uleb128 (discr_value->v.uval);
1953 else
1954 return size_of_sleb128 (discr_value->v.sval);
1955 }
1956
1957 /* Return the size of the value in a DW_AT_discr_list attribute. */
1958
1959 static int
1960 size_of_discr_list (dw_discr_list_ref discr_list)
1961 {
1962 int size = 0;
1963
1964 for (dw_discr_list_ref list = discr_list;
1965 list != NULL;
1966 list = list->dw_discr_next)
1967 {
1968 /* One byte for the discriminant value descriptor, and then one or two
1969 LEB128 numbers, depending on whether it's a single case label or a
1970 range label. */
1971 size += 1;
1972 size += size_of_discr_value (&list->dw_discr_lower_bound);
1973 if (list->dw_discr_range != 0)
1974 size += size_of_discr_value (&list->dw_discr_upper_bound);
1975 }
1976 return size;
1977 }
1978
1979 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1980 static void get_ref_die_offset_label (char *, dw_die_ref);
1981 static unsigned long int get_ref_die_offset (dw_die_ref);
1982
1983 /* Output location description stack opcode's operands (if any).
1984 The for_eh_or_skip parameter controls whether register numbers are
1985 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1986 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1987 info). This should be suppressed for the cases that have not been converted
1988 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1989
1990 static void
1991 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1992 {
1993 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1994 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1995
1996 switch (loc->dw_loc_opc)
1997 {
1998 #ifdef DWARF2_DEBUGGING_INFO
1999 case DW_OP_const2u:
2000 case DW_OP_const2s:
2001 dw2_asm_output_data (2, val1->v.val_int, NULL);
2002 break;
2003 case DW_OP_const4u:
2004 if (loc->dtprel)
2005 {
2006 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2007 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2008 val1->v.val_addr);
2009 fputc ('\n', asm_out_file);
2010 break;
2011 }
2012 /* FALLTHRU */
2013 case DW_OP_const4s:
2014 dw2_asm_output_data (4, val1->v.val_int, NULL);
2015 break;
2016 case DW_OP_const8u:
2017 if (loc->dtprel)
2018 {
2019 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2020 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2021 val1->v.val_addr);
2022 fputc ('\n', asm_out_file);
2023 break;
2024 }
2025 /* FALLTHRU */
2026 case DW_OP_const8s:
2027 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2028 dw2_asm_output_data (8, val1->v.val_int, NULL);
2029 break;
2030 case DW_OP_skip:
2031 case DW_OP_bra:
2032 {
2033 int offset;
2034
2035 gcc_assert (val1->val_class == dw_val_class_loc);
2036 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2037
2038 dw2_asm_output_data (2, offset, NULL);
2039 }
2040 break;
2041 case DW_OP_implicit_value:
2042 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2043 switch (val2->val_class)
2044 {
2045 case dw_val_class_const:
2046 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2047 break;
2048 case dw_val_class_vec:
2049 {
2050 unsigned int elt_size = val2->v.val_vec.elt_size;
2051 unsigned int len = val2->v.val_vec.length;
2052 unsigned int i;
2053 unsigned char *p;
2054
2055 if (elt_size > sizeof (HOST_WIDE_INT))
2056 {
2057 elt_size /= 2;
2058 len *= 2;
2059 }
2060 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2061 i < len;
2062 i++, p += elt_size)
2063 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2064 "fp or vector constant word %u", i);
2065 }
2066 break;
2067 case dw_val_class_const_double:
2068 {
2069 unsigned HOST_WIDE_INT first, second;
2070
2071 if (WORDS_BIG_ENDIAN)
2072 {
2073 first = val2->v.val_double.high;
2074 second = val2->v.val_double.low;
2075 }
2076 else
2077 {
2078 first = val2->v.val_double.low;
2079 second = val2->v.val_double.high;
2080 }
2081 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2082 first, NULL);
2083 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2084 second, NULL);
2085 }
2086 break;
2087 case dw_val_class_wide_int:
2088 {
2089 int i;
2090 int len = get_full_len (*val2->v.val_wide);
2091 if (WORDS_BIG_ENDIAN)
2092 for (i = len - 1; i >= 0; --i)
2093 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2094 val2->v.val_wide->elt (i), NULL);
2095 else
2096 for (i = 0; i < len; ++i)
2097 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2098 val2->v.val_wide->elt (i), NULL);
2099 }
2100 break;
2101 case dw_val_class_addr:
2102 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2103 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2104 break;
2105 default:
2106 gcc_unreachable ();
2107 }
2108 break;
2109 #else
2110 case DW_OP_const2u:
2111 case DW_OP_const2s:
2112 case DW_OP_const4u:
2113 case DW_OP_const4s:
2114 case DW_OP_const8u:
2115 case DW_OP_const8s:
2116 case DW_OP_skip:
2117 case DW_OP_bra:
2118 case DW_OP_implicit_value:
2119 /* We currently don't make any attempt to make sure these are
2120 aligned properly like we do for the main unwind info, so
2121 don't support emitting things larger than a byte if we're
2122 only doing unwinding. */
2123 gcc_unreachable ();
2124 #endif
2125 case DW_OP_const1u:
2126 case DW_OP_const1s:
2127 dw2_asm_output_data (1, val1->v.val_int, NULL);
2128 break;
2129 case DW_OP_constu:
2130 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2131 break;
2132 case DW_OP_consts:
2133 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2134 break;
2135 case DW_OP_pick:
2136 dw2_asm_output_data (1, val1->v.val_int, NULL);
2137 break;
2138 case DW_OP_plus_uconst:
2139 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2140 break;
2141 case DW_OP_breg0:
2142 case DW_OP_breg1:
2143 case DW_OP_breg2:
2144 case DW_OP_breg3:
2145 case DW_OP_breg4:
2146 case DW_OP_breg5:
2147 case DW_OP_breg6:
2148 case DW_OP_breg7:
2149 case DW_OP_breg8:
2150 case DW_OP_breg9:
2151 case DW_OP_breg10:
2152 case DW_OP_breg11:
2153 case DW_OP_breg12:
2154 case DW_OP_breg13:
2155 case DW_OP_breg14:
2156 case DW_OP_breg15:
2157 case DW_OP_breg16:
2158 case DW_OP_breg17:
2159 case DW_OP_breg18:
2160 case DW_OP_breg19:
2161 case DW_OP_breg20:
2162 case DW_OP_breg21:
2163 case DW_OP_breg22:
2164 case DW_OP_breg23:
2165 case DW_OP_breg24:
2166 case DW_OP_breg25:
2167 case DW_OP_breg26:
2168 case DW_OP_breg27:
2169 case DW_OP_breg28:
2170 case DW_OP_breg29:
2171 case DW_OP_breg30:
2172 case DW_OP_breg31:
2173 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2174 break;
2175 case DW_OP_regx:
2176 {
2177 unsigned r = val1->v.val_unsigned;
2178 if (for_eh_or_skip >= 0)
2179 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2180 gcc_assert (size_of_uleb128 (r)
2181 == size_of_uleb128 (val1->v.val_unsigned));
2182 dw2_asm_output_data_uleb128 (r, NULL);
2183 }
2184 break;
2185 case DW_OP_fbreg:
2186 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2187 break;
2188 case DW_OP_bregx:
2189 {
2190 unsigned r = val1->v.val_unsigned;
2191 if (for_eh_or_skip >= 0)
2192 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2193 gcc_assert (size_of_uleb128 (r)
2194 == size_of_uleb128 (val1->v.val_unsigned));
2195 dw2_asm_output_data_uleb128 (r, NULL);
2196 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2197 }
2198 break;
2199 case DW_OP_piece:
2200 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2201 break;
2202 case DW_OP_bit_piece:
2203 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2204 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2205 break;
2206 case DW_OP_deref_size:
2207 case DW_OP_xderef_size:
2208 dw2_asm_output_data (1, val1->v.val_int, NULL);
2209 break;
2210
2211 case DW_OP_addr:
2212 if (loc->dtprel)
2213 {
2214 if (targetm.asm_out.output_dwarf_dtprel)
2215 {
2216 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2217 DWARF2_ADDR_SIZE,
2218 val1->v.val_addr);
2219 fputc ('\n', asm_out_file);
2220 }
2221 else
2222 gcc_unreachable ();
2223 }
2224 else
2225 {
2226 #ifdef DWARF2_DEBUGGING_INFO
2227 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2228 #else
2229 gcc_unreachable ();
2230 #endif
2231 }
2232 break;
2233
2234 case DW_OP_GNU_addr_index:
2235 case DW_OP_GNU_const_index:
2236 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2237 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2238 "(index into .debug_addr)");
2239 break;
2240
2241 case DW_OP_call2:
2242 case DW_OP_call4:
2243 {
2244 unsigned long die_offset
2245 = get_ref_die_offset (val1->v.val_die_ref.die);
2246 /* Make sure the offset has been computed and that we can encode it as
2247 an operand. */
2248 gcc_assert (die_offset > 0
2249 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2250 ? 0xffff
2251 : 0xffffffff));
2252 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2253 die_offset, NULL);
2254 }
2255 break;
2256
2257 case DW_OP_call_ref:
2258 case DW_OP_GNU_variable_value:
2259 {
2260 char label[MAX_ARTIFICIAL_LABEL_BYTES
2261 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2262 gcc_assert (val1->val_class == dw_val_class_die_ref);
2263 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2264 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2265 }
2266 break;
2267
2268 case DW_OP_implicit_pointer:
2269 case DW_OP_GNU_implicit_pointer:
2270 {
2271 char label[MAX_ARTIFICIAL_LABEL_BYTES
2272 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2273 gcc_assert (val1->val_class == dw_val_class_die_ref);
2274 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2275 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2276 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2277 }
2278 break;
2279
2280 case DW_OP_entry_value:
2281 case DW_OP_GNU_entry_value:
2282 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2283 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2284 break;
2285
2286 case DW_OP_const_type:
2287 case DW_OP_GNU_const_type:
2288 {
2289 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2290 gcc_assert (o);
2291 dw2_asm_output_data_uleb128 (o, NULL);
2292 switch (val2->val_class)
2293 {
2294 case dw_val_class_const:
2295 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2296 dw2_asm_output_data (1, l, NULL);
2297 dw2_asm_output_data (l, val2->v.val_int, NULL);
2298 break;
2299 case dw_val_class_vec:
2300 {
2301 unsigned int elt_size = val2->v.val_vec.elt_size;
2302 unsigned int len = val2->v.val_vec.length;
2303 unsigned int i;
2304 unsigned char *p;
2305
2306 l = len * elt_size;
2307 dw2_asm_output_data (1, l, NULL);
2308 if (elt_size > sizeof (HOST_WIDE_INT))
2309 {
2310 elt_size /= 2;
2311 len *= 2;
2312 }
2313 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2314 i < len;
2315 i++, p += elt_size)
2316 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2317 "fp or vector constant word %u", i);
2318 }
2319 break;
2320 case dw_val_class_const_double:
2321 {
2322 unsigned HOST_WIDE_INT first, second;
2323 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2324
2325 dw2_asm_output_data (1, 2 * l, NULL);
2326 if (WORDS_BIG_ENDIAN)
2327 {
2328 first = val2->v.val_double.high;
2329 second = val2->v.val_double.low;
2330 }
2331 else
2332 {
2333 first = val2->v.val_double.low;
2334 second = val2->v.val_double.high;
2335 }
2336 dw2_asm_output_data (l, first, NULL);
2337 dw2_asm_output_data (l, second, NULL);
2338 }
2339 break;
2340 case dw_val_class_wide_int:
2341 {
2342 int i;
2343 int len = get_full_len (*val2->v.val_wide);
2344 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2345
2346 dw2_asm_output_data (1, len * l, NULL);
2347 if (WORDS_BIG_ENDIAN)
2348 for (i = len - 1; i >= 0; --i)
2349 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2350 else
2351 for (i = 0; i < len; ++i)
2352 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2353 }
2354 break;
2355 default:
2356 gcc_unreachable ();
2357 }
2358 }
2359 break;
2360 case DW_OP_regval_type:
2361 case DW_OP_GNU_regval_type:
2362 {
2363 unsigned r = val1->v.val_unsigned;
2364 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2365 gcc_assert (o);
2366 if (for_eh_or_skip >= 0)
2367 {
2368 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2369 gcc_assert (size_of_uleb128 (r)
2370 == size_of_uleb128 (val1->v.val_unsigned));
2371 }
2372 dw2_asm_output_data_uleb128 (r, NULL);
2373 dw2_asm_output_data_uleb128 (o, NULL);
2374 }
2375 break;
2376 case DW_OP_deref_type:
2377 case DW_OP_GNU_deref_type:
2378 {
2379 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2380 gcc_assert (o);
2381 dw2_asm_output_data (1, val1->v.val_int, NULL);
2382 dw2_asm_output_data_uleb128 (o, NULL);
2383 }
2384 break;
2385 case DW_OP_convert:
2386 case DW_OP_reinterpret:
2387 case DW_OP_GNU_convert:
2388 case DW_OP_GNU_reinterpret:
2389 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2390 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2391 else
2392 {
2393 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2394 gcc_assert (o);
2395 dw2_asm_output_data_uleb128 (o, NULL);
2396 }
2397 break;
2398
2399 case DW_OP_GNU_parameter_ref:
2400 {
2401 unsigned long o;
2402 gcc_assert (val1->val_class == dw_val_class_die_ref);
2403 o = get_ref_die_offset (val1->v.val_die_ref.die);
2404 dw2_asm_output_data (4, o, NULL);
2405 }
2406 break;
2407
2408 default:
2409 /* Other codes have no operands. */
2410 break;
2411 }
2412 }
2413
2414 /* Output a sequence of location operations.
2415 The for_eh_or_skip parameter controls whether register numbers are
2416 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2417 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2418 info). This should be suppressed for the cases that have not been converted
2419 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2420
2421 void
2422 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2423 {
2424 for (; loc != NULL; loc = loc->dw_loc_next)
2425 {
2426 enum dwarf_location_atom opc = loc->dw_loc_opc;
2427 /* Output the opcode. */
2428 if (for_eh_or_skip >= 0
2429 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2430 {
2431 unsigned r = (opc - DW_OP_breg0);
2432 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2433 gcc_assert (r <= 31);
2434 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2435 }
2436 else if (for_eh_or_skip >= 0
2437 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2438 {
2439 unsigned r = (opc - DW_OP_reg0);
2440 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2441 gcc_assert (r <= 31);
2442 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2443 }
2444
2445 dw2_asm_output_data (1, opc,
2446 "%s", dwarf_stack_op_name (opc));
2447
2448 /* Output the operand(s) (if any). */
2449 output_loc_operands (loc, for_eh_or_skip);
2450 }
2451 }
2452
2453 /* Output location description stack opcode's operands (if any).
2454 The output is single bytes on a line, suitable for .cfi_escape. */
2455
2456 static void
2457 output_loc_operands_raw (dw_loc_descr_ref loc)
2458 {
2459 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2460 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2461
2462 switch (loc->dw_loc_opc)
2463 {
2464 case DW_OP_addr:
2465 case DW_OP_GNU_addr_index:
2466 case DW_OP_GNU_const_index:
2467 case DW_OP_implicit_value:
2468 /* We cannot output addresses in .cfi_escape, only bytes. */
2469 gcc_unreachable ();
2470
2471 case DW_OP_const1u:
2472 case DW_OP_const1s:
2473 case DW_OP_pick:
2474 case DW_OP_deref_size:
2475 case DW_OP_xderef_size:
2476 fputc (',', asm_out_file);
2477 dw2_asm_output_data_raw (1, val1->v.val_int);
2478 break;
2479
2480 case DW_OP_const2u:
2481 case DW_OP_const2s:
2482 fputc (',', asm_out_file);
2483 dw2_asm_output_data_raw (2, val1->v.val_int);
2484 break;
2485
2486 case DW_OP_const4u:
2487 case DW_OP_const4s:
2488 fputc (',', asm_out_file);
2489 dw2_asm_output_data_raw (4, val1->v.val_int);
2490 break;
2491
2492 case DW_OP_const8u:
2493 case DW_OP_const8s:
2494 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2495 fputc (',', asm_out_file);
2496 dw2_asm_output_data_raw (8, val1->v.val_int);
2497 break;
2498
2499 case DW_OP_skip:
2500 case DW_OP_bra:
2501 {
2502 int offset;
2503
2504 gcc_assert (val1->val_class == dw_val_class_loc);
2505 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2506
2507 fputc (',', asm_out_file);
2508 dw2_asm_output_data_raw (2, offset);
2509 }
2510 break;
2511
2512 case DW_OP_regx:
2513 {
2514 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2515 gcc_assert (size_of_uleb128 (r)
2516 == size_of_uleb128 (val1->v.val_unsigned));
2517 fputc (',', asm_out_file);
2518 dw2_asm_output_data_uleb128_raw (r);
2519 }
2520 break;
2521
2522 case DW_OP_constu:
2523 case DW_OP_plus_uconst:
2524 case DW_OP_piece:
2525 fputc (',', asm_out_file);
2526 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2527 break;
2528
2529 case DW_OP_bit_piece:
2530 fputc (',', asm_out_file);
2531 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2532 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2533 break;
2534
2535 case DW_OP_consts:
2536 case DW_OP_breg0:
2537 case DW_OP_breg1:
2538 case DW_OP_breg2:
2539 case DW_OP_breg3:
2540 case DW_OP_breg4:
2541 case DW_OP_breg5:
2542 case DW_OP_breg6:
2543 case DW_OP_breg7:
2544 case DW_OP_breg8:
2545 case DW_OP_breg9:
2546 case DW_OP_breg10:
2547 case DW_OP_breg11:
2548 case DW_OP_breg12:
2549 case DW_OP_breg13:
2550 case DW_OP_breg14:
2551 case DW_OP_breg15:
2552 case DW_OP_breg16:
2553 case DW_OP_breg17:
2554 case DW_OP_breg18:
2555 case DW_OP_breg19:
2556 case DW_OP_breg20:
2557 case DW_OP_breg21:
2558 case DW_OP_breg22:
2559 case DW_OP_breg23:
2560 case DW_OP_breg24:
2561 case DW_OP_breg25:
2562 case DW_OP_breg26:
2563 case DW_OP_breg27:
2564 case DW_OP_breg28:
2565 case DW_OP_breg29:
2566 case DW_OP_breg30:
2567 case DW_OP_breg31:
2568 case DW_OP_fbreg:
2569 fputc (',', asm_out_file);
2570 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2571 break;
2572
2573 case DW_OP_bregx:
2574 {
2575 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2576 gcc_assert (size_of_uleb128 (r)
2577 == size_of_uleb128 (val1->v.val_unsigned));
2578 fputc (',', asm_out_file);
2579 dw2_asm_output_data_uleb128_raw (r);
2580 fputc (',', asm_out_file);
2581 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2582 }
2583 break;
2584
2585 case DW_OP_implicit_pointer:
2586 case DW_OP_entry_value:
2587 case DW_OP_const_type:
2588 case DW_OP_regval_type:
2589 case DW_OP_deref_type:
2590 case DW_OP_convert:
2591 case DW_OP_reinterpret:
2592 case DW_OP_GNU_implicit_pointer:
2593 case DW_OP_GNU_entry_value:
2594 case DW_OP_GNU_const_type:
2595 case DW_OP_GNU_regval_type:
2596 case DW_OP_GNU_deref_type:
2597 case DW_OP_GNU_convert:
2598 case DW_OP_GNU_reinterpret:
2599 case DW_OP_GNU_parameter_ref:
2600 gcc_unreachable ();
2601 break;
2602
2603 default:
2604 /* Other codes have no operands. */
2605 break;
2606 }
2607 }
2608
2609 void
2610 output_loc_sequence_raw (dw_loc_descr_ref loc)
2611 {
2612 while (1)
2613 {
2614 enum dwarf_location_atom opc = loc->dw_loc_opc;
2615 /* Output the opcode. */
2616 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2617 {
2618 unsigned r = (opc - DW_OP_breg0);
2619 r = DWARF2_FRAME_REG_OUT (r, 1);
2620 gcc_assert (r <= 31);
2621 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2622 }
2623 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2624 {
2625 unsigned r = (opc - DW_OP_reg0);
2626 r = DWARF2_FRAME_REG_OUT (r, 1);
2627 gcc_assert (r <= 31);
2628 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2629 }
2630 /* Output the opcode. */
2631 fprintf (asm_out_file, "%#x", opc);
2632 output_loc_operands_raw (loc);
2633
2634 if (!loc->dw_loc_next)
2635 break;
2636 loc = loc->dw_loc_next;
2637
2638 fputc (',', asm_out_file);
2639 }
2640 }
2641
2642 /* This function builds a dwarf location descriptor sequence from a
2643 dw_cfa_location, adding the given OFFSET to the result of the
2644 expression. */
2645
2646 struct dw_loc_descr_node *
2647 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2648 {
2649 struct dw_loc_descr_node *head, *tmp;
2650
2651 offset += cfa->offset;
2652
2653 if (cfa->indirect)
2654 {
2655 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2656 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2657 head->dw_loc_oprnd1.val_entry = NULL;
2658 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2659 add_loc_descr (&head, tmp);
2660 loc_descr_plus_const (&head, offset);
2661 }
2662 else
2663 head = new_reg_loc_descr (cfa->reg, offset);
2664
2665 return head;
2666 }
2667
2668 /* This function builds a dwarf location descriptor sequence for
2669 the address at OFFSET from the CFA when stack is aligned to
2670 ALIGNMENT byte. */
2671
2672 struct dw_loc_descr_node *
2673 build_cfa_aligned_loc (dw_cfa_location *cfa,
2674 poly_int64 offset, HOST_WIDE_INT alignment)
2675 {
2676 struct dw_loc_descr_node *head;
2677 unsigned int dwarf_fp
2678 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2679
2680 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2681 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2682 {
2683 head = new_reg_loc_descr (dwarf_fp, 0);
2684 add_loc_descr (&head, int_loc_descriptor (alignment));
2685 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2686 loc_descr_plus_const (&head, offset);
2687 }
2688 else
2689 head = new_reg_loc_descr (dwarf_fp, offset);
2690 return head;
2691 }
2692 \f
2693 /* And now, the support for symbolic debugging information. */
2694
2695 /* .debug_str support. */
2696
2697 static void dwarf2out_init (const char *);
2698 static void dwarf2out_finish (const char *);
2699 static void dwarf2out_early_finish (const char *);
2700 static void dwarf2out_assembly_start (void);
2701 static void dwarf2out_define (unsigned int, const char *);
2702 static void dwarf2out_undef (unsigned int, const char *);
2703 static void dwarf2out_start_source_file (unsigned, const char *);
2704 static void dwarf2out_end_source_file (unsigned);
2705 static void dwarf2out_function_decl (tree);
2706 static void dwarf2out_begin_block (unsigned, unsigned);
2707 static void dwarf2out_end_block (unsigned, unsigned);
2708 static bool dwarf2out_ignore_block (const_tree);
2709 static void dwarf2out_early_global_decl (tree);
2710 static void dwarf2out_late_global_decl (tree);
2711 static void dwarf2out_type_decl (tree, int);
2712 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2713 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2714 dw_die_ref);
2715 static void dwarf2out_abstract_function (tree);
2716 static void dwarf2out_var_location (rtx_insn *);
2717 static void dwarf2out_size_function (tree);
2718 static void dwarf2out_begin_function (tree);
2719 static void dwarf2out_end_function (unsigned int);
2720 static void dwarf2out_register_main_translation_unit (tree unit);
2721 static void dwarf2out_set_name (tree, tree);
2722 static void dwarf2out_register_external_die (tree decl, const char *sym,
2723 unsigned HOST_WIDE_INT off);
2724 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2725 unsigned HOST_WIDE_INT *off);
2726
2727 /* The debug hooks structure. */
2728
2729 const struct gcc_debug_hooks dwarf2_debug_hooks =
2730 {
2731 dwarf2out_init,
2732 dwarf2out_finish,
2733 dwarf2out_early_finish,
2734 dwarf2out_assembly_start,
2735 dwarf2out_define,
2736 dwarf2out_undef,
2737 dwarf2out_start_source_file,
2738 dwarf2out_end_source_file,
2739 dwarf2out_begin_block,
2740 dwarf2out_end_block,
2741 dwarf2out_ignore_block,
2742 dwarf2out_source_line,
2743 dwarf2out_begin_prologue,
2744 #if VMS_DEBUGGING_INFO
2745 dwarf2out_vms_end_prologue,
2746 dwarf2out_vms_begin_epilogue,
2747 #else
2748 debug_nothing_int_charstar,
2749 debug_nothing_int_charstar,
2750 #endif
2751 dwarf2out_end_epilogue,
2752 dwarf2out_begin_function,
2753 dwarf2out_end_function, /* end_function */
2754 dwarf2out_register_main_translation_unit,
2755 dwarf2out_function_decl, /* function_decl */
2756 dwarf2out_early_global_decl,
2757 dwarf2out_late_global_decl,
2758 dwarf2out_type_decl, /* type_decl */
2759 dwarf2out_imported_module_or_decl,
2760 dwarf2out_die_ref_for_decl,
2761 dwarf2out_register_external_die,
2762 debug_nothing_tree, /* deferred_inline_function */
2763 /* The DWARF 2 backend tries to reduce debugging bloat by not
2764 emitting the abstract description of inline functions until
2765 something tries to reference them. */
2766 dwarf2out_abstract_function, /* outlining_inline_function */
2767 debug_nothing_rtx_code_label, /* label */
2768 debug_nothing_int, /* handle_pch */
2769 dwarf2out_var_location,
2770 debug_nothing_tree, /* inline_entry */
2771 dwarf2out_size_function, /* size_function */
2772 dwarf2out_switch_text_section,
2773 dwarf2out_set_name,
2774 1, /* start_end_main_source_file */
2775 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2776 };
2777
2778 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2779 {
2780 dwarf2out_init,
2781 debug_nothing_charstar,
2782 debug_nothing_charstar,
2783 dwarf2out_assembly_start,
2784 debug_nothing_int_charstar,
2785 debug_nothing_int_charstar,
2786 debug_nothing_int_charstar,
2787 debug_nothing_int,
2788 debug_nothing_int_int, /* begin_block */
2789 debug_nothing_int_int, /* end_block */
2790 debug_true_const_tree, /* ignore_block */
2791 dwarf2out_source_line, /* source_line */
2792 debug_nothing_int_int_charstar, /* begin_prologue */
2793 debug_nothing_int_charstar, /* end_prologue */
2794 debug_nothing_int_charstar, /* begin_epilogue */
2795 debug_nothing_int_charstar, /* end_epilogue */
2796 debug_nothing_tree, /* begin_function */
2797 debug_nothing_int, /* end_function */
2798 debug_nothing_tree, /* register_main_translation_unit */
2799 debug_nothing_tree, /* function_decl */
2800 debug_nothing_tree, /* early_global_decl */
2801 debug_nothing_tree, /* late_global_decl */
2802 debug_nothing_tree_int, /* type_decl */
2803 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2804 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2805 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2806 debug_nothing_tree, /* deferred_inline_function */
2807 debug_nothing_tree, /* outlining_inline_function */
2808 debug_nothing_rtx_code_label, /* label */
2809 debug_nothing_int, /* handle_pch */
2810 debug_nothing_rtx_insn, /* var_location */
2811 debug_nothing_tree, /* inline_entry */
2812 debug_nothing_tree, /* size_function */
2813 debug_nothing_void, /* switch_text_section */
2814 debug_nothing_tree_tree, /* set_name */
2815 0, /* start_end_main_source_file */
2816 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2817 };
2818 \f
2819 /* NOTE: In the comments in this file, many references are made to
2820 "Debugging Information Entries". This term is abbreviated as `DIE'
2821 throughout the remainder of this file. */
2822
2823 /* An internal representation of the DWARF output is built, and then
2824 walked to generate the DWARF debugging info. The walk of the internal
2825 representation is done after the entire program has been compiled.
2826 The types below are used to describe the internal representation. */
2827
2828 /* Whether to put type DIEs into their own section .debug_types instead
2829 of making them part of the .debug_info section. Only supported for
2830 Dwarf V4 or higher and the user didn't disable them through
2831 -fno-debug-types-section. It is more efficient to put them in a
2832 separate comdat sections since the linker will then be able to
2833 remove duplicates. But not all tools support .debug_types sections
2834 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2835 it is DW_UT_type unit type in .debug_info section. */
2836
2837 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2838
2839 /* Various DIE's use offsets relative to the beginning of the
2840 .debug_info section to refer to each other. */
2841
2842 typedef long int dw_offset;
2843
2844 struct comdat_type_node;
2845
2846 /* The entries in the line_info table more-or-less mirror the opcodes
2847 that are used in the real dwarf line table. Arrays of these entries
2848 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2849 supported. */
2850
2851 enum dw_line_info_opcode {
2852 /* Emit DW_LNE_set_address; the operand is the label index. */
2853 LI_set_address,
2854
2855 /* Emit a row to the matrix with the given line. This may be done
2856 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2857 special opcodes. */
2858 LI_set_line,
2859
2860 /* Emit a DW_LNS_set_file. */
2861 LI_set_file,
2862
2863 /* Emit a DW_LNS_set_column. */
2864 LI_set_column,
2865
2866 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2867 LI_negate_stmt,
2868
2869 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2870 LI_set_prologue_end,
2871 LI_set_epilogue_begin,
2872
2873 /* Emit a DW_LNE_set_discriminator. */
2874 LI_set_discriminator
2875 };
2876
2877 typedef struct GTY(()) dw_line_info_struct {
2878 enum dw_line_info_opcode opcode;
2879 unsigned int val;
2880 } dw_line_info_entry;
2881
2882
2883 struct GTY(()) dw_line_info_table {
2884 /* The label that marks the end of this section. */
2885 const char *end_label;
2886
2887 /* The values for the last row of the matrix, as collected in the table.
2888 These are used to minimize the changes to the next row. */
2889 unsigned int file_num;
2890 unsigned int line_num;
2891 unsigned int column_num;
2892 int discrim_num;
2893 bool is_stmt;
2894 bool in_use;
2895
2896 vec<dw_line_info_entry, va_gc> *entries;
2897 };
2898
2899
2900 /* Each DIE attribute has a field specifying the attribute kind,
2901 a link to the next attribute in the chain, and an attribute value.
2902 Attributes are typically linked below the DIE they modify. */
2903
2904 typedef struct GTY(()) dw_attr_struct {
2905 enum dwarf_attribute dw_attr;
2906 dw_val_node dw_attr_val;
2907 }
2908 dw_attr_node;
2909
2910
2911 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2912 The children of each node form a circular list linked by
2913 die_sib. die_child points to the node *before* the "first" child node. */
2914
2915 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2916 union die_symbol_or_type_node
2917 {
2918 const char * GTY ((tag ("0"))) die_symbol;
2919 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2920 }
2921 GTY ((desc ("%0.comdat_type_p"))) die_id;
2922 vec<dw_attr_node, va_gc> *die_attr;
2923 dw_die_ref die_parent;
2924 dw_die_ref die_child;
2925 dw_die_ref die_sib;
2926 dw_die_ref die_definition; /* ref from a specification to its definition */
2927 dw_offset die_offset;
2928 unsigned long die_abbrev;
2929 int die_mark;
2930 unsigned int decl_id;
2931 enum dwarf_tag die_tag;
2932 /* Die is used and must not be pruned as unused. */
2933 BOOL_BITFIELD die_perennial_p : 1;
2934 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2935 /* For an external ref to die_symbol if die_offset contains an extra
2936 offset to that symbol. */
2937 BOOL_BITFIELD with_offset : 1;
2938 /* Whether this DIE was removed from the DIE tree, for example via
2939 prune_unused_types. We don't consider those present from the
2940 DIE lookup routines. */
2941 BOOL_BITFIELD removed : 1;
2942 /* Lots of spare bits. */
2943 }
2944 die_node;
2945
2946 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2947 static bool early_dwarf;
2948 static bool early_dwarf_finished;
2949 struct set_early_dwarf {
2950 bool saved;
2951 set_early_dwarf () : saved(early_dwarf)
2952 {
2953 gcc_assert (! early_dwarf_finished);
2954 early_dwarf = true;
2955 }
2956 ~set_early_dwarf () { early_dwarf = saved; }
2957 };
2958
2959 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2960 #define FOR_EACH_CHILD(die, c, expr) do { \
2961 c = die->die_child; \
2962 if (c) do { \
2963 c = c->die_sib; \
2964 expr; \
2965 } while (c != die->die_child); \
2966 } while (0)
2967
2968 /* The pubname structure */
2969
2970 typedef struct GTY(()) pubname_struct {
2971 dw_die_ref die;
2972 const char *name;
2973 }
2974 pubname_entry;
2975
2976
2977 struct GTY(()) dw_ranges {
2978 const char *label;
2979 /* If this is positive, it's a block number, otherwise it's a
2980 bitwise-negated index into dw_ranges_by_label. */
2981 int num;
2982 /* Index for the range list for DW_FORM_rnglistx. */
2983 unsigned int idx : 31;
2984 /* True if this range might be possibly in a different section
2985 from previous entry. */
2986 unsigned int maybe_new_sec : 1;
2987 };
2988
2989 /* A structure to hold a macinfo entry. */
2990
2991 typedef struct GTY(()) macinfo_struct {
2992 unsigned char code;
2993 unsigned HOST_WIDE_INT lineno;
2994 const char *info;
2995 }
2996 macinfo_entry;
2997
2998
2999 struct GTY(()) dw_ranges_by_label {
3000 const char *begin;
3001 const char *end;
3002 };
3003
3004 /* The comdat type node structure. */
3005 struct GTY(()) comdat_type_node
3006 {
3007 dw_die_ref root_die;
3008 dw_die_ref type_die;
3009 dw_die_ref skeleton_die;
3010 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3011 comdat_type_node *next;
3012 };
3013
3014 /* A list of DIEs for which we can't determine ancestry (parent_die
3015 field) just yet. Later in dwarf2out_finish we will fill in the
3016 missing bits. */
3017 typedef struct GTY(()) limbo_die_struct {
3018 dw_die_ref die;
3019 /* The tree for which this DIE was created. We use this to
3020 determine ancestry later. */
3021 tree created_for;
3022 struct limbo_die_struct *next;
3023 }
3024 limbo_die_node;
3025
3026 typedef struct skeleton_chain_struct
3027 {
3028 dw_die_ref old_die;
3029 dw_die_ref new_die;
3030 struct skeleton_chain_struct *parent;
3031 }
3032 skeleton_chain_node;
3033
3034 /* Define a macro which returns nonzero for a TYPE_DECL which was
3035 implicitly generated for a type.
3036
3037 Note that, unlike the C front-end (which generates a NULL named
3038 TYPE_DECL node for each complete tagged type, each array type,
3039 and each function type node created) the C++ front-end generates
3040 a _named_ TYPE_DECL node for each tagged type node created.
3041 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3042 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3043 front-end, but for each type, tagged or not. */
3044
3045 #define TYPE_DECL_IS_STUB(decl) \
3046 (DECL_NAME (decl) == NULL_TREE \
3047 || (DECL_ARTIFICIAL (decl) \
3048 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3049 /* This is necessary for stub decls that \
3050 appear in nested inline functions. */ \
3051 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3052 && (decl_ultimate_origin (decl) \
3053 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3054
3055 /* Information concerning the compilation unit's programming
3056 language, and compiler version. */
3057
3058 /* Fixed size portion of the DWARF compilation unit header. */
3059 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3060 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3061 + (dwarf_version >= 5 ? 4 : 3))
3062
3063 /* Fixed size portion of the DWARF comdat type unit header. */
3064 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3065 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3066 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3067
3068 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3069 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3070 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3071
3072 /* Fixed size portion of public names info. */
3073 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3074
3075 /* Fixed size portion of the address range info. */
3076 #define DWARF_ARANGES_HEADER_SIZE \
3077 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3078 DWARF2_ADDR_SIZE * 2) \
3079 - DWARF_INITIAL_LENGTH_SIZE)
3080
3081 /* Size of padding portion in the address range info. It must be
3082 aligned to twice the pointer size. */
3083 #define DWARF_ARANGES_PAD_SIZE \
3084 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3085 DWARF2_ADDR_SIZE * 2) \
3086 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3087
3088 /* Use assembler line directives if available. */
3089 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3090 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3091 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3092 #else
3093 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3094 #endif
3095 #endif
3096
3097 /* Minimum line offset in a special line info. opcode.
3098 This value was chosen to give a reasonable range of values. */
3099 #define DWARF_LINE_BASE -10
3100
3101 /* First special line opcode - leave room for the standard opcodes. */
3102 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3103
3104 /* Range of line offsets in a special line info. opcode. */
3105 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3106
3107 /* Flag that indicates the initial value of the is_stmt_start flag.
3108 In the present implementation, we do not mark any lines as
3109 the beginning of a source statement, because that information
3110 is not made available by the GCC front-end. */
3111 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3112
3113 /* Maximum number of operations per instruction bundle. */
3114 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3115 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3116 #endif
3117
3118 /* This location is used by calc_die_sizes() to keep track
3119 the offset of each DIE within the .debug_info section. */
3120 static unsigned long next_die_offset;
3121
3122 /* Record the root of the DIE's built for the current compilation unit. */
3123 static GTY(()) dw_die_ref single_comp_unit_die;
3124
3125 /* A list of type DIEs that have been separated into comdat sections. */
3126 static GTY(()) comdat_type_node *comdat_type_list;
3127
3128 /* A list of CU DIEs that have been separated. */
3129 static GTY(()) limbo_die_node *cu_die_list;
3130
3131 /* A list of DIEs with a NULL parent waiting to be relocated. */
3132 static GTY(()) limbo_die_node *limbo_die_list;
3133
3134 /* A list of DIEs for which we may have to generate
3135 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3136 static GTY(()) limbo_die_node *deferred_asm_name;
3137
3138 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3139 {
3140 typedef const char *compare_type;
3141
3142 static hashval_t hash (dwarf_file_data *);
3143 static bool equal (dwarf_file_data *, const char *);
3144 };
3145
3146 /* Filenames referenced by this compilation unit. */
3147 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3148
3149 struct decl_die_hasher : ggc_ptr_hash<die_node>
3150 {
3151 typedef tree compare_type;
3152
3153 static hashval_t hash (die_node *);
3154 static bool equal (die_node *, tree);
3155 };
3156 /* A hash table of references to DIE's that describe declarations.
3157 The key is a DECL_UID() which is a unique number identifying each decl. */
3158 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3159
3160 struct GTY ((for_user)) variable_value_struct {
3161 unsigned int decl_id;
3162 vec<dw_die_ref, va_gc> *dies;
3163 };
3164
3165 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3166 {
3167 typedef tree compare_type;
3168
3169 static hashval_t hash (variable_value_struct *);
3170 static bool equal (variable_value_struct *, tree);
3171 };
3172 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3173 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3174 DECL_CONTEXT of the referenced VAR_DECLs. */
3175 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3176
3177 struct block_die_hasher : ggc_ptr_hash<die_struct>
3178 {
3179 static hashval_t hash (die_struct *);
3180 static bool equal (die_struct *, die_struct *);
3181 };
3182
3183 /* A hash table of references to DIE's that describe COMMON blocks.
3184 The key is DECL_UID() ^ die_parent. */
3185 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3186
3187 typedef struct GTY(()) die_arg_entry_struct {
3188 dw_die_ref die;
3189 tree arg;
3190 } die_arg_entry;
3191
3192
3193 /* Node of the variable location list. */
3194 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3195 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3196 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3197 in mode of the EXPR_LIST node and first EXPR_LIST operand
3198 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3199 location or NULL for padding. For larger bitsizes,
3200 mode is 0 and first operand is a CONCAT with bitsize
3201 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3202 NULL as second operand. */
3203 rtx GTY (()) loc;
3204 const char * GTY (()) label;
3205 struct var_loc_node * GTY (()) next;
3206 };
3207
3208 /* Variable location list. */
3209 struct GTY ((for_user)) var_loc_list_def {
3210 struct var_loc_node * GTY (()) first;
3211
3212 /* Pointer to the last but one or last element of the
3213 chained list. If the list is empty, both first and
3214 last are NULL, if the list contains just one node
3215 or the last node certainly is not redundant, it points
3216 to the last node, otherwise points to the last but one.
3217 Do not mark it for GC because it is marked through the chain. */
3218 struct var_loc_node * GTY ((skip ("%h"))) last;
3219
3220 /* Pointer to the last element before section switch,
3221 if NULL, either sections weren't switched or first
3222 is after section switch. */
3223 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3224
3225 /* DECL_UID of the variable decl. */
3226 unsigned int decl_id;
3227 };
3228 typedef struct var_loc_list_def var_loc_list;
3229
3230 /* Call argument location list. */
3231 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3232 rtx GTY (()) call_arg_loc_note;
3233 const char * GTY (()) label;
3234 tree GTY (()) block;
3235 bool tail_call_p;
3236 rtx GTY (()) symbol_ref;
3237 struct call_arg_loc_node * GTY (()) next;
3238 };
3239
3240
3241 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3242 {
3243 typedef const_tree compare_type;
3244
3245 static hashval_t hash (var_loc_list *);
3246 static bool equal (var_loc_list *, const_tree);
3247 };
3248
3249 /* Table of decl location linked lists. */
3250 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3251
3252 /* Head and tail of call_arg_loc chain. */
3253 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3254 static struct call_arg_loc_node *call_arg_loc_last;
3255
3256 /* Number of call sites in the current function. */
3257 static int call_site_count = -1;
3258 /* Number of tail call sites in the current function. */
3259 static int tail_call_site_count = -1;
3260
3261 /* A cached location list. */
3262 struct GTY ((for_user)) cached_dw_loc_list_def {
3263 /* The DECL_UID of the decl that this entry describes. */
3264 unsigned int decl_id;
3265
3266 /* The cached location list. */
3267 dw_loc_list_ref loc_list;
3268 };
3269 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3270
3271 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3272 {
3273
3274 typedef const_tree compare_type;
3275
3276 static hashval_t hash (cached_dw_loc_list *);
3277 static bool equal (cached_dw_loc_list *, const_tree);
3278 };
3279
3280 /* Table of cached location lists. */
3281 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3282
3283 /* A vector of references to DIE's that are uniquely identified by their tag,
3284 presence/absence of children DIE's, and list of attribute/value pairs. */
3285 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3286
3287 /* A hash map to remember the stack usage for DWARF procedures. The value
3288 stored is the stack size difference between before the DWARF procedure
3289 invokation and after it returned. In other words, for a DWARF procedure
3290 that consumes N stack slots and that pushes M ones, this stores M - N. */
3291 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3292
3293 /* A global counter for generating labels for line number data. */
3294 static unsigned int line_info_label_num;
3295
3296 /* The current table to which we should emit line number information
3297 for the current function. This will be set up at the beginning of
3298 assembly for the function. */
3299 static GTY(()) dw_line_info_table *cur_line_info_table;
3300
3301 /* The two default tables of line number info. */
3302 static GTY(()) dw_line_info_table *text_section_line_info;
3303 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3304
3305 /* The set of all non-default tables of line number info. */
3306 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3307
3308 /* A flag to tell pubnames/types export if there is an info section to
3309 refer to. */
3310 static bool info_section_emitted;
3311
3312 /* A pointer to the base of a table that contains a list of publicly
3313 accessible names. */
3314 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3315
3316 /* A pointer to the base of a table that contains a list of publicly
3317 accessible types. */
3318 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3319
3320 /* A pointer to the base of a table that contains a list of macro
3321 defines/undefines (and file start/end markers). */
3322 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3323
3324 /* True if .debug_macinfo or .debug_macros section is going to be
3325 emitted. */
3326 #define have_macinfo \
3327 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3328 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3329 && !macinfo_table->is_empty ())
3330
3331 /* Vector of dies for which we should generate .debug_ranges info. */
3332 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3333
3334 /* Vector of pairs of labels referenced in ranges_table. */
3335 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3336
3337 /* Whether we have location lists that need outputting */
3338 static GTY(()) bool have_location_lists;
3339
3340 /* Unique label counter. */
3341 static GTY(()) unsigned int loclabel_num;
3342
3343 /* Unique label counter for point-of-call tables. */
3344 static GTY(()) unsigned int poc_label_num;
3345
3346 /* The last file entry emitted by maybe_emit_file(). */
3347 static GTY(()) struct dwarf_file_data * last_emitted_file;
3348
3349 /* Number of internal labels generated by gen_internal_sym(). */
3350 static GTY(()) int label_num;
3351
3352 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3353
3354 /* Instances of generic types for which we need to generate debug
3355 info that describe their generic parameters and arguments. That
3356 generation needs to happen once all types are properly laid out so
3357 we do it at the end of compilation. */
3358 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3359
3360 /* Offset from the "steady-state frame pointer" to the frame base,
3361 within the current function. */
3362 static poly_int64 frame_pointer_fb_offset;
3363 static bool frame_pointer_fb_offset_valid;
3364
3365 static vec<dw_die_ref> base_types;
3366
3367 /* Flags to represent a set of attribute classes for attributes that represent
3368 a scalar value (bounds, pointers, ...). */
3369 enum dw_scalar_form
3370 {
3371 dw_scalar_form_constant = 0x01,
3372 dw_scalar_form_exprloc = 0x02,
3373 dw_scalar_form_reference = 0x04
3374 };
3375
3376 /* Forward declarations for functions defined in this file. */
3377
3378 static int is_pseudo_reg (const_rtx);
3379 static tree type_main_variant (tree);
3380 static int is_tagged_type (const_tree);
3381 static const char *dwarf_tag_name (unsigned);
3382 static const char *dwarf_attr_name (unsigned);
3383 static const char *dwarf_form_name (unsigned);
3384 static tree decl_ultimate_origin (const_tree);
3385 static tree decl_class_context (tree);
3386 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3387 static inline enum dw_val_class AT_class (dw_attr_node *);
3388 static inline unsigned int AT_index (dw_attr_node *);
3389 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3390 static inline unsigned AT_flag (dw_attr_node *);
3391 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3392 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3393 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3394 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3395 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3396 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3397 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3398 unsigned int, unsigned char *);
3399 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3400 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3401 static inline const char *AT_string (dw_attr_node *);
3402 static enum dwarf_form AT_string_form (dw_attr_node *);
3403 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3404 static void add_AT_specification (dw_die_ref, dw_die_ref);
3405 static inline dw_die_ref AT_ref (dw_attr_node *);
3406 static inline int AT_ref_external (dw_attr_node *);
3407 static inline void set_AT_ref_external (dw_attr_node *, int);
3408 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3409 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3410 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3411 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3412 dw_loc_list_ref);
3413 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3414 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3415 static void remove_addr_table_entry (addr_table_entry *);
3416 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3417 static inline rtx AT_addr (dw_attr_node *);
3418 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3419 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3420 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3421 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3422 const char *);
3423 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3424 unsigned HOST_WIDE_INT);
3425 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3426 unsigned long, bool);
3427 static inline const char *AT_lbl (dw_attr_node *);
3428 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3429 static const char *get_AT_low_pc (dw_die_ref);
3430 static const char *get_AT_hi_pc (dw_die_ref);
3431 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3432 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3433 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3434 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3435 static bool is_cxx (void);
3436 static bool is_cxx (const_tree);
3437 static bool is_fortran (void);
3438 static bool is_ada (void);
3439 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3440 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3441 static void add_child_die (dw_die_ref, dw_die_ref);
3442 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3443 static dw_die_ref lookup_type_die (tree);
3444 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3445 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3446 static void equate_type_number_to_die (tree, dw_die_ref);
3447 static dw_die_ref lookup_decl_die (tree);
3448 static var_loc_list *lookup_decl_loc (const_tree);
3449 static void equate_decl_number_to_die (tree, dw_die_ref);
3450 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3451 static void print_spaces (FILE *);
3452 static void print_die (dw_die_ref, FILE *);
3453 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3454 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3455 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3456 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3457 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3458 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3459 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3460 struct md5_ctx *, int *);
3461 struct checksum_attributes;
3462 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3463 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3464 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3465 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3466 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3467 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3468 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3469 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3470 static int is_type_die (dw_die_ref);
3471 static int is_comdat_die (dw_die_ref);
3472 static inline bool is_template_instantiation (dw_die_ref);
3473 static int is_declaration_die (dw_die_ref);
3474 static int should_move_die_to_comdat (dw_die_ref);
3475 static dw_die_ref clone_as_declaration (dw_die_ref);
3476 static dw_die_ref clone_die (dw_die_ref);
3477 static dw_die_ref clone_tree (dw_die_ref);
3478 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3479 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3480 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3481 static dw_die_ref generate_skeleton (dw_die_ref);
3482 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3483 dw_die_ref,
3484 dw_die_ref);
3485 static void break_out_comdat_types (dw_die_ref);
3486 static void copy_decls_for_unworthy_types (dw_die_ref);
3487
3488 static void add_sibling_attributes (dw_die_ref);
3489 static void output_location_lists (dw_die_ref);
3490 static int constant_size (unsigned HOST_WIDE_INT);
3491 static unsigned long size_of_die (dw_die_ref);
3492 static void calc_die_sizes (dw_die_ref);
3493 static void calc_base_type_die_sizes (void);
3494 static void mark_dies (dw_die_ref);
3495 static void unmark_dies (dw_die_ref);
3496 static void unmark_all_dies (dw_die_ref);
3497 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3498 static unsigned long size_of_aranges (void);
3499 static enum dwarf_form value_format (dw_attr_node *);
3500 static void output_value_format (dw_attr_node *);
3501 static void output_abbrev_section (void);
3502 static void output_die_abbrevs (unsigned long, dw_die_ref);
3503 static void output_die (dw_die_ref);
3504 static void output_compilation_unit_header (enum dwarf_unit_type);
3505 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3506 static void output_comdat_type_unit (comdat_type_node *);
3507 static const char *dwarf2_name (tree, int);
3508 static void add_pubname (tree, dw_die_ref);
3509 static void add_enumerator_pubname (const char *, dw_die_ref);
3510 static void add_pubname_string (const char *, dw_die_ref);
3511 static void add_pubtype (tree, dw_die_ref);
3512 static void output_pubnames (vec<pubname_entry, va_gc> *);
3513 static void output_aranges (void);
3514 static unsigned int add_ranges (const_tree, bool = false);
3515 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3516 bool *, bool);
3517 static void output_ranges (void);
3518 static dw_line_info_table *new_line_info_table (void);
3519 static void output_line_info (bool);
3520 static void output_file_names (void);
3521 static dw_die_ref base_type_die (tree, bool);
3522 static int is_base_type (tree);
3523 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3524 static int decl_quals (const_tree);
3525 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3526 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3527 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3528 static int type_is_enum (const_tree);
3529 static unsigned int dbx_reg_number (const_rtx);
3530 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3531 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3532 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3533 enum var_init_status);
3534 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3535 enum var_init_status);
3536 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3537 enum var_init_status);
3538 static int is_based_loc (const_rtx);
3539 static bool resolve_one_addr (rtx *);
3540 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3541 enum var_init_status);
3542 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3543 enum var_init_status);
3544 struct loc_descr_context;
3545 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3546 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3547 static dw_loc_list_ref loc_list_from_tree (tree, int,
3548 struct loc_descr_context *);
3549 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3550 struct loc_descr_context *);
3551 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3552 static tree field_type (const_tree);
3553 static unsigned int simple_type_align_in_bits (const_tree);
3554 static unsigned int simple_decl_align_in_bits (const_tree);
3555 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3556 struct vlr_context;
3557 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3558 HOST_WIDE_INT *);
3559 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3560 dw_loc_list_ref);
3561 static void add_data_member_location_attribute (dw_die_ref, tree,
3562 struct vlr_context *);
3563 static bool add_const_value_attribute (dw_die_ref, rtx);
3564 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3565 static void insert_wide_int (const wide_int &, unsigned char *, int);
3566 static void insert_float (const_rtx, unsigned char *);
3567 static rtx rtl_for_decl_location (tree);
3568 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3569 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3570 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3571 static void add_name_attribute (dw_die_ref, const char *);
3572 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3573 static void add_comp_dir_attribute (dw_die_ref);
3574 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3575 struct loc_descr_context *);
3576 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3577 struct loc_descr_context *);
3578 static void add_subscript_info (dw_die_ref, tree, bool);
3579 static void add_byte_size_attribute (dw_die_ref, tree);
3580 static void add_alignment_attribute (dw_die_ref, tree);
3581 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3582 struct vlr_context *);
3583 static void add_bit_size_attribute (dw_die_ref, tree);
3584 static void add_prototyped_attribute (dw_die_ref, tree);
3585 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3586 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3587 static void add_src_coords_attributes (dw_die_ref, tree);
3588 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3589 static void add_discr_value (dw_die_ref, dw_discr_value *);
3590 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3591 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3592 static void push_decl_scope (tree);
3593 static void pop_decl_scope (void);
3594 static dw_die_ref scope_die_for (tree, dw_die_ref);
3595 static inline int local_scope_p (dw_die_ref);
3596 static inline int class_scope_p (dw_die_ref);
3597 static inline int class_or_namespace_scope_p (dw_die_ref);
3598 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3599 static void add_calling_convention_attribute (dw_die_ref, tree);
3600 static const char *type_tag (const_tree);
3601 static tree member_declared_type (const_tree);
3602 #if 0
3603 static const char *decl_start_label (tree);
3604 #endif
3605 static void gen_array_type_die (tree, dw_die_ref);
3606 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3607 #if 0
3608 static void gen_entry_point_die (tree, dw_die_ref);
3609 #endif
3610 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3611 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3612 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3613 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3614 static void gen_formal_types_die (tree, dw_die_ref);
3615 static void gen_subprogram_die (tree, dw_die_ref);
3616 static void gen_variable_die (tree, tree, dw_die_ref);
3617 static void gen_const_die (tree, dw_die_ref);
3618 static void gen_label_die (tree, dw_die_ref);
3619 static void gen_lexical_block_die (tree, dw_die_ref);
3620 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3621 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3622 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3623 static dw_die_ref gen_compile_unit_die (const char *);
3624 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3625 static void gen_member_die (tree, dw_die_ref);
3626 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3627 enum debug_info_usage);
3628 static void gen_subroutine_type_die (tree, dw_die_ref);
3629 static void gen_typedef_die (tree, dw_die_ref);
3630 static void gen_type_die (tree, dw_die_ref);
3631 static void gen_block_die (tree, dw_die_ref);
3632 static void decls_for_scope (tree, dw_die_ref);
3633 static bool is_naming_typedef_decl (const_tree);
3634 static inline dw_die_ref get_context_die (tree);
3635 static void gen_namespace_die (tree, dw_die_ref);
3636 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3637 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3638 static dw_die_ref force_decl_die (tree);
3639 static dw_die_ref force_type_die (tree);
3640 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3641 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3642 static struct dwarf_file_data * lookup_filename (const char *);
3643 static void retry_incomplete_types (void);
3644 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3645 static void gen_generic_params_dies (tree);
3646 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3647 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3648 static void splice_child_die (dw_die_ref, dw_die_ref);
3649 static int file_info_cmp (const void *, const void *);
3650 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3651 const char *, const char *);
3652 static void output_loc_list (dw_loc_list_ref);
3653 static char *gen_internal_sym (const char *);
3654 static bool want_pubnames (void);
3655
3656 static void prune_unmark_dies (dw_die_ref);
3657 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3658 static void prune_unused_types_mark (dw_die_ref, int);
3659 static void prune_unused_types_walk (dw_die_ref);
3660 static void prune_unused_types_walk_attribs (dw_die_ref);
3661 static void prune_unused_types_prune (dw_die_ref);
3662 static void prune_unused_types (void);
3663 static int maybe_emit_file (struct dwarf_file_data *fd);
3664 static inline const char *AT_vms_delta1 (dw_attr_node *);
3665 static inline const char *AT_vms_delta2 (dw_attr_node *);
3666 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3667 const char *, const char *);
3668 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3669 static void gen_remaining_tmpl_value_param_die_attribute (void);
3670 static bool generic_type_p (tree);
3671 static void schedule_generic_params_dies_gen (tree t);
3672 static void gen_scheduled_generic_parms_dies (void);
3673 static void resolve_variable_values (void);
3674
3675 static const char *comp_dir_string (void);
3676
3677 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3678
3679 /* enum for tracking thread-local variables whose address is really an offset
3680 relative to the TLS pointer, which will need link-time relocation, but will
3681 not need relocation by the DWARF consumer. */
3682
3683 enum dtprel_bool
3684 {
3685 dtprel_false = 0,
3686 dtprel_true = 1
3687 };
3688
3689 /* Return the operator to use for an address of a variable. For dtprel_true, we
3690 use DW_OP_const*. For regular variables, which need both link-time
3691 relocation and consumer-level relocation (e.g., to account for shared objects
3692 loaded at a random address), we use DW_OP_addr*. */
3693
3694 static inline enum dwarf_location_atom
3695 dw_addr_op (enum dtprel_bool dtprel)
3696 {
3697 if (dtprel == dtprel_true)
3698 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3699 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3700 else
3701 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3702 }
3703
3704 /* Return a pointer to a newly allocated address location description. If
3705 dwarf_split_debug_info is true, then record the address with the appropriate
3706 relocation. */
3707 static inline dw_loc_descr_ref
3708 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3709 {
3710 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3711
3712 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3713 ref->dw_loc_oprnd1.v.val_addr = addr;
3714 ref->dtprel = dtprel;
3715 if (dwarf_split_debug_info)
3716 ref->dw_loc_oprnd1.val_entry
3717 = add_addr_table_entry (addr,
3718 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3719 else
3720 ref->dw_loc_oprnd1.val_entry = NULL;
3721
3722 return ref;
3723 }
3724
3725 /* Section names used to hold DWARF debugging information. */
3726
3727 #ifndef DEBUG_INFO_SECTION
3728 #define DEBUG_INFO_SECTION ".debug_info"
3729 #endif
3730 #ifndef DEBUG_DWO_INFO_SECTION
3731 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3732 #endif
3733 #ifndef DEBUG_LTO_INFO_SECTION
3734 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3735 #endif
3736 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3737 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3738 #endif
3739 #ifndef DEBUG_ABBREV_SECTION
3740 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3741 #endif
3742 #ifndef DEBUG_LTO_ABBREV_SECTION
3743 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3744 #endif
3745 #ifndef DEBUG_DWO_ABBREV_SECTION
3746 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3747 #endif
3748 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3749 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3750 #endif
3751 #ifndef DEBUG_ARANGES_SECTION
3752 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3753 #endif
3754 #ifndef DEBUG_ADDR_SECTION
3755 #define DEBUG_ADDR_SECTION ".debug_addr"
3756 #endif
3757 #ifndef DEBUG_MACINFO_SECTION
3758 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3759 #endif
3760 #ifndef DEBUG_LTO_MACINFO_SECTION
3761 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3762 #endif
3763 #ifndef DEBUG_DWO_MACINFO_SECTION
3764 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3765 #endif
3766 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3767 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3768 #endif
3769 #ifndef DEBUG_MACRO_SECTION
3770 #define DEBUG_MACRO_SECTION ".debug_macro"
3771 #endif
3772 #ifndef DEBUG_LTO_MACRO_SECTION
3773 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3774 #endif
3775 #ifndef DEBUG_DWO_MACRO_SECTION
3776 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3777 #endif
3778 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3779 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3780 #endif
3781 #ifndef DEBUG_LINE_SECTION
3782 #define DEBUG_LINE_SECTION ".debug_line"
3783 #endif
3784 #ifndef DEBUG_LTO_LINE_SECTION
3785 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3786 #endif
3787 #ifndef DEBUG_DWO_LINE_SECTION
3788 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3789 #endif
3790 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3791 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3792 #endif
3793 #ifndef DEBUG_LOC_SECTION
3794 #define DEBUG_LOC_SECTION ".debug_loc"
3795 #endif
3796 #ifndef DEBUG_DWO_LOC_SECTION
3797 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3798 #endif
3799 #ifndef DEBUG_LOCLISTS_SECTION
3800 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3801 #endif
3802 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3803 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3804 #endif
3805 #ifndef DEBUG_PUBNAMES_SECTION
3806 #define DEBUG_PUBNAMES_SECTION \
3807 ((debug_generate_pub_sections == 2) \
3808 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3809 #endif
3810 #ifndef DEBUG_PUBTYPES_SECTION
3811 #define DEBUG_PUBTYPES_SECTION \
3812 ((debug_generate_pub_sections == 2) \
3813 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3814 #endif
3815 #ifndef DEBUG_STR_OFFSETS_SECTION
3816 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3817 #endif
3818 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3819 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3820 #endif
3821 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3822 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3823 #endif
3824 #ifndef DEBUG_STR_SECTION
3825 #define DEBUG_STR_SECTION ".debug_str"
3826 #endif
3827 #ifndef DEBUG_LTO_STR_SECTION
3828 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3829 #endif
3830 #ifndef DEBUG_STR_DWO_SECTION
3831 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3832 #endif
3833 #ifndef DEBUG_LTO_STR_DWO_SECTION
3834 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3835 #endif
3836 #ifndef DEBUG_RANGES_SECTION
3837 #define DEBUG_RANGES_SECTION ".debug_ranges"
3838 #endif
3839 #ifndef DEBUG_RNGLISTS_SECTION
3840 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3841 #endif
3842 #ifndef DEBUG_LINE_STR_SECTION
3843 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3844 #endif
3845 #ifndef DEBUG_LTO_LINE_STR_SECTION
3846 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3847 #endif
3848
3849 /* Standard ELF section names for compiled code and data. */
3850 #ifndef TEXT_SECTION_NAME
3851 #define TEXT_SECTION_NAME ".text"
3852 #endif
3853
3854 /* Section flags for .debug_str section. */
3855 #define DEBUG_STR_SECTION_FLAGS \
3856 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3857 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3858 : SECTION_DEBUG)
3859
3860 /* Section flags for .debug_str.dwo section. */
3861 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3862
3863 /* Attribute used to refer to the macro section. */
3864 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3865 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3866
3867 /* Labels we insert at beginning sections we can reference instead of
3868 the section names themselves. */
3869
3870 #ifndef TEXT_SECTION_LABEL
3871 #define TEXT_SECTION_LABEL "Ltext"
3872 #endif
3873 #ifndef COLD_TEXT_SECTION_LABEL
3874 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3875 #endif
3876 #ifndef DEBUG_LINE_SECTION_LABEL
3877 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3878 #endif
3879 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3880 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3881 #endif
3882 #ifndef DEBUG_INFO_SECTION_LABEL
3883 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3884 #endif
3885 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3886 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3887 #endif
3888 #ifndef DEBUG_ABBREV_SECTION_LABEL
3889 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3890 #endif
3891 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3892 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3893 #endif
3894 #ifndef DEBUG_ADDR_SECTION_LABEL
3895 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3896 #endif
3897 #ifndef DEBUG_LOC_SECTION_LABEL
3898 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3899 #endif
3900 #ifndef DEBUG_RANGES_SECTION_LABEL
3901 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3902 #endif
3903 #ifndef DEBUG_MACINFO_SECTION_LABEL
3904 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3905 #endif
3906 #ifndef DEBUG_MACRO_SECTION_LABEL
3907 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3908 #endif
3909 #define SKELETON_COMP_DIE_ABBREV 1
3910 #define SKELETON_TYPE_DIE_ABBREV 2
3911
3912 /* Definitions of defaults for formats and names of various special
3913 (artificial) labels which may be generated within this file (when the -g
3914 options is used and DWARF2_DEBUGGING_INFO is in effect.
3915 If necessary, these may be overridden from within the tm.h file, but
3916 typically, overriding these defaults is unnecessary. */
3917
3918 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3919 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3920 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3921 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3922 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3923 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3924 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3925 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3926 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3927 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3928 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3929 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3930 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3931 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3932 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3933
3934 #ifndef TEXT_END_LABEL
3935 #define TEXT_END_LABEL "Letext"
3936 #endif
3937 #ifndef COLD_END_LABEL
3938 #define COLD_END_LABEL "Letext_cold"
3939 #endif
3940 #ifndef BLOCK_BEGIN_LABEL
3941 #define BLOCK_BEGIN_LABEL "LBB"
3942 #endif
3943 #ifndef BLOCK_END_LABEL
3944 #define BLOCK_END_LABEL "LBE"
3945 #endif
3946 #ifndef LINE_CODE_LABEL
3947 #define LINE_CODE_LABEL "LM"
3948 #endif
3949
3950 \f
3951 /* Return the root of the DIE's built for the current compilation unit. */
3952 static dw_die_ref
3953 comp_unit_die (void)
3954 {
3955 if (!single_comp_unit_die)
3956 single_comp_unit_die = gen_compile_unit_die (NULL);
3957 return single_comp_unit_die;
3958 }
3959
3960 /* We allow a language front-end to designate a function that is to be
3961 called to "demangle" any name before it is put into a DIE. */
3962
3963 static const char *(*demangle_name_func) (const char *);
3964
3965 void
3966 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3967 {
3968 demangle_name_func = func;
3969 }
3970
3971 /* Test if rtl node points to a pseudo register. */
3972
3973 static inline int
3974 is_pseudo_reg (const_rtx rtl)
3975 {
3976 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3977 || (GET_CODE (rtl) == SUBREG
3978 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3979 }
3980
3981 /* Return a reference to a type, with its const and volatile qualifiers
3982 removed. */
3983
3984 static inline tree
3985 type_main_variant (tree type)
3986 {
3987 type = TYPE_MAIN_VARIANT (type);
3988
3989 /* ??? There really should be only one main variant among any group of
3990 variants of a given type (and all of the MAIN_VARIANT values for all
3991 members of the group should point to that one type) but sometimes the C
3992 front-end messes this up for array types, so we work around that bug
3993 here. */
3994 if (TREE_CODE (type) == ARRAY_TYPE)
3995 while (type != TYPE_MAIN_VARIANT (type))
3996 type = TYPE_MAIN_VARIANT (type);
3997
3998 return type;
3999 }
4000
4001 /* Return nonzero if the given type node represents a tagged type. */
4002
4003 static inline int
4004 is_tagged_type (const_tree type)
4005 {
4006 enum tree_code code = TREE_CODE (type);
4007
4008 return (code == RECORD_TYPE || code == UNION_TYPE
4009 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4010 }
4011
4012 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4013
4014 static void
4015 get_ref_die_offset_label (char *label, dw_die_ref ref)
4016 {
4017 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4018 }
4019
4020 /* Return die_offset of a DIE reference to a base type. */
4021
4022 static unsigned long int
4023 get_base_type_offset (dw_die_ref ref)
4024 {
4025 if (ref->die_offset)
4026 return ref->die_offset;
4027 if (comp_unit_die ()->die_abbrev)
4028 {
4029 calc_base_type_die_sizes ();
4030 gcc_assert (ref->die_offset);
4031 }
4032 return ref->die_offset;
4033 }
4034
4035 /* Return die_offset of a DIE reference other than base type. */
4036
4037 static unsigned long int
4038 get_ref_die_offset (dw_die_ref ref)
4039 {
4040 gcc_assert (ref->die_offset);
4041 return ref->die_offset;
4042 }
4043
4044 /* Convert a DIE tag into its string name. */
4045
4046 static const char *
4047 dwarf_tag_name (unsigned int tag)
4048 {
4049 const char *name = get_DW_TAG_name (tag);
4050
4051 if (name != NULL)
4052 return name;
4053
4054 return "DW_TAG_<unknown>";
4055 }
4056
4057 /* Convert a DWARF attribute code into its string name. */
4058
4059 static const char *
4060 dwarf_attr_name (unsigned int attr)
4061 {
4062 const char *name;
4063
4064 switch (attr)
4065 {
4066 #if VMS_DEBUGGING_INFO
4067 case DW_AT_HP_prologue:
4068 return "DW_AT_HP_prologue";
4069 #else
4070 case DW_AT_MIPS_loop_unroll_factor:
4071 return "DW_AT_MIPS_loop_unroll_factor";
4072 #endif
4073
4074 #if VMS_DEBUGGING_INFO
4075 case DW_AT_HP_epilogue:
4076 return "DW_AT_HP_epilogue";
4077 #else
4078 case DW_AT_MIPS_stride:
4079 return "DW_AT_MIPS_stride";
4080 #endif
4081 }
4082
4083 name = get_DW_AT_name (attr);
4084
4085 if (name != NULL)
4086 return name;
4087
4088 return "DW_AT_<unknown>";
4089 }
4090
4091 /* Convert a DWARF value form code into its string name. */
4092
4093 static const char *
4094 dwarf_form_name (unsigned int form)
4095 {
4096 const char *name = get_DW_FORM_name (form);
4097
4098 if (name != NULL)
4099 return name;
4100
4101 return "DW_FORM_<unknown>";
4102 }
4103 \f
4104 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4105 instance of an inlined instance of a decl which is local to an inline
4106 function, so we have to trace all of the way back through the origin chain
4107 to find out what sort of node actually served as the original seed for the
4108 given block. */
4109
4110 static tree
4111 decl_ultimate_origin (const_tree decl)
4112 {
4113 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4114 return NULL_TREE;
4115
4116 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4117 we're trying to output the abstract instance of this function. */
4118 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4119 return NULL_TREE;
4120
4121 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4122 most distant ancestor, this should never happen. */
4123 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4124
4125 return DECL_ABSTRACT_ORIGIN (decl);
4126 }
4127
4128 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4129 of a virtual function may refer to a base class, so we check the 'this'
4130 parameter. */
4131
4132 static tree
4133 decl_class_context (tree decl)
4134 {
4135 tree context = NULL_TREE;
4136
4137 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4138 context = DECL_CONTEXT (decl);
4139 else
4140 context = TYPE_MAIN_VARIANT
4141 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4142
4143 if (context && !TYPE_P (context))
4144 context = NULL_TREE;
4145
4146 return context;
4147 }
4148 \f
4149 /* Add an attribute/value pair to a DIE. */
4150
4151 static inline void
4152 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4153 {
4154 /* Maybe this should be an assert? */
4155 if (die == NULL)
4156 return;
4157
4158 if (flag_checking)
4159 {
4160 /* Check we do not add duplicate attrs. Can't use get_AT here
4161 because that recurses to the specification/abstract origin DIE. */
4162 dw_attr_node *a;
4163 unsigned ix;
4164 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4165 gcc_assert (a->dw_attr != attr->dw_attr);
4166 }
4167
4168 vec_safe_reserve (die->die_attr, 1);
4169 vec_safe_push (die->die_attr, *attr);
4170 }
4171
4172 static inline enum dw_val_class
4173 AT_class (dw_attr_node *a)
4174 {
4175 return a->dw_attr_val.val_class;
4176 }
4177
4178 /* Return the index for any attribute that will be referenced with a
4179 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4180 are stored in dw_attr_val.v.val_str for reference counting
4181 pruning. */
4182
4183 static inline unsigned int
4184 AT_index (dw_attr_node *a)
4185 {
4186 if (AT_class (a) == dw_val_class_str)
4187 return a->dw_attr_val.v.val_str->index;
4188 else if (a->dw_attr_val.val_entry != NULL)
4189 return a->dw_attr_val.val_entry->index;
4190 return NOT_INDEXED;
4191 }
4192
4193 /* Add a flag value attribute to a DIE. */
4194
4195 static inline void
4196 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4197 {
4198 dw_attr_node attr;
4199
4200 attr.dw_attr = attr_kind;
4201 attr.dw_attr_val.val_class = dw_val_class_flag;
4202 attr.dw_attr_val.val_entry = NULL;
4203 attr.dw_attr_val.v.val_flag = flag;
4204 add_dwarf_attr (die, &attr);
4205 }
4206
4207 static inline unsigned
4208 AT_flag (dw_attr_node *a)
4209 {
4210 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4211 return a->dw_attr_val.v.val_flag;
4212 }
4213
4214 /* Add a signed integer attribute value to a DIE. */
4215
4216 static inline void
4217 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4218 {
4219 dw_attr_node attr;
4220
4221 attr.dw_attr = attr_kind;
4222 attr.dw_attr_val.val_class = dw_val_class_const;
4223 attr.dw_attr_val.val_entry = NULL;
4224 attr.dw_attr_val.v.val_int = int_val;
4225 add_dwarf_attr (die, &attr);
4226 }
4227
4228 static inline HOST_WIDE_INT
4229 AT_int (dw_attr_node *a)
4230 {
4231 gcc_assert (a && (AT_class (a) == dw_val_class_const
4232 || AT_class (a) == dw_val_class_const_implicit));
4233 return a->dw_attr_val.v.val_int;
4234 }
4235
4236 /* Add an unsigned integer attribute value to a DIE. */
4237
4238 static inline void
4239 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4240 unsigned HOST_WIDE_INT unsigned_val)
4241 {
4242 dw_attr_node attr;
4243
4244 attr.dw_attr = attr_kind;
4245 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4246 attr.dw_attr_val.val_entry = NULL;
4247 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4248 add_dwarf_attr (die, &attr);
4249 }
4250
4251 static inline unsigned HOST_WIDE_INT
4252 AT_unsigned (dw_attr_node *a)
4253 {
4254 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4255 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4256 return a->dw_attr_val.v.val_unsigned;
4257 }
4258
4259 /* Add an unsigned wide integer attribute value to a DIE. */
4260
4261 static inline void
4262 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4263 const wide_int& w)
4264 {
4265 dw_attr_node attr;
4266
4267 attr.dw_attr = attr_kind;
4268 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4269 attr.dw_attr_val.val_entry = NULL;
4270 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4271 *attr.dw_attr_val.v.val_wide = w;
4272 add_dwarf_attr (die, &attr);
4273 }
4274
4275 /* Add an unsigned double integer attribute value to a DIE. */
4276
4277 static inline void
4278 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4279 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4280 {
4281 dw_attr_node attr;
4282
4283 attr.dw_attr = attr_kind;
4284 attr.dw_attr_val.val_class = dw_val_class_const_double;
4285 attr.dw_attr_val.val_entry = NULL;
4286 attr.dw_attr_val.v.val_double.high = high;
4287 attr.dw_attr_val.v.val_double.low = low;
4288 add_dwarf_attr (die, &attr);
4289 }
4290
4291 /* Add a floating point attribute value to a DIE and return it. */
4292
4293 static inline void
4294 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4295 unsigned int length, unsigned int elt_size, unsigned char *array)
4296 {
4297 dw_attr_node attr;
4298
4299 attr.dw_attr = attr_kind;
4300 attr.dw_attr_val.val_class = dw_val_class_vec;
4301 attr.dw_attr_val.val_entry = NULL;
4302 attr.dw_attr_val.v.val_vec.length = length;
4303 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4304 attr.dw_attr_val.v.val_vec.array = array;
4305 add_dwarf_attr (die, &attr);
4306 }
4307
4308 /* Add an 8-byte data attribute value to a DIE. */
4309
4310 static inline void
4311 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4312 unsigned char data8[8])
4313 {
4314 dw_attr_node attr;
4315
4316 attr.dw_attr = attr_kind;
4317 attr.dw_attr_val.val_class = dw_val_class_data8;
4318 attr.dw_attr_val.val_entry = NULL;
4319 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4320 add_dwarf_attr (die, &attr);
4321 }
4322
4323 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4324 dwarf_split_debug_info, address attributes in dies destined for the
4325 final executable have force_direct set to avoid using indexed
4326 references. */
4327
4328 static inline void
4329 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4330 bool force_direct)
4331 {
4332 dw_attr_node attr;
4333 char * lbl_id;
4334
4335 lbl_id = xstrdup (lbl_low);
4336 attr.dw_attr = DW_AT_low_pc;
4337 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4338 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4339 if (dwarf_split_debug_info && !force_direct)
4340 attr.dw_attr_val.val_entry
4341 = add_addr_table_entry (lbl_id, ate_kind_label);
4342 else
4343 attr.dw_attr_val.val_entry = NULL;
4344 add_dwarf_attr (die, &attr);
4345
4346 attr.dw_attr = DW_AT_high_pc;
4347 if (dwarf_version < 4)
4348 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4349 else
4350 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4351 lbl_id = xstrdup (lbl_high);
4352 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4353 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4354 && dwarf_split_debug_info && !force_direct)
4355 attr.dw_attr_val.val_entry
4356 = add_addr_table_entry (lbl_id, ate_kind_label);
4357 else
4358 attr.dw_attr_val.val_entry = NULL;
4359 add_dwarf_attr (die, &attr);
4360 }
4361
4362 /* Hash and equality functions for debug_str_hash. */
4363
4364 hashval_t
4365 indirect_string_hasher::hash (indirect_string_node *x)
4366 {
4367 return htab_hash_string (x->str);
4368 }
4369
4370 bool
4371 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4372 {
4373 return strcmp (x1->str, x2) == 0;
4374 }
4375
4376 /* Add STR to the given string hash table. */
4377
4378 static struct indirect_string_node *
4379 find_AT_string_in_table (const char *str,
4380 hash_table<indirect_string_hasher> *table)
4381 {
4382 struct indirect_string_node *node;
4383
4384 indirect_string_node **slot
4385 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4386 if (*slot == NULL)
4387 {
4388 node = ggc_cleared_alloc<indirect_string_node> ();
4389 node->str = ggc_strdup (str);
4390 *slot = node;
4391 }
4392 else
4393 node = *slot;
4394
4395 node->refcount++;
4396 return node;
4397 }
4398
4399 /* Add STR to the indirect string hash table. */
4400
4401 static struct indirect_string_node *
4402 find_AT_string (const char *str)
4403 {
4404 if (! debug_str_hash)
4405 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4406
4407 return find_AT_string_in_table (str, debug_str_hash);
4408 }
4409
4410 /* Add a string attribute value to a DIE. */
4411
4412 static inline void
4413 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4414 {
4415 dw_attr_node attr;
4416 struct indirect_string_node *node;
4417
4418 node = find_AT_string (str);
4419
4420 attr.dw_attr = attr_kind;
4421 attr.dw_attr_val.val_class = dw_val_class_str;
4422 attr.dw_attr_val.val_entry = NULL;
4423 attr.dw_attr_val.v.val_str = node;
4424 add_dwarf_attr (die, &attr);
4425 }
4426
4427 static inline const char *
4428 AT_string (dw_attr_node *a)
4429 {
4430 gcc_assert (a && AT_class (a) == dw_val_class_str);
4431 return a->dw_attr_val.v.val_str->str;
4432 }
4433
4434 /* Call this function directly to bypass AT_string_form's logic to put
4435 the string inline in the die. */
4436
4437 static void
4438 set_indirect_string (struct indirect_string_node *node)
4439 {
4440 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4441 /* Already indirect is a no op. */
4442 if (node->form == DW_FORM_strp
4443 || node->form == DW_FORM_line_strp
4444 || node->form == DW_FORM_GNU_str_index)
4445 {
4446 gcc_assert (node->label);
4447 return;
4448 }
4449 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4450 ++dw2_string_counter;
4451 node->label = xstrdup (label);
4452
4453 if (!dwarf_split_debug_info)
4454 {
4455 node->form = DW_FORM_strp;
4456 node->index = NOT_INDEXED;
4457 }
4458 else
4459 {
4460 node->form = DW_FORM_GNU_str_index;
4461 node->index = NO_INDEX_ASSIGNED;
4462 }
4463 }
4464
4465 /* A helper function for dwarf2out_finish, called to reset indirect
4466 string decisions done for early LTO dwarf output before fat object
4467 dwarf output. */
4468
4469 int
4470 reset_indirect_string (indirect_string_node **h, void *)
4471 {
4472 struct indirect_string_node *node = *h;
4473 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4474 {
4475 free (node->label);
4476 node->label = NULL;
4477 node->form = (dwarf_form) 0;
4478 node->index = 0;
4479 }
4480 return 1;
4481 }
4482
4483 /* Find out whether a string should be output inline in DIE
4484 or out-of-line in .debug_str section. */
4485
4486 static enum dwarf_form
4487 find_string_form (struct indirect_string_node *node)
4488 {
4489 unsigned int len;
4490
4491 if (node->form)
4492 return node->form;
4493
4494 len = strlen (node->str) + 1;
4495
4496 /* If the string is shorter or equal to the size of the reference, it is
4497 always better to put it inline. */
4498 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4499 return node->form = DW_FORM_string;
4500
4501 /* If we cannot expect the linker to merge strings in .debug_str
4502 section, only put it into .debug_str if it is worth even in this
4503 single module. */
4504 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4505 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4506 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4507 return node->form = DW_FORM_string;
4508
4509 set_indirect_string (node);
4510
4511 return node->form;
4512 }
4513
4514 /* Find out whether the string referenced from the attribute should be
4515 output inline in DIE or out-of-line in .debug_str section. */
4516
4517 static enum dwarf_form
4518 AT_string_form (dw_attr_node *a)
4519 {
4520 gcc_assert (a && AT_class (a) == dw_val_class_str);
4521 return find_string_form (a->dw_attr_val.v.val_str);
4522 }
4523
4524 /* Add a DIE reference attribute value to a DIE. */
4525
4526 static inline void
4527 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4528 {
4529 dw_attr_node attr;
4530 gcc_checking_assert (targ_die != NULL);
4531
4532 /* With LTO we can end up trying to reference something we didn't create
4533 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4534 if (targ_die == NULL)
4535 return;
4536
4537 attr.dw_attr = attr_kind;
4538 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4539 attr.dw_attr_val.val_entry = NULL;
4540 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4541 attr.dw_attr_val.v.val_die_ref.external = 0;
4542 add_dwarf_attr (die, &attr);
4543 }
4544
4545 /* Change DIE reference REF to point to NEW_DIE instead. */
4546
4547 static inline void
4548 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4549 {
4550 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4551 ref->dw_attr_val.v.val_die_ref.die = new_die;
4552 ref->dw_attr_val.v.val_die_ref.external = 0;
4553 }
4554
4555 /* Add an AT_specification attribute to a DIE, and also make the back
4556 pointer from the specification to the definition. */
4557
4558 static inline void
4559 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4560 {
4561 add_AT_die_ref (die, DW_AT_specification, targ_die);
4562 gcc_assert (!targ_die->die_definition);
4563 targ_die->die_definition = die;
4564 }
4565
4566 static inline dw_die_ref
4567 AT_ref (dw_attr_node *a)
4568 {
4569 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4570 return a->dw_attr_val.v.val_die_ref.die;
4571 }
4572
4573 static inline int
4574 AT_ref_external (dw_attr_node *a)
4575 {
4576 if (a && AT_class (a) == dw_val_class_die_ref)
4577 return a->dw_attr_val.v.val_die_ref.external;
4578
4579 return 0;
4580 }
4581
4582 static inline void
4583 set_AT_ref_external (dw_attr_node *a, int i)
4584 {
4585 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4586 a->dw_attr_val.v.val_die_ref.external = i;
4587 }
4588
4589 /* Add an FDE reference attribute value to a DIE. */
4590
4591 static inline void
4592 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4593 {
4594 dw_attr_node attr;
4595
4596 attr.dw_attr = attr_kind;
4597 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4598 attr.dw_attr_val.val_entry = NULL;
4599 attr.dw_attr_val.v.val_fde_index = targ_fde;
4600 add_dwarf_attr (die, &attr);
4601 }
4602
4603 /* Add a location description attribute value to a DIE. */
4604
4605 static inline void
4606 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4607 {
4608 dw_attr_node attr;
4609
4610 attr.dw_attr = attr_kind;
4611 attr.dw_attr_val.val_class = dw_val_class_loc;
4612 attr.dw_attr_val.val_entry = NULL;
4613 attr.dw_attr_val.v.val_loc = loc;
4614 add_dwarf_attr (die, &attr);
4615 }
4616
4617 static inline dw_loc_descr_ref
4618 AT_loc (dw_attr_node *a)
4619 {
4620 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4621 return a->dw_attr_val.v.val_loc;
4622 }
4623
4624 static inline void
4625 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4626 {
4627 dw_attr_node attr;
4628
4629 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4630 return;
4631
4632 attr.dw_attr = attr_kind;
4633 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4634 attr.dw_attr_val.val_entry = NULL;
4635 attr.dw_attr_val.v.val_loc_list = loc_list;
4636 add_dwarf_attr (die, &attr);
4637 have_location_lists = true;
4638 }
4639
4640 static inline dw_loc_list_ref
4641 AT_loc_list (dw_attr_node *a)
4642 {
4643 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4644 return a->dw_attr_val.v.val_loc_list;
4645 }
4646
4647 static inline dw_loc_list_ref *
4648 AT_loc_list_ptr (dw_attr_node *a)
4649 {
4650 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4651 return &a->dw_attr_val.v.val_loc_list;
4652 }
4653
4654 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4655 {
4656 static hashval_t hash (addr_table_entry *);
4657 static bool equal (addr_table_entry *, addr_table_entry *);
4658 };
4659
4660 /* Table of entries into the .debug_addr section. */
4661
4662 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4663
4664 /* Hash an address_table_entry. */
4665
4666 hashval_t
4667 addr_hasher::hash (addr_table_entry *a)
4668 {
4669 inchash::hash hstate;
4670 switch (a->kind)
4671 {
4672 case ate_kind_rtx:
4673 hstate.add_int (0);
4674 break;
4675 case ate_kind_rtx_dtprel:
4676 hstate.add_int (1);
4677 break;
4678 case ate_kind_label:
4679 return htab_hash_string (a->addr.label);
4680 default:
4681 gcc_unreachable ();
4682 }
4683 inchash::add_rtx (a->addr.rtl, hstate);
4684 return hstate.end ();
4685 }
4686
4687 /* Determine equality for two address_table_entries. */
4688
4689 bool
4690 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4691 {
4692 if (a1->kind != a2->kind)
4693 return 0;
4694 switch (a1->kind)
4695 {
4696 case ate_kind_rtx:
4697 case ate_kind_rtx_dtprel:
4698 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4699 case ate_kind_label:
4700 return strcmp (a1->addr.label, a2->addr.label) == 0;
4701 default:
4702 gcc_unreachable ();
4703 }
4704 }
4705
4706 /* Initialize an addr_table_entry. */
4707
4708 void
4709 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4710 {
4711 e->kind = kind;
4712 switch (kind)
4713 {
4714 case ate_kind_rtx:
4715 case ate_kind_rtx_dtprel:
4716 e->addr.rtl = (rtx) addr;
4717 break;
4718 case ate_kind_label:
4719 e->addr.label = (char *) addr;
4720 break;
4721 }
4722 e->refcount = 0;
4723 e->index = NO_INDEX_ASSIGNED;
4724 }
4725
4726 /* Add attr to the address table entry to the table. Defer setting an
4727 index until output time. */
4728
4729 static addr_table_entry *
4730 add_addr_table_entry (void *addr, enum ate_kind kind)
4731 {
4732 addr_table_entry *node;
4733 addr_table_entry finder;
4734
4735 gcc_assert (dwarf_split_debug_info);
4736 if (! addr_index_table)
4737 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4738 init_addr_table_entry (&finder, kind, addr);
4739 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4740
4741 if (*slot == HTAB_EMPTY_ENTRY)
4742 {
4743 node = ggc_cleared_alloc<addr_table_entry> ();
4744 init_addr_table_entry (node, kind, addr);
4745 *slot = node;
4746 }
4747 else
4748 node = *slot;
4749
4750 node->refcount++;
4751 return node;
4752 }
4753
4754 /* Remove an entry from the addr table by decrementing its refcount.
4755 Strictly, decrementing the refcount would be enough, but the
4756 assertion that the entry is actually in the table has found
4757 bugs. */
4758
4759 static void
4760 remove_addr_table_entry (addr_table_entry *entry)
4761 {
4762 gcc_assert (dwarf_split_debug_info && addr_index_table);
4763 /* After an index is assigned, the table is frozen. */
4764 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4765 entry->refcount--;
4766 }
4767
4768 /* Given a location list, remove all addresses it refers to from the
4769 address_table. */
4770
4771 static void
4772 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4773 {
4774 for (; descr; descr = descr->dw_loc_next)
4775 if (descr->dw_loc_oprnd1.val_entry != NULL)
4776 {
4777 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4778 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4779 }
4780 }
4781
4782 /* A helper function for dwarf2out_finish called through
4783 htab_traverse. Assign an addr_table_entry its index. All entries
4784 must be collected into the table when this function is called,
4785 because the indexing code relies on htab_traverse to traverse nodes
4786 in the same order for each run. */
4787
4788 int
4789 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4790 {
4791 addr_table_entry *node = *h;
4792
4793 /* Don't index unreferenced nodes. */
4794 if (node->refcount == 0)
4795 return 1;
4796
4797 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4798 node->index = *index;
4799 *index += 1;
4800
4801 return 1;
4802 }
4803
4804 /* Add an address constant attribute value to a DIE. When using
4805 dwarf_split_debug_info, address attributes in dies destined for the
4806 final executable should be direct references--setting the parameter
4807 force_direct ensures this behavior. */
4808
4809 static inline void
4810 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4811 bool force_direct)
4812 {
4813 dw_attr_node attr;
4814
4815 attr.dw_attr = attr_kind;
4816 attr.dw_attr_val.val_class = dw_val_class_addr;
4817 attr.dw_attr_val.v.val_addr = addr;
4818 if (dwarf_split_debug_info && !force_direct)
4819 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4820 else
4821 attr.dw_attr_val.val_entry = NULL;
4822 add_dwarf_attr (die, &attr);
4823 }
4824
4825 /* Get the RTX from to an address DIE attribute. */
4826
4827 static inline rtx
4828 AT_addr (dw_attr_node *a)
4829 {
4830 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4831 return a->dw_attr_val.v.val_addr;
4832 }
4833
4834 /* Add a file attribute value to a DIE. */
4835
4836 static inline void
4837 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4838 struct dwarf_file_data *fd)
4839 {
4840 dw_attr_node attr;
4841
4842 attr.dw_attr = attr_kind;
4843 attr.dw_attr_val.val_class = dw_val_class_file;
4844 attr.dw_attr_val.val_entry = NULL;
4845 attr.dw_attr_val.v.val_file = fd;
4846 add_dwarf_attr (die, &attr);
4847 }
4848
4849 /* Get the dwarf_file_data from a file DIE attribute. */
4850
4851 static inline struct dwarf_file_data *
4852 AT_file (dw_attr_node *a)
4853 {
4854 gcc_assert (a && (AT_class (a) == dw_val_class_file
4855 || AT_class (a) == dw_val_class_file_implicit));
4856 return a->dw_attr_val.v.val_file;
4857 }
4858
4859 /* Add a vms delta attribute value to a DIE. */
4860
4861 static inline void
4862 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4863 const char *lbl1, const char *lbl2)
4864 {
4865 dw_attr_node attr;
4866
4867 attr.dw_attr = attr_kind;
4868 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4869 attr.dw_attr_val.val_entry = NULL;
4870 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4871 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4872 add_dwarf_attr (die, &attr);
4873 }
4874
4875 /* Add a label identifier attribute value to a DIE. */
4876
4877 static inline void
4878 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4879 const char *lbl_id)
4880 {
4881 dw_attr_node attr;
4882
4883 attr.dw_attr = attr_kind;
4884 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4885 attr.dw_attr_val.val_entry = NULL;
4886 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4887 if (dwarf_split_debug_info)
4888 attr.dw_attr_val.val_entry
4889 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4890 ate_kind_label);
4891 add_dwarf_attr (die, &attr);
4892 }
4893
4894 /* Add a section offset attribute value to a DIE, an offset into the
4895 debug_line section. */
4896
4897 static inline void
4898 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4899 const char *label)
4900 {
4901 dw_attr_node attr;
4902
4903 attr.dw_attr = attr_kind;
4904 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4905 attr.dw_attr_val.val_entry = NULL;
4906 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4907 add_dwarf_attr (die, &attr);
4908 }
4909
4910 /* Add a section offset attribute value to a DIE, an offset into the
4911 debug_loclists section. */
4912
4913 static inline void
4914 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4915 const char *label)
4916 {
4917 dw_attr_node attr;
4918
4919 attr.dw_attr = attr_kind;
4920 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4921 attr.dw_attr_val.val_entry = NULL;
4922 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4923 add_dwarf_attr (die, &attr);
4924 }
4925
4926 /* Add a section offset attribute value to a DIE, an offset into the
4927 debug_macinfo section. */
4928
4929 static inline void
4930 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4931 const char *label)
4932 {
4933 dw_attr_node attr;
4934
4935 attr.dw_attr = attr_kind;
4936 attr.dw_attr_val.val_class = dw_val_class_macptr;
4937 attr.dw_attr_val.val_entry = NULL;
4938 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4939 add_dwarf_attr (die, &attr);
4940 }
4941
4942 /* Add an offset attribute value to a DIE. */
4943
4944 static inline void
4945 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4946 unsigned HOST_WIDE_INT offset)
4947 {
4948 dw_attr_node attr;
4949
4950 attr.dw_attr = attr_kind;
4951 attr.dw_attr_val.val_class = dw_val_class_offset;
4952 attr.dw_attr_val.val_entry = NULL;
4953 attr.dw_attr_val.v.val_offset = offset;
4954 add_dwarf_attr (die, &attr);
4955 }
4956
4957 /* Add a range_list attribute value to a DIE. When using
4958 dwarf_split_debug_info, address attributes in dies destined for the
4959 final executable should be direct references--setting the parameter
4960 force_direct ensures this behavior. */
4961
4962 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4963 #define RELOCATED_OFFSET (NULL)
4964
4965 static void
4966 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4967 long unsigned int offset, bool force_direct)
4968 {
4969 dw_attr_node attr;
4970
4971 attr.dw_attr = attr_kind;
4972 attr.dw_attr_val.val_class = dw_val_class_range_list;
4973 /* For the range_list attribute, use val_entry to store whether the
4974 offset should follow split-debug-info or normal semantics. This
4975 value is read in output_range_list_offset. */
4976 if (dwarf_split_debug_info && !force_direct)
4977 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4978 else
4979 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4980 attr.dw_attr_val.v.val_offset = offset;
4981 add_dwarf_attr (die, &attr);
4982 }
4983
4984 /* Return the start label of a delta attribute. */
4985
4986 static inline const char *
4987 AT_vms_delta1 (dw_attr_node *a)
4988 {
4989 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4990 return a->dw_attr_val.v.val_vms_delta.lbl1;
4991 }
4992
4993 /* Return the end label of a delta attribute. */
4994
4995 static inline const char *
4996 AT_vms_delta2 (dw_attr_node *a)
4997 {
4998 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4999 return a->dw_attr_val.v.val_vms_delta.lbl2;
5000 }
5001
5002 static inline const char *
5003 AT_lbl (dw_attr_node *a)
5004 {
5005 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5006 || AT_class (a) == dw_val_class_lineptr
5007 || AT_class (a) == dw_val_class_macptr
5008 || AT_class (a) == dw_val_class_loclistsptr
5009 || AT_class (a) == dw_val_class_high_pc));
5010 return a->dw_attr_val.v.val_lbl_id;
5011 }
5012
5013 /* Get the attribute of type attr_kind. */
5014
5015 static dw_attr_node *
5016 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5017 {
5018 dw_attr_node *a;
5019 unsigned ix;
5020 dw_die_ref spec = NULL;
5021
5022 if (! die)
5023 return NULL;
5024
5025 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5026 if (a->dw_attr == attr_kind)
5027 return a;
5028 else if (a->dw_attr == DW_AT_specification
5029 || a->dw_attr == DW_AT_abstract_origin)
5030 spec = AT_ref (a);
5031
5032 if (spec)
5033 return get_AT (spec, attr_kind);
5034
5035 return NULL;
5036 }
5037
5038 /* Returns the parent of the declaration of DIE. */
5039
5040 static dw_die_ref
5041 get_die_parent (dw_die_ref die)
5042 {
5043 dw_die_ref t;
5044
5045 if (!die)
5046 return NULL;
5047
5048 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5049 || (t = get_AT_ref (die, DW_AT_specification)))
5050 die = t;
5051
5052 return die->die_parent;
5053 }
5054
5055 /* Return the "low pc" attribute value, typically associated with a subprogram
5056 DIE. Return null if the "low pc" attribute is either not present, or if it
5057 cannot be represented as an assembler label identifier. */
5058
5059 static inline const char *
5060 get_AT_low_pc (dw_die_ref die)
5061 {
5062 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5063
5064 return a ? AT_lbl (a) : NULL;
5065 }
5066
5067 /* Return the "high pc" attribute value, typically associated with a subprogram
5068 DIE. Return null if the "high pc" attribute is either not present, or if it
5069 cannot be represented as an assembler label identifier. */
5070
5071 static inline const char *
5072 get_AT_hi_pc (dw_die_ref die)
5073 {
5074 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5075
5076 return a ? AT_lbl (a) : NULL;
5077 }
5078
5079 /* Return the value of the string attribute designated by ATTR_KIND, or
5080 NULL if it is not present. */
5081
5082 static inline const char *
5083 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5084 {
5085 dw_attr_node *a = get_AT (die, attr_kind);
5086
5087 return a ? AT_string (a) : NULL;
5088 }
5089
5090 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5091 if it is not present. */
5092
5093 static inline int
5094 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5095 {
5096 dw_attr_node *a = get_AT (die, attr_kind);
5097
5098 return a ? AT_flag (a) : 0;
5099 }
5100
5101 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5102 if it is not present. */
5103
5104 static inline unsigned
5105 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5106 {
5107 dw_attr_node *a = get_AT (die, attr_kind);
5108
5109 return a ? AT_unsigned (a) : 0;
5110 }
5111
5112 static inline dw_die_ref
5113 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5114 {
5115 dw_attr_node *a = get_AT (die, attr_kind);
5116
5117 return a ? AT_ref (a) : NULL;
5118 }
5119
5120 static inline struct dwarf_file_data *
5121 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5122 {
5123 dw_attr_node *a = get_AT (die, attr_kind);
5124
5125 return a ? AT_file (a) : NULL;
5126 }
5127
5128 /* Return TRUE if the language is C++. */
5129
5130 static inline bool
5131 is_cxx (void)
5132 {
5133 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5134
5135 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5136 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5137 }
5138
5139 /* Return TRUE if DECL was created by the C++ frontend. */
5140
5141 static bool
5142 is_cxx (const_tree decl)
5143 {
5144 if (in_lto_p)
5145 {
5146 const_tree context = get_ultimate_context (decl);
5147 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5148 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5149 }
5150 return is_cxx ();
5151 }
5152
5153 /* Return TRUE if the language is Fortran. */
5154
5155 static inline bool
5156 is_fortran (void)
5157 {
5158 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5159
5160 return (lang == DW_LANG_Fortran77
5161 || lang == DW_LANG_Fortran90
5162 || lang == DW_LANG_Fortran95
5163 || lang == DW_LANG_Fortran03
5164 || lang == DW_LANG_Fortran08);
5165 }
5166
5167 static inline bool
5168 is_fortran (const_tree decl)
5169 {
5170 if (in_lto_p)
5171 {
5172 const_tree context = get_ultimate_context (decl);
5173 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5174 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5175 "GNU Fortran", 11) == 0
5176 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5177 "GNU F77") == 0);
5178 }
5179 return is_fortran ();
5180 }
5181
5182 /* Return TRUE if the language is Ada. */
5183
5184 static inline bool
5185 is_ada (void)
5186 {
5187 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5188
5189 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5190 }
5191
5192 /* Remove the specified attribute if present. Return TRUE if removal
5193 was successful. */
5194
5195 static bool
5196 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5197 {
5198 dw_attr_node *a;
5199 unsigned ix;
5200
5201 if (! die)
5202 return false;
5203
5204 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5205 if (a->dw_attr == attr_kind)
5206 {
5207 if (AT_class (a) == dw_val_class_str)
5208 if (a->dw_attr_val.v.val_str->refcount)
5209 a->dw_attr_val.v.val_str->refcount--;
5210
5211 /* vec::ordered_remove should help reduce the number of abbrevs
5212 that are needed. */
5213 die->die_attr->ordered_remove (ix);
5214 return true;
5215 }
5216 return false;
5217 }
5218
5219 /* Remove CHILD from its parent. PREV must have the property that
5220 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5221
5222 static void
5223 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5224 {
5225 gcc_assert (child->die_parent == prev->die_parent);
5226 gcc_assert (prev->die_sib == child);
5227 if (prev == child)
5228 {
5229 gcc_assert (child->die_parent->die_child == child);
5230 prev = NULL;
5231 }
5232 else
5233 prev->die_sib = child->die_sib;
5234 if (child->die_parent->die_child == child)
5235 child->die_parent->die_child = prev;
5236 child->die_sib = NULL;
5237 }
5238
5239 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5240 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5241
5242 static void
5243 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5244 {
5245 dw_die_ref parent = old_child->die_parent;
5246
5247 gcc_assert (parent == prev->die_parent);
5248 gcc_assert (prev->die_sib == old_child);
5249
5250 new_child->die_parent = parent;
5251 if (prev == old_child)
5252 {
5253 gcc_assert (parent->die_child == old_child);
5254 new_child->die_sib = new_child;
5255 }
5256 else
5257 {
5258 prev->die_sib = new_child;
5259 new_child->die_sib = old_child->die_sib;
5260 }
5261 if (old_child->die_parent->die_child == old_child)
5262 old_child->die_parent->die_child = new_child;
5263 old_child->die_sib = NULL;
5264 }
5265
5266 /* Move all children from OLD_PARENT to NEW_PARENT. */
5267
5268 static void
5269 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5270 {
5271 dw_die_ref c;
5272 new_parent->die_child = old_parent->die_child;
5273 old_parent->die_child = NULL;
5274 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5275 }
5276
5277 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5278 matches TAG. */
5279
5280 static void
5281 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5282 {
5283 dw_die_ref c;
5284
5285 c = die->die_child;
5286 if (c) do {
5287 dw_die_ref prev = c;
5288 c = c->die_sib;
5289 while (c->die_tag == tag)
5290 {
5291 remove_child_with_prev (c, prev);
5292 c->die_parent = NULL;
5293 /* Might have removed every child. */
5294 if (die->die_child == NULL)
5295 return;
5296 c = prev->die_sib;
5297 }
5298 } while (c != die->die_child);
5299 }
5300
5301 /* Add a CHILD_DIE as the last child of DIE. */
5302
5303 static void
5304 add_child_die (dw_die_ref die, dw_die_ref child_die)
5305 {
5306 /* FIXME this should probably be an assert. */
5307 if (! die || ! child_die)
5308 return;
5309 gcc_assert (die != child_die);
5310
5311 child_die->die_parent = die;
5312 if (die->die_child)
5313 {
5314 child_die->die_sib = die->die_child->die_sib;
5315 die->die_child->die_sib = child_die;
5316 }
5317 else
5318 child_die->die_sib = child_die;
5319 die->die_child = child_die;
5320 }
5321
5322 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5323
5324 static void
5325 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5326 dw_die_ref after_die)
5327 {
5328 gcc_assert (die
5329 && child_die
5330 && after_die
5331 && die->die_child
5332 && die != child_die);
5333
5334 child_die->die_parent = die;
5335 child_die->die_sib = after_die->die_sib;
5336 after_die->die_sib = child_die;
5337 if (die->die_child == after_die)
5338 die->die_child = child_die;
5339 }
5340
5341 /* Unassociate CHILD from its parent, and make its parent be
5342 NEW_PARENT. */
5343
5344 static void
5345 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5346 {
5347 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5348 if (p->die_sib == child)
5349 {
5350 remove_child_with_prev (child, p);
5351 break;
5352 }
5353 add_child_die (new_parent, child);
5354 }
5355
5356 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5357 is the specification, to the end of PARENT's list of children.
5358 This is done by removing and re-adding it. */
5359
5360 static void
5361 splice_child_die (dw_die_ref parent, dw_die_ref child)
5362 {
5363 /* We want the declaration DIE from inside the class, not the
5364 specification DIE at toplevel. */
5365 if (child->die_parent != parent)
5366 {
5367 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5368
5369 if (tmp)
5370 child = tmp;
5371 }
5372
5373 gcc_assert (child->die_parent == parent
5374 || (child->die_parent
5375 == get_AT_ref (parent, DW_AT_specification)));
5376
5377 reparent_child (child, parent);
5378 }
5379
5380 /* Create and return a new die with TAG_VALUE as tag. */
5381
5382 static inline dw_die_ref
5383 new_die_raw (enum dwarf_tag tag_value)
5384 {
5385 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5386 die->die_tag = tag_value;
5387 return die;
5388 }
5389
5390 /* Create and return a new die with a parent of PARENT_DIE. If
5391 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5392 associated tree T must be supplied to determine parenthood
5393 later. */
5394
5395 static inline dw_die_ref
5396 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5397 {
5398 dw_die_ref die = new_die_raw (tag_value);
5399
5400 if (parent_die != NULL)
5401 add_child_die (parent_die, die);
5402 else
5403 {
5404 limbo_die_node *limbo_node;
5405
5406 /* No DIEs created after early dwarf should end up in limbo,
5407 because the limbo list should not persist past LTO
5408 streaming. */
5409 if (tag_value != DW_TAG_compile_unit
5410 /* These are allowed because they're generated while
5411 breaking out COMDAT units late. */
5412 && tag_value != DW_TAG_type_unit
5413 && tag_value != DW_TAG_skeleton_unit
5414 && !early_dwarf
5415 /* Allow nested functions to live in limbo because they will
5416 only temporarily live there, as decls_for_scope will fix
5417 them up. */
5418 && (TREE_CODE (t) != FUNCTION_DECL
5419 || !decl_function_context (t))
5420 /* Same as nested functions above but for types. Types that
5421 are local to a function will be fixed in
5422 decls_for_scope. */
5423 && (!RECORD_OR_UNION_TYPE_P (t)
5424 || !TYPE_CONTEXT (t)
5425 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5426 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5427 especially in the ltrans stage, but once we implement LTO
5428 dwarf streaming, we should remove this exception. */
5429 && !in_lto_p)
5430 {
5431 fprintf (stderr, "symbol ended up in limbo too late:");
5432 debug_generic_stmt (t);
5433 gcc_unreachable ();
5434 }
5435
5436 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5437 limbo_node->die = die;
5438 limbo_node->created_for = t;
5439 limbo_node->next = limbo_die_list;
5440 limbo_die_list = limbo_node;
5441 }
5442
5443 return die;
5444 }
5445
5446 /* Return the DIE associated with the given type specifier. */
5447
5448 static inline dw_die_ref
5449 lookup_type_die (tree type)
5450 {
5451 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5452 if (die && die->removed)
5453 {
5454 TYPE_SYMTAB_DIE (type) = NULL;
5455 return NULL;
5456 }
5457 return die;
5458 }
5459
5460 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5461 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5462 anonymous type instead the one of the naming typedef. */
5463
5464 static inline dw_die_ref
5465 strip_naming_typedef (tree type, dw_die_ref type_die)
5466 {
5467 if (type
5468 && TREE_CODE (type) == RECORD_TYPE
5469 && type_die
5470 && type_die->die_tag == DW_TAG_typedef
5471 && is_naming_typedef_decl (TYPE_NAME (type)))
5472 type_die = get_AT_ref (type_die, DW_AT_type);
5473 return type_die;
5474 }
5475
5476 /* Like lookup_type_die, but if type is an anonymous type named by a
5477 typedef[1], return the DIE of the anonymous type instead the one of
5478 the naming typedef. This is because in gen_typedef_die, we did
5479 equate the anonymous struct named by the typedef with the DIE of
5480 the naming typedef. So by default, lookup_type_die on an anonymous
5481 struct yields the DIE of the naming typedef.
5482
5483 [1]: Read the comment of is_naming_typedef_decl to learn about what
5484 a naming typedef is. */
5485
5486 static inline dw_die_ref
5487 lookup_type_die_strip_naming_typedef (tree type)
5488 {
5489 dw_die_ref die = lookup_type_die (type);
5490 return strip_naming_typedef (type, die);
5491 }
5492
5493 /* Equate a DIE to a given type specifier. */
5494
5495 static inline void
5496 equate_type_number_to_die (tree type, dw_die_ref type_die)
5497 {
5498 TYPE_SYMTAB_DIE (type) = type_die;
5499 }
5500
5501 /* Returns a hash value for X (which really is a die_struct). */
5502
5503 inline hashval_t
5504 decl_die_hasher::hash (die_node *x)
5505 {
5506 return (hashval_t) x->decl_id;
5507 }
5508
5509 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5510
5511 inline bool
5512 decl_die_hasher::equal (die_node *x, tree y)
5513 {
5514 return (x->decl_id == DECL_UID (y));
5515 }
5516
5517 /* Return the DIE associated with a given declaration. */
5518
5519 static inline dw_die_ref
5520 lookup_decl_die (tree decl)
5521 {
5522 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5523 NO_INSERT);
5524 if (!die)
5525 return NULL;
5526 if ((*die)->removed)
5527 {
5528 decl_die_table->clear_slot (die);
5529 return NULL;
5530 }
5531 return *die;
5532 }
5533
5534
5535 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5536 style reference. Return true if we found one refering to a DIE for
5537 DECL, otherwise return false. */
5538
5539 static bool
5540 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5541 unsigned HOST_WIDE_INT *off)
5542 {
5543 dw_die_ref die;
5544
5545 if (flag_wpa && !decl_die_table)
5546 return false;
5547
5548 if (TREE_CODE (decl) == BLOCK)
5549 die = BLOCK_DIE (decl);
5550 else
5551 die = lookup_decl_die (decl);
5552 if (!die)
5553 return false;
5554
5555 /* During WPA stage we currently use DIEs to store the
5556 decl <-> label + offset map. That's quite inefficient but it
5557 works for now. */
5558 if (flag_wpa)
5559 {
5560 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5561 if (!ref)
5562 {
5563 gcc_assert (die == comp_unit_die ());
5564 return false;
5565 }
5566 *off = ref->die_offset;
5567 *sym = ref->die_id.die_symbol;
5568 return true;
5569 }
5570
5571 /* Similar to get_ref_die_offset_label, but using the "correct"
5572 label. */
5573 *off = die->die_offset;
5574 while (die->die_parent)
5575 die = die->die_parent;
5576 /* For the containing CU DIE we compute a die_symbol in
5577 compute_comp_unit_symbol. */
5578 gcc_assert (die->die_tag == DW_TAG_compile_unit
5579 && die->die_id.die_symbol != NULL);
5580 *sym = die->die_id.die_symbol;
5581 return true;
5582 }
5583
5584 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5585
5586 static void
5587 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5588 const char *symbol, HOST_WIDE_INT offset)
5589 {
5590 /* Create a fake DIE that contains the reference. Don't use
5591 new_die because we don't want to end up in the limbo list. */
5592 dw_die_ref ref = new_die_raw (die->die_tag);
5593 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5594 ref->die_offset = offset;
5595 ref->with_offset = 1;
5596 add_AT_die_ref (die, attr_kind, ref);
5597 }
5598
5599 /* Create a DIE for DECL if required and add a reference to a DIE
5600 at SYMBOL + OFFSET which contains attributes dumped early. */
5601
5602 static void
5603 dwarf2out_register_external_die (tree decl, const char *sym,
5604 unsigned HOST_WIDE_INT off)
5605 {
5606 if (debug_info_level == DINFO_LEVEL_NONE)
5607 return;
5608
5609 if (flag_wpa && !decl_die_table)
5610 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5611
5612 dw_die_ref die
5613 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5614 gcc_assert (!die);
5615
5616 tree ctx;
5617 dw_die_ref parent = NULL;
5618 /* Need to lookup a DIE for the decls context - the containing
5619 function or translation unit. */
5620 if (TREE_CODE (decl) == BLOCK)
5621 {
5622 ctx = BLOCK_SUPERCONTEXT (decl);
5623 /* ??? We do not output DIEs for all scopes thus skip as
5624 many DIEs as needed. */
5625 while (TREE_CODE (ctx) == BLOCK
5626 && !BLOCK_DIE (ctx))
5627 ctx = BLOCK_SUPERCONTEXT (ctx);
5628 }
5629 else
5630 ctx = DECL_CONTEXT (decl);
5631 while (ctx && TYPE_P (ctx))
5632 ctx = TYPE_CONTEXT (ctx);
5633 if (ctx)
5634 {
5635 if (TREE_CODE (ctx) == BLOCK)
5636 parent = BLOCK_DIE (ctx);
5637 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5638 /* Keep the 1:1 association during WPA. */
5639 && !flag_wpa)
5640 /* Otherwise all late annotations go to the main CU which
5641 imports the original CUs. */
5642 parent = comp_unit_die ();
5643 else if (TREE_CODE (ctx) == FUNCTION_DECL
5644 && TREE_CODE (decl) != PARM_DECL
5645 && TREE_CODE (decl) != BLOCK)
5646 /* Leave function local entities parent determination to when
5647 we process scope vars. */
5648 ;
5649 else
5650 parent = lookup_decl_die (ctx);
5651 }
5652 else
5653 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5654 Handle this case gracefully by globalizing stuff. */
5655 parent = comp_unit_die ();
5656 /* Create a DIE "stub". */
5657 switch (TREE_CODE (decl))
5658 {
5659 case TRANSLATION_UNIT_DECL:
5660 if (! flag_wpa)
5661 {
5662 die = comp_unit_die ();
5663 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5664 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5665 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5666 to create a DIE for the original CUs. */
5667 return;
5668 }
5669 /* Keep the 1:1 association during WPA. */
5670 die = new_die (DW_TAG_compile_unit, NULL, decl);
5671 break;
5672 case NAMESPACE_DECL:
5673 if (is_fortran (decl))
5674 die = new_die (DW_TAG_module, parent, decl);
5675 else
5676 die = new_die (DW_TAG_namespace, parent, decl);
5677 break;
5678 case FUNCTION_DECL:
5679 die = new_die (DW_TAG_subprogram, parent, decl);
5680 break;
5681 case VAR_DECL:
5682 die = new_die (DW_TAG_variable, parent, decl);
5683 break;
5684 case RESULT_DECL:
5685 die = new_die (DW_TAG_variable, parent, decl);
5686 break;
5687 case PARM_DECL:
5688 die = new_die (DW_TAG_formal_parameter, parent, decl);
5689 break;
5690 case CONST_DECL:
5691 die = new_die (DW_TAG_constant, parent, decl);
5692 break;
5693 case LABEL_DECL:
5694 die = new_die (DW_TAG_label, parent, decl);
5695 break;
5696 case BLOCK:
5697 die = new_die (DW_TAG_lexical_block, parent, decl);
5698 break;
5699 default:
5700 gcc_unreachable ();
5701 }
5702 if (TREE_CODE (decl) == BLOCK)
5703 BLOCK_DIE (decl) = die;
5704 else
5705 equate_decl_number_to_die (decl, die);
5706
5707 /* Add a reference to the DIE providing early debug at $sym + off. */
5708 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5709 }
5710
5711 /* Returns a hash value for X (which really is a var_loc_list). */
5712
5713 inline hashval_t
5714 decl_loc_hasher::hash (var_loc_list *x)
5715 {
5716 return (hashval_t) x->decl_id;
5717 }
5718
5719 /* Return nonzero if decl_id of var_loc_list X is the same as
5720 UID of decl *Y. */
5721
5722 inline bool
5723 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5724 {
5725 return (x->decl_id == DECL_UID (y));
5726 }
5727
5728 /* Return the var_loc list associated with a given declaration. */
5729
5730 static inline var_loc_list *
5731 lookup_decl_loc (const_tree decl)
5732 {
5733 if (!decl_loc_table)
5734 return NULL;
5735 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5736 }
5737
5738 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5739
5740 inline hashval_t
5741 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5742 {
5743 return (hashval_t) x->decl_id;
5744 }
5745
5746 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5747 UID of decl *Y. */
5748
5749 inline bool
5750 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5751 {
5752 return (x->decl_id == DECL_UID (y));
5753 }
5754
5755 /* Equate a DIE to a particular declaration. */
5756
5757 static void
5758 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5759 {
5760 unsigned int decl_id = DECL_UID (decl);
5761
5762 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5763 decl_die->decl_id = decl_id;
5764 }
5765
5766 /* Return how many bits covers PIECE EXPR_LIST. */
5767
5768 static HOST_WIDE_INT
5769 decl_piece_bitsize (rtx piece)
5770 {
5771 int ret = (int) GET_MODE (piece);
5772 if (ret)
5773 return ret;
5774 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5775 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5776 return INTVAL (XEXP (XEXP (piece, 0), 0));
5777 }
5778
5779 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5780
5781 static rtx *
5782 decl_piece_varloc_ptr (rtx piece)
5783 {
5784 if ((int) GET_MODE (piece))
5785 return &XEXP (piece, 0);
5786 else
5787 return &XEXP (XEXP (piece, 0), 1);
5788 }
5789
5790 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5791 Next is the chain of following piece nodes. */
5792
5793 static rtx_expr_list *
5794 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5795 {
5796 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5797 return alloc_EXPR_LIST (bitsize, loc_note, next);
5798 else
5799 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5800 GEN_INT (bitsize),
5801 loc_note), next);
5802 }
5803
5804 /* Return rtx that should be stored into loc field for
5805 LOC_NOTE and BITPOS/BITSIZE. */
5806
5807 static rtx
5808 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5809 HOST_WIDE_INT bitsize)
5810 {
5811 if (bitsize != -1)
5812 {
5813 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5814 if (bitpos != 0)
5815 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5816 }
5817 return loc_note;
5818 }
5819
5820 /* This function either modifies location piece list *DEST in
5821 place (if SRC and INNER is NULL), or copies location piece list
5822 *SRC to *DEST while modifying it. Location BITPOS is modified
5823 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5824 not copied and if needed some padding around it is added.
5825 When modifying in place, DEST should point to EXPR_LIST where
5826 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5827 to the start of the whole list and INNER points to the EXPR_LIST
5828 where earlier pieces cover PIECE_BITPOS bits. */
5829
5830 static void
5831 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5832 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5833 HOST_WIDE_INT bitsize, rtx loc_note)
5834 {
5835 HOST_WIDE_INT diff;
5836 bool copy = inner != NULL;
5837
5838 if (copy)
5839 {
5840 /* First copy all nodes preceding the current bitpos. */
5841 while (src != inner)
5842 {
5843 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5844 decl_piece_bitsize (*src), NULL_RTX);
5845 dest = &XEXP (*dest, 1);
5846 src = &XEXP (*src, 1);
5847 }
5848 }
5849 /* Add padding if needed. */
5850 if (bitpos != piece_bitpos)
5851 {
5852 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5853 copy ? NULL_RTX : *dest);
5854 dest = &XEXP (*dest, 1);
5855 }
5856 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5857 {
5858 gcc_assert (!copy);
5859 /* A piece with correct bitpos and bitsize already exist,
5860 just update the location for it and return. */
5861 *decl_piece_varloc_ptr (*dest) = loc_note;
5862 return;
5863 }
5864 /* Add the piece that changed. */
5865 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5866 dest = &XEXP (*dest, 1);
5867 /* Skip over pieces that overlap it. */
5868 diff = bitpos - piece_bitpos + bitsize;
5869 if (!copy)
5870 src = dest;
5871 while (diff > 0 && *src)
5872 {
5873 rtx piece = *src;
5874 diff -= decl_piece_bitsize (piece);
5875 if (copy)
5876 src = &XEXP (piece, 1);
5877 else
5878 {
5879 *src = XEXP (piece, 1);
5880 free_EXPR_LIST_node (piece);
5881 }
5882 }
5883 /* Add padding if needed. */
5884 if (diff < 0 && *src)
5885 {
5886 if (!copy)
5887 dest = src;
5888 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5889 dest = &XEXP (*dest, 1);
5890 }
5891 if (!copy)
5892 return;
5893 /* Finally copy all nodes following it. */
5894 while (*src)
5895 {
5896 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5897 decl_piece_bitsize (*src), NULL_RTX);
5898 dest = &XEXP (*dest, 1);
5899 src = &XEXP (*src, 1);
5900 }
5901 }
5902
5903 /* Add a variable location node to the linked list for DECL. */
5904
5905 static struct var_loc_node *
5906 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5907 {
5908 unsigned int decl_id;
5909 var_loc_list *temp;
5910 struct var_loc_node *loc = NULL;
5911 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5912
5913 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5914 {
5915 tree realdecl = DECL_DEBUG_EXPR (decl);
5916 if (handled_component_p (realdecl)
5917 || (TREE_CODE (realdecl) == MEM_REF
5918 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5919 {
5920 HOST_WIDE_INT maxsize;
5921 bool reverse;
5922 tree innerdecl
5923 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5924 &reverse);
5925 if (!DECL_P (innerdecl)
5926 || DECL_IGNORED_P (innerdecl)
5927 || TREE_STATIC (innerdecl)
5928 || bitsize <= 0
5929 || bitpos + bitsize > 256
5930 || bitsize != maxsize)
5931 return NULL;
5932 decl = innerdecl;
5933 }
5934 }
5935
5936 decl_id = DECL_UID (decl);
5937 var_loc_list **slot
5938 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5939 if (*slot == NULL)
5940 {
5941 temp = ggc_cleared_alloc<var_loc_list> ();
5942 temp->decl_id = decl_id;
5943 *slot = temp;
5944 }
5945 else
5946 temp = *slot;
5947
5948 /* For PARM_DECLs try to keep around the original incoming value,
5949 even if that means we'll emit a zero-range .debug_loc entry. */
5950 if (temp->last
5951 && temp->first == temp->last
5952 && TREE_CODE (decl) == PARM_DECL
5953 && NOTE_P (temp->first->loc)
5954 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5955 && DECL_INCOMING_RTL (decl)
5956 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5957 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5958 == GET_CODE (DECL_INCOMING_RTL (decl))
5959 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5960 && (bitsize != -1
5961 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5962 NOTE_VAR_LOCATION_LOC (loc_note))
5963 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5964 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5965 {
5966 loc = ggc_cleared_alloc<var_loc_node> ();
5967 temp->first->next = loc;
5968 temp->last = loc;
5969 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5970 }
5971 else if (temp->last)
5972 {
5973 struct var_loc_node *last = temp->last, *unused = NULL;
5974 rtx *piece_loc = NULL, last_loc_note;
5975 HOST_WIDE_INT piece_bitpos = 0;
5976 if (last->next)
5977 {
5978 last = last->next;
5979 gcc_assert (last->next == NULL);
5980 }
5981 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5982 {
5983 piece_loc = &last->loc;
5984 do
5985 {
5986 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5987 if (piece_bitpos + cur_bitsize > bitpos)
5988 break;
5989 piece_bitpos += cur_bitsize;
5990 piece_loc = &XEXP (*piece_loc, 1);
5991 }
5992 while (*piece_loc);
5993 }
5994 /* TEMP->LAST here is either pointer to the last but one or
5995 last element in the chained list, LAST is pointer to the
5996 last element. */
5997 if (label && strcmp (last->label, label) == 0)
5998 {
5999 /* For SRA optimized variables if there weren't any real
6000 insns since last note, just modify the last node. */
6001 if (piece_loc != NULL)
6002 {
6003 adjust_piece_list (piece_loc, NULL, NULL,
6004 bitpos, piece_bitpos, bitsize, loc_note);
6005 return NULL;
6006 }
6007 /* If the last note doesn't cover any instructions, remove it. */
6008 if (temp->last != last)
6009 {
6010 temp->last->next = NULL;
6011 unused = last;
6012 last = temp->last;
6013 gcc_assert (strcmp (last->label, label) != 0);
6014 }
6015 else
6016 {
6017 gcc_assert (temp->first == temp->last
6018 || (temp->first->next == temp->last
6019 && TREE_CODE (decl) == PARM_DECL));
6020 memset (temp->last, '\0', sizeof (*temp->last));
6021 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6022 return temp->last;
6023 }
6024 }
6025 if (bitsize == -1 && NOTE_P (last->loc))
6026 last_loc_note = last->loc;
6027 else if (piece_loc != NULL
6028 && *piece_loc != NULL_RTX
6029 && piece_bitpos == bitpos
6030 && decl_piece_bitsize (*piece_loc) == bitsize)
6031 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6032 else
6033 last_loc_note = NULL_RTX;
6034 /* If the current location is the same as the end of the list,
6035 and either both or neither of the locations is uninitialized,
6036 we have nothing to do. */
6037 if (last_loc_note == NULL_RTX
6038 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6039 NOTE_VAR_LOCATION_LOC (loc_note)))
6040 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6041 != NOTE_VAR_LOCATION_STATUS (loc_note))
6042 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6043 == VAR_INIT_STATUS_UNINITIALIZED)
6044 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6045 == VAR_INIT_STATUS_UNINITIALIZED))))
6046 {
6047 /* Add LOC to the end of list and update LAST. If the last
6048 element of the list has been removed above, reuse its
6049 memory for the new node, otherwise allocate a new one. */
6050 if (unused)
6051 {
6052 loc = unused;
6053 memset (loc, '\0', sizeof (*loc));
6054 }
6055 else
6056 loc = ggc_cleared_alloc<var_loc_node> ();
6057 if (bitsize == -1 || piece_loc == NULL)
6058 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6059 else
6060 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6061 bitpos, piece_bitpos, bitsize, loc_note);
6062 last->next = loc;
6063 /* Ensure TEMP->LAST will point either to the new last but one
6064 element of the chain, or to the last element in it. */
6065 if (last != temp->last)
6066 temp->last = last;
6067 }
6068 else if (unused)
6069 ggc_free (unused);
6070 }
6071 else
6072 {
6073 loc = ggc_cleared_alloc<var_loc_node> ();
6074 temp->first = loc;
6075 temp->last = loc;
6076 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6077 }
6078 return loc;
6079 }
6080 \f
6081 /* Keep track of the number of spaces used to indent the
6082 output of the debugging routines that print the structure of
6083 the DIE internal representation. */
6084 static int print_indent;
6085
6086 /* Indent the line the number of spaces given by print_indent. */
6087
6088 static inline void
6089 print_spaces (FILE *outfile)
6090 {
6091 fprintf (outfile, "%*s", print_indent, "");
6092 }
6093
6094 /* Print a type signature in hex. */
6095
6096 static inline void
6097 print_signature (FILE *outfile, char *sig)
6098 {
6099 int i;
6100
6101 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6102 fprintf (outfile, "%02x", sig[i] & 0xff);
6103 }
6104
6105 static inline void
6106 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6107 {
6108 if (discr_value->pos)
6109 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6110 else
6111 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6112 }
6113
6114 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6115
6116 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6117 RECURSE, output location descriptor operations. */
6118
6119 static void
6120 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6121 {
6122 switch (val->val_class)
6123 {
6124 case dw_val_class_addr:
6125 fprintf (outfile, "address");
6126 break;
6127 case dw_val_class_offset:
6128 fprintf (outfile, "offset");
6129 break;
6130 case dw_val_class_loc:
6131 fprintf (outfile, "location descriptor");
6132 if (val->v.val_loc == NULL)
6133 fprintf (outfile, " -> <null>\n");
6134 else if (recurse)
6135 {
6136 fprintf (outfile, ":\n");
6137 print_indent += 4;
6138 print_loc_descr (val->v.val_loc, outfile);
6139 print_indent -= 4;
6140 }
6141 else
6142 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6143 break;
6144 case dw_val_class_loc_list:
6145 fprintf (outfile, "location list -> label:%s",
6146 val->v.val_loc_list->ll_symbol);
6147 break;
6148 case dw_val_class_range_list:
6149 fprintf (outfile, "range list");
6150 break;
6151 case dw_val_class_const:
6152 case dw_val_class_const_implicit:
6153 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6154 break;
6155 case dw_val_class_unsigned_const:
6156 case dw_val_class_unsigned_const_implicit:
6157 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6158 break;
6159 case dw_val_class_const_double:
6160 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6161 HOST_WIDE_INT_PRINT_UNSIGNED")",
6162 val->v.val_double.high,
6163 val->v.val_double.low);
6164 break;
6165 case dw_val_class_wide_int:
6166 {
6167 int i = val->v.val_wide->get_len ();
6168 fprintf (outfile, "constant (");
6169 gcc_assert (i > 0);
6170 if (val->v.val_wide->elt (i - 1) == 0)
6171 fprintf (outfile, "0x");
6172 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6173 val->v.val_wide->elt (--i));
6174 while (--i >= 0)
6175 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6176 val->v.val_wide->elt (i));
6177 fprintf (outfile, ")");
6178 break;
6179 }
6180 case dw_val_class_vec:
6181 fprintf (outfile, "floating-point or vector constant");
6182 break;
6183 case dw_val_class_flag:
6184 fprintf (outfile, "%u", val->v.val_flag);
6185 break;
6186 case dw_val_class_die_ref:
6187 if (val->v.val_die_ref.die != NULL)
6188 {
6189 dw_die_ref die = val->v.val_die_ref.die;
6190
6191 if (die->comdat_type_p)
6192 {
6193 fprintf (outfile, "die -> signature: ");
6194 print_signature (outfile,
6195 die->die_id.die_type_node->signature);
6196 }
6197 else if (die->die_id.die_symbol)
6198 {
6199 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6200 if (die->with_offset)
6201 fprintf (outfile, " + %ld", die->die_offset);
6202 }
6203 else
6204 fprintf (outfile, "die -> %ld", die->die_offset);
6205 fprintf (outfile, " (%p)", (void *) die);
6206 }
6207 else
6208 fprintf (outfile, "die -> <null>");
6209 break;
6210 case dw_val_class_vms_delta:
6211 fprintf (outfile, "delta: @slotcount(%s-%s)",
6212 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6213 break;
6214 case dw_val_class_lbl_id:
6215 case dw_val_class_lineptr:
6216 case dw_val_class_macptr:
6217 case dw_val_class_loclistsptr:
6218 case dw_val_class_high_pc:
6219 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6220 break;
6221 case dw_val_class_str:
6222 if (val->v.val_str->str != NULL)
6223 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6224 else
6225 fprintf (outfile, "<null>");
6226 break;
6227 case dw_val_class_file:
6228 case dw_val_class_file_implicit:
6229 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6230 val->v.val_file->emitted_number);
6231 break;
6232 case dw_val_class_data8:
6233 {
6234 int i;
6235
6236 for (i = 0; i < 8; i++)
6237 fprintf (outfile, "%02x", val->v.val_data8[i]);
6238 break;
6239 }
6240 case dw_val_class_discr_value:
6241 print_discr_value (outfile, &val->v.val_discr_value);
6242 break;
6243 case dw_val_class_discr_list:
6244 for (dw_discr_list_ref node = val->v.val_discr_list;
6245 node != NULL;
6246 node = node->dw_discr_next)
6247 {
6248 if (node->dw_discr_range)
6249 {
6250 fprintf (outfile, " .. ");
6251 print_discr_value (outfile, &node->dw_discr_lower_bound);
6252 print_discr_value (outfile, &node->dw_discr_upper_bound);
6253 }
6254 else
6255 print_discr_value (outfile, &node->dw_discr_lower_bound);
6256
6257 if (node->dw_discr_next != NULL)
6258 fprintf (outfile, " | ");
6259 }
6260 default:
6261 break;
6262 }
6263 }
6264
6265 /* Likewise, for a DIE attribute. */
6266
6267 static void
6268 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6269 {
6270 print_dw_val (&a->dw_attr_val, recurse, outfile);
6271 }
6272
6273
6274 /* Print the list of operands in the LOC location description to OUTFILE. This
6275 routine is a debugging aid only. */
6276
6277 static void
6278 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6279 {
6280 dw_loc_descr_ref l = loc;
6281
6282 if (loc == NULL)
6283 {
6284 print_spaces (outfile);
6285 fprintf (outfile, "<null>\n");
6286 return;
6287 }
6288
6289 for (l = loc; l != NULL; l = l->dw_loc_next)
6290 {
6291 print_spaces (outfile);
6292 fprintf (outfile, "(%p) %s",
6293 (void *) l,
6294 dwarf_stack_op_name (l->dw_loc_opc));
6295 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6296 {
6297 fprintf (outfile, " ");
6298 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6299 }
6300 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6301 {
6302 fprintf (outfile, ", ");
6303 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6304 }
6305 fprintf (outfile, "\n");
6306 }
6307 }
6308
6309 /* Print the information associated with a given DIE, and its children.
6310 This routine is a debugging aid only. */
6311
6312 static void
6313 print_die (dw_die_ref die, FILE *outfile)
6314 {
6315 dw_attr_node *a;
6316 dw_die_ref c;
6317 unsigned ix;
6318
6319 print_spaces (outfile);
6320 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6321 die->die_offset, dwarf_tag_name (die->die_tag),
6322 (void*) die);
6323 print_spaces (outfile);
6324 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6325 fprintf (outfile, " offset: %ld", die->die_offset);
6326 fprintf (outfile, " mark: %d\n", die->die_mark);
6327
6328 if (die->comdat_type_p)
6329 {
6330 print_spaces (outfile);
6331 fprintf (outfile, " signature: ");
6332 print_signature (outfile, die->die_id.die_type_node->signature);
6333 fprintf (outfile, "\n");
6334 }
6335
6336 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6337 {
6338 print_spaces (outfile);
6339 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6340
6341 print_attribute (a, true, outfile);
6342 fprintf (outfile, "\n");
6343 }
6344
6345 if (die->die_child != NULL)
6346 {
6347 print_indent += 4;
6348 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6349 print_indent -= 4;
6350 }
6351 if (print_indent == 0)
6352 fprintf (outfile, "\n");
6353 }
6354
6355 /* Print the list of operations in the LOC location description. */
6356
6357 DEBUG_FUNCTION void
6358 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6359 {
6360 print_loc_descr (loc, stderr);
6361 }
6362
6363 /* Print the information collected for a given DIE. */
6364
6365 DEBUG_FUNCTION void
6366 debug_dwarf_die (dw_die_ref die)
6367 {
6368 print_die (die, stderr);
6369 }
6370
6371 DEBUG_FUNCTION void
6372 debug (die_struct &ref)
6373 {
6374 print_die (&ref, stderr);
6375 }
6376
6377 DEBUG_FUNCTION void
6378 debug (die_struct *ptr)
6379 {
6380 if (ptr)
6381 debug (*ptr);
6382 else
6383 fprintf (stderr, "<nil>\n");
6384 }
6385
6386
6387 /* Print all DWARF information collected for the compilation unit.
6388 This routine is a debugging aid only. */
6389
6390 DEBUG_FUNCTION void
6391 debug_dwarf (void)
6392 {
6393 print_indent = 0;
6394 print_die (comp_unit_die (), stderr);
6395 }
6396
6397 /* Verify the DIE tree structure. */
6398
6399 DEBUG_FUNCTION void
6400 verify_die (dw_die_ref die)
6401 {
6402 gcc_assert (!die->die_mark);
6403 if (die->die_parent == NULL
6404 && die->die_sib == NULL)
6405 return;
6406 /* Verify the die_sib list is cyclic. */
6407 dw_die_ref x = die;
6408 do
6409 {
6410 x->die_mark = 1;
6411 x = x->die_sib;
6412 }
6413 while (x && !x->die_mark);
6414 gcc_assert (x == die);
6415 x = die;
6416 do
6417 {
6418 /* Verify all dies have the same parent. */
6419 gcc_assert (x->die_parent == die->die_parent);
6420 if (x->die_child)
6421 {
6422 /* Verify the child has the proper parent and recurse. */
6423 gcc_assert (x->die_child->die_parent == x);
6424 verify_die (x->die_child);
6425 }
6426 x->die_mark = 0;
6427 x = x->die_sib;
6428 }
6429 while (x && x->die_mark);
6430 }
6431
6432 /* Sanity checks on DIEs. */
6433
6434 static void
6435 check_die (dw_die_ref die)
6436 {
6437 unsigned ix;
6438 dw_attr_node *a;
6439 bool inline_found = false;
6440 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6441 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6442 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6443 {
6444 switch (a->dw_attr)
6445 {
6446 case DW_AT_inline:
6447 if (a->dw_attr_val.v.val_unsigned)
6448 inline_found = true;
6449 break;
6450 case DW_AT_location:
6451 ++n_location;
6452 break;
6453 case DW_AT_low_pc:
6454 ++n_low_pc;
6455 break;
6456 case DW_AT_high_pc:
6457 ++n_high_pc;
6458 break;
6459 case DW_AT_artificial:
6460 ++n_artificial;
6461 break;
6462 case DW_AT_decl_column:
6463 ++n_decl_column;
6464 break;
6465 case DW_AT_decl_line:
6466 ++n_decl_line;
6467 break;
6468 case DW_AT_decl_file:
6469 ++n_decl_file;
6470 break;
6471 default:
6472 break;
6473 }
6474 }
6475 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6476 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6477 {
6478 fprintf (stderr, "Duplicate attributes in DIE:\n");
6479 debug_dwarf_die (die);
6480 gcc_unreachable ();
6481 }
6482 if (inline_found)
6483 {
6484 /* A debugging information entry that is a member of an abstract
6485 instance tree [that has DW_AT_inline] should not contain any
6486 attributes which describe aspects of the subroutine which vary
6487 between distinct inlined expansions or distinct out-of-line
6488 expansions. */
6489 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6490 gcc_assert (a->dw_attr != DW_AT_low_pc
6491 && a->dw_attr != DW_AT_high_pc
6492 && a->dw_attr != DW_AT_location
6493 && a->dw_attr != DW_AT_frame_base
6494 && a->dw_attr != DW_AT_call_all_calls
6495 && a->dw_attr != DW_AT_GNU_all_call_sites);
6496 }
6497 }
6498 \f
6499 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6500 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6501 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6502
6503 /* Calculate the checksum of a location expression. */
6504
6505 static inline void
6506 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6507 {
6508 int tem;
6509 inchash::hash hstate;
6510 hashval_t hash;
6511
6512 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6513 CHECKSUM (tem);
6514 hash_loc_operands (loc, hstate);
6515 hash = hstate.end();
6516 CHECKSUM (hash);
6517 }
6518
6519 /* Calculate the checksum of an attribute. */
6520
6521 static void
6522 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6523 {
6524 dw_loc_descr_ref loc;
6525 rtx r;
6526
6527 CHECKSUM (at->dw_attr);
6528
6529 /* We don't care that this was compiled with a different compiler
6530 snapshot; if the output is the same, that's what matters. */
6531 if (at->dw_attr == DW_AT_producer)
6532 return;
6533
6534 switch (AT_class (at))
6535 {
6536 case dw_val_class_const:
6537 case dw_val_class_const_implicit:
6538 CHECKSUM (at->dw_attr_val.v.val_int);
6539 break;
6540 case dw_val_class_unsigned_const:
6541 case dw_val_class_unsigned_const_implicit:
6542 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6543 break;
6544 case dw_val_class_const_double:
6545 CHECKSUM (at->dw_attr_val.v.val_double);
6546 break;
6547 case dw_val_class_wide_int:
6548 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6549 get_full_len (*at->dw_attr_val.v.val_wide)
6550 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6551 break;
6552 case dw_val_class_vec:
6553 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6554 (at->dw_attr_val.v.val_vec.length
6555 * at->dw_attr_val.v.val_vec.elt_size));
6556 break;
6557 case dw_val_class_flag:
6558 CHECKSUM (at->dw_attr_val.v.val_flag);
6559 break;
6560 case dw_val_class_str:
6561 CHECKSUM_STRING (AT_string (at));
6562 break;
6563
6564 case dw_val_class_addr:
6565 r = AT_addr (at);
6566 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6567 CHECKSUM_STRING (XSTR (r, 0));
6568 break;
6569
6570 case dw_val_class_offset:
6571 CHECKSUM (at->dw_attr_val.v.val_offset);
6572 break;
6573
6574 case dw_val_class_loc:
6575 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6576 loc_checksum (loc, ctx);
6577 break;
6578
6579 case dw_val_class_die_ref:
6580 die_checksum (AT_ref (at), ctx, mark);
6581 break;
6582
6583 case dw_val_class_fde_ref:
6584 case dw_val_class_vms_delta:
6585 case dw_val_class_lbl_id:
6586 case dw_val_class_lineptr:
6587 case dw_val_class_macptr:
6588 case dw_val_class_loclistsptr:
6589 case dw_val_class_high_pc:
6590 break;
6591
6592 case dw_val_class_file:
6593 case dw_val_class_file_implicit:
6594 CHECKSUM_STRING (AT_file (at)->filename);
6595 break;
6596
6597 case dw_val_class_data8:
6598 CHECKSUM (at->dw_attr_val.v.val_data8);
6599 break;
6600
6601 default:
6602 break;
6603 }
6604 }
6605
6606 /* Calculate the checksum of a DIE. */
6607
6608 static void
6609 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6610 {
6611 dw_die_ref c;
6612 dw_attr_node *a;
6613 unsigned ix;
6614
6615 /* To avoid infinite recursion. */
6616 if (die->die_mark)
6617 {
6618 CHECKSUM (die->die_mark);
6619 return;
6620 }
6621 die->die_mark = ++(*mark);
6622
6623 CHECKSUM (die->die_tag);
6624
6625 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6626 attr_checksum (a, ctx, mark);
6627
6628 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6629 }
6630
6631 #undef CHECKSUM
6632 #undef CHECKSUM_BLOCK
6633 #undef CHECKSUM_STRING
6634
6635 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6636 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6637 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6638 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6639 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6640 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6641 #define CHECKSUM_ATTR(FOO) \
6642 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6643
6644 /* Calculate the checksum of a number in signed LEB128 format. */
6645
6646 static void
6647 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6648 {
6649 unsigned char byte;
6650 bool more;
6651
6652 while (1)
6653 {
6654 byte = (value & 0x7f);
6655 value >>= 7;
6656 more = !((value == 0 && (byte & 0x40) == 0)
6657 || (value == -1 && (byte & 0x40) != 0));
6658 if (more)
6659 byte |= 0x80;
6660 CHECKSUM (byte);
6661 if (!more)
6662 break;
6663 }
6664 }
6665
6666 /* Calculate the checksum of a number in unsigned LEB128 format. */
6667
6668 static void
6669 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6670 {
6671 while (1)
6672 {
6673 unsigned char byte = (value & 0x7f);
6674 value >>= 7;
6675 if (value != 0)
6676 /* More bytes to follow. */
6677 byte |= 0x80;
6678 CHECKSUM (byte);
6679 if (value == 0)
6680 break;
6681 }
6682 }
6683
6684 /* Checksum the context of the DIE. This adds the names of any
6685 surrounding namespaces or structures to the checksum. */
6686
6687 static void
6688 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6689 {
6690 const char *name;
6691 dw_die_ref spec;
6692 int tag = die->die_tag;
6693
6694 if (tag != DW_TAG_namespace
6695 && tag != DW_TAG_structure_type
6696 && tag != DW_TAG_class_type)
6697 return;
6698
6699 name = get_AT_string (die, DW_AT_name);
6700
6701 spec = get_AT_ref (die, DW_AT_specification);
6702 if (spec != NULL)
6703 die = spec;
6704
6705 if (die->die_parent != NULL)
6706 checksum_die_context (die->die_parent, ctx);
6707
6708 CHECKSUM_ULEB128 ('C');
6709 CHECKSUM_ULEB128 (tag);
6710 if (name != NULL)
6711 CHECKSUM_STRING (name);
6712 }
6713
6714 /* Calculate the checksum of a location expression. */
6715
6716 static inline void
6717 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6718 {
6719 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6720 were emitted as a DW_FORM_sdata instead of a location expression. */
6721 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6722 {
6723 CHECKSUM_ULEB128 (DW_FORM_sdata);
6724 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6725 return;
6726 }
6727
6728 /* Otherwise, just checksum the raw location expression. */
6729 while (loc != NULL)
6730 {
6731 inchash::hash hstate;
6732 hashval_t hash;
6733
6734 CHECKSUM_ULEB128 (loc->dtprel);
6735 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6736 hash_loc_operands (loc, hstate);
6737 hash = hstate.end ();
6738 CHECKSUM (hash);
6739 loc = loc->dw_loc_next;
6740 }
6741 }
6742
6743 /* Calculate the checksum of an attribute. */
6744
6745 static void
6746 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6747 struct md5_ctx *ctx, int *mark)
6748 {
6749 dw_loc_descr_ref loc;
6750 rtx r;
6751
6752 if (AT_class (at) == dw_val_class_die_ref)
6753 {
6754 dw_die_ref target_die = AT_ref (at);
6755
6756 /* For pointer and reference types, we checksum only the (qualified)
6757 name of the target type (if there is a name). For friend entries,
6758 we checksum only the (qualified) name of the target type or function.
6759 This allows the checksum to remain the same whether the target type
6760 is complete or not. */
6761 if ((at->dw_attr == DW_AT_type
6762 && (tag == DW_TAG_pointer_type
6763 || tag == DW_TAG_reference_type
6764 || tag == DW_TAG_rvalue_reference_type
6765 || tag == DW_TAG_ptr_to_member_type))
6766 || (at->dw_attr == DW_AT_friend
6767 && tag == DW_TAG_friend))
6768 {
6769 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6770
6771 if (name_attr != NULL)
6772 {
6773 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6774
6775 if (decl == NULL)
6776 decl = target_die;
6777 CHECKSUM_ULEB128 ('N');
6778 CHECKSUM_ULEB128 (at->dw_attr);
6779 if (decl->die_parent != NULL)
6780 checksum_die_context (decl->die_parent, ctx);
6781 CHECKSUM_ULEB128 ('E');
6782 CHECKSUM_STRING (AT_string (name_attr));
6783 return;
6784 }
6785 }
6786
6787 /* For all other references to another DIE, we check to see if the
6788 target DIE has already been visited. If it has, we emit a
6789 backward reference; if not, we descend recursively. */
6790 if (target_die->die_mark > 0)
6791 {
6792 CHECKSUM_ULEB128 ('R');
6793 CHECKSUM_ULEB128 (at->dw_attr);
6794 CHECKSUM_ULEB128 (target_die->die_mark);
6795 }
6796 else
6797 {
6798 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6799
6800 if (decl == NULL)
6801 decl = target_die;
6802 target_die->die_mark = ++(*mark);
6803 CHECKSUM_ULEB128 ('T');
6804 CHECKSUM_ULEB128 (at->dw_attr);
6805 if (decl->die_parent != NULL)
6806 checksum_die_context (decl->die_parent, ctx);
6807 die_checksum_ordered (target_die, ctx, mark);
6808 }
6809 return;
6810 }
6811
6812 CHECKSUM_ULEB128 ('A');
6813 CHECKSUM_ULEB128 (at->dw_attr);
6814
6815 switch (AT_class (at))
6816 {
6817 case dw_val_class_const:
6818 case dw_val_class_const_implicit:
6819 CHECKSUM_ULEB128 (DW_FORM_sdata);
6820 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6821 break;
6822
6823 case dw_val_class_unsigned_const:
6824 case dw_val_class_unsigned_const_implicit:
6825 CHECKSUM_ULEB128 (DW_FORM_sdata);
6826 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6827 break;
6828
6829 case dw_val_class_const_double:
6830 CHECKSUM_ULEB128 (DW_FORM_block);
6831 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6832 CHECKSUM (at->dw_attr_val.v.val_double);
6833 break;
6834
6835 case dw_val_class_wide_int:
6836 CHECKSUM_ULEB128 (DW_FORM_block);
6837 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6838 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6839 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6840 get_full_len (*at->dw_attr_val.v.val_wide)
6841 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6842 break;
6843
6844 case dw_val_class_vec:
6845 CHECKSUM_ULEB128 (DW_FORM_block);
6846 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6847 * at->dw_attr_val.v.val_vec.elt_size);
6848 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6849 (at->dw_attr_val.v.val_vec.length
6850 * at->dw_attr_val.v.val_vec.elt_size));
6851 break;
6852
6853 case dw_val_class_flag:
6854 CHECKSUM_ULEB128 (DW_FORM_flag);
6855 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6856 break;
6857
6858 case dw_val_class_str:
6859 CHECKSUM_ULEB128 (DW_FORM_string);
6860 CHECKSUM_STRING (AT_string (at));
6861 break;
6862
6863 case dw_val_class_addr:
6864 r = AT_addr (at);
6865 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6866 CHECKSUM_ULEB128 (DW_FORM_string);
6867 CHECKSUM_STRING (XSTR (r, 0));
6868 break;
6869
6870 case dw_val_class_offset:
6871 CHECKSUM_ULEB128 (DW_FORM_sdata);
6872 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6873 break;
6874
6875 case dw_val_class_loc:
6876 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6877 loc_checksum_ordered (loc, ctx);
6878 break;
6879
6880 case dw_val_class_fde_ref:
6881 case dw_val_class_lbl_id:
6882 case dw_val_class_lineptr:
6883 case dw_val_class_macptr:
6884 case dw_val_class_loclistsptr:
6885 case dw_val_class_high_pc:
6886 break;
6887
6888 case dw_val_class_file:
6889 case dw_val_class_file_implicit:
6890 CHECKSUM_ULEB128 (DW_FORM_string);
6891 CHECKSUM_STRING (AT_file (at)->filename);
6892 break;
6893
6894 case dw_val_class_data8:
6895 CHECKSUM (at->dw_attr_val.v.val_data8);
6896 break;
6897
6898 default:
6899 break;
6900 }
6901 }
6902
6903 struct checksum_attributes
6904 {
6905 dw_attr_node *at_name;
6906 dw_attr_node *at_type;
6907 dw_attr_node *at_friend;
6908 dw_attr_node *at_accessibility;
6909 dw_attr_node *at_address_class;
6910 dw_attr_node *at_alignment;
6911 dw_attr_node *at_allocated;
6912 dw_attr_node *at_artificial;
6913 dw_attr_node *at_associated;
6914 dw_attr_node *at_binary_scale;
6915 dw_attr_node *at_bit_offset;
6916 dw_attr_node *at_bit_size;
6917 dw_attr_node *at_bit_stride;
6918 dw_attr_node *at_byte_size;
6919 dw_attr_node *at_byte_stride;
6920 dw_attr_node *at_const_value;
6921 dw_attr_node *at_containing_type;
6922 dw_attr_node *at_count;
6923 dw_attr_node *at_data_location;
6924 dw_attr_node *at_data_member_location;
6925 dw_attr_node *at_decimal_scale;
6926 dw_attr_node *at_decimal_sign;
6927 dw_attr_node *at_default_value;
6928 dw_attr_node *at_digit_count;
6929 dw_attr_node *at_discr;
6930 dw_attr_node *at_discr_list;
6931 dw_attr_node *at_discr_value;
6932 dw_attr_node *at_encoding;
6933 dw_attr_node *at_endianity;
6934 dw_attr_node *at_explicit;
6935 dw_attr_node *at_is_optional;
6936 dw_attr_node *at_location;
6937 dw_attr_node *at_lower_bound;
6938 dw_attr_node *at_mutable;
6939 dw_attr_node *at_ordering;
6940 dw_attr_node *at_picture_string;
6941 dw_attr_node *at_prototyped;
6942 dw_attr_node *at_small;
6943 dw_attr_node *at_segment;
6944 dw_attr_node *at_string_length;
6945 dw_attr_node *at_string_length_bit_size;
6946 dw_attr_node *at_string_length_byte_size;
6947 dw_attr_node *at_threads_scaled;
6948 dw_attr_node *at_upper_bound;
6949 dw_attr_node *at_use_location;
6950 dw_attr_node *at_use_UTF8;
6951 dw_attr_node *at_variable_parameter;
6952 dw_attr_node *at_virtuality;
6953 dw_attr_node *at_visibility;
6954 dw_attr_node *at_vtable_elem_location;
6955 };
6956
6957 /* Collect the attributes that we will want to use for the checksum. */
6958
6959 static void
6960 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6961 {
6962 dw_attr_node *a;
6963 unsigned ix;
6964
6965 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6966 {
6967 switch (a->dw_attr)
6968 {
6969 case DW_AT_name:
6970 attrs->at_name = a;
6971 break;
6972 case DW_AT_type:
6973 attrs->at_type = a;
6974 break;
6975 case DW_AT_friend:
6976 attrs->at_friend = a;
6977 break;
6978 case DW_AT_accessibility:
6979 attrs->at_accessibility = a;
6980 break;
6981 case DW_AT_address_class:
6982 attrs->at_address_class = a;
6983 break;
6984 case DW_AT_alignment:
6985 attrs->at_alignment = a;
6986 break;
6987 case DW_AT_allocated:
6988 attrs->at_allocated = a;
6989 break;
6990 case DW_AT_artificial:
6991 attrs->at_artificial = a;
6992 break;
6993 case DW_AT_associated:
6994 attrs->at_associated = a;
6995 break;
6996 case DW_AT_binary_scale:
6997 attrs->at_binary_scale = a;
6998 break;
6999 case DW_AT_bit_offset:
7000 attrs->at_bit_offset = a;
7001 break;
7002 case DW_AT_bit_size:
7003 attrs->at_bit_size = a;
7004 break;
7005 case DW_AT_bit_stride:
7006 attrs->at_bit_stride = a;
7007 break;
7008 case DW_AT_byte_size:
7009 attrs->at_byte_size = a;
7010 break;
7011 case DW_AT_byte_stride:
7012 attrs->at_byte_stride = a;
7013 break;
7014 case DW_AT_const_value:
7015 attrs->at_const_value = a;
7016 break;
7017 case DW_AT_containing_type:
7018 attrs->at_containing_type = a;
7019 break;
7020 case DW_AT_count:
7021 attrs->at_count = a;
7022 break;
7023 case DW_AT_data_location:
7024 attrs->at_data_location = a;
7025 break;
7026 case DW_AT_data_member_location:
7027 attrs->at_data_member_location = a;
7028 break;
7029 case DW_AT_decimal_scale:
7030 attrs->at_decimal_scale = a;
7031 break;
7032 case DW_AT_decimal_sign:
7033 attrs->at_decimal_sign = a;
7034 break;
7035 case DW_AT_default_value:
7036 attrs->at_default_value = a;
7037 break;
7038 case DW_AT_digit_count:
7039 attrs->at_digit_count = a;
7040 break;
7041 case DW_AT_discr:
7042 attrs->at_discr = a;
7043 break;
7044 case DW_AT_discr_list:
7045 attrs->at_discr_list = a;
7046 break;
7047 case DW_AT_discr_value:
7048 attrs->at_discr_value = a;
7049 break;
7050 case DW_AT_encoding:
7051 attrs->at_encoding = a;
7052 break;
7053 case DW_AT_endianity:
7054 attrs->at_endianity = a;
7055 break;
7056 case DW_AT_explicit:
7057 attrs->at_explicit = a;
7058 break;
7059 case DW_AT_is_optional:
7060 attrs->at_is_optional = a;
7061 break;
7062 case DW_AT_location:
7063 attrs->at_location = a;
7064 break;
7065 case DW_AT_lower_bound:
7066 attrs->at_lower_bound = a;
7067 break;
7068 case DW_AT_mutable:
7069 attrs->at_mutable = a;
7070 break;
7071 case DW_AT_ordering:
7072 attrs->at_ordering = a;
7073 break;
7074 case DW_AT_picture_string:
7075 attrs->at_picture_string = a;
7076 break;
7077 case DW_AT_prototyped:
7078 attrs->at_prototyped = a;
7079 break;
7080 case DW_AT_small:
7081 attrs->at_small = a;
7082 break;
7083 case DW_AT_segment:
7084 attrs->at_segment = a;
7085 break;
7086 case DW_AT_string_length:
7087 attrs->at_string_length = a;
7088 break;
7089 case DW_AT_string_length_bit_size:
7090 attrs->at_string_length_bit_size = a;
7091 break;
7092 case DW_AT_string_length_byte_size:
7093 attrs->at_string_length_byte_size = a;
7094 break;
7095 case DW_AT_threads_scaled:
7096 attrs->at_threads_scaled = a;
7097 break;
7098 case DW_AT_upper_bound:
7099 attrs->at_upper_bound = a;
7100 break;
7101 case DW_AT_use_location:
7102 attrs->at_use_location = a;
7103 break;
7104 case DW_AT_use_UTF8:
7105 attrs->at_use_UTF8 = a;
7106 break;
7107 case DW_AT_variable_parameter:
7108 attrs->at_variable_parameter = a;
7109 break;
7110 case DW_AT_virtuality:
7111 attrs->at_virtuality = a;
7112 break;
7113 case DW_AT_visibility:
7114 attrs->at_visibility = a;
7115 break;
7116 case DW_AT_vtable_elem_location:
7117 attrs->at_vtable_elem_location = a;
7118 break;
7119 default:
7120 break;
7121 }
7122 }
7123 }
7124
7125 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7126
7127 static void
7128 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7129 {
7130 dw_die_ref c;
7131 dw_die_ref decl;
7132 struct checksum_attributes attrs;
7133
7134 CHECKSUM_ULEB128 ('D');
7135 CHECKSUM_ULEB128 (die->die_tag);
7136
7137 memset (&attrs, 0, sizeof (attrs));
7138
7139 decl = get_AT_ref (die, DW_AT_specification);
7140 if (decl != NULL)
7141 collect_checksum_attributes (&attrs, decl);
7142 collect_checksum_attributes (&attrs, die);
7143
7144 CHECKSUM_ATTR (attrs.at_name);
7145 CHECKSUM_ATTR (attrs.at_accessibility);
7146 CHECKSUM_ATTR (attrs.at_address_class);
7147 CHECKSUM_ATTR (attrs.at_allocated);
7148 CHECKSUM_ATTR (attrs.at_artificial);
7149 CHECKSUM_ATTR (attrs.at_associated);
7150 CHECKSUM_ATTR (attrs.at_binary_scale);
7151 CHECKSUM_ATTR (attrs.at_bit_offset);
7152 CHECKSUM_ATTR (attrs.at_bit_size);
7153 CHECKSUM_ATTR (attrs.at_bit_stride);
7154 CHECKSUM_ATTR (attrs.at_byte_size);
7155 CHECKSUM_ATTR (attrs.at_byte_stride);
7156 CHECKSUM_ATTR (attrs.at_const_value);
7157 CHECKSUM_ATTR (attrs.at_containing_type);
7158 CHECKSUM_ATTR (attrs.at_count);
7159 CHECKSUM_ATTR (attrs.at_data_location);
7160 CHECKSUM_ATTR (attrs.at_data_member_location);
7161 CHECKSUM_ATTR (attrs.at_decimal_scale);
7162 CHECKSUM_ATTR (attrs.at_decimal_sign);
7163 CHECKSUM_ATTR (attrs.at_default_value);
7164 CHECKSUM_ATTR (attrs.at_digit_count);
7165 CHECKSUM_ATTR (attrs.at_discr);
7166 CHECKSUM_ATTR (attrs.at_discr_list);
7167 CHECKSUM_ATTR (attrs.at_discr_value);
7168 CHECKSUM_ATTR (attrs.at_encoding);
7169 CHECKSUM_ATTR (attrs.at_endianity);
7170 CHECKSUM_ATTR (attrs.at_explicit);
7171 CHECKSUM_ATTR (attrs.at_is_optional);
7172 CHECKSUM_ATTR (attrs.at_location);
7173 CHECKSUM_ATTR (attrs.at_lower_bound);
7174 CHECKSUM_ATTR (attrs.at_mutable);
7175 CHECKSUM_ATTR (attrs.at_ordering);
7176 CHECKSUM_ATTR (attrs.at_picture_string);
7177 CHECKSUM_ATTR (attrs.at_prototyped);
7178 CHECKSUM_ATTR (attrs.at_small);
7179 CHECKSUM_ATTR (attrs.at_segment);
7180 CHECKSUM_ATTR (attrs.at_string_length);
7181 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7182 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7183 CHECKSUM_ATTR (attrs.at_threads_scaled);
7184 CHECKSUM_ATTR (attrs.at_upper_bound);
7185 CHECKSUM_ATTR (attrs.at_use_location);
7186 CHECKSUM_ATTR (attrs.at_use_UTF8);
7187 CHECKSUM_ATTR (attrs.at_variable_parameter);
7188 CHECKSUM_ATTR (attrs.at_virtuality);
7189 CHECKSUM_ATTR (attrs.at_visibility);
7190 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7191 CHECKSUM_ATTR (attrs.at_type);
7192 CHECKSUM_ATTR (attrs.at_friend);
7193 CHECKSUM_ATTR (attrs.at_alignment);
7194
7195 /* Checksum the child DIEs. */
7196 c = die->die_child;
7197 if (c) do {
7198 dw_attr_node *name_attr;
7199
7200 c = c->die_sib;
7201 name_attr = get_AT (c, DW_AT_name);
7202 if (is_template_instantiation (c))
7203 {
7204 /* Ignore instantiations of member type and function templates. */
7205 }
7206 else if (name_attr != NULL
7207 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7208 {
7209 /* Use a shallow checksum for named nested types and member
7210 functions. */
7211 CHECKSUM_ULEB128 ('S');
7212 CHECKSUM_ULEB128 (c->die_tag);
7213 CHECKSUM_STRING (AT_string (name_attr));
7214 }
7215 else
7216 {
7217 /* Use a deep checksum for other children. */
7218 /* Mark this DIE so it gets processed when unmarking. */
7219 if (c->die_mark == 0)
7220 c->die_mark = -1;
7221 die_checksum_ordered (c, ctx, mark);
7222 }
7223 } while (c != die->die_child);
7224
7225 CHECKSUM_ULEB128 (0);
7226 }
7227
7228 /* Add a type name and tag to a hash. */
7229 static void
7230 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7231 {
7232 CHECKSUM_ULEB128 (tag);
7233 CHECKSUM_STRING (name);
7234 }
7235
7236 #undef CHECKSUM
7237 #undef CHECKSUM_STRING
7238 #undef CHECKSUM_ATTR
7239 #undef CHECKSUM_LEB128
7240 #undef CHECKSUM_ULEB128
7241
7242 /* Generate the type signature for DIE. This is computed by generating an
7243 MD5 checksum over the DIE's tag, its relevant attributes, and its
7244 children. Attributes that are references to other DIEs are processed
7245 by recursion, using the MARK field to prevent infinite recursion.
7246 If the DIE is nested inside a namespace or another type, we also
7247 need to include that context in the signature. The lower 64 bits
7248 of the resulting MD5 checksum comprise the signature. */
7249
7250 static void
7251 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7252 {
7253 int mark;
7254 const char *name;
7255 unsigned char checksum[16];
7256 struct md5_ctx ctx;
7257 dw_die_ref decl;
7258 dw_die_ref parent;
7259
7260 name = get_AT_string (die, DW_AT_name);
7261 decl = get_AT_ref (die, DW_AT_specification);
7262 parent = get_die_parent (die);
7263
7264 /* First, compute a signature for just the type name (and its surrounding
7265 context, if any. This is stored in the type unit DIE for link-time
7266 ODR (one-definition rule) checking. */
7267
7268 if (is_cxx () && name != NULL)
7269 {
7270 md5_init_ctx (&ctx);
7271
7272 /* Checksum the names of surrounding namespaces and structures. */
7273 if (parent != NULL)
7274 checksum_die_context (parent, &ctx);
7275
7276 /* Checksum the current DIE. */
7277 die_odr_checksum (die->die_tag, name, &ctx);
7278 md5_finish_ctx (&ctx, checksum);
7279
7280 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7281 }
7282
7283 /* Next, compute the complete type signature. */
7284
7285 md5_init_ctx (&ctx);
7286 mark = 1;
7287 die->die_mark = mark;
7288
7289 /* Checksum the names of surrounding namespaces and structures. */
7290 if (parent != NULL)
7291 checksum_die_context (parent, &ctx);
7292
7293 /* Checksum the DIE and its children. */
7294 die_checksum_ordered (die, &ctx, &mark);
7295 unmark_all_dies (die);
7296 md5_finish_ctx (&ctx, checksum);
7297
7298 /* Store the signature in the type node and link the type DIE and the
7299 type node together. */
7300 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7301 DWARF_TYPE_SIGNATURE_SIZE);
7302 die->comdat_type_p = true;
7303 die->die_id.die_type_node = type_node;
7304 type_node->type_die = die;
7305
7306 /* If the DIE is a specification, link its declaration to the type node
7307 as well. */
7308 if (decl != NULL)
7309 {
7310 decl->comdat_type_p = true;
7311 decl->die_id.die_type_node = type_node;
7312 }
7313 }
7314
7315 /* Do the location expressions look same? */
7316 static inline int
7317 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7318 {
7319 return loc1->dw_loc_opc == loc2->dw_loc_opc
7320 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7321 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7322 }
7323
7324 /* Do the values look the same? */
7325 static int
7326 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7327 {
7328 dw_loc_descr_ref loc1, loc2;
7329 rtx r1, r2;
7330
7331 if (v1->val_class != v2->val_class)
7332 return 0;
7333
7334 switch (v1->val_class)
7335 {
7336 case dw_val_class_const:
7337 case dw_val_class_const_implicit:
7338 return v1->v.val_int == v2->v.val_int;
7339 case dw_val_class_unsigned_const:
7340 case dw_val_class_unsigned_const_implicit:
7341 return v1->v.val_unsigned == v2->v.val_unsigned;
7342 case dw_val_class_const_double:
7343 return v1->v.val_double.high == v2->v.val_double.high
7344 && v1->v.val_double.low == v2->v.val_double.low;
7345 case dw_val_class_wide_int:
7346 return *v1->v.val_wide == *v2->v.val_wide;
7347 case dw_val_class_vec:
7348 if (v1->v.val_vec.length != v2->v.val_vec.length
7349 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7350 return 0;
7351 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7352 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7353 return 0;
7354 return 1;
7355 case dw_val_class_flag:
7356 return v1->v.val_flag == v2->v.val_flag;
7357 case dw_val_class_str:
7358 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7359
7360 case dw_val_class_addr:
7361 r1 = v1->v.val_addr;
7362 r2 = v2->v.val_addr;
7363 if (GET_CODE (r1) != GET_CODE (r2))
7364 return 0;
7365 return !rtx_equal_p (r1, r2);
7366
7367 case dw_val_class_offset:
7368 return v1->v.val_offset == v2->v.val_offset;
7369
7370 case dw_val_class_loc:
7371 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7372 loc1 && loc2;
7373 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7374 if (!same_loc_p (loc1, loc2, mark))
7375 return 0;
7376 return !loc1 && !loc2;
7377
7378 case dw_val_class_die_ref:
7379 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7380
7381 case dw_val_class_fde_ref:
7382 case dw_val_class_vms_delta:
7383 case dw_val_class_lbl_id:
7384 case dw_val_class_lineptr:
7385 case dw_val_class_macptr:
7386 case dw_val_class_loclistsptr:
7387 case dw_val_class_high_pc:
7388 return 1;
7389
7390 case dw_val_class_file:
7391 case dw_val_class_file_implicit:
7392 return v1->v.val_file == v2->v.val_file;
7393
7394 case dw_val_class_data8:
7395 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7396
7397 default:
7398 return 1;
7399 }
7400 }
7401
7402 /* Do the attributes look the same? */
7403
7404 static int
7405 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7406 {
7407 if (at1->dw_attr != at2->dw_attr)
7408 return 0;
7409
7410 /* We don't care that this was compiled with a different compiler
7411 snapshot; if the output is the same, that's what matters. */
7412 if (at1->dw_attr == DW_AT_producer)
7413 return 1;
7414
7415 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7416 }
7417
7418 /* Do the dies look the same? */
7419
7420 static int
7421 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7422 {
7423 dw_die_ref c1, c2;
7424 dw_attr_node *a1;
7425 unsigned ix;
7426
7427 /* To avoid infinite recursion. */
7428 if (die1->die_mark)
7429 return die1->die_mark == die2->die_mark;
7430 die1->die_mark = die2->die_mark = ++(*mark);
7431
7432 if (die1->die_tag != die2->die_tag)
7433 return 0;
7434
7435 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7436 return 0;
7437
7438 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7439 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7440 return 0;
7441
7442 c1 = die1->die_child;
7443 c2 = die2->die_child;
7444 if (! c1)
7445 {
7446 if (c2)
7447 return 0;
7448 }
7449 else
7450 for (;;)
7451 {
7452 if (!same_die_p (c1, c2, mark))
7453 return 0;
7454 c1 = c1->die_sib;
7455 c2 = c2->die_sib;
7456 if (c1 == die1->die_child)
7457 {
7458 if (c2 == die2->die_child)
7459 break;
7460 else
7461 return 0;
7462 }
7463 }
7464
7465 return 1;
7466 }
7467
7468 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7469 children, and set die_symbol. */
7470
7471 static void
7472 compute_comp_unit_symbol (dw_die_ref unit_die)
7473 {
7474 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7475 const char *base = die_name ? lbasename (die_name) : "anonymous";
7476 char *name = XALLOCAVEC (char, strlen (base) + 64);
7477 char *p;
7478 int i, mark;
7479 unsigned char checksum[16];
7480 struct md5_ctx ctx;
7481
7482 /* Compute the checksum of the DIE, then append part of it as hex digits to
7483 the name filename of the unit. */
7484
7485 md5_init_ctx (&ctx);
7486 mark = 0;
7487 die_checksum (unit_die, &ctx, &mark);
7488 unmark_all_dies (unit_die);
7489 md5_finish_ctx (&ctx, checksum);
7490
7491 /* When we this for comp_unit_die () we have a DW_AT_name that might
7492 not start with a letter but with anything valid for filenames and
7493 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7494 character is not a letter. */
7495 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7496 clean_symbol_name (name);
7497
7498 p = name + strlen (name);
7499 for (i = 0; i < 4; i++)
7500 {
7501 sprintf (p, "%.2x", checksum[i]);
7502 p += 2;
7503 }
7504
7505 unit_die->die_id.die_symbol = xstrdup (name);
7506 }
7507
7508 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7509
7510 static int
7511 is_type_die (dw_die_ref die)
7512 {
7513 switch (die->die_tag)
7514 {
7515 case DW_TAG_array_type:
7516 case DW_TAG_class_type:
7517 case DW_TAG_interface_type:
7518 case DW_TAG_enumeration_type:
7519 case DW_TAG_pointer_type:
7520 case DW_TAG_reference_type:
7521 case DW_TAG_rvalue_reference_type:
7522 case DW_TAG_string_type:
7523 case DW_TAG_structure_type:
7524 case DW_TAG_subroutine_type:
7525 case DW_TAG_union_type:
7526 case DW_TAG_ptr_to_member_type:
7527 case DW_TAG_set_type:
7528 case DW_TAG_subrange_type:
7529 case DW_TAG_base_type:
7530 case DW_TAG_const_type:
7531 case DW_TAG_file_type:
7532 case DW_TAG_packed_type:
7533 case DW_TAG_volatile_type:
7534 case DW_TAG_typedef:
7535 return 1;
7536 default:
7537 return 0;
7538 }
7539 }
7540
7541 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7542 Basically, we want to choose the bits that are likely to be shared between
7543 compilations (types) and leave out the bits that are specific to individual
7544 compilations (functions). */
7545
7546 static int
7547 is_comdat_die (dw_die_ref c)
7548 {
7549 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7550 we do for stabs. The advantage is a greater likelihood of sharing between
7551 objects that don't include headers in the same order (and therefore would
7552 put the base types in a different comdat). jason 8/28/00 */
7553
7554 if (c->die_tag == DW_TAG_base_type)
7555 return 0;
7556
7557 if (c->die_tag == DW_TAG_pointer_type
7558 || c->die_tag == DW_TAG_reference_type
7559 || c->die_tag == DW_TAG_rvalue_reference_type
7560 || c->die_tag == DW_TAG_const_type
7561 || c->die_tag == DW_TAG_volatile_type)
7562 {
7563 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7564
7565 return t ? is_comdat_die (t) : 0;
7566 }
7567
7568 return is_type_die (c);
7569 }
7570
7571 /* Returns true iff C is a compile-unit DIE. */
7572
7573 static inline bool
7574 is_cu_die (dw_die_ref c)
7575 {
7576 return c && (c->die_tag == DW_TAG_compile_unit
7577 || c->die_tag == DW_TAG_skeleton_unit);
7578 }
7579
7580 /* Returns true iff C is a unit DIE of some sort. */
7581
7582 static inline bool
7583 is_unit_die (dw_die_ref c)
7584 {
7585 return c && (c->die_tag == DW_TAG_compile_unit
7586 || c->die_tag == DW_TAG_partial_unit
7587 || c->die_tag == DW_TAG_type_unit
7588 || c->die_tag == DW_TAG_skeleton_unit);
7589 }
7590
7591 /* Returns true iff C is a namespace DIE. */
7592
7593 static inline bool
7594 is_namespace_die (dw_die_ref c)
7595 {
7596 return c && c->die_tag == DW_TAG_namespace;
7597 }
7598
7599 /* Returns true iff C is a class or structure DIE. */
7600
7601 static inline bool
7602 is_class_die (dw_die_ref c)
7603 {
7604 return c && (c->die_tag == DW_TAG_class_type
7605 || c->die_tag == DW_TAG_structure_type);
7606 }
7607
7608 /* Return non-zero if this DIE is a template parameter. */
7609
7610 static inline bool
7611 is_template_parameter (dw_die_ref die)
7612 {
7613 switch (die->die_tag)
7614 {
7615 case DW_TAG_template_type_param:
7616 case DW_TAG_template_value_param:
7617 case DW_TAG_GNU_template_template_param:
7618 case DW_TAG_GNU_template_parameter_pack:
7619 return true;
7620 default:
7621 return false;
7622 }
7623 }
7624
7625 /* Return non-zero if this DIE represents a template instantiation. */
7626
7627 static inline bool
7628 is_template_instantiation (dw_die_ref die)
7629 {
7630 dw_die_ref c;
7631
7632 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7633 return false;
7634 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7635 return false;
7636 }
7637
7638 static char *
7639 gen_internal_sym (const char *prefix)
7640 {
7641 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7642
7643 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7644 return xstrdup (buf);
7645 }
7646
7647 /* Return non-zero if this DIE is a declaration. */
7648
7649 static int
7650 is_declaration_die (dw_die_ref die)
7651 {
7652 dw_attr_node *a;
7653 unsigned ix;
7654
7655 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7656 if (a->dw_attr == DW_AT_declaration)
7657 return 1;
7658
7659 return 0;
7660 }
7661
7662 /* Return non-zero if this DIE is nested inside a subprogram. */
7663
7664 static int
7665 is_nested_in_subprogram (dw_die_ref die)
7666 {
7667 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7668
7669 if (decl == NULL)
7670 decl = die;
7671 return local_scope_p (decl);
7672 }
7673
7674 /* Return non-zero if this DIE contains a defining declaration of a
7675 subprogram. */
7676
7677 static int
7678 contains_subprogram_definition (dw_die_ref die)
7679 {
7680 dw_die_ref c;
7681
7682 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7683 return 1;
7684 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7685 return 0;
7686 }
7687
7688 /* Return non-zero if this is a type DIE that should be moved to a
7689 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7690 unit type. */
7691
7692 static int
7693 should_move_die_to_comdat (dw_die_ref die)
7694 {
7695 switch (die->die_tag)
7696 {
7697 case DW_TAG_class_type:
7698 case DW_TAG_structure_type:
7699 case DW_TAG_enumeration_type:
7700 case DW_TAG_union_type:
7701 /* Don't move declarations, inlined instances, types nested in a
7702 subprogram, or types that contain subprogram definitions. */
7703 if (is_declaration_die (die)
7704 || get_AT (die, DW_AT_abstract_origin)
7705 || is_nested_in_subprogram (die)
7706 || contains_subprogram_definition (die))
7707 return 0;
7708 return 1;
7709 case DW_TAG_array_type:
7710 case DW_TAG_interface_type:
7711 case DW_TAG_pointer_type:
7712 case DW_TAG_reference_type:
7713 case DW_TAG_rvalue_reference_type:
7714 case DW_TAG_string_type:
7715 case DW_TAG_subroutine_type:
7716 case DW_TAG_ptr_to_member_type:
7717 case DW_TAG_set_type:
7718 case DW_TAG_subrange_type:
7719 case DW_TAG_base_type:
7720 case DW_TAG_const_type:
7721 case DW_TAG_file_type:
7722 case DW_TAG_packed_type:
7723 case DW_TAG_volatile_type:
7724 case DW_TAG_typedef:
7725 default:
7726 return 0;
7727 }
7728 }
7729
7730 /* Make a clone of DIE. */
7731
7732 static dw_die_ref
7733 clone_die (dw_die_ref die)
7734 {
7735 dw_die_ref clone = new_die_raw (die->die_tag);
7736 dw_attr_node *a;
7737 unsigned ix;
7738
7739 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7740 add_dwarf_attr (clone, a);
7741
7742 return clone;
7743 }
7744
7745 /* Make a clone of the tree rooted at DIE. */
7746
7747 static dw_die_ref
7748 clone_tree (dw_die_ref die)
7749 {
7750 dw_die_ref c;
7751 dw_die_ref clone = clone_die (die);
7752
7753 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7754
7755 return clone;
7756 }
7757
7758 /* Make a clone of DIE as a declaration. */
7759
7760 static dw_die_ref
7761 clone_as_declaration (dw_die_ref die)
7762 {
7763 dw_die_ref clone;
7764 dw_die_ref decl;
7765 dw_attr_node *a;
7766 unsigned ix;
7767
7768 /* If the DIE is already a declaration, just clone it. */
7769 if (is_declaration_die (die))
7770 return clone_die (die);
7771
7772 /* If the DIE is a specification, just clone its declaration DIE. */
7773 decl = get_AT_ref (die, DW_AT_specification);
7774 if (decl != NULL)
7775 {
7776 clone = clone_die (decl);
7777 if (die->comdat_type_p)
7778 add_AT_die_ref (clone, DW_AT_signature, die);
7779 return clone;
7780 }
7781
7782 clone = new_die_raw (die->die_tag);
7783
7784 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7785 {
7786 /* We don't want to copy over all attributes.
7787 For example we don't want DW_AT_byte_size because otherwise we will no
7788 longer have a declaration and GDB will treat it as a definition. */
7789
7790 switch (a->dw_attr)
7791 {
7792 case DW_AT_abstract_origin:
7793 case DW_AT_artificial:
7794 case DW_AT_containing_type:
7795 case DW_AT_external:
7796 case DW_AT_name:
7797 case DW_AT_type:
7798 case DW_AT_virtuality:
7799 case DW_AT_linkage_name:
7800 case DW_AT_MIPS_linkage_name:
7801 add_dwarf_attr (clone, a);
7802 break;
7803 case DW_AT_byte_size:
7804 case DW_AT_alignment:
7805 default:
7806 break;
7807 }
7808 }
7809
7810 if (die->comdat_type_p)
7811 add_AT_die_ref (clone, DW_AT_signature, die);
7812
7813 add_AT_flag (clone, DW_AT_declaration, 1);
7814 return clone;
7815 }
7816
7817
7818 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7819
7820 struct decl_table_entry
7821 {
7822 dw_die_ref orig;
7823 dw_die_ref copy;
7824 };
7825
7826 /* Helpers to manipulate hash table of copied declarations. */
7827
7828 /* Hashtable helpers. */
7829
7830 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7831 {
7832 typedef die_struct *compare_type;
7833 static inline hashval_t hash (const decl_table_entry *);
7834 static inline bool equal (const decl_table_entry *, const die_struct *);
7835 };
7836
7837 inline hashval_t
7838 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7839 {
7840 return htab_hash_pointer (entry->orig);
7841 }
7842
7843 inline bool
7844 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7845 const die_struct *entry2)
7846 {
7847 return entry1->orig == entry2;
7848 }
7849
7850 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7851
7852 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7853 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7854 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7855 to check if the ancestor has already been copied into UNIT. */
7856
7857 static dw_die_ref
7858 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7859 decl_hash_type *decl_table)
7860 {
7861 dw_die_ref parent = die->die_parent;
7862 dw_die_ref new_parent = unit;
7863 dw_die_ref copy;
7864 decl_table_entry **slot = NULL;
7865 struct decl_table_entry *entry = NULL;
7866
7867 if (decl_table)
7868 {
7869 /* Check if the entry has already been copied to UNIT. */
7870 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7871 INSERT);
7872 if (*slot != HTAB_EMPTY_ENTRY)
7873 {
7874 entry = *slot;
7875 return entry->copy;
7876 }
7877
7878 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7879 entry = XCNEW (struct decl_table_entry);
7880 entry->orig = die;
7881 entry->copy = NULL;
7882 *slot = entry;
7883 }
7884
7885 if (parent != NULL)
7886 {
7887 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7888 if (spec != NULL)
7889 parent = spec;
7890 if (!is_unit_die (parent))
7891 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7892 }
7893
7894 copy = clone_as_declaration (die);
7895 add_child_die (new_parent, copy);
7896
7897 if (decl_table)
7898 {
7899 /* Record the pointer to the copy. */
7900 entry->copy = copy;
7901 }
7902
7903 return copy;
7904 }
7905 /* Copy the declaration context to the new type unit DIE. This includes
7906 any surrounding namespace or type declarations. If the DIE has an
7907 AT_specification attribute, it also includes attributes and children
7908 attached to the specification, and returns a pointer to the original
7909 parent of the declaration DIE. Returns NULL otherwise. */
7910
7911 static dw_die_ref
7912 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7913 {
7914 dw_die_ref decl;
7915 dw_die_ref new_decl;
7916 dw_die_ref orig_parent = NULL;
7917
7918 decl = get_AT_ref (die, DW_AT_specification);
7919 if (decl == NULL)
7920 decl = die;
7921 else
7922 {
7923 unsigned ix;
7924 dw_die_ref c;
7925 dw_attr_node *a;
7926
7927 /* The original DIE will be changed to a declaration, and must
7928 be moved to be a child of the original declaration DIE. */
7929 orig_parent = decl->die_parent;
7930
7931 /* Copy the type node pointer from the new DIE to the original
7932 declaration DIE so we can forward references later. */
7933 decl->comdat_type_p = true;
7934 decl->die_id.die_type_node = die->die_id.die_type_node;
7935
7936 remove_AT (die, DW_AT_specification);
7937
7938 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7939 {
7940 if (a->dw_attr != DW_AT_name
7941 && a->dw_attr != DW_AT_declaration
7942 && a->dw_attr != DW_AT_external)
7943 add_dwarf_attr (die, a);
7944 }
7945
7946 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7947 }
7948
7949 if (decl->die_parent != NULL
7950 && !is_unit_die (decl->die_parent))
7951 {
7952 new_decl = copy_ancestor_tree (unit, decl, NULL);
7953 if (new_decl != NULL)
7954 {
7955 remove_AT (new_decl, DW_AT_signature);
7956 add_AT_specification (die, new_decl);
7957 }
7958 }
7959
7960 return orig_parent;
7961 }
7962
7963 /* Generate the skeleton ancestor tree for the given NODE, then clone
7964 the DIE and add the clone into the tree. */
7965
7966 static void
7967 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7968 {
7969 if (node->new_die != NULL)
7970 return;
7971
7972 node->new_die = clone_as_declaration (node->old_die);
7973
7974 if (node->parent != NULL)
7975 {
7976 generate_skeleton_ancestor_tree (node->parent);
7977 add_child_die (node->parent->new_die, node->new_die);
7978 }
7979 }
7980
7981 /* Generate a skeleton tree of DIEs containing any declarations that are
7982 found in the original tree. We traverse the tree looking for declaration
7983 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7984
7985 static void
7986 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7987 {
7988 skeleton_chain_node node;
7989 dw_die_ref c;
7990 dw_die_ref first;
7991 dw_die_ref prev = NULL;
7992 dw_die_ref next = NULL;
7993
7994 node.parent = parent;
7995
7996 first = c = parent->old_die->die_child;
7997 if (c)
7998 next = c->die_sib;
7999 if (c) do {
8000 if (prev == NULL || prev->die_sib == c)
8001 prev = c;
8002 c = next;
8003 next = (c == first ? NULL : c->die_sib);
8004 node.old_die = c;
8005 node.new_die = NULL;
8006 if (is_declaration_die (c))
8007 {
8008 if (is_template_instantiation (c))
8009 {
8010 /* Instantiated templates do not need to be cloned into the
8011 type unit. Just move the DIE and its children back to
8012 the skeleton tree (in the main CU). */
8013 remove_child_with_prev (c, prev);
8014 add_child_die (parent->new_die, c);
8015 c = prev;
8016 }
8017 else if (c->comdat_type_p)
8018 {
8019 /* This is the skeleton of earlier break_out_comdat_types
8020 type. Clone the existing DIE, but keep the children
8021 under the original (which is in the main CU). */
8022 dw_die_ref clone = clone_die (c);
8023
8024 replace_child (c, clone, prev);
8025 generate_skeleton_ancestor_tree (parent);
8026 add_child_die (parent->new_die, c);
8027 c = clone;
8028 continue;
8029 }
8030 else
8031 {
8032 /* Clone the existing DIE, move the original to the skeleton
8033 tree (which is in the main CU), and put the clone, with
8034 all the original's children, where the original came from
8035 (which is about to be moved to the type unit). */
8036 dw_die_ref clone = clone_die (c);
8037 move_all_children (c, clone);
8038
8039 /* If the original has a DW_AT_object_pointer attribute,
8040 it would now point to a child DIE just moved to the
8041 cloned tree, so we need to remove that attribute from
8042 the original. */
8043 remove_AT (c, DW_AT_object_pointer);
8044
8045 replace_child (c, clone, prev);
8046 generate_skeleton_ancestor_tree (parent);
8047 add_child_die (parent->new_die, c);
8048 node.old_die = clone;
8049 node.new_die = c;
8050 c = clone;
8051 }
8052 }
8053 generate_skeleton_bottom_up (&node);
8054 } while (next != NULL);
8055 }
8056
8057 /* Wrapper function for generate_skeleton_bottom_up. */
8058
8059 static dw_die_ref
8060 generate_skeleton (dw_die_ref die)
8061 {
8062 skeleton_chain_node node;
8063
8064 node.old_die = die;
8065 node.new_die = NULL;
8066 node.parent = NULL;
8067
8068 /* If this type definition is nested inside another type,
8069 and is not an instantiation of a template, always leave
8070 at least a declaration in its place. */
8071 if (die->die_parent != NULL
8072 && is_type_die (die->die_parent)
8073 && !is_template_instantiation (die))
8074 node.new_die = clone_as_declaration (die);
8075
8076 generate_skeleton_bottom_up (&node);
8077 return node.new_die;
8078 }
8079
8080 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8081 declaration. The original DIE is moved to a new compile unit so that
8082 existing references to it follow it to the new location. If any of the
8083 original DIE's descendants is a declaration, we need to replace the
8084 original DIE with a skeleton tree and move the declarations back into the
8085 skeleton tree. */
8086
8087 static dw_die_ref
8088 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8089 dw_die_ref prev)
8090 {
8091 dw_die_ref skeleton, orig_parent;
8092
8093 /* Copy the declaration context to the type unit DIE. If the returned
8094 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8095 that DIE. */
8096 orig_parent = copy_declaration_context (unit, child);
8097
8098 skeleton = generate_skeleton (child);
8099 if (skeleton == NULL)
8100 remove_child_with_prev (child, prev);
8101 else
8102 {
8103 skeleton->comdat_type_p = true;
8104 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8105
8106 /* If the original DIE was a specification, we need to put
8107 the skeleton under the parent DIE of the declaration.
8108 This leaves the original declaration in the tree, but
8109 it will be pruned later since there are no longer any
8110 references to it. */
8111 if (orig_parent != NULL)
8112 {
8113 remove_child_with_prev (child, prev);
8114 add_child_die (orig_parent, skeleton);
8115 }
8116 else
8117 replace_child (child, skeleton, prev);
8118 }
8119
8120 return skeleton;
8121 }
8122
8123 static void
8124 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8125 comdat_type_node *type_node,
8126 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8127
8128 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8129 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8130 DWARF procedure references in the DW_AT_location attribute. */
8131
8132 static dw_die_ref
8133 copy_dwarf_procedure (dw_die_ref die,
8134 comdat_type_node *type_node,
8135 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8136 {
8137 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8138
8139 /* DWARF procedures are not supposed to have children... */
8140 gcc_assert (die->die_child == NULL);
8141
8142 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8143 gcc_assert (vec_safe_length (die->die_attr) == 1
8144 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8145
8146 /* Do not copy more than once DWARF procedures. */
8147 bool existed;
8148 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8149 if (existed)
8150 return die_copy;
8151
8152 die_copy = clone_die (die);
8153 add_child_die (type_node->root_die, die_copy);
8154 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8155 return die_copy;
8156 }
8157
8158 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8159 procedures in DIE's attributes. */
8160
8161 static void
8162 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8163 comdat_type_node *type_node,
8164 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8165 {
8166 dw_attr_node *a;
8167 unsigned i;
8168
8169 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8170 {
8171 dw_loc_descr_ref loc;
8172
8173 if (a->dw_attr_val.val_class != dw_val_class_loc)
8174 continue;
8175
8176 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8177 {
8178 switch (loc->dw_loc_opc)
8179 {
8180 case DW_OP_call2:
8181 case DW_OP_call4:
8182 case DW_OP_call_ref:
8183 gcc_assert (loc->dw_loc_oprnd1.val_class
8184 == dw_val_class_die_ref);
8185 loc->dw_loc_oprnd1.v.val_die_ref.die
8186 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8187 type_node,
8188 copied_dwarf_procs);
8189
8190 default:
8191 break;
8192 }
8193 }
8194 }
8195 }
8196
8197 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8198 rewrite references to point to the copies.
8199
8200 References are looked for in DIE's attributes and recursively in all its
8201 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8202 mapping from old DWARF procedures to their copy. It is used not to copy
8203 twice the same DWARF procedure under TYPE_NODE. */
8204
8205 static void
8206 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8207 comdat_type_node *type_node,
8208 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8209 {
8210 dw_die_ref c;
8211
8212 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8213 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8214 type_node,
8215 copied_dwarf_procs));
8216 }
8217
8218 /* Traverse the DIE and set up additional .debug_types or .debug_info
8219 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8220 section. */
8221
8222 static void
8223 break_out_comdat_types (dw_die_ref die)
8224 {
8225 dw_die_ref c;
8226 dw_die_ref first;
8227 dw_die_ref prev = NULL;
8228 dw_die_ref next = NULL;
8229 dw_die_ref unit = NULL;
8230
8231 first = c = die->die_child;
8232 if (c)
8233 next = c->die_sib;
8234 if (c) do {
8235 if (prev == NULL || prev->die_sib == c)
8236 prev = c;
8237 c = next;
8238 next = (c == first ? NULL : c->die_sib);
8239 if (should_move_die_to_comdat (c))
8240 {
8241 dw_die_ref replacement;
8242 comdat_type_node *type_node;
8243
8244 /* Break out nested types into their own type units. */
8245 break_out_comdat_types (c);
8246
8247 /* Create a new type unit DIE as the root for the new tree, and
8248 add it to the list of comdat types. */
8249 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8250 add_AT_unsigned (unit, DW_AT_language,
8251 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8252 type_node = ggc_cleared_alloc<comdat_type_node> ();
8253 type_node->root_die = unit;
8254 type_node->next = comdat_type_list;
8255 comdat_type_list = type_node;
8256
8257 /* Generate the type signature. */
8258 generate_type_signature (c, type_node);
8259
8260 /* Copy the declaration context, attributes, and children of the
8261 declaration into the new type unit DIE, then remove this DIE
8262 from the main CU (or replace it with a skeleton if necessary). */
8263 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8264 type_node->skeleton_die = replacement;
8265
8266 /* Add the DIE to the new compunit. */
8267 add_child_die (unit, c);
8268
8269 /* Types can reference DWARF procedures for type size or data location
8270 expressions. Calls in DWARF expressions cannot target procedures
8271 that are not in the same section. So we must copy DWARF procedures
8272 along with this type and then rewrite references to them. */
8273 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8274 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8275
8276 if (replacement != NULL)
8277 c = replacement;
8278 }
8279 else if (c->die_tag == DW_TAG_namespace
8280 || c->die_tag == DW_TAG_class_type
8281 || c->die_tag == DW_TAG_structure_type
8282 || c->die_tag == DW_TAG_union_type)
8283 {
8284 /* Look for nested types that can be broken out. */
8285 break_out_comdat_types (c);
8286 }
8287 } while (next != NULL);
8288 }
8289
8290 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8291 Enter all the cloned children into the hash table decl_table. */
8292
8293 static dw_die_ref
8294 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8295 {
8296 dw_die_ref c;
8297 dw_die_ref clone;
8298 struct decl_table_entry *entry;
8299 decl_table_entry **slot;
8300
8301 if (die->die_tag == DW_TAG_subprogram)
8302 clone = clone_as_declaration (die);
8303 else
8304 clone = clone_die (die);
8305
8306 slot = decl_table->find_slot_with_hash (die,
8307 htab_hash_pointer (die), INSERT);
8308
8309 /* Assert that DIE isn't in the hash table yet. If it would be there
8310 before, the ancestors would be necessarily there as well, therefore
8311 clone_tree_partial wouldn't be called. */
8312 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8313
8314 entry = XCNEW (struct decl_table_entry);
8315 entry->orig = die;
8316 entry->copy = clone;
8317 *slot = entry;
8318
8319 if (die->die_tag != DW_TAG_subprogram)
8320 FOR_EACH_CHILD (die, c,
8321 add_child_die (clone, clone_tree_partial (c, decl_table)));
8322
8323 return clone;
8324 }
8325
8326 /* Walk the DIE and its children, looking for references to incomplete
8327 or trivial types that are unmarked (i.e., that are not in the current
8328 type_unit). */
8329
8330 static void
8331 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8332 {
8333 dw_die_ref c;
8334 dw_attr_node *a;
8335 unsigned ix;
8336
8337 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8338 {
8339 if (AT_class (a) == dw_val_class_die_ref)
8340 {
8341 dw_die_ref targ = AT_ref (a);
8342 decl_table_entry **slot;
8343 struct decl_table_entry *entry;
8344
8345 if (targ->die_mark != 0 || targ->comdat_type_p)
8346 continue;
8347
8348 slot = decl_table->find_slot_with_hash (targ,
8349 htab_hash_pointer (targ),
8350 INSERT);
8351
8352 if (*slot != HTAB_EMPTY_ENTRY)
8353 {
8354 /* TARG has already been copied, so we just need to
8355 modify the reference to point to the copy. */
8356 entry = *slot;
8357 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8358 }
8359 else
8360 {
8361 dw_die_ref parent = unit;
8362 dw_die_ref copy = clone_die (targ);
8363
8364 /* Record in DECL_TABLE that TARG has been copied.
8365 Need to do this now, before the recursive call,
8366 because DECL_TABLE may be expanded and SLOT
8367 would no longer be a valid pointer. */
8368 entry = XCNEW (struct decl_table_entry);
8369 entry->orig = targ;
8370 entry->copy = copy;
8371 *slot = entry;
8372
8373 /* If TARG is not a declaration DIE, we need to copy its
8374 children. */
8375 if (!is_declaration_die (targ))
8376 {
8377 FOR_EACH_CHILD (
8378 targ, c,
8379 add_child_die (copy,
8380 clone_tree_partial (c, decl_table)));
8381 }
8382
8383 /* Make sure the cloned tree is marked as part of the
8384 type unit. */
8385 mark_dies (copy);
8386
8387 /* If TARG has surrounding context, copy its ancestor tree
8388 into the new type unit. */
8389 if (targ->die_parent != NULL
8390 && !is_unit_die (targ->die_parent))
8391 parent = copy_ancestor_tree (unit, targ->die_parent,
8392 decl_table);
8393
8394 add_child_die (parent, copy);
8395 a->dw_attr_val.v.val_die_ref.die = copy;
8396
8397 /* Make sure the newly-copied DIE is walked. If it was
8398 installed in a previously-added context, it won't
8399 get visited otherwise. */
8400 if (parent != unit)
8401 {
8402 /* Find the highest point of the newly-added tree,
8403 mark each node along the way, and walk from there. */
8404 parent->die_mark = 1;
8405 while (parent->die_parent
8406 && parent->die_parent->die_mark == 0)
8407 {
8408 parent = parent->die_parent;
8409 parent->die_mark = 1;
8410 }
8411 copy_decls_walk (unit, parent, decl_table);
8412 }
8413 }
8414 }
8415 }
8416
8417 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8418 }
8419
8420 /* Copy declarations for "unworthy" types into the new comdat section.
8421 Incomplete types, modified types, and certain other types aren't broken
8422 out into comdat sections of their own, so they don't have a signature,
8423 and we need to copy the declaration into the same section so that we
8424 don't have an external reference. */
8425
8426 static void
8427 copy_decls_for_unworthy_types (dw_die_ref unit)
8428 {
8429 mark_dies (unit);
8430 decl_hash_type decl_table (10);
8431 copy_decls_walk (unit, unit, &decl_table);
8432 unmark_dies (unit);
8433 }
8434
8435 /* Traverse the DIE and add a sibling attribute if it may have the
8436 effect of speeding up access to siblings. To save some space,
8437 avoid generating sibling attributes for DIE's without children. */
8438
8439 static void
8440 add_sibling_attributes (dw_die_ref die)
8441 {
8442 dw_die_ref c;
8443
8444 if (! die->die_child)
8445 return;
8446
8447 if (die->die_parent && die != die->die_parent->die_child)
8448 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8449
8450 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8451 }
8452
8453 /* Output all location lists for the DIE and its children. */
8454
8455 static void
8456 output_location_lists (dw_die_ref die)
8457 {
8458 dw_die_ref c;
8459 dw_attr_node *a;
8460 unsigned ix;
8461
8462 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8463 if (AT_class (a) == dw_val_class_loc_list)
8464 output_loc_list (AT_loc_list (a));
8465
8466 FOR_EACH_CHILD (die, c, output_location_lists (c));
8467 }
8468
8469 /* During assign_location_list_indexes and output_loclists_offset the
8470 current index, after it the number of assigned indexes (i.e. how
8471 large the .debug_loclists* offset table should be). */
8472 static unsigned int loc_list_idx;
8473
8474 /* Output all location list offsets for the DIE and its children. */
8475
8476 static void
8477 output_loclists_offsets (dw_die_ref die)
8478 {
8479 dw_die_ref c;
8480 dw_attr_node *a;
8481 unsigned ix;
8482
8483 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8484 if (AT_class (a) == dw_val_class_loc_list)
8485 {
8486 dw_loc_list_ref l = AT_loc_list (a);
8487 if (l->offset_emitted)
8488 continue;
8489 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8490 loc_section_label, NULL);
8491 gcc_assert (l->hash == loc_list_idx);
8492 loc_list_idx++;
8493 l->offset_emitted = true;
8494 }
8495
8496 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8497 }
8498
8499 /* Recursively set indexes of location lists. */
8500
8501 static void
8502 assign_location_list_indexes (dw_die_ref die)
8503 {
8504 dw_die_ref c;
8505 dw_attr_node *a;
8506 unsigned ix;
8507
8508 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8509 if (AT_class (a) == dw_val_class_loc_list)
8510 {
8511 dw_loc_list_ref list = AT_loc_list (a);
8512 if (!list->num_assigned)
8513 {
8514 list->num_assigned = true;
8515 list->hash = loc_list_idx++;
8516 }
8517 }
8518
8519 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8520 }
8521
8522 /* We want to limit the number of external references, because they are
8523 larger than local references: a relocation takes multiple words, and
8524 even a sig8 reference is always eight bytes, whereas a local reference
8525 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8526 So if we encounter multiple external references to the same type DIE, we
8527 make a local typedef stub for it and redirect all references there.
8528
8529 This is the element of the hash table for keeping track of these
8530 references. */
8531
8532 struct external_ref
8533 {
8534 dw_die_ref type;
8535 dw_die_ref stub;
8536 unsigned n_refs;
8537 };
8538
8539 /* Hashtable helpers. */
8540
8541 struct external_ref_hasher : free_ptr_hash <external_ref>
8542 {
8543 static inline hashval_t hash (const external_ref *);
8544 static inline bool equal (const external_ref *, const external_ref *);
8545 };
8546
8547 inline hashval_t
8548 external_ref_hasher::hash (const external_ref *r)
8549 {
8550 dw_die_ref die = r->type;
8551 hashval_t h = 0;
8552
8553 /* We can't use the address of the DIE for hashing, because
8554 that will make the order of the stub DIEs non-deterministic. */
8555 if (! die->comdat_type_p)
8556 /* We have a symbol; use it to compute a hash. */
8557 h = htab_hash_string (die->die_id.die_symbol);
8558 else
8559 {
8560 /* We have a type signature; use a subset of the bits as the hash.
8561 The 8-byte signature is at least as large as hashval_t. */
8562 comdat_type_node *type_node = die->die_id.die_type_node;
8563 memcpy (&h, type_node->signature, sizeof (h));
8564 }
8565 return h;
8566 }
8567
8568 inline bool
8569 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8570 {
8571 return r1->type == r2->type;
8572 }
8573
8574 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8575
8576 /* Return a pointer to the external_ref for references to DIE. */
8577
8578 static struct external_ref *
8579 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8580 {
8581 struct external_ref ref, *ref_p;
8582 external_ref **slot;
8583
8584 ref.type = die;
8585 slot = map->find_slot (&ref, INSERT);
8586 if (*slot != HTAB_EMPTY_ENTRY)
8587 return *slot;
8588
8589 ref_p = XCNEW (struct external_ref);
8590 ref_p->type = die;
8591 *slot = ref_p;
8592 return ref_p;
8593 }
8594
8595 /* Subroutine of optimize_external_refs, below.
8596
8597 If we see a type skeleton, record it as our stub. If we see external
8598 references, remember how many we've seen. */
8599
8600 static void
8601 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8602 {
8603 dw_die_ref c;
8604 dw_attr_node *a;
8605 unsigned ix;
8606 struct external_ref *ref_p;
8607
8608 if (is_type_die (die)
8609 && (c = get_AT_ref (die, DW_AT_signature)))
8610 {
8611 /* This is a local skeleton; use it for local references. */
8612 ref_p = lookup_external_ref (map, c);
8613 ref_p->stub = die;
8614 }
8615
8616 /* Scan the DIE references, and remember any that refer to DIEs from
8617 other CUs (i.e. those which are not marked). */
8618 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8619 if (AT_class (a) == dw_val_class_die_ref
8620 && (c = AT_ref (a))->die_mark == 0
8621 && is_type_die (c))
8622 {
8623 ref_p = lookup_external_ref (map, c);
8624 ref_p->n_refs++;
8625 }
8626
8627 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8628 }
8629
8630 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8631 points to an external_ref, DATA is the CU we're processing. If we don't
8632 already have a local stub, and we have multiple refs, build a stub. */
8633
8634 int
8635 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8636 {
8637 struct external_ref *ref_p = *slot;
8638
8639 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8640 {
8641 /* We have multiple references to this type, so build a small stub.
8642 Both of these forms are a bit dodgy from the perspective of the
8643 DWARF standard, since technically they should have names. */
8644 dw_die_ref cu = data;
8645 dw_die_ref type = ref_p->type;
8646 dw_die_ref stub = NULL;
8647
8648 if (type->comdat_type_p)
8649 {
8650 /* If we refer to this type via sig8, use AT_signature. */
8651 stub = new_die (type->die_tag, cu, NULL_TREE);
8652 add_AT_die_ref (stub, DW_AT_signature, type);
8653 }
8654 else
8655 {
8656 /* Otherwise, use a typedef with no name. */
8657 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8658 add_AT_die_ref (stub, DW_AT_type, type);
8659 }
8660
8661 stub->die_mark++;
8662 ref_p->stub = stub;
8663 }
8664 return 1;
8665 }
8666
8667 /* DIE is a unit; look through all the DIE references to see if there are
8668 any external references to types, and if so, create local stubs for
8669 them which will be applied in build_abbrev_table. This is useful because
8670 references to local DIEs are smaller. */
8671
8672 static external_ref_hash_type *
8673 optimize_external_refs (dw_die_ref die)
8674 {
8675 external_ref_hash_type *map = new external_ref_hash_type (10);
8676 optimize_external_refs_1 (die, map);
8677 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8678 return map;
8679 }
8680
8681 /* The following 3 variables are temporaries that are computed only during the
8682 build_abbrev_table call and used and released during the following
8683 optimize_abbrev_table call. */
8684
8685 /* First abbrev_id that can be optimized based on usage. */
8686 static unsigned int abbrev_opt_start;
8687
8688 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8689 abbrev_id smaller than this, because they must be already sized
8690 during build_abbrev_table). */
8691 static unsigned int abbrev_opt_base_type_end;
8692
8693 /* Vector of usage counts during build_abbrev_table. Indexed by
8694 abbrev_id - abbrev_opt_start. */
8695 static vec<unsigned int> abbrev_usage_count;
8696
8697 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8698 static vec<dw_die_ref> sorted_abbrev_dies;
8699
8700 /* The format of each DIE (and its attribute value pairs) is encoded in an
8701 abbreviation table. This routine builds the abbreviation table and assigns
8702 a unique abbreviation id for each abbreviation entry. The children of each
8703 die are visited recursively. */
8704
8705 static void
8706 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8707 {
8708 unsigned int abbrev_id = 0;
8709 dw_die_ref c;
8710 dw_attr_node *a;
8711 unsigned ix;
8712 dw_die_ref abbrev;
8713
8714 /* Scan the DIE references, and replace any that refer to
8715 DIEs from other CUs (i.e. those which are not marked) with
8716 the local stubs we built in optimize_external_refs. */
8717 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8718 if (AT_class (a) == dw_val_class_die_ref
8719 && (c = AT_ref (a))->die_mark == 0)
8720 {
8721 struct external_ref *ref_p;
8722 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8723
8724 ref_p = lookup_external_ref (extern_map, c);
8725 if (ref_p->stub && ref_p->stub != die)
8726 change_AT_die_ref (a, ref_p->stub);
8727 else
8728 /* We aren't changing this reference, so mark it external. */
8729 set_AT_ref_external (a, 1);
8730 }
8731
8732 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8733 {
8734 dw_attr_node *die_a, *abbrev_a;
8735 unsigned ix;
8736 bool ok = true;
8737
8738 if (abbrev_id == 0)
8739 continue;
8740 if (abbrev->die_tag != die->die_tag)
8741 continue;
8742 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8743 continue;
8744
8745 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8746 continue;
8747
8748 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8749 {
8750 abbrev_a = &(*abbrev->die_attr)[ix];
8751 if ((abbrev_a->dw_attr != die_a->dw_attr)
8752 || (value_format (abbrev_a) != value_format (die_a)))
8753 {
8754 ok = false;
8755 break;
8756 }
8757 }
8758 if (ok)
8759 break;
8760 }
8761
8762 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8763 {
8764 vec_safe_push (abbrev_die_table, die);
8765 if (abbrev_opt_start)
8766 abbrev_usage_count.safe_push (0);
8767 }
8768 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8769 {
8770 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8771 sorted_abbrev_dies.safe_push (die);
8772 }
8773
8774 die->die_abbrev = abbrev_id;
8775 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8776 }
8777
8778 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8779 by die_abbrev's usage count, from the most commonly used
8780 abbreviation to the least. */
8781
8782 static int
8783 die_abbrev_cmp (const void *p1, const void *p2)
8784 {
8785 dw_die_ref die1 = *(const dw_die_ref *) p1;
8786 dw_die_ref die2 = *(const dw_die_ref *) p2;
8787
8788 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8789 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8790
8791 if (die1->die_abbrev >= abbrev_opt_base_type_end
8792 && die2->die_abbrev >= abbrev_opt_base_type_end)
8793 {
8794 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8795 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8796 return -1;
8797 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8798 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8799 return 1;
8800 }
8801
8802 /* Stabilize the sort. */
8803 if (die1->die_abbrev < die2->die_abbrev)
8804 return -1;
8805 if (die1->die_abbrev > die2->die_abbrev)
8806 return 1;
8807
8808 return 0;
8809 }
8810
8811 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8812 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8813 into dw_val_class_const_implicit or
8814 dw_val_class_unsigned_const_implicit. */
8815
8816 static void
8817 optimize_implicit_const (unsigned int first_id, unsigned int end,
8818 vec<bool> &implicit_consts)
8819 {
8820 /* It never makes sense if there is just one DIE using the abbreviation. */
8821 if (end < first_id + 2)
8822 return;
8823
8824 dw_attr_node *a;
8825 unsigned ix, i;
8826 dw_die_ref die = sorted_abbrev_dies[first_id];
8827 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8828 if (implicit_consts[ix])
8829 {
8830 enum dw_val_class new_class = dw_val_class_none;
8831 switch (AT_class (a))
8832 {
8833 case dw_val_class_unsigned_const:
8834 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8835 continue;
8836
8837 /* The .debug_abbrev section will grow by
8838 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8839 in all the DIEs using that abbreviation. */
8840 if (constant_size (AT_unsigned (a)) * (end - first_id)
8841 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8842 continue;
8843
8844 new_class = dw_val_class_unsigned_const_implicit;
8845 break;
8846
8847 case dw_val_class_const:
8848 new_class = dw_val_class_const_implicit;
8849 break;
8850
8851 case dw_val_class_file:
8852 new_class = dw_val_class_file_implicit;
8853 break;
8854
8855 default:
8856 continue;
8857 }
8858 for (i = first_id; i < end; i++)
8859 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8860 = new_class;
8861 }
8862 }
8863
8864 /* Attempt to optimize abbreviation table from abbrev_opt_start
8865 abbreviation above. */
8866
8867 static void
8868 optimize_abbrev_table (void)
8869 {
8870 if (abbrev_opt_start
8871 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8872 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8873 {
8874 auto_vec<bool, 32> implicit_consts;
8875 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8876
8877 unsigned int abbrev_id = abbrev_opt_start - 1;
8878 unsigned int first_id = ~0U;
8879 unsigned int last_abbrev_id = 0;
8880 unsigned int i;
8881 dw_die_ref die;
8882 if (abbrev_opt_base_type_end > abbrev_opt_start)
8883 abbrev_id = abbrev_opt_base_type_end - 1;
8884 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8885 most commonly used abbreviations come first. */
8886 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8887 {
8888 dw_attr_node *a;
8889 unsigned ix;
8890
8891 /* If calc_base_type_die_sizes has been called, the CU and
8892 base types after it can't be optimized, because we've already
8893 calculated their DIE offsets. We've sorted them first. */
8894 if (die->die_abbrev < abbrev_opt_base_type_end)
8895 continue;
8896 if (die->die_abbrev != last_abbrev_id)
8897 {
8898 last_abbrev_id = die->die_abbrev;
8899 if (dwarf_version >= 5 && first_id != ~0U)
8900 optimize_implicit_const (first_id, i, implicit_consts);
8901 abbrev_id++;
8902 (*abbrev_die_table)[abbrev_id] = die;
8903 if (dwarf_version >= 5)
8904 {
8905 first_id = i;
8906 implicit_consts.truncate (0);
8907
8908 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8909 switch (AT_class (a))
8910 {
8911 case dw_val_class_const:
8912 case dw_val_class_unsigned_const:
8913 case dw_val_class_file:
8914 implicit_consts.safe_push (true);
8915 break;
8916 default:
8917 implicit_consts.safe_push (false);
8918 break;
8919 }
8920 }
8921 }
8922 else if (dwarf_version >= 5)
8923 {
8924 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8925 if (!implicit_consts[ix])
8926 continue;
8927 else
8928 {
8929 dw_attr_node *other_a
8930 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8931 if (!dw_val_equal_p (&a->dw_attr_val,
8932 &other_a->dw_attr_val))
8933 implicit_consts[ix] = false;
8934 }
8935 }
8936 die->die_abbrev = abbrev_id;
8937 }
8938 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8939 if (dwarf_version >= 5 && first_id != ~0U)
8940 optimize_implicit_const (first_id, i, implicit_consts);
8941 }
8942
8943 abbrev_opt_start = 0;
8944 abbrev_opt_base_type_end = 0;
8945 abbrev_usage_count.release ();
8946 sorted_abbrev_dies.release ();
8947 }
8948 \f
8949 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8950
8951 static int
8952 constant_size (unsigned HOST_WIDE_INT value)
8953 {
8954 int log;
8955
8956 if (value == 0)
8957 log = 0;
8958 else
8959 log = floor_log2 (value);
8960
8961 log = log / 8;
8962 log = 1 << (floor_log2 (log) + 1);
8963
8964 return log;
8965 }
8966
8967 /* Return the size of a DIE as it is represented in the
8968 .debug_info section. */
8969
8970 static unsigned long
8971 size_of_die (dw_die_ref die)
8972 {
8973 unsigned long size = 0;
8974 dw_attr_node *a;
8975 unsigned ix;
8976 enum dwarf_form form;
8977
8978 size += size_of_uleb128 (die->die_abbrev);
8979 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8980 {
8981 switch (AT_class (a))
8982 {
8983 case dw_val_class_addr:
8984 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8985 {
8986 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8987 size += size_of_uleb128 (AT_index (a));
8988 }
8989 else
8990 size += DWARF2_ADDR_SIZE;
8991 break;
8992 case dw_val_class_offset:
8993 size += DWARF_OFFSET_SIZE;
8994 break;
8995 case dw_val_class_loc:
8996 {
8997 unsigned long lsize = size_of_locs (AT_loc (a));
8998
8999 /* Block length. */
9000 if (dwarf_version >= 4)
9001 size += size_of_uleb128 (lsize);
9002 else
9003 size += constant_size (lsize);
9004 size += lsize;
9005 }
9006 break;
9007 case dw_val_class_loc_list:
9008 if (dwarf_split_debug_info && dwarf_version >= 5)
9009 {
9010 gcc_assert (AT_loc_list (a)->num_assigned);
9011 size += size_of_uleb128 (AT_loc_list (a)->hash);
9012 }
9013 else
9014 size += DWARF_OFFSET_SIZE;
9015 break;
9016 case dw_val_class_range_list:
9017 if (value_format (a) == DW_FORM_rnglistx)
9018 {
9019 gcc_assert (rnglist_idx);
9020 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9021 size += size_of_uleb128 (r->idx);
9022 }
9023 else
9024 size += DWARF_OFFSET_SIZE;
9025 break;
9026 case dw_val_class_const:
9027 size += size_of_sleb128 (AT_int (a));
9028 break;
9029 case dw_val_class_unsigned_const:
9030 {
9031 int csize = constant_size (AT_unsigned (a));
9032 if (dwarf_version == 3
9033 && a->dw_attr == DW_AT_data_member_location
9034 && csize >= 4)
9035 size += size_of_uleb128 (AT_unsigned (a));
9036 else
9037 size += csize;
9038 }
9039 break;
9040 case dw_val_class_const_implicit:
9041 case dw_val_class_unsigned_const_implicit:
9042 case dw_val_class_file_implicit:
9043 /* These occupy no size in the DIE, just an extra sleb128 in
9044 .debug_abbrev. */
9045 break;
9046 case dw_val_class_const_double:
9047 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9048 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9049 size++; /* block */
9050 break;
9051 case dw_val_class_wide_int:
9052 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9053 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9054 if (get_full_len (*a->dw_attr_val.v.val_wide)
9055 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9056 size++; /* block */
9057 break;
9058 case dw_val_class_vec:
9059 size += constant_size (a->dw_attr_val.v.val_vec.length
9060 * a->dw_attr_val.v.val_vec.elt_size)
9061 + a->dw_attr_val.v.val_vec.length
9062 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9063 break;
9064 case dw_val_class_flag:
9065 if (dwarf_version >= 4)
9066 /* Currently all add_AT_flag calls pass in 1 as last argument,
9067 so DW_FORM_flag_present can be used. If that ever changes,
9068 we'll need to use DW_FORM_flag and have some optimization
9069 in build_abbrev_table that will change those to
9070 DW_FORM_flag_present if it is set to 1 in all DIEs using
9071 the same abbrev entry. */
9072 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9073 else
9074 size += 1;
9075 break;
9076 case dw_val_class_die_ref:
9077 if (AT_ref_external (a))
9078 {
9079 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9080 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9081 is sized by target address length, whereas in DWARF3
9082 it's always sized as an offset. */
9083 if (use_debug_types)
9084 size += DWARF_TYPE_SIGNATURE_SIZE;
9085 else if (dwarf_version == 2)
9086 size += DWARF2_ADDR_SIZE;
9087 else
9088 size += DWARF_OFFSET_SIZE;
9089 }
9090 else
9091 size += DWARF_OFFSET_SIZE;
9092 break;
9093 case dw_val_class_fde_ref:
9094 size += DWARF_OFFSET_SIZE;
9095 break;
9096 case dw_val_class_lbl_id:
9097 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9098 {
9099 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9100 size += size_of_uleb128 (AT_index (a));
9101 }
9102 else
9103 size += DWARF2_ADDR_SIZE;
9104 break;
9105 case dw_val_class_lineptr:
9106 case dw_val_class_macptr:
9107 case dw_val_class_loclistsptr:
9108 size += DWARF_OFFSET_SIZE;
9109 break;
9110 case dw_val_class_str:
9111 form = AT_string_form (a);
9112 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9113 size += DWARF_OFFSET_SIZE;
9114 else if (form == DW_FORM_GNU_str_index)
9115 size += size_of_uleb128 (AT_index (a));
9116 else
9117 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9118 break;
9119 case dw_val_class_file:
9120 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9121 break;
9122 case dw_val_class_data8:
9123 size += 8;
9124 break;
9125 case dw_val_class_vms_delta:
9126 size += DWARF_OFFSET_SIZE;
9127 break;
9128 case dw_val_class_high_pc:
9129 size += DWARF2_ADDR_SIZE;
9130 break;
9131 case dw_val_class_discr_value:
9132 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9133 break;
9134 case dw_val_class_discr_list:
9135 {
9136 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9137
9138 /* This is a block, so we have the block length and then its
9139 data. */
9140 size += constant_size (block_size) + block_size;
9141 }
9142 break;
9143 default:
9144 gcc_unreachable ();
9145 }
9146 }
9147
9148 return size;
9149 }
9150
9151 /* Size the debugging information associated with a given DIE. Visits the
9152 DIE's children recursively. Updates the global variable next_die_offset, on
9153 each time through. Uses the current value of next_die_offset to update the
9154 die_offset field in each DIE. */
9155
9156 static void
9157 calc_die_sizes (dw_die_ref die)
9158 {
9159 dw_die_ref c;
9160
9161 gcc_assert (die->die_offset == 0
9162 || (unsigned long int) die->die_offset == next_die_offset);
9163 die->die_offset = next_die_offset;
9164 next_die_offset += size_of_die (die);
9165
9166 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9167
9168 if (die->die_child != NULL)
9169 /* Count the null byte used to terminate sibling lists. */
9170 next_die_offset += 1;
9171 }
9172
9173 /* Size just the base type children at the start of the CU.
9174 This is needed because build_abbrev needs to size locs
9175 and sizing of type based stack ops needs to know die_offset
9176 values for the base types. */
9177
9178 static void
9179 calc_base_type_die_sizes (void)
9180 {
9181 unsigned long die_offset = (dwarf_split_debug_info
9182 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9183 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9184 unsigned int i;
9185 dw_die_ref base_type;
9186 #if ENABLE_ASSERT_CHECKING
9187 dw_die_ref prev = comp_unit_die ()->die_child;
9188 #endif
9189
9190 die_offset += size_of_die (comp_unit_die ());
9191 for (i = 0; base_types.iterate (i, &base_type); i++)
9192 {
9193 #if ENABLE_ASSERT_CHECKING
9194 gcc_assert (base_type->die_offset == 0
9195 && prev->die_sib == base_type
9196 && base_type->die_child == NULL
9197 && base_type->die_abbrev);
9198 prev = base_type;
9199 #endif
9200 if (abbrev_opt_start
9201 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9202 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9203 base_type->die_offset = die_offset;
9204 die_offset += size_of_die (base_type);
9205 }
9206 }
9207
9208 /* Set the marks for a die and its children. We do this so
9209 that we know whether or not a reference needs to use FORM_ref_addr; only
9210 DIEs in the same CU will be marked. We used to clear out the offset
9211 and use that as the flag, but ran into ordering problems. */
9212
9213 static void
9214 mark_dies (dw_die_ref die)
9215 {
9216 dw_die_ref c;
9217
9218 gcc_assert (!die->die_mark);
9219
9220 die->die_mark = 1;
9221 FOR_EACH_CHILD (die, c, mark_dies (c));
9222 }
9223
9224 /* Clear the marks for a die and its children. */
9225
9226 static void
9227 unmark_dies (dw_die_ref die)
9228 {
9229 dw_die_ref c;
9230
9231 if (! use_debug_types)
9232 gcc_assert (die->die_mark);
9233
9234 die->die_mark = 0;
9235 FOR_EACH_CHILD (die, c, unmark_dies (c));
9236 }
9237
9238 /* Clear the marks for a die, its children and referred dies. */
9239
9240 static void
9241 unmark_all_dies (dw_die_ref die)
9242 {
9243 dw_die_ref c;
9244 dw_attr_node *a;
9245 unsigned ix;
9246
9247 if (!die->die_mark)
9248 return;
9249 die->die_mark = 0;
9250
9251 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9252
9253 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9254 if (AT_class (a) == dw_val_class_die_ref)
9255 unmark_all_dies (AT_ref (a));
9256 }
9257
9258 /* Calculate if the entry should appear in the final output file. It may be
9259 from a pruned a type. */
9260
9261 static bool
9262 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9263 {
9264 /* By limiting gnu pubnames to definitions only, gold can generate a
9265 gdb index without entries for declarations, which don't include
9266 enough information to be useful. */
9267 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9268 return false;
9269
9270 if (table == pubname_table)
9271 {
9272 /* Enumerator names are part of the pubname table, but the
9273 parent DW_TAG_enumeration_type die may have been pruned.
9274 Don't output them if that is the case. */
9275 if (p->die->die_tag == DW_TAG_enumerator &&
9276 (p->die->die_parent == NULL
9277 || !p->die->die_parent->die_perennial_p))
9278 return false;
9279
9280 /* Everything else in the pubname table is included. */
9281 return true;
9282 }
9283
9284 /* The pubtypes table shouldn't include types that have been
9285 pruned. */
9286 return (p->die->die_offset != 0
9287 || !flag_eliminate_unused_debug_types);
9288 }
9289
9290 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9291 generated for the compilation unit. */
9292
9293 static unsigned long
9294 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9295 {
9296 unsigned long size;
9297 unsigned i;
9298 pubname_entry *p;
9299 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9300
9301 size = DWARF_PUBNAMES_HEADER_SIZE;
9302 FOR_EACH_VEC_ELT (*names, i, p)
9303 if (include_pubname_in_output (names, p))
9304 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9305
9306 size += DWARF_OFFSET_SIZE;
9307 return size;
9308 }
9309
9310 /* Return the size of the information in the .debug_aranges section. */
9311
9312 static unsigned long
9313 size_of_aranges (void)
9314 {
9315 unsigned long size;
9316
9317 size = DWARF_ARANGES_HEADER_SIZE;
9318
9319 /* Count the address/length pair for this compilation unit. */
9320 if (text_section_used)
9321 size += 2 * DWARF2_ADDR_SIZE;
9322 if (cold_text_section_used)
9323 size += 2 * DWARF2_ADDR_SIZE;
9324 if (have_multiple_function_sections)
9325 {
9326 unsigned fde_idx;
9327 dw_fde_ref fde;
9328
9329 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9330 {
9331 if (DECL_IGNORED_P (fde->decl))
9332 continue;
9333 if (!fde->in_std_section)
9334 size += 2 * DWARF2_ADDR_SIZE;
9335 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9336 size += 2 * DWARF2_ADDR_SIZE;
9337 }
9338 }
9339
9340 /* Count the two zero words used to terminated the address range table. */
9341 size += 2 * DWARF2_ADDR_SIZE;
9342 return size;
9343 }
9344 \f
9345 /* Select the encoding of an attribute value. */
9346
9347 static enum dwarf_form
9348 value_format (dw_attr_node *a)
9349 {
9350 switch (AT_class (a))
9351 {
9352 case dw_val_class_addr:
9353 /* Only very few attributes allow DW_FORM_addr. */
9354 switch (a->dw_attr)
9355 {
9356 case DW_AT_low_pc:
9357 case DW_AT_high_pc:
9358 case DW_AT_entry_pc:
9359 case DW_AT_trampoline:
9360 return (AT_index (a) == NOT_INDEXED
9361 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9362 default:
9363 break;
9364 }
9365 switch (DWARF2_ADDR_SIZE)
9366 {
9367 case 1:
9368 return DW_FORM_data1;
9369 case 2:
9370 return DW_FORM_data2;
9371 case 4:
9372 return DW_FORM_data4;
9373 case 8:
9374 return DW_FORM_data8;
9375 default:
9376 gcc_unreachable ();
9377 }
9378 case dw_val_class_loc_list:
9379 if (dwarf_split_debug_info
9380 && dwarf_version >= 5
9381 && AT_loc_list (a)->num_assigned)
9382 return DW_FORM_loclistx;
9383 /* FALLTHRU */
9384 case dw_val_class_range_list:
9385 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9386 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9387 care about sizes of .debug* sections in shared libraries and
9388 executables and don't take into account relocations that affect just
9389 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9390 table in the .debug_rnglists section. */
9391 if (dwarf_split_debug_info
9392 && dwarf_version >= 5
9393 && AT_class (a) == dw_val_class_range_list
9394 && rnglist_idx
9395 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9396 return DW_FORM_rnglistx;
9397 if (dwarf_version >= 4)
9398 return DW_FORM_sec_offset;
9399 /* FALLTHRU */
9400 case dw_val_class_vms_delta:
9401 case dw_val_class_offset:
9402 switch (DWARF_OFFSET_SIZE)
9403 {
9404 case 4:
9405 return DW_FORM_data4;
9406 case 8:
9407 return DW_FORM_data8;
9408 default:
9409 gcc_unreachable ();
9410 }
9411 case dw_val_class_loc:
9412 if (dwarf_version >= 4)
9413 return DW_FORM_exprloc;
9414 switch (constant_size (size_of_locs (AT_loc (a))))
9415 {
9416 case 1:
9417 return DW_FORM_block1;
9418 case 2:
9419 return DW_FORM_block2;
9420 case 4:
9421 return DW_FORM_block4;
9422 default:
9423 gcc_unreachable ();
9424 }
9425 case dw_val_class_const:
9426 return DW_FORM_sdata;
9427 case dw_val_class_unsigned_const:
9428 switch (constant_size (AT_unsigned (a)))
9429 {
9430 case 1:
9431 return DW_FORM_data1;
9432 case 2:
9433 return DW_FORM_data2;
9434 case 4:
9435 /* In DWARF3 DW_AT_data_member_location with
9436 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9437 constant, so we need to use DW_FORM_udata if we need
9438 a large constant. */
9439 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9440 return DW_FORM_udata;
9441 return DW_FORM_data4;
9442 case 8:
9443 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9444 return DW_FORM_udata;
9445 return DW_FORM_data8;
9446 default:
9447 gcc_unreachable ();
9448 }
9449 case dw_val_class_const_implicit:
9450 case dw_val_class_unsigned_const_implicit:
9451 case dw_val_class_file_implicit:
9452 return DW_FORM_implicit_const;
9453 case dw_val_class_const_double:
9454 switch (HOST_BITS_PER_WIDE_INT)
9455 {
9456 case 8:
9457 return DW_FORM_data2;
9458 case 16:
9459 return DW_FORM_data4;
9460 case 32:
9461 return DW_FORM_data8;
9462 case 64:
9463 if (dwarf_version >= 5)
9464 return DW_FORM_data16;
9465 /* FALLTHRU */
9466 default:
9467 return DW_FORM_block1;
9468 }
9469 case dw_val_class_wide_int:
9470 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9471 {
9472 case 8:
9473 return DW_FORM_data1;
9474 case 16:
9475 return DW_FORM_data2;
9476 case 32:
9477 return DW_FORM_data4;
9478 case 64:
9479 return DW_FORM_data8;
9480 case 128:
9481 if (dwarf_version >= 5)
9482 return DW_FORM_data16;
9483 /* FALLTHRU */
9484 default:
9485 return DW_FORM_block1;
9486 }
9487 case dw_val_class_vec:
9488 switch (constant_size (a->dw_attr_val.v.val_vec.length
9489 * a->dw_attr_val.v.val_vec.elt_size))
9490 {
9491 case 1:
9492 return DW_FORM_block1;
9493 case 2:
9494 return DW_FORM_block2;
9495 case 4:
9496 return DW_FORM_block4;
9497 default:
9498 gcc_unreachable ();
9499 }
9500 case dw_val_class_flag:
9501 if (dwarf_version >= 4)
9502 {
9503 /* Currently all add_AT_flag calls pass in 1 as last argument,
9504 so DW_FORM_flag_present can be used. If that ever changes,
9505 we'll need to use DW_FORM_flag and have some optimization
9506 in build_abbrev_table that will change those to
9507 DW_FORM_flag_present if it is set to 1 in all DIEs using
9508 the same abbrev entry. */
9509 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9510 return DW_FORM_flag_present;
9511 }
9512 return DW_FORM_flag;
9513 case dw_val_class_die_ref:
9514 if (AT_ref_external (a))
9515 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9516 else
9517 return DW_FORM_ref;
9518 case dw_val_class_fde_ref:
9519 return DW_FORM_data;
9520 case dw_val_class_lbl_id:
9521 return (AT_index (a) == NOT_INDEXED
9522 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9523 case dw_val_class_lineptr:
9524 case dw_val_class_macptr:
9525 case dw_val_class_loclistsptr:
9526 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9527 case dw_val_class_str:
9528 return AT_string_form (a);
9529 case dw_val_class_file:
9530 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9531 {
9532 case 1:
9533 return DW_FORM_data1;
9534 case 2:
9535 return DW_FORM_data2;
9536 case 4:
9537 return DW_FORM_data4;
9538 default:
9539 gcc_unreachable ();
9540 }
9541
9542 case dw_val_class_data8:
9543 return DW_FORM_data8;
9544
9545 case dw_val_class_high_pc:
9546 switch (DWARF2_ADDR_SIZE)
9547 {
9548 case 1:
9549 return DW_FORM_data1;
9550 case 2:
9551 return DW_FORM_data2;
9552 case 4:
9553 return DW_FORM_data4;
9554 case 8:
9555 return DW_FORM_data8;
9556 default:
9557 gcc_unreachable ();
9558 }
9559
9560 case dw_val_class_discr_value:
9561 return (a->dw_attr_val.v.val_discr_value.pos
9562 ? DW_FORM_udata
9563 : DW_FORM_sdata);
9564 case dw_val_class_discr_list:
9565 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9566 {
9567 case 1:
9568 return DW_FORM_block1;
9569 case 2:
9570 return DW_FORM_block2;
9571 case 4:
9572 return DW_FORM_block4;
9573 default:
9574 gcc_unreachable ();
9575 }
9576
9577 default:
9578 gcc_unreachable ();
9579 }
9580 }
9581
9582 /* Output the encoding of an attribute value. */
9583
9584 static void
9585 output_value_format (dw_attr_node *a)
9586 {
9587 enum dwarf_form form = value_format (a);
9588
9589 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9590 }
9591
9592 /* Given a die and id, produce the appropriate abbreviations. */
9593
9594 static void
9595 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9596 {
9597 unsigned ix;
9598 dw_attr_node *a_attr;
9599
9600 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9601 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9602 dwarf_tag_name (abbrev->die_tag));
9603
9604 if (abbrev->die_child != NULL)
9605 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9606 else
9607 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9608
9609 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9610 {
9611 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9612 dwarf_attr_name (a_attr->dw_attr));
9613 output_value_format (a_attr);
9614 if (value_format (a_attr) == DW_FORM_implicit_const)
9615 {
9616 if (AT_class (a_attr) == dw_val_class_file_implicit)
9617 {
9618 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9619 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9620 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9621 }
9622 else
9623 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9624 }
9625 }
9626
9627 dw2_asm_output_data (1, 0, NULL);
9628 dw2_asm_output_data (1, 0, NULL);
9629 }
9630
9631
9632 /* Output the .debug_abbrev section which defines the DIE abbreviation
9633 table. */
9634
9635 static void
9636 output_abbrev_section (void)
9637 {
9638 unsigned int abbrev_id;
9639 dw_die_ref abbrev;
9640
9641 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9642 if (abbrev_id != 0)
9643 output_die_abbrevs (abbrev_id, abbrev);
9644
9645 /* Terminate the table. */
9646 dw2_asm_output_data (1, 0, NULL);
9647 }
9648
9649 /* Return a new location list, given the begin and end range, and the
9650 expression. */
9651
9652 static inline dw_loc_list_ref
9653 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9654 const char *section)
9655 {
9656 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9657
9658 retlist->begin = begin;
9659 retlist->begin_entry = NULL;
9660 retlist->end = end;
9661 retlist->expr = expr;
9662 retlist->section = section;
9663
9664 return retlist;
9665 }
9666
9667 /* Generate a new internal symbol for this location list node, if it
9668 hasn't got one yet. */
9669
9670 static inline void
9671 gen_llsym (dw_loc_list_ref list)
9672 {
9673 gcc_assert (!list->ll_symbol);
9674 list->ll_symbol = gen_internal_sym ("LLST");
9675 }
9676
9677 /* Output the location list given to us. */
9678
9679 static void
9680 output_loc_list (dw_loc_list_ref list_head)
9681 {
9682 if (list_head->emitted)
9683 return;
9684 list_head->emitted = true;
9685
9686 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9687
9688 dw_loc_list_ref curr = list_head;
9689 const char *last_section = NULL;
9690 const char *base_label = NULL;
9691
9692 /* Walk the location list, and output each range + expression. */
9693 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9694 {
9695 unsigned long size;
9696 /* Don't output an entry that starts and ends at the same address. */
9697 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9698 continue;
9699 size = size_of_locs (curr->expr);
9700 /* If the expression is too large, drop it on the floor. We could
9701 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9702 in the expression, but >= 64KB expressions for a single value
9703 in a single range are unlikely very useful. */
9704 if (dwarf_version < 5 && size > 0xffff)
9705 continue;
9706 if (dwarf_version >= 5)
9707 {
9708 if (dwarf_split_debug_info)
9709 {
9710 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9711 uleb128 index into .debug_addr and uleb128 length. */
9712 dw2_asm_output_data (1, DW_LLE_startx_length,
9713 "DW_LLE_startx_length (%s)",
9714 list_head->ll_symbol);
9715 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9716 "Location list range start index "
9717 "(%s)", curr->begin);
9718 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9719 For that case we probably need to emit DW_LLE_startx_endx,
9720 but we'd need 2 .debug_addr entries rather than just one. */
9721 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9722 "Location list length (%s)",
9723 list_head->ll_symbol);
9724 }
9725 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9726 {
9727 /* If all code is in .text section, the base address is
9728 already provided by the CU attributes. Use
9729 DW_LLE_offset_pair where both addresses are uleb128 encoded
9730 offsets against that base. */
9731 dw2_asm_output_data (1, DW_LLE_offset_pair,
9732 "DW_LLE_offset_pair (%s)",
9733 list_head->ll_symbol);
9734 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9735 "Location list begin address (%s)",
9736 list_head->ll_symbol);
9737 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9738 "Location list end address (%s)",
9739 list_head->ll_symbol);
9740 }
9741 else if (HAVE_AS_LEB128)
9742 {
9743 /* Otherwise, find out how many consecutive entries could share
9744 the same base entry. If just one, emit DW_LLE_start_length,
9745 otherwise emit DW_LLE_base_address for the base address
9746 followed by a series of DW_LLE_offset_pair. */
9747 if (last_section == NULL || curr->section != last_section)
9748 {
9749 dw_loc_list_ref curr2;
9750 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9751 curr2 = curr2->dw_loc_next)
9752 {
9753 if (strcmp (curr2->begin, curr2->end) == 0
9754 && !curr2->force)
9755 continue;
9756 break;
9757 }
9758 if (curr2 == NULL || curr->section != curr2->section)
9759 last_section = NULL;
9760 else
9761 {
9762 last_section = curr->section;
9763 base_label = curr->begin;
9764 dw2_asm_output_data (1, DW_LLE_base_address,
9765 "DW_LLE_base_address (%s)",
9766 list_head->ll_symbol);
9767 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9768 "Base address (%s)",
9769 list_head->ll_symbol);
9770 }
9771 }
9772 /* Only one entry with the same base address. Use
9773 DW_LLE_start_length with absolute address and uleb128
9774 length. */
9775 if (last_section == NULL)
9776 {
9777 dw2_asm_output_data (1, DW_LLE_start_length,
9778 "DW_LLE_start_length (%s)",
9779 list_head->ll_symbol);
9780 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9781 "Location list begin address (%s)",
9782 list_head->ll_symbol);
9783 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9784 "Location list length "
9785 "(%s)", list_head->ll_symbol);
9786 }
9787 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9788 DW_LLE_base_address. */
9789 else
9790 {
9791 dw2_asm_output_data (1, DW_LLE_offset_pair,
9792 "DW_LLE_offset_pair (%s)",
9793 list_head->ll_symbol);
9794 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9795 "Location list begin address "
9796 "(%s)", list_head->ll_symbol);
9797 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9798 "Location list end address "
9799 "(%s)", list_head->ll_symbol);
9800 }
9801 }
9802 /* The assembler does not support .uleb128 directive. Emit
9803 DW_LLE_start_end with a pair of absolute addresses. */
9804 else
9805 {
9806 dw2_asm_output_data (1, DW_LLE_start_end,
9807 "DW_LLE_start_end (%s)",
9808 list_head->ll_symbol);
9809 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9810 "Location list begin address (%s)",
9811 list_head->ll_symbol);
9812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9813 "Location list end address (%s)",
9814 list_head->ll_symbol);
9815 }
9816 }
9817 else if (dwarf_split_debug_info)
9818 {
9819 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9820 and 4 byte length. */
9821 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9822 "Location list start/length entry (%s)",
9823 list_head->ll_symbol);
9824 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9825 "Location list range start index (%s)",
9826 curr->begin);
9827 /* The length field is 4 bytes. If we ever need to support
9828 an 8-byte length, we can add a new DW_LLE code or fall back
9829 to DW_LLE_GNU_start_end_entry. */
9830 dw2_asm_output_delta (4, curr->end, curr->begin,
9831 "Location list range length (%s)",
9832 list_head->ll_symbol);
9833 }
9834 else if (!have_multiple_function_sections)
9835 {
9836 /* Pair of relative addresses against start of text section. */
9837 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9838 "Location list begin address (%s)",
9839 list_head->ll_symbol);
9840 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9841 "Location list end address (%s)",
9842 list_head->ll_symbol);
9843 }
9844 else
9845 {
9846 /* Pair of absolute addresses. */
9847 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9848 "Location list begin address (%s)",
9849 list_head->ll_symbol);
9850 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9851 "Location list end address (%s)",
9852 list_head->ll_symbol);
9853 }
9854
9855 /* Output the block length for this list of location operations. */
9856 if (dwarf_version >= 5)
9857 dw2_asm_output_data_uleb128 (size, "Location expression size");
9858 else
9859 {
9860 gcc_assert (size <= 0xffff);
9861 dw2_asm_output_data (2, size, "Location expression size");
9862 }
9863
9864 output_loc_sequence (curr->expr, -1);
9865 }
9866
9867 /* And finally list termination. */
9868 if (dwarf_version >= 5)
9869 dw2_asm_output_data (1, DW_LLE_end_of_list,
9870 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9871 else if (dwarf_split_debug_info)
9872 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9873 "Location list terminator (%s)",
9874 list_head->ll_symbol);
9875 else
9876 {
9877 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9878 "Location list terminator begin (%s)",
9879 list_head->ll_symbol);
9880 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9881 "Location list terminator end (%s)",
9882 list_head->ll_symbol);
9883 }
9884 }
9885
9886 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9887 section. Emit a relocated reference if val_entry is NULL, otherwise,
9888 emit an indirect reference. */
9889
9890 static void
9891 output_range_list_offset (dw_attr_node *a)
9892 {
9893 const char *name = dwarf_attr_name (a->dw_attr);
9894
9895 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9896 {
9897 if (dwarf_version >= 5)
9898 {
9899 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9900 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9901 debug_ranges_section, "%s", name);
9902 }
9903 else
9904 {
9905 char *p = strchr (ranges_section_label, '\0');
9906 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9907 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9908 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9909 debug_ranges_section, "%s", name);
9910 *p = '\0';
9911 }
9912 }
9913 else if (dwarf_version >= 5)
9914 {
9915 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9916 gcc_assert (rnglist_idx);
9917 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9918 }
9919 else
9920 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9921 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9922 "%s (offset from %s)", name, ranges_section_label);
9923 }
9924
9925 /* Output the offset into the debug_loc section. */
9926
9927 static void
9928 output_loc_list_offset (dw_attr_node *a)
9929 {
9930 char *sym = AT_loc_list (a)->ll_symbol;
9931
9932 gcc_assert (sym);
9933 if (!dwarf_split_debug_info)
9934 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9935 "%s", dwarf_attr_name (a->dw_attr));
9936 else if (dwarf_version >= 5)
9937 {
9938 gcc_assert (AT_loc_list (a)->num_assigned);
9939 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9940 dwarf_attr_name (a->dw_attr),
9941 sym);
9942 }
9943 else
9944 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9945 "%s", dwarf_attr_name (a->dw_attr));
9946 }
9947
9948 /* Output an attribute's index or value appropriately. */
9949
9950 static void
9951 output_attr_index_or_value (dw_attr_node *a)
9952 {
9953 const char *name = dwarf_attr_name (a->dw_attr);
9954
9955 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9956 {
9957 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9958 return;
9959 }
9960 switch (AT_class (a))
9961 {
9962 case dw_val_class_addr:
9963 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9964 break;
9965 case dw_val_class_high_pc:
9966 case dw_val_class_lbl_id:
9967 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9968 break;
9969 default:
9970 gcc_unreachable ();
9971 }
9972 }
9973
9974 /* Output a type signature. */
9975
9976 static inline void
9977 output_signature (const char *sig, const char *name)
9978 {
9979 int i;
9980
9981 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9982 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9983 }
9984
9985 /* Output a discriminant value. */
9986
9987 static inline void
9988 output_discr_value (dw_discr_value *discr_value, const char *name)
9989 {
9990 if (discr_value->pos)
9991 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9992 else
9993 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9994 }
9995
9996 /* Output the DIE and its attributes. Called recursively to generate
9997 the definitions of each child DIE. */
9998
9999 static void
10000 output_die (dw_die_ref die)
10001 {
10002 dw_attr_node *a;
10003 dw_die_ref c;
10004 unsigned long size;
10005 unsigned ix;
10006
10007 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10008 (unsigned long)die->die_offset,
10009 dwarf_tag_name (die->die_tag));
10010
10011 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10012 {
10013 const char *name = dwarf_attr_name (a->dw_attr);
10014
10015 switch (AT_class (a))
10016 {
10017 case dw_val_class_addr:
10018 output_attr_index_or_value (a);
10019 break;
10020
10021 case dw_val_class_offset:
10022 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10023 "%s", name);
10024 break;
10025
10026 case dw_val_class_range_list:
10027 output_range_list_offset (a);
10028 break;
10029
10030 case dw_val_class_loc:
10031 size = size_of_locs (AT_loc (a));
10032
10033 /* Output the block length for this list of location operations. */
10034 if (dwarf_version >= 4)
10035 dw2_asm_output_data_uleb128 (size, "%s", name);
10036 else
10037 dw2_asm_output_data (constant_size (size), size, "%s", name);
10038
10039 output_loc_sequence (AT_loc (a), -1);
10040 break;
10041
10042 case dw_val_class_const:
10043 /* ??? It would be slightly more efficient to use a scheme like is
10044 used for unsigned constants below, but gdb 4.x does not sign
10045 extend. Gdb 5.x does sign extend. */
10046 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10047 break;
10048
10049 case dw_val_class_unsigned_const:
10050 {
10051 int csize = constant_size (AT_unsigned (a));
10052 if (dwarf_version == 3
10053 && a->dw_attr == DW_AT_data_member_location
10054 && csize >= 4)
10055 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10056 else
10057 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10058 }
10059 break;
10060
10061 case dw_val_class_const_implicit:
10062 if (flag_debug_asm)
10063 fprintf (asm_out_file, "\t\t\t%s %s ("
10064 HOST_WIDE_INT_PRINT_DEC ")\n",
10065 ASM_COMMENT_START, name, AT_int (a));
10066 break;
10067
10068 case dw_val_class_unsigned_const_implicit:
10069 if (flag_debug_asm)
10070 fprintf (asm_out_file, "\t\t\t%s %s ("
10071 HOST_WIDE_INT_PRINT_HEX ")\n",
10072 ASM_COMMENT_START, name, AT_unsigned (a));
10073 break;
10074
10075 case dw_val_class_const_double:
10076 {
10077 unsigned HOST_WIDE_INT first, second;
10078
10079 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10080 dw2_asm_output_data (1,
10081 HOST_BITS_PER_DOUBLE_INT
10082 / HOST_BITS_PER_CHAR,
10083 NULL);
10084
10085 if (WORDS_BIG_ENDIAN)
10086 {
10087 first = a->dw_attr_val.v.val_double.high;
10088 second = a->dw_attr_val.v.val_double.low;
10089 }
10090 else
10091 {
10092 first = a->dw_attr_val.v.val_double.low;
10093 second = a->dw_attr_val.v.val_double.high;
10094 }
10095
10096 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10097 first, "%s", name);
10098 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10099 second, NULL);
10100 }
10101 break;
10102
10103 case dw_val_class_wide_int:
10104 {
10105 int i;
10106 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10107 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10108 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10109 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10110 * l, NULL);
10111
10112 if (WORDS_BIG_ENDIAN)
10113 for (i = len - 1; i >= 0; --i)
10114 {
10115 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10116 "%s", name);
10117 name = "";
10118 }
10119 else
10120 for (i = 0; i < len; ++i)
10121 {
10122 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10123 "%s", name);
10124 name = "";
10125 }
10126 }
10127 break;
10128
10129 case dw_val_class_vec:
10130 {
10131 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10132 unsigned int len = a->dw_attr_val.v.val_vec.length;
10133 unsigned int i;
10134 unsigned char *p;
10135
10136 dw2_asm_output_data (constant_size (len * elt_size),
10137 len * elt_size, "%s", name);
10138 if (elt_size > sizeof (HOST_WIDE_INT))
10139 {
10140 elt_size /= 2;
10141 len *= 2;
10142 }
10143 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10144 i < len;
10145 i++, p += elt_size)
10146 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10147 "fp or vector constant word %u", i);
10148 break;
10149 }
10150
10151 case dw_val_class_flag:
10152 if (dwarf_version >= 4)
10153 {
10154 /* Currently all add_AT_flag calls pass in 1 as last argument,
10155 so DW_FORM_flag_present can be used. If that ever changes,
10156 we'll need to use DW_FORM_flag and have some optimization
10157 in build_abbrev_table that will change those to
10158 DW_FORM_flag_present if it is set to 1 in all DIEs using
10159 the same abbrev entry. */
10160 gcc_assert (AT_flag (a) == 1);
10161 if (flag_debug_asm)
10162 fprintf (asm_out_file, "\t\t\t%s %s\n",
10163 ASM_COMMENT_START, name);
10164 break;
10165 }
10166 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10167 break;
10168
10169 case dw_val_class_loc_list:
10170 output_loc_list_offset (a);
10171 break;
10172
10173 case dw_val_class_die_ref:
10174 if (AT_ref_external (a))
10175 {
10176 if (AT_ref (a)->comdat_type_p)
10177 {
10178 comdat_type_node *type_node
10179 = AT_ref (a)->die_id.die_type_node;
10180
10181 gcc_assert (type_node);
10182 output_signature (type_node->signature, name);
10183 }
10184 else
10185 {
10186 const char *sym = AT_ref (a)->die_id.die_symbol;
10187 int size;
10188
10189 gcc_assert (sym);
10190 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10191 length, whereas in DWARF3 it's always sized as an
10192 offset. */
10193 if (dwarf_version == 2)
10194 size = DWARF2_ADDR_SIZE;
10195 else
10196 size = DWARF_OFFSET_SIZE;
10197 /* ??? We cannot unconditionally output die_offset if
10198 non-zero - others might create references to those
10199 DIEs via symbols.
10200 And we do not clear its DIE offset after outputting it
10201 (and the label refers to the actual DIEs, not the
10202 DWARF CU unit header which is when using label + offset
10203 would be the correct thing to do).
10204 ??? This is the reason for the with_offset flag. */
10205 if (AT_ref (a)->with_offset)
10206 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10207 debug_info_section, "%s", name);
10208 else
10209 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10210 name);
10211 }
10212 }
10213 else
10214 {
10215 gcc_assert (AT_ref (a)->die_offset);
10216 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10217 "%s", name);
10218 }
10219 break;
10220
10221 case dw_val_class_fde_ref:
10222 {
10223 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10224
10225 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10226 a->dw_attr_val.v.val_fde_index * 2);
10227 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10228 "%s", name);
10229 }
10230 break;
10231
10232 case dw_val_class_vms_delta:
10233 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10234 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10235 AT_vms_delta2 (a), AT_vms_delta1 (a),
10236 "%s", name);
10237 #else
10238 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10239 AT_vms_delta2 (a), AT_vms_delta1 (a),
10240 "%s", name);
10241 #endif
10242 break;
10243
10244 case dw_val_class_lbl_id:
10245 output_attr_index_or_value (a);
10246 break;
10247
10248 case dw_val_class_lineptr:
10249 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10250 debug_line_section, "%s", name);
10251 break;
10252
10253 case dw_val_class_macptr:
10254 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10255 debug_macinfo_section, "%s", name);
10256 break;
10257
10258 case dw_val_class_loclistsptr:
10259 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10260 debug_loc_section, "%s", name);
10261 break;
10262
10263 case dw_val_class_str:
10264 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10265 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10266 a->dw_attr_val.v.val_str->label,
10267 debug_str_section,
10268 "%s: \"%s\"", name, AT_string (a));
10269 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10270 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10271 a->dw_attr_val.v.val_str->label,
10272 debug_line_str_section,
10273 "%s: \"%s\"", name, AT_string (a));
10274 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10275 dw2_asm_output_data_uleb128 (AT_index (a),
10276 "%s: \"%s\"", name, AT_string (a));
10277 else
10278 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10279 break;
10280
10281 case dw_val_class_file:
10282 {
10283 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10284
10285 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10286 a->dw_attr_val.v.val_file->filename);
10287 break;
10288 }
10289
10290 case dw_val_class_file_implicit:
10291 if (flag_debug_asm)
10292 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10293 ASM_COMMENT_START, name,
10294 maybe_emit_file (a->dw_attr_val.v.val_file),
10295 a->dw_attr_val.v.val_file->filename);
10296 break;
10297
10298 case dw_val_class_data8:
10299 {
10300 int i;
10301
10302 for (i = 0; i < 8; i++)
10303 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10304 i == 0 ? "%s" : NULL, name);
10305 break;
10306 }
10307
10308 case dw_val_class_high_pc:
10309 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10310 get_AT_low_pc (die), "DW_AT_high_pc");
10311 break;
10312
10313 case dw_val_class_discr_value:
10314 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10315 break;
10316
10317 case dw_val_class_discr_list:
10318 {
10319 dw_discr_list_ref list = AT_discr_list (a);
10320 const int size = size_of_discr_list (list);
10321
10322 /* This is a block, so output its length first. */
10323 dw2_asm_output_data (constant_size (size), size,
10324 "%s: block size", name);
10325
10326 for (; list != NULL; list = list->dw_discr_next)
10327 {
10328 /* One byte for the discriminant value descriptor, and then as
10329 many LEB128 numbers as required. */
10330 if (list->dw_discr_range)
10331 dw2_asm_output_data (1, DW_DSC_range,
10332 "%s: DW_DSC_range", name);
10333 else
10334 dw2_asm_output_data (1, DW_DSC_label,
10335 "%s: DW_DSC_label", name);
10336
10337 output_discr_value (&list->dw_discr_lower_bound, name);
10338 if (list->dw_discr_range)
10339 output_discr_value (&list->dw_discr_upper_bound, name);
10340 }
10341 break;
10342 }
10343
10344 default:
10345 gcc_unreachable ();
10346 }
10347 }
10348
10349 FOR_EACH_CHILD (die, c, output_die (c));
10350
10351 /* Add null byte to terminate sibling list. */
10352 if (die->die_child != NULL)
10353 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10354 (unsigned long) die->die_offset);
10355 }
10356
10357 /* Output the compilation unit that appears at the beginning of the
10358 .debug_info section, and precedes the DIE descriptions. */
10359
10360 static void
10361 output_compilation_unit_header (enum dwarf_unit_type ut)
10362 {
10363 if (!XCOFF_DEBUGGING_INFO)
10364 {
10365 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10366 dw2_asm_output_data (4, 0xffffffff,
10367 "Initial length escape value indicating 64-bit DWARF extension");
10368 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10369 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10370 "Length of Compilation Unit Info");
10371 }
10372
10373 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10374 if (dwarf_version >= 5)
10375 {
10376 const char *name;
10377 switch (ut)
10378 {
10379 case DW_UT_compile: name = "DW_UT_compile"; break;
10380 case DW_UT_type: name = "DW_UT_type"; break;
10381 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10382 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10383 default: gcc_unreachable ();
10384 }
10385 dw2_asm_output_data (1, ut, "%s", name);
10386 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10387 }
10388 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10389 debug_abbrev_section,
10390 "Offset Into Abbrev. Section");
10391 if (dwarf_version < 5)
10392 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10393 }
10394
10395 /* Output the compilation unit DIE and its children. */
10396
10397 static void
10398 output_comp_unit (dw_die_ref die, int output_if_empty,
10399 const unsigned char *dwo_id)
10400 {
10401 const char *secname, *oldsym;
10402 char *tmp;
10403
10404 /* Unless we are outputting main CU, we may throw away empty ones. */
10405 if (!output_if_empty && die->die_child == NULL)
10406 return;
10407
10408 /* Even if there are no children of this DIE, we must output the information
10409 about the compilation unit. Otherwise, on an empty translation unit, we
10410 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10411 will then complain when examining the file. First mark all the DIEs in
10412 this CU so we know which get local refs. */
10413 mark_dies (die);
10414
10415 external_ref_hash_type *extern_map = optimize_external_refs (die);
10416
10417 /* For now, optimize only the main CU, in order to optimize the rest
10418 we'd need to see all of them earlier. Leave the rest for post-linking
10419 tools like DWZ. */
10420 if (die == comp_unit_die ())
10421 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10422
10423 build_abbrev_table (die, extern_map);
10424
10425 optimize_abbrev_table ();
10426
10427 delete extern_map;
10428
10429 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10430 next_die_offset = (dwo_id
10431 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10432 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10433 calc_die_sizes (die);
10434
10435 oldsym = die->die_id.die_symbol;
10436 if (oldsym && die->comdat_type_p)
10437 {
10438 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10439
10440 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10441 secname = tmp;
10442 die->die_id.die_symbol = NULL;
10443 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10444 }
10445 else
10446 {
10447 switch_to_section (debug_info_section);
10448 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10449 info_section_emitted = true;
10450 }
10451
10452 /* For LTO cross unit DIE refs we want a symbol on the start of the
10453 debuginfo section, not on the CU DIE. */
10454 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10455 {
10456 /* ??? No way to get visibility assembled without a decl. */
10457 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10458 get_identifier (oldsym), char_type_node);
10459 TREE_PUBLIC (decl) = true;
10460 TREE_STATIC (decl) = true;
10461 DECL_ARTIFICIAL (decl) = true;
10462 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10463 DECL_VISIBILITY_SPECIFIED (decl) = true;
10464 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10465 #ifdef ASM_WEAKEN_LABEL
10466 /* We prefer a .weak because that handles duplicates from duplicate
10467 archive members in a graceful way. */
10468 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10469 #else
10470 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10471 #endif
10472 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10473 }
10474
10475 /* Output debugging information. */
10476 output_compilation_unit_header (dwo_id
10477 ? DW_UT_split_compile : DW_UT_compile);
10478 if (dwarf_version >= 5)
10479 {
10480 if (dwo_id != NULL)
10481 for (int i = 0; i < 8; i++)
10482 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10483 }
10484 output_die (die);
10485
10486 /* Leave the marks on the main CU, so we can check them in
10487 output_pubnames. */
10488 if (oldsym)
10489 {
10490 unmark_dies (die);
10491 die->die_id.die_symbol = oldsym;
10492 }
10493 }
10494
10495 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10496 and .debug_pubtypes. This is configured per-target, but can be
10497 overridden by the -gpubnames or -gno-pubnames options. */
10498
10499 static inline bool
10500 want_pubnames (void)
10501 {
10502 if (debug_info_level <= DINFO_LEVEL_TERSE)
10503 return false;
10504 if (debug_generate_pub_sections != -1)
10505 return debug_generate_pub_sections;
10506 return targetm.want_debug_pub_sections;
10507 }
10508
10509 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10510
10511 static void
10512 add_AT_pubnames (dw_die_ref die)
10513 {
10514 if (want_pubnames ())
10515 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10516 }
10517
10518 /* Add a string attribute value to a skeleton DIE. */
10519
10520 static inline void
10521 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10522 const char *str)
10523 {
10524 dw_attr_node attr;
10525 struct indirect_string_node *node;
10526
10527 if (! skeleton_debug_str_hash)
10528 skeleton_debug_str_hash
10529 = hash_table<indirect_string_hasher>::create_ggc (10);
10530
10531 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10532 find_string_form (node);
10533 if (node->form == DW_FORM_GNU_str_index)
10534 node->form = DW_FORM_strp;
10535
10536 attr.dw_attr = attr_kind;
10537 attr.dw_attr_val.val_class = dw_val_class_str;
10538 attr.dw_attr_val.val_entry = NULL;
10539 attr.dw_attr_val.v.val_str = node;
10540 add_dwarf_attr (die, &attr);
10541 }
10542
10543 /* Helper function to generate top-level dies for skeleton debug_info and
10544 debug_types. */
10545
10546 static void
10547 add_top_level_skeleton_die_attrs (dw_die_ref die)
10548 {
10549 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10550 const char *comp_dir = comp_dir_string ();
10551
10552 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10553 if (comp_dir != NULL)
10554 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10555 add_AT_pubnames (die);
10556 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10557 }
10558
10559 /* Output skeleton debug sections that point to the dwo file. */
10560
10561 static void
10562 output_skeleton_debug_sections (dw_die_ref comp_unit,
10563 const unsigned char *dwo_id)
10564 {
10565 /* These attributes will be found in the full debug_info section. */
10566 remove_AT (comp_unit, DW_AT_producer);
10567 remove_AT (comp_unit, DW_AT_language);
10568
10569 switch_to_section (debug_skeleton_info_section);
10570 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10571
10572 /* Produce the skeleton compilation-unit header. This one differs enough from
10573 a normal CU header that it's better not to call output_compilation_unit
10574 header. */
10575 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10576 dw2_asm_output_data (4, 0xffffffff,
10577 "Initial length escape value indicating 64-bit "
10578 "DWARF extension");
10579
10580 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10581 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10582 - DWARF_INITIAL_LENGTH_SIZE
10583 + size_of_die (comp_unit),
10584 "Length of Compilation Unit Info");
10585 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10586 if (dwarf_version >= 5)
10587 {
10588 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10589 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10590 }
10591 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10592 debug_skeleton_abbrev_section,
10593 "Offset Into Abbrev. Section");
10594 if (dwarf_version < 5)
10595 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10596 else
10597 for (int i = 0; i < 8; i++)
10598 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10599
10600 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10601 output_die (comp_unit);
10602
10603 /* Build the skeleton debug_abbrev section. */
10604 switch_to_section (debug_skeleton_abbrev_section);
10605 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10606
10607 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10608
10609 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10610 }
10611
10612 /* Output a comdat type unit DIE and its children. */
10613
10614 static void
10615 output_comdat_type_unit (comdat_type_node *node)
10616 {
10617 const char *secname;
10618 char *tmp;
10619 int i;
10620 #if defined (OBJECT_FORMAT_ELF)
10621 tree comdat_key;
10622 #endif
10623
10624 /* First mark all the DIEs in this CU so we know which get local refs. */
10625 mark_dies (node->root_die);
10626
10627 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10628
10629 build_abbrev_table (node->root_die, extern_map);
10630
10631 delete extern_map;
10632 extern_map = NULL;
10633
10634 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10635 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10636 calc_die_sizes (node->root_die);
10637
10638 #if defined (OBJECT_FORMAT_ELF)
10639 if (dwarf_version >= 5)
10640 {
10641 if (!dwarf_split_debug_info)
10642 secname = ".debug_info";
10643 else
10644 secname = ".debug_info.dwo";
10645 }
10646 else if (!dwarf_split_debug_info)
10647 secname = ".debug_types";
10648 else
10649 secname = ".debug_types.dwo";
10650
10651 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10652 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10653 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10654 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10655 comdat_key = get_identifier (tmp);
10656 targetm.asm_out.named_section (secname,
10657 SECTION_DEBUG | SECTION_LINKONCE,
10658 comdat_key);
10659 #else
10660 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10661 sprintf (tmp, (dwarf_version >= 5
10662 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10663 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10664 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10665 secname = tmp;
10666 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10667 #endif
10668
10669 /* Output debugging information. */
10670 output_compilation_unit_header (dwarf_split_debug_info
10671 ? DW_UT_split_type : DW_UT_type);
10672 output_signature (node->signature, "Type Signature");
10673 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10674 "Offset to Type DIE");
10675 output_die (node->root_die);
10676
10677 unmark_dies (node->root_die);
10678 }
10679
10680 /* Return the DWARF2/3 pubname associated with a decl. */
10681
10682 static const char *
10683 dwarf2_name (tree decl, int scope)
10684 {
10685 if (DECL_NAMELESS (decl))
10686 return NULL;
10687 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10688 }
10689
10690 /* Add a new entry to .debug_pubnames if appropriate. */
10691
10692 static void
10693 add_pubname_string (const char *str, dw_die_ref die)
10694 {
10695 pubname_entry e;
10696
10697 e.die = die;
10698 e.name = xstrdup (str);
10699 vec_safe_push (pubname_table, e);
10700 }
10701
10702 static void
10703 add_pubname (tree decl, dw_die_ref die)
10704 {
10705 if (!want_pubnames ())
10706 return;
10707
10708 /* Don't add items to the table when we expect that the consumer will have
10709 just read the enclosing die. For example, if the consumer is looking at a
10710 class_member, it will either be inside the class already, or will have just
10711 looked up the class to find the member. Either way, searching the class is
10712 faster than searching the index. */
10713 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10714 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10715 {
10716 const char *name = dwarf2_name (decl, 1);
10717
10718 if (name)
10719 add_pubname_string (name, die);
10720 }
10721 }
10722
10723 /* Add an enumerator to the pubnames section. */
10724
10725 static void
10726 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10727 {
10728 pubname_entry e;
10729
10730 gcc_assert (scope_name);
10731 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10732 e.die = die;
10733 vec_safe_push (pubname_table, e);
10734 }
10735
10736 /* Add a new entry to .debug_pubtypes if appropriate. */
10737
10738 static void
10739 add_pubtype (tree decl, dw_die_ref die)
10740 {
10741 pubname_entry e;
10742
10743 if (!want_pubnames ())
10744 return;
10745
10746 if ((TREE_PUBLIC (decl)
10747 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10748 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10749 {
10750 tree scope = NULL;
10751 const char *scope_name = "";
10752 const char *sep = is_cxx () ? "::" : ".";
10753 const char *name;
10754
10755 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10756 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10757 {
10758 scope_name = lang_hooks.dwarf_name (scope, 1);
10759 if (scope_name != NULL && scope_name[0] != '\0')
10760 scope_name = concat (scope_name, sep, NULL);
10761 else
10762 scope_name = "";
10763 }
10764
10765 if (TYPE_P (decl))
10766 name = type_tag (decl);
10767 else
10768 name = lang_hooks.dwarf_name (decl, 1);
10769
10770 /* If we don't have a name for the type, there's no point in adding
10771 it to the table. */
10772 if (name != NULL && name[0] != '\0')
10773 {
10774 e.die = die;
10775 e.name = concat (scope_name, name, NULL);
10776 vec_safe_push (pubtype_table, e);
10777 }
10778
10779 /* Although it might be more consistent to add the pubinfo for the
10780 enumerators as their dies are created, they should only be added if the
10781 enum type meets the criteria above. So rather than re-check the parent
10782 enum type whenever an enumerator die is created, just output them all
10783 here. This isn't protected by the name conditional because anonymous
10784 enums don't have names. */
10785 if (die->die_tag == DW_TAG_enumeration_type)
10786 {
10787 dw_die_ref c;
10788
10789 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10790 }
10791 }
10792 }
10793
10794 /* Output a single entry in the pubnames table. */
10795
10796 static void
10797 output_pubname (dw_offset die_offset, pubname_entry *entry)
10798 {
10799 dw_die_ref die = entry->die;
10800 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10801
10802 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10803
10804 if (debug_generate_pub_sections == 2)
10805 {
10806 /* This logic follows gdb's method for determining the value of the flag
10807 byte. */
10808 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10809 switch (die->die_tag)
10810 {
10811 case DW_TAG_typedef:
10812 case DW_TAG_base_type:
10813 case DW_TAG_subrange_type:
10814 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10815 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10816 break;
10817 case DW_TAG_enumerator:
10818 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10819 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10820 if (!is_cxx ())
10821 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10822 break;
10823 case DW_TAG_subprogram:
10824 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10825 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10826 if (!is_ada ())
10827 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10828 break;
10829 case DW_TAG_constant:
10830 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10831 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10832 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10833 break;
10834 case DW_TAG_variable:
10835 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10836 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10837 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10838 break;
10839 case DW_TAG_namespace:
10840 case DW_TAG_imported_declaration:
10841 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10842 break;
10843 case DW_TAG_class_type:
10844 case DW_TAG_interface_type:
10845 case DW_TAG_structure_type:
10846 case DW_TAG_union_type:
10847 case DW_TAG_enumeration_type:
10848 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10849 if (!is_cxx ())
10850 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10851 break;
10852 default:
10853 /* An unusual tag. Leave the flag-byte empty. */
10854 break;
10855 }
10856 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10857 "GDB-index flags");
10858 }
10859
10860 dw2_asm_output_nstring (entry->name, -1, "external name");
10861 }
10862
10863
10864 /* Output the public names table used to speed up access to externally
10865 visible names; or the public types table used to find type definitions. */
10866
10867 static void
10868 output_pubnames (vec<pubname_entry, va_gc> *names)
10869 {
10870 unsigned i;
10871 unsigned long pubnames_length = size_of_pubnames (names);
10872 pubname_entry *pub;
10873
10874 if (!XCOFF_DEBUGGING_INFO)
10875 {
10876 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10877 dw2_asm_output_data (4, 0xffffffff,
10878 "Initial length escape value indicating 64-bit DWARF extension");
10879 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10880 "Pub Info Length");
10881 }
10882
10883 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10884 dw2_asm_output_data (2, 2, "DWARF Version");
10885
10886 if (dwarf_split_debug_info)
10887 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10888 debug_skeleton_info_section,
10889 "Offset of Compilation Unit Info");
10890 else
10891 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10892 debug_info_section,
10893 "Offset of Compilation Unit Info");
10894 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10895 "Compilation Unit Length");
10896
10897 FOR_EACH_VEC_ELT (*names, i, pub)
10898 {
10899 if (include_pubname_in_output (names, pub))
10900 {
10901 dw_offset die_offset = pub->die->die_offset;
10902
10903 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10904 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10905 gcc_assert (pub->die->die_mark);
10906
10907 /* If we're putting types in their own .debug_types sections,
10908 the .debug_pubtypes table will still point to the compile
10909 unit (not the type unit), so we want to use the offset of
10910 the skeleton DIE (if there is one). */
10911 if (pub->die->comdat_type_p && names == pubtype_table)
10912 {
10913 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10914
10915 if (type_node != NULL)
10916 die_offset = (type_node->skeleton_die != NULL
10917 ? type_node->skeleton_die->die_offset
10918 : comp_unit_die ()->die_offset);
10919 }
10920
10921 output_pubname (die_offset, pub);
10922 }
10923 }
10924
10925 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10926 }
10927
10928 /* Output public names and types tables if necessary. */
10929
10930 static void
10931 output_pubtables (void)
10932 {
10933 if (!want_pubnames () || !info_section_emitted)
10934 return;
10935
10936 switch_to_section (debug_pubnames_section);
10937 output_pubnames (pubname_table);
10938 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10939 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10940 simply won't look for the section. */
10941 switch_to_section (debug_pubtypes_section);
10942 output_pubnames (pubtype_table);
10943 }
10944
10945
10946 /* Output the information that goes into the .debug_aranges table.
10947 Namely, define the beginning and ending address range of the
10948 text section generated for this compilation unit. */
10949
10950 static void
10951 output_aranges (void)
10952 {
10953 unsigned i;
10954 unsigned long aranges_length = size_of_aranges ();
10955
10956 if (!XCOFF_DEBUGGING_INFO)
10957 {
10958 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10959 dw2_asm_output_data (4, 0xffffffff,
10960 "Initial length escape value indicating 64-bit DWARF extension");
10961 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10962 "Length of Address Ranges Info");
10963 }
10964
10965 /* Version number for aranges is still 2, even up to DWARF5. */
10966 dw2_asm_output_data (2, 2, "DWARF Version");
10967 if (dwarf_split_debug_info)
10968 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10969 debug_skeleton_info_section,
10970 "Offset of Compilation Unit Info");
10971 else
10972 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10973 debug_info_section,
10974 "Offset of Compilation Unit Info");
10975 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10976 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10977
10978 /* We need to align to twice the pointer size here. */
10979 if (DWARF_ARANGES_PAD_SIZE)
10980 {
10981 /* Pad using a 2 byte words so that padding is correct for any
10982 pointer size. */
10983 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10984 2 * DWARF2_ADDR_SIZE);
10985 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10986 dw2_asm_output_data (2, 0, NULL);
10987 }
10988
10989 /* It is necessary not to output these entries if the sections were
10990 not used; if the sections were not used, the length will be 0 and
10991 the address may end up as 0 if the section is discarded by ld
10992 --gc-sections, leaving an invalid (0, 0) entry that can be
10993 confused with the terminator. */
10994 if (text_section_used)
10995 {
10996 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10997 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10998 text_section_label, "Length");
10999 }
11000 if (cold_text_section_used)
11001 {
11002 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11003 "Address");
11004 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11005 cold_text_section_label, "Length");
11006 }
11007
11008 if (have_multiple_function_sections)
11009 {
11010 unsigned fde_idx;
11011 dw_fde_ref fde;
11012
11013 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11014 {
11015 if (DECL_IGNORED_P (fde->decl))
11016 continue;
11017 if (!fde->in_std_section)
11018 {
11019 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11020 "Address");
11021 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11022 fde->dw_fde_begin, "Length");
11023 }
11024 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11025 {
11026 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11027 "Address");
11028 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11029 fde->dw_fde_second_begin, "Length");
11030 }
11031 }
11032 }
11033
11034 /* Output the terminator words. */
11035 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11036 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11037 }
11038
11039 /* Add a new entry to .debug_ranges. Return its index into
11040 ranges_table vector. */
11041
11042 static unsigned int
11043 add_ranges_num (int num, bool maybe_new_sec)
11044 {
11045 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11046 vec_safe_push (ranges_table, r);
11047 return vec_safe_length (ranges_table) - 1;
11048 }
11049
11050 /* Add a new entry to .debug_ranges corresponding to a block, or a
11051 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11052 this entry might be in a different section from previous range. */
11053
11054 static unsigned int
11055 add_ranges (const_tree block, bool maybe_new_sec)
11056 {
11057 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11058 }
11059
11060 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11061 chain, or middle entry of a chain that will be directly referred to. */
11062
11063 static void
11064 note_rnglist_head (unsigned int offset)
11065 {
11066 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11067 return;
11068 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11069 }
11070
11071 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11072 When using dwarf_split_debug_info, address attributes in dies destined
11073 for the final executable should be direct references--setting the
11074 parameter force_direct ensures this behavior. */
11075
11076 static void
11077 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11078 bool *added, bool force_direct)
11079 {
11080 unsigned int in_use = vec_safe_length (ranges_by_label);
11081 unsigned int offset;
11082 dw_ranges_by_label rbl = { begin, end };
11083 vec_safe_push (ranges_by_label, rbl);
11084 offset = add_ranges_num (-(int)in_use - 1, true);
11085 if (!*added)
11086 {
11087 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11088 *added = true;
11089 note_rnglist_head (offset);
11090 }
11091 }
11092
11093 /* Emit .debug_ranges section. */
11094
11095 static void
11096 output_ranges (void)
11097 {
11098 unsigned i;
11099 static const char *const start_fmt = "Offset %#x";
11100 const char *fmt = start_fmt;
11101 dw_ranges *r;
11102
11103 switch_to_section (debug_ranges_section);
11104 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11105 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11106 {
11107 int block_num = r->num;
11108
11109 if (block_num > 0)
11110 {
11111 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11112 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11113
11114 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11115 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11116
11117 /* If all code is in the text section, then the compilation
11118 unit base address defaults to DW_AT_low_pc, which is the
11119 base of the text section. */
11120 if (!have_multiple_function_sections)
11121 {
11122 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11123 text_section_label,
11124 fmt, i * 2 * DWARF2_ADDR_SIZE);
11125 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11126 text_section_label, NULL);
11127 }
11128
11129 /* Otherwise, the compilation unit base address is zero,
11130 which allows us to use absolute addresses, and not worry
11131 about whether the target supports cross-section
11132 arithmetic. */
11133 else
11134 {
11135 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11136 fmt, i * 2 * DWARF2_ADDR_SIZE);
11137 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11138 }
11139
11140 fmt = NULL;
11141 }
11142
11143 /* Negative block_num stands for an index into ranges_by_label. */
11144 else if (block_num < 0)
11145 {
11146 int lab_idx = - block_num - 1;
11147
11148 if (!have_multiple_function_sections)
11149 {
11150 gcc_unreachable ();
11151 #if 0
11152 /* If we ever use add_ranges_by_labels () for a single
11153 function section, all we have to do is to take out
11154 the #if 0 above. */
11155 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11156 (*ranges_by_label)[lab_idx].begin,
11157 text_section_label,
11158 fmt, i * 2 * DWARF2_ADDR_SIZE);
11159 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11160 (*ranges_by_label)[lab_idx].end,
11161 text_section_label, NULL);
11162 #endif
11163 }
11164 else
11165 {
11166 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11167 (*ranges_by_label)[lab_idx].begin,
11168 fmt, i * 2 * DWARF2_ADDR_SIZE);
11169 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11170 (*ranges_by_label)[lab_idx].end,
11171 NULL);
11172 }
11173 }
11174 else
11175 {
11176 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11177 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11178 fmt = start_fmt;
11179 }
11180 }
11181 }
11182
11183 /* Non-zero if .debug_line_str should be used for .debug_line section
11184 strings or strings that are likely shareable with those. */
11185 #define DWARF5_USE_DEBUG_LINE_STR \
11186 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11187 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11188 /* FIXME: there is no .debug_line_str.dwo section, \
11189 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11190 && !dwarf_split_debug_info)
11191
11192 /* Assign .debug_rnglists indexes. */
11193
11194 static void
11195 index_rnglists (void)
11196 {
11197 unsigned i;
11198 dw_ranges *r;
11199
11200 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11201 if (r->label)
11202 r->idx = rnglist_idx++;
11203 }
11204
11205 /* Emit .debug_rnglists section. */
11206
11207 static void
11208 output_rnglists (unsigned generation)
11209 {
11210 unsigned i;
11211 dw_ranges *r;
11212 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11213 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11214 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11215
11216 switch_to_section (debug_ranges_section);
11217 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11218 /* There are up to 4 unique ranges labels per generation.
11219 See also init_sections_and_labels. */
11220 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11221 2 + generation * 4);
11222 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11223 3 + generation * 4);
11224 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11225 dw2_asm_output_data (4, 0xffffffff,
11226 "Initial length escape value indicating "
11227 "64-bit DWARF extension");
11228 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11229 "Length of Range Lists");
11230 ASM_OUTPUT_LABEL (asm_out_file, l1);
11231 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11232 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11233 dw2_asm_output_data (1, 0, "Segment Size");
11234 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11235 about relocation sizes and primarily care about the size of .debug*
11236 sections in linked shared libraries and executables, then
11237 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11238 into it are usually larger than just DW_FORM_sec_offset offsets
11239 into the .debug_rnglists section. */
11240 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11241 "Offset Entry Count");
11242 if (dwarf_split_debug_info)
11243 {
11244 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11245 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11246 if (r->label)
11247 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11248 ranges_base_label, NULL);
11249 }
11250
11251 const char *lab = "";
11252 unsigned int len = vec_safe_length (ranges_table);
11253 const char *base = NULL;
11254 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11255 {
11256 int block_num = r->num;
11257
11258 if (r->label)
11259 {
11260 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11261 lab = r->label;
11262 }
11263 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11264 base = NULL;
11265 if (block_num > 0)
11266 {
11267 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11268 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11269
11270 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11271 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11272
11273 if (HAVE_AS_LEB128)
11274 {
11275 /* If all code is in the text section, then the compilation
11276 unit base address defaults to DW_AT_low_pc, which is the
11277 base of the text section. */
11278 if (!have_multiple_function_sections)
11279 {
11280 dw2_asm_output_data (1, DW_RLE_offset_pair,
11281 "DW_RLE_offset_pair (%s)", lab);
11282 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11283 "Range begin address (%s)", lab);
11284 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11285 "Range end address (%s)", lab);
11286 continue;
11287 }
11288 if (base == NULL)
11289 {
11290 dw_ranges *r2 = NULL;
11291 if (i < len - 1)
11292 r2 = &(*ranges_table)[i + 1];
11293 if (r2
11294 && r2->num != 0
11295 && r2->label == NULL
11296 && !r2->maybe_new_sec)
11297 {
11298 dw2_asm_output_data (1, DW_RLE_base_address,
11299 "DW_RLE_base_address (%s)", lab);
11300 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11301 "Base address (%s)", lab);
11302 strcpy (basebuf, blabel);
11303 base = basebuf;
11304 }
11305 }
11306 if (base)
11307 {
11308 dw2_asm_output_data (1, DW_RLE_offset_pair,
11309 "DW_RLE_offset_pair (%s)", lab);
11310 dw2_asm_output_delta_uleb128 (blabel, base,
11311 "Range begin address (%s)", lab);
11312 dw2_asm_output_delta_uleb128 (elabel, base,
11313 "Range end address (%s)", lab);
11314 continue;
11315 }
11316 dw2_asm_output_data (1, DW_RLE_start_length,
11317 "DW_RLE_start_length (%s)", lab);
11318 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11319 "Range begin address (%s)", lab);
11320 dw2_asm_output_delta_uleb128 (elabel, blabel,
11321 "Range length (%s)", lab);
11322 }
11323 else
11324 {
11325 dw2_asm_output_data (1, DW_RLE_start_end,
11326 "DW_RLE_start_end (%s)", lab);
11327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11328 "Range begin address (%s)", lab);
11329 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11330 "Range end address (%s)", lab);
11331 }
11332 }
11333
11334 /* Negative block_num stands for an index into ranges_by_label. */
11335 else if (block_num < 0)
11336 {
11337 int lab_idx = - block_num - 1;
11338 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11339 const char *elabel = (*ranges_by_label)[lab_idx].end;
11340
11341 if (!have_multiple_function_sections)
11342 gcc_unreachable ();
11343 if (HAVE_AS_LEB128)
11344 {
11345 dw2_asm_output_data (1, DW_RLE_start_length,
11346 "DW_RLE_start_length (%s)", lab);
11347 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11348 "Range begin address (%s)", lab);
11349 dw2_asm_output_delta_uleb128 (elabel, blabel,
11350 "Range length (%s)", lab);
11351 }
11352 else
11353 {
11354 dw2_asm_output_data (1, DW_RLE_start_end,
11355 "DW_RLE_start_end (%s)", lab);
11356 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11357 "Range begin address (%s)", lab);
11358 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11359 "Range end address (%s)", lab);
11360 }
11361 }
11362 else
11363 dw2_asm_output_data (1, DW_RLE_end_of_list,
11364 "DW_RLE_end_of_list (%s)", lab);
11365 }
11366 ASM_OUTPUT_LABEL (asm_out_file, l2);
11367 }
11368
11369 /* Data structure containing information about input files. */
11370 struct file_info
11371 {
11372 const char *path; /* Complete file name. */
11373 const char *fname; /* File name part. */
11374 int length; /* Length of entire string. */
11375 struct dwarf_file_data * file_idx; /* Index in input file table. */
11376 int dir_idx; /* Index in directory table. */
11377 };
11378
11379 /* Data structure containing information about directories with source
11380 files. */
11381 struct dir_info
11382 {
11383 const char *path; /* Path including directory name. */
11384 int length; /* Path length. */
11385 int prefix; /* Index of directory entry which is a prefix. */
11386 int count; /* Number of files in this directory. */
11387 int dir_idx; /* Index of directory used as base. */
11388 };
11389
11390 /* Callback function for file_info comparison. We sort by looking at
11391 the directories in the path. */
11392
11393 static int
11394 file_info_cmp (const void *p1, const void *p2)
11395 {
11396 const struct file_info *const s1 = (const struct file_info *) p1;
11397 const struct file_info *const s2 = (const struct file_info *) p2;
11398 const unsigned char *cp1;
11399 const unsigned char *cp2;
11400
11401 /* Take care of file names without directories. We need to make sure that
11402 we return consistent values to qsort since some will get confused if
11403 we return the same value when identical operands are passed in opposite
11404 orders. So if neither has a directory, return 0 and otherwise return
11405 1 or -1 depending on which one has the directory. */
11406 if ((s1->path == s1->fname || s2->path == s2->fname))
11407 return (s2->path == s2->fname) - (s1->path == s1->fname);
11408
11409 cp1 = (const unsigned char *) s1->path;
11410 cp2 = (const unsigned char *) s2->path;
11411
11412 while (1)
11413 {
11414 ++cp1;
11415 ++cp2;
11416 /* Reached the end of the first path? If so, handle like above. */
11417 if ((cp1 == (const unsigned char *) s1->fname)
11418 || (cp2 == (const unsigned char *) s2->fname))
11419 return ((cp2 == (const unsigned char *) s2->fname)
11420 - (cp1 == (const unsigned char *) s1->fname));
11421
11422 /* Character of current path component the same? */
11423 else if (*cp1 != *cp2)
11424 return *cp1 - *cp2;
11425 }
11426 }
11427
11428 struct file_name_acquire_data
11429 {
11430 struct file_info *files;
11431 int used_files;
11432 int max_files;
11433 };
11434
11435 /* Traversal function for the hash table. */
11436
11437 int
11438 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11439 {
11440 struct dwarf_file_data *d = *slot;
11441 struct file_info *fi;
11442 const char *f;
11443
11444 gcc_assert (fnad->max_files >= d->emitted_number);
11445
11446 if (! d->emitted_number)
11447 return 1;
11448
11449 gcc_assert (fnad->max_files != fnad->used_files);
11450
11451 fi = fnad->files + fnad->used_files++;
11452
11453 /* Skip all leading "./". */
11454 f = d->filename;
11455 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11456 f += 2;
11457
11458 /* Create a new array entry. */
11459 fi->path = f;
11460 fi->length = strlen (f);
11461 fi->file_idx = d;
11462
11463 /* Search for the file name part. */
11464 f = strrchr (f, DIR_SEPARATOR);
11465 #if defined (DIR_SEPARATOR_2)
11466 {
11467 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11468
11469 if (g != NULL)
11470 {
11471 if (f == NULL || f < g)
11472 f = g;
11473 }
11474 }
11475 #endif
11476
11477 fi->fname = f == NULL ? fi->path : f + 1;
11478 return 1;
11479 }
11480
11481 /* Helper function for output_file_names. Emit a FORM encoded
11482 string STR, with assembly comment start ENTRY_KIND and
11483 index IDX */
11484
11485 static void
11486 output_line_string (enum dwarf_form form, const char *str,
11487 const char *entry_kind, unsigned int idx)
11488 {
11489 switch (form)
11490 {
11491 case DW_FORM_string:
11492 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11493 break;
11494 case DW_FORM_line_strp:
11495 if (!debug_line_str_hash)
11496 debug_line_str_hash
11497 = hash_table<indirect_string_hasher>::create_ggc (10);
11498
11499 struct indirect_string_node *node;
11500 node = find_AT_string_in_table (str, debug_line_str_hash);
11501 set_indirect_string (node);
11502 node->form = form;
11503 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11504 debug_line_str_section, "%s: %#x: \"%s\"",
11505 entry_kind, 0, node->str);
11506 break;
11507 default:
11508 gcc_unreachable ();
11509 }
11510 }
11511
11512 /* Output the directory table and the file name table. We try to minimize
11513 the total amount of memory needed. A heuristic is used to avoid large
11514 slowdowns with many input files. */
11515
11516 static void
11517 output_file_names (void)
11518 {
11519 struct file_name_acquire_data fnad;
11520 int numfiles;
11521 struct file_info *files;
11522 struct dir_info *dirs;
11523 int *saved;
11524 int *savehere;
11525 int *backmap;
11526 int ndirs;
11527 int idx_offset;
11528 int i;
11529
11530 if (!last_emitted_file)
11531 {
11532 if (dwarf_version >= 5)
11533 {
11534 dw2_asm_output_data (1, 0, "Directory entry format count");
11535 dw2_asm_output_data_uleb128 (0, "Directories count");
11536 dw2_asm_output_data (1, 0, "File name entry format count");
11537 dw2_asm_output_data_uleb128 (0, "File names count");
11538 }
11539 else
11540 {
11541 dw2_asm_output_data (1, 0, "End directory table");
11542 dw2_asm_output_data (1, 0, "End file name table");
11543 }
11544 return;
11545 }
11546
11547 numfiles = last_emitted_file->emitted_number;
11548
11549 /* Allocate the various arrays we need. */
11550 files = XALLOCAVEC (struct file_info, numfiles);
11551 dirs = XALLOCAVEC (struct dir_info, numfiles);
11552
11553 fnad.files = files;
11554 fnad.used_files = 0;
11555 fnad.max_files = numfiles;
11556 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11557 gcc_assert (fnad.used_files == fnad.max_files);
11558
11559 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11560
11561 /* Find all the different directories used. */
11562 dirs[0].path = files[0].path;
11563 dirs[0].length = files[0].fname - files[0].path;
11564 dirs[0].prefix = -1;
11565 dirs[0].count = 1;
11566 dirs[0].dir_idx = 0;
11567 files[0].dir_idx = 0;
11568 ndirs = 1;
11569
11570 for (i = 1; i < numfiles; i++)
11571 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11572 && memcmp (dirs[ndirs - 1].path, files[i].path,
11573 dirs[ndirs - 1].length) == 0)
11574 {
11575 /* Same directory as last entry. */
11576 files[i].dir_idx = ndirs - 1;
11577 ++dirs[ndirs - 1].count;
11578 }
11579 else
11580 {
11581 int j;
11582
11583 /* This is a new directory. */
11584 dirs[ndirs].path = files[i].path;
11585 dirs[ndirs].length = files[i].fname - files[i].path;
11586 dirs[ndirs].count = 1;
11587 dirs[ndirs].dir_idx = ndirs;
11588 files[i].dir_idx = ndirs;
11589
11590 /* Search for a prefix. */
11591 dirs[ndirs].prefix = -1;
11592 for (j = 0; j < ndirs; j++)
11593 if (dirs[j].length < dirs[ndirs].length
11594 && dirs[j].length > 1
11595 && (dirs[ndirs].prefix == -1
11596 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11597 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11598 dirs[ndirs].prefix = j;
11599
11600 ++ndirs;
11601 }
11602
11603 /* Now to the actual work. We have to find a subset of the directories which
11604 allow expressing the file name using references to the directory table
11605 with the least amount of characters. We do not do an exhaustive search
11606 where we would have to check out every combination of every single
11607 possible prefix. Instead we use a heuristic which provides nearly optimal
11608 results in most cases and never is much off. */
11609 saved = XALLOCAVEC (int, ndirs);
11610 savehere = XALLOCAVEC (int, ndirs);
11611
11612 memset (saved, '\0', ndirs * sizeof (saved[0]));
11613 for (i = 0; i < ndirs; i++)
11614 {
11615 int j;
11616 int total;
11617
11618 /* We can always save some space for the current directory. But this
11619 does not mean it will be enough to justify adding the directory. */
11620 savehere[i] = dirs[i].length;
11621 total = (savehere[i] - saved[i]) * dirs[i].count;
11622
11623 for (j = i + 1; j < ndirs; j++)
11624 {
11625 savehere[j] = 0;
11626 if (saved[j] < dirs[i].length)
11627 {
11628 /* Determine whether the dirs[i] path is a prefix of the
11629 dirs[j] path. */
11630 int k;
11631
11632 k = dirs[j].prefix;
11633 while (k != -1 && k != (int) i)
11634 k = dirs[k].prefix;
11635
11636 if (k == (int) i)
11637 {
11638 /* Yes it is. We can possibly save some memory by
11639 writing the filenames in dirs[j] relative to
11640 dirs[i]. */
11641 savehere[j] = dirs[i].length;
11642 total += (savehere[j] - saved[j]) * dirs[j].count;
11643 }
11644 }
11645 }
11646
11647 /* Check whether we can save enough to justify adding the dirs[i]
11648 directory. */
11649 if (total > dirs[i].length + 1)
11650 {
11651 /* It's worthwhile adding. */
11652 for (j = i; j < ndirs; j++)
11653 if (savehere[j] > 0)
11654 {
11655 /* Remember how much we saved for this directory so far. */
11656 saved[j] = savehere[j];
11657
11658 /* Remember the prefix directory. */
11659 dirs[j].dir_idx = i;
11660 }
11661 }
11662 }
11663
11664 /* Emit the directory name table. */
11665 idx_offset = dirs[0].length > 0 ? 1 : 0;
11666 enum dwarf_form str_form = DW_FORM_string;
11667 enum dwarf_form idx_form = DW_FORM_udata;
11668 if (dwarf_version >= 5)
11669 {
11670 const char *comp_dir = comp_dir_string ();
11671 if (comp_dir == NULL)
11672 comp_dir = "";
11673 dw2_asm_output_data (1, 1, "Directory entry format count");
11674 if (DWARF5_USE_DEBUG_LINE_STR)
11675 str_form = DW_FORM_line_strp;
11676 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11677 dw2_asm_output_data_uleb128 (str_form, "%s",
11678 get_DW_FORM_name (str_form));
11679 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11680 if (str_form == DW_FORM_string)
11681 {
11682 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11683 for (i = 1 - idx_offset; i < ndirs; i++)
11684 dw2_asm_output_nstring (dirs[i].path,
11685 dirs[i].length
11686 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11687 "Directory Entry: %#x", i + idx_offset);
11688 }
11689 else
11690 {
11691 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11692 for (i = 1 - idx_offset; i < ndirs; i++)
11693 {
11694 const char *str
11695 = ggc_alloc_string (dirs[i].path,
11696 dirs[i].length
11697 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11698 output_line_string (str_form, str, "Directory Entry",
11699 (unsigned) i + idx_offset);
11700 }
11701 }
11702 }
11703 else
11704 {
11705 for (i = 1 - idx_offset; i < ndirs; i++)
11706 dw2_asm_output_nstring (dirs[i].path,
11707 dirs[i].length
11708 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11709 "Directory Entry: %#x", i + idx_offset);
11710
11711 dw2_asm_output_data (1, 0, "End directory table");
11712 }
11713
11714 /* We have to emit them in the order of emitted_number since that's
11715 used in the debug info generation. To do this efficiently we
11716 generate a back-mapping of the indices first. */
11717 backmap = XALLOCAVEC (int, numfiles);
11718 for (i = 0; i < numfiles; i++)
11719 backmap[files[i].file_idx->emitted_number - 1] = i;
11720
11721 if (dwarf_version >= 5)
11722 {
11723 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11724 if (filename0 == NULL)
11725 filename0 = "";
11726 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11727 DW_FORM_data2. Choose one based on the number of directories
11728 and how much space would they occupy in each encoding.
11729 If we have at most 256 directories, all indexes fit into
11730 a single byte, so DW_FORM_data1 is most compact (if there
11731 are at most 128 directories, DW_FORM_udata would be as
11732 compact as that, but not shorter and slower to decode). */
11733 if (ndirs + idx_offset <= 256)
11734 idx_form = DW_FORM_data1;
11735 /* If there are more than 65536 directories, we have to use
11736 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11737 Otherwise, compute what space would occupy if all the indexes
11738 used DW_FORM_udata - sum - and compare that to how large would
11739 be DW_FORM_data2 encoding, and pick the more efficient one. */
11740 else if (ndirs + idx_offset <= 65536)
11741 {
11742 unsigned HOST_WIDE_INT sum = 1;
11743 for (i = 0; i < numfiles; i++)
11744 {
11745 int file_idx = backmap[i];
11746 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11747 sum += size_of_uleb128 (dir_idx);
11748 }
11749 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11750 idx_form = DW_FORM_data2;
11751 }
11752 #ifdef VMS_DEBUGGING_INFO
11753 dw2_asm_output_data (1, 4, "File name entry format count");
11754 #else
11755 dw2_asm_output_data (1, 2, "File name entry format count");
11756 #endif
11757 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11758 dw2_asm_output_data_uleb128 (str_form, "%s",
11759 get_DW_FORM_name (str_form));
11760 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11761 "DW_LNCT_directory_index");
11762 dw2_asm_output_data_uleb128 (idx_form, "%s",
11763 get_DW_FORM_name (idx_form));
11764 #ifdef VMS_DEBUGGING_INFO
11765 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11766 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11767 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11768 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11769 #endif
11770 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11771
11772 output_line_string (str_form, filename0, "File Entry", 0);
11773
11774 /* Include directory index. */
11775 if (idx_form != DW_FORM_udata)
11776 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11777 0, NULL);
11778 else
11779 dw2_asm_output_data_uleb128 (0, NULL);
11780
11781 #ifdef VMS_DEBUGGING_INFO
11782 dw2_asm_output_data_uleb128 (0, NULL);
11783 dw2_asm_output_data_uleb128 (0, NULL);
11784 #endif
11785 }
11786
11787 /* Now write all the file names. */
11788 for (i = 0; i < numfiles; i++)
11789 {
11790 int file_idx = backmap[i];
11791 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11792
11793 #ifdef VMS_DEBUGGING_INFO
11794 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11795
11796 /* Setting these fields can lead to debugger miscomparisons,
11797 but VMS Debug requires them to be set correctly. */
11798
11799 int ver;
11800 long long cdt;
11801 long siz;
11802 int maxfilelen = (strlen (files[file_idx].path)
11803 + dirs[dir_idx].length
11804 + MAX_VMS_VERSION_LEN + 1);
11805 char *filebuf = XALLOCAVEC (char, maxfilelen);
11806
11807 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11808 snprintf (filebuf, maxfilelen, "%s;%d",
11809 files[file_idx].path + dirs[dir_idx].length, ver);
11810
11811 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11812
11813 /* Include directory index. */
11814 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11815 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11816 dir_idx + idx_offset, NULL);
11817 else
11818 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11819
11820 /* Modification time. */
11821 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11822 &cdt, 0, 0, 0) == 0)
11823 ? cdt : 0, NULL);
11824
11825 /* File length in bytes. */
11826 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11827 0, &siz, 0, 0) == 0)
11828 ? siz : 0, NULL);
11829 #else
11830 output_line_string (str_form,
11831 files[file_idx].path + dirs[dir_idx].length,
11832 "File Entry", (unsigned) i + 1);
11833
11834 /* Include directory index. */
11835 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11836 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11837 dir_idx + idx_offset, NULL);
11838 else
11839 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11840
11841 if (dwarf_version >= 5)
11842 continue;
11843
11844 /* Modification time. */
11845 dw2_asm_output_data_uleb128 (0, NULL);
11846
11847 /* File length in bytes. */
11848 dw2_asm_output_data_uleb128 (0, NULL);
11849 #endif /* VMS_DEBUGGING_INFO */
11850 }
11851
11852 if (dwarf_version < 5)
11853 dw2_asm_output_data (1, 0, "End file name table");
11854 }
11855
11856
11857 /* Output one line number table into the .debug_line section. */
11858
11859 static void
11860 output_one_line_info_table (dw_line_info_table *table)
11861 {
11862 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11863 unsigned int current_line = 1;
11864 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11865 dw_line_info_entry *ent;
11866 size_t i;
11867
11868 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11869 {
11870 switch (ent->opcode)
11871 {
11872 case LI_set_address:
11873 /* ??? Unfortunately, we have little choice here currently, and
11874 must always use the most general form. GCC does not know the
11875 address delta itself, so we can't use DW_LNS_advance_pc. Many
11876 ports do have length attributes which will give an upper bound
11877 on the address range. We could perhaps use length attributes
11878 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11879 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11880
11881 /* This can handle any delta. This takes
11882 4+DWARF2_ADDR_SIZE bytes. */
11883 dw2_asm_output_data (1, 0, "set address %s", line_label);
11884 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11885 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11886 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11887 break;
11888
11889 case LI_set_line:
11890 if (ent->val == current_line)
11891 {
11892 /* We still need to start a new row, so output a copy insn. */
11893 dw2_asm_output_data (1, DW_LNS_copy,
11894 "copy line %u", current_line);
11895 }
11896 else
11897 {
11898 int line_offset = ent->val - current_line;
11899 int line_delta = line_offset - DWARF_LINE_BASE;
11900
11901 current_line = ent->val;
11902 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11903 {
11904 /* This can handle deltas from -10 to 234, using the current
11905 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11906 This takes 1 byte. */
11907 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11908 "line %u", current_line);
11909 }
11910 else
11911 {
11912 /* This can handle any delta. This takes at least 4 bytes,
11913 depending on the value being encoded. */
11914 dw2_asm_output_data (1, DW_LNS_advance_line,
11915 "advance to line %u", current_line);
11916 dw2_asm_output_data_sleb128 (line_offset, NULL);
11917 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11918 }
11919 }
11920 break;
11921
11922 case LI_set_file:
11923 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11924 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11925 break;
11926
11927 case LI_set_column:
11928 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11929 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11930 break;
11931
11932 case LI_negate_stmt:
11933 current_is_stmt = !current_is_stmt;
11934 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11935 "is_stmt %d", current_is_stmt);
11936 break;
11937
11938 case LI_set_prologue_end:
11939 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11940 "set prologue end");
11941 break;
11942
11943 case LI_set_epilogue_begin:
11944 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11945 "set epilogue begin");
11946 break;
11947
11948 case LI_set_discriminator:
11949 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11950 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11951 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11952 dw2_asm_output_data_uleb128 (ent->val, NULL);
11953 break;
11954 }
11955 }
11956
11957 /* Emit debug info for the address of the end of the table. */
11958 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11959 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11960 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11961 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11962
11963 dw2_asm_output_data (1, 0, "end sequence");
11964 dw2_asm_output_data_uleb128 (1, NULL);
11965 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11966 }
11967
11968 /* Output the source line number correspondence information. This
11969 information goes into the .debug_line section. */
11970
11971 static void
11972 output_line_info (bool prologue_only)
11973 {
11974 static unsigned int generation;
11975 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11976 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11977 bool saw_one = false;
11978 int opc;
11979
11980 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11981 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11982 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11983 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11984
11985 if (!XCOFF_DEBUGGING_INFO)
11986 {
11987 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11988 dw2_asm_output_data (4, 0xffffffff,
11989 "Initial length escape value indicating 64-bit DWARF extension");
11990 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11991 "Length of Source Line Info");
11992 }
11993
11994 ASM_OUTPUT_LABEL (asm_out_file, l1);
11995
11996 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11997 if (dwarf_version >= 5)
11998 {
11999 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12000 dw2_asm_output_data (1, 0, "Segment Size");
12001 }
12002 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12003 ASM_OUTPUT_LABEL (asm_out_file, p1);
12004
12005 /* Define the architecture-dependent minimum instruction length (in bytes).
12006 In this implementation of DWARF, this field is used for information
12007 purposes only. Since GCC generates assembly language, we have no
12008 a priori knowledge of how many instruction bytes are generated for each
12009 source line, and therefore can use only the DW_LNE_set_address and
12010 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12011 this as '1', which is "correct enough" for all architectures,
12012 and don't let the target override. */
12013 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12014
12015 if (dwarf_version >= 4)
12016 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12017 "Maximum Operations Per Instruction");
12018 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12019 "Default is_stmt_start flag");
12020 dw2_asm_output_data (1, DWARF_LINE_BASE,
12021 "Line Base Value (Special Opcodes)");
12022 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12023 "Line Range Value (Special Opcodes)");
12024 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12025 "Special Opcode Base");
12026
12027 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12028 {
12029 int n_op_args;
12030 switch (opc)
12031 {
12032 case DW_LNS_advance_pc:
12033 case DW_LNS_advance_line:
12034 case DW_LNS_set_file:
12035 case DW_LNS_set_column:
12036 case DW_LNS_fixed_advance_pc:
12037 case DW_LNS_set_isa:
12038 n_op_args = 1;
12039 break;
12040 default:
12041 n_op_args = 0;
12042 break;
12043 }
12044
12045 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12046 opc, n_op_args);
12047 }
12048
12049 /* Write out the information about the files we use. */
12050 output_file_names ();
12051 ASM_OUTPUT_LABEL (asm_out_file, p2);
12052 if (prologue_only)
12053 {
12054 /* Output the marker for the end of the line number info. */
12055 ASM_OUTPUT_LABEL (asm_out_file, l2);
12056 return;
12057 }
12058
12059 if (separate_line_info)
12060 {
12061 dw_line_info_table *table;
12062 size_t i;
12063
12064 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12065 if (table->in_use)
12066 {
12067 output_one_line_info_table (table);
12068 saw_one = true;
12069 }
12070 }
12071 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12072 {
12073 output_one_line_info_table (cold_text_section_line_info);
12074 saw_one = true;
12075 }
12076
12077 /* ??? Some Darwin linkers crash on a .debug_line section with no
12078 sequences. Further, merely a DW_LNE_end_sequence entry is not
12079 sufficient -- the address column must also be initialized.
12080 Make sure to output at least one set_address/end_sequence pair,
12081 choosing .text since that section is always present. */
12082 if (text_section_line_info->in_use || !saw_one)
12083 output_one_line_info_table (text_section_line_info);
12084
12085 /* Output the marker for the end of the line number info. */
12086 ASM_OUTPUT_LABEL (asm_out_file, l2);
12087 }
12088 \f
12089 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12090
12091 static inline bool
12092 need_endianity_attribute_p (bool reverse)
12093 {
12094 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12095 }
12096
12097 /* Given a pointer to a tree node for some base type, return a pointer to
12098 a DIE that describes the given type. REVERSE is true if the type is
12099 to be interpreted in the reverse storage order wrt the target order.
12100
12101 This routine must only be called for GCC type nodes that correspond to
12102 Dwarf base (fundamental) types. */
12103
12104 static dw_die_ref
12105 base_type_die (tree type, bool reverse)
12106 {
12107 dw_die_ref base_type_result;
12108 enum dwarf_type encoding;
12109 bool fpt_used = false;
12110 struct fixed_point_type_info fpt_info;
12111 tree type_bias = NULL_TREE;
12112
12113 /* If this is a subtype that should not be emitted as a subrange type,
12114 use the base type. See subrange_type_for_debug_p. */
12115 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12116 type = TREE_TYPE (type);
12117
12118 switch (TREE_CODE (type))
12119 {
12120 case INTEGER_TYPE:
12121 if ((dwarf_version >= 4 || !dwarf_strict)
12122 && TYPE_NAME (type)
12123 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12124 && DECL_IS_BUILTIN (TYPE_NAME (type))
12125 && DECL_NAME (TYPE_NAME (type)))
12126 {
12127 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12128 if (strcmp (name, "char16_t") == 0
12129 || strcmp (name, "char32_t") == 0)
12130 {
12131 encoding = DW_ATE_UTF;
12132 break;
12133 }
12134 }
12135 if ((dwarf_version >= 3 || !dwarf_strict)
12136 && lang_hooks.types.get_fixed_point_type_info)
12137 {
12138 memset (&fpt_info, 0, sizeof (fpt_info));
12139 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12140 {
12141 fpt_used = true;
12142 encoding = ((TYPE_UNSIGNED (type))
12143 ? DW_ATE_unsigned_fixed
12144 : DW_ATE_signed_fixed);
12145 break;
12146 }
12147 }
12148 if (TYPE_STRING_FLAG (type))
12149 {
12150 if (TYPE_UNSIGNED (type))
12151 encoding = DW_ATE_unsigned_char;
12152 else
12153 encoding = DW_ATE_signed_char;
12154 }
12155 else if (TYPE_UNSIGNED (type))
12156 encoding = DW_ATE_unsigned;
12157 else
12158 encoding = DW_ATE_signed;
12159
12160 if (!dwarf_strict
12161 && lang_hooks.types.get_type_bias)
12162 type_bias = lang_hooks.types.get_type_bias (type);
12163 break;
12164
12165 case REAL_TYPE:
12166 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12167 {
12168 if (dwarf_version >= 3 || !dwarf_strict)
12169 encoding = DW_ATE_decimal_float;
12170 else
12171 encoding = DW_ATE_lo_user;
12172 }
12173 else
12174 encoding = DW_ATE_float;
12175 break;
12176
12177 case FIXED_POINT_TYPE:
12178 if (!(dwarf_version >= 3 || !dwarf_strict))
12179 encoding = DW_ATE_lo_user;
12180 else if (TYPE_UNSIGNED (type))
12181 encoding = DW_ATE_unsigned_fixed;
12182 else
12183 encoding = DW_ATE_signed_fixed;
12184 break;
12185
12186 /* Dwarf2 doesn't know anything about complex ints, so use
12187 a user defined type for it. */
12188 case COMPLEX_TYPE:
12189 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12190 encoding = DW_ATE_complex_float;
12191 else
12192 encoding = DW_ATE_lo_user;
12193 break;
12194
12195 case BOOLEAN_TYPE:
12196 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12197 encoding = DW_ATE_boolean;
12198 break;
12199
12200 default:
12201 /* No other TREE_CODEs are Dwarf fundamental types. */
12202 gcc_unreachable ();
12203 }
12204
12205 base_type_result = new_die_raw (DW_TAG_base_type);
12206
12207 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12208 int_size_in_bytes (type));
12209 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12210
12211 if (need_endianity_attribute_p (reverse))
12212 add_AT_unsigned (base_type_result, DW_AT_endianity,
12213 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12214
12215 add_alignment_attribute (base_type_result, type);
12216
12217 if (fpt_used)
12218 {
12219 switch (fpt_info.scale_factor_kind)
12220 {
12221 case fixed_point_scale_factor_binary:
12222 add_AT_int (base_type_result, DW_AT_binary_scale,
12223 fpt_info.scale_factor.binary);
12224 break;
12225
12226 case fixed_point_scale_factor_decimal:
12227 add_AT_int (base_type_result, DW_AT_decimal_scale,
12228 fpt_info.scale_factor.decimal);
12229 break;
12230
12231 case fixed_point_scale_factor_arbitrary:
12232 /* Arbitrary scale factors cannot be described in standard DWARF,
12233 yet. */
12234 if (!dwarf_strict)
12235 {
12236 /* Describe the scale factor as a rational constant. */
12237 const dw_die_ref scale_factor
12238 = new_die (DW_TAG_constant, comp_unit_die (), type);
12239
12240 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12241 fpt_info.scale_factor.arbitrary.numerator);
12242 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12243 fpt_info.scale_factor.arbitrary.denominator);
12244
12245 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12246 }
12247 break;
12248
12249 default:
12250 gcc_unreachable ();
12251 }
12252 }
12253
12254 if (type_bias)
12255 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12256 dw_scalar_form_constant
12257 | dw_scalar_form_exprloc
12258 | dw_scalar_form_reference,
12259 NULL);
12260
12261 return base_type_result;
12262 }
12263
12264 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12265 named 'auto' in its type: return true for it, false otherwise. */
12266
12267 static inline bool
12268 is_cxx_auto (tree type)
12269 {
12270 if (is_cxx ())
12271 {
12272 tree name = TYPE_IDENTIFIER (type);
12273 if (name == get_identifier ("auto")
12274 || name == get_identifier ("decltype(auto)"))
12275 return true;
12276 }
12277 return false;
12278 }
12279
12280 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12281 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12282
12283 static inline int
12284 is_base_type (tree type)
12285 {
12286 switch (TREE_CODE (type))
12287 {
12288 case INTEGER_TYPE:
12289 case REAL_TYPE:
12290 case FIXED_POINT_TYPE:
12291 case COMPLEX_TYPE:
12292 case BOOLEAN_TYPE:
12293 case POINTER_BOUNDS_TYPE:
12294 return 1;
12295
12296 case VOID_TYPE:
12297 case ARRAY_TYPE:
12298 case RECORD_TYPE:
12299 case UNION_TYPE:
12300 case QUAL_UNION_TYPE:
12301 case ENUMERAL_TYPE:
12302 case FUNCTION_TYPE:
12303 case METHOD_TYPE:
12304 case POINTER_TYPE:
12305 case REFERENCE_TYPE:
12306 case NULLPTR_TYPE:
12307 case OFFSET_TYPE:
12308 case LANG_TYPE:
12309 case VECTOR_TYPE:
12310 return 0;
12311
12312 default:
12313 if (is_cxx_auto (type))
12314 return 0;
12315 gcc_unreachable ();
12316 }
12317
12318 return 0;
12319 }
12320
12321 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12322 node, return the size in bits for the type if it is a constant, or else
12323 return the alignment for the type if the type's size is not constant, or
12324 else return BITS_PER_WORD if the type actually turns out to be an
12325 ERROR_MARK node. */
12326
12327 static inline unsigned HOST_WIDE_INT
12328 simple_type_size_in_bits (const_tree type)
12329 {
12330 if (TREE_CODE (type) == ERROR_MARK)
12331 return BITS_PER_WORD;
12332 else if (TYPE_SIZE (type) == NULL_TREE)
12333 return 0;
12334 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12335 return tree_to_uhwi (TYPE_SIZE (type));
12336 else
12337 return TYPE_ALIGN (type);
12338 }
12339
12340 /* Similarly, but return an offset_int instead of UHWI. */
12341
12342 static inline offset_int
12343 offset_int_type_size_in_bits (const_tree type)
12344 {
12345 if (TREE_CODE (type) == ERROR_MARK)
12346 return BITS_PER_WORD;
12347 else if (TYPE_SIZE (type) == NULL_TREE)
12348 return 0;
12349 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12350 return wi::to_offset (TYPE_SIZE (type));
12351 else
12352 return TYPE_ALIGN (type);
12353 }
12354
12355 /* Given a pointer to a tree node for a subrange type, return a pointer
12356 to a DIE that describes the given type. */
12357
12358 static dw_die_ref
12359 subrange_type_die (tree type, tree low, tree high, tree bias,
12360 dw_die_ref context_die)
12361 {
12362 dw_die_ref subrange_die;
12363 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12364
12365 if (context_die == NULL)
12366 context_die = comp_unit_die ();
12367
12368 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12369
12370 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12371 {
12372 /* The size of the subrange type and its base type do not match,
12373 so we need to generate a size attribute for the subrange type. */
12374 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12375 }
12376
12377 add_alignment_attribute (subrange_die, type);
12378
12379 if (low)
12380 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12381 if (high)
12382 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12383 if (bias && !dwarf_strict)
12384 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12385 dw_scalar_form_constant
12386 | dw_scalar_form_exprloc
12387 | dw_scalar_form_reference,
12388 NULL);
12389
12390 return subrange_die;
12391 }
12392
12393 /* Returns the (const and/or volatile) cv_qualifiers associated with
12394 the decl node. This will normally be augmented with the
12395 cv_qualifiers of the underlying type in add_type_attribute. */
12396
12397 static int
12398 decl_quals (const_tree decl)
12399 {
12400 return ((TREE_READONLY (decl)
12401 /* The C++ front-end correctly marks reference-typed
12402 variables as readonly, but from a language (and debug
12403 info) standpoint they are not const-qualified. */
12404 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12405 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12406 | (TREE_THIS_VOLATILE (decl)
12407 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12408 }
12409
12410 /* Determine the TYPE whose qualifiers match the largest strict subset
12411 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12412 qualifiers outside QUAL_MASK. */
12413
12414 static int
12415 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12416 {
12417 tree t;
12418 int best_rank = 0, best_qual = 0, max_rank;
12419
12420 type_quals &= qual_mask;
12421 max_rank = popcount_hwi (type_quals) - 1;
12422
12423 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12424 t = TYPE_NEXT_VARIANT (t))
12425 {
12426 int q = TYPE_QUALS (t) & qual_mask;
12427
12428 if ((q & type_quals) == q && q != type_quals
12429 && check_base_type (t, type))
12430 {
12431 int rank = popcount_hwi (q);
12432
12433 if (rank > best_rank)
12434 {
12435 best_rank = rank;
12436 best_qual = q;
12437 }
12438 }
12439 }
12440
12441 return best_qual;
12442 }
12443
12444 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12445 static const dwarf_qual_info_t dwarf_qual_info[] =
12446 {
12447 { TYPE_QUAL_CONST, DW_TAG_const_type },
12448 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12449 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12450 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12451 };
12452 static const unsigned int dwarf_qual_info_size
12453 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12454
12455 /* If DIE is a qualified DIE of some base DIE with the same parent,
12456 return the base DIE, otherwise return NULL. Set MASK to the
12457 qualifiers added compared to the returned DIE. */
12458
12459 static dw_die_ref
12460 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12461 {
12462 unsigned int i;
12463 for (i = 0; i < dwarf_qual_info_size; i++)
12464 if (die->die_tag == dwarf_qual_info[i].t)
12465 break;
12466 if (i == dwarf_qual_info_size)
12467 return NULL;
12468 if (vec_safe_length (die->die_attr) != 1)
12469 return NULL;
12470 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12471 if (type == NULL || type->die_parent != die->die_parent)
12472 return NULL;
12473 *mask |= dwarf_qual_info[i].q;
12474 if (depth)
12475 {
12476 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12477 if (ret)
12478 return ret;
12479 }
12480 return type;
12481 }
12482
12483 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12484 entry that chains the modifiers specified by CV_QUALS in front of the
12485 given type. REVERSE is true if the type is to be interpreted in the
12486 reverse storage order wrt the target order. */
12487
12488 static dw_die_ref
12489 modified_type_die (tree type, int cv_quals, bool reverse,
12490 dw_die_ref context_die)
12491 {
12492 enum tree_code code = TREE_CODE (type);
12493 dw_die_ref mod_type_die;
12494 dw_die_ref sub_die = NULL;
12495 tree item_type = NULL;
12496 tree qualified_type;
12497 tree name, low, high;
12498 dw_die_ref mod_scope;
12499 /* Only these cv-qualifiers are currently handled. */
12500 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12501 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12502 ENCODE_QUAL_ADDR_SPACE(~0U));
12503 const bool reverse_base_type
12504 = need_endianity_attribute_p (reverse) && is_base_type (type);
12505
12506 if (code == ERROR_MARK)
12507 return NULL;
12508
12509 if (lang_hooks.types.get_debug_type)
12510 {
12511 tree debug_type = lang_hooks.types.get_debug_type (type);
12512
12513 if (debug_type != NULL_TREE && debug_type != type)
12514 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12515 }
12516
12517 cv_quals &= cv_qual_mask;
12518
12519 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12520 tag modifier (and not an attribute) old consumers won't be able
12521 to handle it. */
12522 if (dwarf_version < 3)
12523 cv_quals &= ~TYPE_QUAL_RESTRICT;
12524
12525 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12526 if (dwarf_version < 5)
12527 cv_quals &= ~TYPE_QUAL_ATOMIC;
12528
12529 /* See if we already have the appropriately qualified variant of
12530 this type. */
12531 qualified_type = get_qualified_type (type, cv_quals);
12532
12533 if (qualified_type == sizetype)
12534 {
12535 /* Try not to expose the internal sizetype type's name. */
12536 if (TYPE_NAME (qualified_type)
12537 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12538 {
12539 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12540
12541 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12542 && (TYPE_PRECISION (t)
12543 == TYPE_PRECISION (qualified_type))
12544 && (TYPE_UNSIGNED (t)
12545 == TYPE_UNSIGNED (qualified_type)));
12546 qualified_type = t;
12547 }
12548 else if (qualified_type == sizetype
12549 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12550 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12551 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12552 qualified_type = size_type_node;
12553 }
12554
12555 /* If we do, then we can just use its DIE, if it exists. */
12556 if (qualified_type)
12557 {
12558 mod_type_die = lookup_type_die (qualified_type);
12559
12560 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12561 dealt with specially: the DIE with the attribute, if it exists, is
12562 placed immediately after the regular DIE for the same base type. */
12563 if (mod_type_die
12564 && (!reverse_base_type
12565 || ((mod_type_die = mod_type_die->die_sib) != NULL
12566 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12567 return mod_type_die;
12568 }
12569
12570 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12571
12572 /* Handle C typedef types. */
12573 if (name
12574 && TREE_CODE (name) == TYPE_DECL
12575 && DECL_ORIGINAL_TYPE (name)
12576 && !DECL_ARTIFICIAL (name))
12577 {
12578 tree dtype = TREE_TYPE (name);
12579
12580 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12581 if (qualified_type == dtype && !reverse_base_type)
12582 {
12583 tree origin = decl_ultimate_origin (name);
12584
12585 /* Typedef variants that have an abstract origin don't get their own
12586 type DIE (see gen_typedef_die), so fall back on the ultimate
12587 abstract origin instead. */
12588 if (origin != NULL && origin != name)
12589 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12590 context_die);
12591
12592 /* For a named type, use the typedef. */
12593 gen_type_die (qualified_type, context_die);
12594 return lookup_type_die (qualified_type);
12595 }
12596 else
12597 {
12598 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12599 dquals &= cv_qual_mask;
12600 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12601 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12602 /* cv-unqualified version of named type. Just use
12603 the unnamed type to which it refers. */
12604 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12605 reverse, context_die);
12606 /* Else cv-qualified version of named type; fall through. */
12607 }
12608 }
12609
12610 mod_scope = scope_die_for (type, context_die);
12611
12612 if (cv_quals)
12613 {
12614 int sub_quals = 0, first_quals = 0;
12615 unsigned i;
12616 dw_die_ref first = NULL, last = NULL;
12617
12618 /* Determine a lesser qualified type that most closely matches
12619 this one. Then generate DW_TAG_* entries for the remaining
12620 qualifiers. */
12621 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12622 cv_qual_mask);
12623 if (sub_quals && use_debug_types)
12624 {
12625 bool needed = false;
12626 /* If emitting type units, make sure the order of qualifiers
12627 is canonical. Thus, start from unqualified type if
12628 an earlier qualifier is missing in sub_quals, but some later
12629 one is present there. */
12630 for (i = 0; i < dwarf_qual_info_size; i++)
12631 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12632 needed = true;
12633 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12634 {
12635 sub_quals = 0;
12636 break;
12637 }
12638 }
12639 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12640 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12641 {
12642 /* As not all intermediate qualified DIEs have corresponding
12643 tree types, ensure that qualified DIEs in the same scope
12644 as their DW_AT_type are emitted after their DW_AT_type,
12645 only with other qualified DIEs for the same type possibly
12646 in between them. Determine the range of such qualified
12647 DIEs now (first being the base type, last being corresponding
12648 last qualified DIE for it). */
12649 unsigned int count = 0;
12650 first = qualified_die_p (mod_type_die, &first_quals,
12651 dwarf_qual_info_size);
12652 if (first == NULL)
12653 first = mod_type_die;
12654 gcc_assert ((first_quals & ~sub_quals) == 0);
12655 for (count = 0, last = first;
12656 count < (1U << dwarf_qual_info_size);
12657 count++, last = last->die_sib)
12658 {
12659 int quals = 0;
12660 if (last == mod_scope->die_child)
12661 break;
12662 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12663 != first)
12664 break;
12665 }
12666 }
12667
12668 for (i = 0; i < dwarf_qual_info_size; i++)
12669 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12670 {
12671 dw_die_ref d;
12672 if (first && first != last)
12673 {
12674 for (d = first->die_sib; ; d = d->die_sib)
12675 {
12676 int quals = 0;
12677 qualified_die_p (d, &quals, dwarf_qual_info_size);
12678 if (quals == (first_quals | dwarf_qual_info[i].q))
12679 break;
12680 if (d == last)
12681 {
12682 d = NULL;
12683 break;
12684 }
12685 }
12686 if (d)
12687 {
12688 mod_type_die = d;
12689 continue;
12690 }
12691 }
12692 if (first)
12693 {
12694 d = new_die_raw (dwarf_qual_info[i].t);
12695 add_child_die_after (mod_scope, d, last);
12696 last = d;
12697 }
12698 else
12699 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12700 if (mod_type_die)
12701 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12702 mod_type_die = d;
12703 first_quals |= dwarf_qual_info[i].q;
12704 }
12705 }
12706 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12707 {
12708 dwarf_tag tag = DW_TAG_pointer_type;
12709 if (code == REFERENCE_TYPE)
12710 {
12711 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12712 tag = DW_TAG_rvalue_reference_type;
12713 else
12714 tag = DW_TAG_reference_type;
12715 }
12716 mod_type_die = new_die (tag, mod_scope, type);
12717
12718 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12719 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12720 add_alignment_attribute (mod_type_die, type);
12721 item_type = TREE_TYPE (type);
12722
12723 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12724 if (!ADDR_SPACE_GENERIC_P (as))
12725 {
12726 int action = targetm.addr_space.debug (as);
12727 if (action >= 0)
12728 {
12729 /* Positive values indicate an address_class. */
12730 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12731 }
12732 else
12733 {
12734 /* Negative values indicate an (inverted) segment base reg. */
12735 dw_loc_descr_ref d
12736 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12737 add_AT_loc (mod_type_die, DW_AT_segment, d);
12738 }
12739 }
12740 }
12741 else if (code == INTEGER_TYPE
12742 && TREE_TYPE (type) != NULL_TREE
12743 && subrange_type_for_debug_p (type, &low, &high))
12744 {
12745 tree bias = NULL_TREE;
12746 if (lang_hooks.types.get_type_bias)
12747 bias = lang_hooks.types.get_type_bias (type);
12748 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12749 item_type = TREE_TYPE (type);
12750 }
12751 else if (is_base_type (type))
12752 {
12753 mod_type_die = base_type_die (type, reverse);
12754
12755 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12756 if (reverse_base_type)
12757 {
12758 dw_die_ref after_die
12759 = modified_type_die (type, cv_quals, false, context_die);
12760 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12761 }
12762 else
12763 add_child_die (comp_unit_die (), mod_type_die);
12764
12765 add_pubtype (type, mod_type_die);
12766 }
12767 else
12768 {
12769 gen_type_die (type, context_die);
12770
12771 /* We have to get the type_main_variant here (and pass that to the
12772 `lookup_type_die' routine) because the ..._TYPE node we have
12773 might simply be a *copy* of some original type node (where the
12774 copy was created to help us keep track of typedef names) and
12775 that copy might have a different TYPE_UID from the original
12776 ..._TYPE node. */
12777 if (TREE_CODE (type) == FUNCTION_TYPE
12778 || TREE_CODE (type) == METHOD_TYPE)
12779 {
12780 /* For function/method types, can't just use type_main_variant here,
12781 because that can have different ref-qualifiers for C++,
12782 but try to canonicalize. */
12783 tree main = TYPE_MAIN_VARIANT (type);
12784 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12785 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12786 && check_base_type (t, main)
12787 && check_lang_type (t, type))
12788 return lookup_type_die (t);
12789 return lookup_type_die (type);
12790 }
12791 else if (TREE_CODE (type) != VECTOR_TYPE
12792 && TREE_CODE (type) != ARRAY_TYPE)
12793 return lookup_type_die (type_main_variant (type));
12794 else
12795 /* Vectors have the debugging information in the type,
12796 not the main variant. */
12797 return lookup_type_die (type);
12798 }
12799
12800 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12801 don't output a DW_TAG_typedef, since there isn't one in the
12802 user's program; just attach a DW_AT_name to the type.
12803 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12804 if the base type already has the same name. */
12805 if (name
12806 && ((TREE_CODE (name) != TYPE_DECL
12807 && (qualified_type == TYPE_MAIN_VARIANT (type)
12808 || (cv_quals == TYPE_UNQUALIFIED)))
12809 || (TREE_CODE (name) == TYPE_DECL
12810 && TREE_TYPE (name) == qualified_type
12811 && DECL_NAME (name))))
12812 {
12813 if (TREE_CODE (name) == TYPE_DECL)
12814 /* Could just call add_name_and_src_coords_attributes here,
12815 but since this is a builtin type it doesn't have any
12816 useful source coordinates anyway. */
12817 name = DECL_NAME (name);
12818 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12819 }
12820 /* This probably indicates a bug. */
12821 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12822 {
12823 name = TYPE_IDENTIFIER (type);
12824 add_name_attribute (mod_type_die,
12825 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12826 }
12827
12828 if (qualified_type && !reverse_base_type)
12829 equate_type_number_to_die (qualified_type, mod_type_die);
12830
12831 if (item_type)
12832 /* We must do this after the equate_type_number_to_die call, in case
12833 this is a recursive type. This ensures that the modified_type_die
12834 recursion will terminate even if the type is recursive. Recursive
12835 types are possible in Ada. */
12836 sub_die = modified_type_die (item_type,
12837 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12838 reverse,
12839 context_die);
12840
12841 if (sub_die != NULL)
12842 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12843
12844 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12845 if (TYPE_ARTIFICIAL (type))
12846 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12847
12848 return mod_type_die;
12849 }
12850
12851 /* Generate DIEs for the generic parameters of T.
12852 T must be either a generic type or a generic function.
12853 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12854
12855 static void
12856 gen_generic_params_dies (tree t)
12857 {
12858 tree parms, args;
12859 int parms_num, i;
12860 dw_die_ref die = NULL;
12861 int non_default;
12862
12863 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12864 return;
12865
12866 if (TYPE_P (t))
12867 die = lookup_type_die (t);
12868 else if (DECL_P (t))
12869 die = lookup_decl_die (t);
12870
12871 gcc_assert (die);
12872
12873 parms = lang_hooks.get_innermost_generic_parms (t);
12874 if (!parms)
12875 /* T has no generic parameter. It means T is neither a generic type
12876 or function. End of story. */
12877 return;
12878
12879 parms_num = TREE_VEC_LENGTH (parms);
12880 args = lang_hooks.get_innermost_generic_args (t);
12881 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12882 non_default = int_cst_value (TREE_CHAIN (args));
12883 else
12884 non_default = TREE_VEC_LENGTH (args);
12885 for (i = 0; i < parms_num; i++)
12886 {
12887 tree parm, arg, arg_pack_elems;
12888 dw_die_ref parm_die;
12889
12890 parm = TREE_VEC_ELT (parms, i);
12891 arg = TREE_VEC_ELT (args, i);
12892 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12893 gcc_assert (parm && TREE_VALUE (parm) && arg);
12894
12895 if (parm && TREE_VALUE (parm) && arg)
12896 {
12897 /* If PARM represents a template parameter pack,
12898 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12899 by DW_TAG_template_*_parameter DIEs for the argument
12900 pack elements of ARG. Note that ARG would then be
12901 an argument pack. */
12902 if (arg_pack_elems)
12903 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12904 arg_pack_elems,
12905 die);
12906 else
12907 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12908 true /* emit name */, die);
12909 if (i >= non_default)
12910 add_AT_flag (parm_die, DW_AT_default_value, 1);
12911 }
12912 }
12913 }
12914
12915 /* Create and return a DIE for PARM which should be
12916 the representation of a generic type parameter.
12917 For instance, in the C++ front end, PARM would be a template parameter.
12918 ARG is the argument to PARM.
12919 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12920 name of the PARM.
12921 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12922 as a child node. */
12923
12924 static dw_die_ref
12925 generic_parameter_die (tree parm, tree arg,
12926 bool emit_name_p,
12927 dw_die_ref parent_die)
12928 {
12929 dw_die_ref tmpl_die = NULL;
12930 const char *name = NULL;
12931
12932 if (!parm || !DECL_NAME (parm) || !arg)
12933 return NULL;
12934
12935 /* We support non-type generic parameters and arguments,
12936 type generic parameters and arguments, as well as
12937 generic generic parameters (a.k.a. template template parameters in C++)
12938 and arguments. */
12939 if (TREE_CODE (parm) == PARM_DECL)
12940 /* PARM is a nontype generic parameter */
12941 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12942 else if (TREE_CODE (parm) == TYPE_DECL)
12943 /* PARM is a type generic parameter. */
12944 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12945 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12946 /* PARM is a generic generic parameter.
12947 Its DIE is a GNU extension. It shall have a
12948 DW_AT_name attribute to represent the name of the template template
12949 parameter, and a DW_AT_GNU_template_name attribute to represent the
12950 name of the template template argument. */
12951 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12952 parent_die, parm);
12953 else
12954 gcc_unreachable ();
12955
12956 if (tmpl_die)
12957 {
12958 tree tmpl_type;
12959
12960 /* If PARM is a generic parameter pack, it means we are
12961 emitting debug info for a template argument pack element.
12962 In other terms, ARG is a template argument pack element.
12963 In that case, we don't emit any DW_AT_name attribute for
12964 the die. */
12965 if (emit_name_p)
12966 {
12967 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12968 gcc_assert (name);
12969 add_AT_string (tmpl_die, DW_AT_name, name);
12970 }
12971
12972 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12973 {
12974 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12975 TMPL_DIE should have a child DW_AT_type attribute that is set
12976 to the type of the argument to PARM, which is ARG.
12977 If PARM is a type generic parameter, TMPL_DIE should have a
12978 child DW_AT_type that is set to ARG. */
12979 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12980 add_type_attribute (tmpl_die, tmpl_type,
12981 (TREE_THIS_VOLATILE (tmpl_type)
12982 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12983 false, parent_die);
12984 }
12985 else
12986 {
12987 /* So TMPL_DIE is a DIE representing a
12988 a generic generic template parameter, a.k.a template template
12989 parameter in C++ and arg is a template. */
12990
12991 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12992 to the name of the argument. */
12993 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12994 if (name)
12995 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12996 }
12997
12998 if (TREE_CODE (parm) == PARM_DECL)
12999 /* So PARM is a non-type generic parameter.
13000 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13001 attribute of TMPL_DIE which value represents the value
13002 of ARG.
13003 We must be careful here:
13004 The value of ARG might reference some function decls.
13005 We might currently be emitting debug info for a generic
13006 type and types are emitted before function decls, we don't
13007 know if the function decls referenced by ARG will actually be
13008 emitted after cgraph computations.
13009 So must defer the generation of the DW_AT_const_value to
13010 after cgraph is ready. */
13011 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13012 }
13013
13014 return tmpl_die;
13015 }
13016
13017 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13018 PARM_PACK must be a template parameter pack. The returned DIE
13019 will be child DIE of PARENT_DIE. */
13020
13021 static dw_die_ref
13022 template_parameter_pack_die (tree parm_pack,
13023 tree parm_pack_args,
13024 dw_die_ref parent_die)
13025 {
13026 dw_die_ref die;
13027 int j;
13028
13029 gcc_assert (parent_die && parm_pack);
13030
13031 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13032 add_name_and_src_coords_attributes (die, parm_pack);
13033 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13034 generic_parameter_die (parm_pack,
13035 TREE_VEC_ELT (parm_pack_args, j),
13036 false /* Don't emit DW_AT_name */,
13037 die);
13038 return die;
13039 }
13040
13041 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13042 an enumerated type. */
13043
13044 static inline int
13045 type_is_enum (const_tree type)
13046 {
13047 return TREE_CODE (type) == ENUMERAL_TYPE;
13048 }
13049
13050 /* Return the DBX register number described by a given RTL node. */
13051
13052 static unsigned int
13053 dbx_reg_number (const_rtx rtl)
13054 {
13055 unsigned regno = REGNO (rtl);
13056
13057 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13058
13059 #ifdef LEAF_REG_REMAP
13060 if (crtl->uses_only_leaf_regs)
13061 {
13062 int leaf_reg = LEAF_REG_REMAP (regno);
13063 if (leaf_reg != -1)
13064 regno = (unsigned) leaf_reg;
13065 }
13066 #endif
13067
13068 regno = DBX_REGISTER_NUMBER (regno);
13069 gcc_assert (regno != INVALID_REGNUM);
13070 return regno;
13071 }
13072
13073 /* Optionally add a DW_OP_piece term to a location description expression.
13074 DW_OP_piece is only added if the location description expression already
13075 doesn't end with DW_OP_piece. */
13076
13077 static void
13078 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13079 {
13080 dw_loc_descr_ref loc;
13081
13082 if (*list_head != NULL)
13083 {
13084 /* Find the end of the chain. */
13085 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13086 ;
13087
13088 if (loc->dw_loc_opc != DW_OP_piece)
13089 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13090 }
13091 }
13092
13093 /* Return a location descriptor that designates a machine register or
13094 zero if there is none. */
13095
13096 static dw_loc_descr_ref
13097 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13098 {
13099 rtx regs;
13100
13101 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13102 return 0;
13103
13104 /* We only use "frame base" when we're sure we're talking about the
13105 post-prologue local stack frame. We do this by *not* running
13106 register elimination until this point, and recognizing the special
13107 argument pointer and soft frame pointer rtx's.
13108 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13109 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13110 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13111 {
13112 dw_loc_descr_ref result = NULL;
13113
13114 if (dwarf_version >= 4 || !dwarf_strict)
13115 {
13116 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13117 initialized);
13118 if (result)
13119 add_loc_descr (&result,
13120 new_loc_descr (DW_OP_stack_value, 0, 0));
13121 }
13122 return result;
13123 }
13124
13125 regs = targetm.dwarf_register_span (rtl);
13126
13127 if (REG_NREGS (rtl) > 1 || regs)
13128 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13129 else
13130 {
13131 unsigned int dbx_regnum = dbx_reg_number (rtl);
13132 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13133 return 0;
13134 return one_reg_loc_descriptor (dbx_regnum, initialized);
13135 }
13136 }
13137
13138 /* Return a location descriptor that designates a machine register for
13139 a given hard register number. */
13140
13141 static dw_loc_descr_ref
13142 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13143 {
13144 dw_loc_descr_ref reg_loc_descr;
13145
13146 if (regno <= 31)
13147 reg_loc_descr
13148 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13149 else
13150 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13151
13152 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13153 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13154
13155 return reg_loc_descr;
13156 }
13157
13158 /* Given an RTL of a register, return a location descriptor that
13159 designates a value that spans more than one register. */
13160
13161 static dw_loc_descr_ref
13162 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13163 enum var_init_status initialized)
13164 {
13165 int size, i;
13166 dw_loc_descr_ref loc_result = NULL;
13167
13168 /* Simple, contiguous registers. */
13169 if (regs == NULL_RTX)
13170 {
13171 unsigned reg = REGNO (rtl);
13172 int nregs;
13173
13174 #ifdef LEAF_REG_REMAP
13175 if (crtl->uses_only_leaf_regs)
13176 {
13177 int leaf_reg = LEAF_REG_REMAP (reg);
13178 if (leaf_reg != -1)
13179 reg = (unsigned) leaf_reg;
13180 }
13181 #endif
13182
13183 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13184 nregs = REG_NREGS (rtl);
13185
13186 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13187
13188 loc_result = NULL;
13189 while (nregs--)
13190 {
13191 dw_loc_descr_ref t;
13192
13193 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13194 VAR_INIT_STATUS_INITIALIZED);
13195 add_loc_descr (&loc_result, t);
13196 add_loc_descr_op_piece (&loc_result, size);
13197 ++reg;
13198 }
13199 return loc_result;
13200 }
13201
13202 /* Now onto stupid register sets in non contiguous locations. */
13203
13204 gcc_assert (GET_CODE (regs) == PARALLEL);
13205
13206 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13207 loc_result = NULL;
13208
13209 for (i = 0; i < XVECLEN (regs, 0); ++i)
13210 {
13211 dw_loc_descr_ref t;
13212
13213 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13214 VAR_INIT_STATUS_INITIALIZED);
13215 add_loc_descr (&loc_result, t);
13216 add_loc_descr_op_piece (&loc_result, size);
13217 }
13218
13219 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13220 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13221 return loc_result;
13222 }
13223
13224 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13225
13226 /* Return a location descriptor that designates a constant i,
13227 as a compound operation from constant (i >> shift), constant shift
13228 and DW_OP_shl. */
13229
13230 static dw_loc_descr_ref
13231 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13232 {
13233 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13234 add_loc_descr (&ret, int_loc_descriptor (shift));
13235 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13236 return ret;
13237 }
13238
13239 /* Return a location descriptor that designates constant POLY_I. */
13240
13241 static dw_loc_descr_ref
13242 int_loc_descriptor (poly_int64 poly_i)
13243 {
13244 enum dwarf_location_atom op;
13245
13246 HOST_WIDE_INT i;
13247 if (!poly_i.is_constant (&i))
13248 {
13249 /* Create location descriptions for the non-constant part and
13250 add any constant offset at the end. */
13251 dw_loc_descr_ref ret = NULL;
13252 HOST_WIDE_INT constant = poly_i.coeffs[0];
13253 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13254 {
13255 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13256 if (coeff != 0)
13257 {
13258 dw_loc_descr_ref start = ret;
13259 unsigned int factor;
13260 int bias;
13261 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13262 (j, &factor, &bias);
13263
13264 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13265 add COEFF * (REGNO / FACTOR) now and subtract
13266 COEFF * BIAS from the final constant part. */
13267 constant -= coeff * bias;
13268 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13269 if (coeff % factor == 0)
13270 coeff /= factor;
13271 else
13272 {
13273 int amount = exact_log2 (factor);
13274 gcc_assert (amount >= 0);
13275 add_loc_descr (&ret, int_loc_descriptor (amount));
13276 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13277 }
13278 if (coeff != 1)
13279 {
13280 add_loc_descr (&ret, int_loc_descriptor (coeff));
13281 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13282 }
13283 if (start)
13284 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13285 }
13286 }
13287 loc_descr_plus_const (&ret, constant);
13288 return ret;
13289 }
13290
13291 /* Pick the smallest representation of a constant, rather than just
13292 defaulting to the LEB encoding. */
13293 if (i >= 0)
13294 {
13295 int clz = clz_hwi (i);
13296 int ctz = ctz_hwi (i);
13297 if (i <= 31)
13298 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13299 else if (i <= 0xff)
13300 op = DW_OP_const1u;
13301 else if (i <= 0xffff)
13302 op = DW_OP_const2u;
13303 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13304 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13305 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13306 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13307 while DW_OP_const4u is 5 bytes. */
13308 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13309 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13310 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13311 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13312 while DW_OP_const4u is 5 bytes. */
13313 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13314
13315 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13316 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13317 <= 4)
13318 {
13319 /* As i >= 2**31, the double cast above will yield a negative number.
13320 Since wrapping is defined in DWARF expressions we can output big
13321 positive integers as small negative ones, regardless of the size
13322 of host wide ints.
13323
13324 Here, since the evaluator will handle 32-bit values and since i >=
13325 2**31, we know it's going to be interpreted as a negative literal:
13326 store it this way if we can do better than 5 bytes this way. */
13327 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13328 }
13329 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13330 op = DW_OP_const4u;
13331
13332 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13333 least 6 bytes: see if we can do better before falling back to it. */
13334 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13335 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13336 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13337 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13338 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13339 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13340 >= HOST_BITS_PER_WIDE_INT)
13341 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13342 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13343 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13344 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13345 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13346 && size_of_uleb128 (i) > 6)
13347 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13348 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13349 else
13350 op = DW_OP_constu;
13351 }
13352 else
13353 {
13354 if (i >= -0x80)
13355 op = DW_OP_const1s;
13356 else if (i >= -0x8000)
13357 op = DW_OP_const2s;
13358 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13359 {
13360 if (size_of_int_loc_descriptor (i) < 5)
13361 {
13362 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13363 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13364 return ret;
13365 }
13366 op = DW_OP_const4s;
13367 }
13368 else
13369 {
13370 if (size_of_int_loc_descriptor (i)
13371 < (unsigned long) 1 + size_of_sleb128 (i))
13372 {
13373 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13374 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13375 return ret;
13376 }
13377 op = DW_OP_consts;
13378 }
13379 }
13380
13381 return new_loc_descr (op, i, 0);
13382 }
13383
13384 /* Likewise, for unsigned constants. */
13385
13386 static dw_loc_descr_ref
13387 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13388 {
13389 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13390 const unsigned HOST_WIDE_INT max_uint
13391 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13392
13393 /* If possible, use the clever signed constants handling. */
13394 if (i <= max_int)
13395 return int_loc_descriptor ((HOST_WIDE_INT) i);
13396
13397 /* Here, we are left with positive numbers that cannot be represented as
13398 HOST_WIDE_INT, i.e.:
13399 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13400
13401 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13402 whereas may be better to output a negative integer: thanks to integer
13403 wrapping, we know that:
13404 x = x - 2 ** DWARF2_ADDR_SIZE
13405 = x - 2 * (max (HOST_WIDE_INT) + 1)
13406 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13407 small negative integers. Let's try that in cases it will clearly improve
13408 the encoding: there is no gain turning DW_OP_const4u into
13409 DW_OP_const4s. */
13410 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13411 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13412 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13413 {
13414 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13415
13416 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13417 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13418 const HOST_WIDE_INT second_shift
13419 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13420
13421 /* So we finally have:
13422 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13423 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13424 return int_loc_descriptor (second_shift);
13425 }
13426
13427 /* Last chance: fallback to a simple constant operation. */
13428 return new_loc_descr
13429 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13430 ? DW_OP_const4u
13431 : DW_OP_const8u,
13432 i, 0);
13433 }
13434
13435 /* Generate and return a location description that computes the unsigned
13436 comparison of the two stack top entries (a OP b where b is the top-most
13437 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13438 LE_EXPR, GT_EXPR or GE_EXPR. */
13439
13440 static dw_loc_descr_ref
13441 uint_comparison_loc_list (enum tree_code kind)
13442 {
13443 enum dwarf_location_atom op, flip_op;
13444 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13445
13446 switch (kind)
13447 {
13448 case LT_EXPR:
13449 op = DW_OP_lt;
13450 break;
13451 case LE_EXPR:
13452 op = DW_OP_le;
13453 break;
13454 case GT_EXPR:
13455 op = DW_OP_gt;
13456 break;
13457 case GE_EXPR:
13458 op = DW_OP_ge;
13459 break;
13460 default:
13461 gcc_unreachable ();
13462 }
13463
13464 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13465 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13466
13467 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13468 possible to perform unsigned comparisons: we just have to distinguish
13469 three cases:
13470
13471 1. when a and b have the same sign (as signed integers); then we should
13472 return: a OP(signed) b;
13473
13474 2. when a is a negative signed integer while b is a positive one, then a
13475 is a greater unsigned integer than b; likewise when a and b's roles
13476 are flipped.
13477
13478 So first, compare the sign of the two operands. */
13479 ret = new_loc_descr (DW_OP_over, 0, 0);
13480 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13481 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13482 /* If they have different signs (i.e. they have different sign bits), then
13483 the stack top value has now the sign bit set and thus it's smaller than
13484 zero. */
13485 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13486 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13487 add_loc_descr (&ret, bra_node);
13488
13489 /* We are in case 1. At this point, we know both operands have the same
13490 sign, to it's safe to use the built-in signed comparison. */
13491 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13492 add_loc_descr (&ret, jmp_node);
13493
13494 /* We are in case 2. Here, we know both operands do not have the same sign,
13495 so we have to flip the signed comparison. */
13496 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13497 tmp = new_loc_descr (flip_op, 0, 0);
13498 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13499 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13500 add_loc_descr (&ret, tmp);
13501
13502 /* This dummy operation is necessary to make the two branches join. */
13503 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13504 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13505 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13506 add_loc_descr (&ret, tmp);
13507
13508 return ret;
13509 }
13510
13511 /* Likewise, but takes the location description lists (might be destructive on
13512 them). Return NULL if either is NULL or if concatenation fails. */
13513
13514 static dw_loc_list_ref
13515 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13516 enum tree_code kind)
13517 {
13518 if (left == NULL || right == NULL)
13519 return NULL;
13520
13521 add_loc_list (&left, right);
13522 if (left == NULL)
13523 return NULL;
13524
13525 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13526 return left;
13527 }
13528
13529 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13530 without actually allocating it. */
13531
13532 static unsigned long
13533 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13534 {
13535 return size_of_int_loc_descriptor (i >> shift)
13536 + size_of_int_loc_descriptor (shift)
13537 + 1;
13538 }
13539
13540 /* Return size_of_locs (int_loc_descriptor (i)) without
13541 actually allocating it. */
13542
13543 static unsigned long
13544 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13545 {
13546 unsigned long s;
13547
13548 if (i >= 0)
13549 {
13550 int clz, ctz;
13551 if (i <= 31)
13552 return 1;
13553 else if (i <= 0xff)
13554 return 2;
13555 else if (i <= 0xffff)
13556 return 3;
13557 clz = clz_hwi (i);
13558 ctz = ctz_hwi (i);
13559 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13560 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13561 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13562 - clz - 5);
13563 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13564 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13565 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13566 - clz - 8);
13567 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13568 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13569 <= 4)
13570 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13571 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13572 return 5;
13573 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13574 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13575 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13576 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13577 - clz - 8);
13578 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13579 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13580 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13581 - clz - 16);
13582 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13583 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13584 && s > 6)
13585 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13586 - clz - 32);
13587 else
13588 return 1 + s;
13589 }
13590 else
13591 {
13592 if (i >= -0x80)
13593 return 2;
13594 else if (i >= -0x8000)
13595 return 3;
13596 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13597 {
13598 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13599 {
13600 s = size_of_int_loc_descriptor (-i) + 1;
13601 if (s < 5)
13602 return s;
13603 }
13604 return 5;
13605 }
13606 else
13607 {
13608 unsigned long r = 1 + size_of_sleb128 (i);
13609 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13610 {
13611 s = size_of_int_loc_descriptor (-i) + 1;
13612 if (s < r)
13613 return s;
13614 }
13615 return r;
13616 }
13617 }
13618 }
13619
13620 /* Return loc description representing "address" of integer value.
13621 This can appear only as toplevel expression. */
13622
13623 static dw_loc_descr_ref
13624 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13625 {
13626 int litsize;
13627 dw_loc_descr_ref loc_result = NULL;
13628
13629 if (!(dwarf_version >= 4 || !dwarf_strict))
13630 return NULL;
13631
13632 litsize = size_of_int_loc_descriptor (i);
13633 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13634 is more compact. For DW_OP_stack_value we need:
13635 litsize + 1 (DW_OP_stack_value)
13636 and for DW_OP_implicit_value:
13637 1 (DW_OP_implicit_value) + 1 (length) + size. */
13638 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13639 {
13640 loc_result = int_loc_descriptor (i);
13641 add_loc_descr (&loc_result,
13642 new_loc_descr (DW_OP_stack_value, 0, 0));
13643 return loc_result;
13644 }
13645
13646 loc_result = new_loc_descr (DW_OP_implicit_value,
13647 size, 0);
13648 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13649 loc_result->dw_loc_oprnd2.v.val_int = i;
13650 return loc_result;
13651 }
13652
13653 /* Return a location descriptor that designates a base+offset location. */
13654
13655 static dw_loc_descr_ref
13656 based_loc_descr (rtx reg, poly_int64 offset,
13657 enum var_init_status initialized)
13658 {
13659 unsigned int regno;
13660 dw_loc_descr_ref result;
13661 dw_fde_ref fde = cfun->fde;
13662
13663 /* We only use "frame base" when we're sure we're talking about the
13664 post-prologue local stack frame. We do this by *not* running
13665 register elimination until this point, and recognizing the special
13666 argument pointer and soft frame pointer rtx's. */
13667 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13668 {
13669 rtx elim = (ira_use_lra_p
13670 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13671 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13672
13673 if (elim != reg)
13674 {
13675 elim = strip_offset_and_add (elim, &offset);
13676 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13677 && (elim == hard_frame_pointer_rtx
13678 || elim == stack_pointer_rtx))
13679 || elim == (frame_pointer_needed
13680 ? hard_frame_pointer_rtx
13681 : stack_pointer_rtx));
13682
13683 /* If drap register is used to align stack, use frame
13684 pointer + offset to access stack variables. If stack
13685 is aligned without drap, use stack pointer + offset to
13686 access stack variables. */
13687 if (crtl->stack_realign_tried
13688 && reg == frame_pointer_rtx)
13689 {
13690 int base_reg
13691 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13692 ? HARD_FRAME_POINTER_REGNUM
13693 : REGNO (elim));
13694 return new_reg_loc_descr (base_reg, offset);
13695 }
13696
13697 gcc_assert (frame_pointer_fb_offset_valid);
13698 offset += frame_pointer_fb_offset;
13699 HOST_WIDE_INT const_offset;
13700 if (offset.is_constant (&const_offset))
13701 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
13702 else
13703 {
13704 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
13705 loc_descr_plus_const (&ret, offset);
13706 return ret;
13707 }
13708 }
13709 }
13710
13711 regno = REGNO (reg);
13712 #ifdef LEAF_REG_REMAP
13713 if (crtl->uses_only_leaf_regs)
13714 {
13715 int leaf_reg = LEAF_REG_REMAP (regno);
13716 if (leaf_reg != -1)
13717 regno = (unsigned) leaf_reg;
13718 }
13719 #endif
13720 regno = DWARF_FRAME_REGNUM (regno);
13721
13722 HOST_WIDE_INT const_offset;
13723 if (!optimize && fde
13724 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
13725 && offset.is_constant (&const_offset))
13726 {
13727 /* Use cfa+offset to represent the location of arguments passed
13728 on the stack when drap is used to align stack.
13729 Only do this when not optimizing, for optimized code var-tracking
13730 is supposed to track where the arguments live and the register
13731 used as vdrap or drap in some spot might be used for something
13732 else in other part of the routine. */
13733 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
13734 }
13735
13736 result = new_reg_loc_descr (regno, offset);
13737
13738 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13739 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13740
13741 return result;
13742 }
13743
13744 /* Return true if this RTL expression describes a base+offset calculation. */
13745
13746 static inline int
13747 is_based_loc (const_rtx rtl)
13748 {
13749 return (GET_CODE (rtl) == PLUS
13750 && ((REG_P (XEXP (rtl, 0))
13751 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13752 && CONST_INT_P (XEXP (rtl, 1)))));
13753 }
13754
13755 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13756 failed. */
13757
13758 static dw_loc_descr_ref
13759 tls_mem_loc_descriptor (rtx mem)
13760 {
13761 tree base;
13762 dw_loc_descr_ref loc_result;
13763
13764 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13765 return NULL;
13766
13767 base = get_base_address (MEM_EXPR (mem));
13768 if (base == NULL
13769 || !VAR_P (base)
13770 || !DECL_THREAD_LOCAL_P (base))
13771 return NULL;
13772
13773 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13774 if (loc_result == NULL)
13775 return NULL;
13776
13777 if (maybe_ne (MEM_OFFSET (mem), 0))
13778 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13779
13780 return loc_result;
13781 }
13782
13783 /* Output debug info about reason why we failed to expand expression as dwarf
13784 expression. */
13785
13786 static void
13787 expansion_failed (tree expr, rtx rtl, char const *reason)
13788 {
13789 if (dump_file && (dump_flags & TDF_DETAILS))
13790 {
13791 fprintf (dump_file, "Failed to expand as dwarf: ");
13792 if (expr)
13793 print_generic_expr (dump_file, expr, dump_flags);
13794 if (rtl)
13795 {
13796 fprintf (dump_file, "\n");
13797 print_rtl (dump_file, rtl);
13798 }
13799 fprintf (dump_file, "\nReason: %s\n", reason);
13800 }
13801 }
13802
13803 /* Helper function for const_ok_for_output. */
13804
13805 static bool
13806 const_ok_for_output_1 (rtx rtl)
13807 {
13808 if (targetm.const_not_ok_for_debug_p (rtl))
13809 {
13810 if (GET_CODE (rtl) != UNSPEC)
13811 {
13812 expansion_failed (NULL_TREE, rtl,
13813 "Expression rejected for debug by the backend.\n");
13814 return false;
13815 }
13816
13817 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13818 the target hook doesn't explicitly allow it in debug info, assume
13819 we can't express it in the debug info. */
13820 /* Don't complain about TLS UNSPECs, those are just too hard to
13821 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13822 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13823 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13824 if (flag_checking
13825 && (XVECLEN (rtl, 0) == 0
13826 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13827 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13828 inform (current_function_decl
13829 ? DECL_SOURCE_LOCATION (current_function_decl)
13830 : UNKNOWN_LOCATION,
13831 #if NUM_UNSPEC_VALUES > 0
13832 "non-delegitimized UNSPEC %s (%d) found in variable location",
13833 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13834 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13835 XINT (rtl, 1));
13836 #else
13837 "non-delegitimized UNSPEC %d found in variable location",
13838 XINT (rtl, 1));
13839 #endif
13840 expansion_failed (NULL_TREE, rtl,
13841 "UNSPEC hasn't been delegitimized.\n");
13842 return false;
13843 }
13844
13845 if (CONST_POLY_INT_P (rtl))
13846 return false;
13847
13848 if (targetm.const_not_ok_for_debug_p (rtl))
13849 {
13850 expansion_failed (NULL_TREE, rtl,
13851 "Expression rejected for debug by the backend.\n");
13852 return false;
13853 }
13854
13855 /* FIXME: Refer to PR60655. It is possible for simplification
13856 of rtl expressions in var tracking to produce such expressions.
13857 We should really identify / validate expressions
13858 enclosed in CONST that can be handled by assemblers on various
13859 targets and only handle legitimate cases here. */
13860 switch (GET_CODE (rtl))
13861 {
13862 case SYMBOL_REF:
13863 break;
13864 case NOT:
13865 case NEG:
13866 return false;
13867 default:
13868 return true;
13869 }
13870
13871 if (CONSTANT_POOL_ADDRESS_P (rtl))
13872 {
13873 bool marked;
13874 get_pool_constant_mark (rtl, &marked);
13875 /* If all references to this pool constant were optimized away,
13876 it was not output and thus we can't represent it. */
13877 if (!marked)
13878 {
13879 expansion_failed (NULL_TREE, rtl,
13880 "Constant was removed from constant pool.\n");
13881 return false;
13882 }
13883 }
13884
13885 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13886 return false;
13887
13888 /* Avoid references to external symbols in debug info, on several targets
13889 the linker might even refuse to link when linking a shared library,
13890 and in many other cases the relocations for .debug_info/.debug_loc are
13891 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13892 to be defined within the same shared library or executable are fine. */
13893 if (SYMBOL_REF_EXTERNAL_P (rtl))
13894 {
13895 tree decl = SYMBOL_REF_DECL (rtl);
13896
13897 if (decl == NULL || !targetm.binds_local_p (decl))
13898 {
13899 expansion_failed (NULL_TREE, rtl,
13900 "Symbol not defined in current TU.\n");
13901 return false;
13902 }
13903 }
13904
13905 return true;
13906 }
13907
13908 /* Return true if constant RTL can be emitted in DW_OP_addr or
13909 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13910 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13911
13912 static bool
13913 const_ok_for_output (rtx rtl)
13914 {
13915 if (GET_CODE (rtl) == SYMBOL_REF)
13916 return const_ok_for_output_1 (rtl);
13917
13918 if (GET_CODE (rtl) == CONST)
13919 {
13920 subrtx_var_iterator::array_type array;
13921 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13922 if (!const_ok_for_output_1 (*iter))
13923 return false;
13924 return true;
13925 }
13926
13927 return true;
13928 }
13929
13930 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13931 if possible, NULL otherwise. */
13932
13933 static dw_die_ref
13934 base_type_for_mode (machine_mode mode, bool unsignedp)
13935 {
13936 dw_die_ref type_die;
13937 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13938
13939 if (type == NULL)
13940 return NULL;
13941 switch (TREE_CODE (type))
13942 {
13943 case INTEGER_TYPE:
13944 case REAL_TYPE:
13945 break;
13946 default:
13947 return NULL;
13948 }
13949 type_die = lookup_type_die (type);
13950 if (!type_die)
13951 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13952 comp_unit_die ());
13953 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13954 return NULL;
13955 return type_die;
13956 }
13957
13958 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13959 type matching MODE, or, if MODE is narrower than or as wide as
13960 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13961 possible. */
13962
13963 static dw_loc_descr_ref
13964 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13965 {
13966 machine_mode outer_mode = mode;
13967 dw_die_ref type_die;
13968 dw_loc_descr_ref cvt;
13969
13970 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13971 {
13972 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13973 return op;
13974 }
13975 type_die = base_type_for_mode (outer_mode, 1);
13976 if (type_die == NULL)
13977 return NULL;
13978 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13979 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13980 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13981 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13982 add_loc_descr (&op, cvt);
13983 return op;
13984 }
13985
13986 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13987
13988 static dw_loc_descr_ref
13989 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13990 dw_loc_descr_ref op1)
13991 {
13992 dw_loc_descr_ref ret = op0;
13993 add_loc_descr (&ret, op1);
13994 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13995 if (STORE_FLAG_VALUE != 1)
13996 {
13997 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13998 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13999 }
14000 return ret;
14001 }
14002
14003 /* Subroutine of scompare_loc_descriptor for the case in which we're
14004 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14005 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14006
14007 static dw_loc_descr_ref
14008 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14009 scalar_int_mode op_mode,
14010 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14011 {
14012 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14013 dw_loc_descr_ref cvt;
14014
14015 if (type_die == NULL)
14016 return NULL;
14017 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14018 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14019 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14020 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14021 add_loc_descr (&op0, cvt);
14022 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14023 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14024 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14025 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14026 add_loc_descr (&op1, cvt);
14027 return compare_loc_descriptor (op, op0, op1);
14028 }
14029
14030 /* Subroutine of scompare_loc_descriptor for the case in which we're
14031 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14032 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14033
14034 static dw_loc_descr_ref
14035 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14036 scalar_int_mode op_mode,
14037 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14038 {
14039 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14040 /* For eq/ne, if the operands are known to be zero-extended,
14041 there is no need to do the fancy shifting up. */
14042 if (op == DW_OP_eq || op == DW_OP_ne)
14043 {
14044 dw_loc_descr_ref last0, last1;
14045 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14046 ;
14047 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14048 ;
14049 /* deref_size zero extends, and for constants we can check
14050 whether they are zero extended or not. */
14051 if (((last0->dw_loc_opc == DW_OP_deref_size
14052 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14053 || (CONST_INT_P (XEXP (rtl, 0))
14054 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14055 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14056 && ((last1->dw_loc_opc == DW_OP_deref_size
14057 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14058 || (CONST_INT_P (XEXP (rtl, 1))
14059 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14060 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14061 return compare_loc_descriptor (op, op0, op1);
14062
14063 /* EQ/NE comparison against constant in narrower type than
14064 DWARF2_ADDR_SIZE can be performed either as
14065 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14066 DW_OP_{eq,ne}
14067 or
14068 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14069 DW_OP_{eq,ne}. Pick whatever is shorter. */
14070 if (CONST_INT_P (XEXP (rtl, 1))
14071 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14072 && (size_of_int_loc_descriptor (shift) + 1
14073 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14074 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14075 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14076 & GET_MODE_MASK (op_mode))))
14077 {
14078 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14079 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14080 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14081 & GET_MODE_MASK (op_mode));
14082 return compare_loc_descriptor (op, op0, op1);
14083 }
14084 }
14085 add_loc_descr (&op0, int_loc_descriptor (shift));
14086 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14087 if (CONST_INT_P (XEXP (rtl, 1)))
14088 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14089 else
14090 {
14091 add_loc_descr (&op1, int_loc_descriptor (shift));
14092 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14093 }
14094 return compare_loc_descriptor (op, op0, op1);
14095 }
14096
14097 /* Return location descriptor for unsigned comparison OP RTL. */
14098
14099 static dw_loc_descr_ref
14100 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14101 machine_mode mem_mode)
14102 {
14103 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14104 dw_loc_descr_ref op0, op1;
14105
14106 if (op_mode == VOIDmode)
14107 op_mode = GET_MODE (XEXP (rtl, 1));
14108 if (op_mode == VOIDmode)
14109 return NULL;
14110
14111 scalar_int_mode int_op_mode;
14112 if (dwarf_strict
14113 && dwarf_version < 5
14114 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14115 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14116 return NULL;
14117
14118 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14119 VAR_INIT_STATUS_INITIALIZED);
14120 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14121 VAR_INIT_STATUS_INITIALIZED);
14122
14123 if (op0 == NULL || op1 == NULL)
14124 return NULL;
14125
14126 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14127 {
14128 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14129 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14130
14131 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14132 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14133 }
14134 return compare_loc_descriptor (op, op0, op1);
14135 }
14136
14137 /* Return location descriptor for unsigned comparison OP RTL. */
14138
14139 static dw_loc_descr_ref
14140 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14141 machine_mode mem_mode)
14142 {
14143 dw_loc_descr_ref op0, op1;
14144
14145 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14146 if (test_op_mode == VOIDmode)
14147 test_op_mode = GET_MODE (XEXP (rtl, 1));
14148
14149 scalar_int_mode op_mode;
14150 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14151 return NULL;
14152
14153 if (dwarf_strict
14154 && dwarf_version < 5
14155 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14156 return NULL;
14157
14158 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14159 VAR_INIT_STATUS_INITIALIZED);
14160 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14161 VAR_INIT_STATUS_INITIALIZED);
14162
14163 if (op0 == NULL || op1 == NULL)
14164 return NULL;
14165
14166 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14167 {
14168 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14169 dw_loc_descr_ref last0, last1;
14170 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14171 ;
14172 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14173 ;
14174 if (CONST_INT_P (XEXP (rtl, 0)))
14175 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14176 /* deref_size zero extends, so no need to mask it again. */
14177 else if (last0->dw_loc_opc != DW_OP_deref_size
14178 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14179 {
14180 add_loc_descr (&op0, int_loc_descriptor (mask));
14181 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14182 }
14183 if (CONST_INT_P (XEXP (rtl, 1)))
14184 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14185 /* deref_size zero extends, so no need to mask it again. */
14186 else if (last1->dw_loc_opc != DW_OP_deref_size
14187 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14188 {
14189 add_loc_descr (&op1, int_loc_descriptor (mask));
14190 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14191 }
14192 }
14193 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14194 {
14195 HOST_WIDE_INT bias = 1;
14196 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14197 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14198 if (CONST_INT_P (XEXP (rtl, 1)))
14199 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14200 + INTVAL (XEXP (rtl, 1)));
14201 else
14202 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14203 bias, 0));
14204 }
14205 return compare_loc_descriptor (op, op0, op1);
14206 }
14207
14208 /* Return location descriptor for {U,S}{MIN,MAX}. */
14209
14210 static dw_loc_descr_ref
14211 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14212 machine_mode mem_mode)
14213 {
14214 enum dwarf_location_atom op;
14215 dw_loc_descr_ref op0, op1, ret;
14216 dw_loc_descr_ref bra_node, drop_node;
14217
14218 scalar_int_mode int_mode;
14219 if (dwarf_strict
14220 && dwarf_version < 5
14221 && (!is_a <scalar_int_mode> (mode, &int_mode)
14222 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14223 return NULL;
14224
14225 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14226 VAR_INIT_STATUS_INITIALIZED);
14227 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14228 VAR_INIT_STATUS_INITIALIZED);
14229
14230 if (op0 == NULL || op1 == NULL)
14231 return NULL;
14232
14233 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14234 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14235 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14236 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14237 {
14238 /* Checked by the caller. */
14239 int_mode = as_a <scalar_int_mode> (mode);
14240 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14241 {
14242 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14243 add_loc_descr (&op0, int_loc_descriptor (mask));
14244 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14245 add_loc_descr (&op1, int_loc_descriptor (mask));
14246 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14247 }
14248 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14249 {
14250 HOST_WIDE_INT bias = 1;
14251 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14252 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14253 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14254 }
14255 }
14256 else if (is_a <scalar_int_mode> (mode, &int_mode)
14257 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14258 {
14259 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14260 add_loc_descr (&op0, int_loc_descriptor (shift));
14261 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14262 add_loc_descr (&op1, int_loc_descriptor (shift));
14263 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14264 }
14265 else if (is_a <scalar_int_mode> (mode, &int_mode)
14266 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14267 {
14268 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14269 dw_loc_descr_ref cvt;
14270 if (type_die == NULL)
14271 return NULL;
14272 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14273 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14274 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14275 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14276 add_loc_descr (&op0, cvt);
14277 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14278 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14279 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14280 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14281 add_loc_descr (&op1, cvt);
14282 }
14283
14284 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14285 op = DW_OP_lt;
14286 else
14287 op = DW_OP_gt;
14288 ret = op0;
14289 add_loc_descr (&ret, op1);
14290 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14291 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14292 add_loc_descr (&ret, bra_node);
14293 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14294 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14295 add_loc_descr (&ret, drop_node);
14296 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14297 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14298 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14299 && is_a <scalar_int_mode> (mode, &int_mode)
14300 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14301 ret = convert_descriptor_to_mode (int_mode, ret);
14302 return ret;
14303 }
14304
14305 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14306 but after converting arguments to type_die, afterwards
14307 convert back to unsigned. */
14308
14309 static dw_loc_descr_ref
14310 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14311 scalar_int_mode mode, machine_mode mem_mode)
14312 {
14313 dw_loc_descr_ref cvt, op0, op1;
14314
14315 if (type_die == NULL)
14316 return NULL;
14317 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14318 VAR_INIT_STATUS_INITIALIZED);
14319 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14320 VAR_INIT_STATUS_INITIALIZED);
14321 if (op0 == NULL || op1 == NULL)
14322 return NULL;
14323 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14324 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14325 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14326 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14327 add_loc_descr (&op0, cvt);
14328 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14329 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14330 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14331 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14332 add_loc_descr (&op1, cvt);
14333 add_loc_descr (&op0, op1);
14334 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14335 return convert_descriptor_to_mode (mode, op0);
14336 }
14337
14338 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14339 const0 is DW_OP_lit0 or corresponding typed constant,
14340 const1 is DW_OP_lit1 or corresponding typed constant
14341 and constMSB is constant with just the MSB bit set
14342 for the mode):
14343 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14344 L1: const0 DW_OP_swap
14345 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14346 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14347 L3: DW_OP_drop
14348 L4: DW_OP_nop
14349
14350 CTZ is similar:
14351 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14352 L1: const0 DW_OP_swap
14353 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14354 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14355 L3: DW_OP_drop
14356 L4: DW_OP_nop
14357
14358 FFS is similar:
14359 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14360 L1: const1 DW_OP_swap
14361 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14362 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14363 L3: DW_OP_drop
14364 L4: DW_OP_nop */
14365
14366 static dw_loc_descr_ref
14367 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14368 machine_mode mem_mode)
14369 {
14370 dw_loc_descr_ref op0, ret, tmp;
14371 HOST_WIDE_INT valv;
14372 dw_loc_descr_ref l1jump, l1label;
14373 dw_loc_descr_ref l2jump, l2label;
14374 dw_loc_descr_ref l3jump, l3label;
14375 dw_loc_descr_ref l4jump, l4label;
14376 rtx msb;
14377
14378 if (GET_MODE (XEXP (rtl, 0)) != mode)
14379 return NULL;
14380
14381 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14382 VAR_INIT_STATUS_INITIALIZED);
14383 if (op0 == NULL)
14384 return NULL;
14385 ret = op0;
14386 if (GET_CODE (rtl) == CLZ)
14387 {
14388 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14389 valv = GET_MODE_BITSIZE (mode);
14390 }
14391 else if (GET_CODE (rtl) == FFS)
14392 valv = 0;
14393 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14394 valv = GET_MODE_BITSIZE (mode);
14395 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14396 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14397 add_loc_descr (&ret, l1jump);
14398 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14399 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14400 VAR_INIT_STATUS_INITIALIZED);
14401 if (tmp == NULL)
14402 return NULL;
14403 add_loc_descr (&ret, tmp);
14404 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14405 add_loc_descr (&ret, l4jump);
14406 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14407 ? const1_rtx : const0_rtx,
14408 mode, mem_mode,
14409 VAR_INIT_STATUS_INITIALIZED);
14410 if (l1label == NULL)
14411 return NULL;
14412 add_loc_descr (&ret, l1label);
14413 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14414 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14415 add_loc_descr (&ret, l2label);
14416 if (GET_CODE (rtl) != CLZ)
14417 msb = const1_rtx;
14418 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14419 msb = GEN_INT (HOST_WIDE_INT_1U
14420 << (GET_MODE_BITSIZE (mode) - 1));
14421 else
14422 msb = immed_wide_int_const
14423 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14424 GET_MODE_PRECISION (mode)), mode);
14425 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14426 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14427 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14428 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14429 else
14430 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14431 VAR_INIT_STATUS_INITIALIZED);
14432 if (tmp == NULL)
14433 return NULL;
14434 add_loc_descr (&ret, tmp);
14435 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14436 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14437 add_loc_descr (&ret, l3jump);
14438 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14439 VAR_INIT_STATUS_INITIALIZED);
14440 if (tmp == NULL)
14441 return NULL;
14442 add_loc_descr (&ret, tmp);
14443 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14444 ? DW_OP_shl : DW_OP_shr, 0, 0));
14445 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14446 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14447 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14448 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14449 add_loc_descr (&ret, l2jump);
14450 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14451 add_loc_descr (&ret, l3label);
14452 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14453 add_loc_descr (&ret, l4label);
14454 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14455 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14456 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14457 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14458 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14459 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14460 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14461 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14462 return ret;
14463 }
14464
14465 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14466 const1 is DW_OP_lit1 or corresponding typed constant):
14467 const0 DW_OP_swap
14468 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14469 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14470 L2: DW_OP_drop
14471
14472 PARITY is similar:
14473 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14474 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14475 L2: DW_OP_drop */
14476
14477 static dw_loc_descr_ref
14478 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14479 machine_mode mem_mode)
14480 {
14481 dw_loc_descr_ref op0, ret, tmp;
14482 dw_loc_descr_ref l1jump, l1label;
14483 dw_loc_descr_ref l2jump, l2label;
14484
14485 if (GET_MODE (XEXP (rtl, 0)) != mode)
14486 return NULL;
14487
14488 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14489 VAR_INIT_STATUS_INITIALIZED);
14490 if (op0 == NULL)
14491 return NULL;
14492 ret = op0;
14493 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14494 VAR_INIT_STATUS_INITIALIZED);
14495 if (tmp == NULL)
14496 return NULL;
14497 add_loc_descr (&ret, tmp);
14498 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14499 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14500 add_loc_descr (&ret, l1label);
14501 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14502 add_loc_descr (&ret, l2jump);
14503 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14505 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14506 VAR_INIT_STATUS_INITIALIZED);
14507 if (tmp == NULL)
14508 return NULL;
14509 add_loc_descr (&ret, tmp);
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14511 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14512 ? DW_OP_plus : DW_OP_xor, 0, 0));
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14514 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14515 VAR_INIT_STATUS_INITIALIZED);
14516 add_loc_descr (&ret, tmp);
14517 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14518 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14519 add_loc_descr (&ret, l1jump);
14520 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14521 add_loc_descr (&ret, l2label);
14522 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14523 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14524 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14525 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14526 return ret;
14527 }
14528
14529 /* BSWAP (constS is initial shift count, either 56 or 24):
14530 constS const0
14531 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14532 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14533 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14534 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14535 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14536
14537 static dw_loc_descr_ref
14538 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14539 machine_mode mem_mode)
14540 {
14541 dw_loc_descr_ref op0, ret, tmp;
14542 dw_loc_descr_ref l1jump, l1label;
14543 dw_loc_descr_ref l2jump, l2label;
14544
14545 if (BITS_PER_UNIT != 8
14546 || (GET_MODE_BITSIZE (mode) != 32
14547 && GET_MODE_BITSIZE (mode) != 64))
14548 return NULL;
14549
14550 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14551 VAR_INIT_STATUS_INITIALIZED);
14552 if (op0 == NULL)
14553 return NULL;
14554
14555 ret = op0;
14556 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14557 mode, mem_mode,
14558 VAR_INIT_STATUS_INITIALIZED);
14559 if (tmp == NULL)
14560 return NULL;
14561 add_loc_descr (&ret, tmp);
14562 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14563 VAR_INIT_STATUS_INITIALIZED);
14564 if (tmp == NULL)
14565 return NULL;
14566 add_loc_descr (&ret, tmp);
14567 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14568 add_loc_descr (&ret, l1label);
14569 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14570 mode, mem_mode,
14571 VAR_INIT_STATUS_INITIALIZED);
14572 add_loc_descr (&ret, tmp);
14573 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14574 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14575 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14576 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14577 VAR_INIT_STATUS_INITIALIZED);
14578 if (tmp == NULL)
14579 return NULL;
14580 add_loc_descr (&ret, tmp);
14581 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14582 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14583 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14584 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14585 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14586 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14587 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14588 VAR_INIT_STATUS_INITIALIZED);
14589 add_loc_descr (&ret, tmp);
14590 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14591 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14592 add_loc_descr (&ret, l2jump);
14593 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14594 VAR_INIT_STATUS_INITIALIZED);
14595 add_loc_descr (&ret, tmp);
14596 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14597 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14598 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14599 add_loc_descr (&ret, l1jump);
14600 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14601 add_loc_descr (&ret, l2label);
14602 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14603 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14604 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14605 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14606 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14607 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14608 return ret;
14609 }
14610
14611 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14612 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14613 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14614 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14615
14616 ROTATERT is similar:
14617 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14618 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14619 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14620
14621 static dw_loc_descr_ref
14622 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14623 machine_mode mem_mode)
14624 {
14625 rtx rtlop1 = XEXP (rtl, 1);
14626 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14627 int i;
14628
14629 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14630 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14631 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14632 VAR_INIT_STATUS_INITIALIZED);
14633 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14634 VAR_INIT_STATUS_INITIALIZED);
14635 if (op0 == NULL || op1 == NULL)
14636 return NULL;
14637 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14638 for (i = 0; i < 2; i++)
14639 {
14640 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14641 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14642 mode, mem_mode,
14643 VAR_INIT_STATUS_INITIALIZED);
14644 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14645 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14646 ? DW_OP_const4u
14647 : HOST_BITS_PER_WIDE_INT == 64
14648 ? DW_OP_const8u : DW_OP_constu,
14649 GET_MODE_MASK (mode), 0);
14650 else
14651 mask[i] = NULL;
14652 if (mask[i] == NULL)
14653 return NULL;
14654 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14655 }
14656 ret = op0;
14657 add_loc_descr (&ret, op1);
14658 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14659 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14660 if (GET_CODE (rtl) == ROTATERT)
14661 {
14662 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14663 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14664 GET_MODE_BITSIZE (mode), 0));
14665 }
14666 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14667 if (mask[0] != NULL)
14668 add_loc_descr (&ret, mask[0]);
14669 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14670 if (mask[1] != NULL)
14671 {
14672 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14673 add_loc_descr (&ret, mask[1]);
14674 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14675 }
14676 if (GET_CODE (rtl) == ROTATE)
14677 {
14678 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14679 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14680 GET_MODE_BITSIZE (mode), 0));
14681 }
14682 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14683 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14684 return ret;
14685 }
14686
14687 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14688 for DEBUG_PARAMETER_REF RTL. */
14689
14690 static dw_loc_descr_ref
14691 parameter_ref_descriptor (rtx rtl)
14692 {
14693 dw_loc_descr_ref ret;
14694 dw_die_ref ref;
14695
14696 if (dwarf_strict)
14697 return NULL;
14698 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14699 /* With LTO during LTRANS we get the late DIE that refers to the early
14700 DIE, thus we add another indirection here. This seems to confuse
14701 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14702 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14703 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14704 if (ref)
14705 {
14706 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14707 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14708 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14709 }
14710 else
14711 {
14712 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14713 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14714 }
14715 return ret;
14716 }
14717
14718 /* The following routine converts the RTL for a variable or parameter
14719 (resident in memory) into an equivalent Dwarf representation of a
14720 mechanism for getting the address of that same variable onto the top of a
14721 hypothetical "address evaluation" stack.
14722
14723 When creating memory location descriptors, we are effectively transforming
14724 the RTL for a memory-resident object into its Dwarf postfix expression
14725 equivalent. This routine recursively descends an RTL tree, turning
14726 it into Dwarf postfix code as it goes.
14727
14728 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14729
14730 MEM_MODE is the mode of the memory reference, needed to handle some
14731 autoincrement addressing modes.
14732
14733 Return 0 if we can't represent the location. */
14734
14735 dw_loc_descr_ref
14736 mem_loc_descriptor (rtx rtl, machine_mode mode,
14737 machine_mode mem_mode,
14738 enum var_init_status initialized)
14739 {
14740 dw_loc_descr_ref mem_loc_result = NULL;
14741 enum dwarf_location_atom op;
14742 dw_loc_descr_ref op0, op1;
14743 rtx inner = NULL_RTX;
14744 poly_int64 offset;
14745
14746 if (mode == VOIDmode)
14747 mode = GET_MODE (rtl);
14748
14749 /* Note that for a dynamically sized array, the location we will generate a
14750 description of here will be the lowest numbered location which is
14751 actually within the array. That's *not* necessarily the same as the
14752 zeroth element of the array. */
14753
14754 rtl = targetm.delegitimize_address (rtl);
14755
14756 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14757 return NULL;
14758
14759 scalar_int_mode int_mode, inner_mode, op1_mode;
14760 switch (GET_CODE (rtl))
14761 {
14762 case POST_INC:
14763 case POST_DEC:
14764 case POST_MODIFY:
14765 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14766
14767 case SUBREG:
14768 /* The case of a subreg may arise when we have a local (register)
14769 variable or a formal (register) parameter which doesn't quite fill
14770 up an entire register. For now, just assume that it is
14771 legitimate to make the Dwarf info refer to the whole register which
14772 contains the given subreg. */
14773 if (!subreg_lowpart_p (rtl))
14774 break;
14775 inner = SUBREG_REG (rtl);
14776 /* FALLTHRU */
14777 case TRUNCATE:
14778 if (inner == NULL_RTX)
14779 inner = XEXP (rtl, 0);
14780 if (is_a <scalar_int_mode> (mode, &int_mode)
14781 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14782 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14783 #ifdef POINTERS_EXTEND_UNSIGNED
14784 || (int_mode == Pmode && mem_mode != VOIDmode)
14785 #endif
14786 )
14787 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14788 {
14789 mem_loc_result = mem_loc_descriptor (inner,
14790 inner_mode,
14791 mem_mode, initialized);
14792 break;
14793 }
14794 if (dwarf_strict && dwarf_version < 5)
14795 break;
14796 if (is_a <scalar_int_mode> (mode, &int_mode)
14797 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14798 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14799 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14800 {
14801 dw_die_ref type_die;
14802 dw_loc_descr_ref cvt;
14803
14804 mem_loc_result = mem_loc_descriptor (inner,
14805 GET_MODE (inner),
14806 mem_mode, initialized);
14807 if (mem_loc_result == NULL)
14808 break;
14809 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14810 if (type_die == NULL)
14811 {
14812 mem_loc_result = NULL;
14813 break;
14814 }
14815 if (GET_MODE_SIZE (mode)
14816 != GET_MODE_SIZE (GET_MODE (inner)))
14817 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14818 else
14819 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14820 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14821 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14822 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14823 add_loc_descr (&mem_loc_result, cvt);
14824 if (is_a <scalar_int_mode> (mode, &int_mode)
14825 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14826 {
14827 /* Convert it to untyped afterwards. */
14828 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14829 add_loc_descr (&mem_loc_result, cvt);
14830 }
14831 }
14832 break;
14833
14834 case REG:
14835 if (!is_a <scalar_int_mode> (mode, &int_mode)
14836 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14837 && rtl != arg_pointer_rtx
14838 && rtl != frame_pointer_rtx
14839 #ifdef POINTERS_EXTEND_UNSIGNED
14840 && (int_mode != Pmode || mem_mode == VOIDmode)
14841 #endif
14842 ))
14843 {
14844 dw_die_ref type_die;
14845 unsigned int dbx_regnum;
14846
14847 if (dwarf_strict && dwarf_version < 5)
14848 break;
14849 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14850 break;
14851 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14852 if (type_die == NULL)
14853 break;
14854
14855 dbx_regnum = dbx_reg_number (rtl);
14856 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14857 break;
14858 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14859 dbx_regnum, 0);
14860 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14861 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14862 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14863 break;
14864 }
14865 /* Whenever a register number forms a part of the description of the
14866 method for calculating the (dynamic) address of a memory resident
14867 object, DWARF rules require the register number be referred to as
14868 a "base register". This distinction is not based in any way upon
14869 what category of register the hardware believes the given register
14870 belongs to. This is strictly DWARF terminology we're dealing with
14871 here. Note that in cases where the location of a memory-resident
14872 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14873 OP_CONST (0)) the actual DWARF location descriptor that we generate
14874 may just be OP_BASEREG (basereg). This may look deceptively like
14875 the object in question was allocated to a register (rather than in
14876 memory) so DWARF consumers need to be aware of the subtle
14877 distinction between OP_REG and OP_BASEREG. */
14878 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14879 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14880 else if (stack_realign_drap
14881 && crtl->drap_reg
14882 && crtl->args.internal_arg_pointer == rtl
14883 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14884 {
14885 /* If RTL is internal_arg_pointer, which has been optimized
14886 out, use DRAP instead. */
14887 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14888 VAR_INIT_STATUS_INITIALIZED);
14889 }
14890 break;
14891
14892 case SIGN_EXTEND:
14893 case ZERO_EXTEND:
14894 if (!is_a <scalar_int_mode> (mode, &int_mode)
14895 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14896 break;
14897 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14898 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14899 if (op0 == 0)
14900 break;
14901 else if (GET_CODE (rtl) == ZERO_EXTEND
14902 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14903 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14904 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14905 to expand zero extend as two shifts instead of
14906 masking. */
14907 && GET_MODE_SIZE (inner_mode) <= 4)
14908 {
14909 mem_loc_result = op0;
14910 add_loc_descr (&mem_loc_result,
14911 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14912 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14913 }
14914 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14915 {
14916 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14917 shift *= BITS_PER_UNIT;
14918 if (GET_CODE (rtl) == SIGN_EXTEND)
14919 op = DW_OP_shra;
14920 else
14921 op = DW_OP_shr;
14922 mem_loc_result = op0;
14923 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14924 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14925 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14926 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14927 }
14928 else if (!dwarf_strict || dwarf_version >= 5)
14929 {
14930 dw_die_ref type_die1, type_die2;
14931 dw_loc_descr_ref cvt;
14932
14933 type_die1 = base_type_for_mode (inner_mode,
14934 GET_CODE (rtl) == ZERO_EXTEND);
14935 if (type_die1 == NULL)
14936 break;
14937 type_die2 = base_type_for_mode (int_mode, 1);
14938 if (type_die2 == NULL)
14939 break;
14940 mem_loc_result = op0;
14941 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14942 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14943 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14944 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14945 add_loc_descr (&mem_loc_result, cvt);
14946 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14947 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14948 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14949 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14950 add_loc_descr (&mem_loc_result, cvt);
14951 }
14952 break;
14953
14954 case MEM:
14955 {
14956 rtx new_rtl = avoid_constant_pool_reference (rtl);
14957 if (new_rtl != rtl)
14958 {
14959 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14960 initialized);
14961 if (mem_loc_result != NULL)
14962 return mem_loc_result;
14963 }
14964 }
14965 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14966 get_address_mode (rtl), mode,
14967 VAR_INIT_STATUS_INITIALIZED);
14968 if (mem_loc_result == NULL)
14969 mem_loc_result = tls_mem_loc_descriptor (rtl);
14970 if (mem_loc_result != NULL)
14971 {
14972 if (!is_a <scalar_int_mode> (mode, &int_mode)
14973 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14974 {
14975 dw_die_ref type_die;
14976 dw_loc_descr_ref deref;
14977
14978 if (dwarf_strict && dwarf_version < 5)
14979 return NULL;
14980 type_die
14981 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14982 if (type_die == NULL)
14983 return NULL;
14984 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14985 GET_MODE_SIZE (mode), 0);
14986 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14987 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14988 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14989 add_loc_descr (&mem_loc_result, deref);
14990 }
14991 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14992 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14993 else
14994 add_loc_descr (&mem_loc_result,
14995 new_loc_descr (DW_OP_deref_size,
14996 GET_MODE_SIZE (int_mode), 0));
14997 }
14998 break;
14999
15000 case LO_SUM:
15001 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15002
15003 case LABEL_REF:
15004 /* Some ports can transform a symbol ref into a label ref, because
15005 the symbol ref is too far away and has to be dumped into a constant
15006 pool. */
15007 case CONST:
15008 case SYMBOL_REF:
15009 if (!is_a <scalar_int_mode> (mode, &int_mode)
15010 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15011 #ifdef POINTERS_EXTEND_UNSIGNED
15012 && (int_mode != Pmode || mem_mode == VOIDmode)
15013 #endif
15014 ))
15015 break;
15016 if (GET_CODE (rtl) == SYMBOL_REF
15017 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15018 {
15019 dw_loc_descr_ref temp;
15020
15021 /* If this is not defined, we have no way to emit the data. */
15022 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15023 break;
15024
15025 temp = new_addr_loc_descr (rtl, dtprel_true);
15026
15027 /* We check for DWARF 5 here because gdb did not implement
15028 DW_OP_form_tls_address until after 7.12. */
15029 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15030 ? DW_OP_form_tls_address
15031 : DW_OP_GNU_push_tls_address),
15032 0, 0);
15033 add_loc_descr (&mem_loc_result, temp);
15034
15035 break;
15036 }
15037
15038 if (!const_ok_for_output (rtl))
15039 {
15040 if (GET_CODE (rtl) == CONST)
15041 switch (GET_CODE (XEXP (rtl, 0)))
15042 {
15043 case NOT:
15044 op = DW_OP_not;
15045 goto try_const_unop;
15046 case NEG:
15047 op = DW_OP_neg;
15048 goto try_const_unop;
15049 try_const_unop:
15050 rtx arg;
15051 arg = XEXP (XEXP (rtl, 0), 0);
15052 if (!CONSTANT_P (arg))
15053 arg = gen_rtx_CONST (int_mode, arg);
15054 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15055 initialized);
15056 if (op0)
15057 {
15058 mem_loc_result = op0;
15059 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15060 }
15061 break;
15062 default:
15063 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15064 mem_mode, initialized);
15065 break;
15066 }
15067 break;
15068 }
15069
15070 symref:
15071 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15072 vec_safe_push (used_rtx_array, rtl);
15073 break;
15074
15075 case CONCAT:
15076 case CONCATN:
15077 case VAR_LOCATION:
15078 case DEBUG_IMPLICIT_PTR:
15079 expansion_failed (NULL_TREE, rtl,
15080 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15081 return 0;
15082
15083 case ENTRY_VALUE:
15084 if (dwarf_strict && dwarf_version < 5)
15085 return NULL;
15086 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15087 {
15088 if (!is_a <scalar_int_mode> (mode, &int_mode)
15089 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15090 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15091 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15092 else
15093 {
15094 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15095 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15096 return NULL;
15097 op0 = one_reg_loc_descriptor (dbx_regnum,
15098 VAR_INIT_STATUS_INITIALIZED);
15099 }
15100 }
15101 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15102 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15103 {
15104 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15105 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15106 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15107 return NULL;
15108 }
15109 else
15110 gcc_unreachable ();
15111 if (op0 == NULL)
15112 return NULL;
15113 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15114 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15115 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15116 break;
15117
15118 case DEBUG_PARAMETER_REF:
15119 mem_loc_result = parameter_ref_descriptor (rtl);
15120 break;
15121
15122 case PRE_MODIFY:
15123 /* Extract the PLUS expression nested inside and fall into
15124 PLUS code below. */
15125 rtl = XEXP (rtl, 1);
15126 goto plus;
15127
15128 case PRE_INC:
15129 case PRE_DEC:
15130 /* Turn these into a PLUS expression and fall into the PLUS code
15131 below. */
15132 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15133 gen_int_mode (GET_CODE (rtl) == PRE_INC
15134 ? GET_MODE_UNIT_SIZE (mem_mode)
15135 : -GET_MODE_UNIT_SIZE (mem_mode),
15136 mode));
15137
15138 /* fall through */
15139
15140 case PLUS:
15141 plus:
15142 if (is_based_loc (rtl)
15143 && is_a <scalar_int_mode> (mode, &int_mode)
15144 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15145 || XEXP (rtl, 0) == arg_pointer_rtx
15146 || XEXP (rtl, 0) == frame_pointer_rtx))
15147 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15148 INTVAL (XEXP (rtl, 1)),
15149 VAR_INIT_STATUS_INITIALIZED);
15150 else
15151 {
15152 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15153 VAR_INIT_STATUS_INITIALIZED);
15154 if (mem_loc_result == 0)
15155 break;
15156
15157 if (CONST_INT_P (XEXP (rtl, 1))
15158 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15159 <= DWARF2_ADDR_SIZE))
15160 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15161 else
15162 {
15163 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15164 VAR_INIT_STATUS_INITIALIZED);
15165 if (op1 == 0)
15166 return NULL;
15167 add_loc_descr (&mem_loc_result, op1);
15168 add_loc_descr (&mem_loc_result,
15169 new_loc_descr (DW_OP_plus, 0, 0));
15170 }
15171 }
15172 break;
15173
15174 /* If a pseudo-reg is optimized away, it is possible for it to
15175 be replaced with a MEM containing a multiply or shift. */
15176 case MINUS:
15177 op = DW_OP_minus;
15178 goto do_binop;
15179
15180 case MULT:
15181 op = DW_OP_mul;
15182 goto do_binop;
15183
15184 case DIV:
15185 if ((!dwarf_strict || dwarf_version >= 5)
15186 && is_a <scalar_int_mode> (mode, &int_mode)
15187 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15188 {
15189 mem_loc_result = typed_binop (DW_OP_div, rtl,
15190 base_type_for_mode (mode, 0),
15191 int_mode, mem_mode);
15192 break;
15193 }
15194 op = DW_OP_div;
15195 goto do_binop;
15196
15197 case UMOD:
15198 op = DW_OP_mod;
15199 goto do_binop;
15200
15201 case ASHIFT:
15202 op = DW_OP_shl;
15203 goto do_shift;
15204
15205 case ASHIFTRT:
15206 op = DW_OP_shra;
15207 goto do_shift;
15208
15209 case LSHIFTRT:
15210 op = DW_OP_shr;
15211 goto do_shift;
15212
15213 do_shift:
15214 if (!is_a <scalar_int_mode> (mode, &int_mode))
15215 break;
15216 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15217 VAR_INIT_STATUS_INITIALIZED);
15218 {
15219 rtx rtlop1 = XEXP (rtl, 1);
15220 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15221 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15222 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15223 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15224 VAR_INIT_STATUS_INITIALIZED);
15225 }
15226
15227 if (op0 == 0 || op1 == 0)
15228 break;
15229
15230 mem_loc_result = op0;
15231 add_loc_descr (&mem_loc_result, op1);
15232 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15233 break;
15234
15235 case AND:
15236 op = DW_OP_and;
15237 goto do_binop;
15238
15239 case IOR:
15240 op = DW_OP_or;
15241 goto do_binop;
15242
15243 case XOR:
15244 op = DW_OP_xor;
15245 goto do_binop;
15246
15247 do_binop:
15248 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15249 VAR_INIT_STATUS_INITIALIZED);
15250 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15251 VAR_INIT_STATUS_INITIALIZED);
15252
15253 if (op0 == 0 || op1 == 0)
15254 break;
15255
15256 mem_loc_result = op0;
15257 add_loc_descr (&mem_loc_result, op1);
15258 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15259 break;
15260
15261 case MOD:
15262 if ((!dwarf_strict || dwarf_version >= 5)
15263 && is_a <scalar_int_mode> (mode, &int_mode)
15264 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15265 {
15266 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15267 base_type_for_mode (mode, 0),
15268 int_mode, mem_mode);
15269 break;
15270 }
15271
15272 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15273 VAR_INIT_STATUS_INITIALIZED);
15274 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15275 VAR_INIT_STATUS_INITIALIZED);
15276
15277 if (op0 == 0 || op1 == 0)
15278 break;
15279
15280 mem_loc_result = op0;
15281 add_loc_descr (&mem_loc_result, op1);
15282 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15283 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15284 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15285 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15286 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15287 break;
15288
15289 case UDIV:
15290 if ((!dwarf_strict || dwarf_version >= 5)
15291 && is_a <scalar_int_mode> (mode, &int_mode))
15292 {
15293 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15294 {
15295 op = DW_OP_div;
15296 goto do_binop;
15297 }
15298 mem_loc_result = typed_binop (DW_OP_div, rtl,
15299 base_type_for_mode (int_mode, 1),
15300 int_mode, mem_mode);
15301 }
15302 break;
15303
15304 case NOT:
15305 op = DW_OP_not;
15306 goto do_unop;
15307
15308 case ABS:
15309 op = DW_OP_abs;
15310 goto do_unop;
15311
15312 case NEG:
15313 op = DW_OP_neg;
15314 goto do_unop;
15315
15316 do_unop:
15317 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15318 VAR_INIT_STATUS_INITIALIZED);
15319
15320 if (op0 == 0)
15321 break;
15322
15323 mem_loc_result = op0;
15324 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15325 break;
15326
15327 case CONST_INT:
15328 if (!is_a <scalar_int_mode> (mode, &int_mode)
15329 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15330 #ifdef POINTERS_EXTEND_UNSIGNED
15331 || (int_mode == Pmode
15332 && mem_mode != VOIDmode
15333 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15334 #endif
15335 )
15336 {
15337 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15338 break;
15339 }
15340 if ((!dwarf_strict || dwarf_version >= 5)
15341 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15342 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15343 {
15344 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15345 scalar_int_mode amode;
15346 if (type_die == NULL)
15347 return NULL;
15348 if (INTVAL (rtl) >= 0
15349 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15350 .exists (&amode))
15351 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15352 /* const DW_OP_convert <XXX> vs.
15353 DW_OP_const_type <XXX, 1, const>. */
15354 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15355 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15356 {
15357 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15358 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15359 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15360 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15361 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15362 add_loc_descr (&mem_loc_result, op0);
15363 return mem_loc_result;
15364 }
15365 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15366 INTVAL (rtl));
15367 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15368 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15369 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15370 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15371 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15372 else
15373 {
15374 mem_loc_result->dw_loc_oprnd2.val_class
15375 = dw_val_class_const_double;
15376 mem_loc_result->dw_loc_oprnd2.v.val_double
15377 = double_int::from_shwi (INTVAL (rtl));
15378 }
15379 }
15380 break;
15381
15382 case CONST_DOUBLE:
15383 if (!dwarf_strict || dwarf_version >= 5)
15384 {
15385 dw_die_ref type_die;
15386
15387 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15388 CONST_DOUBLE rtx could represent either a large integer
15389 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15390 the value is always a floating point constant.
15391
15392 When it is an integer, a CONST_DOUBLE is used whenever
15393 the constant requires 2 HWIs to be adequately represented.
15394 We output CONST_DOUBLEs as blocks. */
15395 if (mode == VOIDmode
15396 || (GET_MODE (rtl) == VOIDmode
15397 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15398 break;
15399 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15400 if (type_die == NULL)
15401 return NULL;
15402 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15403 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15404 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15405 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15406 #if TARGET_SUPPORTS_WIDE_INT == 0
15407 if (!SCALAR_FLOAT_MODE_P (mode))
15408 {
15409 mem_loc_result->dw_loc_oprnd2.val_class
15410 = dw_val_class_const_double;
15411 mem_loc_result->dw_loc_oprnd2.v.val_double
15412 = rtx_to_double_int (rtl);
15413 }
15414 else
15415 #endif
15416 {
15417 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15418 unsigned int length = GET_MODE_SIZE (float_mode);
15419 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15420
15421 insert_float (rtl, array);
15422 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15423 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15424 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15425 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15426 }
15427 }
15428 break;
15429
15430 case CONST_WIDE_INT:
15431 if (!dwarf_strict || dwarf_version >= 5)
15432 {
15433 dw_die_ref type_die;
15434
15435 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15436 if (type_die == NULL)
15437 return NULL;
15438 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15439 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15440 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15441 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15442 mem_loc_result->dw_loc_oprnd2.val_class
15443 = dw_val_class_wide_int;
15444 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15445 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15446 }
15447 break;
15448
15449 case CONST_POLY_INT:
15450 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
15451 break;
15452
15453 case EQ:
15454 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15455 break;
15456
15457 case GE:
15458 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15459 break;
15460
15461 case GT:
15462 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15463 break;
15464
15465 case LE:
15466 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15467 break;
15468
15469 case LT:
15470 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15471 break;
15472
15473 case NE:
15474 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15475 break;
15476
15477 case GEU:
15478 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15479 break;
15480
15481 case GTU:
15482 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15483 break;
15484
15485 case LEU:
15486 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15487 break;
15488
15489 case LTU:
15490 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15491 break;
15492
15493 case UMIN:
15494 case UMAX:
15495 if (!SCALAR_INT_MODE_P (mode))
15496 break;
15497 /* FALLTHRU */
15498 case SMIN:
15499 case SMAX:
15500 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15501 break;
15502
15503 case ZERO_EXTRACT:
15504 case SIGN_EXTRACT:
15505 if (CONST_INT_P (XEXP (rtl, 1))
15506 && CONST_INT_P (XEXP (rtl, 2))
15507 && is_a <scalar_int_mode> (mode, &int_mode)
15508 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15509 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15510 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15511 && ((unsigned) INTVAL (XEXP (rtl, 1))
15512 + (unsigned) INTVAL (XEXP (rtl, 2))
15513 <= GET_MODE_BITSIZE (int_mode)))
15514 {
15515 int shift, size;
15516 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15517 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15518 if (op0 == 0)
15519 break;
15520 if (GET_CODE (rtl) == SIGN_EXTRACT)
15521 op = DW_OP_shra;
15522 else
15523 op = DW_OP_shr;
15524 mem_loc_result = op0;
15525 size = INTVAL (XEXP (rtl, 1));
15526 shift = INTVAL (XEXP (rtl, 2));
15527 if (BITS_BIG_ENDIAN)
15528 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15529 if (shift + size != (int) DWARF2_ADDR_SIZE)
15530 {
15531 add_loc_descr (&mem_loc_result,
15532 int_loc_descriptor (DWARF2_ADDR_SIZE
15533 - shift - size));
15534 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15535 }
15536 if (size != (int) DWARF2_ADDR_SIZE)
15537 {
15538 add_loc_descr (&mem_loc_result,
15539 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15540 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15541 }
15542 }
15543 break;
15544
15545 case IF_THEN_ELSE:
15546 {
15547 dw_loc_descr_ref op2, bra_node, drop_node;
15548 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15549 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15550 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15551 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15552 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15553 VAR_INIT_STATUS_INITIALIZED);
15554 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15555 VAR_INIT_STATUS_INITIALIZED);
15556 if (op0 == NULL || op1 == NULL || op2 == NULL)
15557 break;
15558
15559 mem_loc_result = op1;
15560 add_loc_descr (&mem_loc_result, op2);
15561 add_loc_descr (&mem_loc_result, op0);
15562 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15563 add_loc_descr (&mem_loc_result, bra_node);
15564 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15565 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15566 add_loc_descr (&mem_loc_result, drop_node);
15567 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15568 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15569 }
15570 break;
15571
15572 case FLOAT_EXTEND:
15573 case FLOAT_TRUNCATE:
15574 case FLOAT:
15575 case UNSIGNED_FLOAT:
15576 case FIX:
15577 case UNSIGNED_FIX:
15578 if (!dwarf_strict || dwarf_version >= 5)
15579 {
15580 dw_die_ref type_die;
15581 dw_loc_descr_ref cvt;
15582
15583 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15584 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15585 if (op0 == NULL)
15586 break;
15587 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15588 && (GET_CODE (rtl) == FLOAT
15589 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15590 {
15591 type_die = base_type_for_mode (int_mode,
15592 GET_CODE (rtl) == UNSIGNED_FLOAT);
15593 if (type_die == NULL)
15594 break;
15595 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15596 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15597 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15598 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15599 add_loc_descr (&op0, cvt);
15600 }
15601 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15602 if (type_die == NULL)
15603 break;
15604 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15605 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15606 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15607 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15608 add_loc_descr (&op0, cvt);
15609 if (is_a <scalar_int_mode> (mode, &int_mode)
15610 && (GET_CODE (rtl) == FIX
15611 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15612 {
15613 op0 = convert_descriptor_to_mode (int_mode, op0);
15614 if (op0 == NULL)
15615 break;
15616 }
15617 mem_loc_result = op0;
15618 }
15619 break;
15620
15621 case CLZ:
15622 case CTZ:
15623 case FFS:
15624 if (is_a <scalar_int_mode> (mode, &int_mode))
15625 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15626 break;
15627
15628 case POPCOUNT:
15629 case PARITY:
15630 if (is_a <scalar_int_mode> (mode, &int_mode))
15631 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15632 break;
15633
15634 case BSWAP:
15635 if (is_a <scalar_int_mode> (mode, &int_mode))
15636 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15637 break;
15638
15639 case ROTATE:
15640 case ROTATERT:
15641 if (is_a <scalar_int_mode> (mode, &int_mode))
15642 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15643 break;
15644
15645 case COMPARE:
15646 /* In theory, we could implement the above. */
15647 /* DWARF cannot represent the unsigned compare operations
15648 natively. */
15649 case SS_MULT:
15650 case US_MULT:
15651 case SS_DIV:
15652 case US_DIV:
15653 case SS_PLUS:
15654 case US_PLUS:
15655 case SS_MINUS:
15656 case US_MINUS:
15657 case SS_NEG:
15658 case US_NEG:
15659 case SS_ABS:
15660 case SS_ASHIFT:
15661 case US_ASHIFT:
15662 case SS_TRUNCATE:
15663 case US_TRUNCATE:
15664 case UNORDERED:
15665 case ORDERED:
15666 case UNEQ:
15667 case UNGE:
15668 case UNGT:
15669 case UNLE:
15670 case UNLT:
15671 case LTGT:
15672 case FRACT_CONVERT:
15673 case UNSIGNED_FRACT_CONVERT:
15674 case SAT_FRACT:
15675 case UNSIGNED_SAT_FRACT:
15676 case SQRT:
15677 case ASM_OPERANDS:
15678 case VEC_MERGE:
15679 case VEC_SELECT:
15680 case VEC_CONCAT:
15681 case VEC_DUPLICATE:
15682 case VEC_SERIES:
15683 case UNSPEC:
15684 case HIGH:
15685 case FMA:
15686 case STRICT_LOW_PART:
15687 case CONST_VECTOR:
15688 case CONST_FIXED:
15689 case CLRSB:
15690 case CLOBBER:
15691 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15692 can't express it in the debug info. This can happen e.g. with some
15693 TLS UNSPECs. */
15694 break;
15695
15696 case CONST_STRING:
15697 resolve_one_addr (&rtl);
15698 goto symref;
15699
15700 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15701 the expression. An UNSPEC rtx represents a raw DWARF operation,
15702 new_loc_descr is called for it to build the operation directly.
15703 Otherwise mem_loc_descriptor is called recursively. */
15704 case PARALLEL:
15705 {
15706 int index = 0;
15707 dw_loc_descr_ref exp_result = NULL;
15708
15709 for (; index < XVECLEN (rtl, 0); index++)
15710 {
15711 rtx elem = XVECEXP (rtl, 0, index);
15712 if (GET_CODE (elem) == UNSPEC)
15713 {
15714 /* Each DWARF operation UNSPEC contain two operands, if
15715 one operand is not used for the operation, const0_rtx is
15716 passed. */
15717 gcc_assert (XVECLEN (elem, 0) == 2);
15718
15719 HOST_WIDE_INT dw_op = XINT (elem, 1);
15720 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15721 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15722 exp_result
15723 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15724 oprnd2);
15725 }
15726 else
15727 exp_result
15728 = mem_loc_descriptor (elem, mode, mem_mode,
15729 VAR_INIT_STATUS_INITIALIZED);
15730
15731 if (!mem_loc_result)
15732 mem_loc_result = exp_result;
15733 else
15734 add_loc_descr (&mem_loc_result, exp_result);
15735 }
15736
15737 break;
15738 }
15739
15740 default:
15741 if (flag_checking)
15742 {
15743 print_rtl (stderr, rtl);
15744 gcc_unreachable ();
15745 }
15746 break;
15747 }
15748
15749 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15750 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15751
15752 return mem_loc_result;
15753 }
15754
15755 /* Return a descriptor that describes the concatenation of two locations.
15756 This is typically a complex variable. */
15757
15758 static dw_loc_descr_ref
15759 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15760 {
15761 dw_loc_descr_ref cc_loc_result = NULL;
15762 dw_loc_descr_ref x0_ref
15763 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15764 dw_loc_descr_ref x1_ref
15765 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15766
15767 if (x0_ref == 0 || x1_ref == 0)
15768 return 0;
15769
15770 cc_loc_result = x0_ref;
15771 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15772
15773 add_loc_descr (&cc_loc_result, x1_ref);
15774 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15775
15776 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15777 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15778
15779 return cc_loc_result;
15780 }
15781
15782 /* Return a descriptor that describes the concatenation of N
15783 locations. */
15784
15785 static dw_loc_descr_ref
15786 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15787 {
15788 unsigned int i;
15789 dw_loc_descr_ref cc_loc_result = NULL;
15790 unsigned int n = XVECLEN (concatn, 0);
15791
15792 for (i = 0; i < n; ++i)
15793 {
15794 dw_loc_descr_ref ref;
15795 rtx x = XVECEXP (concatn, 0, i);
15796
15797 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15798 if (ref == NULL)
15799 return NULL;
15800
15801 add_loc_descr (&cc_loc_result, ref);
15802 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15803 }
15804
15805 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15806 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15807
15808 return cc_loc_result;
15809 }
15810
15811 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15812 for DEBUG_IMPLICIT_PTR RTL. */
15813
15814 static dw_loc_descr_ref
15815 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15816 {
15817 dw_loc_descr_ref ret;
15818 dw_die_ref ref;
15819
15820 if (dwarf_strict && dwarf_version < 5)
15821 return NULL;
15822 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15823 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15824 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15825 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15826 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15827 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15828 if (ref)
15829 {
15830 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15831 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15832 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15833 }
15834 else
15835 {
15836 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15837 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15838 }
15839 return ret;
15840 }
15841
15842 /* Output a proper Dwarf location descriptor for a variable or parameter
15843 which is either allocated in a register or in a memory location. For a
15844 register, we just generate an OP_REG and the register number. For a
15845 memory location we provide a Dwarf postfix expression describing how to
15846 generate the (dynamic) address of the object onto the address stack.
15847
15848 MODE is mode of the decl if this loc_descriptor is going to be used in
15849 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15850 allowed, VOIDmode otherwise.
15851
15852 If we don't know how to describe it, return 0. */
15853
15854 static dw_loc_descr_ref
15855 loc_descriptor (rtx rtl, machine_mode mode,
15856 enum var_init_status initialized)
15857 {
15858 dw_loc_descr_ref loc_result = NULL;
15859 scalar_int_mode int_mode;
15860
15861 switch (GET_CODE (rtl))
15862 {
15863 case SUBREG:
15864 /* The case of a subreg may arise when we have a local (register)
15865 variable or a formal (register) parameter which doesn't quite fill
15866 up an entire register. For now, just assume that it is
15867 legitimate to make the Dwarf info refer to the whole register which
15868 contains the given subreg. */
15869 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15870 loc_result = loc_descriptor (SUBREG_REG (rtl),
15871 GET_MODE (SUBREG_REG (rtl)), initialized);
15872 else
15873 goto do_default;
15874 break;
15875
15876 case REG:
15877 loc_result = reg_loc_descriptor (rtl, initialized);
15878 break;
15879
15880 case MEM:
15881 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15882 GET_MODE (rtl), initialized);
15883 if (loc_result == NULL)
15884 loc_result = tls_mem_loc_descriptor (rtl);
15885 if (loc_result == NULL)
15886 {
15887 rtx new_rtl = avoid_constant_pool_reference (rtl);
15888 if (new_rtl != rtl)
15889 loc_result = loc_descriptor (new_rtl, mode, initialized);
15890 }
15891 break;
15892
15893 case CONCAT:
15894 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15895 initialized);
15896 break;
15897
15898 case CONCATN:
15899 loc_result = concatn_loc_descriptor (rtl, initialized);
15900 break;
15901
15902 case VAR_LOCATION:
15903 /* Single part. */
15904 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15905 {
15906 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15907 if (GET_CODE (loc) == EXPR_LIST)
15908 loc = XEXP (loc, 0);
15909 loc_result = loc_descriptor (loc, mode, initialized);
15910 break;
15911 }
15912
15913 rtl = XEXP (rtl, 1);
15914 /* FALLTHRU */
15915
15916 case PARALLEL:
15917 {
15918 rtvec par_elems = XVEC (rtl, 0);
15919 int num_elem = GET_NUM_ELEM (par_elems);
15920 machine_mode mode;
15921 int i;
15922
15923 /* Create the first one, so we have something to add to. */
15924 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15925 VOIDmode, initialized);
15926 if (loc_result == NULL)
15927 return NULL;
15928 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15929 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15930 for (i = 1; i < num_elem; i++)
15931 {
15932 dw_loc_descr_ref temp;
15933
15934 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15935 VOIDmode, initialized);
15936 if (temp == NULL)
15937 return NULL;
15938 add_loc_descr (&loc_result, temp);
15939 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15940 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15941 }
15942 }
15943 break;
15944
15945 case CONST_INT:
15946 if (mode != VOIDmode && mode != BLKmode)
15947 {
15948 int_mode = as_a <scalar_int_mode> (mode);
15949 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15950 INTVAL (rtl));
15951 }
15952 break;
15953
15954 case CONST_DOUBLE:
15955 if (mode == VOIDmode)
15956 mode = GET_MODE (rtl);
15957
15958 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15959 {
15960 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15961
15962 /* Note that a CONST_DOUBLE rtx could represent either an integer
15963 or a floating-point constant. A CONST_DOUBLE is used whenever
15964 the constant requires more than one word in order to be
15965 adequately represented. We output CONST_DOUBLEs as blocks. */
15966 scalar_mode smode = as_a <scalar_mode> (mode);
15967 loc_result = new_loc_descr (DW_OP_implicit_value,
15968 GET_MODE_SIZE (smode), 0);
15969 #if TARGET_SUPPORTS_WIDE_INT == 0
15970 if (!SCALAR_FLOAT_MODE_P (smode))
15971 {
15972 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15973 loc_result->dw_loc_oprnd2.v.val_double
15974 = rtx_to_double_int (rtl);
15975 }
15976 else
15977 #endif
15978 {
15979 unsigned int length = GET_MODE_SIZE (smode);
15980 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15981
15982 insert_float (rtl, array);
15983 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15984 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15985 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15986 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15987 }
15988 }
15989 break;
15990
15991 case CONST_WIDE_INT:
15992 if (mode == VOIDmode)
15993 mode = GET_MODE (rtl);
15994
15995 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15996 {
15997 int_mode = as_a <scalar_int_mode> (mode);
15998 loc_result = new_loc_descr (DW_OP_implicit_value,
15999 GET_MODE_SIZE (int_mode), 0);
16000 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16001 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16002 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16003 }
16004 break;
16005
16006 case CONST_VECTOR:
16007 if (mode == VOIDmode)
16008 mode = GET_MODE (rtl);
16009
16010 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16011 {
16012 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16013 unsigned int length = CONST_VECTOR_NUNITS (rtl);
16014 unsigned char *array
16015 = ggc_vec_alloc<unsigned char> (length * elt_size);
16016 unsigned int i;
16017 unsigned char *p;
16018 machine_mode imode = GET_MODE_INNER (mode);
16019
16020 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16021 switch (GET_MODE_CLASS (mode))
16022 {
16023 case MODE_VECTOR_INT:
16024 for (i = 0, p = array; i < length; i++, p += elt_size)
16025 {
16026 rtx elt = CONST_VECTOR_ELT (rtl, i);
16027 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16028 }
16029 break;
16030
16031 case MODE_VECTOR_FLOAT:
16032 for (i = 0, p = array; i < length; i++, p += elt_size)
16033 {
16034 rtx elt = CONST_VECTOR_ELT (rtl, i);
16035 insert_float (elt, p);
16036 }
16037 break;
16038
16039 default:
16040 gcc_unreachable ();
16041 }
16042
16043 loc_result = new_loc_descr (DW_OP_implicit_value,
16044 length * elt_size, 0);
16045 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16046 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16047 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16048 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16049 }
16050 break;
16051
16052 case CONST:
16053 if (mode == VOIDmode
16054 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16055 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16056 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16057 {
16058 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16059 break;
16060 }
16061 /* FALLTHROUGH */
16062 case SYMBOL_REF:
16063 if (!const_ok_for_output (rtl))
16064 break;
16065 /* FALLTHROUGH */
16066 case LABEL_REF:
16067 if (is_a <scalar_int_mode> (mode, &int_mode)
16068 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16069 && (dwarf_version >= 4 || !dwarf_strict))
16070 {
16071 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16072 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16073 vec_safe_push (used_rtx_array, rtl);
16074 }
16075 break;
16076
16077 case DEBUG_IMPLICIT_PTR:
16078 loc_result = implicit_ptr_descriptor (rtl, 0);
16079 break;
16080
16081 case PLUS:
16082 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16083 && CONST_INT_P (XEXP (rtl, 1)))
16084 {
16085 loc_result
16086 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16087 break;
16088 }
16089 /* FALLTHRU */
16090 do_default:
16091 default:
16092 if ((is_a <scalar_int_mode> (mode, &int_mode)
16093 && GET_MODE (rtl) == int_mode
16094 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16095 && dwarf_version >= 4)
16096 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16097 {
16098 /* Value expression. */
16099 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16100 if (loc_result)
16101 add_loc_descr (&loc_result,
16102 new_loc_descr (DW_OP_stack_value, 0, 0));
16103 }
16104 break;
16105 }
16106
16107 return loc_result;
16108 }
16109
16110 /* We need to figure out what section we should use as the base for the
16111 address ranges where a given location is valid.
16112 1. If this particular DECL has a section associated with it, use that.
16113 2. If this function has a section associated with it, use that.
16114 3. Otherwise, use the text section.
16115 XXX: If you split a variable across multiple sections, we won't notice. */
16116
16117 static const char *
16118 secname_for_decl (const_tree decl)
16119 {
16120 const char *secname;
16121
16122 if (VAR_OR_FUNCTION_DECL_P (decl)
16123 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16124 && DECL_SECTION_NAME (decl))
16125 secname = DECL_SECTION_NAME (decl);
16126 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16127 secname = DECL_SECTION_NAME (current_function_decl);
16128 else if (cfun && in_cold_section_p)
16129 secname = crtl->subsections.cold_section_label;
16130 else
16131 secname = text_section_label;
16132
16133 return secname;
16134 }
16135
16136 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16137
16138 static bool
16139 decl_by_reference_p (tree decl)
16140 {
16141 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16142 || VAR_P (decl))
16143 && DECL_BY_REFERENCE (decl));
16144 }
16145
16146 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16147 for VARLOC. */
16148
16149 static dw_loc_descr_ref
16150 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16151 enum var_init_status initialized)
16152 {
16153 int have_address = 0;
16154 dw_loc_descr_ref descr;
16155 machine_mode mode;
16156
16157 if (want_address != 2)
16158 {
16159 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16160 /* Single part. */
16161 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16162 {
16163 varloc = PAT_VAR_LOCATION_LOC (varloc);
16164 if (GET_CODE (varloc) == EXPR_LIST)
16165 varloc = XEXP (varloc, 0);
16166 mode = GET_MODE (varloc);
16167 if (MEM_P (varloc))
16168 {
16169 rtx addr = XEXP (varloc, 0);
16170 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16171 mode, initialized);
16172 if (descr)
16173 have_address = 1;
16174 else
16175 {
16176 rtx x = avoid_constant_pool_reference (varloc);
16177 if (x != varloc)
16178 descr = mem_loc_descriptor (x, mode, VOIDmode,
16179 initialized);
16180 }
16181 }
16182 else
16183 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16184 }
16185 else
16186 return 0;
16187 }
16188 else
16189 {
16190 if (GET_CODE (varloc) == VAR_LOCATION)
16191 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16192 else
16193 mode = DECL_MODE (loc);
16194 descr = loc_descriptor (varloc, mode, initialized);
16195 have_address = 1;
16196 }
16197
16198 if (!descr)
16199 return 0;
16200
16201 if (want_address == 2 && !have_address
16202 && (dwarf_version >= 4 || !dwarf_strict))
16203 {
16204 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16205 {
16206 expansion_failed (loc, NULL_RTX,
16207 "DWARF address size mismatch");
16208 return 0;
16209 }
16210 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16211 have_address = 1;
16212 }
16213 /* Show if we can't fill the request for an address. */
16214 if (want_address && !have_address)
16215 {
16216 expansion_failed (loc, NULL_RTX,
16217 "Want address and only have value");
16218 return 0;
16219 }
16220
16221 /* If we've got an address and don't want one, dereference. */
16222 if (!want_address && have_address)
16223 {
16224 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16225 enum dwarf_location_atom op;
16226
16227 if (size > DWARF2_ADDR_SIZE || size == -1)
16228 {
16229 expansion_failed (loc, NULL_RTX,
16230 "DWARF address size mismatch");
16231 return 0;
16232 }
16233 else if (size == DWARF2_ADDR_SIZE)
16234 op = DW_OP_deref;
16235 else
16236 op = DW_OP_deref_size;
16237
16238 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16239 }
16240
16241 return descr;
16242 }
16243
16244 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16245 if it is not possible. */
16246
16247 static dw_loc_descr_ref
16248 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16249 {
16250 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16251 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16252 else if (dwarf_version >= 3 || !dwarf_strict)
16253 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16254 else
16255 return NULL;
16256 }
16257
16258 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16259 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16260
16261 static dw_loc_descr_ref
16262 dw_sra_loc_expr (tree decl, rtx loc)
16263 {
16264 rtx p;
16265 unsigned HOST_WIDE_INT padsize = 0;
16266 dw_loc_descr_ref descr, *descr_tail;
16267 unsigned HOST_WIDE_INT decl_size;
16268 rtx varloc;
16269 enum var_init_status initialized;
16270
16271 if (DECL_SIZE (decl) == NULL
16272 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16273 return NULL;
16274
16275 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16276 descr = NULL;
16277 descr_tail = &descr;
16278
16279 for (p = loc; p; p = XEXP (p, 1))
16280 {
16281 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16282 rtx loc_note = *decl_piece_varloc_ptr (p);
16283 dw_loc_descr_ref cur_descr;
16284 dw_loc_descr_ref *tail, last = NULL;
16285 unsigned HOST_WIDE_INT opsize = 0;
16286
16287 if (loc_note == NULL_RTX
16288 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16289 {
16290 padsize += bitsize;
16291 continue;
16292 }
16293 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16294 varloc = NOTE_VAR_LOCATION (loc_note);
16295 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16296 if (cur_descr == NULL)
16297 {
16298 padsize += bitsize;
16299 continue;
16300 }
16301
16302 /* Check that cur_descr either doesn't use
16303 DW_OP_*piece operations, or their sum is equal
16304 to bitsize. Otherwise we can't embed it. */
16305 for (tail = &cur_descr; *tail != NULL;
16306 tail = &(*tail)->dw_loc_next)
16307 if ((*tail)->dw_loc_opc == DW_OP_piece)
16308 {
16309 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16310 * BITS_PER_UNIT;
16311 last = *tail;
16312 }
16313 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16314 {
16315 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16316 last = *tail;
16317 }
16318
16319 if (last != NULL && opsize != bitsize)
16320 {
16321 padsize += bitsize;
16322 /* Discard the current piece of the descriptor and release any
16323 addr_table entries it uses. */
16324 remove_loc_list_addr_table_entries (cur_descr);
16325 continue;
16326 }
16327
16328 /* If there is a hole, add DW_OP_*piece after empty DWARF
16329 expression, which means that those bits are optimized out. */
16330 if (padsize)
16331 {
16332 if (padsize > decl_size)
16333 {
16334 remove_loc_list_addr_table_entries (cur_descr);
16335 goto discard_descr;
16336 }
16337 decl_size -= padsize;
16338 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16339 if (*descr_tail == NULL)
16340 {
16341 remove_loc_list_addr_table_entries (cur_descr);
16342 goto discard_descr;
16343 }
16344 descr_tail = &(*descr_tail)->dw_loc_next;
16345 padsize = 0;
16346 }
16347 *descr_tail = cur_descr;
16348 descr_tail = tail;
16349 if (bitsize > decl_size)
16350 goto discard_descr;
16351 decl_size -= bitsize;
16352 if (last == NULL)
16353 {
16354 HOST_WIDE_INT offset = 0;
16355 if (GET_CODE (varloc) == VAR_LOCATION
16356 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16357 {
16358 varloc = PAT_VAR_LOCATION_LOC (varloc);
16359 if (GET_CODE (varloc) == EXPR_LIST)
16360 varloc = XEXP (varloc, 0);
16361 }
16362 do
16363 {
16364 if (GET_CODE (varloc) == CONST
16365 || GET_CODE (varloc) == SIGN_EXTEND
16366 || GET_CODE (varloc) == ZERO_EXTEND)
16367 varloc = XEXP (varloc, 0);
16368 else if (GET_CODE (varloc) == SUBREG)
16369 varloc = SUBREG_REG (varloc);
16370 else
16371 break;
16372 }
16373 while (1);
16374 /* DW_OP_bit_size offset should be zero for register
16375 or implicit location descriptions and empty location
16376 descriptions, but for memory addresses needs big endian
16377 adjustment. */
16378 if (MEM_P (varloc))
16379 {
16380 unsigned HOST_WIDE_INT memsize;
16381 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
16382 goto discard_descr;
16383 memsize *= BITS_PER_UNIT;
16384 if (memsize != bitsize)
16385 {
16386 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16387 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16388 goto discard_descr;
16389 if (memsize < bitsize)
16390 goto discard_descr;
16391 if (BITS_BIG_ENDIAN)
16392 offset = memsize - bitsize;
16393 }
16394 }
16395
16396 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16397 if (*descr_tail == NULL)
16398 goto discard_descr;
16399 descr_tail = &(*descr_tail)->dw_loc_next;
16400 }
16401 }
16402
16403 /* If there were any non-empty expressions, add padding till the end of
16404 the decl. */
16405 if (descr != NULL && decl_size != 0)
16406 {
16407 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16408 if (*descr_tail == NULL)
16409 goto discard_descr;
16410 }
16411 return descr;
16412
16413 discard_descr:
16414 /* Discard the descriptor and release any addr_table entries it uses. */
16415 remove_loc_list_addr_table_entries (descr);
16416 return NULL;
16417 }
16418
16419 /* Return the dwarf representation of the location list LOC_LIST of
16420 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16421 function. */
16422
16423 static dw_loc_list_ref
16424 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16425 {
16426 const char *endname, *secname;
16427 rtx varloc;
16428 enum var_init_status initialized;
16429 struct var_loc_node *node;
16430 dw_loc_descr_ref descr;
16431 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16432 dw_loc_list_ref list = NULL;
16433 dw_loc_list_ref *listp = &list;
16434
16435 /* Now that we know what section we are using for a base,
16436 actually construct the list of locations.
16437 The first location information is what is passed to the
16438 function that creates the location list, and the remaining
16439 locations just get added on to that list.
16440 Note that we only know the start address for a location
16441 (IE location changes), so to build the range, we use
16442 the range [current location start, next location start].
16443 This means we have to special case the last node, and generate
16444 a range of [last location start, end of function label]. */
16445
16446 if (cfun && crtl->has_bb_partition)
16447 {
16448 bool save_in_cold_section_p = in_cold_section_p;
16449 in_cold_section_p = first_function_block_is_cold;
16450 if (loc_list->last_before_switch == NULL)
16451 in_cold_section_p = !in_cold_section_p;
16452 secname = secname_for_decl (decl);
16453 in_cold_section_p = save_in_cold_section_p;
16454 }
16455 else
16456 secname = secname_for_decl (decl);
16457
16458 for (node = loc_list->first; node; node = node->next)
16459 {
16460 bool range_across_switch = false;
16461 if (GET_CODE (node->loc) == EXPR_LIST
16462 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16463 {
16464 if (GET_CODE (node->loc) == EXPR_LIST)
16465 {
16466 descr = NULL;
16467 /* This requires DW_OP_{,bit_}piece, which is not usable
16468 inside DWARF expressions. */
16469 if (want_address == 2)
16470 descr = dw_sra_loc_expr (decl, node->loc);
16471 }
16472 else
16473 {
16474 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16475 varloc = NOTE_VAR_LOCATION (node->loc);
16476 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16477 }
16478 if (descr)
16479 {
16480 /* If section switch happens in between node->label
16481 and node->next->label (or end of function) and
16482 we can't emit it as a single entry list,
16483 emit two ranges, first one ending at the end
16484 of first partition and second one starting at the
16485 beginning of second partition. */
16486 if (node == loc_list->last_before_switch
16487 && (node != loc_list->first || loc_list->first->next)
16488 && current_function_decl)
16489 {
16490 endname = cfun->fde->dw_fde_end;
16491 range_across_switch = true;
16492 }
16493 /* The variable has a location between NODE->LABEL and
16494 NODE->NEXT->LABEL. */
16495 else if (node->next)
16496 endname = node->next->label;
16497 /* If the variable has a location at the last label
16498 it keeps its location until the end of function. */
16499 else if (!current_function_decl)
16500 endname = text_end_label;
16501 else
16502 {
16503 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16504 current_function_funcdef_no);
16505 endname = ggc_strdup (label_id);
16506 }
16507
16508 *listp = new_loc_list (descr, node->label, endname, secname);
16509 if (TREE_CODE (decl) == PARM_DECL
16510 && node == loc_list->first
16511 && NOTE_P (node->loc)
16512 && strcmp (node->label, endname) == 0)
16513 (*listp)->force = true;
16514 listp = &(*listp)->dw_loc_next;
16515 }
16516 }
16517
16518 if (cfun
16519 && crtl->has_bb_partition
16520 && node == loc_list->last_before_switch)
16521 {
16522 bool save_in_cold_section_p = in_cold_section_p;
16523 in_cold_section_p = !first_function_block_is_cold;
16524 secname = secname_for_decl (decl);
16525 in_cold_section_p = save_in_cold_section_p;
16526 }
16527
16528 if (range_across_switch)
16529 {
16530 if (GET_CODE (node->loc) == EXPR_LIST)
16531 descr = dw_sra_loc_expr (decl, node->loc);
16532 else
16533 {
16534 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16535 varloc = NOTE_VAR_LOCATION (node->loc);
16536 descr = dw_loc_list_1 (decl, varloc, want_address,
16537 initialized);
16538 }
16539 gcc_assert (descr);
16540 /* The variable has a location between NODE->LABEL and
16541 NODE->NEXT->LABEL. */
16542 if (node->next)
16543 endname = node->next->label;
16544 else
16545 endname = cfun->fde->dw_fde_second_end;
16546 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin,
16547 endname, secname);
16548 listp = &(*listp)->dw_loc_next;
16549 }
16550 }
16551
16552 /* Try to avoid the overhead of a location list emitting a location
16553 expression instead, but only if we didn't have more than one
16554 location entry in the first place. If some entries were not
16555 representable, we don't want to pretend a single entry that was
16556 applies to the entire scope in which the variable is
16557 available. */
16558 if (list && loc_list->first->next)
16559 gen_llsym (list);
16560
16561 return list;
16562 }
16563
16564 /* Return if the loc_list has only single element and thus can be represented
16565 as location description. */
16566
16567 static bool
16568 single_element_loc_list_p (dw_loc_list_ref list)
16569 {
16570 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16571 return !list->ll_symbol;
16572 }
16573
16574 /* Duplicate a single element of location list. */
16575
16576 static inline dw_loc_descr_ref
16577 copy_loc_descr (dw_loc_descr_ref ref)
16578 {
16579 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16580 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16581 return copy;
16582 }
16583
16584 /* To each location in list LIST append loc descr REF. */
16585
16586 static void
16587 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16588 {
16589 dw_loc_descr_ref copy;
16590 add_loc_descr (&list->expr, ref);
16591 list = list->dw_loc_next;
16592 while (list)
16593 {
16594 copy = copy_loc_descr (ref);
16595 add_loc_descr (&list->expr, copy);
16596 while (copy->dw_loc_next)
16597 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16598 list = list->dw_loc_next;
16599 }
16600 }
16601
16602 /* To each location in list LIST prepend loc descr REF. */
16603
16604 static void
16605 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16606 {
16607 dw_loc_descr_ref copy;
16608 dw_loc_descr_ref ref_end = list->expr;
16609 add_loc_descr (&ref, list->expr);
16610 list->expr = ref;
16611 list = list->dw_loc_next;
16612 while (list)
16613 {
16614 dw_loc_descr_ref end = list->expr;
16615 list->expr = copy = copy_loc_descr (ref);
16616 while (copy->dw_loc_next != ref_end)
16617 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16618 copy->dw_loc_next = end;
16619 list = list->dw_loc_next;
16620 }
16621 }
16622
16623 /* Given two lists RET and LIST
16624 produce location list that is result of adding expression in LIST
16625 to expression in RET on each position in program.
16626 Might be destructive on both RET and LIST.
16627
16628 TODO: We handle only simple cases of RET or LIST having at most one
16629 element. General case would involve sorting the lists in program order
16630 and merging them that will need some additional work.
16631 Adding that will improve quality of debug info especially for SRA-ed
16632 structures. */
16633
16634 static void
16635 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16636 {
16637 if (!list)
16638 return;
16639 if (!*ret)
16640 {
16641 *ret = list;
16642 return;
16643 }
16644 if (!list->dw_loc_next)
16645 {
16646 add_loc_descr_to_each (*ret, list->expr);
16647 return;
16648 }
16649 if (!(*ret)->dw_loc_next)
16650 {
16651 prepend_loc_descr_to_each (list, (*ret)->expr);
16652 *ret = list;
16653 return;
16654 }
16655 expansion_failed (NULL_TREE, NULL_RTX,
16656 "Don't know how to merge two non-trivial"
16657 " location lists.\n");
16658 *ret = NULL;
16659 return;
16660 }
16661
16662 /* LOC is constant expression. Try a luck, look it up in constant
16663 pool and return its loc_descr of its address. */
16664
16665 static dw_loc_descr_ref
16666 cst_pool_loc_descr (tree loc)
16667 {
16668 /* Get an RTL for this, if something has been emitted. */
16669 rtx rtl = lookup_constant_def (loc);
16670
16671 if (!rtl || !MEM_P (rtl))
16672 {
16673 gcc_assert (!rtl);
16674 return 0;
16675 }
16676 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16677
16678 /* TODO: We might get more coverage if we was actually delaying expansion
16679 of all expressions till end of compilation when constant pools are fully
16680 populated. */
16681 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16682 {
16683 expansion_failed (loc, NULL_RTX,
16684 "CST value in contant pool but not marked.");
16685 return 0;
16686 }
16687 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16688 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16689 }
16690
16691 /* Return dw_loc_list representing address of addr_expr LOC
16692 by looking for inner INDIRECT_REF expression and turning
16693 it into simple arithmetics.
16694
16695 See loc_list_from_tree for the meaning of CONTEXT. */
16696
16697 static dw_loc_list_ref
16698 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16699 loc_descr_context *context)
16700 {
16701 tree obj, offset;
16702 HOST_WIDE_INT bitsize, bitpos, bytepos;
16703 machine_mode mode;
16704 int unsignedp, reversep, volatilep = 0;
16705 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16706
16707 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16708 &bitsize, &bitpos, &offset, &mode,
16709 &unsignedp, &reversep, &volatilep);
16710 STRIP_NOPS (obj);
16711 if (bitpos % BITS_PER_UNIT)
16712 {
16713 expansion_failed (loc, NULL_RTX, "bitfield access");
16714 return 0;
16715 }
16716 if (!INDIRECT_REF_P (obj))
16717 {
16718 expansion_failed (obj,
16719 NULL_RTX, "no indirect ref in inner refrence");
16720 return 0;
16721 }
16722 if (!offset && !bitpos)
16723 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16724 context);
16725 else if (toplev
16726 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16727 && (dwarf_version >= 4 || !dwarf_strict))
16728 {
16729 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16730 if (!list_ret)
16731 return 0;
16732 if (offset)
16733 {
16734 /* Variable offset. */
16735 list_ret1 = loc_list_from_tree (offset, 0, context);
16736 if (list_ret1 == 0)
16737 return 0;
16738 add_loc_list (&list_ret, list_ret1);
16739 if (!list_ret)
16740 return 0;
16741 add_loc_descr_to_each (list_ret,
16742 new_loc_descr (DW_OP_plus, 0, 0));
16743 }
16744 bytepos = bitpos / BITS_PER_UNIT;
16745 if (bytepos > 0)
16746 add_loc_descr_to_each (list_ret,
16747 new_loc_descr (DW_OP_plus_uconst,
16748 bytepos, 0));
16749 else if (bytepos < 0)
16750 loc_list_plus_const (list_ret, bytepos);
16751 add_loc_descr_to_each (list_ret,
16752 new_loc_descr (DW_OP_stack_value, 0, 0));
16753 }
16754 return list_ret;
16755 }
16756
16757 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16758 all operations from LOC are nops, move to the last one. Insert in NOPS all
16759 operations that are skipped. */
16760
16761 static void
16762 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16763 hash_set<dw_loc_descr_ref> &nops)
16764 {
16765 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16766 {
16767 nops.add (loc);
16768 loc = loc->dw_loc_next;
16769 }
16770 }
16771
16772 /* Helper for loc_descr_without_nops: free the location description operation
16773 P. */
16774
16775 bool
16776 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16777 {
16778 ggc_free (loc);
16779 return true;
16780 }
16781
16782 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16783 finishes LOC. */
16784
16785 static void
16786 loc_descr_without_nops (dw_loc_descr_ref &loc)
16787 {
16788 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16789 return;
16790
16791 /* Set of all DW_OP_nop operations we remove. */
16792 hash_set<dw_loc_descr_ref> nops;
16793
16794 /* First, strip all prefix NOP operations in order to keep the head of the
16795 operations list. */
16796 loc_descr_to_next_no_nop (loc, nops);
16797
16798 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16799 {
16800 /* For control flow operations: strip "prefix" nops in destination
16801 labels. */
16802 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16803 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16804 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16805 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16806
16807 /* Do the same for the operations that follow, then move to the next
16808 iteration. */
16809 if (cur->dw_loc_next != NULL)
16810 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16811 cur = cur->dw_loc_next;
16812 }
16813
16814 nops.traverse<void *, free_loc_descr> (NULL);
16815 }
16816
16817
16818 struct dwarf_procedure_info;
16819
16820 /* Helper structure for location descriptions generation. */
16821 struct loc_descr_context
16822 {
16823 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16824 NULL_TREE if DW_OP_push_object_address in invalid for this location
16825 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16826 tree context_type;
16827 /* The ..._DECL node that should be translated as a
16828 DW_OP_push_object_address operation. */
16829 tree base_decl;
16830 /* Information about the DWARF procedure we are currently generating. NULL if
16831 we are not generating a DWARF procedure. */
16832 struct dwarf_procedure_info *dpi;
16833 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16834 by consumer. Used for DW_TAG_generic_subrange attributes. */
16835 bool placeholder_arg;
16836 /* True if PLACEHOLDER_EXPR has been seen. */
16837 bool placeholder_seen;
16838 };
16839
16840 /* DWARF procedures generation
16841
16842 DWARF expressions (aka. location descriptions) are used to encode variable
16843 things such as sizes or offsets. Such computations can have redundant parts
16844 that can be factorized in order to reduce the size of the output debug
16845 information. This is the whole point of DWARF procedures.
16846
16847 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16848 already factorized into functions ("size functions") in order to handle very
16849 big and complex types. Such functions are quite simple: they have integral
16850 arguments, they return an integral result and their body contains only a
16851 return statement with arithmetic expressions. This is the only kind of
16852 function we are interested in translating into DWARF procedures, here.
16853
16854 DWARF expressions and DWARF procedure are executed using a stack, so we have
16855 to define some calling convention for them to interact. Let's say that:
16856
16857 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16858 all arguments in reverse order (right-to-left) so that when the DWARF
16859 procedure execution starts, the first argument is the top of the stack.
16860
16861 - Then, when returning, the DWARF procedure must have consumed all arguments
16862 on the stack, must have pushed the result and touched nothing else.
16863
16864 - Each integral argument and the result are integral types can be hold in a
16865 single stack slot.
16866
16867 - We call "frame offset" the number of stack slots that are "under DWARF
16868 procedure control": it includes the arguments slots, the temporaries and
16869 the result slot. Thus, it is equal to the number of arguments when the
16870 procedure execution starts and must be equal to one (the result) when it
16871 returns. */
16872
16873 /* Helper structure used when generating operations for a DWARF procedure. */
16874 struct dwarf_procedure_info
16875 {
16876 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16877 currently translated. */
16878 tree fndecl;
16879 /* The number of arguments FNDECL takes. */
16880 unsigned args_count;
16881 };
16882
16883 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16884 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16885 equate it to this DIE. */
16886
16887 static dw_die_ref
16888 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16889 dw_die_ref parent_die)
16890 {
16891 dw_die_ref dwarf_proc_die;
16892
16893 if ((dwarf_version < 3 && dwarf_strict)
16894 || location == NULL)
16895 return NULL;
16896
16897 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16898 if (fndecl)
16899 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16900 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16901 return dwarf_proc_die;
16902 }
16903
16904 /* Return whether TYPE is a supported type as a DWARF procedure argument
16905 type or return type (we handle only scalar types and pointer types that
16906 aren't wider than the DWARF expression evaluation stack. */
16907
16908 static bool
16909 is_handled_procedure_type (tree type)
16910 {
16911 return ((INTEGRAL_TYPE_P (type)
16912 || TREE_CODE (type) == OFFSET_TYPE
16913 || TREE_CODE (type) == POINTER_TYPE)
16914 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16915 }
16916
16917 /* Helper for resolve_args_picking: do the same but stop when coming across
16918 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16919 offset *before* evaluating the corresponding operation. */
16920
16921 static bool
16922 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16923 struct dwarf_procedure_info *dpi,
16924 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16925 {
16926 /* The "frame_offset" identifier is already used to name a macro... */
16927 unsigned frame_offset_ = initial_frame_offset;
16928 dw_loc_descr_ref l;
16929
16930 for (l = loc; l != NULL;)
16931 {
16932 bool existed;
16933 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16934
16935 /* If we already met this node, there is nothing to compute anymore. */
16936 if (existed)
16937 {
16938 /* Make sure that the stack size is consistent wherever the execution
16939 flow comes from. */
16940 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16941 break;
16942 }
16943 l_frame_offset = frame_offset_;
16944
16945 /* If needed, relocate the picking offset with respect to the frame
16946 offset. */
16947 if (l->frame_offset_rel)
16948 {
16949 unsigned HOST_WIDE_INT off;
16950 switch (l->dw_loc_opc)
16951 {
16952 case DW_OP_pick:
16953 off = l->dw_loc_oprnd1.v.val_unsigned;
16954 break;
16955 case DW_OP_dup:
16956 off = 0;
16957 break;
16958 case DW_OP_over:
16959 off = 1;
16960 break;
16961 default:
16962 gcc_unreachable ();
16963 }
16964 /* frame_offset_ is the size of the current stack frame, including
16965 incoming arguments. Besides, the arguments are pushed
16966 right-to-left. Thus, in order to access the Nth argument from
16967 this operation node, the picking has to skip temporaries *plus*
16968 one stack slot per argument (0 for the first one, 1 for the second
16969 one, etc.).
16970
16971 The targetted argument number (N) is already set as the operand,
16972 and the number of temporaries can be computed with:
16973 frame_offsets_ - dpi->args_count */
16974 off += frame_offset_ - dpi->args_count;
16975
16976 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16977 if (off > 255)
16978 return false;
16979
16980 if (off == 0)
16981 {
16982 l->dw_loc_opc = DW_OP_dup;
16983 l->dw_loc_oprnd1.v.val_unsigned = 0;
16984 }
16985 else if (off == 1)
16986 {
16987 l->dw_loc_opc = DW_OP_over;
16988 l->dw_loc_oprnd1.v.val_unsigned = 0;
16989 }
16990 else
16991 {
16992 l->dw_loc_opc = DW_OP_pick;
16993 l->dw_loc_oprnd1.v.val_unsigned = off;
16994 }
16995 }
16996
16997 /* Update frame_offset according to the effect the current operation has
16998 on the stack. */
16999 switch (l->dw_loc_opc)
17000 {
17001 case DW_OP_deref:
17002 case DW_OP_swap:
17003 case DW_OP_rot:
17004 case DW_OP_abs:
17005 case DW_OP_neg:
17006 case DW_OP_not:
17007 case DW_OP_plus_uconst:
17008 case DW_OP_skip:
17009 case DW_OP_reg0:
17010 case DW_OP_reg1:
17011 case DW_OP_reg2:
17012 case DW_OP_reg3:
17013 case DW_OP_reg4:
17014 case DW_OP_reg5:
17015 case DW_OP_reg6:
17016 case DW_OP_reg7:
17017 case DW_OP_reg8:
17018 case DW_OP_reg9:
17019 case DW_OP_reg10:
17020 case DW_OP_reg11:
17021 case DW_OP_reg12:
17022 case DW_OP_reg13:
17023 case DW_OP_reg14:
17024 case DW_OP_reg15:
17025 case DW_OP_reg16:
17026 case DW_OP_reg17:
17027 case DW_OP_reg18:
17028 case DW_OP_reg19:
17029 case DW_OP_reg20:
17030 case DW_OP_reg21:
17031 case DW_OP_reg22:
17032 case DW_OP_reg23:
17033 case DW_OP_reg24:
17034 case DW_OP_reg25:
17035 case DW_OP_reg26:
17036 case DW_OP_reg27:
17037 case DW_OP_reg28:
17038 case DW_OP_reg29:
17039 case DW_OP_reg30:
17040 case DW_OP_reg31:
17041 case DW_OP_bregx:
17042 case DW_OP_piece:
17043 case DW_OP_deref_size:
17044 case DW_OP_nop:
17045 case DW_OP_bit_piece:
17046 case DW_OP_implicit_value:
17047 case DW_OP_stack_value:
17048 break;
17049
17050 case DW_OP_addr:
17051 case DW_OP_const1u:
17052 case DW_OP_const1s:
17053 case DW_OP_const2u:
17054 case DW_OP_const2s:
17055 case DW_OP_const4u:
17056 case DW_OP_const4s:
17057 case DW_OP_const8u:
17058 case DW_OP_const8s:
17059 case DW_OP_constu:
17060 case DW_OP_consts:
17061 case DW_OP_dup:
17062 case DW_OP_over:
17063 case DW_OP_pick:
17064 case DW_OP_lit0:
17065 case DW_OP_lit1:
17066 case DW_OP_lit2:
17067 case DW_OP_lit3:
17068 case DW_OP_lit4:
17069 case DW_OP_lit5:
17070 case DW_OP_lit6:
17071 case DW_OP_lit7:
17072 case DW_OP_lit8:
17073 case DW_OP_lit9:
17074 case DW_OP_lit10:
17075 case DW_OP_lit11:
17076 case DW_OP_lit12:
17077 case DW_OP_lit13:
17078 case DW_OP_lit14:
17079 case DW_OP_lit15:
17080 case DW_OP_lit16:
17081 case DW_OP_lit17:
17082 case DW_OP_lit18:
17083 case DW_OP_lit19:
17084 case DW_OP_lit20:
17085 case DW_OP_lit21:
17086 case DW_OP_lit22:
17087 case DW_OP_lit23:
17088 case DW_OP_lit24:
17089 case DW_OP_lit25:
17090 case DW_OP_lit26:
17091 case DW_OP_lit27:
17092 case DW_OP_lit28:
17093 case DW_OP_lit29:
17094 case DW_OP_lit30:
17095 case DW_OP_lit31:
17096 case DW_OP_breg0:
17097 case DW_OP_breg1:
17098 case DW_OP_breg2:
17099 case DW_OP_breg3:
17100 case DW_OP_breg4:
17101 case DW_OP_breg5:
17102 case DW_OP_breg6:
17103 case DW_OP_breg7:
17104 case DW_OP_breg8:
17105 case DW_OP_breg9:
17106 case DW_OP_breg10:
17107 case DW_OP_breg11:
17108 case DW_OP_breg12:
17109 case DW_OP_breg13:
17110 case DW_OP_breg14:
17111 case DW_OP_breg15:
17112 case DW_OP_breg16:
17113 case DW_OP_breg17:
17114 case DW_OP_breg18:
17115 case DW_OP_breg19:
17116 case DW_OP_breg20:
17117 case DW_OP_breg21:
17118 case DW_OP_breg22:
17119 case DW_OP_breg23:
17120 case DW_OP_breg24:
17121 case DW_OP_breg25:
17122 case DW_OP_breg26:
17123 case DW_OP_breg27:
17124 case DW_OP_breg28:
17125 case DW_OP_breg29:
17126 case DW_OP_breg30:
17127 case DW_OP_breg31:
17128 case DW_OP_fbreg:
17129 case DW_OP_push_object_address:
17130 case DW_OP_call_frame_cfa:
17131 case DW_OP_GNU_variable_value:
17132 ++frame_offset_;
17133 break;
17134
17135 case DW_OP_drop:
17136 case DW_OP_xderef:
17137 case DW_OP_and:
17138 case DW_OP_div:
17139 case DW_OP_minus:
17140 case DW_OP_mod:
17141 case DW_OP_mul:
17142 case DW_OP_or:
17143 case DW_OP_plus:
17144 case DW_OP_shl:
17145 case DW_OP_shr:
17146 case DW_OP_shra:
17147 case DW_OP_xor:
17148 case DW_OP_bra:
17149 case DW_OP_eq:
17150 case DW_OP_ge:
17151 case DW_OP_gt:
17152 case DW_OP_le:
17153 case DW_OP_lt:
17154 case DW_OP_ne:
17155 case DW_OP_regx:
17156 case DW_OP_xderef_size:
17157 --frame_offset_;
17158 break;
17159
17160 case DW_OP_call2:
17161 case DW_OP_call4:
17162 case DW_OP_call_ref:
17163 {
17164 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17165 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17166
17167 if (stack_usage == NULL)
17168 return false;
17169 frame_offset_ += *stack_usage;
17170 break;
17171 }
17172
17173 case DW_OP_implicit_pointer:
17174 case DW_OP_entry_value:
17175 case DW_OP_const_type:
17176 case DW_OP_regval_type:
17177 case DW_OP_deref_type:
17178 case DW_OP_convert:
17179 case DW_OP_reinterpret:
17180 case DW_OP_form_tls_address:
17181 case DW_OP_GNU_push_tls_address:
17182 case DW_OP_GNU_uninit:
17183 case DW_OP_GNU_encoded_addr:
17184 case DW_OP_GNU_implicit_pointer:
17185 case DW_OP_GNU_entry_value:
17186 case DW_OP_GNU_const_type:
17187 case DW_OP_GNU_regval_type:
17188 case DW_OP_GNU_deref_type:
17189 case DW_OP_GNU_convert:
17190 case DW_OP_GNU_reinterpret:
17191 case DW_OP_GNU_parameter_ref:
17192 /* loc_list_from_tree will probably not output these operations for
17193 size functions, so assume they will not appear here. */
17194 /* Fall through... */
17195
17196 default:
17197 gcc_unreachable ();
17198 }
17199
17200 /* Now, follow the control flow (except subroutine calls). */
17201 switch (l->dw_loc_opc)
17202 {
17203 case DW_OP_bra:
17204 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17205 frame_offsets))
17206 return false;
17207 /* Fall through. */
17208
17209 case DW_OP_skip:
17210 l = l->dw_loc_oprnd1.v.val_loc;
17211 break;
17212
17213 case DW_OP_stack_value:
17214 return true;
17215
17216 default:
17217 l = l->dw_loc_next;
17218 break;
17219 }
17220 }
17221
17222 return true;
17223 }
17224
17225 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17226 operations) in order to resolve the operand of DW_OP_pick operations that
17227 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17228 offset *before* LOC is executed. Return if all relocations were
17229 successful. */
17230
17231 static bool
17232 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17233 struct dwarf_procedure_info *dpi)
17234 {
17235 /* Associate to all visited operations the frame offset *before* evaluating
17236 this operation. */
17237 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17238
17239 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17240 frame_offsets);
17241 }
17242
17243 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17244 Return NULL if it is not possible. */
17245
17246 static dw_die_ref
17247 function_to_dwarf_procedure (tree fndecl)
17248 {
17249 struct loc_descr_context ctx;
17250 struct dwarf_procedure_info dpi;
17251 dw_die_ref dwarf_proc_die;
17252 tree tree_body = DECL_SAVED_TREE (fndecl);
17253 dw_loc_descr_ref loc_body, epilogue;
17254
17255 tree cursor;
17256 unsigned i;
17257
17258 /* Do not generate multiple DWARF procedures for the same function
17259 declaration. */
17260 dwarf_proc_die = lookup_decl_die (fndecl);
17261 if (dwarf_proc_die != NULL)
17262 return dwarf_proc_die;
17263
17264 /* DWARF procedures are available starting with the DWARFv3 standard. */
17265 if (dwarf_version < 3 && dwarf_strict)
17266 return NULL;
17267
17268 /* We handle only functions for which we still have a body, that return a
17269 supported type and that takes arguments with supported types. Note that
17270 there is no point translating functions that return nothing. */
17271 if (tree_body == NULL_TREE
17272 || DECL_RESULT (fndecl) == NULL_TREE
17273 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17274 return NULL;
17275
17276 for (cursor = DECL_ARGUMENTS (fndecl);
17277 cursor != NULL_TREE;
17278 cursor = TREE_CHAIN (cursor))
17279 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17280 return NULL;
17281
17282 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17283 if (TREE_CODE (tree_body) != RETURN_EXPR)
17284 return NULL;
17285 tree_body = TREE_OPERAND (tree_body, 0);
17286 if (TREE_CODE (tree_body) != MODIFY_EXPR
17287 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17288 return NULL;
17289 tree_body = TREE_OPERAND (tree_body, 1);
17290
17291 /* Try to translate the body expression itself. Note that this will probably
17292 cause an infinite recursion if its call graph has a cycle. This is very
17293 unlikely for size functions, however, so don't bother with such things at
17294 the moment. */
17295 ctx.context_type = NULL_TREE;
17296 ctx.base_decl = NULL_TREE;
17297 ctx.dpi = &dpi;
17298 ctx.placeholder_arg = false;
17299 ctx.placeholder_seen = false;
17300 dpi.fndecl = fndecl;
17301 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17302 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17303 if (!loc_body)
17304 return NULL;
17305
17306 /* After evaluating all operands in "loc_body", we should still have on the
17307 stack all arguments plus the desired function result (top of the stack).
17308 Generate code in order to keep only the result in our stack frame. */
17309 epilogue = NULL;
17310 for (i = 0; i < dpi.args_count; ++i)
17311 {
17312 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17313 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17314 op_couple->dw_loc_next->dw_loc_next = epilogue;
17315 epilogue = op_couple;
17316 }
17317 add_loc_descr (&loc_body, epilogue);
17318 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17319 return NULL;
17320
17321 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17322 because they are considered useful. Now there is an epilogue, they are
17323 not anymore, so give it another try. */
17324 loc_descr_without_nops (loc_body);
17325
17326 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17327 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17328 though, given that size functions do not come from source, so they should
17329 not have a dedicated DW_TAG_subprogram DIE. */
17330 dwarf_proc_die
17331 = new_dwarf_proc_die (loc_body, fndecl,
17332 get_context_die (DECL_CONTEXT (fndecl)));
17333
17334 /* The called DWARF procedure consumes one stack slot per argument and
17335 returns one stack slot. */
17336 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17337
17338 return dwarf_proc_die;
17339 }
17340
17341
17342 /* Generate Dwarf location list representing LOC.
17343 If WANT_ADDRESS is false, expression computing LOC will be computed
17344 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17345 if WANT_ADDRESS is 2, expression computing address useable in location
17346 will be returned (i.e. DW_OP_reg can be used
17347 to refer to register values).
17348
17349 CONTEXT provides information to customize the location descriptions
17350 generation. Its context_type field specifies what type is implicitly
17351 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17352 will not be generated.
17353
17354 Its DPI field determines whether we are generating a DWARF expression for a
17355 DWARF procedure, so PARM_DECL references are processed specifically.
17356
17357 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17358 and dpi fields were null. */
17359
17360 static dw_loc_list_ref
17361 loc_list_from_tree_1 (tree loc, int want_address,
17362 struct loc_descr_context *context)
17363 {
17364 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17365 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17366 int have_address = 0;
17367 enum dwarf_location_atom op;
17368
17369 /* ??? Most of the time we do not take proper care for sign/zero
17370 extending the values properly. Hopefully this won't be a real
17371 problem... */
17372
17373 if (context != NULL
17374 && context->base_decl == loc
17375 && want_address == 0)
17376 {
17377 if (dwarf_version >= 3 || !dwarf_strict)
17378 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17379 NULL, NULL, NULL);
17380 else
17381 return NULL;
17382 }
17383
17384 switch (TREE_CODE (loc))
17385 {
17386 case ERROR_MARK:
17387 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17388 return 0;
17389
17390 case PLACEHOLDER_EXPR:
17391 /* This case involves extracting fields from an object to determine the
17392 position of other fields. It is supposed to appear only as the first
17393 operand of COMPONENT_REF nodes and to reference precisely the type
17394 that the context allows. */
17395 if (context != NULL
17396 && TREE_TYPE (loc) == context->context_type
17397 && want_address >= 1)
17398 {
17399 if (dwarf_version >= 3 || !dwarf_strict)
17400 {
17401 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17402 have_address = 1;
17403 break;
17404 }
17405 else
17406 return NULL;
17407 }
17408 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17409 the single argument passed by consumer. */
17410 else if (context != NULL
17411 && context->placeholder_arg
17412 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17413 && want_address == 0)
17414 {
17415 ret = new_loc_descr (DW_OP_pick, 0, 0);
17416 ret->frame_offset_rel = 1;
17417 context->placeholder_seen = true;
17418 break;
17419 }
17420 else
17421 expansion_failed (loc, NULL_RTX,
17422 "PLACEHOLDER_EXPR for an unexpected type");
17423 break;
17424
17425 case CALL_EXPR:
17426 {
17427 const int nargs = call_expr_nargs (loc);
17428 tree callee = get_callee_fndecl (loc);
17429 int i;
17430 dw_die_ref dwarf_proc;
17431
17432 if (callee == NULL_TREE)
17433 goto call_expansion_failed;
17434
17435 /* We handle only functions that return an integer. */
17436 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17437 goto call_expansion_failed;
17438
17439 dwarf_proc = function_to_dwarf_procedure (callee);
17440 if (dwarf_proc == NULL)
17441 goto call_expansion_failed;
17442
17443 /* Evaluate arguments right-to-left so that the first argument will
17444 be the top-most one on the stack. */
17445 for (i = nargs - 1; i >= 0; --i)
17446 {
17447 dw_loc_descr_ref loc_descr
17448 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17449 context);
17450
17451 if (loc_descr == NULL)
17452 goto call_expansion_failed;
17453
17454 add_loc_descr (&ret, loc_descr);
17455 }
17456
17457 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17458 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17459 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17460 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17461 add_loc_descr (&ret, ret1);
17462 break;
17463
17464 call_expansion_failed:
17465 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17466 /* There are no opcodes for these operations. */
17467 return 0;
17468 }
17469
17470 case PREINCREMENT_EXPR:
17471 case PREDECREMENT_EXPR:
17472 case POSTINCREMENT_EXPR:
17473 case POSTDECREMENT_EXPR:
17474 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17475 /* There are no opcodes for these operations. */
17476 return 0;
17477
17478 case ADDR_EXPR:
17479 /* If we already want an address, see if there is INDIRECT_REF inside
17480 e.g. for &this->field. */
17481 if (want_address)
17482 {
17483 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17484 (loc, want_address == 2, context);
17485 if (list_ret)
17486 have_address = 1;
17487 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17488 && (ret = cst_pool_loc_descr (loc)))
17489 have_address = 1;
17490 }
17491 /* Otherwise, process the argument and look for the address. */
17492 if (!list_ret && !ret)
17493 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17494 else
17495 {
17496 if (want_address)
17497 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17498 return NULL;
17499 }
17500 break;
17501
17502 case VAR_DECL:
17503 if (DECL_THREAD_LOCAL_P (loc))
17504 {
17505 rtx rtl;
17506 enum dwarf_location_atom tls_op;
17507 enum dtprel_bool dtprel = dtprel_false;
17508
17509 if (targetm.have_tls)
17510 {
17511 /* If this is not defined, we have no way to emit the
17512 data. */
17513 if (!targetm.asm_out.output_dwarf_dtprel)
17514 return 0;
17515
17516 /* The way DW_OP_GNU_push_tls_address is specified, we
17517 can only look up addresses of objects in the current
17518 module. We used DW_OP_addr as first op, but that's
17519 wrong, because DW_OP_addr is relocated by the debug
17520 info consumer, while DW_OP_GNU_push_tls_address
17521 operand shouldn't be. */
17522 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17523 return 0;
17524 dtprel = dtprel_true;
17525 /* We check for DWARF 5 here because gdb did not implement
17526 DW_OP_form_tls_address until after 7.12. */
17527 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17528 : DW_OP_GNU_push_tls_address);
17529 }
17530 else
17531 {
17532 if (!targetm.emutls.debug_form_tls_address
17533 || !(dwarf_version >= 3 || !dwarf_strict))
17534 return 0;
17535 /* We stuffed the control variable into the DECL_VALUE_EXPR
17536 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17537 no longer appear in gimple code. We used the control
17538 variable in specific so that we could pick it up here. */
17539 loc = DECL_VALUE_EXPR (loc);
17540 tls_op = DW_OP_form_tls_address;
17541 }
17542
17543 rtl = rtl_for_decl_location (loc);
17544 if (rtl == NULL_RTX)
17545 return 0;
17546
17547 if (!MEM_P (rtl))
17548 return 0;
17549 rtl = XEXP (rtl, 0);
17550 if (! CONSTANT_P (rtl))
17551 return 0;
17552
17553 ret = new_addr_loc_descr (rtl, dtprel);
17554 ret1 = new_loc_descr (tls_op, 0, 0);
17555 add_loc_descr (&ret, ret1);
17556
17557 have_address = 1;
17558 break;
17559 }
17560 /* FALLTHRU */
17561
17562 case PARM_DECL:
17563 if (context != NULL && context->dpi != NULL
17564 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17565 {
17566 /* We are generating code for a DWARF procedure and we want to access
17567 one of its arguments: find the appropriate argument offset and let
17568 the resolve_args_picking pass compute the offset that complies
17569 with the stack frame size. */
17570 unsigned i = 0;
17571 tree cursor;
17572
17573 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17574 cursor != NULL_TREE && cursor != loc;
17575 cursor = TREE_CHAIN (cursor), ++i)
17576 ;
17577 /* If we are translating a DWARF procedure, all referenced parameters
17578 must belong to the current function. */
17579 gcc_assert (cursor != NULL_TREE);
17580
17581 ret = new_loc_descr (DW_OP_pick, i, 0);
17582 ret->frame_offset_rel = 1;
17583 break;
17584 }
17585 /* FALLTHRU */
17586
17587 case RESULT_DECL:
17588 if (DECL_HAS_VALUE_EXPR_P (loc))
17589 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17590 want_address, context);
17591 /* FALLTHRU */
17592
17593 case FUNCTION_DECL:
17594 {
17595 rtx rtl;
17596 var_loc_list *loc_list = lookup_decl_loc (loc);
17597
17598 if (loc_list && loc_list->first)
17599 {
17600 list_ret = dw_loc_list (loc_list, loc, want_address);
17601 have_address = want_address != 0;
17602 break;
17603 }
17604 rtl = rtl_for_decl_location (loc);
17605 if (rtl == NULL_RTX)
17606 {
17607 if (TREE_CODE (loc) != FUNCTION_DECL
17608 && early_dwarf
17609 && current_function_decl
17610 && want_address != 1
17611 && ! DECL_IGNORED_P (loc)
17612 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17613 || POINTER_TYPE_P (TREE_TYPE (loc)))
17614 && DECL_CONTEXT (loc) == current_function_decl
17615 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17616 <= DWARF2_ADDR_SIZE))
17617 {
17618 dw_die_ref ref = lookup_decl_die (loc);
17619 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17620 if (ref)
17621 {
17622 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17623 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17624 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17625 }
17626 else
17627 {
17628 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17629 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17630 }
17631 break;
17632 }
17633 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17634 return 0;
17635 }
17636 else if (CONST_INT_P (rtl))
17637 {
17638 HOST_WIDE_INT val = INTVAL (rtl);
17639 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17640 val &= GET_MODE_MASK (DECL_MODE (loc));
17641 ret = int_loc_descriptor (val);
17642 }
17643 else if (GET_CODE (rtl) == CONST_STRING)
17644 {
17645 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17646 return 0;
17647 }
17648 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17649 ret = new_addr_loc_descr (rtl, dtprel_false);
17650 else
17651 {
17652 machine_mode mode, mem_mode;
17653
17654 /* Certain constructs can only be represented at top-level. */
17655 if (want_address == 2)
17656 {
17657 ret = loc_descriptor (rtl, VOIDmode,
17658 VAR_INIT_STATUS_INITIALIZED);
17659 have_address = 1;
17660 }
17661 else
17662 {
17663 mode = GET_MODE (rtl);
17664 mem_mode = VOIDmode;
17665 if (MEM_P (rtl))
17666 {
17667 mem_mode = mode;
17668 mode = get_address_mode (rtl);
17669 rtl = XEXP (rtl, 0);
17670 have_address = 1;
17671 }
17672 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17673 VAR_INIT_STATUS_INITIALIZED);
17674 }
17675 if (!ret)
17676 expansion_failed (loc, rtl,
17677 "failed to produce loc descriptor for rtl");
17678 }
17679 }
17680 break;
17681
17682 case MEM_REF:
17683 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17684 {
17685 have_address = 1;
17686 goto do_plus;
17687 }
17688 /* Fallthru. */
17689 case INDIRECT_REF:
17690 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17691 have_address = 1;
17692 break;
17693
17694 case TARGET_MEM_REF:
17695 case SSA_NAME:
17696 case DEBUG_EXPR_DECL:
17697 return NULL;
17698
17699 case COMPOUND_EXPR:
17700 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17701 context);
17702
17703 CASE_CONVERT:
17704 case VIEW_CONVERT_EXPR:
17705 case SAVE_EXPR:
17706 case MODIFY_EXPR:
17707 case NON_LVALUE_EXPR:
17708 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17709 context);
17710
17711 case COMPONENT_REF:
17712 case BIT_FIELD_REF:
17713 case ARRAY_REF:
17714 case ARRAY_RANGE_REF:
17715 case REALPART_EXPR:
17716 case IMAGPART_EXPR:
17717 {
17718 tree obj, offset;
17719 HOST_WIDE_INT bitsize, bitpos, bytepos;
17720 machine_mode mode;
17721 int unsignedp, reversep, volatilep = 0;
17722
17723 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17724 &unsignedp, &reversep, &volatilep);
17725
17726 gcc_assert (obj != loc);
17727
17728 list_ret = loc_list_from_tree_1 (obj,
17729 want_address == 2
17730 && !bitpos && !offset ? 2 : 1,
17731 context);
17732 /* TODO: We can extract value of the small expression via shifting even
17733 for nonzero bitpos. */
17734 if (list_ret == 0)
17735 return 0;
17736 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17737 {
17738 expansion_failed (loc, NULL_RTX,
17739 "bitfield access");
17740 return 0;
17741 }
17742
17743 if (offset != NULL_TREE)
17744 {
17745 /* Variable offset. */
17746 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17747 if (list_ret1 == 0)
17748 return 0;
17749 add_loc_list (&list_ret, list_ret1);
17750 if (!list_ret)
17751 return 0;
17752 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17753 }
17754
17755 bytepos = bitpos / BITS_PER_UNIT;
17756 if (bytepos > 0)
17757 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17758 else if (bytepos < 0)
17759 loc_list_plus_const (list_ret, bytepos);
17760
17761 have_address = 1;
17762 break;
17763 }
17764
17765 case INTEGER_CST:
17766 if ((want_address || !tree_fits_shwi_p (loc))
17767 && (ret = cst_pool_loc_descr (loc)))
17768 have_address = 1;
17769 else if (want_address == 2
17770 && tree_fits_shwi_p (loc)
17771 && (ret = address_of_int_loc_descriptor
17772 (int_size_in_bytes (TREE_TYPE (loc)),
17773 tree_to_shwi (loc))))
17774 have_address = 1;
17775 else if (tree_fits_shwi_p (loc))
17776 ret = int_loc_descriptor (tree_to_shwi (loc));
17777 else if (tree_fits_uhwi_p (loc))
17778 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17779 else
17780 {
17781 expansion_failed (loc, NULL_RTX,
17782 "Integer operand is not host integer");
17783 return 0;
17784 }
17785 break;
17786
17787 case CONSTRUCTOR:
17788 case REAL_CST:
17789 case STRING_CST:
17790 case COMPLEX_CST:
17791 if ((ret = cst_pool_loc_descr (loc)))
17792 have_address = 1;
17793 else if (TREE_CODE (loc) == CONSTRUCTOR)
17794 {
17795 tree type = TREE_TYPE (loc);
17796 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17797 unsigned HOST_WIDE_INT offset = 0;
17798 unsigned HOST_WIDE_INT cnt;
17799 constructor_elt *ce;
17800
17801 if (TREE_CODE (type) == RECORD_TYPE)
17802 {
17803 /* This is very limited, but it's enough to output
17804 pointers to member functions, as long as the
17805 referenced function is defined in the current
17806 translation unit. */
17807 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17808 {
17809 tree val = ce->value;
17810
17811 tree field = ce->index;
17812
17813 if (val)
17814 STRIP_NOPS (val);
17815
17816 if (!field || DECL_BIT_FIELD (field))
17817 {
17818 expansion_failed (loc, NULL_RTX,
17819 "bitfield in record type constructor");
17820 size = offset = (unsigned HOST_WIDE_INT)-1;
17821 ret = NULL;
17822 break;
17823 }
17824
17825 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17826 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17827 gcc_assert (pos + fieldsize <= size);
17828 if (pos < offset)
17829 {
17830 expansion_failed (loc, NULL_RTX,
17831 "out-of-order fields in record constructor");
17832 size = offset = (unsigned HOST_WIDE_INT)-1;
17833 ret = NULL;
17834 break;
17835 }
17836 if (pos > offset)
17837 {
17838 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17839 add_loc_descr (&ret, ret1);
17840 offset = pos;
17841 }
17842 if (val && fieldsize != 0)
17843 {
17844 ret1 = loc_descriptor_from_tree (val, want_address, context);
17845 if (!ret1)
17846 {
17847 expansion_failed (loc, NULL_RTX,
17848 "unsupported expression in field");
17849 size = offset = (unsigned HOST_WIDE_INT)-1;
17850 ret = NULL;
17851 break;
17852 }
17853 add_loc_descr (&ret, ret1);
17854 }
17855 if (fieldsize)
17856 {
17857 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17858 add_loc_descr (&ret, ret1);
17859 offset = pos + fieldsize;
17860 }
17861 }
17862
17863 if (offset != size)
17864 {
17865 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17866 add_loc_descr (&ret, ret1);
17867 offset = size;
17868 }
17869
17870 have_address = !!want_address;
17871 }
17872 else
17873 expansion_failed (loc, NULL_RTX,
17874 "constructor of non-record type");
17875 }
17876 else
17877 /* We can construct small constants here using int_loc_descriptor. */
17878 expansion_failed (loc, NULL_RTX,
17879 "constructor or constant not in constant pool");
17880 break;
17881
17882 case TRUTH_AND_EXPR:
17883 case TRUTH_ANDIF_EXPR:
17884 case BIT_AND_EXPR:
17885 op = DW_OP_and;
17886 goto do_binop;
17887
17888 case TRUTH_XOR_EXPR:
17889 case BIT_XOR_EXPR:
17890 op = DW_OP_xor;
17891 goto do_binop;
17892
17893 case TRUTH_OR_EXPR:
17894 case TRUTH_ORIF_EXPR:
17895 case BIT_IOR_EXPR:
17896 op = DW_OP_or;
17897 goto do_binop;
17898
17899 case FLOOR_DIV_EXPR:
17900 case CEIL_DIV_EXPR:
17901 case ROUND_DIV_EXPR:
17902 case TRUNC_DIV_EXPR:
17903 case EXACT_DIV_EXPR:
17904 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17905 return 0;
17906 op = DW_OP_div;
17907 goto do_binop;
17908
17909 case MINUS_EXPR:
17910 op = DW_OP_minus;
17911 goto do_binop;
17912
17913 case FLOOR_MOD_EXPR:
17914 case CEIL_MOD_EXPR:
17915 case ROUND_MOD_EXPR:
17916 case TRUNC_MOD_EXPR:
17917 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17918 {
17919 op = DW_OP_mod;
17920 goto do_binop;
17921 }
17922 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17923 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17924 if (list_ret == 0 || list_ret1 == 0)
17925 return 0;
17926
17927 add_loc_list (&list_ret, list_ret1);
17928 if (list_ret == 0)
17929 return 0;
17930 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17931 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17932 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17933 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17934 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17935 break;
17936
17937 case MULT_EXPR:
17938 op = DW_OP_mul;
17939 goto do_binop;
17940
17941 case LSHIFT_EXPR:
17942 op = DW_OP_shl;
17943 goto do_binop;
17944
17945 case RSHIFT_EXPR:
17946 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17947 goto do_binop;
17948
17949 case POINTER_PLUS_EXPR:
17950 case PLUS_EXPR:
17951 do_plus:
17952 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17953 {
17954 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17955 smarter to encode their opposite. The DW_OP_plus_uconst operation
17956 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17957 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17958 bytes, Y being the size of the operation that pushes the opposite
17959 of the addend. So let's choose the smallest representation. */
17960 const tree tree_addend = TREE_OPERAND (loc, 1);
17961 offset_int wi_addend;
17962 HOST_WIDE_INT shwi_addend;
17963 dw_loc_descr_ref loc_naddend;
17964
17965 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17966 if (list_ret == 0)
17967 return 0;
17968
17969 /* Try to get the literal to push. It is the opposite of the addend,
17970 so as we rely on wrapping during DWARF evaluation, first decode
17971 the literal as a "DWARF-sized" signed number. */
17972 wi_addend = wi::to_offset (tree_addend);
17973 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17974 shwi_addend = wi_addend.to_shwi ();
17975 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17976 ? int_loc_descriptor (-shwi_addend)
17977 : NULL;
17978
17979 if (loc_naddend != NULL
17980 && ((unsigned) size_of_uleb128 (shwi_addend)
17981 > size_of_loc_descr (loc_naddend)))
17982 {
17983 add_loc_descr_to_each (list_ret, loc_naddend);
17984 add_loc_descr_to_each (list_ret,
17985 new_loc_descr (DW_OP_minus, 0, 0));
17986 }
17987 else
17988 {
17989 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17990 {
17991 loc_naddend = loc_cur;
17992 loc_cur = loc_cur->dw_loc_next;
17993 ggc_free (loc_naddend);
17994 }
17995 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17996 }
17997 break;
17998 }
17999
18000 op = DW_OP_plus;
18001 goto do_binop;
18002
18003 case LE_EXPR:
18004 op = DW_OP_le;
18005 goto do_comp_binop;
18006
18007 case GE_EXPR:
18008 op = DW_OP_ge;
18009 goto do_comp_binop;
18010
18011 case LT_EXPR:
18012 op = DW_OP_lt;
18013 goto do_comp_binop;
18014
18015 case GT_EXPR:
18016 op = DW_OP_gt;
18017 goto do_comp_binop;
18018
18019 do_comp_binop:
18020 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18021 {
18022 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18023 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18024 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18025 TREE_CODE (loc));
18026 break;
18027 }
18028 else
18029 goto do_binop;
18030
18031 case EQ_EXPR:
18032 op = DW_OP_eq;
18033 goto do_binop;
18034
18035 case NE_EXPR:
18036 op = DW_OP_ne;
18037 goto do_binop;
18038
18039 do_binop:
18040 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18041 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18042 if (list_ret == 0 || list_ret1 == 0)
18043 return 0;
18044
18045 add_loc_list (&list_ret, list_ret1);
18046 if (list_ret == 0)
18047 return 0;
18048 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18049 break;
18050
18051 case TRUTH_NOT_EXPR:
18052 case BIT_NOT_EXPR:
18053 op = DW_OP_not;
18054 goto do_unop;
18055
18056 case ABS_EXPR:
18057 op = DW_OP_abs;
18058 goto do_unop;
18059
18060 case NEGATE_EXPR:
18061 op = DW_OP_neg;
18062 goto do_unop;
18063
18064 do_unop:
18065 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18066 if (list_ret == 0)
18067 return 0;
18068
18069 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18070 break;
18071
18072 case MIN_EXPR:
18073 case MAX_EXPR:
18074 {
18075 const enum tree_code code =
18076 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18077
18078 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18079 build2 (code, integer_type_node,
18080 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18081 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18082 }
18083
18084 /* fall through */
18085
18086 case COND_EXPR:
18087 {
18088 dw_loc_descr_ref lhs
18089 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18090 dw_loc_list_ref rhs
18091 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18092 dw_loc_descr_ref bra_node, jump_node, tmp;
18093
18094 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18095 if (list_ret == 0 || lhs == 0 || rhs == 0)
18096 return 0;
18097
18098 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18099 add_loc_descr_to_each (list_ret, bra_node);
18100
18101 add_loc_list (&list_ret, rhs);
18102 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18103 add_loc_descr_to_each (list_ret, jump_node);
18104
18105 add_loc_descr_to_each (list_ret, lhs);
18106 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18107 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18108
18109 /* ??? Need a node to point the skip at. Use a nop. */
18110 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18111 add_loc_descr_to_each (list_ret, tmp);
18112 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18113 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18114 }
18115 break;
18116
18117 case FIX_TRUNC_EXPR:
18118 return 0;
18119
18120 default:
18121 /* Leave front-end specific codes as simply unknown. This comes
18122 up, for instance, with the C STMT_EXPR. */
18123 if ((unsigned int) TREE_CODE (loc)
18124 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18125 {
18126 expansion_failed (loc, NULL_RTX,
18127 "language specific tree node");
18128 return 0;
18129 }
18130
18131 /* Otherwise this is a generic code; we should just lists all of
18132 these explicitly. We forgot one. */
18133 if (flag_checking)
18134 gcc_unreachable ();
18135
18136 /* In a release build, we want to degrade gracefully: better to
18137 generate incomplete debugging information than to crash. */
18138 return NULL;
18139 }
18140
18141 if (!ret && !list_ret)
18142 return 0;
18143
18144 if (want_address == 2 && !have_address
18145 && (dwarf_version >= 4 || !dwarf_strict))
18146 {
18147 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18148 {
18149 expansion_failed (loc, NULL_RTX,
18150 "DWARF address size mismatch");
18151 return 0;
18152 }
18153 if (ret)
18154 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18155 else
18156 add_loc_descr_to_each (list_ret,
18157 new_loc_descr (DW_OP_stack_value, 0, 0));
18158 have_address = 1;
18159 }
18160 /* Show if we can't fill the request for an address. */
18161 if (want_address && !have_address)
18162 {
18163 expansion_failed (loc, NULL_RTX,
18164 "Want address and only have value");
18165 return 0;
18166 }
18167
18168 gcc_assert (!ret || !list_ret);
18169
18170 /* If we've got an address and don't want one, dereference. */
18171 if (!want_address && have_address)
18172 {
18173 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18174
18175 if (size > DWARF2_ADDR_SIZE || size == -1)
18176 {
18177 expansion_failed (loc, NULL_RTX,
18178 "DWARF address size mismatch");
18179 return 0;
18180 }
18181 else if (size == DWARF2_ADDR_SIZE)
18182 op = DW_OP_deref;
18183 else
18184 op = DW_OP_deref_size;
18185
18186 if (ret)
18187 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18188 else
18189 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18190 }
18191 if (ret)
18192 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18193
18194 return list_ret;
18195 }
18196
18197 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18198 expressions. */
18199
18200 static dw_loc_list_ref
18201 loc_list_from_tree (tree loc, int want_address,
18202 struct loc_descr_context *context)
18203 {
18204 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18205
18206 for (dw_loc_list_ref loc_cur = result;
18207 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18208 loc_descr_without_nops (loc_cur->expr);
18209 return result;
18210 }
18211
18212 /* Same as above but return only single location expression. */
18213 static dw_loc_descr_ref
18214 loc_descriptor_from_tree (tree loc, int want_address,
18215 struct loc_descr_context *context)
18216 {
18217 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18218 if (!ret)
18219 return NULL;
18220 if (ret->dw_loc_next)
18221 {
18222 expansion_failed (loc, NULL_RTX,
18223 "Location list where only loc descriptor needed");
18224 return NULL;
18225 }
18226 return ret->expr;
18227 }
18228
18229 /* Given a value, round it up to the lowest multiple of `boundary'
18230 which is not less than the value itself. */
18231
18232 static inline HOST_WIDE_INT
18233 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18234 {
18235 return (((value + boundary - 1) / boundary) * boundary);
18236 }
18237
18238 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18239 pointer to the declared type for the relevant field variable, or return
18240 `integer_type_node' if the given node turns out to be an
18241 ERROR_MARK node. */
18242
18243 static inline tree
18244 field_type (const_tree decl)
18245 {
18246 tree type;
18247
18248 if (TREE_CODE (decl) == ERROR_MARK)
18249 return integer_type_node;
18250
18251 type = DECL_BIT_FIELD_TYPE (decl);
18252 if (type == NULL_TREE)
18253 type = TREE_TYPE (decl);
18254
18255 return type;
18256 }
18257
18258 /* Given a pointer to a tree node, return the alignment in bits for
18259 it, or else return BITS_PER_WORD if the node actually turns out to
18260 be an ERROR_MARK node. */
18261
18262 static inline unsigned
18263 simple_type_align_in_bits (const_tree type)
18264 {
18265 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18266 }
18267
18268 static inline unsigned
18269 simple_decl_align_in_bits (const_tree decl)
18270 {
18271 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18272 }
18273
18274 /* Return the result of rounding T up to ALIGN. */
18275
18276 static inline offset_int
18277 round_up_to_align (const offset_int &t, unsigned int align)
18278 {
18279 return wi::udiv_trunc (t + align - 1, align) * align;
18280 }
18281
18282 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18283 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18284 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18285 if we fail to return the size in one of these two forms. */
18286
18287 static dw_loc_descr_ref
18288 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18289 {
18290 tree tree_size;
18291 struct loc_descr_context ctx;
18292
18293 /* Return a constant integer in priority, if possible. */
18294 *cst_size = int_size_in_bytes (type);
18295 if (*cst_size != -1)
18296 return NULL;
18297
18298 ctx.context_type = const_cast<tree> (type);
18299 ctx.base_decl = NULL_TREE;
18300 ctx.dpi = NULL;
18301 ctx.placeholder_arg = false;
18302 ctx.placeholder_seen = false;
18303
18304 type = TYPE_MAIN_VARIANT (type);
18305 tree_size = TYPE_SIZE_UNIT (type);
18306 return ((tree_size != NULL_TREE)
18307 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18308 : NULL);
18309 }
18310
18311 /* Helper structure for RECORD_TYPE processing. */
18312 struct vlr_context
18313 {
18314 /* Root RECORD_TYPE. It is needed to generate data member location
18315 descriptions in variable-length records (VLR), but also to cope with
18316 variants, which are composed of nested structures multiplexed with
18317 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18318 function processing a FIELD_DECL, it is required to be non null. */
18319 tree struct_type;
18320 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18321 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18322 this variant part as part of the root record (in storage units). For
18323 regular records, it must be NULL_TREE. */
18324 tree variant_part_offset;
18325 };
18326
18327 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18328 addressed byte of the "containing object" for the given FIELD_DECL. If
18329 possible, return a native constant through CST_OFFSET (in which case NULL is
18330 returned); otherwise return a DWARF expression that computes the offset.
18331
18332 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18333 that offset is, either because the argument turns out to be a pointer to an
18334 ERROR_MARK node, or because the offset expression is too complex for us.
18335
18336 CTX is required: see the comment for VLR_CONTEXT. */
18337
18338 static dw_loc_descr_ref
18339 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18340 HOST_WIDE_INT *cst_offset)
18341 {
18342 tree tree_result;
18343 dw_loc_list_ref loc_result;
18344
18345 *cst_offset = 0;
18346
18347 if (TREE_CODE (decl) == ERROR_MARK)
18348 return NULL;
18349 else
18350 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18351
18352 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18353 case. */
18354 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18355 return NULL;
18356
18357 #ifdef PCC_BITFIELD_TYPE_MATTERS
18358 /* We used to handle only constant offsets in all cases. Now, we handle
18359 properly dynamic byte offsets only when PCC bitfield type doesn't
18360 matter. */
18361 if (PCC_BITFIELD_TYPE_MATTERS
18362 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18363 {
18364 offset_int object_offset_in_bits;
18365 offset_int object_offset_in_bytes;
18366 offset_int bitpos_int;
18367 tree type;
18368 tree field_size_tree;
18369 offset_int deepest_bitpos;
18370 offset_int field_size_in_bits;
18371 unsigned int type_align_in_bits;
18372 unsigned int decl_align_in_bits;
18373 offset_int type_size_in_bits;
18374
18375 bitpos_int = wi::to_offset (bit_position (decl));
18376 type = field_type (decl);
18377 type_size_in_bits = offset_int_type_size_in_bits (type);
18378 type_align_in_bits = simple_type_align_in_bits (type);
18379
18380 field_size_tree = DECL_SIZE (decl);
18381
18382 /* The size could be unspecified if there was an error, or for
18383 a flexible array member. */
18384 if (!field_size_tree)
18385 field_size_tree = bitsize_zero_node;
18386
18387 /* If the size of the field is not constant, use the type size. */
18388 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18389 field_size_in_bits = wi::to_offset (field_size_tree);
18390 else
18391 field_size_in_bits = type_size_in_bits;
18392
18393 decl_align_in_bits = simple_decl_align_in_bits (decl);
18394
18395 /* The GCC front-end doesn't make any attempt to keep track of the
18396 starting bit offset (relative to the start of the containing
18397 structure type) of the hypothetical "containing object" for a
18398 bit-field. Thus, when computing the byte offset value for the
18399 start of the "containing object" of a bit-field, we must deduce
18400 this information on our own. This can be rather tricky to do in
18401 some cases. For example, handling the following structure type
18402 definition when compiling for an i386/i486 target (which only
18403 aligns long long's to 32-bit boundaries) can be very tricky:
18404
18405 struct S { int field1; long long field2:31; };
18406
18407 Fortunately, there is a simple rule-of-thumb which can be used
18408 in such cases. When compiling for an i386/i486, GCC will
18409 allocate 8 bytes for the structure shown above. It decides to
18410 do this based upon one simple rule for bit-field allocation.
18411 GCC allocates each "containing object" for each bit-field at
18412 the first (i.e. lowest addressed) legitimate alignment boundary
18413 (based upon the required minimum alignment for the declared
18414 type of the field) which it can possibly use, subject to the
18415 condition that there is still enough available space remaining
18416 in the containing object (when allocated at the selected point)
18417 to fully accommodate all of the bits of the bit-field itself.
18418
18419 This simple rule makes it obvious why GCC allocates 8 bytes for
18420 each object of the structure type shown above. When looking
18421 for a place to allocate the "containing object" for `field2',
18422 the compiler simply tries to allocate a 64-bit "containing
18423 object" at each successive 32-bit boundary (starting at zero)
18424 until it finds a place to allocate that 64- bit field such that
18425 at least 31 contiguous (and previously unallocated) bits remain
18426 within that selected 64 bit field. (As it turns out, for the
18427 example above, the compiler finds it is OK to allocate the
18428 "containing object" 64-bit field at bit-offset zero within the
18429 structure type.)
18430
18431 Here we attempt to work backwards from the limited set of facts
18432 we're given, and we try to deduce from those facts, where GCC
18433 must have believed that the containing object started (within
18434 the structure type). The value we deduce is then used (by the
18435 callers of this routine) to generate DW_AT_location and
18436 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18437 the case of DW_AT_location, regular fields as well). */
18438
18439 /* Figure out the bit-distance from the start of the structure to
18440 the "deepest" bit of the bit-field. */
18441 deepest_bitpos = bitpos_int + field_size_in_bits;
18442
18443 /* This is the tricky part. Use some fancy footwork to deduce
18444 where the lowest addressed bit of the containing object must
18445 be. */
18446 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18447
18448 /* Round up to type_align by default. This works best for
18449 bitfields. */
18450 object_offset_in_bits
18451 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18452
18453 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18454 {
18455 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18456
18457 /* Round up to decl_align instead. */
18458 object_offset_in_bits
18459 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18460 }
18461
18462 object_offset_in_bytes
18463 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18464 if (ctx->variant_part_offset == NULL_TREE)
18465 {
18466 *cst_offset = object_offset_in_bytes.to_shwi ();
18467 return NULL;
18468 }
18469 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18470 }
18471 else
18472 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18473 tree_result = byte_position (decl);
18474
18475 if (ctx->variant_part_offset != NULL_TREE)
18476 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18477 ctx->variant_part_offset, tree_result);
18478
18479 /* If the byte offset is a constant, it's simplier to handle a native
18480 constant rather than a DWARF expression. */
18481 if (TREE_CODE (tree_result) == INTEGER_CST)
18482 {
18483 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18484 return NULL;
18485 }
18486 struct loc_descr_context loc_ctx = {
18487 ctx->struct_type, /* context_type */
18488 NULL_TREE, /* base_decl */
18489 NULL, /* dpi */
18490 false, /* placeholder_arg */
18491 false /* placeholder_seen */
18492 };
18493 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18494
18495 /* We want a DWARF expression: abort if we only have a location list with
18496 multiple elements. */
18497 if (!loc_result || !single_element_loc_list_p (loc_result))
18498 return NULL;
18499 else
18500 return loc_result->expr;
18501 }
18502 \f
18503 /* The following routines define various Dwarf attributes and any data
18504 associated with them. */
18505
18506 /* Add a location description attribute value to a DIE.
18507
18508 This emits location attributes suitable for whole variables and
18509 whole parameters. Note that the location attributes for struct fields are
18510 generated by the routine `data_member_location_attribute' below. */
18511
18512 static inline void
18513 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18514 dw_loc_list_ref descr)
18515 {
18516 if (descr == 0)
18517 return;
18518 if (single_element_loc_list_p (descr))
18519 add_AT_loc (die, attr_kind, descr->expr);
18520 else
18521 add_AT_loc_list (die, attr_kind, descr);
18522 }
18523
18524 /* Add DW_AT_accessibility attribute to DIE if needed. */
18525
18526 static void
18527 add_accessibility_attribute (dw_die_ref die, tree decl)
18528 {
18529 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18530 children, otherwise the default is DW_ACCESS_public. In DWARF2
18531 the default has always been DW_ACCESS_public. */
18532 if (TREE_PROTECTED (decl))
18533 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18534 else if (TREE_PRIVATE (decl))
18535 {
18536 if (dwarf_version == 2
18537 || die->die_parent == NULL
18538 || die->die_parent->die_tag != DW_TAG_class_type)
18539 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18540 }
18541 else if (dwarf_version > 2
18542 && die->die_parent
18543 && die->die_parent->die_tag == DW_TAG_class_type)
18544 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18545 }
18546
18547 /* Attach the specialized form of location attribute used for data members of
18548 struct and union types. In the special case of a FIELD_DECL node which
18549 represents a bit-field, the "offset" part of this special location
18550 descriptor must indicate the distance in bytes from the lowest-addressed
18551 byte of the containing struct or union type to the lowest-addressed byte of
18552 the "containing object" for the bit-field. (See the `field_byte_offset'
18553 function above).
18554
18555 For any given bit-field, the "containing object" is a hypothetical object
18556 (of some integral or enum type) within which the given bit-field lives. The
18557 type of this hypothetical "containing object" is always the same as the
18558 declared type of the individual bit-field itself (for GCC anyway... the
18559 DWARF spec doesn't actually mandate this). Note that it is the size (in
18560 bytes) of the hypothetical "containing object" which will be given in the
18561 DW_AT_byte_size attribute for this bit-field. (See the
18562 `byte_size_attribute' function below.) It is also used when calculating the
18563 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18564 function below.)
18565
18566 CTX is required: see the comment for VLR_CONTEXT. */
18567
18568 static void
18569 add_data_member_location_attribute (dw_die_ref die,
18570 tree decl,
18571 struct vlr_context *ctx)
18572 {
18573 HOST_WIDE_INT offset;
18574 dw_loc_descr_ref loc_descr = 0;
18575
18576 if (TREE_CODE (decl) == TREE_BINFO)
18577 {
18578 /* We're working on the TAG_inheritance for a base class. */
18579 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18580 {
18581 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18582 aren't at a fixed offset from all (sub)objects of the same
18583 type. We need to extract the appropriate offset from our
18584 vtable. The following dwarf expression means
18585
18586 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18587
18588 This is specific to the V3 ABI, of course. */
18589
18590 dw_loc_descr_ref tmp;
18591
18592 /* Make a copy of the object address. */
18593 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18594 add_loc_descr (&loc_descr, tmp);
18595
18596 /* Extract the vtable address. */
18597 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18598 add_loc_descr (&loc_descr, tmp);
18599
18600 /* Calculate the address of the offset. */
18601 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18602 gcc_assert (offset < 0);
18603
18604 tmp = int_loc_descriptor (-offset);
18605 add_loc_descr (&loc_descr, tmp);
18606 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18607 add_loc_descr (&loc_descr, tmp);
18608
18609 /* Extract the offset. */
18610 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18611 add_loc_descr (&loc_descr, tmp);
18612
18613 /* Add it to the object address. */
18614 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18615 add_loc_descr (&loc_descr, tmp);
18616 }
18617 else
18618 offset = tree_to_shwi (BINFO_OFFSET (decl));
18619 }
18620 else
18621 {
18622 loc_descr = field_byte_offset (decl, ctx, &offset);
18623
18624 /* If loc_descr is available then we know the field offset is dynamic.
18625 However, GDB does not handle dynamic field offsets very well at the
18626 moment. */
18627 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18628 {
18629 loc_descr = NULL;
18630 offset = 0;
18631 }
18632
18633 /* Data member location evalutation starts with the base address on the
18634 stack. Compute the field offset and add it to this base address. */
18635 else if (loc_descr != NULL)
18636 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18637 }
18638
18639 if (! loc_descr)
18640 {
18641 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18642 e.g. GDB only added support to it in November 2016. For DWARF5
18643 we need newer debug info consumers anyway. We might change this
18644 to dwarf_version >= 4 once most consumers catched up. */
18645 if (dwarf_version >= 5
18646 && TREE_CODE (decl) == FIELD_DECL
18647 && DECL_BIT_FIELD_TYPE (decl))
18648 {
18649 tree off = bit_position (decl);
18650 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18651 {
18652 remove_AT (die, DW_AT_byte_size);
18653 remove_AT (die, DW_AT_bit_offset);
18654 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18655 return;
18656 }
18657 }
18658 if (dwarf_version > 2)
18659 {
18660 /* Don't need to output a location expression, just the constant. */
18661 if (offset < 0)
18662 add_AT_int (die, DW_AT_data_member_location, offset);
18663 else
18664 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18665 return;
18666 }
18667 else
18668 {
18669 enum dwarf_location_atom op;
18670
18671 /* The DWARF2 standard says that we should assume that the structure
18672 address is already on the stack, so we can specify a structure
18673 field address by using DW_OP_plus_uconst. */
18674 op = DW_OP_plus_uconst;
18675 loc_descr = new_loc_descr (op, offset, 0);
18676 }
18677 }
18678
18679 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18680 }
18681
18682 /* Writes integer values to dw_vec_const array. */
18683
18684 static void
18685 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18686 {
18687 while (size != 0)
18688 {
18689 *dest++ = val & 0xff;
18690 val >>= 8;
18691 --size;
18692 }
18693 }
18694
18695 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18696
18697 static HOST_WIDE_INT
18698 extract_int (const unsigned char *src, unsigned int size)
18699 {
18700 HOST_WIDE_INT val = 0;
18701
18702 src += size;
18703 while (size != 0)
18704 {
18705 val <<= 8;
18706 val |= *--src & 0xff;
18707 --size;
18708 }
18709 return val;
18710 }
18711
18712 /* Writes wide_int values to dw_vec_const array. */
18713
18714 static void
18715 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18716 {
18717 int i;
18718
18719 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18720 {
18721 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18722 return;
18723 }
18724
18725 /* We'd have to extend this code to support odd sizes. */
18726 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18727
18728 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18729
18730 if (WORDS_BIG_ENDIAN)
18731 for (i = n - 1; i >= 0; i--)
18732 {
18733 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18734 dest += sizeof (HOST_WIDE_INT);
18735 }
18736 else
18737 for (i = 0; i < n; i++)
18738 {
18739 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18740 dest += sizeof (HOST_WIDE_INT);
18741 }
18742 }
18743
18744 /* Writes floating point values to dw_vec_const array. */
18745
18746 static void
18747 insert_float (const_rtx rtl, unsigned char *array)
18748 {
18749 long val[4];
18750 int i;
18751 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18752
18753 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18754
18755 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18756 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18757 {
18758 insert_int (val[i], 4, array);
18759 array += 4;
18760 }
18761 }
18762
18763 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18764 does not have a "location" either in memory or in a register. These
18765 things can arise in GNU C when a constant is passed as an actual parameter
18766 to an inlined function. They can also arise in C++ where declared
18767 constants do not necessarily get memory "homes". */
18768
18769 static bool
18770 add_const_value_attribute (dw_die_ref die, rtx rtl)
18771 {
18772 switch (GET_CODE (rtl))
18773 {
18774 case CONST_INT:
18775 {
18776 HOST_WIDE_INT val = INTVAL (rtl);
18777
18778 if (val < 0)
18779 add_AT_int (die, DW_AT_const_value, val);
18780 else
18781 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18782 }
18783 return true;
18784
18785 case CONST_WIDE_INT:
18786 {
18787 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18788 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18789 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18790 wide_int w = wi::zext (w1, prec);
18791 add_AT_wide (die, DW_AT_const_value, w);
18792 }
18793 return true;
18794
18795 case CONST_DOUBLE:
18796 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18797 floating-point constant. A CONST_DOUBLE is used whenever the
18798 constant requires more than one word in order to be adequately
18799 represented. */
18800 if (TARGET_SUPPORTS_WIDE_INT == 0
18801 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18802 add_AT_double (die, DW_AT_const_value,
18803 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18804 else
18805 {
18806 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18807 unsigned int length = GET_MODE_SIZE (mode);
18808 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18809
18810 insert_float (rtl, array);
18811 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18812 }
18813 return true;
18814
18815 case CONST_VECTOR:
18816 {
18817 machine_mode mode = GET_MODE (rtl);
18818 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18819 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18820 unsigned char *array
18821 = ggc_vec_alloc<unsigned char> (length * elt_size);
18822 unsigned int i;
18823 unsigned char *p;
18824 machine_mode imode = GET_MODE_INNER (mode);
18825
18826 switch (GET_MODE_CLASS (mode))
18827 {
18828 case MODE_VECTOR_INT:
18829 for (i = 0, p = array; i < length; i++, p += elt_size)
18830 {
18831 rtx elt = CONST_VECTOR_ELT (rtl, i);
18832 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18833 }
18834 break;
18835
18836 case MODE_VECTOR_FLOAT:
18837 for (i = 0, p = array; i < length; i++, p += elt_size)
18838 {
18839 rtx elt = CONST_VECTOR_ELT (rtl, i);
18840 insert_float (elt, p);
18841 }
18842 break;
18843
18844 default:
18845 gcc_unreachable ();
18846 }
18847
18848 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18849 }
18850 return true;
18851
18852 case CONST_STRING:
18853 if (dwarf_version >= 4 || !dwarf_strict)
18854 {
18855 dw_loc_descr_ref loc_result;
18856 resolve_one_addr (&rtl);
18857 rtl_addr:
18858 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18859 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18860 add_AT_loc (die, DW_AT_location, loc_result);
18861 vec_safe_push (used_rtx_array, rtl);
18862 return true;
18863 }
18864 return false;
18865
18866 case CONST:
18867 if (CONSTANT_P (XEXP (rtl, 0)))
18868 return add_const_value_attribute (die, XEXP (rtl, 0));
18869 /* FALLTHROUGH */
18870 case SYMBOL_REF:
18871 if (!const_ok_for_output (rtl))
18872 return false;
18873 /* FALLTHROUGH */
18874 case LABEL_REF:
18875 if (dwarf_version >= 4 || !dwarf_strict)
18876 goto rtl_addr;
18877 return false;
18878
18879 case PLUS:
18880 /* In cases where an inlined instance of an inline function is passed
18881 the address of an `auto' variable (which is local to the caller) we
18882 can get a situation where the DECL_RTL of the artificial local
18883 variable (for the inlining) which acts as a stand-in for the
18884 corresponding formal parameter (of the inline function) will look
18885 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18886 exactly a compile-time constant expression, but it isn't the address
18887 of the (artificial) local variable either. Rather, it represents the
18888 *value* which the artificial local variable always has during its
18889 lifetime. We currently have no way to represent such quasi-constant
18890 values in Dwarf, so for now we just punt and generate nothing. */
18891 return false;
18892
18893 case HIGH:
18894 case CONST_FIXED:
18895 return false;
18896
18897 case MEM:
18898 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18899 && MEM_READONLY_P (rtl)
18900 && GET_MODE (rtl) == BLKmode)
18901 {
18902 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18903 return true;
18904 }
18905 return false;
18906
18907 default:
18908 /* No other kinds of rtx should be possible here. */
18909 gcc_unreachable ();
18910 }
18911 return false;
18912 }
18913
18914 /* Determine whether the evaluation of EXPR references any variables
18915 or functions which aren't otherwise used (and therefore may not be
18916 output). */
18917 static tree
18918 reference_to_unused (tree * tp, int * walk_subtrees,
18919 void * data ATTRIBUTE_UNUSED)
18920 {
18921 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18922 *walk_subtrees = 0;
18923
18924 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18925 && ! TREE_ASM_WRITTEN (*tp))
18926 return *tp;
18927 /* ??? The C++ FE emits debug information for using decls, so
18928 putting gcc_unreachable here falls over. See PR31899. For now
18929 be conservative. */
18930 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18931 return *tp;
18932 else if (VAR_P (*tp))
18933 {
18934 varpool_node *node = varpool_node::get (*tp);
18935 if (!node || !node->definition)
18936 return *tp;
18937 }
18938 else if (TREE_CODE (*tp) == FUNCTION_DECL
18939 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18940 {
18941 /* The call graph machinery must have finished analyzing,
18942 optimizing and gimplifying the CU by now.
18943 So if *TP has no call graph node associated
18944 to it, it means *TP will not be emitted. */
18945 if (!cgraph_node::get (*tp))
18946 return *tp;
18947 }
18948 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18949 return *tp;
18950
18951 return NULL_TREE;
18952 }
18953
18954 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18955 for use in a later add_const_value_attribute call. */
18956
18957 static rtx
18958 rtl_for_decl_init (tree init, tree type)
18959 {
18960 rtx rtl = NULL_RTX;
18961
18962 STRIP_NOPS (init);
18963
18964 /* If a variable is initialized with a string constant without embedded
18965 zeros, build CONST_STRING. */
18966 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18967 {
18968 tree enttype = TREE_TYPE (type);
18969 tree domain = TYPE_DOMAIN (type);
18970 scalar_int_mode mode;
18971
18972 if (is_int_mode (TYPE_MODE (enttype), &mode)
18973 && GET_MODE_SIZE (mode) == 1
18974 && domain
18975 && integer_zerop (TYPE_MIN_VALUE (domain))
18976 && compare_tree_int (TYPE_MAX_VALUE (domain),
18977 TREE_STRING_LENGTH (init) - 1) == 0
18978 && ((size_t) TREE_STRING_LENGTH (init)
18979 == strlen (TREE_STRING_POINTER (init)) + 1))
18980 {
18981 rtl = gen_rtx_CONST_STRING (VOIDmode,
18982 ggc_strdup (TREE_STRING_POINTER (init)));
18983 rtl = gen_rtx_MEM (BLKmode, rtl);
18984 MEM_READONLY_P (rtl) = 1;
18985 }
18986 }
18987 /* Other aggregates, and complex values, could be represented using
18988 CONCAT: FIXME! */
18989 else if (AGGREGATE_TYPE_P (type)
18990 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18991 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18992 || TREE_CODE (type) == COMPLEX_TYPE)
18993 ;
18994 /* Vectors only work if their mode is supported by the target.
18995 FIXME: generic vectors ought to work too. */
18996 else if (TREE_CODE (type) == VECTOR_TYPE
18997 && !VECTOR_MODE_P (TYPE_MODE (type)))
18998 ;
18999 /* If the initializer is something that we know will expand into an
19000 immediate RTL constant, expand it now. We must be careful not to
19001 reference variables which won't be output. */
19002 else if (initializer_constant_valid_p (init, type)
19003 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19004 {
19005 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19006 possible. */
19007 if (TREE_CODE (type) == VECTOR_TYPE)
19008 switch (TREE_CODE (init))
19009 {
19010 case VECTOR_CST:
19011 break;
19012 case CONSTRUCTOR:
19013 if (TREE_CONSTANT (init))
19014 {
19015 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19016 bool constant_p = true;
19017 tree value;
19018 unsigned HOST_WIDE_INT ix;
19019
19020 /* Even when ctor is constant, it might contain non-*_CST
19021 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19022 belong into VECTOR_CST nodes. */
19023 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19024 if (!CONSTANT_CLASS_P (value))
19025 {
19026 constant_p = false;
19027 break;
19028 }
19029
19030 if (constant_p)
19031 {
19032 init = build_vector_from_ctor (type, elts);
19033 break;
19034 }
19035 }
19036 /* FALLTHRU */
19037
19038 default:
19039 return NULL;
19040 }
19041
19042 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19043
19044 /* If expand_expr returns a MEM, it wasn't immediate. */
19045 gcc_assert (!rtl || !MEM_P (rtl));
19046 }
19047
19048 return rtl;
19049 }
19050
19051 /* Generate RTL for the variable DECL to represent its location. */
19052
19053 static rtx
19054 rtl_for_decl_location (tree decl)
19055 {
19056 rtx rtl;
19057
19058 /* Here we have to decide where we are going to say the parameter "lives"
19059 (as far as the debugger is concerned). We only have a couple of
19060 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19061
19062 DECL_RTL normally indicates where the parameter lives during most of the
19063 activation of the function. If optimization is enabled however, this
19064 could be either NULL or else a pseudo-reg. Both of those cases indicate
19065 that the parameter doesn't really live anywhere (as far as the code
19066 generation parts of GCC are concerned) during most of the function's
19067 activation. That will happen (for example) if the parameter is never
19068 referenced within the function.
19069
19070 We could just generate a location descriptor here for all non-NULL
19071 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19072 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19073 where DECL_RTL is NULL or is a pseudo-reg.
19074
19075 Note however that we can only get away with using DECL_INCOMING_RTL as
19076 a backup substitute for DECL_RTL in certain limited cases. In cases
19077 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19078 we can be sure that the parameter was passed using the same type as it is
19079 declared to have within the function, and that its DECL_INCOMING_RTL
19080 points us to a place where a value of that type is passed.
19081
19082 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19083 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19084 because in these cases DECL_INCOMING_RTL points us to a value of some
19085 type which is *different* from the type of the parameter itself. Thus,
19086 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19087 such cases, the debugger would end up (for example) trying to fetch a
19088 `float' from a place which actually contains the first part of a
19089 `double'. That would lead to really incorrect and confusing
19090 output at debug-time.
19091
19092 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19093 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19094 are a couple of exceptions however. On little-endian machines we can
19095 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19096 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19097 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19098 when (on a little-endian machine) a non-prototyped function has a
19099 parameter declared to be of type `short' or `char'. In such cases,
19100 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19101 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19102 passed `int' value. If the debugger then uses that address to fetch
19103 a `short' or a `char' (on a little-endian machine) the result will be
19104 the correct data, so we allow for such exceptional cases below.
19105
19106 Note that our goal here is to describe the place where the given formal
19107 parameter lives during most of the function's activation (i.e. between the
19108 end of the prologue and the start of the epilogue). We'll do that as best
19109 as we can. Note however that if the given formal parameter is modified
19110 sometime during the execution of the function, then a stack backtrace (at
19111 debug-time) will show the function as having been called with the *new*
19112 value rather than the value which was originally passed in. This happens
19113 rarely enough that it is not a major problem, but it *is* a problem, and
19114 I'd like to fix it.
19115
19116 A future version of dwarf2out.c may generate two additional attributes for
19117 any given DW_TAG_formal_parameter DIE which will describe the "passed
19118 type" and the "passed location" for the given formal parameter in addition
19119 to the attributes we now generate to indicate the "declared type" and the
19120 "active location" for each parameter. This additional set of attributes
19121 could be used by debuggers for stack backtraces. Separately, note that
19122 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19123 This happens (for example) for inlined-instances of inline function formal
19124 parameters which are never referenced. This really shouldn't be
19125 happening. All PARM_DECL nodes should get valid non-NULL
19126 DECL_INCOMING_RTL values. FIXME. */
19127
19128 /* Use DECL_RTL as the "location" unless we find something better. */
19129 rtl = DECL_RTL_IF_SET (decl);
19130
19131 /* When generating abstract instances, ignore everything except
19132 constants, symbols living in memory, and symbols living in
19133 fixed registers. */
19134 if (! reload_completed)
19135 {
19136 if (rtl
19137 && (CONSTANT_P (rtl)
19138 || (MEM_P (rtl)
19139 && CONSTANT_P (XEXP (rtl, 0)))
19140 || (REG_P (rtl)
19141 && VAR_P (decl)
19142 && TREE_STATIC (decl))))
19143 {
19144 rtl = targetm.delegitimize_address (rtl);
19145 return rtl;
19146 }
19147 rtl = NULL_RTX;
19148 }
19149 else if (TREE_CODE (decl) == PARM_DECL)
19150 {
19151 if (rtl == NULL_RTX
19152 || is_pseudo_reg (rtl)
19153 || (MEM_P (rtl)
19154 && is_pseudo_reg (XEXP (rtl, 0))
19155 && DECL_INCOMING_RTL (decl)
19156 && MEM_P (DECL_INCOMING_RTL (decl))
19157 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19158 {
19159 tree declared_type = TREE_TYPE (decl);
19160 tree passed_type = DECL_ARG_TYPE (decl);
19161 machine_mode dmode = TYPE_MODE (declared_type);
19162 machine_mode pmode = TYPE_MODE (passed_type);
19163
19164 /* This decl represents a formal parameter which was optimized out.
19165 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19166 all cases where (rtl == NULL_RTX) just below. */
19167 if (dmode == pmode)
19168 rtl = DECL_INCOMING_RTL (decl);
19169 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19170 && SCALAR_INT_MODE_P (dmode)
19171 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19172 && DECL_INCOMING_RTL (decl))
19173 {
19174 rtx inc = DECL_INCOMING_RTL (decl);
19175 if (REG_P (inc))
19176 rtl = inc;
19177 else if (MEM_P (inc))
19178 {
19179 if (BYTES_BIG_ENDIAN)
19180 rtl = adjust_address_nv (inc, dmode,
19181 GET_MODE_SIZE (pmode)
19182 - GET_MODE_SIZE (dmode));
19183 else
19184 rtl = inc;
19185 }
19186 }
19187 }
19188
19189 /* If the parm was passed in registers, but lives on the stack, then
19190 make a big endian correction if the mode of the type of the
19191 parameter is not the same as the mode of the rtl. */
19192 /* ??? This is the same series of checks that are made in dbxout.c before
19193 we reach the big endian correction code there. It isn't clear if all
19194 of these checks are necessary here, but keeping them all is the safe
19195 thing to do. */
19196 else if (MEM_P (rtl)
19197 && XEXP (rtl, 0) != const0_rtx
19198 && ! CONSTANT_P (XEXP (rtl, 0))
19199 /* Not passed in memory. */
19200 && !MEM_P (DECL_INCOMING_RTL (decl))
19201 /* Not passed by invisible reference. */
19202 && (!REG_P (XEXP (rtl, 0))
19203 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19204 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19205 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19206 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19207 #endif
19208 )
19209 /* Big endian correction check. */
19210 && BYTES_BIG_ENDIAN
19211 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19212 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19213 < UNITS_PER_WORD))
19214 {
19215 machine_mode addr_mode = get_address_mode (rtl);
19216 int offset = (UNITS_PER_WORD
19217 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19218
19219 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19220 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19221 }
19222 }
19223 else if (VAR_P (decl)
19224 && rtl
19225 && MEM_P (rtl)
19226 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19227 {
19228 machine_mode addr_mode = get_address_mode (rtl);
19229 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19230 GET_MODE (rtl));
19231
19232 /* If a variable is declared "register" yet is smaller than
19233 a register, then if we store the variable to memory, it
19234 looks like we're storing a register-sized value, when in
19235 fact we are not. We need to adjust the offset of the
19236 storage location to reflect the actual value's bytes,
19237 else gdb will not be able to display it. */
19238 if (offset != 0)
19239 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19240 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19241 }
19242
19243 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19244 and will have been substituted directly into all expressions that use it.
19245 C does not have such a concept, but C++ and other languages do. */
19246 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19247 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19248
19249 if (rtl)
19250 rtl = targetm.delegitimize_address (rtl);
19251
19252 /* If we don't look past the constant pool, we risk emitting a
19253 reference to a constant pool entry that isn't referenced from
19254 code, and thus is not emitted. */
19255 if (rtl)
19256 rtl = avoid_constant_pool_reference (rtl);
19257
19258 /* Try harder to get a rtl. If this symbol ends up not being emitted
19259 in the current CU, resolve_addr will remove the expression referencing
19260 it. */
19261 if (rtl == NULL_RTX
19262 && VAR_P (decl)
19263 && !DECL_EXTERNAL (decl)
19264 && TREE_STATIC (decl)
19265 && DECL_NAME (decl)
19266 && !DECL_HARD_REGISTER (decl)
19267 && DECL_MODE (decl) != VOIDmode)
19268 {
19269 rtl = make_decl_rtl_for_debug (decl);
19270 if (!MEM_P (rtl)
19271 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19272 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19273 rtl = NULL_RTX;
19274 }
19275
19276 return rtl;
19277 }
19278
19279 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19280 returned. If so, the decl for the COMMON block is returned, and the
19281 value is the offset into the common block for the symbol. */
19282
19283 static tree
19284 fortran_common (tree decl, HOST_WIDE_INT *value)
19285 {
19286 tree val_expr, cvar;
19287 machine_mode mode;
19288 HOST_WIDE_INT bitsize, bitpos;
19289 tree offset;
19290 int unsignedp, reversep, volatilep = 0;
19291
19292 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19293 it does not have a value (the offset into the common area), or if it
19294 is thread local (as opposed to global) then it isn't common, and shouldn't
19295 be handled as such. */
19296 if (!VAR_P (decl)
19297 || !TREE_STATIC (decl)
19298 || !DECL_HAS_VALUE_EXPR_P (decl)
19299 || !is_fortran ())
19300 return NULL_TREE;
19301
19302 val_expr = DECL_VALUE_EXPR (decl);
19303 if (TREE_CODE (val_expr) != COMPONENT_REF)
19304 return NULL_TREE;
19305
19306 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19307 &unsignedp, &reversep, &volatilep);
19308
19309 if (cvar == NULL_TREE
19310 || !VAR_P (cvar)
19311 || DECL_ARTIFICIAL (cvar)
19312 || !TREE_PUBLIC (cvar))
19313 return NULL_TREE;
19314
19315 *value = 0;
19316 if (offset != NULL)
19317 {
19318 if (!tree_fits_shwi_p (offset))
19319 return NULL_TREE;
19320 *value = tree_to_shwi (offset);
19321 }
19322 if (bitpos != 0)
19323 *value += bitpos / BITS_PER_UNIT;
19324
19325 return cvar;
19326 }
19327
19328 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19329 data attribute for a variable or a parameter. We generate the
19330 DW_AT_const_value attribute only in those cases where the given variable
19331 or parameter does not have a true "location" either in memory or in a
19332 register. This can happen (for example) when a constant is passed as an
19333 actual argument in a call to an inline function. (It's possible that
19334 these things can crop up in other ways also.) Note that one type of
19335 constant value which can be passed into an inlined function is a constant
19336 pointer. This can happen for example if an actual argument in an inlined
19337 function call evaluates to a compile-time constant address.
19338
19339 CACHE_P is true if it is worth caching the location list for DECL,
19340 so that future calls can reuse it rather than regenerate it from scratch.
19341 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19342 since we will need to refer to them each time the function is inlined. */
19343
19344 static bool
19345 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19346 {
19347 rtx rtl;
19348 dw_loc_list_ref list;
19349 var_loc_list *loc_list;
19350 cached_dw_loc_list *cache;
19351
19352 if (early_dwarf)
19353 return false;
19354
19355 if (TREE_CODE (decl) == ERROR_MARK)
19356 return false;
19357
19358 if (get_AT (die, DW_AT_location)
19359 || get_AT (die, DW_AT_const_value))
19360 return true;
19361
19362 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19363 || TREE_CODE (decl) == RESULT_DECL);
19364
19365 /* Try to get some constant RTL for this decl, and use that as the value of
19366 the location. */
19367
19368 rtl = rtl_for_decl_location (decl);
19369 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19370 && add_const_value_attribute (die, rtl))
19371 return true;
19372
19373 /* See if we have single element location list that is equivalent to
19374 a constant value. That way we are better to use add_const_value_attribute
19375 rather than expanding constant value equivalent. */
19376 loc_list = lookup_decl_loc (decl);
19377 if (loc_list
19378 && loc_list->first
19379 && loc_list->first->next == NULL
19380 && NOTE_P (loc_list->first->loc)
19381 && NOTE_VAR_LOCATION (loc_list->first->loc)
19382 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19383 {
19384 struct var_loc_node *node;
19385
19386 node = loc_list->first;
19387 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19388 if (GET_CODE (rtl) == EXPR_LIST)
19389 rtl = XEXP (rtl, 0);
19390 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19391 && add_const_value_attribute (die, rtl))
19392 return true;
19393 }
19394 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19395 list several times. See if we've already cached the contents. */
19396 list = NULL;
19397 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19398 cache_p = false;
19399 if (cache_p)
19400 {
19401 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19402 if (cache)
19403 list = cache->loc_list;
19404 }
19405 if (list == NULL)
19406 {
19407 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19408 NULL);
19409 /* It is usually worth caching this result if the decl is from
19410 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19411 if (cache_p && list && list->dw_loc_next)
19412 {
19413 cached_dw_loc_list **slot
19414 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19415 DECL_UID (decl),
19416 INSERT);
19417 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19418 cache->decl_id = DECL_UID (decl);
19419 cache->loc_list = list;
19420 *slot = cache;
19421 }
19422 }
19423 if (list)
19424 {
19425 add_AT_location_description (die, DW_AT_location, list);
19426 return true;
19427 }
19428 /* None of that worked, so it must not really have a location;
19429 try adding a constant value attribute from the DECL_INITIAL. */
19430 return tree_add_const_value_attribute_for_decl (die, decl);
19431 }
19432
19433 /* Helper function for tree_add_const_value_attribute. Natively encode
19434 initializer INIT into an array. Return true if successful. */
19435
19436 static bool
19437 native_encode_initializer (tree init, unsigned char *array, int size)
19438 {
19439 tree type;
19440
19441 if (init == NULL_TREE)
19442 return false;
19443
19444 STRIP_NOPS (init);
19445 switch (TREE_CODE (init))
19446 {
19447 case STRING_CST:
19448 type = TREE_TYPE (init);
19449 if (TREE_CODE (type) == ARRAY_TYPE)
19450 {
19451 tree enttype = TREE_TYPE (type);
19452 scalar_int_mode mode;
19453
19454 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19455 || GET_MODE_SIZE (mode) != 1)
19456 return false;
19457 if (int_size_in_bytes (type) != size)
19458 return false;
19459 if (size > TREE_STRING_LENGTH (init))
19460 {
19461 memcpy (array, TREE_STRING_POINTER (init),
19462 TREE_STRING_LENGTH (init));
19463 memset (array + TREE_STRING_LENGTH (init),
19464 '\0', size - TREE_STRING_LENGTH (init));
19465 }
19466 else
19467 memcpy (array, TREE_STRING_POINTER (init), size);
19468 return true;
19469 }
19470 return false;
19471 case CONSTRUCTOR:
19472 type = TREE_TYPE (init);
19473 if (int_size_in_bytes (type) != size)
19474 return false;
19475 if (TREE_CODE (type) == ARRAY_TYPE)
19476 {
19477 HOST_WIDE_INT min_index;
19478 unsigned HOST_WIDE_INT cnt;
19479 int curpos = 0, fieldsize;
19480 constructor_elt *ce;
19481
19482 if (TYPE_DOMAIN (type) == NULL_TREE
19483 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19484 return false;
19485
19486 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19487 if (fieldsize <= 0)
19488 return false;
19489
19490 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19491 memset (array, '\0', size);
19492 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19493 {
19494 tree val = ce->value;
19495 tree index = ce->index;
19496 int pos = curpos;
19497 if (index && TREE_CODE (index) == RANGE_EXPR)
19498 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19499 * fieldsize;
19500 else if (index)
19501 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19502
19503 if (val)
19504 {
19505 STRIP_NOPS (val);
19506 if (!native_encode_initializer (val, array + pos, fieldsize))
19507 return false;
19508 }
19509 curpos = pos + fieldsize;
19510 if (index && TREE_CODE (index) == RANGE_EXPR)
19511 {
19512 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19513 - tree_to_shwi (TREE_OPERAND (index, 0));
19514 while (count-- > 0)
19515 {
19516 if (val)
19517 memcpy (array + curpos, array + pos, fieldsize);
19518 curpos += fieldsize;
19519 }
19520 }
19521 gcc_assert (curpos <= size);
19522 }
19523 return true;
19524 }
19525 else if (TREE_CODE (type) == RECORD_TYPE
19526 || TREE_CODE (type) == UNION_TYPE)
19527 {
19528 tree field = NULL_TREE;
19529 unsigned HOST_WIDE_INT cnt;
19530 constructor_elt *ce;
19531
19532 if (int_size_in_bytes (type) != size)
19533 return false;
19534
19535 if (TREE_CODE (type) == RECORD_TYPE)
19536 field = TYPE_FIELDS (type);
19537
19538 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19539 {
19540 tree val = ce->value;
19541 int pos, fieldsize;
19542
19543 if (ce->index != 0)
19544 field = ce->index;
19545
19546 if (val)
19547 STRIP_NOPS (val);
19548
19549 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19550 return false;
19551
19552 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19553 && TYPE_DOMAIN (TREE_TYPE (field))
19554 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19555 return false;
19556 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19557 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19558 return false;
19559 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19560 pos = int_byte_position (field);
19561 gcc_assert (pos + fieldsize <= size);
19562 if (val && fieldsize != 0
19563 && !native_encode_initializer (val, array + pos, fieldsize))
19564 return false;
19565 }
19566 return true;
19567 }
19568 return false;
19569 case VIEW_CONVERT_EXPR:
19570 case NON_LVALUE_EXPR:
19571 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19572 default:
19573 return native_encode_expr (init, array, size) == size;
19574 }
19575 }
19576
19577 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19578 attribute is the const value T. */
19579
19580 static bool
19581 tree_add_const_value_attribute (dw_die_ref die, tree t)
19582 {
19583 tree init;
19584 tree type = TREE_TYPE (t);
19585 rtx rtl;
19586
19587 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19588 return false;
19589
19590 init = t;
19591 gcc_assert (!DECL_P (init));
19592
19593 if (TREE_CODE (init) == INTEGER_CST)
19594 {
19595 if (tree_fits_uhwi_p (init))
19596 {
19597 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19598 return true;
19599 }
19600 if (tree_fits_shwi_p (init))
19601 {
19602 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19603 return true;
19604 }
19605 }
19606 if (! early_dwarf)
19607 {
19608 rtl = rtl_for_decl_init (init, type);
19609 if (rtl)
19610 return add_const_value_attribute (die, rtl);
19611 }
19612 /* If the host and target are sane, try harder. */
19613 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19614 && initializer_constant_valid_p (init, type))
19615 {
19616 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19617 if (size > 0 && (int) size == size)
19618 {
19619 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19620
19621 if (native_encode_initializer (init, array, size))
19622 {
19623 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19624 return true;
19625 }
19626 ggc_free (array);
19627 }
19628 }
19629 return false;
19630 }
19631
19632 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19633 attribute is the const value of T, where T is an integral constant
19634 variable with static storage duration
19635 (so it can't be a PARM_DECL or a RESULT_DECL). */
19636
19637 static bool
19638 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19639 {
19640
19641 if (!decl
19642 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19643 || (VAR_P (decl) && !TREE_STATIC (decl)))
19644 return false;
19645
19646 if (TREE_READONLY (decl)
19647 && ! TREE_THIS_VOLATILE (decl)
19648 && DECL_INITIAL (decl))
19649 /* OK */;
19650 else
19651 return false;
19652
19653 /* Don't add DW_AT_const_value if abstract origin already has one. */
19654 if (get_AT (var_die, DW_AT_const_value))
19655 return false;
19656
19657 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19658 }
19659
19660 /* Convert the CFI instructions for the current function into a
19661 location list. This is used for DW_AT_frame_base when we targeting
19662 a dwarf2 consumer that does not support the dwarf3
19663 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19664 expressions. */
19665
19666 static dw_loc_list_ref
19667 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19668 {
19669 int ix;
19670 dw_fde_ref fde;
19671 dw_loc_list_ref list, *list_tail;
19672 dw_cfi_ref cfi;
19673 dw_cfa_location last_cfa, next_cfa;
19674 const char *start_label, *last_label, *section;
19675 dw_cfa_location remember;
19676
19677 fde = cfun->fde;
19678 gcc_assert (fde != NULL);
19679
19680 section = secname_for_decl (current_function_decl);
19681 list_tail = &list;
19682 list = NULL;
19683
19684 memset (&next_cfa, 0, sizeof (next_cfa));
19685 next_cfa.reg = INVALID_REGNUM;
19686 remember = next_cfa;
19687
19688 start_label = fde->dw_fde_begin;
19689
19690 /* ??? Bald assumption that the CIE opcode list does not contain
19691 advance opcodes. */
19692 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19693 lookup_cfa_1 (cfi, &next_cfa, &remember);
19694
19695 last_cfa = next_cfa;
19696 last_label = start_label;
19697
19698 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19699 {
19700 /* If the first partition contained no CFI adjustments, the
19701 CIE opcodes apply to the whole first partition. */
19702 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19703 fde->dw_fde_begin, fde->dw_fde_end, section);
19704 list_tail =&(*list_tail)->dw_loc_next;
19705 start_label = last_label = fde->dw_fde_second_begin;
19706 }
19707
19708 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19709 {
19710 switch (cfi->dw_cfi_opc)
19711 {
19712 case DW_CFA_set_loc:
19713 case DW_CFA_advance_loc1:
19714 case DW_CFA_advance_loc2:
19715 case DW_CFA_advance_loc4:
19716 if (!cfa_equal_p (&last_cfa, &next_cfa))
19717 {
19718 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19719 start_label, last_label, section);
19720
19721 list_tail = &(*list_tail)->dw_loc_next;
19722 last_cfa = next_cfa;
19723 start_label = last_label;
19724 }
19725 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19726 break;
19727
19728 case DW_CFA_advance_loc:
19729 /* The encoding is complex enough that we should never emit this. */
19730 gcc_unreachable ();
19731
19732 default:
19733 lookup_cfa_1 (cfi, &next_cfa, &remember);
19734 break;
19735 }
19736 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19737 {
19738 if (!cfa_equal_p (&last_cfa, &next_cfa))
19739 {
19740 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19741 start_label, last_label, section);
19742
19743 list_tail = &(*list_tail)->dw_loc_next;
19744 last_cfa = next_cfa;
19745 start_label = last_label;
19746 }
19747 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19748 start_label, fde->dw_fde_end, section);
19749 list_tail = &(*list_tail)->dw_loc_next;
19750 start_label = last_label = fde->dw_fde_second_begin;
19751 }
19752 }
19753
19754 if (!cfa_equal_p (&last_cfa, &next_cfa))
19755 {
19756 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19757 start_label, last_label, section);
19758 list_tail = &(*list_tail)->dw_loc_next;
19759 start_label = last_label;
19760 }
19761
19762 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19763 start_label,
19764 fde->dw_fde_second_begin
19765 ? fde->dw_fde_second_end : fde->dw_fde_end,
19766 section);
19767
19768 if (list && list->dw_loc_next)
19769 gen_llsym (list);
19770
19771 return list;
19772 }
19773
19774 /* Compute a displacement from the "steady-state frame pointer" to the
19775 frame base (often the same as the CFA), and store it in
19776 frame_pointer_fb_offset. OFFSET is added to the displacement
19777 before the latter is negated. */
19778
19779 static void
19780 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
19781 {
19782 rtx reg, elim;
19783
19784 #ifdef FRAME_POINTER_CFA_OFFSET
19785 reg = frame_pointer_rtx;
19786 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19787 #else
19788 reg = arg_pointer_rtx;
19789 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19790 #endif
19791
19792 elim = (ira_use_lra_p
19793 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19794 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19795 elim = strip_offset_and_add (elim, &offset);
19796
19797 frame_pointer_fb_offset = -offset;
19798
19799 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19800 in which to eliminate. This is because it's stack pointer isn't
19801 directly accessible as a register within the ISA. To work around
19802 this, assume that while we cannot provide a proper value for
19803 frame_pointer_fb_offset, we won't need one either. */
19804 frame_pointer_fb_offset_valid
19805 = ((SUPPORTS_STACK_ALIGNMENT
19806 && (elim == hard_frame_pointer_rtx
19807 || elim == stack_pointer_rtx))
19808 || elim == (frame_pointer_needed
19809 ? hard_frame_pointer_rtx
19810 : stack_pointer_rtx));
19811 }
19812
19813 /* Generate a DW_AT_name attribute given some string value to be included as
19814 the value of the attribute. */
19815
19816 static void
19817 add_name_attribute (dw_die_ref die, const char *name_string)
19818 {
19819 if (name_string != NULL && *name_string != 0)
19820 {
19821 if (demangle_name_func)
19822 name_string = (*demangle_name_func) (name_string);
19823
19824 add_AT_string (die, DW_AT_name, name_string);
19825 }
19826 }
19827
19828 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19829 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19830 of TYPE accordingly.
19831
19832 ??? This is a temporary measure until after we're able to generate
19833 regular DWARF for the complex Ada type system. */
19834
19835 static void
19836 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19837 dw_die_ref context_die)
19838 {
19839 tree dtype;
19840 dw_die_ref dtype_die;
19841
19842 if (!lang_hooks.types.descriptive_type)
19843 return;
19844
19845 dtype = lang_hooks.types.descriptive_type (type);
19846 if (!dtype)
19847 return;
19848
19849 dtype_die = lookup_type_die (dtype);
19850 if (!dtype_die)
19851 {
19852 gen_type_die (dtype, context_die);
19853 dtype_die = lookup_type_die (dtype);
19854 gcc_assert (dtype_die);
19855 }
19856
19857 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19858 }
19859
19860 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19861
19862 static const char *
19863 comp_dir_string (void)
19864 {
19865 const char *wd;
19866 char *wd1;
19867 static const char *cached_wd = NULL;
19868
19869 if (cached_wd != NULL)
19870 return cached_wd;
19871
19872 wd = get_src_pwd ();
19873 if (wd == NULL)
19874 return NULL;
19875
19876 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19877 {
19878 int wdlen;
19879
19880 wdlen = strlen (wd);
19881 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19882 strcpy (wd1, wd);
19883 wd1 [wdlen] = DIR_SEPARATOR;
19884 wd1 [wdlen + 1] = 0;
19885 wd = wd1;
19886 }
19887
19888 cached_wd = remap_debug_filename (wd);
19889 return cached_wd;
19890 }
19891
19892 /* Generate a DW_AT_comp_dir attribute for DIE. */
19893
19894 static void
19895 add_comp_dir_attribute (dw_die_ref die)
19896 {
19897 const char * wd = comp_dir_string ();
19898 if (wd != NULL)
19899 add_AT_string (die, DW_AT_comp_dir, wd);
19900 }
19901
19902 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19903 pointer computation, ...), output a representation for that bound according
19904 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19905 loc_list_from_tree for the meaning of CONTEXT. */
19906
19907 static void
19908 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19909 int forms, struct loc_descr_context *context)
19910 {
19911 dw_die_ref context_die, decl_die;
19912 dw_loc_list_ref list;
19913 bool strip_conversions = true;
19914 bool placeholder_seen = false;
19915
19916 while (strip_conversions)
19917 switch (TREE_CODE (value))
19918 {
19919 case ERROR_MARK:
19920 case SAVE_EXPR:
19921 return;
19922
19923 CASE_CONVERT:
19924 case VIEW_CONVERT_EXPR:
19925 value = TREE_OPERAND (value, 0);
19926 break;
19927
19928 default:
19929 strip_conversions = false;
19930 break;
19931 }
19932
19933 /* If possible and permitted, output the attribute as a constant. */
19934 if ((forms & dw_scalar_form_constant) != 0
19935 && TREE_CODE (value) == INTEGER_CST)
19936 {
19937 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19938
19939 /* If HOST_WIDE_INT is big enough then represent the bound as
19940 a constant value. We need to choose a form based on
19941 whether the type is signed or unsigned. We cannot just
19942 call add_AT_unsigned if the value itself is positive
19943 (add_AT_unsigned might add the unsigned value encoded as
19944 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19945 bounds type and then sign extend any unsigned values found
19946 for signed types. This is needed only for
19947 DW_AT_{lower,upper}_bound, since for most other attributes,
19948 consumers will treat DW_FORM_data[1248] as unsigned values,
19949 regardless of the underlying type. */
19950 if (prec <= HOST_BITS_PER_WIDE_INT
19951 || tree_fits_uhwi_p (value))
19952 {
19953 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19954 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19955 else
19956 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19957 }
19958 else
19959 /* Otherwise represent the bound as an unsigned value with
19960 the precision of its type. The precision and signedness
19961 of the type will be necessary to re-interpret it
19962 unambiguously. */
19963 add_AT_wide (die, attr, wi::to_wide (value));
19964 return;
19965 }
19966
19967 /* Otherwise, if it's possible and permitted too, output a reference to
19968 another DIE. */
19969 if ((forms & dw_scalar_form_reference) != 0)
19970 {
19971 tree decl = NULL_TREE;
19972
19973 /* Some type attributes reference an outer type. For instance, the upper
19974 bound of an array may reference an embedding record (this happens in
19975 Ada). */
19976 if (TREE_CODE (value) == COMPONENT_REF
19977 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19978 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19979 decl = TREE_OPERAND (value, 1);
19980
19981 else if (VAR_P (value)
19982 || TREE_CODE (value) == PARM_DECL
19983 || TREE_CODE (value) == RESULT_DECL)
19984 decl = value;
19985
19986 if (decl != NULL_TREE)
19987 {
19988 dw_die_ref decl_die = lookup_decl_die (decl);
19989
19990 /* ??? Can this happen, or should the variable have been bound
19991 first? Probably it can, since I imagine that we try to create
19992 the types of parameters in the order in which they exist in
19993 the list, and won't have created a forward reference to a
19994 later parameter. */
19995 if (decl_die != NULL)
19996 {
19997 add_AT_die_ref (die, attr, decl_die);
19998 return;
19999 }
20000 }
20001 }
20002
20003 /* Last chance: try to create a stack operation procedure to evaluate the
20004 value. Do nothing if even that is not possible or permitted. */
20005 if ((forms & dw_scalar_form_exprloc) == 0)
20006 return;
20007
20008 list = loc_list_from_tree (value, 2, context);
20009 if (context && context->placeholder_arg)
20010 {
20011 placeholder_seen = context->placeholder_seen;
20012 context->placeholder_seen = false;
20013 }
20014 if (list == NULL || single_element_loc_list_p (list))
20015 {
20016 /* If this attribute is not a reference nor constant, it is
20017 a DWARF expression rather than location description. For that
20018 loc_list_from_tree (value, 0, &context) is needed. */
20019 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20020 if (list2 && single_element_loc_list_p (list2))
20021 {
20022 if (placeholder_seen)
20023 {
20024 struct dwarf_procedure_info dpi;
20025 dpi.fndecl = NULL_TREE;
20026 dpi.args_count = 1;
20027 if (!resolve_args_picking (list2->expr, 1, &dpi))
20028 return;
20029 }
20030 add_AT_loc (die, attr, list2->expr);
20031 return;
20032 }
20033 }
20034
20035 /* If that failed to give a single element location list, fall back to
20036 outputting this as a reference... still if permitted. */
20037 if (list == NULL
20038 || (forms & dw_scalar_form_reference) == 0
20039 || placeholder_seen)
20040 return;
20041
20042 if (current_function_decl == 0)
20043 context_die = comp_unit_die ();
20044 else
20045 context_die = lookup_decl_die (current_function_decl);
20046
20047 decl_die = new_die (DW_TAG_variable, context_die, value);
20048 add_AT_flag (decl_die, DW_AT_artificial, 1);
20049 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20050 context_die);
20051 add_AT_location_description (decl_die, DW_AT_location, list);
20052 add_AT_die_ref (die, attr, decl_die);
20053 }
20054
20055 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20056 default. */
20057
20058 static int
20059 lower_bound_default (void)
20060 {
20061 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20062 {
20063 case DW_LANG_C:
20064 case DW_LANG_C89:
20065 case DW_LANG_C99:
20066 case DW_LANG_C11:
20067 case DW_LANG_C_plus_plus:
20068 case DW_LANG_C_plus_plus_11:
20069 case DW_LANG_C_plus_plus_14:
20070 case DW_LANG_ObjC:
20071 case DW_LANG_ObjC_plus_plus:
20072 return 0;
20073 case DW_LANG_Fortran77:
20074 case DW_LANG_Fortran90:
20075 case DW_LANG_Fortran95:
20076 case DW_LANG_Fortran03:
20077 case DW_LANG_Fortran08:
20078 return 1;
20079 case DW_LANG_UPC:
20080 case DW_LANG_D:
20081 case DW_LANG_Python:
20082 return dwarf_version >= 4 ? 0 : -1;
20083 case DW_LANG_Ada95:
20084 case DW_LANG_Ada83:
20085 case DW_LANG_Cobol74:
20086 case DW_LANG_Cobol85:
20087 case DW_LANG_Modula2:
20088 case DW_LANG_PLI:
20089 return dwarf_version >= 4 ? 1 : -1;
20090 default:
20091 return -1;
20092 }
20093 }
20094
20095 /* Given a tree node describing an array bound (either lower or upper) output
20096 a representation for that bound. */
20097
20098 static void
20099 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20100 tree bound, struct loc_descr_context *context)
20101 {
20102 int dflt;
20103
20104 while (1)
20105 switch (TREE_CODE (bound))
20106 {
20107 /* Strip all conversions. */
20108 CASE_CONVERT:
20109 case VIEW_CONVERT_EXPR:
20110 bound = TREE_OPERAND (bound, 0);
20111 break;
20112
20113 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20114 are even omitted when they are the default. */
20115 case INTEGER_CST:
20116 /* If the value for this bound is the default one, we can even omit the
20117 attribute. */
20118 if (bound_attr == DW_AT_lower_bound
20119 && tree_fits_shwi_p (bound)
20120 && (dflt = lower_bound_default ()) != -1
20121 && tree_to_shwi (bound) == dflt)
20122 return;
20123
20124 /* FALLTHRU */
20125
20126 default:
20127 /* Because of the complex interaction there can be with other GNAT
20128 encodings, GDB isn't ready yet to handle proper DWARF description
20129 for self-referencial subrange bounds: let GNAT encodings do the
20130 magic in such a case. */
20131 if (is_ada ()
20132 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20133 && contains_placeholder_p (bound))
20134 return;
20135
20136 add_scalar_info (subrange_die, bound_attr, bound,
20137 dw_scalar_form_constant
20138 | dw_scalar_form_exprloc
20139 | dw_scalar_form_reference,
20140 context);
20141 return;
20142 }
20143 }
20144
20145 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20146 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20147 Note that the block of subscript information for an array type also
20148 includes information about the element type of the given array type.
20149
20150 This function reuses previously set type and bound information if
20151 available. */
20152
20153 static void
20154 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20155 {
20156 unsigned dimension_number;
20157 tree lower, upper;
20158 dw_die_ref child = type_die->die_child;
20159
20160 for (dimension_number = 0;
20161 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20162 type = TREE_TYPE (type), dimension_number++)
20163 {
20164 tree domain = TYPE_DOMAIN (type);
20165
20166 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20167 break;
20168
20169 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20170 and (in GNU C only) variable bounds. Handle all three forms
20171 here. */
20172
20173 /* Find and reuse a previously generated DW_TAG_subrange_type if
20174 available.
20175
20176 For multi-dimensional arrays, as we iterate through the
20177 various dimensions in the enclosing for loop above, we also
20178 iterate through the DIE children and pick at each
20179 DW_TAG_subrange_type previously generated (if available).
20180 Each child DW_TAG_subrange_type DIE describes the range of
20181 the current dimension. At this point we should have as many
20182 DW_TAG_subrange_type's as we have dimensions in the
20183 array. */
20184 dw_die_ref subrange_die = NULL;
20185 if (child)
20186 while (1)
20187 {
20188 child = child->die_sib;
20189 if (child->die_tag == DW_TAG_subrange_type)
20190 subrange_die = child;
20191 if (child == type_die->die_child)
20192 {
20193 /* If we wrapped around, stop looking next time. */
20194 child = NULL;
20195 break;
20196 }
20197 if (child->die_tag == DW_TAG_subrange_type)
20198 break;
20199 }
20200 if (!subrange_die)
20201 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20202
20203 if (domain)
20204 {
20205 /* We have an array type with specified bounds. */
20206 lower = TYPE_MIN_VALUE (domain);
20207 upper = TYPE_MAX_VALUE (domain);
20208
20209 /* Define the index type. */
20210 if (TREE_TYPE (domain)
20211 && !get_AT (subrange_die, DW_AT_type))
20212 {
20213 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20214 TREE_TYPE field. We can't emit debug info for this
20215 because it is an unnamed integral type. */
20216 if (TREE_CODE (domain) == INTEGER_TYPE
20217 && TYPE_NAME (domain) == NULL_TREE
20218 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20219 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20220 ;
20221 else
20222 add_type_attribute (subrange_die, TREE_TYPE (domain),
20223 TYPE_UNQUALIFIED, false, type_die);
20224 }
20225
20226 /* ??? If upper is NULL, the array has unspecified length,
20227 but it does have a lower bound. This happens with Fortran
20228 dimension arr(N:*)
20229 Since the debugger is definitely going to need to know N
20230 to produce useful results, go ahead and output the lower
20231 bound solo, and hope the debugger can cope. */
20232
20233 if (!get_AT (subrange_die, DW_AT_lower_bound))
20234 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20235 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20236 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20237 }
20238
20239 /* Otherwise we have an array type with an unspecified length. The
20240 DWARF-2 spec does not say how to handle this; let's just leave out the
20241 bounds. */
20242 }
20243 }
20244
20245 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20246
20247 static void
20248 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20249 {
20250 dw_die_ref decl_die;
20251 HOST_WIDE_INT size;
20252 dw_loc_descr_ref size_expr = NULL;
20253
20254 switch (TREE_CODE (tree_node))
20255 {
20256 case ERROR_MARK:
20257 size = 0;
20258 break;
20259 case ENUMERAL_TYPE:
20260 case RECORD_TYPE:
20261 case UNION_TYPE:
20262 case QUAL_UNION_TYPE:
20263 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20264 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20265 {
20266 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20267 return;
20268 }
20269 size_expr = type_byte_size (tree_node, &size);
20270 break;
20271 case FIELD_DECL:
20272 /* For a data member of a struct or union, the DW_AT_byte_size is
20273 generally given as the number of bytes normally allocated for an
20274 object of the *declared* type of the member itself. This is true
20275 even for bit-fields. */
20276 size = int_size_in_bytes (field_type (tree_node));
20277 break;
20278 default:
20279 gcc_unreachable ();
20280 }
20281
20282 /* Support for dynamically-sized objects was introduced by DWARFv3.
20283 At the moment, GDB does not handle variable byte sizes very well,
20284 though. */
20285 if ((dwarf_version >= 3 || !dwarf_strict)
20286 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20287 && size_expr != NULL)
20288 add_AT_loc (die, DW_AT_byte_size, size_expr);
20289
20290 /* Note that `size' might be -1 when we get to this point. If it is, that
20291 indicates that the byte size of the entity in question is variable and
20292 that we could not generate a DWARF expression that computes it. */
20293 if (size >= 0)
20294 add_AT_unsigned (die, DW_AT_byte_size, size);
20295 }
20296
20297 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20298 alignment. */
20299
20300 static void
20301 add_alignment_attribute (dw_die_ref die, tree tree_node)
20302 {
20303 if (dwarf_version < 5 && dwarf_strict)
20304 return;
20305
20306 unsigned align;
20307
20308 if (DECL_P (tree_node))
20309 {
20310 if (!DECL_USER_ALIGN (tree_node))
20311 return;
20312
20313 align = DECL_ALIGN_UNIT (tree_node);
20314 }
20315 else if (TYPE_P (tree_node))
20316 {
20317 if (!TYPE_USER_ALIGN (tree_node))
20318 return;
20319
20320 align = TYPE_ALIGN_UNIT (tree_node);
20321 }
20322 else
20323 gcc_unreachable ();
20324
20325 add_AT_unsigned (die, DW_AT_alignment, align);
20326 }
20327
20328 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20329 which specifies the distance in bits from the highest order bit of the
20330 "containing object" for the bit-field to the highest order bit of the
20331 bit-field itself.
20332
20333 For any given bit-field, the "containing object" is a hypothetical object
20334 (of some integral or enum type) within which the given bit-field lives. The
20335 type of this hypothetical "containing object" is always the same as the
20336 declared type of the individual bit-field itself. The determination of the
20337 exact location of the "containing object" for a bit-field is rather
20338 complicated. It's handled by the `field_byte_offset' function (above).
20339
20340 CTX is required: see the comment for VLR_CONTEXT.
20341
20342 Note that it is the size (in bytes) of the hypothetical "containing object"
20343 which will be given in the DW_AT_byte_size attribute for this bit-field.
20344 (See `byte_size_attribute' above). */
20345
20346 static inline void
20347 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20348 {
20349 HOST_WIDE_INT object_offset_in_bytes;
20350 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20351 HOST_WIDE_INT bitpos_int;
20352 HOST_WIDE_INT highest_order_object_bit_offset;
20353 HOST_WIDE_INT highest_order_field_bit_offset;
20354 HOST_WIDE_INT bit_offset;
20355
20356 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20357
20358 /* Must be a field and a bit field. */
20359 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20360
20361 /* We can't yet handle bit-fields whose offsets are variable, so if we
20362 encounter such things, just return without generating any attribute
20363 whatsoever. Likewise for variable or too large size. */
20364 if (! tree_fits_shwi_p (bit_position (decl))
20365 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20366 return;
20367
20368 bitpos_int = int_bit_position (decl);
20369
20370 /* Note that the bit offset is always the distance (in bits) from the
20371 highest-order bit of the "containing object" to the highest-order bit of
20372 the bit-field itself. Since the "high-order end" of any object or field
20373 is different on big-endian and little-endian machines, the computation
20374 below must take account of these differences. */
20375 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20376 highest_order_field_bit_offset = bitpos_int;
20377
20378 if (! BYTES_BIG_ENDIAN)
20379 {
20380 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20381 highest_order_object_bit_offset +=
20382 simple_type_size_in_bits (original_type);
20383 }
20384
20385 bit_offset
20386 = (! BYTES_BIG_ENDIAN
20387 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20388 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20389
20390 if (bit_offset < 0)
20391 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20392 else
20393 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20394 }
20395
20396 /* For a FIELD_DECL node which represents a bit field, output an attribute
20397 which specifies the length in bits of the given field. */
20398
20399 static inline void
20400 add_bit_size_attribute (dw_die_ref die, tree decl)
20401 {
20402 /* Must be a field and a bit field. */
20403 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20404 && DECL_BIT_FIELD_TYPE (decl));
20405
20406 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20407 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20408 }
20409
20410 /* If the compiled language is ANSI C, then add a 'prototyped'
20411 attribute, if arg types are given for the parameters of a function. */
20412
20413 static inline void
20414 add_prototyped_attribute (dw_die_ref die, tree func_type)
20415 {
20416 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20417 {
20418 case DW_LANG_C:
20419 case DW_LANG_C89:
20420 case DW_LANG_C99:
20421 case DW_LANG_C11:
20422 case DW_LANG_ObjC:
20423 if (prototype_p (func_type))
20424 add_AT_flag (die, DW_AT_prototyped, 1);
20425 break;
20426 default:
20427 break;
20428 }
20429 }
20430
20431 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20432 by looking in the type declaration, the object declaration equate table or
20433 the block mapping. */
20434
20435 static inline dw_die_ref
20436 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20437 {
20438 dw_die_ref origin_die = NULL;
20439
20440 if (DECL_P (origin))
20441 {
20442 dw_die_ref c;
20443 origin_die = lookup_decl_die (origin);
20444 /* "Unwrap" the decls DIE which we put in the imported unit context.
20445 We are looking for the abstract copy here. */
20446 if (in_lto_p
20447 && origin_die
20448 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20449 /* ??? Identify this better. */
20450 && c->with_offset)
20451 origin_die = c;
20452 }
20453 else if (TYPE_P (origin))
20454 origin_die = lookup_type_die (origin);
20455 else if (TREE_CODE (origin) == BLOCK)
20456 origin_die = BLOCK_DIE (origin);
20457
20458 /* XXX: Functions that are never lowered don't always have correct block
20459 trees (in the case of java, they simply have no block tree, in some other
20460 languages). For these functions, there is nothing we can really do to
20461 output correct debug info for inlined functions in all cases. Rather
20462 than die, we'll just produce deficient debug info now, in that we will
20463 have variables without a proper abstract origin. In the future, when all
20464 functions are lowered, we should re-add a gcc_assert (origin_die)
20465 here. */
20466
20467 if (origin_die)
20468 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20469 return origin_die;
20470 }
20471
20472 /* We do not currently support the pure_virtual attribute. */
20473
20474 static inline void
20475 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20476 {
20477 if (DECL_VINDEX (func_decl))
20478 {
20479 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20480
20481 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20482 add_AT_loc (die, DW_AT_vtable_elem_location,
20483 new_loc_descr (DW_OP_constu,
20484 tree_to_shwi (DECL_VINDEX (func_decl)),
20485 0));
20486
20487 /* GNU extension: Record what type this method came from originally. */
20488 if (debug_info_level > DINFO_LEVEL_TERSE
20489 && DECL_CONTEXT (func_decl))
20490 add_AT_die_ref (die, DW_AT_containing_type,
20491 lookup_type_die (DECL_CONTEXT (func_decl)));
20492 }
20493 }
20494 \f
20495 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20496 given decl. This used to be a vendor extension until after DWARF 4
20497 standardized it. */
20498
20499 static void
20500 add_linkage_attr (dw_die_ref die, tree decl)
20501 {
20502 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20503
20504 /* Mimic what assemble_name_raw does with a leading '*'. */
20505 if (name[0] == '*')
20506 name = &name[1];
20507
20508 if (dwarf_version >= 4)
20509 add_AT_string (die, DW_AT_linkage_name, name);
20510 else
20511 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20512 }
20513
20514 /* Add source coordinate attributes for the given decl. */
20515
20516 static void
20517 add_src_coords_attributes (dw_die_ref die, tree decl)
20518 {
20519 expanded_location s;
20520
20521 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20522 return;
20523 s = expand_location (DECL_SOURCE_LOCATION (decl));
20524 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20525 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20526 if (debug_column_info && s.column)
20527 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20528 }
20529
20530 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20531
20532 static void
20533 add_linkage_name_raw (dw_die_ref die, tree decl)
20534 {
20535 /* Defer until we have an assembler name set. */
20536 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20537 {
20538 limbo_die_node *asm_name;
20539
20540 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20541 asm_name->die = die;
20542 asm_name->created_for = decl;
20543 asm_name->next = deferred_asm_name;
20544 deferred_asm_name = asm_name;
20545 }
20546 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20547 add_linkage_attr (die, decl);
20548 }
20549
20550 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20551
20552 static void
20553 add_linkage_name (dw_die_ref die, tree decl)
20554 {
20555 if (debug_info_level > DINFO_LEVEL_NONE
20556 && VAR_OR_FUNCTION_DECL_P (decl)
20557 && TREE_PUBLIC (decl)
20558 && !(VAR_P (decl) && DECL_REGISTER (decl))
20559 && die->die_tag != DW_TAG_member)
20560 add_linkage_name_raw (die, decl);
20561 }
20562
20563 /* Add a DW_AT_name attribute and source coordinate attribute for the
20564 given decl, but only if it actually has a name. */
20565
20566 static void
20567 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20568 bool no_linkage_name)
20569 {
20570 tree decl_name;
20571
20572 decl_name = DECL_NAME (decl);
20573 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20574 {
20575 const char *name = dwarf2_name (decl, 0);
20576 if (name)
20577 add_name_attribute (die, name);
20578 if (! DECL_ARTIFICIAL (decl))
20579 add_src_coords_attributes (die, decl);
20580
20581 if (!no_linkage_name)
20582 add_linkage_name (die, decl);
20583 }
20584
20585 #ifdef VMS_DEBUGGING_INFO
20586 /* Get the function's name, as described by its RTL. This may be different
20587 from the DECL_NAME name used in the source file. */
20588 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20589 {
20590 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20591 XEXP (DECL_RTL (decl), 0), false);
20592 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20593 }
20594 #endif /* VMS_DEBUGGING_INFO */
20595 }
20596
20597 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20598
20599 static void
20600 add_discr_value (dw_die_ref die, dw_discr_value *value)
20601 {
20602 dw_attr_node attr;
20603
20604 attr.dw_attr = DW_AT_discr_value;
20605 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20606 attr.dw_attr_val.val_entry = NULL;
20607 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20608 if (value->pos)
20609 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20610 else
20611 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20612 add_dwarf_attr (die, &attr);
20613 }
20614
20615 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20616
20617 static void
20618 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20619 {
20620 dw_attr_node attr;
20621
20622 attr.dw_attr = DW_AT_discr_list;
20623 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20624 attr.dw_attr_val.val_entry = NULL;
20625 attr.dw_attr_val.v.val_discr_list = discr_list;
20626 add_dwarf_attr (die, &attr);
20627 }
20628
20629 static inline dw_discr_list_ref
20630 AT_discr_list (dw_attr_node *attr)
20631 {
20632 return attr->dw_attr_val.v.val_discr_list;
20633 }
20634
20635 #ifdef VMS_DEBUGGING_INFO
20636 /* Output the debug main pointer die for VMS */
20637
20638 void
20639 dwarf2out_vms_debug_main_pointer (void)
20640 {
20641 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20642 dw_die_ref die;
20643
20644 /* Allocate the VMS debug main subprogram die. */
20645 die = new_die_raw (DW_TAG_subprogram);
20646 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20647 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20648 current_function_funcdef_no);
20649 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20650
20651 /* Make it the first child of comp_unit_die (). */
20652 die->die_parent = comp_unit_die ();
20653 if (comp_unit_die ()->die_child)
20654 {
20655 die->die_sib = comp_unit_die ()->die_child->die_sib;
20656 comp_unit_die ()->die_child->die_sib = die;
20657 }
20658 else
20659 {
20660 die->die_sib = die;
20661 comp_unit_die ()->die_child = die;
20662 }
20663 }
20664 #endif /* VMS_DEBUGGING_INFO */
20665
20666 /* Push a new declaration scope. */
20667
20668 static void
20669 push_decl_scope (tree scope)
20670 {
20671 vec_safe_push (decl_scope_table, scope);
20672 }
20673
20674 /* Pop a declaration scope. */
20675
20676 static inline void
20677 pop_decl_scope (void)
20678 {
20679 decl_scope_table->pop ();
20680 }
20681
20682 /* walk_tree helper function for uses_local_type, below. */
20683
20684 static tree
20685 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20686 {
20687 if (!TYPE_P (*tp))
20688 *walk_subtrees = 0;
20689 else
20690 {
20691 tree name = TYPE_NAME (*tp);
20692 if (name && DECL_P (name) && decl_function_context (name))
20693 return *tp;
20694 }
20695 return NULL_TREE;
20696 }
20697
20698 /* If TYPE involves a function-local type (including a local typedef to a
20699 non-local type), returns that type; otherwise returns NULL_TREE. */
20700
20701 static tree
20702 uses_local_type (tree type)
20703 {
20704 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20705 return used;
20706 }
20707
20708 /* Return the DIE for the scope that immediately contains this type.
20709 Non-named types that do not involve a function-local type get global
20710 scope. Named types nested in namespaces or other types get their
20711 containing scope. All other types (i.e. function-local named types) get
20712 the current active scope. */
20713
20714 static dw_die_ref
20715 scope_die_for (tree t, dw_die_ref context_die)
20716 {
20717 dw_die_ref scope_die = NULL;
20718 tree containing_scope;
20719
20720 /* Non-types always go in the current scope. */
20721 gcc_assert (TYPE_P (t));
20722
20723 /* Use the scope of the typedef, rather than the scope of the type
20724 it refers to. */
20725 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20726 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20727 else
20728 containing_scope = TYPE_CONTEXT (t);
20729
20730 /* Use the containing namespace if there is one. */
20731 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20732 {
20733 if (context_die == lookup_decl_die (containing_scope))
20734 /* OK */;
20735 else if (debug_info_level > DINFO_LEVEL_TERSE)
20736 context_die = get_context_die (containing_scope);
20737 else
20738 containing_scope = NULL_TREE;
20739 }
20740
20741 /* Ignore function type "scopes" from the C frontend. They mean that
20742 a tagged type is local to a parmlist of a function declarator, but
20743 that isn't useful to DWARF. */
20744 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20745 containing_scope = NULL_TREE;
20746
20747 if (SCOPE_FILE_SCOPE_P (containing_scope))
20748 {
20749 /* If T uses a local type keep it local as well, to avoid references
20750 to function-local DIEs from outside the function. */
20751 if (current_function_decl && uses_local_type (t))
20752 scope_die = context_die;
20753 else
20754 scope_die = comp_unit_die ();
20755 }
20756 else if (TYPE_P (containing_scope))
20757 {
20758 /* For types, we can just look up the appropriate DIE. */
20759 if (debug_info_level > DINFO_LEVEL_TERSE)
20760 scope_die = get_context_die (containing_scope);
20761 else
20762 {
20763 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20764 if (scope_die == NULL)
20765 scope_die = comp_unit_die ();
20766 }
20767 }
20768 else
20769 scope_die = context_die;
20770
20771 return scope_die;
20772 }
20773
20774 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20775
20776 static inline int
20777 local_scope_p (dw_die_ref context_die)
20778 {
20779 for (; context_die; context_die = context_die->die_parent)
20780 if (context_die->die_tag == DW_TAG_inlined_subroutine
20781 || context_die->die_tag == DW_TAG_subprogram)
20782 return 1;
20783
20784 return 0;
20785 }
20786
20787 /* Returns nonzero if CONTEXT_DIE is a class. */
20788
20789 static inline int
20790 class_scope_p (dw_die_ref context_die)
20791 {
20792 return (context_die
20793 && (context_die->die_tag == DW_TAG_structure_type
20794 || context_die->die_tag == DW_TAG_class_type
20795 || context_die->die_tag == DW_TAG_interface_type
20796 || context_die->die_tag == DW_TAG_union_type));
20797 }
20798
20799 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20800 whether or not to treat a DIE in this context as a declaration. */
20801
20802 static inline int
20803 class_or_namespace_scope_p (dw_die_ref context_die)
20804 {
20805 return (class_scope_p (context_die)
20806 || (context_die && context_die->die_tag == DW_TAG_namespace));
20807 }
20808
20809 /* Many forms of DIEs require a "type description" attribute. This
20810 routine locates the proper "type descriptor" die for the type given
20811 by 'type' plus any additional qualifiers given by 'cv_quals', and
20812 adds a DW_AT_type attribute below the given die. */
20813
20814 static void
20815 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20816 bool reverse, dw_die_ref context_die)
20817 {
20818 enum tree_code code = TREE_CODE (type);
20819 dw_die_ref type_die = NULL;
20820
20821 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20822 or fixed-point type, use the inner type. This is because we have no
20823 support for unnamed types in base_type_die. This can happen if this is
20824 an Ada subrange type. Correct solution is emit a subrange type die. */
20825 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20826 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20827 type = TREE_TYPE (type), code = TREE_CODE (type);
20828
20829 if (code == ERROR_MARK
20830 /* Handle a special case. For functions whose return type is void, we
20831 generate *no* type attribute. (Note that no object may have type
20832 `void', so this only applies to function return types). */
20833 || code == VOID_TYPE)
20834 return;
20835
20836 type_die = modified_type_die (type,
20837 cv_quals | TYPE_QUALS (type),
20838 reverse,
20839 context_die);
20840
20841 if (type_die != NULL)
20842 add_AT_die_ref (object_die, DW_AT_type, type_die);
20843 }
20844
20845 /* Given an object die, add the calling convention attribute for the
20846 function call type. */
20847 static void
20848 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20849 {
20850 enum dwarf_calling_convention value = DW_CC_normal;
20851
20852 value = ((enum dwarf_calling_convention)
20853 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20854
20855 if (is_fortran ()
20856 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20857 {
20858 /* DWARF 2 doesn't provide a way to identify a program's source-level
20859 entry point. DW_AT_calling_convention attributes are only meant
20860 to describe functions' calling conventions. However, lacking a
20861 better way to signal the Fortran main program, we used this for
20862 a long time, following existing custom. Now, DWARF 4 has
20863 DW_AT_main_subprogram, which we add below, but some tools still
20864 rely on the old way, which we thus keep. */
20865 value = DW_CC_program;
20866
20867 if (dwarf_version >= 4 || !dwarf_strict)
20868 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20869 }
20870
20871 /* Only add the attribute if the backend requests it, and
20872 is not DW_CC_normal. */
20873 if (value && (value != DW_CC_normal))
20874 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20875 }
20876
20877 /* Given a tree pointer to a struct, class, union, or enum type node, return
20878 a pointer to the (string) tag name for the given type, or zero if the type
20879 was declared without a tag. */
20880
20881 static const char *
20882 type_tag (const_tree type)
20883 {
20884 const char *name = 0;
20885
20886 if (TYPE_NAME (type) != 0)
20887 {
20888 tree t = 0;
20889
20890 /* Find the IDENTIFIER_NODE for the type name. */
20891 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20892 && !TYPE_NAMELESS (type))
20893 t = TYPE_NAME (type);
20894
20895 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20896 a TYPE_DECL node, regardless of whether or not a `typedef' was
20897 involved. */
20898 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20899 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20900 {
20901 /* We want to be extra verbose. Don't call dwarf_name if
20902 DECL_NAME isn't set. The default hook for decl_printable_name
20903 doesn't like that, and in this context it's correct to return
20904 0, instead of "<anonymous>" or the like. */
20905 if (DECL_NAME (TYPE_NAME (type))
20906 && !DECL_NAMELESS (TYPE_NAME (type)))
20907 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20908 }
20909
20910 /* Now get the name as a string, or invent one. */
20911 if (!name && t != 0)
20912 name = IDENTIFIER_POINTER (t);
20913 }
20914
20915 return (name == 0 || *name == '\0') ? 0 : name;
20916 }
20917
20918 /* Return the type associated with a data member, make a special check
20919 for bit field types. */
20920
20921 static inline tree
20922 member_declared_type (const_tree member)
20923 {
20924 return (DECL_BIT_FIELD_TYPE (member)
20925 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20926 }
20927
20928 /* Get the decl's label, as described by its RTL. This may be different
20929 from the DECL_NAME name used in the source file. */
20930
20931 #if 0
20932 static const char *
20933 decl_start_label (tree decl)
20934 {
20935 rtx x;
20936 const char *fnname;
20937
20938 x = DECL_RTL (decl);
20939 gcc_assert (MEM_P (x));
20940
20941 x = XEXP (x, 0);
20942 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20943
20944 fnname = XSTR (x, 0);
20945 return fnname;
20946 }
20947 #endif
20948 \f
20949 /* For variable-length arrays that have been previously generated, but
20950 may be incomplete due to missing subscript info, fill the subscript
20951 info. Return TRUE if this is one of those cases. */
20952 static bool
20953 fill_variable_array_bounds (tree type)
20954 {
20955 if (TREE_ASM_WRITTEN (type)
20956 && TREE_CODE (type) == ARRAY_TYPE
20957 && variably_modified_type_p (type, NULL))
20958 {
20959 dw_die_ref array_die = lookup_type_die (type);
20960 if (!array_die)
20961 return false;
20962 add_subscript_info (array_die, type, !is_ada ());
20963 return true;
20964 }
20965 return false;
20966 }
20967
20968 /* These routines generate the internal representation of the DIE's for
20969 the compilation unit. Debugging information is collected by walking
20970 the declaration trees passed in from dwarf2out_decl(). */
20971
20972 static void
20973 gen_array_type_die (tree type, dw_die_ref context_die)
20974 {
20975 dw_die_ref array_die;
20976
20977 /* GNU compilers represent multidimensional array types as sequences of one
20978 dimensional array types whose element types are themselves array types.
20979 We sometimes squish that down to a single array_type DIE with multiple
20980 subscripts in the Dwarf debugging info. The draft Dwarf specification
20981 say that we are allowed to do this kind of compression in C, because
20982 there is no difference between an array of arrays and a multidimensional
20983 array. We don't do this for Ada to remain as close as possible to the
20984 actual representation, which is especially important against the language
20985 flexibilty wrt arrays of variable size. */
20986
20987 bool collapse_nested_arrays = !is_ada ();
20988
20989 if (fill_variable_array_bounds (type))
20990 return;
20991
20992 dw_die_ref scope_die = scope_die_for (type, context_die);
20993 tree element_type;
20994
20995 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20996 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20997 if (TYPE_STRING_FLAG (type)
20998 && TREE_CODE (type) == ARRAY_TYPE
20999 && is_fortran ()
21000 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21001 {
21002 HOST_WIDE_INT size;
21003
21004 array_die = new_die (DW_TAG_string_type, scope_die, type);
21005 add_name_attribute (array_die, type_tag (type));
21006 equate_type_number_to_die (type, array_die);
21007 size = int_size_in_bytes (type);
21008 if (size >= 0)
21009 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21010 /* ??? We can't annotate types late, but for LTO we may not
21011 generate a location early either (gfortran.dg/save_6.f90). */
21012 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21013 && TYPE_DOMAIN (type) != NULL_TREE
21014 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21015 {
21016 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21017 tree rszdecl = szdecl;
21018
21019 size = int_size_in_bytes (TREE_TYPE (szdecl));
21020 if (!DECL_P (szdecl))
21021 {
21022 if (TREE_CODE (szdecl) == INDIRECT_REF
21023 && DECL_P (TREE_OPERAND (szdecl, 0)))
21024 {
21025 rszdecl = TREE_OPERAND (szdecl, 0);
21026 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21027 != DWARF2_ADDR_SIZE)
21028 size = 0;
21029 }
21030 else
21031 size = 0;
21032 }
21033 if (size > 0)
21034 {
21035 dw_loc_list_ref loc
21036 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21037 NULL);
21038 if (loc)
21039 {
21040 add_AT_location_description (array_die, DW_AT_string_length,
21041 loc);
21042 if (size != DWARF2_ADDR_SIZE)
21043 add_AT_unsigned (array_die, dwarf_version >= 5
21044 ? DW_AT_string_length_byte_size
21045 : DW_AT_byte_size, size);
21046 }
21047 }
21048 }
21049 return;
21050 }
21051
21052 array_die = new_die (DW_TAG_array_type, scope_die, type);
21053 add_name_attribute (array_die, type_tag (type));
21054 equate_type_number_to_die (type, array_die);
21055
21056 if (TREE_CODE (type) == VECTOR_TYPE)
21057 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21058
21059 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21060 if (is_fortran ()
21061 && TREE_CODE (type) == ARRAY_TYPE
21062 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21063 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21064 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21065
21066 #if 0
21067 /* We default the array ordering. Debuggers will probably do the right
21068 things even if DW_AT_ordering is not present. It's not even an issue
21069 until we start to get into multidimensional arrays anyway. If a debugger
21070 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21071 then we'll have to put the DW_AT_ordering attribute back in. (But if
21072 and when we find out that we need to put these in, we will only do so
21073 for multidimensional arrays. */
21074 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21075 #endif
21076
21077 if (TREE_CODE (type) == VECTOR_TYPE)
21078 {
21079 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21080 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21081 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21082 add_bound_info (subrange_die, DW_AT_upper_bound,
21083 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21084 }
21085 else
21086 add_subscript_info (array_die, type, collapse_nested_arrays);
21087
21088 /* Add representation of the type of the elements of this array type and
21089 emit the corresponding DIE if we haven't done it already. */
21090 element_type = TREE_TYPE (type);
21091 if (collapse_nested_arrays)
21092 while (TREE_CODE (element_type) == ARRAY_TYPE)
21093 {
21094 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21095 break;
21096 element_type = TREE_TYPE (element_type);
21097 }
21098
21099 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21100 TREE_CODE (type) == ARRAY_TYPE
21101 && TYPE_REVERSE_STORAGE_ORDER (type),
21102 context_die);
21103
21104 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21105 if (TYPE_ARTIFICIAL (type))
21106 add_AT_flag (array_die, DW_AT_artificial, 1);
21107
21108 if (get_AT (array_die, DW_AT_name))
21109 add_pubtype (type, array_die);
21110
21111 add_alignment_attribute (array_die, type);
21112 }
21113
21114 /* This routine generates DIE for array with hidden descriptor, details
21115 are filled into *info by a langhook. */
21116
21117 static void
21118 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21119 dw_die_ref context_die)
21120 {
21121 const dw_die_ref scope_die = scope_die_for (type, context_die);
21122 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21123 struct loc_descr_context context = { type, info->base_decl, NULL,
21124 false, false };
21125 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21126 int dim;
21127
21128 add_name_attribute (array_die, type_tag (type));
21129 equate_type_number_to_die (type, array_die);
21130
21131 if (info->ndimensions > 1)
21132 switch (info->ordering)
21133 {
21134 case array_descr_ordering_row_major:
21135 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21136 break;
21137 case array_descr_ordering_column_major:
21138 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21139 break;
21140 default:
21141 break;
21142 }
21143
21144 if (dwarf_version >= 3 || !dwarf_strict)
21145 {
21146 if (info->data_location)
21147 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21148 dw_scalar_form_exprloc, &context);
21149 if (info->associated)
21150 add_scalar_info (array_die, DW_AT_associated, info->associated,
21151 dw_scalar_form_constant
21152 | dw_scalar_form_exprloc
21153 | dw_scalar_form_reference, &context);
21154 if (info->allocated)
21155 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21156 dw_scalar_form_constant
21157 | dw_scalar_form_exprloc
21158 | dw_scalar_form_reference, &context);
21159 if (info->stride)
21160 {
21161 const enum dwarf_attribute attr
21162 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21163 const int forms
21164 = (info->stride_in_bits)
21165 ? dw_scalar_form_constant
21166 : (dw_scalar_form_constant
21167 | dw_scalar_form_exprloc
21168 | dw_scalar_form_reference);
21169
21170 add_scalar_info (array_die, attr, info->stride, forms, &context);
21171 }
21172 }
21173 if (dwarf_version >= 5)
21174 {
21175 if (info->rank)
21176 {
21177 add_scalar_info (array_die, DW_AT_rank, info->rank,
21178 dw_scalar_form_constant
21179 | dw_scalar_form_exprloc, &context);
21180 subrange_tag = DW_TAG_generic_subrange;
21181 context.placeholder_arg = true;
21182 }
21183 }
21184
21185 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21186
21187 for (dim = 0; dim < info->ndimensions; dim++)
21188 {
21189 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21190
21191 if (info->dimen[dim].bounds_type)
21192 add_type_attribute (subrange_die,
21193 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21194 false, context_die);
21195 if (info->dimen[dim].lower_bound)
21196 add_bound_info (subrange_die, DW_AT_lower_bound,
21197 info->dimen[dim].lower_bound, &context);
21198 if (info->dimen[dim].upper_bound)
21199 add_bound_info (subrange_die, DW_AT_upper_bound,
21200 info->dimen[dim].upper_bound, &context);
21201 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21202 add_scalar_info (subrange_die, DW_AT_byte_stride,
21203 info->dimen[dim].stride,
21204 dw_scalar_form_constant
21205 | dw_scalar_form_exprloc
21206 | dw_scalar_form_reference,
21207 &context);
21208 }
21209
21210 gen_type_die (info->element_type, context_die);
21211 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21212 TREE_CODE (type) == ARRAY_TYPE
21213 && TYPE_REVERSE_STORAGE_ORDER (type),
21214 context_die);
21215
21216 if (get_AT (array_die, DW_AT_name))
21217 add_pubtype (type, array_die);
21218
21219 add_alignment_attribute (array_die, type);
21220 }
21221
21222 #if 0
21223 static void
21224 gen_entry_point_die (tree decl, dw_die_ref context_die)
21225 {
21226 tree origin = decl_ultimate_origin (decl);
21227 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21228
21229 if (origin != NULL)
21230 add_abstract_origin_attribute (decl_die, origin);
21231 else
21232 {
21233 add_name_and_src_coords_attributes (decl_die, decl);
21234 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21235 TYPE_UNQUALIFIED, false, context_die);
21236 }
21237
21238 if (DECL_ABSTRACT_P (decl))
21239 equate_decl_number_to_die (decl, decl_die);
21240 else
21241 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21242 }
21243 #endif
21244
21245 /* Walk through the list of incomplete types again, trying once more to
21246 emit full debugging info for them. */
21247
21248 static void
21249 retry_incomplete_types (void)
21250 {
21251 set_early_dwarf s;
21252 int i;
21253
21254 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21255 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21256 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21257 vec_safe_truncate (incomplete_types, 0);
21258 }
21259
21260 /* Determine what tag to use for a record type. */
21261
21262 static enum dwarf_tag
21263 record_type_tag (tree type)
21264 {
21265 if (! lang_hooks.types.classify_record)
21266 return DW_TAG_structure_type;
21267
21268 switch (lang_hooks.types.classify_record (type))
21269 {
21270 case RECORD_IS_STRUCT:
21271 return DW_TAG_structure_type;
21272
21273 case RECORD_IS_CLASS:
21274 return DW_TAG_class_type;
21275
21276 case RECORD_IS_INTERFACE:
21277 if (dwarf_version >= 3 || !dwarf_strict)
21278 return DW_TAG_interface_type;
21279 return DW_TAG_structure_type;
21280
21281 default:
21282 gcc_unreachable ();
21283 }
21284 }
21285
21286 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21287 include all of the information about the enumeration values also. Each
21288 enumerated type name/value is listed as a child of the enumerated type
21289 DIE. */
21290
21291 static dw_die_ref
21292 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21293 {
21294 dw_die_ref type_die = lookup_type_die (type);
21295
21296 if (type_die == NULL)
21297 {
21298 type_die = new_die (DW_TAG_enumeration_type,
21299 scope_die_for (type, context_die), type);
21300 equate_type_number_to_die (type, type_die);
21301 add_name_attribute (type_die, type_tag (type));
21302 if (dwarf_version >= 4 || !dwarf_strict)
21303 {
21304 if (ENUM_IS_SCOPED (type))
21305 add_AT_flag (type_die, DW_AT_enum_class, 1);
21306 if (ENUM_IS_OPAQUE (type))
21307 add_AT_flag (type_die, DW_AT_declaration, 1);
21308 }
21309 if (!dwarf_strict)
21310 add_AT_unsigned (type_die, DW_AT_encoding,
21311 TYPE_UNSIGNED (type)
21312 ? DW_ATE_unsigned
21313 : DW_ATE_signed);
21314 }
21315 else if (! TYPE_SIZE (type))
21316 return type_die;
21317 else
21318 remove_AT (type_die, DW_AT_declaration);
21319
21320 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21321 given enum type is incomplete, do not generate the DW_AT_byte_size
21322 attribute or the DW_AT_element_list attribute. */
21323 if (TYPE_SIZE (type))
21324 {
21325 tree link;
21326
21327 TREE_ASM_WRITTEN (type) = 1;
21328 add_byte_size_attribute (type_die, type);
21329 add_alignment_attribute (type_die, type);
21330 if (dwarf_version >= 3 || !dwarf_strict)
21331 {
21332 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21333 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21334 context_die);
21335 }
21336 if (TYPE_STUB_DECL (type) != NULL_TREE)
21337 {
21338 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21339 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21340 }
21341
21342 /* If the first reference to this type was as the return type of an
21343 inline function, then it may not have a parent. Fix this now. */
21344 if (type_die->die_parent == NULL)
21345 add_child_die (scope_die_for (type, context_die), type_die);
21346
21347 for (link = TYPE_VALUES (type);
21348 link != NULL; link = TREE_CHAIN (link))
21349 {
21350 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21351 tree value = TREE_VALUE (link);
21352
21353 add_name_attribute (enum_die,
21354 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21355
21356 if (TREE_CODE (value) == CONST_DECL)
21357 value = DECL_INITIAL (value);
21358
21359 if (simple_type_size_in_bits (TREE_TYPE (value))
21360 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21361 {
21362 /* For constant forms created by add_AT_unsigned DWARF
21363 consumers (GDB, elfutils, etc.) always zero extend
21364 the value. Only when the actual value is negative
21365 do we need to use add_AT_int to generate a constant
21366 form that can represent negative values. */
21367 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21368 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21369 add_AT_unsigned (enum_die, DW_AT_const_value,
21370 (unsigned HOST_WIDE_INT) val);
21371 else
21372 add_AT_int (enum_die, DW_AT_const_value, val);
21373 }
21374 else
21375 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21376 that here. TODO: This should be re-worked to use correct
21377 signed/unsigned double tags for all cases. */
21378 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21379 }
21380
21381 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21382 if (TYPE_ARTIFICIAL (type))
21383 add_AT_flag (type_die, DW_AT_artificial, 1);
21384 }
21385 else
21386 add_AT_flag (type_die, DW_AT_declaration, 1);
21387
21388 add_pubtype (type, type_die);
21389
21390 return type_die;
21391 }
21392
21393 /* Generate a DIE to represent either a real live formal parameter decl or to
21394 represent just the type of some formal parameter position in some function
21395 type.
21396
21397 Note that this routine is a bit unusual because its argument may be a
21398 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21399 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21400 node. If it's the former then this function is being called to output a
21401 DIE to represent a formal parameter object (or some inlining thereof). If
21402 it's the latter, then this function is only being called to output a
21403 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21404 argument type of some subprogram type.
21405 If EMIT_NAME_P is true, name and source coordinate attributes
21406 are emitted. */
21407
21408 static dw_die_ref
21409 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21410 dw_die_ref context_die)
21411 {
21412 tree node_or_origin = node ? node : origin;
21413 tree ultimate_origin;
21414 dw_die_ref parm_die = NULL;
21415
21416 if (DECL_P (node_or_origin))
21417 {
21418 parm_die = lookup_decl_die (node);
21419
21420 /* If the contexts differ, we may not be talking about the same
21421 thing.
21422 ??? When in LTO the DIE parent is the "abstract" copy and the
21423 context_die is the specification "copy". But this whole block
21424 should eventually be no longer needed. */
21425 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21426 {
21427 if (!DECL_ABSTRACT_P (node))
21428 {
21429 /* This can happen when creating an inlined instance, in
21430 which case we need to create a new DIE that will get
21431 annotated with DW_AT_abstract_origin. */
21432 parm_die = NULL;
21433 }
21434 else
21435 gcc_unreachable ();
21436 }
21437
21438 if (parm_die && parm_die->die_parent == NULL)
21439 {
21440 /* Check that parm_die already has the right attributes that
21441 we would have added below. If any attributes are
21442 missing, fall through to add them. */
21443 if (! DECL_ABSTRACT_P (node_or_origin)
21444 && !get_AT (parm_die, DW_AT_location)
21445 && !get_AT (parm_die, DW_AT_const_value))
21446 /* We are missing location info, and are about to add it. */
21447 ;
21448 else
21449 {
21450 add_child_die (context_die, parm_die);
21451 return parm_die;
21452 }
21453 }
21454 }
21455
21456 /* If we have a previously generated DIE, use it, unless this is an
21457 concrete instance (origin != NULL), in which case we need a new
21458 DIE with a corresponding DW_AT_abstract_origin. */
21459 bool reusing_die;
21460 if (parm_die && origin == NULL)
21461 reusing_die = true;
21462 else
21463 {
21464 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21465 reusing_die = false;
21466 }
21467
21468 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21469 {
21470 case tcc_declaration:
21471 ultimate_origin = decl_ultimate_origin (node_or_origin);
21472 if (node || ultimate_origin)
21473 origin = ultimate_origin;
21474
21475 if (reusing_die)
21476 goto add_location;
21477
21478 if (origin != NULL)
21479 add_abstract_origin_attribute (parm_die, origin);
21480 else if (emit_name_p)
21481 add_name_and_src_coords_attributes (parm_die, node);
21482 if (origin == NULL
21483 || (! DECL_ABSTRACT_P (node_or_origin)
21484 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21485 decl_function_context
21486 (node_or_origin))))
21487 {
21488 tree type = TREE_TYPE (node_or_origin);
21489 if (decl_by_reference_p (node_or_origin))
21490 add_type_attribute (parm_die, TREE_TYPE (type),
21491 TYPE_UNQUALIFIED,
21492 false, context_die);
21493 else
21494 add_type_attribute (parm_die, type,
21495 decl_quals (node_or_origin),
21496 false, context_die);
21497 }
21498 if (origin == NULL && DECL_ARTIFICIAL (node))
21499 add_AT_flag (parm_die, DW_AT_artificial, 1);
21500 add_location:
21501 if (node && node != origin)
21502 equate_decl_number_to_die (node, parm_die);
21503 if (! DECL_ABSTRACT_P (node_or_origin))
21504 add_location_or_const_value_attribute (parm_die, node_or_origin,
21505 node == NULL);
21506
21507 break;
21508
21509 case tcc_type:
21510 /* We were called with some kind of a ..._TYPE node. */
21511 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21512 context_die);
21513 break;
21514
21515 default:
21516 gcc_unreachable ();
21517 }
21518
21519 return parm_die;
21520 }
21521
21522 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21523 children DW_TAG_formal_parameter DIEs representing the arguments of the
21524 parameter pack.
21525
21526 PARM_PACK must be a function parameter pack.
21527 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21528 must point to the subsequent arguments of the function PACK_ARG belongs to.
21529 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21530 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21531 following the last one for which a DIE was generated. */
21532
21533 static dw_die_ref
21534 gen_formal_parameter_pack_die (tree parm_pack,
21535 tree pack_arg,
21536 dw_die_ref subr_die,
21537 tree *next_arg)
21538 {
21539 tree arg;
21540 dw_die_ref parm_pack_die;
21541
21542 gcc_assert (parm_pack
21543 && lang_hooks.function_parameter_pack_p (parm_pack)
21544 && subr_die);
21545
21546 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21547 add_src_coords_attributes (parm_pack_die, parm_pack);
21548
21549 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21550 {
21551 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21552 parm_pack))
21553 break;
21554 gen_formal_parameter_die (arg, NULL,
21555 false /* Don't emit name attribute. */,
21556 parm_pack_die);
21557 }
21558 if (next_arg)
21559 *next_arg = arg;
21560 return parm_pack_die;
21561 }
21562
21563 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21564 at the end of an (ANSI prototyped) formal parameters list. */
21565
21566 static void
21567 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21568 {
21569 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21570 }
21571
21572 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21573 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21574 parameters as specified in some function type specification (except for
21575 those which appear as part of a function *definition*). */
21576
21577 static void
21578 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21579 {
21580 tree link;
21581 tree formal_type = NULL;
21582 tree first_parm_type;
21583 tree arg;
21584
21585 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21586 {
21587 arg = DECL_ARGUMENTS (function_or_method_type);
21588 function_or_method_type = TREE_TYPE (function_or_method_type);
21589 }
21590 else
21591 arg = NULL_TREE;
21592
21593 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21594
21595 /* Make our first pass over the list of formal parameter types and output a
21596 DW_TAG_formal_parameter DIE for each one. */
21597 for (link = first_parm_type; link; )
21598 {
21599 dw_die_ref parm_die;
21600
21601 formal_type = TREE_VALUE (link);
21602 if (formal_type == void_type_node)
21603 break;
21604
21605 /* Output a (nameless) DIE to represent the formal parameter itself. */
21606 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21607 {
21608 parm_die = gen_formal_parameter_die (formal_type, NULL,
21609 true /* Emit name attribute. */,
21610 context_die);
21611 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21612 && link == first_parm_type)
21613 {
21614 add_AT_flag (parm_die, DW_AT_artificial, 1);
21615 if (dwarf_version >= 3 || !dwarf_strict)
21616 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21617 }
21618 else if (arg && DECL_ARTIFICIAL (arg))
21619 add_AT_flag (parm_die, DW_AT_artificial, 1);
21620 }
21621
21622 link = TREE_CHAIN (link);
21623 if (arg)
21624 arg = DECL_CHAIN (arg);
21625 }
21626
21627 /* If this function type has an ellipsis, add a
21628 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21629 if (formal_type != void_type_node)
21630 gen_unspecified_parameters_die (function_or_method_type, context_die);
21631
21632 /* Make our second (and final) pass over the list of formal parameter types
21633 and output DIEs to represent those types (as necessary). */
21634 for (link = TYPE_ARG_TYPES (function_or_method_type);
21635 link && TREE_VALUE (link);
21636 link = TREE_CHAIN (link))
21637 gen_type_die (TREE_VALUE (link), context_die);
21638 }
21639
21640 /* We want to generate the DIE for TYPE so that we can generate the
21641 die for MEMBER, which has been defined; we will need to refer back
21642 to the member declaration nested within TYPE. If we're trying to
21643 generate minimal debug info for TYPE, processing TYPE won't do the
21644 trick; we need to attach the member declaration by hand. */
21645
21646 static void
21647 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21648 {
21649 gen_type_die (type, context_die);
21650
21651 /* If we're trying to avoid duplicate debug info, we may not have
21652 emitted the member decl for this function. Emit it now. */
21653 if (TYPE_STUB_DECL (type)
21654 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21655 && ! lookup_decl_die (member))
21656 {
21657 dw_die_ref type_die;
21658 gcc_assert (!decl_ultimate_origin (member));
21659
21660 push_decl_scope (type);
21661 type_die = lookup_type_die_strip_naming_typedef (type);
21662 if (TREE_CODE (member) == FUNCTION_DECL)
21663 gen_subprogram_die (member, type_die);
21664 else if (TREE_CODE (member) == FIELD_DECL)
21665 {
21666 /* Ignore the nameless fields that are used to skip bits but handle
21667 C++ anonymous unions and structs. */
21668 if (DECL_NAME (member) != NULL_TREE
21669 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21670 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21671 {
21672 struct vlr_context vlr_ctx = {
21673 DECL_CONTEXT (member), /* struct_type */
21674 NULL_TREE /* variant_part_offset */
21675 };
21676 gen_type_die (member_declared_type (member), type_die);
21677 gen_field_die (member, &vlr_ctx, type_die);
21678 }
21679 }
21680 else
21681 gen_variable_die (member, NULL_TREE, type_die);
21682
21683 pop_decl_scope ();
21684 }
21685 }
21686 \f
21687 /* Forward declare these functions, because they are mutually recursive
21688 with their set_block_* pairing functions. */
21689 static void set_decl_origin_self (tree);
21690
21691 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21692 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21693 that it points to the node itself, thus indicating that the node is its
21694 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21695 the given node is NULL, recursively descend the decl/block tree which
21696 it is the root of, and for each other ..._DECL or BLOCK node contained
21697 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21698 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21699 values to point to themselves. */
21700
21701 static void
21702 set_block_origin_self (tree stmt)
21703 {
21704 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21705 {
21706 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21707
21708 {
21709 tree local_decl;
21710
21711 for (local_decl = BLOCK_VARS (stmt);
21712 local_decl != NULL_TREE;
21713 local_decl = DECL_CHAIN (local_decl))
21714 /* Do not recurse on nested functions since the inlining status
21715 of parent and child can be different as per the DWARF spec. */
21716 if (TREE_CODE (local_decl) != FUNCTION_DECL
21717 && !DECL_EXTERNAL (local_decl))
21718 set_decl_origin_self (local_decl);
21719 }
21720
21721 {
21722 tree subblock;
21723
21724 for (subblock = BLOCK_SUBBLOCKS (stmt);
21725 subblock != NULL_TREE;
21726 subblock = BLOCK_CHAIN (subblock))
21727 set_block_origin_self (subblock); /* Recurse. */
21728 }
21729 }
21730 }
21731
21732 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21733 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21734 node to so that it points to the node itself, thus indicating that the
21735 node represents its own (abstract) origin. Additionally, if the
21736 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21737 the decl/block tree of which the given node is the root of, and for
21738 each other ..._DECL or BLOCK node contained therein whose
21739 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21740 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21741 point to themselves. */
21742
21743 static void
21744 set_decl_origin_self (tree decl)
21745 {
21746 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21747 {
21748 DECL_ABSTRACT_ORIGIN (decl) = decl;
21749 if (TREE_CODE (decl) == FUNCTION_DECL)
21750 {
21751 tree arg;
21752
21753 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21754 DECL_ABSTRACT_ORIGIN (arg) = arg;
21755 if (DECL_INITIAL (decl) != NULL_TREE
21756 && DECL_INITIAL (decl) != error_mark_node)
21757 set_block_origin_self (DECL_INITIAL (decl));
21758 }
21759 }
21760 }
21761 \f
21762 /* Mark the early DIE for DECL as the abstract instance. */
21763
21764 static void
21765 dwarf2out_abstract_function (tree decl)
21766 {
21767 dw_die_ref old_die;
21768
21769 /* Make sure we have the actual abstract inline, not a clone. */
21770 decl = DECL_ORIGIN (decl);
21771
21772 if (DECL_IGNORED_P (decl))
21773 return;
21774
21775 old_die = lookup_decl_die (decl);
21776 /* With early debug we always have an old DIE unless we are in LTO
21777 and the user did not compile but only link with debug. */
21778 if (in_lto_p && ! old_die)
21779 return;
21780 gcc_assert (old_die != NULL);
21781 if (get_AT (old_die, DW_AT_inline)
21782 || get_AT (old_die, DW_AT_abstract_origin))
21783 /* We've already generated the abstract instance. */
21784 return;
21785
21786 /* Go ahead and put DW_AT_inline on the DIE. */
21787 if (DECL_DECLARED_INLINE_P (decl))
21788 {
21789 if (cgraph_function_possibly_inlined_p (decl))
21790 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21791 else
21792 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21793 }
21794 else
21795 {
21796 if (cgraph_function_possibly_inlined_p (decl))
21797 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21798 else
21799 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21800 }
21801
21802 if (DECL_DECLARED_INLINE_P (decl)
21803 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21804 add_AT_flag (old_die, DW_AT_artificial, 1);
21805
21806 set_decl_origin_self (decl);
21807 }
21808
21809 /* Helper function of premark_used_types() which gets called through
21810 htab_traverse.
21811
21812 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21813 marked as unused by prune_unused_types. */
21814
21815 bool
21816 premark_used_types_helper (tree const &type, void *)
21817 {
21818 dw_die_ref die;
21819
21820 die = lookup_type_die (type);
21821 if (die != NULL)
21822 die->die_perennial_p = 1;
21823 return true;
21824 }
21825
21826 /* Helper function of premark_types_used_by_global_vars which gets called
21827 through htab_traverse.
21828
21829 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21830 marked as unused by prune_unused_types. The DIE of the type is marked
21831 only if the global variable using the type will actually be emitted. */
21832
21833 int
21834 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21835 void *)
21836 {
21837 struct types_used_by_vars_entry *entry;
21838 dw_die_ref die;
21839
21840 entry = (struct types_used_by_vars_entry *) *slot;
21841 gcc_assert (entry->type != NULL
21842 && entry->var_decl != NULL);
21843 die = lookup_type_die (entry->type);
21844 if (die)
21845 {
21846 /* Ask cgraph if the global variable really is to be emitted.
21847 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21848 varpool_node *node = varpool_node::get (entry->var_decl);
21849 if (node && node->definition)
21850 {
21851 die->die_perennial_p = 1;
21852 /* Keep the parent DIEs as well. */
21853 while ((die = die->die_parent) && die->die_perennial_p == 0)
21854 die->die_perennial_p = 1;
21855 }
21856 }
21857 return 1;
21858 }
21859
21860 /* Mark all members of used_types_hash as perennial. */
21861
21862 static void
21863 premark_used_types (struct function *fun)
21864 {
21865 if (fun && fun->used_types_hash)
21866 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21867 }
21868
21869 /* Mark all members of types_used_by_vars_entry as perennial. */
21870
21871 static void
21872 premark_types_used_by_global_vars (void)
21873 {
21874 if (types_used_by_vars_hash)
21875 types_used_by_vars_hash
21876 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21877 }
21878
21879 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21880 for CA_LOC call arg loc node. */
21881
21882 static dw_die_ref
21883 gen_call_site_die (tree decl, dw_die_ref subr_die,
21884 struct call_arg_loc_node *ca_loc)
21885 {
21886 dw_die_ref stmt_die = NULL, die;
21887 tree block = ca_loc->block;
21888
21889 while (block
21890 && block != DECL_INITIAL (decl)
21891 && TREE_CODE (block) == BLOCK)
21892 {
21893 stmt_die = BLOCK_DIE (block);
21894 if (stmt_die)
21895 break;
21896 block = BLOCK_SUPERCONTEXT (block);
21897 }
21898 if (stmt_die == NULL)
21899 stmt_die = subr_die;
21900 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21901 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21902 if (ca_loc->tail_call_p)
21903 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21904 if (ca_loc->symbol_ref)
21905 {
21906 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21907 if (tdie)
21908 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21909 else
21910 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21911 false);
21912 }
21913 return die;
21914 }
21915
21916 /* Generate a DIE to represent a declared function (either file-scope or
21917 block-local). */
21918
21919 static void
21920 gen_subprogram_die (tree decl, dw_die_ref context_die)
21921 {
21922 tree origin = decl_ultimate_origin (decl);
21923 dw_die_ref subr_die;
21924 dw_die_ref old_die = lookup_decl_die (decl);
21925
21926 /* This function gets called multiple times for different stages of
21927 the debug process. For example, for func() in this code:
21928
21929 namespace S
21930 {
21931 void func() { ... }
21932 }
21933
21934 ...we get called 4 times. Twice in early debug and twice in
21935 late debug:
21936
21937 Early debug
21938 -----------
21939
21940 1. Once while generating func() within the namespace. This is
21941 the declaration. The declaration bit below is set, as the
21942 context is the namespace.
21943
21944 A new DIE will be generated with DW_AT_declaration set.
21945
21946 2. Once for func() itself. This is the specification. The
21947 declaration bit below is clear as the context is the CU.
21948
21949 We will use the cached DIE from (1) to create a new DIE with
21950 DW_AT_specification pointing to the declaration in (1).
21951
21952 Late debug via rest_of_handle_final()
21953 -------------------------------------
21954
21955 3. Once generating func() within the namespace. This is also the
21956 declaration, as in (1), but this time we will early exit below
21957 as we have a cached DIE and a declaration needs no additional
21958 annotations (no locations), as the source declaration line
21959 info is enough.
21960
21961 4. Once for func() itself. As in (2), this is the specification,
21962 but this time we will re-use the cached DIE, and just annotate
21963 it with the location information that should now be available.
21964
21965 For something without namespaces, but with abstract instances, we
21966 are also called a multiple times:
21967
21968 class Base
21969 {
21970 public:
21971 Base (); // constructor declaration (1)
21972 };
21973
21974 Base::Base () { } // constructor specification (2)
21975
21976 Early debug
21977 -----------
21978
21979 1. Once for the Base() constructor by virtue of it being a
21980 member of the Base class. This is done via
21981 rest_of_type_compilation.
21982
21983 This is a declaration, so a new DIE will be created with
21984 DW_AT_declaration.
21985
21986 2. Once for the Base() constructor definition, but this time
21987 while generating the abstract instance of the base
21988 constructor (__base_ctor) which is being generated via early
21989 debug of reachable functions.
21990
21991 Even though we have a cached version of the declaration (1),
21992 we will create a DW_AT_specification of the declaration DIE
21993 in (1).
21994
21995 3. Once for the __base_ctor itself, but this time, we generate
21996 an DW_AT_abstract_origin version of the DW_AT_specification in
21997 (2).
21998
21999 Late debug via rest_of_handle_final
22000 -----------------------------------
22001
22002 4. One final time for the __base_ctor (which will have a cached
22003 DIE with DW_AT_abstract_origin created in (3). This time,
22004 we will just annotate the location information now
22005 available.
22006 */
22007 int declaration = (current_function_decl != decl
22008 || class_or_namespace_scope_p (context_die));
22009
22010 /* Now that the C++ front end lazily declares artificial member fns, we
22011 might need to retrofit the declaration into its class. */
22012 if (!declaration && !origin && !old_die
22013 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22014 && !class_or_namespace_scope_p (context_die)
22015 && debug_info_level > DINFO_LEVEL_TERSE)
22016 old_die = force_decl_die (decl);
22017
22018 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22019 if (origin != NULL)
22020 {
22021 gcc_assert (!declaration || local_scope_p (context_die));
22022
22023 /* Fixup die_parent for the abstract instance of a nested
22024 inline function. */
22025 if (old_die && old_die->die_parent == NULL)
22026 add_child_die (context_die, old_die);
22027
22028 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22029 {
22030 /* If we have a DW_AT_abstract_origin we have a working
22031 cached version. */
22032 subr_die = old_die;
22033 }
22034 else
22035 {
22036 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22037 add_abstract_origin_attribute (subr_die, origin);
22038 /* This is where the actual code for a cloned function is.
22039 Let's emit linkage name attribute for it. This helps
22040 debuggers to e.g, set breakpoints into
22041 constructors/destructors when the user asks "break
22042 K::K". */
22043 add_linkage_name (subr_die, decl);
22044 }
22045 }
22046 /* A cached copy, possibly from early dwarf generation. Reuse as
22047 much as possible. */
22048 else if (old_die)
22049 {
22050 /* A declaration that has been previously dumped needs no
22051 additional information. */
22052 if (declaration)
22053 return;
22054
22055 if (!get_AT_flag (old_die, DW_AT_declaration)
22056 /* We can have a normal definition following an inline one in the
22057 case of redefinition of GNU C extern inlines.
22058 It seems reasonable to use AT_specification in this case. */
22059 && !get_AT (old_die, DW_AT_inline))
22060 {
22061 /* Detect and ignore this case, where we are trying to output
22062 something we have already output. */
22063 if (get_AT (old_die, DW_AT_low_pc)
22064 || get_AT (old_die, DW_AT_ranges))
22065 return;
22066
22067 /* If we have no location information, this must be a
22068 partially generated DIE from early dwarf generation.
22069 Fall through and generate it. */
22070 }
22071
22072 /* If the definition comes from the same place as the declaration,
22073 maybe use the old DIE. We always want the DIE for this function
22074 that has the *_pc attributes to be under comp_unit_die so the
22075 debugger can find it. We also need to do this for abstract
22076 instances of inlines, since the spec requires the out-of-line copy
22077 to have the same parent. For local class methods, this doesn't
22078 apply; we just use the old DIE. */
22079 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22080 struct dwarf_file_data * file_index = lookup_filename (s.file);
22081 if ((is_cu_die (old_die->die_parent)
22082 /* This condition fixes the inconsistency/ICE with the
22083 following Fortran test (or some derivative thereof) while
22084 building libgfortran:
22085
22086 module some_m
22087 contains
22088 logical function funky (FLAG)
22089 funky = .true.
22090 end function
22091 end module
22092 */
22093 || (old_die->die_parent
22094 && old_die->die_parent->die_tag == DW_TAG_module)
22095 || context_die == NULL)
22096 && (DECL_ARTIFICIAL (decl)
22097 /* The location attributes may be in the abstract origin
22098 which in the case of LTO might be not available to
22099 look at. */
22100 || get_AT (old_die, DW_AT_abstract_origin)
22101 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22102 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22103 == (unsigned) s.line)
22104 && (!debug_column_info
22105 || s.column == 0
22106 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22107 == (unsigned) s.column)))))
22108 {
22109 subr_die = old_die;
22110
22111 /* Clear out the declaration attribute, but leave the
22112 parameters so they can be augmented with location
22113 information later. Unless this was a declaration, in
22114 which case, wipe out the nameless parameters and recreate
22115 them further down. */
22116 if (remove_AT (subr_die, DW_AT_declaration))
22117 {
22118
22119 remove_AT (subr_die, DW_AT_object_pointer);
22120 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22121 }
22122 }
22123 /* Make a specification pointing to the previously built
22124 declaration. */
22125 else
22126 {
22127 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22128 add_AT_specification (subr_die, old_die);
22129 add_pubname (decl, subr_die);
22130 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22131 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22132 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22133 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22134 if (debug_column_info
22135 && s.column
22136 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22137 != (unsigned) s.column))
22138 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22139
22140 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22141 emit the real type on the definition die. */
22142 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22143 {
22144 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22145 if (die == auto_die || die == decltype_auto_die)
22146 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22147 TYPE_UNQUALIFIED, false, context_die);
22148 }
22149
22150 /* When we process the method declaration, we haven't seen
22151 the out-of-class defaulted definition yet, so we have to
22152 recheck now. */
22153 if ((dwarf_version >= 5 || ! dwarf_strict)
22154 && !get_AT (subr_die, DW_AT_defaulted))
22155 {
22156 int defaulted
22157 = lang_hooks.decls.decl_dwarf_attribute (decl,
22158 DW_AT_defaulted);
22159 if (defaulted != -1)
22160 {
22161 /* Other values must have been handled before. */
22162 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22163 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22164 }
22165 }
22166 }
22167 }
22168 /* Create a fresh DIE for anything else. */
22169 else
22170 {
22171 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22172
22173 if (TREE_PUBLIC (decl))
22174 add_AT_flag (subr_die, DW_AT_external, 1);
22175
22176 add_name_and_src_coords_attributes (subr_die, decl);
22177 add_pubname (decl, subr_die);
22178 if (debug_info_level > DINFO_LEVEL_TERSE)
22179 {
22180 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22181 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22182 TYPE_UNQUALIFIED, false, context_die);
22183 }
22184
22185 add_pure_or_virtual_attribute (subr_die, decl);
22186 if (DECL_ARTIFICIAL (decl))
22187 add_AT_flag (subr_die, DW_AT_artificial, 1);
22188
22189 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22190 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22191
22192 add_alignment_attribute (subr_die, decl);
22193
22194 add_accessibility_attribute (subr_die, decl);
22195 }
22196
22197 /* Unless we have an existing non-declaration DIE, equate the new
22198 DIE. */
22199 if (!old_die || is_declaration_die (old_die))
22200 equate_decl_number_to_die (decl, subr_die);
22201
22202 if (declaration)
22203 {
22204 if (!old_die || !get_AT (old_die, DW_AT_inline))
22205 {
22206 add_AT_flag (subr_die, DW_AT_declaration, 1);
22207
22208 /* If this is an explicit function declaration then generate
22209 a DW_AT_explicit attribute. */
22210 if ((dwarf_version >= 3 || !dwarf_strict)
22211 && lang_hooks.decls.decl_dwarf_attribute (decl,
22212 DW_AT_explicit) == 1)
22213 add_AT_flag (subr_die, DW_AT_explicit, 1);
22214
22215 /* If this is a C++11 deleted special function member then generate
22216 a DW_AT_deleted attribute. */
22217 if ((dwarf_version >= 5 || !dwarf_strict)
22218 && lang_hooks.decls.decl_dwarf_attribute (decl,
22219 DW_AT_deleted) == 1)
22220 add_AT_flag (subr_die, DW_AT_deleted, 1);
22221
22222 /* If this is a C++11 defaulted special function member then
22223 generate a DW_AT_defaulted attribute. */
22224 if (dwarf_version >= 5 || !dwarf_strict)
22225 {
22226 int defaulted
22227 = lang_hooks.decls.decl_dwarf_attribute (decl,
22228 DW_AT_defaulted);
22229 if (defaulted != -1)
22230 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22231 }
22232
22233 /* If this is a C++11 non-static member function with & ref-qualifier
22234 then generate a DW_AT_reference attribute. */
22235 if ((dwarf_version >= 5 || !dwarf_strict)
22236 && lang_hooks.decls.decl_dwarf_attribute (decl,
22237 DW_AT_reference) == 1)
22238 add_AT_flag (subr_die, DW_AT_reference, 1);
22239
22240 /* If this is a C++11 non-static member function with &&
22241 ref-qualifier then generate a DW_AT_reference attribute. */
22242 if ((dwarf_version >= 5 || !dwarf_strict)
22243 && lang_hooks.decls.decl_dwarf_attribute (decl,
22244 DW_AT_rvalue_reference)
22245 == 1)
22246 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22247 }
22248 }
22249 /* For non DECL_EXTERNALs, if range information is available, fill
22250 the DIE with it. */
22251 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22252 {
22253 HOST_WIDE_INT cfa_fb_offset;
22254
22255 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22256
22257 if (!crtl->has_bb_partition)
22258 {
22259 dw_fde_ref fde = fun->fde;
22260 if (fde->dw_fde_begin)
22261 {
22262 /* We have already generated the labels. */
22263 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22264 fde->dw_fde_end, false);
22265 }
22266 else
22267 {
22268 /* Create start/end labels and add the range. */
22269 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22270 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22271 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22272 current_function_funcdef_no);
22273 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22274 current_function_funcdef_no);
22275 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22276 false);
22277 }
22278
22279 #if VMS_DEBUGGING_INFO
22280 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22281 Section 2.3 Prologue and Epilogue Attributes:
22282 When a breakpoint is set on entry to a function, it is generally
22283 desirable for execution to be suspended, not on the very first
22284 instruction of the function, but rather at a point after the
22285 function's frame has been set up, after any language defined local
22286 declaration processing has been completed, and before execution of
22287 the first statement of the function begins. Debuggers generally
22288 cannot properly determine where this point is. Similarly for a
22289 breakpoint set on exit from a function. The prologue and epilogue
22290 attributes allow a compiler to communicate the location(s) to use. */
22291
22292 {
22293 if (fde->dw_fde_vms_end_prologue)
22294 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22295 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22296
22297 if (fde->dw_fde_vms_begin_epilogue)
22298 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22299 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22300 }
22301 #endif
22302
22303 }
22304 else
22305 {
22306 /* Generate pubnames entries for the split function code ranges. */
22307 dw_fde_ref fde = fun->fde;
22308
22309 if (fde->dw_fde_second_begin)
22310 {
22311 if (dwarf_version >= 3 || !dwarf_strict)
22312 {
22313 /* We should use ranges for non-contiguous code section
22314 addresses. Use the actual code range for the initial
22315 section, since the HOT/COLD labels might precede an
22316 alignment offset. */
22317 bool range_list_added = false;
22318 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22319 fde->dw_fde_end, &range_list_added,
22320 false);
22321 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22322 fde->dw_fde_second_end,
22323 &range_list_added, false);
22324 if (range_list_added)
22325 add_ranges (NULL);
22326 }
22327 else
22328 {
22329 /* There is no real support in DW2 for this .. so we make
22330 a work-around. First, emit the pub name for the segment
22331 containing the function label. Then make and emit a
22332 simplified subprogram DIE for the second segment with the
22333 name pre-fixed by __hot/cold_sect_of_. We use the same
22334 linkage name for the second die so that gdb will find both
22335 sections when given "b foo". */
22336 const char *name = NULL;
22337 tree decl_name = DECL_NAME (decl);
22338 dw_die_ref seg_die;
22339
22340 /* Do the 'primary' section. */
22341 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22342 fde->dw_fde_end, false);
22343
22344 /* Build a minimal DIE for the secondary section. */
22345 seg_die = new_die (DW_TAG_subprogram,
22346 subr_die->die_parent, decl);
22347
22348 if (TREE_PUBLIC (decl))
22349 add_AT_flag (seg_die, DW_AT_external, 1);
22350
22351 if (decl_name != NULL
22352 && IDENTIFIER_POINTER (decl_name) != NULL)
22353 {
22354 name = dwarf2_name (decl, 1);
22355 if (! DECL_ARTIFICIAL (decl))
22356 add_src_coords_attributes (seg_die, decl);
22357
22358 add_linkage_name (seg_die, decl);
22359 }
22360 gcc_assert (name != NULL);
22361 add_pure_or_virtual_attribute (seg_die, decl);
22362 if (DECL_ARTIFICIAL (decl))
22363 add_AT_flag (seg_die, DW_AT_artificial, 1);
22364
22365 name = concat ("__second_sect_of_", name, NULL);
22366 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22367 fde->dw_fde_second_end, false);
22368 add_name_attribute (seg_die, name);
22369 if (want_pubnames ())
22370 add_pubname_string (name, seg_die);
22371 }
22372 }
22373 else
22374 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22375 false);
22376 }
22377
22378 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22379
22380 /* We define the "frame base" as the function's CFA. This is more
22381 convenient for several reasons: (1) It's stable across the prologue
22382 and epilogue, which makes it better than just a frame pointer,
22383 (2) With dwarf3, there exists a one-byte encoding that allows us
22384 to reference the .debug_frame data by proxy, but failing that,
22385 (3) We can at least reuse the code inspection and interpretation
22386 code that determines the CFA position at various points in the
22387 function. */
22388 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22389 {
22390 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22391 add_AT_loc (subr_die, DW_AT_frame_base, op);
22392 }
22393 else
22394 {
22395 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22396 if (list->dw_loc_next)
22397 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22398 else
22399 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22400 }
22401
22402 /* Compute a displacement from the "steady-state frame pointer" to
22403 the CFA. The former is what all stack slots and argument slots
22404 will reference in the rtl; the latter is what we've told the
22405 debugger about. We'll need to adjust all frame_base references
22406 by this displacement. */
22407 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22408
22409 if (fun->static_chain_decl)
22410 {
22411 /* DWARF requires here a location expression that computes the
22412 address of the enclosing subprogram's frame base. The machinery
22413 in tree-nested.c is supposed to store this specific address in the
22414 last field of the FRAME record. */
22415 const tree frame_type
22416 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22417 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22418
22419 tree fb_expr
22420 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22421 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22422 fb_expr, fb_decl, NULL_TREE);
22423
22424 add_AT_location_description (subr_die, DW_AT_static_link,
22425 loc_list_from_tree (fb_expr, 0, NULL));
22426 }
22427
22428 resolve_variable_values ();
22429 }
22430
22431 /* Generate child dies for template paramaters. */
22432 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22433 gen_generic_params_dies (decl);
22434
22435 /* Now output descriptions of the arguments for this function. This gets
22436 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22437 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22438 `...' at the end of the formal parameter list. In order to find out if
22439 there was a trailing ellipsis or not, we must instead look at the type
22440 associated with the FUNCTION_DECL. This will be a node of type
22441 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22442 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22443 an ellipsis at the end. */
22444
22445 /* In the case where we are describing a mere function declaration, all we
22446 need to do here (and all we *can* do here) is to describe the *types* of
22447 its formal parameters. */
22448 if (debug_info_level <= DINFO_LEVEL_TERSE)
22449 ;
22450 else if (declaration)
22451 gen_formal_types_die (decl, subr_die);
22452 else
22453 {
22454 /* Generate DIEs to represent all known formal parameters. */
22455 tree parm = DECL_ARGUMENTS (decl);
22456 tree generic_decl = early_dwarf
22457 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22458 tree generic_decl_parm = generic_decl
22459 ? DECL_ARGUMENTS (generic_decl)
22460 : NULL;
22461
22462 /* Now we want to walk the list of parameters of the function and
22463 emit their relevant DIEs.
22464
22465 We consider the case of DECL being an instance of a generic function
22466 as well as it being a normal function.
22467
22468 If DECL is an instance of a generic function we walk the
22469 parameters of the generic function declaration _and_ the parameters of
22470 DECL itself. This is useful because we want to emit specific DIEs for
22471 function parameter packs and those are declared as part of the
22472 generic function declaration. In that particular case,
22473 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22474 That DIE has children DIEs representing the set of arguments
22475 of the pack. Note that the set of pack arguments can be empty.
22476 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22477 children DIE.
22478
22479 Otherwise, we just consider the parameters of DECL. */
22480 while (generic_decl_parm || parm)
22481 {
22482 if (generic_decl_parm
22483 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22484 gen_formal_parameter_pack_die (generic_decl_parm,
22485 parm, subr_die,
22486 &parm);
22487 else if (parm && !POINTER_BOUNDS_P (parm))
22488 {
22489 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22490
22491 if (early_dwarf
22492 && parm == DECL_ARGUMENTS (decl)
22493 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22494 && parm_die
22495 && (dwarf_version >= 3 || !dwarf_strict))
22496 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22497
22498 parm = DECL_CHAIN (parm);
22499 }
22500 else if (parm)
22501 parm = DECL_CHAIN (parm);
22502
22503 if (generic_decl_parm)
22504 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22505 }
22506
22507 /* Decide whether we need an unspecified_parameters DIE at the end.
22508 There are 2 more cases to do this for: 1) the ansi ... declaration -
22509 this is detectable when the end of the arg list is not a
22510 void_type_node 2) an unprototyped function declaration (not a
22511 definition). This just means that we have no info about the
22512 parameters at all. */
22513 if (early_dwarf)
22514 {
22515 if (prototype_p (TREE_TYPE (decl)))
22516 {
22517 /* This is the prototyped case, check for.... */
22518 if (stdarg_p (TREE_TYPE (decl)))
22519 gen_unspecified_parameters_die (decl, subr_die);
22520 }
22521 else if (DECL_INITIAL (decl) == NULL_TREE)
22522 gen_unspecified_parameters_die (decl, subr_die);
22523 }
22524 }
22525
22526 if (subr_die != old_die)
22527 /* Add the calling convention attribute if requested. */
22528 add_calling_convention_attribute (subr_die, decl);
22529
22530 /* Output Dwarf info for all of the stuff within the body of the function
22531 (if it has one - it may be just a declaration).
22532
22533 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22534 a function. This BLOCK actually represents the outermost binding contour
22535 for the function, i.e. the contour in which the function's formal
22536 parameters and labels get declared. Curiously, it appears that the front
22537 end doesn't actually put the PARM_DECL nodes for the current function onto
22538 the BLOCK_VARS list for this outer scope, but are strung off of the
22539 DECL_ARGUMENTS list for the function instead.
22540
22541 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22542 the LABEL_DECL nodes for the function however, and we output DWARF info
22543 for those in decls_for_scope. Just within the `outer_scope' there will be
22544 a BLOCK node representing the function's outermost pair of curly braces,
22545 and any blocks used for the base and member initializers of a C++
22546 constructor function. */
22547 tree outer_scope = DECL_INITIAL (decl);
22548 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22549 {
22550 int call_site_note_count = 0;
22551 int tail_call_site_note_count = 0;
22552
22553 /* Emit a DW_TAG_variable DIE for a named return value. */
22554 if (DECL_NAME (DECL_RESULT (decl)))
22555 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22556
22557 /* The first time through decls_for_scope we will generate the
22558 DIEs for the locals. The second time, we fill in the
22559 location info. */
22560 decls_for_scope (outer_scope, subr_die);
22561
22562 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22563 {
22564 struct call_arg_loc_node *ca_loc;
22565 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22566 {
22567 dw_die_ref die = NULL;
22568 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22569 rtx arg, next_arg;
22570
22571 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22572 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22573 : NULL_RTX);
22574 arg; arg = next_arg)
22575 {
22576 dw_loc_descr_ref reg, val;
22577 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22578 dw_die_ref cdie, tdie = NULL;
22579
22580 next_arg = XEXP (arg, 1);
22581 if (REG_P (XEXP (XEXP (arg, 0), 0))
22582 && next_arg
22583 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22584 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22585 && REGNO (XEXP (XEXP (arg, 0), 0))
22586 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22587 next_arg = XEXP (next_arg, 1);
22588 if (mode == VOIDmode)
22589 {
22590 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22591 if (mode == VOIDmode)
22592 mode = GET_MODE (XEXP (arg, 0));
22593 }
22594 if (mode == VOIDmode || mode == BLKmode)
22595 continue;
22596 /* Get dynamic information about call target only if we
22597 have no static information: we cannot generate both
22598 DW_AT_call_origin and DW_AT_call_target
22599 attributes. */
22600 if (ca_loc->symbol_ref == NULL_RTX)
22601 {
22602 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22603 {
22604 tloc = XEXP (XEXP (arg, 0), 1);
22605 continue;
22606 }
22607 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22608 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22609 {
22610 tlocc = XEXP (XEXP (arg, 0), 1);
22611 continue;
22612 }
22613 }
22614 reg = NULL;
22615 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22616 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22617 VAR_INIT_STATUS_INITIALIZED);
22618 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22619 {
22620 rtx mem = XEXP (XEXP (arg, 0), 0);
22621 reg = mem_loc_descriptor (XEXP (mem, 0),
22622 get_address_mode (mem),
22623 GET_MODE (mem),
22624 VAR_INIT_STATUS_INITIALIZED);
22625 }
22626 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22627 == DEBUG_PARAMETER_REF)
22628 {
22629 tree tdecl
22630 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22631 tdie = lookup_decl_die (tdecl);
22632 if (tdie == NULL)
22633 continue;
22634 }
22635 else
22636 continue;
22637 if (reg == NULL
22638 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22639 != DEBUG_PARAMETER_REF)
22640 continue;
22641 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22642 VOIDmode,
22643 VAR_INIT_STATUS_INITIALIZED);
22644 if (val == NULL)
22645 continue;
22646 if (die == NULL)
22647 die = gen_call_site_die (decl, subr_die, ca_loc);
22648 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22649 NULL_TREE);
22650 if (reg != NULL)
22651 add_AT_loc (cdie, DW_AT_location, reg);
22652 else if (tdie != NULL)
22653 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22654 tdie);
22655 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22656 if (next_arg != XEXP (arg, 1))
22657 {
22658 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22659 if (mode == VOIDmode)
22660 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22661 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22662 0), 1),
22663 mode, VOIDmode,
22664 VAR_INIT_STATUS_INITIALIZED);
22665 if (val != NULL)
22666 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22667 val);
22668 }
22669 }
22670 if (die == NULL
22671 && (ca_loc->symbol_ref || tloc))
22672 die = gen_call_site_die (decl, subr_die, ca_loc);
22673 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22674 {
22675 dw_loc_descr_ref tval = NULL;
22676
22677 if (tloc != NULL_RTX)
22678 tval = mem_loc_descriptor (tloc,
22679 GET_MODE (tloc) == VOIDmode
22680 ? Pmode : GET_MODE (tloc),
22681 VOIDmode,
22682 VAR_INIT_STATUS_INITIALIZED);
22683 if (tval)
22684 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22685 else if (tlocc != NULL_RTX)
22686 {
22687 tval = mem_loc_descriptor (tlocc,
22688 GET_MODE (tlocc) == VOIDmode
22689 ? Pmode : GET_MODE (tlocc),
22690 VOIDmode,
22691 VAR_INIT_STATUS_INITIALIZED);
22692 if (tval)
22693 add_AT_loc (die,
22694 dwarf_AT (DW_AT_call_target_clobbered),
22695 tval);
22696 }
22697 }
22698 if (die != NULL)
22699 {
22700 call_site_note_count++;
22701 if (ca_loc->tail_call_p)
22702 tail_call_site_note_count++;
22703 }
22704 }
22705 }
22706 call_arg_locations = NULL;
22707 call_arg_loc_last = NULL;
22708 if (tail_call_site_count >= 0
22709 && tail_call_site_count == tail_call_site_note_count
22710 && (!dwarf_strict || dwarf_version >= 5))
22711 {
22712 if (call_site_count >= 0
22713 && call_site_count == call_site_note_count)
22714 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22715 else
22716 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22717 }
22718 call_site_count = -1;
22719 tail_call_site_count = -1;
22720 }
22721
22722 /* Mark used types after we have created DIEs for the functions scopes. */
22723 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22724 }
22725
22726 /* Returns a hash value for X (which really is a die_struct). */
22727
22728 hashval_t
22729 block_die_hasher::hash (die_struct *d)
22730 {
22731 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22732 }
22733
22734 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22735 as decl_id and die_parent of die_struct Y. */
22736
22737 bool
22738 block_die_hasher::equal (die_struct *x, die_struct *y)
22739 {
22740 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22741 }
22742
22743 /* Return TRUE if DECL, which may have been previously generated as
22744 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22745 true if decl (or its origin) is either an extern declaration or a
22746 class/namespace scoped declaration.
22747
22748 The declare_in_namespace support causes us to get two DIEs for one
22749 variable, both of which are declarations. We want to avoid
22750 considering one to be a specification, so we must test for
22751 DECLARATION and DW_AT_declaration. */
22752 static inline bool
22753 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22754 {
22755 return (old_die && TREE_STATIC (decl) && !declaration
22756 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22757 }
22758
22759 /* Return true if DECL is a local static. */
22760
22761 static inline bool
22762 local_function_static (tree decl)
22763 {
22764 gcc_assert (VAR_P (decl));
22765 return TREE_STATIC (decl)
22766 && DECL_CONTEXT (decl)
22767 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22768 }
22769
22770 /* Generate a DIE to represent a declared data object.
22771 Either DECL or ORIGIN must be non-null. */
22772
22773 static void
22774 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22775 {
22776 HOST_WIDE_INT off = 0;
22777 tree com_decl;
22778 tree decl_or_origin = decl ? decl : origin;
22779 tree ultimate_origin;
22780 dw_die_ref var_die;
22781 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22782 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22783 || class_or_namespace_scope_p (context_die));
22784 bool specialization_p = false;
22785 bool no_linkage_name = false;
22786
22787 /* While C++ inline static data members have definitions inside of the
22788 class, force the first DIE to be a declaration, then let gen_member_die
22789 reparent it to the class context and call gen_variable_die again
22790 to create the outside of the class DIE for the definition. */
22791 if (!declaration
22792 && old_die == NULL
22793 && decl
22794 && DECL_CONTEXT (decl)
22795 && TYPE_P (DECL_CONTEXT (decl))
22796 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22797 {
22798 declaration = true;
22799 if (dwarf_version < 5)
22800 no_linkage_name = true;
22801 }
22802
22803 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22804 if (decl || ultimate_origin)
22805 origin = ultimate_origin;
22806 com_decl = fortran_common (decl_or_origin, &off);
22807
22808 /* Symbol in common gets emitted as a child of the common block, in the form
22809 of a data member. */
22810 if (com_decl)
22811 {
22812 dw_die_ref com_die;
22813 dw_loc_list_ref loc = NULL;
22814 die_node com_die_arg;
22815
22816 var_die = lookup_decl_die (decl_or_origin);
22817 if (var_die)
22818 {
22819 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22820 {
22821 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22822 if (loc)
22823 {
22824 if (off)
22825 {
22826 /* Optimize the common case. */
22827 if (single_element_loc_list_p (loc)
22828 && loc->expr->dw_loc_opc == DW_OP_addr
22829 && loc->expr->dw_loc_next == NULL
22830 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22831 == SYMBOL_REF)
22832 {
22833 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22834 loc->expr->dw_loc_oprnd1.v.val_addr
22835 = plus_constant (GET_MODE (x), x , off);
22836 }
22837 else
22838 loc_list_plus_const (loc, off);
22839 }
22840 add_AT_location_description (var_die, DW_AT_location, loc);
22841 remove_AT (var_die, DW_AT_declaration);
22842 }
22843 }
22844 return;
22845 }
22846
22847 if (common_block_die_table == NULL)
22848 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22849
22850 com_die_arg.decl_id = DECL_UID (com_decl);
22851 com_die_arg.die_parent = context_die;
22852 com_die = common_block_die_table->find (&com_die_arg);
22853 if (! early_dwarf)
22854 loc = loc_list_from_tree (com_decl, 2, NULL);
22855 if (com_die == NULL)
22856 {
22857 const char *cnam
22858 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22859 die_node **slot;
22860
22861 com_die = new_die (DW_TAG_common_block, context_die, decl);
22862 add_name_and_src_coords_attributes (com_die, com_decl);
22863 if (loc)
22864 {
22865 add_AT_location_description (com_die, DW_AT_location, loc);
22866 /* Avoid sharing the same loc descriptor between
22867 DW_TAG_common_block and DW_TAG_variable. */
22868 loc = loc_list_from_tree (com_decl, 2, NULL);
22869 }
22870 else if (DECL_EXTERNAL (decl_or_origin))
22871 add_AT_flag (com_die, DW_AT_declaration, 1);
22872 if (want_pubnames ())
22873 add_pubname_string (cnam, com_die); /* ??? needed? */
22874 com_die->decl_id = DECL_UID (com_decl);
22875 slot = common_block_die_table->find_slot (com_die, INSERT);
22876 *slot = com_die;
22877 }
22878 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22879 {
22880 add_AT_location_description (com_die, DW_AT_location, loc);
22881 loc = loc_list_from_tree (com_decl, 2, NULL);
22882 remove_AT (com_die, DW_AT_declaration);
22883 }
22884 var_die = new_die (DW_TAG_variable, com_die, decl);
22885 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22886 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22887 decl_quals (decl_or_origin), false,
22888 context_die);
22889 add_alignment_attribute (var_die, decl);
22890 add_AT_flag (var_die, DW_AT_external, 1);
22891 if (loc)
22892 {
22893 if (off)
22894 {
22895 /* Optimize the common case. */
22896 if (single_element_loc_list_p (loc)
22897 && loc->expr->dw_loc_opc == DW_OP_addr
22898 && loc->expr->dw_loc_next == NULL
22899 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22900 {
22901 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22902 loc->expr->dw_loc_oprnd1.v.val_addr
22903 = plus_constant (GET_MODE (x), x, off);
22904 }
22905 else
22906 loc_list_plus_const (loc, off);
22907 }
22908 add_AT_location_description (var_die, DW_AT_location, loc);
22909 }
22910 else if (DECL_EXTERNAL (decl_or_origin))
22911 add_AT_flag (var_die, DW_AT_declaration, 1);
22912 if (decl)
22913 equate_decl_number_to_die (decl, var_die);
22914 return;
22915 }
22916
22917 if (old_die)
22918 {
22919 if (declaration)
22920 {
22921 /* A declaration that has been previously dumped, needs no
22922 further annotations, since it doesn't need location on
22923 the second pass. */
22924 return;
22925 }
22926 else if (decl_will_get_specification_p (old_die, decl, declaration)
22927 && !get_AT (old_die, DW_AT_specification))
22928 {
22929 /* Fall-thru so we can make a new variable die along with a
22930 DW_AT_specification. */
22931 }
22932 else if (origin && old_die->die_parent != context_die)
22933 {
22934 /* If we will be creating an inlined instance, we need a
22935 new DIE that will get annotated with
22936 DW_AT_abstract_origin. Clear things so we can get a
22937 new DIE. */
22938 gcc_assert (!DECL_ABSTRACT_P (decl));
22939 old_die = NULL;
22940 }
22941 else
22942 {
22943 /* If a DIE was dumped early, it still needs location info.
22944 Skip to where we fill the location bits. */
22945 var_die = old_die;
22946
22947 /* ??? In LTRANS we cannot annotate early created variably
22948 modified type DIEs without copying them and adjusting all
22949 references to them. Thus we dumped them again, also add a
22950 reference to them. */
22951 tree type = TREE_TYPE (decl_or_origin);
22952 if (in_lto_p
22953 && variably_modified_type_p
22954 (type, decl_function_context (decl_or_origin)))
22955 {
22956 if (decl_by_reference_p (decl_or_origin))
22957 add_type_attribute (var_die, TREE_TYPE (type),
22958 TYPE_UNQUALIFIED, false, context_die);
22959 else
22960 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22961 false, context_die);
22962 }
22963
22964 goto gen_variable_die_location;
22965 }
22966 }
22967
22968 /* For static data members, the declaration in the class is supposed
22969 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22970 also in DWARF2; the specification should still be DW_TAG_variable
22971 referencing the DW_TAG_member DIE. */
22972 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22973 var_die = new_die (DW_TAG_member, context_die, decl);
22974 else
22975 var_die = new_die (DW_TAG_variable, context_die, decl);
22976
22977 if (origin != NULL)
22978 add_abstract_origin_attribute (var_die, origin);
22979
22980 /* Loop unrolling can create multiple blocks that refer to the same
22981 static variable, so we must test for the DW_AT_declaration flag.
22982
22983 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22984 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22985 sharing them.
22986
22987 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22988 else if (decl_will_get_specification_p (old_die, decl, declaration))
22989 {
22990 /* This is a definition of a C++ class level static. */
22991 add_AT_specification (var_die, old_die);
22992 specialization_p = true;
22993 if (DECL_NAME (decl))
22994 {
22995 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22996 struct dwarf_file_data * file_index = lookup_filename (s.file);
22997
22998 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22999 add_AT_file (var_die, DW_AT_decl_file, file_index);
23000
23001 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23002 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23003
23004 if (debug_column_info
23005 && s.column
23006 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23007 != (unsigned) s.column))
23008 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23009
23010 if (old_die->die_tag == DW_TAG_member)
23011 add_linkage_name (var_die, decl);
23012 }
23013 }
23014 else
23015 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23016
23017 if ((origin == NULL && !specialization_p)
23018 || (origin != NULL
23019 && !DECL_ABSTRACT_P (decl_or_origin)
23020 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23021 decl_function_context
23022 (decl_or_origin))))
23023 {
23024 tree type = TREE_TYPE (decl_or_origin);
23025
23026 if (decl_by_reference_p (decl_or_origin))
23027 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23028 context_die);
23029 else
23030 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23031 context_die);
23032 }
23033
23034 if (origin == NULL && !specialization_p)
23035 {
23036 if (TREE_PUBLIC (decl))
23037 add_AT_flag (var_die, DW_AT_external, 1);
23038
23039 if (DECL_ARTIFICIAL (decl))
23040 add_AT_flag (var_die, DW_AT_artificial, 1);
23041
23042 add_alignment_attribute (var_die, decl);
23043
23044 add_accessibility_attribute (var_die, decl);
23045 }
23046
23047 if (declaration)
23048 add_AT_flag (var_die, DW_AT_declaration, 1);
23049
23050 if (decl && (DECL_ABSTRACT_P (decl)
23051 || !old_die || is_declaration_die (old_die)))
23052 equate_decl_number_to_die (decl, var_die);
23053
23054 gen_variable_die_location:
23055 if (! declaration
23056 && (! DECL_ABSTRACT_P (decl_or_origin)
23057 /* Local static vars are shared between all clones/inlines,
23058 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23059 already set. */
23060 || (VAR_P (decl_or_origin)
23061 && TREE_STATIC (decl_or_origin)
23062 && DECL_RTL_SET_P (decl_or_origin))))
23063 {
23064 if (early_dwarf)
23065 add_pubname (decl_or_origin, var_die);
23066 else
23067 add_location_or_const_value_attribute (var_die, decl_or_origin,
23068 decl == NULL);
23069 }
23070 else
23071 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23072
23073 if ((dwarf_version >= 4 || !dwarf_strict)
23074 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23075 DW_AT_const_expr) == 1
23076 && !get_AT (var_die, DW_AT_const_expr)
23077 && !specialization_p)
23078 add_AT_flag (var_die, DW_AT_const_expr, 1);
23079
23080 if (!dwarf_strict)
23081 {
23082 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23083 DW_AT_inline);
23084 if (inl != -1
23085 && !get_AT (var_die, DW_AT_inline)
23086 && !specialization_p)
23087 add_AT_unsigned (var_die, DW_AT_inline, inl);
23088 }
23089 }
23090
23091 /* Generate a DIE to represent a named constant. */
23092
23093 static void
23094 gen_const_die (tree decl, dw_die_ref context_die)
23095 {
23096 dw_die_ref const_die;
23097 tree type = TREE_TYPE (decl);
23098
23099 const_die = lookup_decl_die (decl);
23100 if (const_die)
23101 return;
23102
23103 const_die = new_die (DW_TAG_constant, context_die, decl);
23104 equate_decl_number_to_die (decl, const_die);
23105 add_name_and_src_coords_attributes (const_die, decl);
23106 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23107 if (TREE_PUBLIC (decl))
23108 add_AT_flag (const_die, DW_AT_external, 1);
23109 if (DECL_ARTIFICIAL (decl))
23110 add_AT_flag (const_die, DW_AT_artificial, 1);
23111 tree_add_const_value_attribute_for_decl (const_die, decl);
23112 }
23113
23114 /* Generate a DIE to represent a label identifier. */
23115
23116 static void
23117 gen_label_die (tree decl, dw_die_ref context_die)
23118 {
23119 tree origin = decl_ultimate_origin (decl);
23120 dw_die_ref lbl_die = lookup_decl_die (decl);
23121 rtx insn;
23122 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23123
23124 if (!lbl_die)
23125 {
23126 lbl_die = new_die (DW_TAG_label, context_die, decl);
23127 equate_decl_number_to_die (decl, lbl_die);
23128
23129 if (origin != NULL)
23130 add_abstract_origin_attribute (lbl_die, origin);
23131 else
23132 add_name_and_src_coords_attributes (lbl_die, decl);
23133 }
23134
23135 if (DECL_ABSTRACT_P (decl))
23136 equate_decl_number_to_die (decl, lbl_die);
23137 else if (! early_dwarf)
23138 {
23139 insn = DECL_RTL_IF_SET (decl);
23140
23141 /* Deleted labels are programmer specified labels which have been
23142 eliminated because of various optimizations. We still emit them
23143 here so that it is possible to put breakpoints on them. */
23144 if (insn
23145 && (LABEL_P (insn)
23146 || ((NOTE_P (insn)
23147 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23148 {
23149 /* When optimization is enabled (via -O) some parts of the compiler
23150 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23151 represent source-level labels which were explicitly declared by
23152 the user. This really shouldn't be happening though, so catch
23153 it if it ever does happen. */
23154 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23155
23156 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23157 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23158 }
23159 else if (insn
23160 && NOTE_P (insn)
23161 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23162 && CODE_LABEL_NUMBER (insn) != -1)
23163 {
23164 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23165 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23166 }
23167 }
23168 }
23169
23170 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23171 attributes to the DIE for a block STMT, to describe where the inlined
23172 function was called from. This is similar to add_src_coords_attributes. */
23173
23174 static inline void
23175 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23176 {
23177 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23178
23179 if (dwarf_version >= 3 || !dwarf_strict)
23180 {
23181 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23182 add_AT_unsigned (die, DW_AT_call_line, s.line);
23183 if (debug_column_info && s.column)
23184 add_AT_unsigned (die, DW_AT_call_column, s.column);
23185 }
23186 }
23187
23188
23189 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23190 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23191
23192 static inline void
23193 add_high_low_attributes (tree stmt, dw_die_ref die)
23194 {
23195 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23196
23197 if (BLOCK_FRAGMENT_CHAIN (stmt)
23198 && (dwarf_version >= 3 || !dwarf_strict))
23199 {
23200 tree chain, superblock = NULL_TREE;
23201 dw_die_ref pdie;
23202 dw_attr_node *attr = NULL;
23203
23204 if (inlined_function_outer_scope_p (stmt))
23205 {
23206 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23207 BLOCK_NUMBER (stmt));
23208 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23209 }
23210
23211 /* Optimize duplicate .debug_ranges lists or even tails of
23212 lists. If this BLOCK has same ranges as its supercontext,
23213 lookup DW_AT_ranges attribute in the supercontext (and
23214 recursively so), verify that the ranges_table contains the
23215 right values and use it instead of adding a new .debug_range. */
23216 for (chain = stmt, pdie = die;
23217 BLOCK_SAME_RANGE (chain);
23218 chain = BLOCK_SUPERCONTEXT (chain))
23219 {
23220 dw_attr_node *new_attr;
23221
23222 pdie = pdie->die_parent;
23223 if (pdie == NULL)
23224 break;
23225 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23226 break;
23227 new_attr = get_AT (pdie, DW_AT_ranges);
23228 if (new_attr == NULL
23229 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23230 break;
23231 attr = new_attr;
23232 superblock = BLOCK_SUPERCONTEXT (chain);
23233 }
23234 if (attr != NULL
23235 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23236 == BLOCK_NUMBER (superblock))
23237 && BLOCK_FRAGMENT_CHAIN (superblock))
23238 {
23239 unsigned long off = attr->dw_attr_val.v.val_offset;
23240 unsigned long supercnt = 0, thiscnt = 0;
23241 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23242 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23243 {
23244 ++supercnt;
23245 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23246 == BLOCK_NUMBER (chain));
23247 }
23248 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23249 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23250 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23251 ++thiscnt;
23252 gcc_assert (supercnt >= thiscnt);
23253 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23254 false);
23255 note_rnglist_head (off + supercnt - thiscnt);
23256 return;
23257 }
23258
23259 unsigned int offset = add_ranges (stmt, true);
23260 add_AT_range_list (die, DW_AT_ranges, offset, false);
23261 note_rnglist_head (offset);
23262
23263 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23264 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23265 do
23266 {
23267 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23268 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23269 chain = BLOCK_FRAGMENT_CHAIN (chain);
23270 }
23271 while (chain);
23272 add_ranges (NULL);
23273 }
23274 else
23275 {
23276 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23277 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23278 BLOCK_NUMBER (stmt));
23279 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23280 BLOCK_NUMBER (stmt));
23281 add_AT_low_high_pc (die, label, label_high, false);
23282 }
23283 }
23284
23285 /* Generate a DIE for a lexical block. */
23286
23287 static void
23288 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23289 {
23290 dw_die_ref old_die = BLOCK_DIE (stmt);
23291 dw_die_ref stmt_die = NULL;
23292 if (!old_die)
23293 {
23294 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23295 BLOCK_DIE (stmt) = stmt_die;
23296 }
23297
23298 if (BLOCK_ABSTRACT (stmt))
23299 {
23300 if (old_die)
23301 {
23302 /* This must have been generated early and it won't even
23303 need location information since it's a DW_AT_inline
23304 function. */
23305 if (flag_checking)
23306 for (dw_die_ref c = context_die; c; c = c->die_parent)
23307 if (c->die_tag == DW_TAG_inlined_subroutine
23308 || c->die_tag == DW_TAG_subprogram)
23309 {
23310 gcc_assert (get_AT (c, DW_AT_inline));
23311 break;
23312 }
23313 return;
23314 }
23315 }
23316 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23317 {
23318 /* If this is an inlined instance, create a new lexical die for
23319 anything below to attach DW_AT_abstract_origin to. */
23320 if (old_die)
23321 {
23322 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23323 BLOCK_DIE (stmt) = stmt_die;
23324 old_die = NULL;
23325 }
23326
23327 tree origin = block_ultimate_origin (stmt);
23328 if (origin != NULL_TREE && origin != stmt)
23329 add_abstract_origin_attribute (stmt_die, origin);
23330 }
23331
23332 if (old_die)
23333 stmt_die = old_die;
23334
23335 /* A non abstract block whose blocks have already been reordered
23336 should have the instruction range for this block. If so, set the
23337 high/low attributes. */
23338 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23339 {
23340 gcc_assert (stmt_die);
23341 add_high_low_attributes (stmt, stmt_die);
23342 }
23343
23344 decls_for_scope (stmt, stmt_die);
23345 }
23346
23347 /* Generate a DIE for an inlined subprogram. */
23348
23349 static void
23350 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23351 {
23352 tree decl;
23353
23354 /* The instance of function that is effectively being inlined shall not
23355 be abstract. */
23356 gcc_assert (! BLOCK_ABSTRACT (stmt));
23357
23358 decl = block_ultimate_origin (stmt);
23359
23360 /* Make sure any inlined functions are known to be inlineable. */
23361 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23362 || cgraph_function_possibly_inlined_p (decl));
23363
23364 if (! BLOCK_ABSTRACT (stmt))
23365 {
23366 dw_die_ref subr_die
23367 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23368
23369 if (call_arg_locations)
23370 BLOCK_DIE (stmt) = subr_die;
23371 add_abstract_origin_attribute (subr_die, decl);
23372 if (TREE_ASM_WRITTEN (stmt))
23373 add_high_low_attributes (stmt, subr_die);
23374 add_call_src_coords_attributes (stmt, subr_die);
23375
23376 decls_for_scope (stmt, subr_die);
23377 }
23378 }
23379
23380 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23381 the comment for VLR_CONTEXT. */
23382
23383 static void
23384 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23385 {
23386 dw_die_ref decl_die;
23387
23388 if (TREE_TYPE (decl) == error_mark_node)
23389 return;
23390
23391 decl_die = new_die (DW_TAG_member, context_die, decl);
23392 add_name_and_src_coords_attributes (decl_die, decl);
23393 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23394 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23395 context_die);
23396
23397 if (DECL_BIT_FIELD_TYPE (decl))
23398 {
23399 add_byte_size_attribute (decl_die, decl);
23400 add_bit_size_attribute (decl_die, decl);
23401 add_bit_offset_attribute (decl_die, decl, ctx);
23402 }
23403
23404 add_alignment_attribute (decl_die, decl);
23405
23406 /* If we have a variant part offset, then we are supposed to process a member
23407 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23408 trees. */
23409 gcc_assert (ctx->variant_part_offset == NULL_TREE
23410 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23411 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23412 add_data_member_location_attribute (decl_die, decl, ctx);
23413
23414 if (DECL_ARTIFICIAL (decl))
23415 add_AT_flag (decl_die, DW_AT_artificial, 1);
23416
23417 add_accessibility_attribute (decl_die, decl);
23418
23419 /* Equate decl number to die, so that we can look up this decl later on. */
23420 equate_decl_number_to_die (decl, decl_die);
23421 }
23422
23423 /* Generate a DIE for a pointer to a member type. TYPE can be an
23424 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23425 pointer to member function. */
23426
23427 static void
23428 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23429 {
23430 if (lookup_type_die (type))
23431 return;
23432
23433 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23434 scope_die_for (type, context_die), type);
23435
23436 equate_type_number_to_die (type, ptr_die);
23437 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23438 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23439 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23440 context_die);
23441 add_alignment_attribute (ptr_die, type);
23442
23443 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23444 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23445 {
23446 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23447 add_AT_loc (ptr_die, DW_AT_use_location, op);
23448 }
23449 }
23450
23451 static char *producer_string;
23452
23453 /* Return a heap allocated producer string including command line options
23454 if -grecord-gcc-switches. */
23455
23456 static char *
23457 gen_producer_string (void)
23458 {
23459 size_t j;
23460 auto_vec<const char *> switches;
23461 const char *language_string = lang_hooks.name;
23462 char *producer, *tail;
23463 const char *p;
23464 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23465 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23466
23467 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23468 switch (save_decoded_options[j].opt_index)
23469 {
23470 case OPT_o:
23471 case OPT_d:
23472 case OPT_dumpbase:
23473 case OPT_dumpdir:
23474 case OPT_auxbase:
23475 case OPT_auxbase_strip:
23476 case OPT_quiet:
23477 case OPT_version:
23478 case OPT_v:
23479 case OPT_w:
23480 case OPT_L:
23481 case OPT_D:
23482 case OPT_I:
23483 case OPT_U:
23484 case OPT_SPECIAL_unknown:
23485 case OPT_SPECIAL_ignore:
23486 case OPT_SPECIAL_program_name:
23487 case OPT_SPECIAL_input_file:
23488 case OPT_grecord_gcc_switches:
23489 case OPT__output_pch_:
23490 case OPT_fdiagnostics_show_location_:
23491 case OPT_fdiagnostics_show_option:
23492 case OPT_fdiagnostics_show_caret:
23493 case OPT_fdiagnostics_color_:
23494 case OPT_fverbose_asm:
23495 case OPT____:
23496 case OPT__sysroot_:
23497 case OPT_nostdinc:
23498 case OPT_nostdinc__:
23499 case OPT_fpreprocessed:
23500 case OPT_fltrans_output_list_:
23501 case OPT_fresolution_:
23502 case OPT_fdebug_prefix_map_:
23503 case OPT_fcompare_debug:
23504 /* Ignore these. */
23505 continue;
23506 default:
23507 if (cl_options[save_decoded_options[j].opt_index].flags
23508 & CL_NO_DWARF_RECORD)
23509 continue;
23510 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23511 == '-');
23512 switch (save_decoded_options[j].canonical_option[0][1])
23513 {
23514 case 'M':
23515 case 'i':
23516 case 'W':
23517 continue;
23518 case 'f':
23519 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23520 "dump", 4) == 0)
23521 continue;
23522 break;
23523 default:
23524 break;
23525 }
23526 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23527 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23528 break;
23529 }
23530
23531 producer = XNEWVEC (char, plen + 1 + len + 1);
23532 tail = producer;
23533 sprintf (tail, "%s %s", language_string, version_string);
23534 tail += plen;
23535
23536 FOR_EACH_VEC_ELT (switches, j, p)
23537 {
23538 len = strlen (p);
23539 *tail = ' ';
23540 memcpy (tail + 1, p, len);
23541 tail += len + 1;
23542 }
23543
23544 *tail = '\0';
23545 return producer;
23546 }
23547
23548 /* Given a C and/or C++ language/version string return the "highest".
23549 C++ is assumed to be "higher" than C in this case. Used for merging
23550 LTO translation unit languages. */
23551 static const char *
23552 highest_c_language (const char *lang1, const char *lang2)
23553 {
23554 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23555 return "GNU C++17";
23556 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23557 return "GNU C++14";
23558 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23559 return "GNU C++11";
23560 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23561 return "GNU C++98";
23562
23563 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23564 return "GNU C17";
23565 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23566 return "GNU C11";
23567 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23568 return "GNU C99";
23569 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23570 return "GNU C89";
23571
23572 gcc_unreachable ();
23573 }
23574
23575
23576 /* Generate the DIE for the compilation unit. */
23577
23578 static dw_die_ref
23579 gen_compile_unit_die (const char *filename)
23580 {
23581 dw_die_ref die;
23582 const char *language_string = lang_hooks.name;
23583 int language;
23584
23585 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23586
23587 if (filename)
23588 {
23589 add_name_attribute (die, filename);
23590 /* Don't add cwd for <built-in>. */
23591 if (filename[0] != '<')
23592 add_comp_dir_attribute (die);
23593 }
23594
23595 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23596
23597 /* If our producer is LTO try to figure out a common language to use
23598 from the global list of translation units. */
23599 if (strcmp (language_string, "GNU GIMPLE") == 0)
23600 {
23601 unsigned i;
23602 tree t;
23603 const char *common_lang = NULL;
23604
23605 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23606 {
23607 if (!TRANSLATION_UNIT_LANGUAGE (t))
23608 continue;
23609 if (!common_lang)
23610 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23611 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23612 ;
23613 else if (strncmp (common_lang, "GNU C", 5) == 0
23614 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23615 /* Mixing C and C++ is ok, use C++ in that case. */
23616 common_lang = highest_c_language (common_lang,
23617 TRANSLATION_UNIT_LANGUAGE (t));
23618 else
23619 {
23620 /* Fall back to C. */
23621 common_lang = NULL;
23622 break;
23623 }
23624 }
23625
23626 if (common_lang)
23627 language_string = common_lang;
23628 }
23629
23630 language = DW_LANG_C;
23631 if (strncmp (language_string, "GNU C", 5) == 0
23632 && ISDIGIT (language_string[5]))
23633 {
23634 language = DW_LANG_C89;
23635 if (dwarf_version >= 3 || !dwarf_strict)
23636 {
23637 if (strcmp (language_string, "GNU C89") != 0)
23638 language = DW_LANG_C99;
23639
23640 if (dwarf_version >= 5 /* || !dwarf_strict */)
23641 if (strcmp (language_string, "GNU C11") == 0
23642 || strcmp (language_string, "GNU C17") == 0)
23643 language = DW_LANG_C11;
23644 }
23645 }
23646 else if (strncmp (language_string, "GNU C++", 7) == 0)
23647 {
23648 language = DW_LANG_C_plus_plus;
23649 if (dwarf_version >= 5 /* || !dwarf_strict */)
23650 {
23651 if (strcmp (language_string, "GNU C++11") == 0)
23652 language = DW_LANG_C_plus_plus_11;
23653 else if (strcmp (language_string, "GNU C++14") == 0)
23654 language = DW_LANG_C_plus_plus_14;
23655 else if (strcmp (language_string, "GNU C++17") == 0)
23656 /* For now. */
23657 language = DW_LANG_C_plus_plus_14;
23658 }
23659 }
23660 else if (strcmp (language_string, "GNU F77") == 0)
23661 language = DW_LANG_Fortran77;
23662 else if (dwarf_version >= 3 || !dwarf_strict)
23663 {
23664 if (strcmp (language_string, "GNU Ada") == 0)
23665 language = DW_LANG_Ada95;
23666 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23667 {
23668 language = DW_LANG_Fortran95;
23669 if (dwarf_version >= 5 /* || !dwarf_strict */)
23670 {
23671 if (strcmp (language_string, "GNU Fortran2003") == 0)
23672 language = DW_LANG_Fortran03;
23673 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23674 language = DW_LANG_Fortran08;
23675 }
23676 }
23677 else if (strcmp (language_string, "GNU Objective-C") == 0)
23678 language = DW_LANG_ObjC;
23679 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23680 language = DW_LANG_ObjC_plus_plus;
23681 else if (dwarf_version >= 5 || !dwarf_strict)
23682 {
23683 if (strcmp (language_string, "GNU Go") == 0)
23684 language = DW_LANG_Go;
23685 }
23686 }
23687 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23688 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23689 language = DW_LANG_Fortran90;
23690
23691 add_AT_unsigned (die, DW_AT_language, language);
23692
23693 switch (language)
23694 {
23695 case DW_LANG_Fortran77:
23696 case DW_LANG_Fortran90:
23697 case DW_LANG_Fortran95:
23698 case DW_LANG_Fortran03:
23699 case DW_LANG_Fortran08:
23700 /* Fortran has case insensitive identifiers and the front-end
23701 lowercases everything. */
23702 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23703 break;
23704 default:
23705 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23706 break;
23707 }
23708 return die;
23709 }
23710
23711 /* Generate the DIE for a base class. */
23712
23713 static void
23714 gen_inheritance_die (tree binfo, tree access, tree type,
23715 dw_die_ref context_die)
23716 {
23717 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23718 struct vlr_context ctx = { type, NULL };
23719
23720 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23721 context_die);
23722 add_data_member_location_attribute (die, binfo, &ctx);
23723
23724 if (BINFO_VIRTUAL_P (binfo))
23725 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23726
23727 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23728 children, otherwise the default is DW_ACCESS_public. In DWARF2
23729 the default has always been DW_ACCESS_private. */
23730 if (access == access_public_node)
23731 {
23732 if (dwarf_version == 2
23733 || context_die->die_tag == DW_TAG_class_type)
23734 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23735 }
23736 else if (access == access_protected_node)
23737 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23738 else if (dwarf_version > 2
23739 && context_die->die_tag != DW_TAG_class_type)
23740 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23741 }
23742
23743 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23744 structure. */
23745 static bool
23746 is_variant_part (tree decl)
23747 {
23748 return (TREE_CODE (decl) == FIELD_DECL
23749 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23750 }
23751
23752 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23753 return the FIELD_DECL. Return NULL_TREE otherwise. */
23754
23755 static tree
23756 analyze_discr_in_predicate (tree operand, tree struct_type)
23757 {
23758 bool continue_stripping = true;
23759 while (continue_stripping)
23760 switch (TREE_CODE (operand))
23761 {
23762 CASE_CONVERT:
23763 operand = TREE_OPERAND (operand, 0);
23764 break;
23765 default:
23766 continue_stripping = false;
23767 break;
23768 }
23769
23770 /* Match field access to members of struct_type only. */
23771 if (TREE_CODE (operand) == COMPONENT_REF
23772 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23773 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23774 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23775 return TREE_OPERAND (operand, 1);
23776 else
23777 return NULL_TREE;
23778 }
23779
23780 /* Check that SRC is a constant integer that can be represented as a native
23781 integer constant (either signed or unsigned). If so, store it into DEST and
23782 return true. Return false otherwise. */
23783
23784 static bool
23785 get_discr_value (tree src, dw_discr_value *dest)
23786 {
23787 tree discr_type = TREE_TYPE (src);
23788
23789 if (lang_hooks.types.get_debug_type)
23790 {
23791 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23792 if (debug_type != NULL)
23793 discr_type = debug_type;
23794 }
23795
23796 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23797 return false;
23798
23799 /* Signedness can vary between the original type and the debug type. This
23800 can happen for character types in Ada for instance: the character type
23801 used for code generation can be signed, to be compatible with the C one,
23802 but from a debugger point of view, it must be unsigned. */
23803 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23804 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23805
23806 if (is_orig_unsigned != is_debug_unsigned)
23807 src = fold_convert (discr_type, src);
23808
23809 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23810 return false;
23811
23812 dest->pos = is_debug_unsigned;
23813 if (is_debug_unsigned)
23814 dest->v.uval = tree_to_uhwi (src);
23815 else
23816 dest->v.sval = tree_to_shwi (src);
23817
23818 return true;
23819 }
23820
23821 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23822 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23823 store NULL_TREE in DISCR_DECL. Otherwise:
23824
23825 - store the discriminant field in STRUCT_TYPE that controls the variant
23826 part to *DISCR_DECL
23827
23828 - put in *DISCR_LISTS_P an array where for each variant, the item
23829 represents the corresponding matching list of discriminant values.
23830
23831 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23832 the above array.
23833
23834 Note that when the array is allocated (i.e. when the analysis is
23835 successful), it is up to the caller to free the array. */
23836
23837 static void
23838 analyze_variants_discr (tree variant_part_decl,
23839 tree struct_type,
23840 tree *discr_decl,
23841 dw_discr_list_ref **discr_lists_p,
23842 unsigned *discr_lists_length)
23843 {
23844 tree variant_part_type = TREE_TYPE (variant_part_decl);
23845 tree variant;
23846 dw_discr_list_ref *discr_lists;
23847 unsigned i;
23848
23849 /* Compute how many variants there are in this variant part. */
23850 *discr_lists_length = 0;
23851 for (variant = TYPE_FIELDS (variant_part_type);
23852 variant != NULL_TREE;
23853 variant = DECL_CHAIN (variant))
23854 ++*discr_lists_length;
23855
23856 *discr_decl = NULL_TREE;
23857 *discr_lists_p
23858 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23859 sizeof (**discr_lists_p));
23860 discr_lists = *discr_lists_p;
23861
23862 /* And then analyze all variants to extract discriminant information for all
23863 of them. This analysis is conservative: as soon as we detect something we
23864 do not support, abort everything and pretend we found nothing. */
23865 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23866 variant != NULL_TREE;
23867 variant = DECL_CHAIN (variant), ++i)
23868 {
23869 tree match_expr = DECL_QUALIFIER (variant);
23870
23871 /* Now, try to analyze the predicate and deduce a discriminant for
23872 it. */
23873 if (match_expr == boolean_true_node)
23874 /* Typically happens for the default variant: it matches all cases that
23875 previous variants rejected. Don't output any matching value for
23876 this one. */
23877 continue;
23878
23879 /* The following loop tries to iterate over each discriminant
23880 possibility: single values or ranges. */
23881 while (match_expr != NULL_TREE)
23882 {
23883 tree next_round_match_expr;
23884 tree candidate_discr = NULL_TREE;
23885 dw_discr_list_ref new_node = NULL;
23886
23887 /* Possibilities are matched one after the other by nested
23888 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23889 continue with the rest at next iteration. */
23890 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23891 {
23892 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23893 match_expr = TREE_OPERAND (match_expr, 1);
23894 }
23895 else
23896 next_round_match_expr = NULL_TREE;
23897
23898 if (match_expr == boolean_false_node)
23899 /* This sub-expression matches nothing: just wait for the next
23900 one. */
23901 ;
23902
23903 else if (TREE_CODE (match_expr) == EQ_EXPR)
23904 {
23905 /* We are matching: <discr_field> == <integer_cst>
23906 This sub-expression matches a single value. */
23907 tree integer_cst = TREE_OPERAND (match_expr, 1);
23908
23909 candidate_discr
23910 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23911 struct_type);
23912
23913 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23914 if (!get_discr_value (integer_cst,
23915 &new_node->dw_discr_lower_bound))
23916 goto abort;
23917 new_node->dw_discr_range = false;
23918 }
23919
23920 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23921 {
23922 /* We are matching:
23923 <discr_field> > <integer_cst>
23924 && <discr_field> < <integer_cst>.
23925 This sub-expression matches the range of values between the
23926 two matched integer constants. Note that comparisons can be
23927 inclusive or exclusive. */
23928 tree candidate_discr_1, candidate_discr_2;
23929 tree lower_cst, upper_cst;
23930 bool lower_cst_included, upper_cst_included;
23931 tree lower_op = TREE_OPERAND (match_expr, 0);
23932 tree upper_op = TREE_OPERAND (match_expr, 1);
23933
23934 /* When the comparison is exclusive, the integer constant is not
23935 the discriminant range bound we are looking for: we will have
23936 to increment or decrement it. */
23937 if (TREE_CODE (lower_op) == GE_EXPR)
23938 lower_cst_included = true;
23939 else if (TREE_CODE (lower_op) == GT_EXPR)
23940 lower_cst_included = false;
23941 else
23942 goto abort;
23943
23944 if (TREE_CODE (upper_op) == LE_EXPR)
23945 upper_cst_included = true;
23946 else if (TREE_CODE (upper_op) == LT_EXPR)
23947 upper_cst_included = false;
23948 else
23949 goto abort;
23950
23951 /* Extract the discriminant from the first operand and check it
23952 is consistant with the same analysis in the second
23953 operand. */
23954 candidate_discr_1
23955 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23956 struct_type);
23957 candidate_discr_2
23958 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23959 struct_type);
23960 if (candidate_discr_1 == candidate_discr_2)
23961 candidate_discr = candidate_discr_1;
23962 else
23963 goto abort;
23964
23965 /* Extract bounds from both. */
23966 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23967 lower_cst = TREE_OPERAND (lower_op, 1);
23968 upper_cst = TREE_OPERAND (upper_op, 1);
23969
23970 if (!lower_cst_included)
23971 lower_cst
23972 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23973 build_int_cst (TREE_TYPE (lower_cst), 1));
23974 if (!upper_cst_included)
23975 upper_cst
23976 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23977 build_int_cst (TREE_TYPE (upper_cst), 1));
23978
23979 if (!get_discr_value (lower_cst,
23980 &new_node->dw_discr_lower_bound)
23981 || !get_discr_value (upper_cst,
23982 &new_node->dw_discr_upper_bound))
23983 goto abort;
23984
23985 new_node->dw_discr_range = true;
23986 }
23987
23988 else
23989 /* Unsupported sub-expression: we cannot determine the set of
23990 matching discriminant values. Abort everything. */
23991 goto abort;
23992
23993 /* If the discriminant info is not consistant with what we saw so
23994 far, consider the analysis failed and abort everything. */
23995 if (candidate_discr == NULL_TREE
23996 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23997 goto abort;
23998 else
23999 *discr_decl = candidate_discr;
24000
24001 if (new_node != NULL)
24002 {
24003 new_node->dw_discr_next = discr_lists[i];
24004 discr_lists[i] = new_node;
24005 }
24006 match_expr = next_round_match_expr;
24007 }
24008 }
24009
24010 /* If we reach this point, we could match everything we were interested
24011 in. */
24012 return;
24013
24014 abort:
24015 /* Clean all data structure and return no result. */
24016 free (*discr_lists_p);
24017 *discr_lists_p = NULL;
24018 *discr_decl = NULL_TREE;
24019 }
24020
24021 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24022 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24023 under CONTEXT_DIE.
24024
24025 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24026 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24027 this type, which are record types, represent the available variants and each
24028 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24029 values are inferred from these attributes.
24030
24031 In trees, the offsets for the fields inside these sub-records are relative
24032 to the variant part itself, whereas the corresponding DIEs should have
24033 offset attributes that are relative to the embedding record base address.
24034 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24035 must be an expression that computes the offset of the variant part to
24036 describe in DWARF. */
24037
24038 static void
24039 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24040 dw_die_ref context_die)
24041 {
24042 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24043 tree variant_part_offset = vlr_ctx->variant_part_offset;
24044 struct loc_descr_context ctx = {
24045 vlr_ctx->struct_type, /* context_type */
24046 NULL_TREE, /* base_decl */
24047 NULL, /* dpi */
24048 false, /* placeholder_arg */
24049 false /* placeholder_seen */
24050 };
24051
24052 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24053 NULL_TREE if there is no such field. */
24054 tree discr_decl = NULL_TREE;
24055 dw_discr_list_ref *discr_lists;
24056 unsigned discr_lists_length = 0;
24057 unsigned i;
24058
24059 dw_die_ref dwarf_proc_die = NULL;
24060 dw_die_ref variant_part_die
24061 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24062
24063 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24064
24065 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24066 &discr_decl, &discr_lists, &discr_lists_length);
24067
24068 if (discr_decl != NULL_TREE)
24069 {
24070 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24071
24072 if (discr_die)
24073 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24074 else
24075 /* We have no DIE for the discriminant, so just discard all
24076 discrimimant information in the output. */
24077 discr_decl = NULL_TREE;
24078 }
24079
24080 /* If the offset for this variant part is more complex than a constant,
24081 create a DWARF procedure for it so that we will not have to generate DWARF
24082 expressions for it for each member. */
24083 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24084 && (dwarf_version >= 3 || !dwarf_strict))
24085 {
24086 const tree dwarf_proc_fndecl
24087 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24088 build_function_type (TREE_TYPE (variant_part_offset),
24089 NULL_TREE));
24090 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24091 const dw_loc_descr_ref dwarf_proc_body
24092 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24093
24094 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24095 dwarf_proc_fndecl, context_die);
24096 if (dwarf_proc_die != NULL)
24097 variant_part_offset = dwarf_proc_call;
24098 }
24099
24100 /* Output DIEs for all variants. */
24101 i = 0;
24102 for (tree variant = TYPE_FIELDS (variant_part_type);
24103 variant != NULL_TREE;
24104 variant = DECL_CHAIN (variant), ++i)
24105 {
24106 tree variant_type = TREE_TYPE (variant);
24107 dw_die_ref variant_die;
24108
24109 /* All variants (i.e. members of a variant part) are supposed to be
24110 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24111 under these records. */
24112 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24113
24114 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24115 equate_decl_number_to_die (variant, variant_die);
24116
24117 /* Output discriminant values this variant matches, if any. */
24118 if (discr_decl == NULL || discr_lists[i] == NULL)
24119 /* In the case we have discriminant information at all, this is
24120 probably the default variant: as the standard says, don't
24121 output any discriminant value/list attribute. */
24122 ;
24123 else if (discr_lists[i]->dw_discr_next == NULL
24124 && !discr_lists[i]->dw_discr_range)
24125 /* If there is only one accepted value, don't bother outputting a
24126 list. */
24127 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24128 else
24129 add_discr_list (variant_die, discr_lists[i]);
24130
24131 for (tree member = TYPE_FIELDS (variant_type);
24132 member != NULL_TREE;
24133 member = DECL_CHAIN (member))
24134 {
24135 struct vlr_context vlr_sub_ctx = {
24136 vlr_ctx->struct_type, /* struct_type */
24137 NULL /* variant_part_offset */
24138 };
24139 if (is_variant_part (member))
24140 {
24141 /* All offsets for fields inside variant parts are relative to
24142 the top-level embedding RECORD_TYPE's base address. On the
24143 other hand, offsets in GCC's types are relative to the
24144 nested-most variant part. So we have to sum offsets each time
24145 we recurse. */
24146
24147 vlr_sub_ctx.variant_part_offset
24148 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24149 variant_part_offset, byte_position (member));
24150 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24151 }
24152 else
24153 {
24154 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24155 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24156 }
24157 }
24158 }
24159
24160 free (discr_lists);
24161 }
24162
24163 /* Generate a DIE for a class member. */
24164
24165 static void
24166 gen_member_die (tree type, dw_die_ref context_die)
24167 {
24168 tree member;
24169 tree binfo = TYPE_BINFO (type);
24170
24171 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24172
24173 /* If this is not an incomplete type, output descriptions of each of its
24174 members. Note that as we output the DIEs necessary to represent the
24175 members of this record or union type, we will also be trying to output
24176 DIEs to represent the *types* of those members. However the `type'
24177 function (above) will specifically avoid generating type DIEs for member
24178 types *within* the list of member DIEs for this (containing) type except
24179 for those types (of members) which are explicitly marked as also being
24180 members of this (containing) type themselves. The g++ front- end can
24181 force any given type to be treated as a member of some other (containing)
24182 type by setting the TYPE_CONTEXT of the given (member) type to point to
24183 the TREE node representing the appropriate (containing) type. */
24184
24185 /* First output info about the base classes. */
24186 if (binfo)
24187 {
24188 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24189 int i;
24190 tree base;
24191
24192 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24193 gen_inheritance_die (base,
24194 (accesses ? (*accesses)[i] : access_public_node),
24195 type,
24196 context_die);
24197 }
24198
24199 /* Now output info about the data members and type members. */
24200 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24201 {
24202 struct vlr_context vlr_ctx = { type, NULL_TREE };
24203 bool static_inline_p
24204 = (TREE_STATIC (member)
24205 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24206 != -1));
24207
24208 /* Ignore clones. */
24209 if (DECL_ABSTRACT_ORIGIN (member))
24210 continue;
24211
24212 /* If we thought we were generating minimal debug info for TYPE
24213 and then changed our minds, some of the member declarations
24214 may have already been defined. Don't define them again, but
24215 do put them in the right order. */
24216
24217 if (dw_die_ref child = lookup_decl_die (member))
24218 {
24219 /* Handle inline static data members, which only have in-class
24220 declarations. */
24221 dw_die_ref ref = NULL;
24222 if (child->die_tag == DW_TAG_variable
24223 && child->die_parent == comp_unit_die ())
24224 {
24225 ref = get_AT_ref (child, DW_AT_specification);
24226 /* For C++17 inline static data members followed by redundant
24227 out of class redeclaration, we might get here with
24228 child being the DIE created for the out of class
24229 redeclaration and with its DW_AT_specification being
24230 the DIE created for in-class definition. We want to
24231 reparent the latter, and don't want to create another
24232 DIE with DW_AT_specification in that case, because
24233 we already have one. */
24234 if (ref
24235 && static_inline_p
24236 && ref->die_tag == DW_TAG_variable
24237 && ref->die_parent == comp_unit_die ()
24238 && get_AT (ref, DW_AT_specification) == NULL)
24239 {
24240 child = ref;
24241 ref = NULL;
24242 static_inline_p = false;
24243 }
24244 }
24245
24246 if (child->die_tag == DW_TAG_variable
24247 && child->die_parent == comp_unit_die ()
24248 && ref == NULL)
24249 {
24250 reparent_child (child, context_die);
24251 if (dwarf_version < 5)
24252 child->die_tag = DW_TAG_member;
24253 }
24254 else
24255 splice_child_die (context_die, child);
24256 }
24257
24258 /* Do not generate standard DWARF for variant parts if we are generating
24259 the corresponding GNAT encodings: DIEs generated for both would
24260 conflict in our mappings. */
24261 else if (is_variant_part (member)
24262 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24263 {
24264 vlr_ctx.variant_part_offset = byte_position (member);
24265 gen_variant_part (member, &vlr_ctx, context_die);
24266 }
24267 else
24268 {
24269 vlr_ctx.variant_part_offset = NULL_TREE;
24270 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24271 }
24272
24273 /* For C++ inline static data members emit immediately a DW_TAG_variable
24274 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24275 DW_AT_specification. */
24276 if (static_inline_p)
24277 {
24278 int old_extern = DECL_EXTERNAL (member);
24279 DECL_EXTERNAL (member) = 0;
24280 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24281 DECL_EXTERNAL (member) = old_extern;
24282 }
24283 }
24284 }
24285
24286 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24287 is set, we pretend that the type was never defined, so we only get the
24288 member DIEs needed by later specification DIEs. */
24289
24290 static void
24291 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24292 enum debug_info_usage usage)
24293 {
24294 if (TREE_ASM_WRITTEN (type))
24295 {
24296 /* Fill in the bound of variable-length fields in late dwarf if
24297 still incomplete. */
24298 if (!early_dwarf && variably_modified_type_p (type, NULL))
24299 for (tree member = TYPE_FIELDS (type);
24300 member;
24301 member = DECL_CHAIN (member))
24302 fill_variable_array_bounds (TREE_TYPE (member));
24303 return;
24304 }
24305
24306 dw_die_ref type_die = lookup_type_die (type);
24307 dw_die_ref scope_die = 0;
24308 int nested = 0;
24309 int complete = (TYPE_SIZE (type)
24310 && (! TYPE_STUB_DECL (type)
24311 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24312 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24313 complete = complete && should_emit_struct_debug (type, usage);
24314
24315 if (type_die && ! complete)
24316 return;
24317
24318 if (TYPE_CONTEXT (type) != NULL_TREE
24319 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24320 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24321 nested = 1;
24322
24323 scope_die = scope_die_for (type, context_die);
24324
24325 /* Generate child dies for template paramaters. */
24326 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24327 schedule_generic_params_dies_gen (type);
24328
24329 if (! type_die || (nested && is_cu_die (scope_die)))
24330 /* First occurrence of type or toplevel definition of nested class. */
24331 {
24332 dw_die_ref old_die = type_die;
24333
24334 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24335 ? record_type_tag (type) : DW_TAG_union_type,
24336 scope_die, type);
24337 equate_type_number_to_die (type, type_die);
24338 if (old_die)
24339 add_AT_specification (type_die, old_die);
24340 else
24341 add_name_attribute (type_die, type_tag (type));
24342 }
24343 else
24344 remove_AT (type_die, DW_AT_declaration);
24345
24346 /* If this type has been completed, then give it a byte_size attribute and
24347 then give a list of members. */
24348 if (complete && !ns_decl)
24349 {
24350 /* Prevent infinite recursion in cases where the type of some member of
24351 this type is expressed in terms of this type itself. */
24352 TREE_ASM_WRITTEN (type) = 1;
24353 add_byte_size_attribute (type_die, type);
24354 add_alignment_attribute (type_die, type);
24355 if (TYPE_STUB_DECL (type) != NULL_TREE)
24356 {
24357 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24358 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24359 }
24360
24361 /* If the first reference to this type was as the return type of an
24362 inline function, then it may not have a parent. Fix this now. */
24363 if (type_die->die_parent == NULL)
24364 add_child_die (scope_die, type_die);
24365
24366 push_decl_scope (type);
24367 gen_member_die (type, type_die);
24368 pop_decl_scope ();
24369
24370 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24371 if (TYPE_ARTIFICIAL (type))
24372 add_AT_flag (type_die, DW_AT_artificial, 1);
24373
24374 /* GNU extension: Record what type our vtable lives in. */
24375 if (TYPE_VFIELD (type))
24376 {
24377 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24378
24379 gen_type_die (vtype, context_die);
24380 add_AT_die_ref (type_die, DW_AT_containing_type,
24381 lookup_type_die (vtype));
24382 }
24383 }
24384 else
24385 {
24386 add_AT_flag (type_die, DW_AT_declaration, 1);
24387
24388 /* We don't need to do this for function-local types. */
24389 if (TYPE_STUB_DECL (type)
24390 && ! decl_function_context (TYPE_STUB_DECL (type)))
24391 vec_safe_push (incomplete_types, type);
24392 }
24393
24394 if (get_AT (type_die, DW_AT_name))
24395 add_pubtype (type, type_die);
24396 }
24397
24398 /* Generate a DIE for a subroutine _type_. */
24399
24400 static void
24401 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24402 {
24403 tree return_type = TREE_TYPE (type);
24404 dw_die_ref subr_die
24405 = new_die (DW_TAG_subroutine_type,
24406 scope_die_for (type, context_die), type);
24407
24408 equate_type_number_to_die (type, subr_die);
24409 add_prototyped_attribute (subr_die, type);
24410 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24411 context_die);
24412 add_alignment_attribute (subr_die, type);
24413 gen_formal_types_die (type, subr_die);
24414
24415 if (get_AT (subr_die, DW_AT_name))
24416 add_pubtype (type, subr_die);
24417 if ((dwarf_version >= 5 || !dwarf_strict)
24418 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24419 add_AT_flag (subr_die, DW_AT_reference, 1);
24420 if ((dwarf_version >= 5 || !dwarf_strict)
24421 && lang_hooks.types.type_dwarf_attribute (type,
24422 DW_AT_rvalue_reference) != -1)
24423 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24424 }
24425
24426 /* Generate a DIE for a type definition. */
24427
24428 static void
24429 gen_typedef_die (tree decl, dw_die_ref context_die)
24430 {
24431 dw_die_ref type_die;
24432 tree type;
24433
24434 if (TREE_ASM_WRITTEN (decl))
24435 {
24436 if (DECL_ORIGINAL_TYPE (decl))
24437 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24438 return;
24439 }
24440
24441 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24442 checks in process_scope_var and modified_type_die), this should be called
24443 only for original types. */
24444 gcc_assert (decl_ultimate_origin (decl) == NULL
24445 || decl_ultimate_origin (decl) == decl);
24446
24447 TREE_ASM_WRITTEN (decl) = 1;
24448 type_die = new_die (DW_TAG_typedef, context_die, decl);
24449
24450 add_name_and_src_coords_attributes (type_die, decl);
24451 if (DECL_ORIGINAL_TYPE (decl))
24452 {
24453 type = DECL_ORIGINAL_TYPE (decl);
24454 if (type == error_mark_node)
24455 return;
24456
24457 gcc_assert (type != TREE_TYPE (decl));
24458 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24459 }
24460 else
24461 {
24462 type = TREE_TYPE (decl);
24463 if (type == error_mark_node)
24464 return;
24465
24466 if (is_naming_typedef_decl (TYPE_NAME (type)))
24467 {
24468 /* Here, we are in the case of decl being a typedef naming
24469 an anonymous type, e.g:
24470 typedef struct {...} foo;
24471 In that case TREE_TYPE (decl) is not a typedef variant
24472 type and TYPE_NAME of the anonymous type is set to the
24473 TYPE_DECL of the typedef. This construct is emitted by
24474 the C++ FE.
24475
24476 TYPE is the anonymous struct named by the typedef
24477 DECL. As we need the DW_AT_type attribute of the
24478 DW_TAG_typedef to point to the DIE of TYPE, let's
24479 generate that DIE right away. add_type_attribute
24480 called below will then pick (via lookup_type_die) that
24481 anonymous struct DIE. */
24482 if (!TREE_ASM_WRITTEN (type))
24483 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24484
24485 /* This is a GNU Extension. We are adding a
24486 DW_AT_linkage_name attribute to the DIE of the
24487 anonymous struct TYPE. The value of that attribute
24488 is the name of the typedef decl naming the anonymous
24489 struct. This greatly eases the work of consumers of
24490 this debug info. */
24491 add_linkage_name_raw (lookup_type_die (type), decl);
24492 }
24493 }
24494
24495 add_type_attribute (type_die, type, decl_quals (decl), false,
24496 context_die);
24497
24498 if (is_naming_typedef_decl (decl))
24499 /* We want that all subsequent calls to lookup_type_die with
24500 TYPE in argument yield the DW_TAG_typedef we have just
24501 created. */
24502 equate_type_number_to_die (type, type_die);
24503
24504 add_alignment_attribute (type_die, TREE_TYPE (decl));
24505
24506 add_accessibility_attribute (type_die, decl);
24507
24508 if (DECL_ABSTRACT_P (decl))
24509 equate_decl_number_to_die (decl, type_die);
24510
24511 if (get_AT (type_die, DW_AT_name))
24512 add_pubtype (decl, type_die);
24513 }
24514
24515 /* Generate a DIE for a struct, class, enum or union type. */
24516
24517 static void
24518 gen_tagged_type_die (tree type,
24519 dw_die_ref context_die,
24520 enum debug_info_usage usage)
24521 {
24522 int need_pop;
24523
24524 if (type == NULL_TREE
24525 || !is_tagged_type (type))
24526 return;
24527
24528 if (TREE_ASM_WRITTEN (type))
24529 need_pop = 0;
24530 /* If this is a nested type whose containing class hasn't been written
24531 out yet, writing it out will cover this one, too. This does not apply
24532 to instantiations of member class templates; they need to be added to
24533 the containing class as they are generated. FIXME: This hurts the
24534 idea of combining type decls from multiple TUs, since we can't predict
24535 what set of template instantiations we'll get. */
24536 else if (TYPE_CONTEXT (type)
24537 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24538 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24539 {
24540 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24541
24542 if (TREE_ASM_WRITTEN (type))
24543 return;
24544
24545 /* If that failed, attach ourselves to the stub. */
24546 push_decl_scope (TYPE_CONTEXT (type));
24547 context_die = lookup_type_die (TYPE_CONTEXT (type));
24548 need_pop = 1;
24549 }
24550 else if (TYPE_CONTEXT (type) != NULL_TREE
24551 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24552 {
24553 /* If this type is local to a function that hasn't been written
24554 out yet, use a NULL context for now; it will be fixed up in
24555 decls_for_scope. */
24556 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24557 /* A declaration DIE doesn't count; nested types need to go in the
24558 specification. */
24559 if (context_die && is_declaration_die (context_die))
24560 context_die = NULL;
24561 need_pop = 0;
24562 }
24563 else
24564 {
24565 context_die = declare_in_namespace (type, context_die);
24566 need_pop = 0;
24567 }
24568
24569 if (TREE_CODE (type) == ENUMERAL_TYPE)
24570 {
24571 /* This might have been written out by the call to
24572 declare_in_namespace. */
24573 if (!TREE_ASM_WRITTEN (type))
24574 gen_enumeration_type_die (type, context_die);
24575 }
24576 else
24577 gen_struct_or_union_type_die (type, context_die, usage);
24578
24579 if (need_pop)
24580 pop_decl_scope ();
24581
24582 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24583 it up if it is ever completed. gen_*_type_die will set it for us
24584 when appropriate. */
24585 }
24586
24587 /* Generate a type description DIE. */
24588
24589 static void
24590 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24591 enum debug_info_usage usage)
24592 {
24593 struct array_descr_info info;
24594
24595 if (type == NULL_TREE || type == error_mark_node)
24596 return;
24597
24598 if (flag_checking && type)
24599 verify_type (type);
24600
24601 if (TYPE_NAME (type) != NULL_TREE
24602 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24603 && is_redundant_typedef (TYPE_NAME (type))
24604 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24605 /* The DECL of this type is a typedef we don't want to emit debug
24606 info for but we want debug info for its underlying typedef.
24607 This can happen for e.g, the injected-class-name of a C++
24608 type. */
24609 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24610
24611 /* If TYPE is a typedef type variant, let's generate debug info
24612 for the parent typedef which TYPE is a type of. */
24613 if (typedef_variant_p (type))
24614 {
24615 if (TREE_ASM_WRITTEN (type))
24616 return;
24617
24618 tree name = TYPE_NAME (type);
24619 tree origin = decl_ultimate_origin (name);
24620 if (origin != NULL && origin != name)
24621 {
24622 gen_decl_die (origin, NULL, NULL, context_die);
24623 return;
24624 }
24625
24626 /* Prevent broken recursion; we can't hand off to the same type. */
24627 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24628
24629 /* Give typedefs the right scope. */
24630 context_die = scope_die_for (type, context_die);
24631
24632 TREE_ASM_WRITTEN (type) = 1;
24633
24634 gen_decl_die (name, NULL, NULL, context_die);
24635 return;
24636 }
24637
24638 /* If type is an anonymous tagged type named by a typedef, let's
24639 generate debug info for the typedef. */
24640 if (is_naming_typedef_decl (TYPE_NAME (type)))
24641 {
24642 /* Use the DIE of the containing namespace as the parent DIE of
24643 the type description DIE we want to generate. */
24644 if (DECL_CONTEXT (TYPE_NAME (type))
24645 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24646 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24647
24648 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24649 return;
24650 }
24651
24652 if (lang_hooks.types.get_debug_type)
24653 {
24654 tree debug_type = lang_hooks.types.get_debug_type (type);
24655
24656 if (debug_type != NULL_TREE && debug_type != type)
24657 {
24658 gen_type_die_with_usage (debug_type, context_die, usage);
24659 return;
24660 }
24661 }
24662
24663 /* We are going to output a DIE to represent the unqualified version
24664 of this type (i.e. without any const or volatile qualifiers) so
24665 get the main variant (i.e. the unqualified version) of this type
24666 now. (Vectors and arrays are special because the debugging info is in the
24667 cloned type itself. Similarly function/method types can contain extra
24668 ref-qualification). */
24669 if (TREE_CODE (type) == FUNCTION_TYPE
24670 || TREE_CODE (type) == METHOD_TYPE)
24671 {
24672 /* For function/method types, can't use type_main_variant here,
24673 because that can have different ref-qualifiers for C++,
24674 but try to canonicalize. */
24675 tree main = TYPE_MAIN_VARIANT (type);
24676 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24677 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24678 && check_base_type (t, main)
24679 && check_lang_type (t, type))
24680 {
24681 type = t;
24682 break;
24683 }
24684 }
24685 else if (TREE_CODE (type) != VECTOR_TYPE
24686 && TREE_CODE (type) != ARRAY_TYPE)
24687 type = type_main_variant (type);
24688
24689 /* If this is an array type with hidden descriptor, handle it first. */
24690 if (!TREE_ASM_WRITTEN (type)
24691 && lang_hooks.types.get_array_descr_info)
24692 {
24693 memset (&info, 0, sizeof (info));
24694 if (lang_hooks.types.get_array_descr_info (type, &info))
24695 {
24696 /* Fortran sometimes emits array types with no dimension. */
24697 gcc_assert (info.ndimensions >= 0
24698 && (info.ndimensions
24699 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24700 gen_descr_array_type_die (type, &info, context_die);
24701 TREE_ASM_WRITTEN (type) = 1;
24702 return;
24703 }
24704 }
24705
24706 if (TREE_ASM_WRITTEN (type))
24707 {
24708 /* Variable-length types may be incomplete even if
24709 TREE_ASM_WRITTEN. For such types, fall through to
24710 gen_array_type_die() and possibly fill in
24711 DW_AT_{upper,lower}_bound attributes. */
24712 if ((TREE_CODE (type) != ARRAY_TYPE
24713 && TREE_CODE (type) != RECORD_TYPE
24714 && TREE_CODE (type) != UNION_TYPE
24715 && TREE_CODE (type) != QUAL_UNION_TYPE)
24716 || !variably_modified_type_p (type, NULL))
24717 return;
24718 }
24719
24720 switch (TREE_CODE (type))
24721 {
24722 case ERROR_MARK:
24723 break;
24724
24725 case POINTER_TYPE:
24726 case REFERENCE_TYPE:
24727 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24728 ensures that the gen_type_die recursion will terminate even if the
24729 type is recursive. Recursive types are possible in Ada. */
24730 /* ??? We could perhaps do this for all types before the switch
24731 statement. */
24732 TREE_ASM_WRITTEN (type) = 1;
24733
24734 /* For these types, all that is required is that we output a DIE (or a
24735 set of DIEs) to represent the "basis" type. */
24736 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24737 DINFO_USAGE_IND_USE);
24738 break;
24739
24740 case OFFSET_TYPE:
24741 /* This code is used for C++ pointer-to-data-member types.
24742 Output a description of the relevant class type. */
24743 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24744 DINFO_USAGE_IND_USE);
24745
24746 /* Output a description of the type of the object pointed to. */
24747 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24748 DINFO_USAGE_IND_USE);
24749
24750 /* Now output a DIE to represent this pointer-to-data-member type
24751 itself. */
24752 gen_ptr_to_mbr_type_die (type, context_die);
24753 break;
24754
24755 case FUNCTION_TYPE:
24756 /* Force out return type (in case it wasn't forced out already). */
24757 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24758 DINFO_USAGE_DIR_USE);
24759 gen_subroutine_type_die (type, context_die);
24760 break;
24761
24762 case METHOD_TYPE:
24763 /* Force out return type (in case it wasn't forced out already). */
24764 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24765 DINFO_USAGE_DIR_USE);
24766 gen_subroutine_type_die (type, context_die);
24767 break;
24768
24769 case ARRAY_TYPE:
24770 case VECTOR_TYPE:
24771 gen_array_type_die (type, context_die);
24772 break;
24773
24774 case ENUMERAL_TYPE:
24775 case RECORD_TYPE:
24776 case UNION_TYPE:
24777 case QUAL_UNION_TYPE:
24778 gen_tagged_type_die (type, context_die, usage);
24779 return;
24780
24781 case VOID_TYPE:
24782 case INTEGER_TYPE:
24783 case REAL_TYPE:
24784 case FIXED_POINT_TYPE:
24785 case COMPLEX_TYPE:
24786 case BOOLEAN_TYPE:
24787 case POINTER_BOUNDS_TYPE:
24788 /* No DIEs needed for fundamental types. */
24789 break;
24790
24791 case NULLPTR_TYPE:
24792 case LANG_TYPE:
24793 /* Just use DW_TAG_unspecified_type. */
24794 {
24795 dw_die_ref type_die = lookup_type_die (type);
24796 if (type_die == NULL)
24797 {
24798 tree name = TYPE_IDENTIFIER (type);
24799 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24800 type);
24801 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24802 equate_type_number_to_die (type, type_die);
24803 }
24804 }
24805 break;
24806
24807 default:
24808 if (is_cxx_auto (type))
24809 {
24810 tree name = TYPE_IDENTIFIER (type);
24811 dw_die_ref *die = (name == get_identifier ("auto")
24812 ? &auto_die : &decltype_auto_die);
24813 if (!*die)
24814 {
24815 *die = new_die (DW_TAG_unspecified_type,
24816 comp_unit_die (), NULL_TREE);
24817 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24818 }
24819 equate_type_number_to_die (type, *die);
24820 break;
24821 }
24822 gcc_unreachable ();
24823 }
24824
24825 TREE_ASM_WRITTEN (type) = 1;
24826 }
24827
24828 static void
24829 gen_type_die (tree type, dw_die_ref context_die)
24830 {
24831 if (type != error_mark_node)
24832 {
24833 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24834 if (flag_checking)
24835 {
24836 dw_die_ref die = lookup_type_die (type);
24837 if (die)
24838 check_die (die);
24839 }
24840 }
24841 }
24842
24843 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24844 things which are local to the given block. */
24845
24846 static void
24847 gen_block_die (tree stmt, dw_die_ref context_die)
24848 {
24849 int must_output_die = 0;
24850 bool inlined_func;
24851
24852 /* Ignore blocks that are NULL. */
24853 if (stmt == NULL_TREE)
24854 return;
24855
24856 inlined_func = inlined_function_outer_scope_p (stmt);
24857
24858 /* If the block is one fragment of a non-contiguous block, do not
24859 process the variables, since they will have been done by the
24860 origin block. Do process subblocks. */
24861 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24862 {
24863 tree sub;
24864
24865 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24866 gen_block_die (sub, context_die);
24867
24868 return;
24869 }
24870
24871 /* Determine if we need to output any Dwarf DIEs at all to represent this
24872 block. */
24873 if (inlined_func)
24874 /* The outer scopes for inlinings *must* always be represented. We
24875 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24876 must_output_die = 1;
24877 else
24878 {
24879 /* Determine if this block directly contains any "significant"
24880 local declarations which we will need to output DIEs for. */
24881 if (debug_info_level > DINFO_LEVEL_TERSE)
24882 /* We are not in terse mode so *any* local declaration counts
24883 as being a "significant" one. */
24884 must_output_die = ((BLOCK_VARS (stmt) != NULL
24885 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24886 && (TREE_USED (stmt)
24887 || TREE_ASM_WRITTEN (stmt)
24888 || BLOCK_ABSTRACT (stmt)));
24889 else if ((TREE_USED (stmt)
24890 || TREE_ASM_WRITTEN (stmt)
24891 || BLOCK_ABSTRACT (stmt))
24892 && !dwarf2out_ignore_block (stmt))
24893 must_output_die = 1;
24894 }
24895
24896 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24897 DIE for any block which contains no significant local declarations at
24898 all. Rather, in such cases we just call `decls_for_scope' so that any
24899 needed Dwarf info for any sub-blocks will get properly generated. Note
24900 that in terse mode, our definition of what constitutes a "significant"
24901 local declaration gets restricted to include only inlined function
24902 instances and local (nested) function definitions. */
24903 if (must_output_die)
24904 {
24905 if (inlined_func)
24906 {
24907 /* If STMT block is abstract, that means we have been called
24908 indirectly from dwarf2out_abstract_function.
24909 That function rightfully marks the descendent blocks (of
24910 the abstract function it is dealing with) as being abstract,
24911 precisely to prevent us from emitting any
24912 DW_TAG_inlined_subroutine DIE as a descendent
24913 of an abstract function instance. So in that case, we should
24914 not call gen_inlined_subroutine_die.
24915
24916 Later though, when cgraph asks dwarf2out to emit info
24917 for the concrete instance of the function decl into which
24918 the concrete instance of STMT got inlined, the later will lead
24919 to the generation of a DW_TAG_inlined_subroutine DIE. */
24920 if (! BLOCK_ABSTRACT (stmt))
24921 gen_inlined_subroutine_die (stmt, context_die);
24922 }
24923 else
24924 gen_lexical_block_die (stmt, context_die);
24925 }
24926 else
24927 decls_for_scope (stmt, context_die);
24928 }
24929
24930 /* Process variable DECL (or variable with origin ORIGIN) within
24931 block STMT and add it to CONTEXT_DIE. */
24932 static void
24933 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24934 {
24935 dw_die_ref die;
24936 tree decl_or_origin = decl ? decl : origin;
24937
24938 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24939 die = lookup_decl_die (decl_or_origin);
24940 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24941 {
24942 if (TYPE_DECL_IS_STUB (decl_or_origin))
24943 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24944 else
24945 die = lookup_decl_die (decl_or_origin);
24946 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24947 if (! die && ! early_dwarf)
24948 return;
24949 }
24950 else
24951 die = NULL;
24952
24953 /* Avoid creating DIEs for local typedefs and concrete static variables that
24954 will only be pruned later. */
24955 if ((origin || decl_ultimate_origin (decl))
24956 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24957 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24958 {
24959 origin = decl_ultimate_origin (decl_or_origin);
24960 if (decl && VAR_P (decl) && die != NULL)
24961 {
24962 die = lookup_decl_die (origin);
24963 if (die != NULL)
24964 equate_decl_number_to_die (decl, die);
24965 }
24966 return;
24967 }
24968
24969 if (die != NULL && die->die_parent == NULL)
24970 add_child_die (context_die, die);
24971 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24972 {
24973 if (early_dwarf)
24974 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24975 stmt, context_die);
24976 }
24977 else
24978 {
24979 if (decl && DECL_P (decl))
24980 {
24981 die = lookup_decl_die (decl);
24982
24983 /* Early created DIEs do not have a parent as the decls refer
24984 to the function as DECL_CONTEXT rather than the BLOCK. */
24985 if (die && die->die_parent == NULL)
24986 {
24987 gcc_assert (in_lto_p);
24988 add_child_die (context_die, die);
24989 }
24990 }
24991
24992 gen_decl_die (decl, origin, NULL, context_die);
24993 }
24994 }
24995
24996 /* Generate all of the decls declared within a given scope and (recursively)
24997 all of its sub-blocks. */
24998
24999 static void
25000 decls_for_scope (tree stmt, dw_die_ref context_die)
25001 {
25002 tree decl;
25003 unsigned int i;
25004 tree subblocks;
25005
25006 /* Ignore NULL blocks. */
25007 if (stmt == NULL_TREE)
25008 return;
25009
25010 /* Output the DIEs to represent all of the data objects and typedefs
25011 declared directly within this block but not within any nested
25012 sub-blocks. Also, nested function and tag DIEs have been
25013 generated with a parent of NULL; fix that up now. We don't
25014 have to do this if we're at -g1. */
25015 if (debug_info_level > DINFO_LEVEL_TERSE)
25016 {
25017 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25018 process_scope_var (stmt, decl, NULL_TREE, context_die);
25019 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25020 origin - avoid doing this twice as we have no good way to see
25021 if we've done it once already. */
25022 if (! early_dwarf)
25023 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25024 {
25025 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25026 if (decl == current_function_decl)
25027 /* Ignore declarations of the current function, while they
25028 are declarations, gen_subprogram_die would treat them
25029 as definitions again, because they are equal to
25030 current_function_decl and endlessly recurse. */;
25031 else if (TREE_CODE (decl) == FUNCTION_DECL)
25032 process_scope_var (stmt, decl, NULL_TREE, context_die);
25033 else
25034 process_scope_var (stmt, NULL_TREE, decl, context_die);
25035 }
25036 }
25037
25038 /* Even if we're at -g1, we need to process the subblocks in order to get
25039 inlined call information. */
25040
25041 /* Output the DIEs to represent all sub-blocks (and the items declared
25042 therein) of this block. */
25043 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25044 subblocks != NULL;
25045 subblocks = BLOCK_CHAIN (subblocks))
25046 gen_block_die (subblocks, context_die);
25047 }
25048
25049 /* Is this a typedef we can avoid emitting? */
25050
25051 bool
25052 is_redundant_typedef (const_tree decl)
25053 {
25054 if (TYPE_DECL_IS_STUB (decl))
25055 return true;
25056
25057 if (DECL_ARTIFICIAL (decl)
25058 && DECL_CONTEXT (decl)
25059 && is_tagged_type (DECL_CONTEXT (decl))
25060 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25061 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25062 /* Also ignore the artificial member typedef for the class name. */
25063 return true;
25064
25065 return false;
25066 }
25067
25068 /* Return TRUE if TYPE is a typedef that names a type for linkage
25069 purposes. This kind of typedefs is produced by the C++ FE for
25070 constructs like:
25071
25072 typedef struct {...} foo;
25073
25074 In that case, there is no typedef variant type produced for foo.
25075 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25076 struct type. */
25077
25078 static bool
25079 is_naming_typedef_decl (const_tree decl)
25080 {
25081 if (decl == NULL_TREE
25082 || TREE_CODE (decl) != TYPE_DECL
25083 || DECL_NAMELESS (decl)
25084 || !is_tagged_type (TREE_TYPE (decl))
25085 || DECL_IS_BUILTIN (decl)
25086 || is_redundant_typedef (decl)
25087 /* It looks like Ada produces TYPE_DECLs that are very similar
25088 to C++ naming typedefs but that have different
25089 semantics. Let's be specific to c++ for now. */
25090 || !is_cxx (decl))
25091 return FALSE;
25092
25093 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25094 && TYPE_NAME (TREE_TYPE (decl)) == decl
25095 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25096 != TYPE_NAME (TREE_TYPE (decl))));
25097 }
25098
25099 /* Looks up the DIE for a context. */
25100
25101 static inline dw_die_ref
25102 lookup_context_die (tree context)
25103 {
25104 if (context)
25105 {
25106 /* Find die that represents this context. */
25107 if (TYPE_P (context))
25108 {
25109 context = TYPE_MAIN_VARIANT (context);
25110 dw_die_ref ctx = lookup_type_die (context);
25111 if (!ctx)
25112 return NULL;
25113 return strip_naming_typedef (context, ctx);
25114 }
25115 else
25116 return lookup_decl_die (context);
25117 }
25118 return comp_unit_die ();
25119 }
25120
25121 /* Returns the DIE for a context. */
25122
25123 static inline dw_die_ref
25124 get_context_die (tree context)
25125 {
25126 if (context)
25127 {
25128 /* Find die that represents this context. */
25129 if (TYPE_P (context))
25130 {
25131 context = TYPE_MAIN_VARIANT (context);
25132 return strip_naming_typedef (context, force_type_die (context));
25133 }
25134 else
25135 return force_decl_die (context);
25136 }
25137 return comp_unit_die ();
25138 }
25139
25140 /* Returns the DIE for decl. A DIE will always be returned. */
25141
25142 static dw_die_ref
25143 force_decl_die (tree decl)
25144 {
25145 dw_die_ref decl_die;
25146 unsigned saved_external_flag;
25147 tree save_fn = NULL_TREE;
25148 decl_die = lookup_decl_die (decl);
25149 if (!decl_die)
25150 {
25151 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25152
25153 decl_die = lookup_decl_die (decl);
25154 if (decl_die)
25155 return decl_die;
25156
25157 switch (TREE_CODE (decl))
25158 {
25159 case FUNCTION_DECL:
25160 /* Clear current_function_decl, so that gen_subprogram_die thinks
25161 that this is a declaration. At this point, we just want to force
25162 declaration die. */
25163 save_fn = current_function_decl;
25164 current_function_decl = NULL_TREE;
25165 gen_subprogram_die (decl, context_die);
25166 current_function_decl = save_fn;
25167 break;
25168
25169 case VAR_DECL:
25170 /* Set external flag to force declaration die. Restore it after
25171 gen_decl_die() call. */
25172 saved_external_flag = DECL_EXTERNAL (decl);
25173 DECL_EXTERNAL (decl) = 1;
25174 gen_decl_die (decl, NULL, NULL, context_die);
25175 DECL_EXTERNAL (decl) = saved_external_flag;
25176 break;
25177
25178 case NAMESPACE_DECL:
25179 if (dwarf_version >= 3 || !dwarf_strict)
25180 dwarf2out_decl (decl);
25181 else
25182 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25183 decl_die = comp_unit_die ();
25184 break;
25185
25186 case TRANSLATION_UNIT_DECL:
25187 decl_die = comp_unit_die ();
25188 break;
25189
25190 default:
25191 gcc_unreachable ();
25192 }
25193
25194 /* We should be able to find the DIE now. */
25195 if (!decl_die)
25196 decl_die = lookup_decl_die (decl);
25197 gcc_assert (decl_die);
25198 }
25199
25200 return decl_die;
25201 }
25202
25203 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25204 always returned. */
25205
25206 static dw_die_ref
25207 force_type_die (tree type)
25208 {
25209 dw_die_ref type_die;
25210
25211 type_die = lookup_type_die (type);
25212 if (!type_die)
25213 {
25214 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25215
25216 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25217 false, context_die);
25218 gcc_assert (type_die);
25219 }
25220 return type_die;
25221 }
25222
25223 /* Force out any required namespaces to be able to output DECL,
25224 and return the new context_die for it, if it's changed. */
25225
25226 static dw_die_ref
25227 setup_namespace_context (tree thing, dw_die_ref context_die)
25228 {
25229 tree context = (DECL_P (thing)
25230 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25231 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25232 /* Force out the namespace. */
25233 context_die = force_decl_die (context);
25234
25235 return context_die;
25236 }
25237
25238 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25239 type) within its namespace, if appropriate.
25240
25241 For compatibility with older debuggers, namespace DIEs only contain
25242 declarations; all definitions are emitted at CU scope, with
25243 DW_AT_specification pointing to the declaration (like with class
25244 members). */
25245
25246 static dw_die_ref
25247 declare_in_namespace (tree thing, dw_die_ref context_die)
25248 {
25249 dw_die_ref ns_context;
25250
25251 if (debug_info_level <= DINFO_LEVEL_TERSE)
25252 return context_die;
25253
25254 /* External declarations in the local scope only need to be emitted
25255 once, not once in the namespace and once in the scope.
25256
25257 This avoids declaring the `extern' below in the
25258 namespace DIE as well as in the innermost scope:
25259
25260 namespace S
25261 {
25262 int i=5;
25263 int foo()
25264 {
25265 int i=8;
25266 extern int i;
25267 return i;
25268 }
25269 }
25270 */
25271 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25272 return context_die;
25273
25274 /* If this decl is from an inlined function, then don't try to emit it in its
25275 namespace, as we will get confused. It would have already been emitted
25276 when the abstract instance of the inline function was emitted anyways. */
25277 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25278 return context_die;
25279
25280 ns_context = setup_namespace_context (thing, context_die);
25281
25282 if (ns_context != context_die)
25283 {
25284 if (is_fortran ())
25285 return ns_context;
25286 if (DECL_P (thing))
25287 gen_decl_die (thing, NULL, NULL, ns_context);
25288 else
25289 gen_type_die (thing, ns_context);
25290 }
25291 return context_die;
25292 }
25293
25294 /* Generate a DIE for a namespace or namespace alias. */
25295
25296 static void
25297 gen_namespace_die (tree decl, dw_die_ref context_die)
25298 {
25299 dw_die_ref namespace_die;
25300
25301 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25302 they are an alias of. */
25303 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25304 {
25305 /* Output a real namespace or module. */
25306 context_die = setup_namespace_context (decl, comp_unit_die ());
25307 namespace_die = new_die (is_fortran ()
25308 ? DW_TAG_module : DW_TAG_namespace,
25309 context_die, decl);
25310 /* For Fortran modules defined in different CU don't add src coords. */
25311 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25312 {
25313 const char *name = dwarf2_name (decl, 0);
25314 if (name)
25315 add_name_attribute (namespace_die, name);
25316 }
25317 else
25318 add_name_and_src_coords_attributes (namespace_die, decl);
25319 if (DECL_EXTERNAL (decl))
25320 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25321 equate_decl_number_to_die (decl, namespace_die);
25322 }
25323 else
25324 {
25325 /* Output a namespace alias. */
25326
25327 /* Force out the namespace we are an alias of, if necessary. */
25328 dw_die_ref origin_die
25329 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25330
25331 if (DECL_FILE_SCOPE_P (decl)
25332 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25333 context_die = setup_namespace_context (decl, comp_unit_die ());
25334 /* Now create the namespace alias DIE. */
25335 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25336 add_name_and_src_coords_attributes (namespace_die, decl);
25337 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25338 equate_decl_number_to_die (decl, namespace_die);
25339 }
25340 if ((dwarf_version >= 5 || !dwarf_strict)
25341 && lang_hooks.decls.decl_dwarf_attribute (decl,
25342 DW_AT_export_symbols) == 1)
25343 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25344
25345 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25346 if (want_pubnames ())
25347 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25348 }
25349
25350 /* Generate Dwarf debug information for a decl described by DECL.
25351 The return value is currently only meaningful for PARM_DECLs,
25352 for all other decls it returns NULL.
25353
25354 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25355 It can be NULL otherwise. */
25356
25357 static dw_die_ref
25358 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25359 dw_die_ref context_die)
25360 {
25361 tree decl_or_origin = decl ? decl : origin;
25362 tree class_origin = NULL, ultimate_origin;
25363
25364 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25365 return NULL;
25366
25367 /* Ignore pointer bounds decls. */
25368 if (DECL_P (decl_or_origin)
25369 && TREE_TYPE (decl_or_origin)
25370 && POINTER_BOUNDS_P (decl_or_origin))
25371 return NULL;
25372
25373 switch (TREE_CODE (decl_or_origin))
25374 {
25375 case ERROR_MARK:
25376 break;
25377
25378 case CONST_DECL:
25379 if (!is_fortran () && !is_ada ())
25380 {
25381 /* The individual enumerators of an enum type get output when we output
25382 the Dwarf representation of the relevant enum type itself. */
25383 break;
25384 }
25385
25386 /* Emit its type. */
25387 gen_type_die (TREE_TYPE (decl), context_die);
25388
25389 /* And its containing namespace. */
25390 context_die = declare_in_namespace (decl, context_die);
25391
25392 gen_const_die (decl, context_die);
25393 break;
25394
25395 case FUNCTION_DECL:
25396 #if 0
25397 /* FIXME */
25398 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25399 on local redeclarations of global functions. That seems broken. */
25400 if (current_function_decl != decl)
25401 /* This is only a declaration. */;
25402 #endif
25403
25404 /* We should have abstract copies already and should not generate
25405 stray type DIEs in late LTO dumping. */
25406 if (! early_dwarf)
25407 ;
25408
25409 /* If we're emitting a clone, emit info for the abstract instance. */
25410 else if (origin || DECL_ORIGIN (decl) != decl)
25411 dwarf2out_abstract_function (origin
25412 ? DECL_ORIGIN (origin)
25413 : DECL_ABSTRACT_ORIGIN (decl));
25414
25415 /* If we're emitting a possibly inlined function emit it as
25416 abstract instance. */
25417 else if (cgraph_function_possibly_inlined_p (decl)
25418 && ! DECL_ABSTRACT_P (decl)
25419 && ! class_or_namespace_scope_p (context_die)
25420 /* dwarf2out_abstract_function won't emit a die if this is just
25421 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25422 that case, because that works only if we have a die. */
25423 && DECL_INITIAL (decl) != NULL_TREE)
25424 dwarf2out_abstract_function (decl);
25425
25426 /* Otherwise we're emitting the primary DIE for this decl. */
25427 else if (debug_info_level > DINFO_LEVEL_TERSE)
25428 {
25429 /* Before we describe the FUNCTION_DECL itself, make sure that we
25430 have its containing type. */
25431 if (!origin)
25432 origin = decl_class_context (decl);
25433 if (origin != NULL_TREE)
25434 gen_type_die (origin, context_die);
25435
25436 /* And its return type. */
25437 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25438
25439 /* And its virtual context. */
25440 if (DECL_VINDEX (decl) != NULL_TREE)
25441 gen_type_die (DECL_CONTEXT (decl), context_die);
25442
25443 /* Make sure we have a member DIE for decl. */
25444 if (origin != NULL_TREE)
25445 gen_type_die_for_member (origin, decl, context_die);
25446
25447 /* And its containing namespace. */
25448 context_die = declare_in_namespace (decl, context_die);
25449 }
25450
25451 /* Now output a DIE to represent the function itself. */
25452 if (decl)
25453 gen_subprogram_die (decl, context_die);
25454 break;
25455
25456 case TYPE_DECL:
25457 /* If we are in terse mode, don't generate any DIEs to represent any
25458 actual typedefs. */
25459 if (debug_info_level <= DINFO_LEVEL_TERSE)
25460 break;
25461
25462 /* In the special case of a TYPE_DECL node representing the declaration
25463 of some type tag, if the given TYPE_DECL is marked as having been
25464 instantiated from some other (original) TYPE_DECL node (e.g. one which
25465 was generated within the original definition of an inline function) we
25466 used to generate a special (abbreviated) DW_TAG_structure_type,
25467 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25468 should be actually referencing those DIEs, as variable DIEs with that
25469 type would be emitted already in the abstract origin, so it was always
25470 removed during unused type prunning. Don't add anything in this
25471 case. */
25472 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25473 break;
25474
25475 if (is_redundant_typedef (decl))
25476 gen_type_die (TREE_TYPE (decl), context_die);
25477 else
25478 /* Output a DIE to represent the typedef itself. */
25479 gen_typedef_die (decl, context_die);
25480 break;
25481
25482 case LABEL_DECL:
25483 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25484 gen_label_die (decl, context_die);
25485 break;
25486
25487 case VAR_DECL:
25488 case RESULT_DECL:
25489 /* If we are in terse mode, don't generate any DIEs to represent any
25490 variable declarations or definitions. */
25491 if (debug_info_level <= DINFO_LEVEL_TERSE)
25492 break;
25493
25494 /* Avoid generating stray type DIEs during late dwarf dumping.
25495 All types have been dumped early. */
25496 if (early_dwarf
25497 /* ??? But in LTRANS we cannot annotate early created variably
25498 modified type DIEs without copying them and adjusting all
25499 references to them. Dump them again as happens for inlining
25500 which copies both the decl and the types. */
25501 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25502 in VLA bound information for example. */
25503 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25504 current_function_decl)))
25505 {
25506 /* Output any DIEs that are needed to specify the type of this data
25507 object. */
25508 if (decl_by_reference_p (decl_or_origin))
25509 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25510 else
25511 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25512 }
25513
25514 if (early_dwarf)
25515 {
25516 /* And its containing type. */
25517 class_origin = decl_class_context (decl_or_origin);
25518 if (class_origin != NULL_TREE)
25519 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25520
25521 /* And its containing namespace. */
25522 context_die = declare_in_namespace (decl_or_origin, context_die);
25523 }
25524
25525 /* Now output the DIE to represent the data object itself. This gets
25526 complicated because of the possibility that the VAR_DECL really
25527 represents an inlined instance of a formal parameter for an inline
25528 function. */
25529 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25530 if (ultimate_origin != NULL_TREE
25531 && TREE_CODE (ultimate_origin) == PARM_DECL)
25532 gen_formal_parameter_die (decl, origin,
25533 true /* Emit name attribute. */,
25534 context_die);
25535 else
25536 gen_variable_die (decl, origin, context_die);
25537 break;
25538
25539 case FIELD_DECL:
25540 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25541 /* Ignore the nameless fields that are used to skip bits but handle C++
25542 anonymous unions and structs. */
25543 if (DECL_NAME (decl) != NULL_TREE
25544 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25545 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25546 {
25547 gen_type_die (member_declared_type (decl), context_die);
25548 gen_field_die (decl, ctx, context_die);
25549 }
25550 break;
25551
25552 case PARM_DECL:
25553 /* Avoid generating stray type DIEs during late dwarf dumping.
25554 All types have been dumped early. */
25555 if (early_dwarf
25556 /* ??? But in LTRANS we cannot annotate early created variably
25557 modified type DIEs without copying them and adjusting all
25558 references to them. Dump them again as happens for inlining
25559 which copies both the decl and the types. */
25560 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25561 in VLA bound information for example. */
25562 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25563 current_function_decl)))
25564 {
25565 if (DECL_BY_REFERENCE (decl_or_origin))
25566 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25567 else
25568 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25569 }
25570 return gen_formal_parameter_die (decl, origin,
25571 true /* Emit name attribute. */,
25572 context_die);
25573
25574 case NAMESPACE_DECL:
25575 if (dwarf_version >= 3 || !dwarf_strict)
25576 gen_namespace_die (decl, context_die);
25577 break;
25578
25579 case IMPORTED_DECL:
25580 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25581 DECL_CONTEXT (decl), context_die);
25582 break;
25583
25584 case NAMELIST_DECL:
25585 gen_namelist_decl (DECL_NAME (decl), context_die,
25586 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25587 break;
25588
25589 default:
25590 /* Probably some frontend-internal decl. Assume we don't care. */
25591 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25592 break;
25593 }
25594
25595 return NULL;
25596 }
25597 \f
25598 /* Output initial debug information for global DECL. Called at the
25599 end of the parsing process.
25600
25601 This is the initial debug generation process. As such, the DIEs
25602 generated may be incomplete. A later debug generation pass
25603 (dwarf2out_late_global_decl) will augment the information generated
25604 in this pass (e.g., with complete location info). */
25605
25606 static void
25607 dwarf2out_early_global_decl (tree decl)
25608 {
25609 set_early_dwarf s;
25610
25611 /* gen_decl_die() will set DECL_ABSTRACT because
25612 cgraph_function_possibly_inlined_p() returns true. This is in
25613 turn will cause DW_AT_inline attributes to be set.
25614
25615 This happens because at early dwarf generation, there is no
25616 cgraph information, causing cgraph_function_possibly_inlined_p()
25617 to return true. Trick cgraph_function_possibly_inlined_p()
25618 while we generate dwarf early. */
25619 bool save = symtab->global_info_ready;
25620 symtab->global_info_ready = true;
25621
25622 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25623 other DECLs and they can point to template types or other things
25624 that dwarf2out can't handle when done via dwarf2out_decl. */
25625 if (TREE_CODE (decl) != TYPE_DECL
25626 && TREE_CODE (decl) != PARM_DECL)
25627 {
25628 if (TREE_CODE (decl) == FUNCTION_DECL)
25629 {
25630 tree save_fndecl = current_function_decl;
25631
25632 /* For nested functions, make sure we have DIEs for the parents first
25633 so that all nested DIEs are generated at the proper scope in the
25634 first shot. */
25635 tree context = decl_function_context (decl);
25636 if (context != NULL)
25637 {
25638 dw_die_ref context_die = lookup_decl_die (context);
25639 current_function_decl = context;
25640
25641 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25642 enough so that it lands in its own context. This avoids type
25643 pruning issues later on. */
25644 if (context_die == NULL || is_declaration_die (context_die))
25645 dwarf2out_decl (context);
25646 }
25647
25648 /* Emit an abstract origin of a function first. This happens
25649 with C++ constructor clones for example and makes
25650 dwarf2out_abstract_function happy which requires the early
25651 DIE of the abstract instance to be present. */
25652 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25653 dw_die_ref origin_die;
25654 if (origin != NULL
25655 /* Do not emit the DIE multiple times but make sure to
25656 process it fully here in case we just saw a declaration. */
25657 && ((origin_die = lookup_decl_die (origin)) == NULL
25658 || is_declaration_die (origin_die)))
25659 {
25660 current_function_decl = origin;
25661 dwarf2out_decl (origin);
25662 }
25663
25664 /* Emit the DIE for decl but avoid doing that multiple times. */
25665 dw_die_ref old_die;
25666 if ((old_die = lookup_decl_die (decl)) == NULL
25667 || is_declaration_die (old_die))
25668 {
25669 current_function_decl = decl;
25670 dwarf2out_decl (decl);
25671 }
25672
25673 current_function_decl = save_fndecl;
25674 }
25675 else
25676 dwarf2out_decl (decl);
25677 }
25678 symtab->global_info_ready = save;
25679 }
25680
25681 /* Output debug information for global decl DECL. Called from
25682 toplev.c after compilation proper has finished. */
25683
25684 static void
25685 dwarf2out_late_global_decl (tree decl)
25686 {
25687 /* Fill-in any location information we were unable to determine
25688 on the first pass. */
25689 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25690 {
25691 dw_die_ref die = lookup_decl_die (decl);
25692
25693 /* We may have to generate early debug late for LTO in case debug
25694 was not enabled at compile-time or the target doesn't support
25695 the LTO early debug scheme. */
25696 if (! die && in_lto_p)
25697 {
25698 dwarf2out_decl (decl);
25699 die = lookup_decl_die (decl);
25700 }
25701
25702 if (die)
25703 {
25704 /* We get called via the symtab code invoking late_global_decl
25705 for symbols that are optimized out. Do not add locations
25706 for those, except if they have a DECL_VALUE_EXPR, in which case
25707 they are relevant for debuggers. */
25708 varpool_node *node = varpool_node::get (decl);
25709 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25710 tree_add_const_value_attribute_for_decl (die, decl);
25711 else
25712 add_location_or_const_value_attribute (die, decl, false);
25713 }
25714 }
25715 }
25716
25717 /* Output debug information for type decl DECL. Called from toplev.c
25718 and from language front ends (to record built-in types). */
25719 static void
25720 dwarf2out_type_decl (tree decl, int local)
25721 {
25722 if (!local)
25723 {
25724 set_early_dwarf s;
25725 dwarf2out_decl (decl);
25726 }
25727 }
25728
25729 /* Output debug information for imported module or decl DECL.
25730 NAME is non-NULL name in the lexical block if the decl has been renamed.
25731 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25732 that DECL belongs to.
25733 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25734 static void
25735 dwarf2out_imported_module_or_decl_1 (tree decl,
25736 tree name,
25737 tree lexical_block,
25738 dw_die_ref lexical_block_die)
25739 {
25740 expanded_location xloc;
25741 dw_die_ref imported_die = NULL;
25742 dw_die_ref at_import_die;
25743
25744 if (TREE_CODE (decl) == IMPORTED_DECL)
25745 {
25746 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25747 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25748 gcc_assert (decl);
25749 }
25750 else
25751 xloc = expand_location (input_location);
25752
25753 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25754 {
25755 at_import_die = force_type_die (TREE_TYPE (decl));
25756 /* For namespace N { typedef void T; } using N::T; base_type_die
25757 returns NULL, but DW_TAG_imported_declaration requires
25758 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25759 if (!at_import_die)
25760 {
25761 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25762 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25763 at_import_die = lookup_type_die (TREE_TYPE (decl));
25764 gcc_assert (at_import_die);
25765 }
25766 }
25767 else
25768 {
25769 at_import_die = lookup_decl_die (decl);
25770 if (!at_import_die)
25771 {
25772 /* If we're trying to avoid duplicate debug info, we may not have
25773 emitted the member decl for this field. Emit it now. */
25774 if (TREE_CODE (decl) == FIELD_DECL)
25775 {
25776 tree type = DECL_CONTEXT (decl);
25777
25778 if (TYPE_CONTEXT (type)
25779 && TYPE_P (TYPE_CONTEXT (type))
25780 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25781 DINFO_USAGE_DIR_USE))
25782 return;
25783 gen_type_die_for_member (type, decl,
25784 get_context_die (TYPE_CONTEXT (type)));
25785 }
25786 if (TREE_CODE (decl) == NAMELIST_DECL)
25787 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25788 get_context_die (DECL_CONTEXT (decl)),
25789 NULL_TREE);
25790 else
25791 at_import_die = force_decl_die (decl);
25792 }
25793 }
25794
25795 if (TREE_CODE (decl) == NAMESPACE_DECL)
25796 {
25797 if (dwarf_version >= 3 || !dwarf_strict)
25798 imported_die = new_die (DW_TAG_imported_module,
25799 lexical_block_die,
25800 lexical_block);
25801 else
25802 return;
25803 }
25804 else
25805 imported_die = new_die (DW_TAG_imported_declaration,
25806 lexical_block_die,
25807 lexical_block);
25808
25809 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25810 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25811 if (debug_column_info && xloc.column)
25812 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25813 if (name)
25814 add_AT_string (imported_die, DW_AT_name,
25815 IDENTIFIER_POINTER (name));
25816 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25817 }
25818
25819 /* Output debug information for imported module or decl DECL.
25820 NAME is non-NULL name in context if the decl has been renamed.
25821 CHILD is true if decl is one of the renamed decls as part of
25822 importing whole module.
25823 IMPLICIT is set if this hook is called for an implicit import
25824 such as inline namespace. */
25825
25826 static void
25827 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25828 bool child, bool implicit)
25829 {
25830 /* dw_die_ref at_import_die; */
25831 dw_die_ref scope_die;
25832
25833 if (debug_info_level <= DINFO_LEVEL_TERSE)
25834 return;
25835
25836 gcc_assert (decl);
25837
25838 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25839 should be enough, for DWARF4 and older even if we emit as extension
25840 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25841 for the benefit of consumers unaware of DW_AT_export_symbols. */
25842 if (implicit
25843 && dwarf_version >= 5
25844 && lang_hooks.decls.decl_dwarf_attribute (decl,
25845 DW_AT_export_symbols) == 1)
25846 return;
25847
25848 set_early_dwarf s;
25849
25850 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25851 We need decl DIE for reference and scope die. First, get DIE for the decl
25852 itself. */
25853
25854 /* Get the scope die for decl context. Use comp_unit_die for global module
25855 or decl. If die is not found for non globals, force new die. */
25856 if (context
25857 && TYPE_P (context)
25858 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25859 return;
25860
25861 scope_die = get_context_die (context);
25862
25863 if (child)
25864 {
25865 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25866 there is nothing we can do, here. */
25867 if (dwarf_version < 3 && dwarf_strict)
25868 return;
25869
25870 gcc_assert (scope_die->die_child);
25871 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25872 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25873 scope_die = scope_die->die_child;
25874 }
25875
25876 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25877 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25878 }
25879
25880 /* Output debug information for namelists. */
25881
25882 static dw_die_ref
25883 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25884 {
25885 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25886 tree value;
25887 unsigned i;
25888
25889 if (debug_info_level <= DINFO_LEVEL_TERSE)
25890 return NULL;
25891
25892 gcc_assert (scope_die != NULL);
25893 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25894 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25895
25896 /* If there are no item_decls, we have a nondefining namelist, e.g.
25897 with USE association; hence, set DW_AT_declaration. */
25898 if (item_decls == NULL_TREE)
25899 {
25900 add_AT_flag (nml_die, DW_AT_declaration, 1);
25901 return nml_die;
25902 }
25903
25904 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25905 {
25906 nml_item_ref_die = lookup_decl_die (value);
25907 if (!nml_item_ref_die)
25908 nml_item_ref_die = force_decl_die (value);
25909
25910 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25911 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25912 }
25913 return nml_die;
25914 }
25915
25916
25917 /* Write the debugging output for DECL and return the DIE. */
25918
25919 static void
25920 dwarf2out_decl (tree decl)
25921 {
25922 dw_die_ref context_die = comp_unit_die ();
25923
25924 switch (TREE_CODE (decl))
25925 {
25926 case ERROR_MARK:
25927 return;
25928
25929 case FUNCTION_DECL:
25930 /* If we're a nested function, initially use a parent of NULL; if we're
25931 a plain function, this will be fixed up in decls_for_scope. If
25932 we're a method, it will be ignored, since we already have a DIE. */
25933 if (decl_function_context (decl)
25934 /* But if we're in terse mode, we don't care about scope. */
25935 && debug_info_level > DINFO_LEVEL_TERSE)
25936 context_die = NULL;
25937 break;
25938
25939 case VAR_DECL:
25940 /* For local statics lookup proper context die. */
25941 if (local_function_static (decl))
25942 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25943
25944 /* If we are in terse mode, don't generate any DIEs to represent any
25945 variable declarations or definitions. */
25946 if (debug_info_level <= DINFO_LEVEL_TERSE)
25947 return;
25948 break;
25949
25950 case CONST_DECL:
25951 if (debug_info_level <= DINFO_LEVEL_TERSE)
25952 return;
25953 if (!is_fortran () && !is_ada ())
25954 return;
25955 if (TREE_STATIC (decl) && decl_function_context (decl))
25956 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25957 break;
25958
25959 case NAMESPACE_DECL:
25960 case IMPORTED_DECL:
25961 if (debug_info_level <= DINFO_LEVEL_TERSE)
25962 return;
25963 if (lookup_decl_die (decl) != NULL)
25964 return;
25965 break;
25966
25967 case TYPE_DECL:
25968 /* Don't emit stubs for types unless they are needed by other DIEs. */
25969 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25970 return;
25971
25972 /* Don't bother trying to generate any DIEs to represent any of the
25973 normal built-in types for the language we are compiling. */
25974 if (DECL_IS_BUILTIN (decl))
25975 return;
25976
25977 /* If we are in terse mode, don't generate any DIEs for types. */
25978 if (debug_info_level <= DINFO_LEVEL_TERSE)
25979 return;
25980
25981 /* If we're a function-scope tag, initially use a parent of NULL;
25982 this will be fixed up in decls_for_scope. */
25983 if (decl_function_context (decl))
25984 context_die = NULL;
25985
25986 break;
25987
25988 case NAMELIST_DECL:
25989 break;
25990
25991 default:
25992 return;
25993 }
25994
25995 gen_decl_die (decl, NULL, NULL, context_die);
25996
25997 if (flag_checking)
25998 {
25999 dw_die_ref die = lookup_decl_die (decl);
26000 if (die)
26001 check_die (die);
26002 }
26003 }
26004
26005 /* Write the debugging output for DECL. */
26006
26007 static void
26008 dwarf2out_function_decl (tree decl)
26009 {
26010 dwarf2out_decl (decl);
26011 call_arg_locations = NULL;
26012 call_arg_loc_last = NULL;
26013 call_site_count = -1;
26014 tail_call_site_count = -1;
26015 decl_loc_table->empty ();
26016 cached_dw_loc_list_table->empty ();
26017 }
26018
26019 /* Output a marker (i.e. a label) for the beginning of the generated code for
26020 a lexical block. */
26021
26022 static void
26023 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26024 unsigned int blocknum)
26025 {
26026 switch_to_section (current_function_section ());
26027 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26028 }
26029
26030 /* Output a marker (i.e. a label) for the end of the generated code for a
26031 lexical block. */
26032
26033 static void
26034 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26035 {
26036 switch_to_section (current_function_section ());
26037 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26038 }
26039
26040 /* Returns nonzero if it is appropriate not to emit any debugging
26041 information for BLOCK, because it doesn't contain any instructions.
26042
26043 Don't allow this for blocks with nested functions or local classes
26044 as we would end up with orphans, and in the presence of scheduling
26045 we may end up calling them anyway. */
26046
26047 static bool
26048 dwarf2out_ignore_block (const_tree block)
26049 {
26050 tree decl;
26051 unsigned int i;
26052
26053 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26054 if (TREE_CODE (decl) == FUNCTION_DECL
26055 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26056 return 0;
26057 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26058 {
26059 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26060 if (TREE_CODE (decl) == FUNCTION_DECL
26061 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26062 return 0;
26063 }
26064
26065 return 1;
26066 }
26067
26068 /* Hash table routines for file_hash. */
26069
26070 bool
26071 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26072 {
26073 return filename_cmp (p1->filename, p2) == 0;
26074 }
26075
26076 hashval_t
26077 dwarf_file_hasher::hash (dwarf_file_data *p)
26078 {
26079 return htab_hash_string (p->filename);
26080 }
26081
26082 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26083 dwarf2out.c) and return its "index". The index of each (known) filename is
26084 just a unique number which is associated with only that one filename. We
26085 need such numbers for the sake of generating labels (in the .debug_sfnames
26086 section) and references to those files numbers (in the .debug_srcinfo
26087 and .debug_macinfo sections). If the filename given as an argument is not
26088 found in our current list, add it to the list and assign it the next
26089 available unique index number. */
26090
26091 static struct dwarf_file_data *
26092 lookup_filename (const char *file_name)
26093 {
26094 struct dwarf_file_data * created;
26095
26096 if (!file_name)
26097 return NULL;
26098
26099 dwarf_file_data **slot
26100 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26101 INSERT);
26102 if (*slot)
26103 return *slot;
26104
26105 created = ggc_alloc<dwarf_file_data> ();
26106 created->filename = file_name;
26107 created->emitted_number = 0;
26108 *slot = created;
26109 return created;
26110 }
26111
26112 /* If the assembler will construct the file table, then translate the compiler
26113 internal file table number into the assembler file table number, and emit
26114 a .file directive if we haven't already emitted one yet. The file table
26115 numbers are different because we prune debug info for unused variables and
26116 types, which may include filenames. */
26117
26118 static int
26119 maybe_emit_file (struct dwarf_file_data * fd)
26120 {
26121 if (! fd->emitted_number)
26122 {
26123 if (last_emitted_file)
26124 fd->emitted_number = last_emitted_file->emitted_number + 1;
26125 else
26126 fd->emitted_number = 1;
26127 last_emitted_file = fd;
26128
26129 if (DWARF2_ASM_LINE_DEBUG_INFO)
26130 {
26131 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26132 output_quoted_string (asm_out_file,
26133 remap_debug_filename (fd->filename));
26134 fputc ('\n', asm_out_file);
26135 }
26136 }
26137
26138 return fd->emitted_number;
26139 }
26140
26141 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26142 That generation should happen after function debug info has been
26143 generated. The value of the attribute is the constant value of ARG. */
26144
26145 static void
26146 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26147 {
26148 die_arg_entry entry;
26149
26150 if (!die || !arg)
26151 return;
26152
26153 gcc_assert (early_dwarf);
26154
26155 if (!tmpl_value_parm_die_table)
26156 vec_alloc (tmpl_value_parm_die_table, 32);
26157
26158 entry.die = die;
26159 entry.arg = arg;
26160 vec_safe_push (tmpl_value_parm_die_table, entry);
26161 }
26162
26163 /* Return TRUE if T is an instance of generic type, FALSE
26164 otherwise. */
26165
26166 static bool
26167 generic_type_p (tree t)
26168 {
26169 if (t == NULL_TREE || !TYPE_P (t))
26170 return false;
26171 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26172 }
26173
26174 /* Schedule the generation of the generic parameter dies for the
26175 instance of generic type T. The proper generation itself is later
26176 done by gen_scheduled_generic_parms_dies. */
26177
26178 static void
26179 schedule_generic_params_dies_gen (tree t)
26180 {
26181 if (!generic_type_p (t))
26182 return;
26183
26184 gcc_assert (early_dwarf);
26185
26186 if (!generic_type_instances)
26187 vec_alloc (generic_type_instances, 256);
26188
26189 vec_safe_push (generic_type_instances, t);
26190 }
26191
26192 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26193 by append_entry_to_tmpl_value_parm_die_table. This function must
26194 be called after function DIEs have been generated. */
26195
26196 static void
26197 gen_remaining_tmpl_value_param_die_attribute (void)
26198 {
26199 if (tmpl_value_parm_die_table)
26200 {
26201 unsigned i, j;
26202 die_arg_entry *e;
26203
26204 /* We do this in two phases - first get the cases we can
26205 handle during early-finish, preserving those we cannot
26206 (containing symbolic constants where we don't yet know
26207 whether we are going to output the referenced symbols).
26208 For those we try again at late-finish. */
26209 j = 0;
26210 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26211 {
26212 if (!e->die->removed
26213 && !tree_add_const_value_attribute (e->die, e->arg))
26214 {
26215 dw_loc_descr_ref loc = NULL;
26216 if (! early_dwarf
26217 && (dwarf_version >= 5 || !dwarf_strict))
26218 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26219 if (loc)
26220 add_AT_loc (e->die, DW_AT_location, loc);
26221 else
26222 (*tmpl_value_parm_die_table)[j++] = *e;
26223 }
26224 }
26225 tmpl_value_parm_die_table->truncate (j);
26226 }
26227 }
26228
26229 /* Generate generic parameters DIEs for instances of generic types
26230 that have been previously scheduled by
26231 schedule_generic_params_dies_gen. This function must be called
26232 after all the types of the CU have been laid out. */
26233
26234 static void
26235 gen_scheduled_generic_parms_dies (void)
26236 {
26237 unsigned i;
26238 tree t;
26239
26240 if (!generic_type_instances)
26241 return;
26242
26243 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26244 if (COMPLETE_TYPE_P (t))
26245 gen_generic_params_dies (t);
26246
26247 generic_type_instances = NULL;
26248 }
26249
26250
26251 /* Replace DW_AT_name for the decl with name. */
26252
26253 static void
26254 dwarf2out_set_name (tree decl, tree name)
26255 {
26256 dw_die_ref die;
26257 dw_attr_node *attr;
26258 const char *dname;
26259
26260 die = TYPE_SYMTAB_DIE (decl);
26261 if (!die)
26262 return;
26263
26264 dname = dwarf2_name (name, 0);
26265 if (!dname)
26266 return;
26267
26268 attr = get_AT (die, DW_AT_name);
26269 if (attr)
26270 {
26271 struct indirect_string_node *node;
26272
26273 node = find_AT_string (dname);
26274 /* replace the string. */
26275 attr->dw_attr_val.v.val_str = node;
26276 }
26277
26278 else
26279 add_name_attribute (die, dname);
26280 }
26281
26282 /* True if before or during processing of the first function being emitted. */
26283 static bool in_first_function_p = true;
26284 /* True if loc_note during dwarf2out_var_location call might still be
26285 before first real instruction at address equal to .Ltext0. */
26286 static bool maybe_at_text_label_p = true;
26287 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26288 static unsigned int first_loclabel_num_not_at_text_label;
26289
26290 /* Look ahead for a real insn, or for a begin stmt marker. */
26291
26292 static rtx_insn *
26293 dwarf2out_next_real_insn (rtx_insn *loc_note)
26294 {
26295 rtx_insn *next_real = NEXT_INSN (loc_note);
26296
26297 while (next_real)
26298 if (INSN_P (next_real))
26299 break;
26300 else
26301 next_real = NEXT_INSN (next_real);
26302
26303 return next_real;
26304 }
26305
26306 /* Called by the final INSN scan whenever we see a var location. We
26307 use it to drop labels in the right places, and throw the location in
26308 our lookup table. */
26309
26310 static void
26311 dwarf2out_var_location (rtx_insn *loc_note)
26312 {
26313 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26314 struct var_loc_node *newloc;
26315 rtx_insn *next_real, *next_note;
26316 rtx_insn *call_insn = NULL;
26317 static const char *last_label;
26318 static const char *last_postcall_label;
26319 static bool last_in_cold_section_p;
26320 static rtx_insn *expected_next_loc_note;
26321 tree decl;
26322 bool var_loc_p;
26323
26324 if (!NOTE_P (loc_note))
26325 {
26326 if (CALL_P (loc_note))
26327 {
26328 call_site_count++;
26329 if (SIBLING_CALL_P (loc_note))
26330 tail_call_site_count++;
26331 if (optimize == 0 && !flag_var_tracking)
26332 {
26333 /* When the var-tracking pass is not running, there is no note
26334 for indirect calls whose target is compile-time known. In this
26335 case, process such calls specifically so that we generate call
26336 sites for them anyway. */
26337 rtx x = PATTERN (loc_note);
26338 if (GET_CODE (x) == PARALLEL)
26339 x = XVECEXP (x, 0, 0);
26340 if (GET_CODE (x) == SET)
26341 x = SET_SRC (x);
26342 if (GET_CODE (x) == CALL)
26343 x = XEXP (x, 0);
26344 if (!MEM_P (x)
26345 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26346 || !SYMBOL_REF_DECL (XEXP (x, 0))
26347 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26348 != FUNCTION_DECL))
26349 {
26350 call_insn = loc_note;
26351 loc_note = NULL;
26352 var_loc_p = false;
26353
26354 next_real = dwarf2out_next_real_insn (call_insn);
26355 next_note = NULL;
26356 cached_next_real_insn = NULL;
26357 goto create_label;
26358 }
26359 }
26360 }
26361 return;
26362 }
26363
26364 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26365 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26366 return;
26367
26368 /* Optimize processing a large consecutive sequence of location
26369 notes so we don't spend too much time in next_real_insn. If the
26370 next insn is another location note, remember the next_real_insn
26371 calculation for next time. */
26372 next_real = cached_next_real_insn;
26373 if (next_real)
26374 {
26375 if (expected_next_loc_note != loc_note)
26376 next_real = NULL;
26377 }
26378
26379 next_note = NEXT_INSN (loc_note);
26380 if (! next_note
26381 || next_note->deleted ()
26382 || ! NOTE_P (next_note)
26383 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26384 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
26385 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26386 next_note = NULL;
26387
26388 if (! next_real)
26389 next_real = dwarf2out_next_real_insn (loc_note);
26390
26391 if (next_note)
26392 {
26393 expected_next_loc_note = next_note;
26394 cached_next_real_insn = next_real;
26395 }
26396 else
26397 cached_next_real_insn = NULL;
26398
26399 /* If there are no instructions which would be affected by this note,
26400 don't do anything. */
26401 if (var_loc_p
26402 && next_real == NULL_RTX
26403 && !NOTE_DURING_CALL_P (loc_note))
26404 return;
26405
26406 create_label:
26407
26408 if (next_real == NULL_RTX)
26409 next_real = get_last_insn ();
26410
26411 /* If there were any real insns between note we processed last time
26412 and this note (or if it is the first note), clear
26413 last_{,postcall_}label so that they are not reused this time. */
26414 if (last_var_location_insn == NULL_RTX
26415 || last_var_location_insn != next_real
26416 || last_in_cold_section_p != in_cold_section_p)
26417 {
26418 last_label = NULL;
26419 last_postcall_label = NULL;
26420 }
26421
26422 if (var_loc_p)
26423 {
26424 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26425 newloc = add_var_loc_to_decl (decl, loc_note,
26426 NOTE_DURING_CALL_P (loc_note)
26427 ? last_postcall_label : last_label);
26428 if (newloc == NULL)
26429 return;
26430 }
26431 else
26432 {
26433 decl = NULL_TREE;
26434 newloc = NULL;
26435 }
26436
26437 /* If there were no real insns between note we processed last time
26438 and this note, use the label we emitted last time. Otherwise
26439 create a new label and emit it. */
26440 if (last_label == NULL)
26441 {
26442 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26443 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26444 loclabel_num++;
26445 last_label = ggc_strdup (loclabel);
26446 /* See if loclabel might be equal to .Ltext0. If yes,
26447 bump first_loclabel_num_not_at_text_label. */
26448 if (!have_multiple_function_sections
26449 && in_first_function_p
26450 && maybe_at_text_label_p)
26451 {
26452 static rtx_insn *last_start;
26453 rtx_insn *insn;
26454 for (insn = loc_note; insn; insn = previous_insn (insn))
26455 if (insn == last_start)
26456 break;
26457 else if (!NONDEBUG_INSN_P (insn))
26458 continue;
26459 else
26460 {
26461 rtx body = PATTERN (insn);
26462 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26463 continue;
26464 /* Inline asm could occupy zero bytes. */
26465 else if (GET_CODE (body) == ASM_INPUT
26466 || asm_noperands (body) >= 0)
26467 continue;
26468 #ifdef HAVE_attr_length
26469 else if (get_attr_min_length (insn) == 0)
26470 continue;
26471 #endif
26472 else
26473 {
26474 /* Assume insn has non-zero length. */
26475 maybe_at_text_label_p = false;
26476 break;
26477 }
26478 }
26479 if (maybe_at_text_label_p)
26480 {
26481 last_start = loc_note;
26482 first_loclabel_num_not_at_text_label = loclabel_num;
26483 }
26484 }
26485 }
26486
26487 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26488 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26489
26490 if (!var_loc_p)
26491 {
26492 struct call_arg_loc_node *ca_loc
26493 = ggc_cleared_alloc<call_arg_loc_node> ();
26494 rtx_insn *prev
26495 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26496
26497 ca_loc->call_arg_loc_note = loc_note;
26498 ca_loc->next = NULL;
26499 ca_loc->label = last_label;
26500 gcc_assert (prev
26501 && (CALL_P (prev)
26502 || (NONJUMP_INSN_P (prev)
26503 && GET_CODE (PATTERN (prev)) == SEQUENCE
26504 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26505 if (!CALL_P (prev))
26506 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26507 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26508
26509 /* Look for a SYMBOL_REF in the "prev" instruction. */
26510 rtx x = get_call_rtx_from (PATTERN (prev));
26511 if (x)
26512 {
26513 /* Try to get the call symbol, if any. */
26514 if (MEM_P (XEXP (x, 0)))
26515 x = XEXP (x, 0);
26516 /* First, look for a memory access to a symbol_ref. */
26517 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26518 && SYMBOL_REF_DECL (XEXP (x, 0))
26519 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26520 ca_loc->symbol_ref = XEXP (x, 0);
26521 /* Otherwise, look at a compile-time known user-level function
26522 declaration. */
26523 else if (MEM_P (x)
26524 && MEM_EXPR (x)
26525 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26526 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26527 }
26528
26529 ca_loc->block = insn_scope (prev);
26530 if (call_arg_locations)
26531 call_arg_loc_last->next = ca_loc;
26532 else
26533 call_arg_locations = ca_loc;
26534 call_arg_loc_last = ca_loc;
26535 }
26536 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26537 newloc->label = last_label;
26538 else
26539 {
26540 if (!last_postcall_label)
26541 {
26542 sprintf (loclabel, "%s-1", last_label);
26543 last_postcall_label = ggc_strdup (loclabel);
26544 }
26545 newloc->label = last_postcall_label;
26546 }
26547
26548 if (var_loc_p && flag_debug_asm)
26549 {
26550 const char *name = NULL, *sep = " => ", *patstr = NULL;
26551 if (decl && DECL_NAME (decl))
26552 name = IDENTIFIER_POINTER (DECL_NAME (decl));
26553 if (NOTE_VAR_LOCATION_LOC (loc_note))
26554 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
26555 else
26556 {
26557 sep = " ";
26558 patstr = "RESET";
26559 }
26560 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
26561 name, sep, patstr);
26562 }
26563
26564 last_var_location_insn = next_real;
26565 last_in_cold_section_p = in_cold_section_p;
26566 }
26567
26568 /* Called from finalize_size_functions for size functions so that their body
26569 can be encoded in the debug info to describe the layout of variable-length
26570 structures. */
26571
26572 static void
26573 dwarf2out_size_function (tree decl)
26574 {
26575 function_to_dwarf_procedure (decl);
26576 }
26577
26578 /* Note in one location list that text section has changed. */
26579
26580 int
26581 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26582 {
26583 var_loc_list *list = *slot;
26584 if (list->first)
26585 list->last_before_switch
26586 = list->last->next ? list->last->next : list->last;
26587 return 1;
26588 }
26589
26590 /* Note in all location lists that text section has changed. */
26591
26592 static void
26593 var_location_switch_text_section (void)
26594 {
26595 if (decl_loc_table == NULL)
26596 return;
26597
26598 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26599 }
26600
26601 /* Create a new line number table. */
26602
26603 static dw_line_info_table *
26604 new_line_info_table (void)
26605 {
26606 dw_line_info_table *table;
26607
26608 table = ggc_cleared_alloc<dw_line_info_table> ();
26609 table->file_num = 1;
26610 table->line_num = 1;
26611 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26612
26613 return table;
26614 }
26615
26616 /* Lookup the "current" table into which we emit line info, so
26617 that we don't have to do it for every source line. */
26618
26619 static void
26620 set_cur_line_info_table (section *sec)
26621 {
26622 dw_line_info_table *table;
26623
26624 if (sec == text_section)
26625 table = text_section_line_info;
26626 else if (sec == cold_text_section)
26627 {
26628 table = cold_text_section_line_info;
26629 if (!table)
26630 {
26631 cold_text_section_line_info = table = new_line_info_table ();
26632 table->end_label = cold_end_label;
26633 }
26634 }
26635 else
26636 {
26637 const char *end_label;
26638
26639 if (crtl->has_bb_partition)
26640 {
26641 if (in_cold_section_p)
26642 end_label = crtl->subsections.cold_section_end_label;
26643 else
26644 end_label = crtl->subsections.hot_section_end_label;
26645 }
26646 else
26647 {
26648 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26649 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26650 current_function_funcdef_no);
26651 end_label = ggc_strdup (label);
26652 }
26653
26654 table = new_line_info_table ();
26655 table->end_label = end_label;
26656
26657 vec_safe_push (separate_line_info, table);
26658 }
26659
26660 if (DWARF2_ASM_LINE_DEBUG_INFO)
26661 table->is_stmt = (cur_line_info_table
26662 ? cur_line_info_table->is_stmt
26663 : DWARF_LINE_DEFAULT_IS_STMT_START);
26664 cur_line_info_table = table;
26665 }
26666
26667
26668 /* We need to reset the locations at the beginning of each
26669 function. We can't do this in the end_function hook, because the
26670 declarations that use the locations won't have been output when
26671 that hook is called. Also compute have_multiple_function_sections here. */
26672
26673 static void
26674 dwarf2out_begin_function (tree fun)
26675 {
26676 section *sec = function_section (fun);
26677
26678 if (sec != text_section)
26679 have_multiple_function_sections = true;
26680
26681 if (crtl->has_bb_partition && !cold_text_section)
26682 {
26683 gcc_assert (current_function_decl == fun);
26684 cold_text_section = unlikely_text_section ();
26685 switch_to_section (cold_text_section);
26686 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26687 switch_to_section (sec);
26688 }
26689
26690 dwarf2out_note_section_used ();
26691 call_site_count = 0;
26692 tail_call_site_count = 0;
26693
26694 set_cur_line_info_table (sec);
26695 }
26696
26697 /* Helper function of dwarf2out_end_function, called only after emitting
26698 the very first function into assembly. Check if some .debug_loc range
26699 might end with a .LVL* label that could be equal to .Ltext0.
26700 In that case we must force using absolute addresses in .debug_loc ranges,
26701 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26702 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26703 list terminator.
26704 Set have_multiple_function_sections to true in that case and
26705 terminate htab traversal. */
26706
26707 int
26708 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26709 {
26710 var_loc_list *entry = *slot;
26711 struct var_loc_node *node;
26712
26713 node = entry->first;
26714 if (node && node->next && node->next->label)
26715 {
26716 unsigned int i;
26717 const char *label = node->next->label;
26718 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26719
26720 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26721 {
26722 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26723 if (strcmp (label, loclabel) == 0)
26724 {
26725 have_multiple_function_sections = true;
26726 return 0;
26727 }
26728 }
26729 }
26730 return 1;
26731 }
26732
26733 /* Hook called after emitting a function into assembly.
26734 This does something only for the very first function emitted. */
26735
26736 static void
26737 dwarf2out_end_function (unsigned int)
26738 {
26739 if (in_first_function_p
26740 && !have_multiple_function_sections
26741 && first_loclabel_num_not_at_text_label
26742 && decl_loc_table)
26743 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26744 in_first_function_p = false;
26745 maybe_at_text_label_p = false;
26746 }
26747
26748 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26749 front-ends register a translation unit even before dwarf2out_init is
26750 called. */
26751 static tree main_translation_unit = NULL_TREE;
26752
26753 /* Hook called by front-ends after they built their main translation unit.
26754 Associate comp_unit_die to UNIT. */
26755
26756 static void
26757 dwarf2out_register_main_translation_unit (tree unit)
26758 {
26759 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26760 && main_translation_unit == NULL_TREE);
26761 main_translation_unit = unit;
26762 /* If dwarf2out_init has not been called yet, it will perform the association
26763 itself looking at main_translation_unit. */
26764 if (decl_die_table != NULL)
26765 equate_decl_number_to_die (unit, comp_unit_die ());
26766 }
26767
26768 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26769
26770 static void
26771 push_dw_line_info_entry (dw_line_info_table *table,
26772 enum dw_line_info_opcode opcode, unsigned int val)
26773 {
26774 dw_line_info_entry e;
26775 e.opcode = opcode;
26776 e.val = val;
26777 vec_safe_push (table->entries, e);
26778 }
26779
26780 /* Output a label to mark the beginning of a source code line entry
26781 and record information relating to this source line, in
26782 'line_info_table' for later output of the .debug_line section. */
26783 /* ??? The discriminator parameter ought to be unsigned. */
26784
26785 static void
26786 dwarf2out_source_line (unsigned int line, unsigned int column,
26787 const char *filename,
26788 int discriminator, bool is_stmt)
26789 {
26790 unsigned int file_num;
26791 dw_line_info_table *table;
26792
26793 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26794 return;
26795
26796 /* The discriminator column was added in dwarf4. Simplify the below
26797 by simply removing it if we're not supposed to output it. */
26798 if (dwarf_version < 4 && dwarf_strict)
26799 discriminator = 0;
26800
26801 if (!debug_column_info)
26802 column = 0;
26803
26804 table = cur_line_info_table;
26805 file_num = maybe_emit_file (lookup_filename (filename));
26806
26807 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26808 the debugger has used the second (possibly duplicate) line number
26809 at the beginning of the function to mark the end of the prologue.
26810 We could eliminate any other duplicates within the function. For
26811 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26812 that second line number entry. */
26813 /* Recall that this end-of-prologue indication is *not* the same thing
26814 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26815 to which the hook corresponds, follows the last insn that was
26816 emitted by gen_prologue. What we need is to precede the first insn
26817 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26818 insn that corresponds to something the user wrote. These may be
26819 very different locations once scheduling is enabled. */
26820
26821 if (0 && file_num == table->file_num
26822 && line == table->line_num
26823 && column == table->column_num
26824 && discriminator == table->discrim_num
26825 && is_stmt == table->is_stmt)
26826 return;
26827
26828 switch_to_section (current_function_section ());
26829
26830 /* If requested, emit something human-readable. */
26831 if (flag_debug_asm)
26832 {
26833 if (debug_column_info)
26834 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26835 filename, line, column);
26836 else
26837 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26838 filename, line);
26839 }
26840
26841 if (DWARF2_ASM_LINE_DEBUG_INFO)
26842 {
26843 /* Emit the .loc directive understood by GNU as. */
26844 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26845 file_num, line, is_stmt, discriminator */
26846 fputs ("\t.loc ", asm_out_file);
26847 fprint_ul (asm_out_file, file_num);
26848 putc (' ', asm_out_file);
26849 fprint_ul (asm_out_file, line);
26850 putc (' ', asm_out_file);
26851 fprint_ul (asm_out_file, column);
26852
26853 if (is_stmt != table->is_stmt)
26854 {
26855 fputs (" is_stmt ", asm_out_file);
26856 putc (is_stmt ? '1' : '0', asm_out_file);
26857 }
26858 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26859 {
26860 gcc_assert (discriminator > 0);
26861 fputs (" discriminator ", asm_out_file);
26862 fprint_ul (asm_out_file, (unsigned long) discriminator);
26863 }
26864 putc ('\n', asm_out_file);
26865 }
26866 else
26867 {
26868 unsigned int label_num = ++line_info_label_num;
26869
26870 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26871
26872 push_dw_line_info_entry (table, LI_set_address, label_num);
26873 if (file_num != table->file_num)
26874 push_dw_line_info_entry (table, LI_set_file, file_num);
26875 if (discriminator != table->discrim_num)
26876 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26877 if (is_stmt != table->is_stmt)
26878 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26879 push_dw_line_info_entry (table, LI_set_line, line);
26880 if (debug_column_info)
26881 push_dw_line_info_entry (table, LI_set_column, column);
26882 }
26883
26884 table->file_num = file_num;
26885 table->line_num = line;
26886 table->column_num = column;
26887 table->discrim_num = discriminator;
26888 table->is_stmt = is_stmt;
26889 table->in_use = true;
26890 }
26891
26892 /* Record the beginning of a new source file. */
26893
26894 static void
26895 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26896 {
26897 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26898 {
26899 macinfo_entry e;
26900 e.code = DW_MACINFO_start_file;
26901 e.lineno = lineno;
26902 e.info = ggc_strdup (filename);
26903 vec_safe_push (macinfo_table, e);
26904 }
26905 }
26906
26907 /* Record the end of a source file. */
26908
26909 static void
26910 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26911 {
26912 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26913 {
26914 macinfo_entry e;
26915 e.code = DW_MACINFO_end_file;
26916 e.lineno = lineno;
26917 e.info = NULL;
26918 vec_safe_push (macinfo_table, e);
26919 }
26920 }
26921
26922 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26923 the tail part of the directive line, i.e. the part which is past the
26924 initial whitespace, #, whitespace, directive-name, whitespace part. */
26925
26926 static void
26927 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26928 const char *buffer ATTRIBUTE_UNUSED)
26929 {
26930 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26931 {
26932 macinfo_entry e;
26933 /* Insert a dummy first entry to be able to optimize the whole
26934 predefined macro block using DW_MACRO_import. */
26935 if (macinfo_table->is_empty () && lineno <= 1)
26936 {
26937 e.code = 0;
26938 e.lineno = 0;
26939 e.info = NULL;
26940 vec_safe_push (macinfo_table, e);
26941 }
26942 e.code = DW_MACINFO_define;
26943 e.lineno = lineno;
26944 e.info = ggc_strdup (buffer);
26945 vec_safe_push (macinfo_table, e);
26946 }
26947 }
26948
26949 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26950 the tail part of the directive line, i.e. the part which is past the
26951 initial whitespace, #, whitespace, directive-name, whitespace part. */
26952
26953 static void
26954 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26955 const char *buffer ATTRIBUTE_UNUSED)
26956 {
26957 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26958 {
26959 macinfo_entry e;
26960 /* Insert a dummy first entry to be able to optimize the whole
26961 predefined macro block using DW_MACRO_import. */
26962 if (macinfo_table->is_empty () && lineno <= 1)
26963 {
26964 e.code = 0;
26965 e.lineno = 0;
26966 e.info = NULL;
26967 vec_safe_push (macinfo_table, e);
26968 }
26969 e.code = DW_MACINFO_undef;
26970 e.lineno = lineno;
26971 e.info = ggc_strdup (buffer);
26972 vec_safe_push (macinfo_table, e);
26973 }
26974 }
26975
26976 /* Helpers to manipulate hash table of CUs. */
26977
26978 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26979 {
26980 static inline hashval_t hash (const macinfo_entry *);
26981 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26982 };
26983
26984 inline hashval_t
26985 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26986 {
26987 return htab_hash_string (entry->info);
26988 }
26989
26990 inline bool
26991 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26992 const macinfo_entry *entry2)
26993 {
26994 return !strcmp (entry1->info, entry2->info);
26995 }
26996
26997 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26998
26999 /* Output a single .debug_macinfo entry. */
27000
27001 static void
27002 output_macinfo_op (macinfo_entry *ref)
27003 {
27004 int file_num;
27005 size_t len;
27006 struct indirect_string_node *node;
27007 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27008 struct dwarf_file_data *fd;
27009
27010 switch (ref->code)
27011 {
27012 case DW_MACINFO_start_file:
27013 fd = lookup_filename (ref->info);
27014 file_num = maybe_emit_file (fd);
27015 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
27016 dw2_asm_output_data_uleb128 (ref->lineno,
27017 "Included from line number %lu",
27018 (unsigned long) ref->lineno);
27019 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
27020 break;
27021 case DW_MACINFO_end_file:
27022 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
27023 break;
27024 case DW_MACINFO_define:
27025 case DW_MACINFO_undef:
27026 len = strlen (ref->info) + 1;
27027 if (!dwarf_strict
27028 && len > DWARF_OFFSET_SIZE
27029 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27030 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27031 {
27032 ref->code = ref->code == DW_MACINFO_define
27033 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
27034 output_macinfo_op (ref);
27035 return;
27036 }
27037 dw2_asm_output_data (1, ref->code,
27038 ref->code == DW_MACINFO_define
27039 ? "Define macro" : "Undefine macro");
27040 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27041 (unsigned long) ref->lineno);
27042 dw2_asm_output_nstring (ref->info, -1, "The macro");
27043 break;
27044 case DW_MACRO_define_strp:
27045 case DW_MACRO_undef_strp:
27046 node = find_AT_string (ref->info);
27047 gcc_assert (node
27048 && (node->form == DW_FORM_strp
27049 || node->form == DW_FORM_GNU_str_index));
27050 dw2_asm_output_data (1, ref->code,
27051 ref->code == DW_MACRO_define_strp
27052 ? "Define macro strp"
27053 : "Undefine macro strp");
27054 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27055 (unsigned long) ref->lineno);
27056 if (node->form == DW_FORM_strp)
27057 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
27058 debug_str_section, "The macro: \"%s\"",
27059 ref->info);
27060 else
27061 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
27062 ref->info);
27063 break;
27064 case DW_MACRO_import:
27065 dw2_asm_output_data (1, ref->code, "Import");
27066 ASM_GENERATE_INTERNAL_LABEL (label,
27067 DEBUG_MACRO_SECTION_LABEL,
27068 ref->lineno + macinfo_label_base);
27069 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
27070 break;
27071 default:
27072 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
27073 ASM_COMMENT_START, (unsigned long) ref->code);
27074 break;
27075 }
27076 }
27077
27078 /* Attempt to make a sequence of define/undef macinfo ops shareable with
27079 other compilation unit .debug_macinfo sections. IDX is the first
27080 index of a define/undef, return the number of ops that should be
27081 emitted in a comdat .debug_macinfo section and emit
27082 a DW_MACRO_import entry referencing it.
27083 If the define/undef entry should be emitted normally, return 0. */
27084
27085 static unsigned
27086 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
27087 macinfo_hash_type **macinfo_htab)
27088 {
27089 macinfo_entry *first, *second, *cur, *inc;
27090 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
27091 unsigned char checksum[16];
27092 struct md5_ctx ctx;
27093 char *grp_name, *tail;
27094 const char *base;
27095 unsigned int i, count, encoded_filename_len, linebuf_len;
27096 macinfo_entry **slot;
27097
27098 first = &(*macinfo_table)[idx];
27099 second = &(*macinfo_table)[idx + 1];
27100
27101 /* Optimize only if there are at least two consecutive define/undef ops,
27102 and either all of them are before first DW_MACINFO_start_file
27103 with lineno {0,1} (i.e. predefined macro block), or all of them are
27104 in some included header file. */
27105 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
27106 return 0;
27107 if (vec_safe_is_empty (files))
27108 {
27109 if (first->lineno > 1 || second->lineno > 1)
27110 return 0;
27111 }
27112 else if (first->lineno == 0)
27113 return 0;
27114
27115 /* Find the last define/undef entry that can be grouped together
27116 with first and at the same time compute md5 checksum of their
27117 codes, linenumbers and strings. */
27118 md5_init_ctx (&ctx);
27119 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27120 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27121 break;
27122 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27123 break;
27124 else
27125 {
27126 unsigned char code = cur->code;
27127 md5_process_bytes (&code, 1, &ctx);
27128 checksum_uleb128 (cur->lineno, &ctx);
27129 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27130 }
27131 md5_finish_ctx (&ctx, checksum);
27132 count = i - idx;
27133
27134 /* From the containing include filename (if any) pick up just
27135 usable characters from its basename. */
27136 if (vec_safe_is_empty (files))
27137 base = "";
27138 else
27139 base = lbasename (files->last ().info);
27140 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27141 if (ISIDNUM (base[i]) || base[i] == '.')
27142 encoded_filename_len++;
27143 /* Count . at the end. */
27144 if (encoded_filename_len)
27145 encoded_filename_len++;
27146
27147 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27148 linebuf_len = strlen (linebuf);
27149
27150 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27151 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27152 + 16 * 2 + 1);
27153 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27154 tail = grp_name + 4;
27155 if (encoded_filename_len)
27156 {
27157 for (i = 0; base[i]; i++)
27158 if (ISIDNUM (base[i]) || base[i] == '.')
27159 *tail++ = base[i];
27160 *tail++ = '.';
27161 }
27162 memcpy (tail, linebuf, linebuf_len);
27163 tail += linebuf_len;
27164 *tail++ = '.';
27165 for (i = 0; i < 16; i++)
27166 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27167
27168 /* Construct a macinfo_entry for DW_MACRO_import
27169 in the empty vector entry before the first define/undef. */
27170 inc = &(*macinfo_table)[idx - 1];
27171 inc->code = DW_MACRO_import;
27172 inc->lineno = 0;
27173 inc->info = ggc_strdup (grp_name);
27174 if (!*macinfo_htab)
27175 *macinfo_htab = new macinfo_hash_type (10);
27176 /* Avoid emitting duplicates. */
27177 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27178 if (*slot != NULL)
27179 {
27180 inc->code = 0;
27181 inc->info = NULL;
27182 /* If such an entry has been used before, just emit
27183 a DW_MACRO_import op. */
27184 inc = *slot;
27185 output_macinfo_op (inc);
27186 /* And clear all macinfo_entry in the range to avoid emitting them
27187 in the second pass. */
27188 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27189 {
27190 cur->code = 0;
27191 cur->info = NULL;
27192 }
27193 }
27194 else
27195 {
27196 *slot = inc;
27197 inc->lineno = (*macinfo_htab)->elements ();
27198 output_macinfo_op (inc);
27199 }
27200 return count;
27201 }
27202
27203 /* Save any strings needed by the macinfo table in the debug str
27204 table. All strings must be collected into the table by the time
27205 index_string is called. */
27206
27207 static void
27208 save_macinfo_strings (void)
27209 {
27210 unsigned len;
27211 unsigned i;
27212 macinfo_entry *ref;
27213
27214 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27215 {
27216 switch (ref->code)
27217 {
27218 /* Match the logic in output_macinfo_op to decide on
27219 indirect strings. */
27220 case DW_MACINFO_define:
27221 case DW_MACINFO_undef:
27222 len = strlen (ref->info) + 1;
27223 if (!dwarf_strict
27224 && len > DWARF_OFFSET_SIZE
27225 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27226 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27227 set_indirect_string (find_AT_string (ref->info));
27228 break;
27229 case DW_MACRO_define_strp:
27230 case DW_MACRO_undef_strp:
27231 set_indirect_string (find_AT_string (ref->info));
27232 break;
27233 default:
27234 break;
27235 }
27236 }
27237 }
27238
27239 /* Output macinfo section(s). */
27240
27241 static void
27242 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27243 {
27244 unsigned i;
27245 unsigned long length = vec_safe_length (macinfo_table);
27246 macinfo_entry *ref;
27247 vec<macinfo_entry, va_gc> *files = NULL;
27248 macinfo_hash_type *macinfo_htab = NULL;
27249 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27250
27251 if (! length)
27252 return;
27253
27254 /* output_macinfo* uses these interchangeably. */
27255 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27256 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27257 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27258 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27259
27260 /* AIX Assembler inserts the length, so adjust the reference to match the
27261 offset expected by debuggers. */
27262 strcpy (dl_section_ref, debug_line_label);
27263 if (XCOFF_DEBUGGING_INFO)
27264 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27265
27266 /* For .debug_macro emit the section header. */
27267 if (!dwarf_strict || dwarf_version >= 5)
27268 {
27269 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27270 "DWARF macro version number");
27271 if (DWARF_OFFSET_SIZE == 8)
27272 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27273 else
27274 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27275 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27276 debug_line_section, NULL);
27277 }
27278
27279 /* In the first loop, it emits the primary .debug_macinfo section
27280 and after each emitted op the macinfo_entry is cleared.
27281 If a longer range of define/undef ops can be optimized using
27282 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27283 the vector before the first define/undef in the range and the
27284 whole range of define/undef ops is not emitted and kept. */
27285 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27286 {
27287 switch (ref->code)
27288 {
27289 case DW_MACINFO_start_file:
27290 vec_safe_push (files, *ref);
27291 break;
27292 case DW_MACINFO_end_file:
27293 if (!vec_safe_is_empty (files))
27294 files->pop ();
27295 break;
27296 case DW_MACINFO_define:
27297 case DW_MACINFO_undef:
27298 if ((!dwarf_strict || dwarf_version >= 5)
27299 && HAVE_COMDAT_GROUP
27300 && vec_safe_length (files) != 1
27301 && i > 0
27302 && i + 1 < length
27303 && (*macinfo_table)[i - 1].code == 0)
27304 {
27305 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27306 if (count)
27307 {
27308 i += count - 1;
27309 continue;
27310 }
27311 }
27312 break;
27313 case 0:
27314 /* A dummy entry may be inserted at the beginning to be able
27315 to optimize the whole block of predefined macros. */
27316 if (i == 0)
27317 continue;
27318 default:
27319 break;
27320 }
27321 output_macinfo_op (ref);
27322 ref->info = NULL;
27323 ref->code = 0;
27324 }
27325
27326 if (!macinfo_htab)
27327 return;
27328
27329 /* Save the number of transparent includes so we can adjust the
27330 label number for the fat LTO object DWARF. */
27331 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27332
27333 delete macinfo_htab;
27334 macinfo_htab = NULL;
27335
27336 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27337 terminate the current chain and switch to a new comdat .debug_macinfo
27338 section and emit the define/undef entries within it. */
27339 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27340 switch (ref->code)
27341 {
27342 case 0:
27343 continue;
27344 case DW_MACRO_import:
27345 {
27346 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27347 tree comdat_key = get_identifier (ref->info);
27348 /* Terminate the previous .debug_macinfo section. */
27349 dw2_asm_output_data (1, 0, "End compilation unit");
27350 targetm.asm_out.named_section (debug_macinfo_section_name,
27351 SECTION_DEBUG
27352 | SECTION_LINKONCE
27353 | (early_lto_debug
27354 ? SECTION_EXCLUDE : 0),
27355 comdat_key);
27356 ASM_GENERATE_INTERNAL_LABEL (label,
27357 DEBUG_MACRO_SECTION_LABEL,
27358 ref->lineno + macinfo_label_base);
27359 ASM_OUTPUT_LABEL (asm_out_file, label);
27360 ref->code = 0;
27361 ref->info = NULL;
27362 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27363 "DWARF macro version number");
27364 if (DWARF_OFFSET_SIZE == 8)
27365 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27366 else
27367 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27368 }
27369 break;
27370 case DW_MACINFO_define:
27371 case DW_MACINFO_undef:
27372 output_macinfo_op (ref);
27373 ref->code = 0;
27374 ref->info = NULL;
27375 break;
27376 default:
27377 gcc_unreachable ();
27378 }
27379
27380 macinfo_label_base += macinfo_label_base_adj;
27381 }
27382
27383 /* Initialize the various sections and labels for dwarf output and prefix
27384 them with PREFIX if non-NULL. Returns the generation (zero based
27385 number of times function was called). */
27386
27387 static unsigned
27388 init_sections_and_labels (bool early_lto_debug)
27389 {
27390 /* As we may get called multiple times have a generation count for
27391 labels. */
27392 static unsigned generation = 0;
27393
27394 if (early_lto_debug)
27395 {
27396 if (!dwarf_split_debug_info)
27397 {
27398 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27399 SECTION_DEBUG | SECTION_EXCLUDE,
27400 NULL);
27401 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27402 SECTION_DEBUG | SECTION_EXCLUDE,
27403 NULL);
27404 debug_macinfo_section_name
27405 = ((dwarf_strict && dwarf_version < 5)
27406 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27407 debug_macinfo_section = get_section (debug_macinfo_section_name,
27408 SECTION_DEBUG
27409 | SECTION_EXCLUDE, NULL);
27410 /* For macro info we have to refer to a debug_line section, so
27411 similar to split-dwarf emit a skeleton one for early debug. */
27412 debug_skeleton_line_section
27413 = get_section (DEBUG_LTO_LINE_SECTION,
27414 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27415 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27416 DEBUG_SKELETON_LINE_SECTION_LABEL,
27417 generation);
27418 }
27419 else
27420 {
27421 /* ??? Which of the following do we need early? */
27422 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27423 SECTION_DEBUG | SECTION_EXCLUDE,
27424 NULL);
27425 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27426 SECTION_DEBUG | SECTION_EXCLUDE,
27427 NULL);
27428 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27429 SECTION_DEBUG
27430 | SECTION_EXCLUDE, NULL);
27431 debug_skeleton_abbrev_section
27432 = get_section (DEBUG_LTO_ABBREV_SECTION,
27433 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27434 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27435 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27436 generation);
27437
27438 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27439 stay in the main .o, but the skeleton_line goes into the split
27440 off dwo. */
27441 debug_skeleton_line_section
27442 = get_section (DEBUG_LTO_LINE_SECTION,
27443 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27444 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27445 DEBUG_SKELETON_LINE_SECTION_LABEL,
27446 generation);
27447 debug_str_offsets_section
27448 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27449 SECTION_DEBUG | SECTION_EXCLUDE,
27450 NULL);
27451 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27452 DEBUG_SKELETON_INFO_SECTION_LABEL,
27453 generation);
27454 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27455 DEBUG_STR_DWO_SECTION_FLAGS,
27456 NULL);
27457 debug_macinfo_section_name
27458 = ((dwarf_strict && dwarf_version < 5)
27459 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27460 debug_macinfo_section = get_section (debug_macinfo_section_name,
27461 SECTION_DEBUG | SECTION_EXCLUDE,
27462 NULL);
27463 }
27464 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27465 DEBUG_STR_SECTION_FLAGS
27466 | SECTION_EXCLUDE, NULL);
27467 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27468 debug_line_str_section
27469 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27470 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27471 }
27472 else
27473 {
27474 if (!dwarf_split_debug_info)
27475 {
27476 debug_info_section = get_section (DEBUG_INFO_SECTION,
27477 SECTION_DEBUG, NULL);
27478 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27479 SECTION_DEBUG, NULL);
27480 debug_loc_section = get_section (dwarf_version >= 5
27481 ? DEBUG_LOCLISTS_SECTION
27482 : DEBUG_LOC_SECTION,
27483 SECTION_DEBUG, NULL);
27484 debug_macinfo_section_name
27485 = ((dwarf_strict && dwarf_version < 5)
27486 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27487 debug_macinfo_section = get_section (debug_macinfo_section_name,
27488 SECTION_DEBUG, NULL);
27489 }
27490 else
27491 {
27492 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27493 SECTION_DEBUG | SECTION_EXCLUDE,
27494 NULL);
27495 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27496 SECTION_DEBUG | SECTION_EXCLUDE,
27497 NULL);
27498 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27499 SECTION_DEBUG, NULL);
27500 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27501 SECTION_DEBUG, NULL);
27502 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27503 SECTION_DEBUG, NULL);
27504 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27505 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27506 generation);
27507
27508 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27509 stay in the main .o, but the skeleton_line goes into the
27510 split off dwo. */
27511 debug_skeleton_line_section
27512 = get_section (DEBUG_DWO_LINE_SECTION,
27513 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27514 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27515 DEBUG_SKELETON_LINE_SECTION_LABEL,
27516 generation);
27517 debug_str_offsets_section
27518 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27519 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27520 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27521 DEBUG_SKELETON_INFO_SECTION_LABEL,
27522 generation);
27523 debug_loc_section = get_section (dwarf_version >= 5
27524 ? DEBUG_DWO_LOCLISTS_SECTION
27525 : DEBUG_DWO_LOC_SECTION,
27526 SECTION_DEBUG | SECTION_EXCLUDE,
27527 NULL);
27528 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27529 DEBUG_STR_DWO_SECTION_FLAGS,
27530 NULL);
27531 debug_macinfo_section_name
27532 = ((dwarf_strict && dwarf_version < 5)
27533 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27534 debug_macinfo_section = get_section (debug_macinfo_section_name,
27535 SECTION_DEBUG | SECTION_EXCLUDE,
27536 NULL);
27537 }
27538 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27539 SECTION_DEBUG, NULL);
27540 debug_line_section = get_section (DEBUG_LINE_SECTION,
27541 SECTION_DEBUG, NULL);
27542 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27543 SECTION_DEBUG, NULL);
27544 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27545 SECTION_DEBUG, NULL);
27546 debug_str_section = get_section (DEBUG_STR_SECTION,
27547 DEBUG_STR_SECTION_FLAGS, NULL);
27548 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27549 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27550 DEBUG_STR_SECTION_FLAGS, NULL);
27551 debug_ranges_section = get_section (dwarf_version >= 5
27552 ? DEBUG_RNGLISTS_SECTION
27553 : DEBUG_RANGES_SECTION,
27554 SECTION_DEBUG, NULL);
27555 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27556 SECTION_DEBUG, NULL);
27557 }
27558
27559 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27560 DEBUG_ABBREV_SECTION_LABEL, generation);
27561 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27562 DEBUG_INFO_SECTION_LABEL, generation);
27563 info_section_emitted = false;
27564 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27565 DEBUG_LINE_SECTION_LABEL, generation);
27566 /* There are up to 4 unique ranges labels per generation.
27567 See also output_rnglists. */
27568 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27569 DEBUG_RANGES_SECTION_LABEL, generation * 4);
27570 if (dwarf_version >= 5 && dwarf_split_debug_info)
27571 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27572 DEBUG_RANGES_SECTION_LABEL,
27573 1 + generation * 4);
27574 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27575 DEBUG_ADDR_SECTION_LABEL, generation);
27576 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27577 (dwarf_strict && dwarf_version < 5)
27578 ? DEBUG_MACINFO_SECTION_LABEL
27579 : DEBUG_MACRO_SECTION_LABEL, generation);
27580 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27581 generation);
27582
27583 ++generation;
27584 return generation - 1;
27585 }
27586
27587 /* Set up for Dwarf output at the start of compilation. */
27588
27589 static void
27590 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27591 {
27592 /* Allocate the file_table. */
27593 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27594
27595 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27596 /* Allocate the decl_die_table. */
27597 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27598
27599 /* Allocate the decl_loc_table. */
27600 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27601
27602 /* Allocate the cached_dw_loc_list_table. */
27603 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27604
27605 /* Allocate the initial hunk of the decl_scope_table. */
27606 vec_alloc (decl_scope_table, 256);
27607
27608 /* Allocate the initial hunk of the abbrev_die_table. */
27609 vec_alloc (abbrev_die_table, 256);
27610 /* Zero-th entry is allocated, but unused. */
27611 abbrev_die_table->quick_push (NULL);
27612
27613 /* Allocate the dwarf_proc_stack_usage_map. */
27614 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27615
27616 /* Allocate the pubtypes and pubnames vectors. */
27617 vec_alloc (pubname_table, 32);
27618 vec_alloc (pubtype_table, 32);
27619
27620 vec_alloc (incomplete_types, 64);
27621
27622 vec_alloc (used_rtx_array, 32);
27623
27624 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27625 vec_alloc (macinfo_table, 64);
27626 #endif
27627
27628 /* If front-ends already registered a main translation unit but we were not
27629 ready to perform the association, do this now. */
27630 if (main_translation_unit != NULL_TREE)
27631 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27632 }
27633
27634 /* Called before compile () starts outputtting functions, variables
27635 and toplevel asms into assembly. */
27636
27637 static void
27638 dwarf2out_assembly_start (void)
27639 {
27640 if (text_section_line_info)
27641 return;
27642
27643 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27644 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27645 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27646 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27647 COLD_TEXT_SECTION_LABEL, 0);
27648 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27649
27650 switch_to_section (text_section);
27651 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27652 #endif
27653
27654 /* Make sure the line number table for .text always exists. */
27655 text_section_line_info = new_line_info_table ();
27656 text_section_line_info->end_label = text_end_label;
27657
27658 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27659 cur_line_info_table = text_section_line_info;
27660 #endif
27661
27662 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27663 && dwarf2out_do_cfi_asm ()
27664 && !dwarf2out_do_eh_frame ())
27665 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27666 }
27667
27668 /* A helper function for dwarf2out_finish called through
27669 htab_traverse. Assign a string its index. All strings must be
27670 collected into the table by the time index_string is called,
27671 because the indexing code relies on htab_traverse to traverse nodes
27672 in the same order for each run. */
27673
27674 int
27675 index_string (indirect_string_node **h, unsigned int *index)
27676 {
27677 indirect_string_node *node = *h;
27678
27679 find_string_form (node);
27680 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27681 {
27682 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27683 node->index = *index;
27684 *index += 1;
27685 }
27686 return 1;
27687 }
27688
27689 /* A helper function for output_indirect_strings called through
27690 htab_traverse. Output the offset to a string and update the
27691 current offset. */
27692
27693 int
27694 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27695 {
27696 indirect_string_node *node = *h;
27697
27698 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27699 {
27700 /* Assert that this node has been assigned an index. */
27701 gcc_assert (node->index != NO_INDEX_ASSIGNED
27702 && node->index != NOT_INDEXED);
27703 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27704 "indexed string 0x%x: %s", node->index, node->str);
27705 *offset += strlen (node->str) + 1;
27706 }
27707 return 1;
27708 }
27709
27710 /* A helper function for dwarf2out_finish called through
27711 htab_traverse. Output the indexed string. */
27712
27713 int
27714 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27715 {
27716 struct indirect_string_node *node = *h;
27717
27718 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27719 {
27720 /* Assert that the strings are output in the same order as their
27721 indexes were assigned. */
27722 gcc_assert (*cur_idx == node->index);
27723 assemble_string (node->str, strlen (node->str) + 1);
27724 *cur_idx += 1;
27725 }
27726 return 1;
27727 }
27728
27729 /* A helper function for dwarf2out_finish called through
27730 htab_traverse. Emit one queued .debug_str string. */
27731
27732 int
27733 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27734 {
27735 struct indirect_string_node *node = *h;
27736
27737 node->form = find_string_form (node);
27738 if (node->form == form && node->refcount > 0)
27739 {
27740 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27741 assemble_string (node->str, strlen (node->str) + 1);
27742 }
27743
27744 return 1;
27745 }
27746
27747 /* Output the indexed string table. */
27748
27749 static void
27750 output_indirect_strings (void)
27751 {
27752 switch_to_section (debug_str_section);
27753 if (!dwarf_split_debug_info)
27754 debug_str_hash->traverse<enum dwarf_form,
27755 output_indirect_string> (DW_FORM_strp);
27756 else
27757 {
27758 unsigned int offset = 0;
27759 unsigned int cur_idx = 0;
27760
27761 skeleton_debug_str_hash->traverse<enum dwarf_form,
27762 output_indirect_string> (DW_FORM_strp);
27763
27764 switch_to_section (debug_str_offsets_section);
27765 debug_str_hash->traverse_noresize
27766 <unsigned int *, output_index_string_offset> (&offset);
27767 switch_to_section (debug_str_dwo_section);
27768 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27769 (&cur_idx);
27770 }
27771 }
27772
27773 /* Callback for htab_traverse to assign an index to an entry in the
27774 table, and to write that entry to the .debug_addr section. */
27775
27776 int
27777 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27778 {
27779 addr_table_entry *entry = *slot;
27780
27781 if (entry->refcount == 0)
27782 {
27783 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27784 || entry->index == NOT_INDEXED);
27785 return 1;
27786 }
27787
27788 gcc_assert (entry->index == *cur_index);
27789 (*cur_index)++;
27790
27791 switch (entry->kind)
27792 {
27793 case ate_kind_rtx:
27794 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27795 "0x%x", entry->index);
27796 break;
27797 case ate_kind_rtx_dtprel:
27798 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27799 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27800 DWARF2_ADDR_SIZE,
27801 entry->addr.rtl);
27802 fputc ('\n', asm_out_file);
27803 break;
27804 case ate_kind_label:
27805 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27806 "0x%x", entry->index);
27807 break;
27808 default:
27809 gcc_unreachable ();
27810 }
27811 return 1;
27812 }
27813
27814 /* Produce the .debug_addr section. */
27815
27816 static void
27817 output_addr_table (void)
27818 {
27819 unsigned int index = 0;
27820 if (addr_index_table == NULL || addr_index_table->size () == 0)
27821 return;
27822
27823 switch_to_section (debug_addr_section);
27824 addr_index_table
27825 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27826 }
27827
27828 #if ENABLE_ASSERT_CHECKING
27829 /* Verify that all marks are clear. */
27830
27831 static void
27832 verify_marks_clear (dw_die_ref die)
27833 {
27834 dw_die_ref c;
27835
27836 gcc_assert (! die->die_mark);
27837 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27838 }
27839 #endif /* ENABLE_ASSERT_CHECKING */
27840
27841 /* Clear the marks for a die and its children.
27842 Be cool if the mark isn't set. */
27843
27844 static void
27845 prune_unmark_dies (dw_die_ref die)
27846 {
27847 dw_die_ref c;
27848
27849 if (die->die_mark)
27850 die->die_mark = 0;
27851 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27852 }
27853
27854 /* Given LOC that is referenced by a DIE we're marking as used, find all
27855 referenced DWARF procedures it references and mark them as used. */
27856
27857 static void
27858 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27859 {
27860 for (; loc != NULL; loc = loc->dw_loc_next)
27861 switch (loc->dw_loc_opc)
27862 {
27863 case DW_OP_implicit_pointer:
27864 case DW_OP_convert:
27865 case DW_OP_reinterpret:
27866 case DW_OP_GNU_implicit_pointer:
27867 case DW_OP_GNU_convert:
27868 case DW_OP_GNU_reinterpret:
27869 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27870 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27871 break;
27872 case DW_OP_GNU_variable_value:
27873 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27874 {
27875 dw_die_ref ref
27876 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27877 if (ref == NULL)
27878 break;
27879 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27880 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27881 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27882 }
27883 /* FALLTHRU */
27884 case DW_OP_call2:
27885 case DW_OP_call4:
27886 case DW_OP_call_ref:
27887 case DW_OP_const_type:
27888 case DW_OP_GNU_const_type:
27889 case DW_OP_GNU_parameter_ref:
27890 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27891 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27892 break;
27893 case DW_OP_regval_type:
27894 case DW_OP_deref_type:
27895 case DW_OP_GNU_regval_type:
27896 case DW_OP_GNU_deref_type:
27897 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27898 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27899 break;
27900 case DW_OP_entry_value:
27901 case DW_OP_GNU_entry_value:
27902 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27903 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27904 break;
27905 default:
27906 break;
27907 }
27908 }
27909
27910 /* Given DIE that we're marking as used, find any other dies
27911 it references as attributes and mark them as used. */
27912
27913 static void
27914 prune_unused_types_walk_attribs (dw_die_ref die)
27915 {
27916 dw_attr_node *a;
27917 unsigned ix;
27918
27919 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27920 {
27921 switch (AT_class (a))
27922 {
27923 /* Make sure DWARF procedures referenced by location descriptions will
27924 get emitted. */
27925 case dw_val_class_loc:
27926 prune_unused_types_walk_loc_descr (AT_loc (a));
27927 break;
27928 case dw_val_class_loc_list:
27929 for (dw_loc_list_ref list = AT_loc_list (a);
27930 list != NULL;
27931 list = list->dw_loc_next)
27932 prune_unused_types_walk_loc_descr (list->expr);
27933 break;
27934
27935 case dw_val_class_die_ref:
27936 /* A reference to another DIE.
27937 Make sure that it will get emitted.
27938 If it was broken out into a comdat group, don't follow it. */
27939 if (! AT_ref (a)->comdat_type_p
27940 || a->dw_attr == DW_AT_specification)
27941 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27942 break;
27943
27944 case dw_val_class_str:
27945 /* Set the string's refcount to 0 so that prune_unused_types_mark
27946 accounts properly for it. */
27947 a->dw_attr_val.v.val_str->refcount = 0;
27948 break;
27949
27950 default:
27951 break;
27952 }
27953 }
27954 }
27955
27956 /* Mark the generic parameters and arguments children DIEs of DIE. */
27957
27958 static void
27959 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27960 {
27961 dw_die_ref c;
27962
27963 if (die == NULL || die->die_child == NULL)
27964 return;
27965 c = die->die_child;
27966 do
27967 {
27968 if (is_template_parameter (c))
27969 prune_unused_types_mark (c, 1);
27970 c = c->die_sib;
27971 } while (c && c != die->die_child);
27972 }
27973
27974 /* Mark DIE as being used. If DOKIDS is true, then walk down
27975 to DIE's children. */
27976
27977 static void
27978 prune_unused_types_mark (dw_die_ref die, int dokids)
27979 {
27980 dw_die_ref c;
27981
27982 if (die->die_mark == 0)
27983 {
27984 /* We haven't done this node yet. Mark it as used. */
27985 die->die_mark = 1;
27986 /* If this is the DIE of a generic type instantiation,
27987 mark the children DIEs that describe its generic parms and
27988 args. */
27989 prune_unused_types_mark_generic_parms_dies (die);
27990
27991 /* We also have to mark its parents as used.
27992 (But we don't want to mark our parent's kids due to this,
27993 unless it is a class.) */
27994 if (die->die_parent)
27995 prune_unused_types_mark (die->die_parent,
27996 class_scope_p (die->die_parent));
27997
27998 /* Mark any referenced nodes. */
27999 prune_unused_types_walk_attribs (die);
28000
28001 /* If this node is a specification,
28002 also mark the definition, if it exists. */
28003 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
28004 prune_unused_types_mark (die->die_definition, 1);
28005 }
28006
28007 if (dokids && die->die_mark != 2)
28008 {
28009 /* We need to walk the children, but haven't done so yet.
28010 Remember that we've walked the kids. */
28011 die->die_mark = 2;
28012
28013 /* If this is an array type, we need to make sure our
28014 kids get marked, even if they're types. If we're
28015 breaking out types into comdat sections, do this
28016 for all type definitions. */
28017 if (die->die_tag == DW_TAG_array_type
28018 || (use_debug_types
28019 && is_type_die (die) && ! is_declaration_die (die)))
28020 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
28021 else
28022 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28023 }
28024 }
28025
28026 /* For local classes, look if any static member functions were emitted
28027 and if so, mark them. */
28028
28029 static void
28030 prune_unused_types_walk_local_classes (dw_die_ref die)
28031 {
28032 dw_die_ref c;
28033
28034 if (die->die_mark == 2)
28035 return;
28036
28037 switch (die->die_tag)
28038 {
28039 case DW_TAG_structure_type:
28040 case DW_TAG_union_type:
28041 case DW_TAG_class_type:
28042 break;
28043
28044 case DW_TAG_subprogram:
28045 if (!get_AT_flag (die, DW_AT_declaration)
28046 || die->die_definition != NULL)
28047 prune_unused_types_mark (die, 1);
28048 return;
28049
28050 default:
28051 return;
28052 }
28053
28054 /* Mark children. */
28055 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
28056 }
28057
28058 /* Walk the tree DIE and mark types that we actually use. */
28059
28060 static void
28061 prune_unused_types_walk (dw_die_ref die)
28062 {
28063 dw_die_ref c;
28064
28065 /* Don't do anything if this node is already marked and
28066 children have been marked as well. */
28067 if (die->die_mark == 2)
28068 return;
28069
28070 switch (die->die_tag)
28071 {
28072 case DW_TAG_structure_type:
28073 case DW_TAG_union_type:
28074 case DW_TAG_class_type:
28075 if (die->die_perennial_p)
28076 break;
28077
28078 for (c = die->die_parent; c; c = c->die_parent)
28079 if (c->die_tag == DW_TAG_subprogram)
28080 break;
28081
28082 /* Finding used static member functions inside of classes
28083 is needed just for local classes, because for other classes
28084 static member function DIEs with DW_AT_specification
28085 are emitted outside of the DW_TAG_*_type. If we ever change
28086 it, we'd need to call this even for non-local classes. */
28087 if (c)
28088 prune_unused_types_walk_local_classes (die);
28089
28090 /* It's a type node --- don't mark it. */
28091 return;
28092
28093 case DW_TAG_const_type:
28094 case DW_TAG_packed_type:
28095 case DW_TAG_pointer_type:
28096 case DW_TAG_reference_type:
28097 case DW_TAG_rvalue_reference_type:
28098 case DW_TAG_volatile_type:
28099 case DW_TAG_typedef:
28100 case DW_TAG_array_type:
28101 case DW_TAG_interface_type:
28102 case DW_TAG_friend:
28103 case DW_TAG_enumeration_type:
28104 case DW_TAG_subroutine_type:
28105 case DW_TAG_string_type:
28106 case DW_TAG_set_type:
28107 case DW_TAG_subrange_type:
28108 case DW_TAG_ptr_to_member_type:
28109 case DW_TAG_file_type:
28110 /* Type nodes are useful only when other DIEs reference them --- don't
28111 mark them. */
28112 /* FALLTHROUGH */
28113
28114 case DW_TAG_dwarf_procedure:
28115 /* Likewise for DWARF procedures. */
28116
28117 if (die->die_perennial_p)
28118 break;
28119
28120 return;
28121
28122 default:
28123 /* Mark everything else. */
28124 break;
28125 }
28126
28127 if (die->die_mark == 0)
28128 {
28129 die->die_mark = 1;
28130
28131 /* Now, mark any dies referenced from here. */
28132 prune_unused_types_walk_attribs (die);
28133 }
28134
28135 die->die_mark = 2;
28136
28137 /* Mark children. */
28138 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28139 }
28140
28141 /* Increment the string counts on strings referred to from DIE's
28142 attributes. */
28143
28144 static void
28145 prune_unused_types_update_strings (dw_die_ref die)
28146 {
28147 dw_attr_node *a;
28148 unsigned ix;
28149
28150 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28151 if (AT_class (a) == dw_val_class_str)
28152 {
28153 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28154 s->refcount++;
28155 /* Avoid unnecessarily putting strings that are used less than
28156 twice in the hash table. */
28157 if (s->refcount
28158 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28159 {
28160 indirect_string_node **slot
28161 = debug_str_hash->find_slot_with_hash (s->str,
28162 htab_hash_string (s->str),
28163 INSERT);
28164 gcc_assert (*slot == NULL);
28165 *slot = s;
28166 }
28167 }
28168 }
28169
28170 /* Mark DIE and its children as removed. */
28171
28172 static void
28173 mark_removed (dw_die_ref die)
28174 {
28175 dw_die_ref c;
28176 die->removed = true;
28177 FOR_EACH_CHILD (die, c, mark_removed (c));
28178 }
28179
28180 /* Remove from the tree DIE any dies that aren't marked. */
28181
28182 static void
28183 prune_unused_types_prune (dw_die_ref die)
28184 {
28185 dw_die_ref c;
28186
28187 gcc_assert (die->die_mark);
28188 prune_unused_types_update_strings (die);
28189
28190 if (! die->die_child)
28191 return;
28192
28193 c = die->die_child;
28194 do {
28195 dw_die_ref prev = c, next;
28196 for (c = c->die_sib; ! c->die_mark; c = next)
28197 if (c == die->die_child)
28198 {
28199 /* No marked children between 'prev' and the end of the list. */
28200 if (prev == c)
28201 /* No marked children at all. */
28202 die->die_child = NULL;
28203 else
28204 {
28205 prev->die_sib = c->die_sib;
28206 die->die_child = prev;
28207 }
28208 c->die_sib = NULL;
28209 mark_removed (c);
28210 return;
28211 }
28212 else
28213 {
28214 next = c->die_sib;
28215 c->die_sib = NULL;
28216 mark_removed (c);
28217 }
28218
28219 if (c != prev->die_sib)
28220 prev->die_sib = c;
28221 prune_unused_types_prune (c);
28222 } while (c != die->die_child);
28223 }
28224
28225 /* Remove dies representing declarations that we never use. */
28226
28227 static void
28228 prune_unused_types (void)
28229 {
28230 unsigned int i;
28231 limbo_die_node *node;
28232 comdat_type_node *ctnode;
28233 pubname_entry *pub;
28234 dw_die_ref base_type;
28235
28236 #if ENABLE_ASSERT_CHECKING
28237 /* All the marks should already be clear. */
28238 verify_marks_clear (comp_unit_die ());
28239 for (node = limbo_die_list; node; node = node->next)
28240 verify_marks_clear (node->die);
28241 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28242 verify_marks_clear (ctnode->root_die);
28243 #endif /* ENABLE_ASSERT_CHECKING */
28244
28245 /* Mark types that are used in global variables. */
28246 premark_types_used_by_global_vars ();
28247
28248 /* Set the mark on nodes that are actually used. */
28249 prune_unused_types_walk (comp_unit_die ());
28250 for (node = limbo_die_list; node; node = node->next)
28251 prune_unused_types_walk (node->die);
28252 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28253 {
28254 prune_unused_types_walk (ctnode->root_die);
28255 prune_unused_types_mark (ctnode->type_die, 1);
28256 }
28257
28258 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28259 are unusual in that they are pubnames that are the children of pubtypes.
28260 They should only be marked via their parent DW_TAG_enumeration_type die,
28261 not as roots in themselves. */
28262 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28263 if (pub->die->die_tag != DW_TAG_enumerator)
28264 prune_unused_types_mark (pub->die, 1);
28265 for (i = 0; base_types.iterate (i, &base_type); i++)
28266 prune_unused_types_mark (base_type, 1);
28267
28268 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28269 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28270 callees). */
28271 cgraph_node *cnode;
28272 FOR_EACH_FUNCTION (cnode)
28273 if (cnode->referred_to_p (false))
28274 {
28275 dw_die_ref die = lookup_decl_die (cnode->decl);
28276 if (die == NULL || die->die_mark)
28277 continue;
28278 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28279 if (e->caller != cnode
28280 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28281 {
28282 prune_unused_types_mark (die, 1);
28283 break;
28284 }
28285 }
28286
28287 if (debug_str_hash)
28288 debug_str_hash->empty ();
28289 if (skeleton_debug_str_hash)
28290 skeleton_debug_str_hash->empty ();
28291 prune_unused_types_prune (comp_unit_die ());
28292 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28293 {
28294 node = *pnode;
28295 if (!node->die->die_mark)
28296 *pnode = node->next;
28297 else
28298 {
28299 prune_unused_types_prune (node->die);
28300 pnode = &node->next;
28301 }
28302 }
28303 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28304 prune_unused_types_prune (ctnode->root_die);
28305
28306 /* Leave the marks clear. */
28307 prune_unmark_dies (comp_unit_die ());
28308 for (node = limbo_die_list; node; node = node->next)
28309 prune_unmark_dies (node->die);
28310 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28311 prune_unmark_dies (ctnode->root_die);
28312 }
28313
28314 /* Helpers to manipulate hash table of comdat type units. */
28315
28316 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28317 {
28318 static inline hashval_t hash (const comdat_type_node *);
28319 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28320 };
28321
28322 inline hashval_t
28323 comdat_type_hasher::hash (const comdat_type_node *type_node)
28324 {
28325 hashval_t h;
28326 memcpy (&h, type_node->signature, sizeof (h));
28327 return h;
28328 }
28329
28330 inline bool
28331 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28332 const comdat_type_node *type_node_2)
28333 {
28334 return (! memcmp (type_node_1->signature, type_node_2->signature,
28335 DWARF_TYPE_SIGNATURE_SIZE));
28336 }
28337
28338 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28339 to the location it would have been added, should we know its
28340 DECL_ASSEMBLER_NAME when we added other attributes. This will
28341 probably improve compactness of debug info, removing equivalent
28342 abbrevs, and hide any differences caused by deferring the
28343 computation of the assembler name, triggered by e.g. PCH. */
28344
28345 static inline void
28346 move_linkage_attr (dw_die_ref die)
28347 {
28348 unsigned ix = vec_safe_length (die->die_attr);
28349 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28350
28351 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28352 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28353
28354 while (--ix > 0)
28355 {
28356 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28357
28358 if (prev->dw_attr == DW_AT_decl_line
28359 || prev->dw_attr == DW_AT_decl_column
28360 || prev->dw_attr == DW_AT_name)
28361 break;
28362 }
28363
28364 if (ix != vec_safe_length (die->die_attr) - 1)
28365 {
28366 die->die_attr->pop ();
28367 die->die_attr->quick_insert (ix, linkage);
28368 }
28369 }
28370
28371 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28372 referenced from typed stack ops and count how often they are used. */
28373
28374 static void
28375 mark_base_types (dw_loc_descr_ref loc)
28376 {
28377 dw_die_ref base_type = NULL;
28378
28379 for (; loc; loc = loc->dw_loc_next)
28380 {
28381 switch (loc->dw_loc_opc)
28382 {
28383 case DW_OP_regval_type:
28384 case DW_OP_deref_type:
28385 case DW_OP_GNU_regval_type:
28386 case DW_OP_GNU_deref_type:
28387 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28388 break;
28389 case DW_OP_convert:
28390 case DW_OP_reinterpret:
28391 case DW_OP_GNU_convert:
28392 case DW_OP_GNU_reinterpret:
28393 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28394 continue;
28395 /* FALLTHRU */
28396 case DW_OP_const_type:
28397 case DW_OP_GNU_const_type:
28398 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28399 break;
28400 case DW_OP_entry_value:
28401 case DW_OP_GNU_entry_value:
28402 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28403 continue;
28404 default:
28405 continue;
28406 }
28407 gcc_assert (base_type->die_parent == comp_unit_die ());
28408 if (base_type->die_mark)
28409 base_type->die_mark++;
28410 else
28411 {
28412 base_types.safe_push (base_type);
28413 base_type->die_mark = 1;
28414 }
28415 }
28416 }
28417
28418 /* Comparison function for sorting marked base types. */
28419
28420 static int
28421 base_type_cmp (const void *x, const void *y)
28422 {
28423 dw_die_ref dx = *(const dw_die_ref *) x;
28424 dw_die_ref dy = *(const dw_die_ref *) y;
28425 unsigned int byte_size1, byte_size2;
28426 unsigned int encoding1, encoding2;
28427 unsigned int align1, align2;
28428 if (dx->die_mark > dy->die_mark)
28429 return -1;
28430 if (dx->die_mark < dy->die_mark)
28431 return 1;
28432 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28433 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28434 if (byte_size1 < byte_size2)
28435 return 1;
28436 if (byte_size1 > byte_size2)
28437 return -1;
28438 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28439 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28440 if (encoding1 < encoding2)
28441 return 1;
28442 if (encoding1 > encoding2)
28443 return -1;
28444 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28445 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28446 if (align1 < align2)
28447 return 1;
28448 if (align1 > align2)
28449 return -1;
28450 return 0;
28451 }
28452
28453 /* Move base types marked by mark_base_types as early as possible
28454 in the CU, sorted by decreasing usage count both to make the
28455 uleb128 references as small as possible and to make sure they
28456 will have die_offset already computed by calc_die_sizes when
28457 sizes of typed stack loc ops is computed. */
28458
28459 static void
28460 move_marked_base_types (void)
28461 {
28462 unsigned int i;
28463 dw_die_ref base_type, die, c;
28464
28465 if (base_types.is_empty ())
28466 return;
28467
28468 /* Sort by decreasing usage count, they will be added again in that
28469 order later on. */
28470 base_types.qsort (base_type_cmp);
28471 die = comp_unit_die ();
28472 c = die->die_child;
28473 do
28474 {
28475 dw_die_ref prev = c;
28476 c = c->die_sib;
28477 while (c->die_mark)
28478 {
28479 remove_child_with_prev (c, prev);
28480 /* As base types got marked, there must be at least
28481 one node other than DW_TAG_base_type. */
28482 gcc_assert (die->die_child != NULL);
28483 c = prev->die_sib;
28484 }
28485 }
28486 while (c != die->die_child);
28487 gcc_assert (die->die_child);
28488 c = die->die_child;
28489 for (i = 0; base_types.iterate (i, &base_type); i++)
28490 {
28491 base_type->die_mark = 0;
28492 base_type->die_sib = c->die_sib;
28493 c->die_sib = base_type;
28494 c = base_type;
28495 }
28496 }
28497
28498 /* Helper function for resolve_addr, attempt to resolve
28499 one CONST_STRING, return true if successful. Similarly verify that
28500 SYMBOL_REFs refer to variables emitted in the current CU. */
28501
28502 static bool
28503 resolve_one_addr (rtx *addr)
28504 {
28505 rtx rtl = *addr;
28506
28507 if (GET_CODE (rtl) == CONST_STRING)
28508 {
28509 size_t len = strlen (XSTR (rtl, 0)) + 1;
28510 tree t = build_string (len, XSTR (rtl, 0));
28511 tree tlen = size_int (len - 1);
28512 TREE_TYPE (t)
28513 = build_array_type (char_type_node, build_index_type (tlen));
28514 rtl = lookup_constant_def (t);
28515 if (!rtl || !MEM_P (rtl))
28516 return false;
28517 rtl = XEXP (rtl, 0);
28518 if (GET_CODE (rtl) == SYMBOL_REF
28519 && SYMBOL_REF_DECL (rtl)
28520 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28521 return false;
28522 vec_safe_push (used_rtx_array, rtl);
28523 *addr = rtl;
28524 return true;
28525 }
28526
28527 if (GET_CODE (rtl) == SYMBOL_REF
28528 && SYMBOL_REF_DECL (rtl))
28529 {
28530 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28531 {
28532 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28533 return false;
28534 }
28535 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28536 return false;
28537 }
28538
28539 if (GET_CODE (rtl) == CONST)
28540 {
28541 subrtx_ptr_iterator::array_type array;
28542 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28543 if (!resolve_one_addr (*iter))
28544 return false;
28545 }
28546
28547 return true;
28548 }
28549
28550 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28551 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28552 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28553
28554 static rtx
28555 string_cst_pool_decl (tree t)
28556 {
28557 rtx rtl = output_constant_def (t, 1);
28558 unsigned char *array;
28559 dw_loc_descr_ref l;
28560 tree decl;
28561 size_t len;
28562 dw_die_ref ref;
28563
28564 if (!rtl || !MEM_P (rtl))
28565 return NULL_RTX;
28566 rtl = XEXP (rtl, 0);
28567 if (GET_CODE (rtl) != SYMBOL_REF
28568 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28569 return NULL_RTX;
28570
28571 decl = SYMBOL_REF_DECL (rtl);
28572 if (!lookup_decl_die (decl))
28573 {
28574 len = TREE_STRING_LENGTH (t);
28575 vec_safe_push (used_rtx_array, rtl);
28576 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28577 array = ggc_vec_alloc<unsigned char> (len);
28578 memcpy (array, TREE_STRING_POINTER (t), len);
28579 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28580 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28581 l->dw_loc_oprnd2.v.val_vec.length = len;
28582 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28583 l->dw_loc_oprnd2.v.val_vec.array = array;
28584 add_AT_loc (ref, DW_AT_location, l);
28585 equate_decl_number_to_die (decl, ref);
28586 }
28587 return rtl;
28588 }
28589
28590 /* Helper function of resolve_addr_in_expr. LOC is
28591 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28592 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28593 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28594 with DW_OP_implicit_pointer if possible
28595 and return true, if unsuccessful, return false. */
28596
28597 static bool
28598 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28599 {
28600 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28601 HOST_WIDE_INT offset = 0;
28602 dw_die_ref ref = NULL;
28603 tree decl;
28604
28605 if (GET_CODE (rtl) == CONST
28606 && GET_CODE (XEXP (rtl, 0)) == PLUS
28607 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28608 {
28609 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28610 rtl = XEXP (XEXP (rtl, 0), 0);
28611 }
28612 if (GET_CODE (rtl) == CONST_STRING)
28613 {
28614 size_t len = strlen (XSTR (rtl, 0)) + 1;
28615 tree t = build_string (len, XSTR (rtl, 0));
28616 tree tlen = size_int (len - 1);
28617
28618 TREE_TYPE (t)
28619 = build_array_type (char_type_node, build_index_type (tlen));
28620 rtl = string_cst_pool_decl (t);
28621 if (!rtl)
28622 return false;
28623 }
28624 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28625 {
28626 decl = SYMBOL_REF_DECL (rtl);
28627 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28628 {
28629 ref = lookup_decl_die (decl);
28630 if (ref && (get_AT (ref, DW_AT_location)
28631 || get_AT (ref, DW_AT_const_value)))
28632 {
28633 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28634 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28635 loc->dw_loc_oprnd1.val_entry = NULL;
28636 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28637 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28638 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28639 loc->dw_loc_oprnd2.v.val_int = offset;
28640 return true;
28641 }
28642 }
28643 }
28644 return false;
28645 }
28646
28647 /* Helper function for resolve_addr, handle one location
28648 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28649 the location list couldn't be resolved. */
28650
28651 static bool
28652 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28653 {
28654 dw_loc_descr_ref keep = NULL;
28655 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28656 switch (loc->dw_loc_opc)
28657 {
28658 case DW_OP_addr:
28659 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28660 {
28661 if ((prev == NULL
28662 || prev->dw_loc_opc == DW_OP_piece
28663 || prev->dw_loc_opc == DW_OP_bit_piece)
28664 && loc->dw_loc_next
28665 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28666 && (!dwarf_strict || dwarf_version >= 5)
28667 && optimize_one_addr_into_implicit_ptr (loc))
28668 break;
28669 return false;
28670 }
28671 break;
28672 case DW_OP_GNU_addr_index:
28673 case DW_OP_GNU_const_index:
28674 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28675 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28676 {
28677 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28678 if (!resolve_one_addr (&rtl))
28679 return false;
28680 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28681 loc->dw_loc_oprnd1.val_entry
28682 = add_addr_table_entry (rtl, ate_kind_rtx);
28683 }
28684 break;
28685 case DW_OP_const4u:
28686 case DW_OP_const8u:
28687 if (loc->dtprel
28688 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28689 return false;
28690 break;
28691 case DW_OP_plus_uconst:
28692 if (size_of_loc_descr (loc)
28693 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28694 + 1
28695 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28696 {
28697 dw_loc_descr_ref repl
28698 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28699 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28700 add_loc_descr (&repl, loc->dw_loc_next);
28701 *loc = *repl;
28702 }
28703 break;
28704 case DW_OP_implicit_value:
28705 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28706 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28707 return false;
28708 break;
28709 case DW_OP_implicit_pointer:
28710 case DW_OP_GNU_implicit_pointer:
28711 case DW_OP_GNU_parameter_ref:
28712 case DW_OP_GNU_variable_value:
28713 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28714 {
28715 dw_die_ref ref
28716 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28717 if (ref == NULL)
28718 return false;
28719 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28720 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28721 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28722 }
28723 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28724 {
28725 if (prev == NULL
28726 && loc->dw_loc_next == NULL
28727 && AT_class (a) == dw_val_class_loc)
28728 switch (a->dw_attr)
28729 {
28730 /* Following attributes allow both exprloc and reference,
28731 so if the whole expression is DW_OP_GNU_variable_value
28732 alone we could transform it into reference. */
28733 case DW_AT_byte_size:
28734 case DW_AT_bit_size:
28735 case DW_AT_lower_bound:
28736 case DW_AT_upper_bound:
28737 case DW_AT_bit_stride:
28738 case DW_AT_count:
28739 case DW_AT_allocated:
28740 case DW_AT_associated:
28741 case DW_AT_byte_stride:
28742 a->dw_attr_val.val_class = dw_val_class_die_ref;
28743 a->dw_attr_val.val_entry = NULL;
28744 a->dw_attr_val.v.val_die_ref.die
28745 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28746 a->dw_attr_val.v.val_die_ref.external = 0;
28747 return true;
28748 default:
28749 break;
28750 }
28751 if (dwarf_strict)
28752 return false;
28753 }
28754 break;
28755 case DW_OP_const_type:
28756 case DW_OP_regval_type:
28757 case DW_OP_deref_type:
28758 case DW_OP_convert:
28759 case DW_OP_reinterpret:
28760 case DW_OP_GNU_const_type:
28761 case DW_OP_GNU_regval_type:
28762 case DW_OP_GNU_deref_type:
28763 case DW_OP_GNU_convert:
28764 case DW_OP_GNU_reinterpret:
28765 while (loc->dw_loc_next
28766 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28767 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28768 {
28769 dw_die_ref base1, base2;
28770 unsigned enc1, enc2, size1, size2;
28771 if (loc->dw_loc_opc == DW_OP_regval_type
28772 || loc->dw_loc_opc == DW_OP_deref_type
28773 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28774 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28775 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28776 else if (loc->dw_loc_oprnd1.val_class
28777 == dw_val_class_unsigned_const)
28778 break;
28779 else
28780 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28781 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28782 == dw_val_class_unsigned_const)
28783 break;
28784 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28785 gcc_assert (base1->die_tag == DW_TAG_base_type
28786 && base2->die_tag == DW_TAG_base_type);
28787 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28788 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28789 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28790 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28791 if (size1 == size2
28792 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28793 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28794 && loc != keep)
28795 || enc1 == enc2))
28796 {
28797 /* Optimize away next DW_OP_convert after
28798 adjusting LOC's base type die reference. */
28799 if (loc->dw_loc_opc == DW_OP_regval_type
28800 || loc->dw_loc_opc == DW_OP_deref_type
28801 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28802 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28803 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28804 else
28805 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28806 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28807 continue;
28808 }
28809 /* Don't change integer DW_OP_convert after e.g. floating
28810 point typed stack entry. */
28811 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28812 keep = loc->dw_loc_next;
28813 break;
28814 }
28815 break;
28816 default:
28817 break;
28818 }
28819 return true;
28820 }
28821
28822 /* Helper function of resolve_addr. DIE had DW_AT_location of
28823 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28824 and DW_OP_addr couldn't be resolved. resolve_addr has already
28825 removed the DW_AT_location attribute. This function attempts to
28826 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28827 to it or DW_AT_const_value attribute, if possible. */
28828
28829 static void
28830 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28831 {
28832 if (!VAR_P (decl)
28833 || lookup_decl_die (decl) != die
28834 || DECL_EXTERNAL (decl)
28835 || !TREE_STATIC (decl)
28836 || DECL_INITIAL (decl) == NULL_TREE
28837 || DECL_P (DECL_INITIAL (decl))
28838 || get_AT (die, DW_AT_const_value))
28839 return;
28840
28841 tree init = DECL_INITIAL (decl);
28842 HOST_WIDE_INT offset = 0;
28843 /* For variables that have been optimized away and thus
28844 don't have a memory location, see if we can emit
28845 DW_AT_const_value instead. */
28846 if (tree_add_const_value_attribute (die, init))
28847 return;
28848 if (dwarf_strict && dwarf_version < 5)
28849 return;
28850 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28851 and ADDR_EXPR refers to a decl that has DW_AT_location or
28852 DW_AT_const_value (but isn't addressable, otherwise
28853 resolving the original DW_OP_addr wouldn't fail), see if
28854 we can add DW_OP_implicit_pointer. */
28855 STRIP_NOPS (init);
28856 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28857 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28858 {
28859 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28860 init = TREE_OPERAND (init, 0);
28861 STRIP_NOPS (init);
28862 }
28863 if (TREE_CODE (init) != ADDR_EXPR)
28864 return;
28865 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28866 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28867 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28868 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28869 && TREE_OPERAND (init, 0) != decl))
28870 {
28871 dw_die_ref ref;
28872 dw_loc_descr_ref l;
28873
28874 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28875 {
28876 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28877 if (!rtl)
28878 return;
28879 decl = SYMBOL_REF_DECL (rtl);
28880 }
28881 else
28882 decl = TREE_OPERAND (init, 0);
28883 ref = lookup_decl_die (decl);
28884 if (ref == NULL
28885 || (!get_AT (ref, DW_AT_location)
28886 && !get_AT (ref, DW_AT_const_value)))
28887 return;
28888 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28889 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28890 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28891 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28892 add_AT_loc (die, DW_AT_location, l);
28893 }
28894 }
28895
28896 /* Return NULL if l is a DWARF expression, or first op that is not
28897 valid DWARF expression. */
28898
28899 static dw_loc_descr_ref
28900 non_dwarf_expression (dw_loc_descr_ref l)
28901 {
28902 while (l)
28903 {
28904 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28905 return l;
28906 switch (l->dw_loc_opc)
28907 {
28908 case DW_OP_regx:
28909 case DW_OP_implicit_value:
28910 case DW_OP_stack_value:
28911 case DW_OP_implicit_pointer:
28912 case DW_OP_GNU_implicit_pointer:
28913 case DW_OP_GNU_parameter_ref:
28914 case DW_OP_piece:
28915 case DW_OP_bit_piece:
28916 return l;
28917 default:
28918 break;
28919 }
28920 l = l->dw_loc_next;
28921 }
28922 return NULL;
28923 }
28924
28925 /* Return adjusted copy of EXPR:
28926 If it is empty DWARF expression, return it.
28927 If it is valid non-empty DWARF expression,
28928 return copy of EXPR with DW_OP_deref appended to it.
28929 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28930 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28931 If it is DWARF expression followed by DW_OP_stack_value, return
28932 copy of the DWARF expression without anything appended.
28933 Otherwise, return NULL. */
28934
28935 static dw_loc_descr_ref
28936 copy_deref_exprloc (dw_loc_descr_ref expr)
28937 {
28938 dw_loc_descr_ref tail = NULL;
28939
28940 if (expr == NULL)
28941 return NULL;
28942
28943 dw_loc_descr_ref l = non_dwarf_expression (expr);
28944 if (l && l->dw_loc_next)
28945 return NULL;
28946
28947 if (l)
28948 {
28949 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28950 tail = new_loc_descr ((enum dwarf_location_atom)
28951 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28952 0, 0);
28953 else
28954 switch (l->dw_loc_opc)
28955 {
28956 case DW_OP_regx:
28957 tail = new_loc_descr (DW_OP_bregx,
28958 l->dw_loc_oprnd1.v.val_unsigned, 0);
28959 break;
28960 case DW_OP_stack_value:
28961 break;
28962 default:
28963 return NULL;
28964 }
28965 }
28966 else
28967 tail = new_loc_descr (DW_OP_deref, 0, 0);
28968
28969 dw_loc_descr_ref ret = NULL, *p = &ret;
28970 while (expr != l)
28971 {
28972 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28973 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28974 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28975 p = &(*p)->dw_loc_next;
28976 expr = expr->dw_loc_next;
28977 }
28978 *p = tail;
28979 return ret;
28980 }
28981
28982 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28983 reference to a variable or argument, adjust it if needed and return:
28984 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28985 attribute if present should be removed
28986 0 keep the attribute perhaps with minor modifications, no need to rescan
28987 1 if the attribute has been successfully adjusted. */
28988
28989 static int
28990 optimize_string_length (dw_attr_node *a)
28991 {
28992 dw_loc_descr_ref l = AT_loc (a), lv;
28993 dw_die_ref die;
28994 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28995 {
28996 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28997 die = lookup_decl_die (decl);
28998 if (die)
28999 {
29000 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29001 l->dw_loc_oprnd1.v.val_die_ref.die = die;
29002 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29003 }
29004 else
29005 return -1;
29006 }
29007 else
29008 die = l->dw_loc_oprnd1.v.val_die_ref.die;
29009
29010 /* DWARF5 allows reference class, so we can then reference the DIE.
29011 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
29012 if (l->dw_loc_next != NULL && dwarf_version >= 5)
29013 {
29014 a->dw_attr_val.val_class = dw_val_class_die_ref;
29015 a->dw_attr_val.val_entry = NULL;
29016 a->dw_attr_val.v.val_die_ref.die = die;
29017 a->dw_attr_val.v.val_die_ref.external = 0;
29018 return 0;
29019 }
29020
29021 dw_attr_node *av = get_AT (die, DW_AT_location);
29022 dw_loc_list_ref d;
29023 bool non_dwarf_expr = false;
29024
29025 if (av == NULL)
29026 return dwarf_strict ? -1 : 0;
29027 switch (AT_class (av))
29028 {
29029 case dw_val_class_loc_list:
29030 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29031 if (d->expr && non_dwarf_expression (d->expr))
29032 non_dwarf_expr = true;
29033 break;
29034 case dw_val_class_loc:
29035 lv = AT_loc (av);
29036 if (lv == NULL)
29037 return dwarf_strict ? -1 : 0;
29038 if (non_dwarf_expression (lv))
29039 non_dwarf_expr = true;
29040 break;
29041 default:
29042 return dwarf_strict ? -1 : 0;
29043 }
29044
29045 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
29046 into DW_OP_call4 or DW_OP_GNU_variable_value into
29047 DW_OP_call4 DW_OP_deref, do so. */
29048 if (!non_dwarf_expr
29049 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
29050 {
29051 l->dw_loc_opc = DW_OP_call4;
29052 if (l->dw_loc_next)
29053 l->dw_loc_next = NULL;
29054 else
29055 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
29056 return 0;
29057 }
29058
29059 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
29060 copy over the DW_AT_location attribute from die to a. */
29061 if (l->dw_loc_next != NULL)
29062 {
29063 a->dw_attr_val = av->dw_attr_val;
29064 return 1;
29065 }
29066
29067 dw_loc_list_ref list, *p;
29068 switch (AT_class (av))
29069 {
29070 case dw_val_class_loc_list:
29071 p = &list;
29072 list = NULL;
29073 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29074 {
29075 lv = copy_deref_exprloc (d->expr);
29076 if (lv)
29077 {
29078 *p = new_loc_list (lv, d->begin, d->end, d->section);
29079 p = &(*p)->dw_loc_next;
29080 }
29081 else if (!dwarf_strict && d->expr)
29082 return 0;
29083 }
29084 if (list == NULL)
29085 return dwarf_strict ? -1 : 0;
29086 a->dw_attr_val.val_class = dw_val_class_loc_list;
29087 gen_llsym (list);
29088 *AT_loc_list_ptr (a) = list;
29089 return 1;
29090 case dw_val_class_loc:
29091 lv = copy_deref_exprloc (AT_loc (av));
29092 if (lv == NULL)
29093 return dwarf_strict ? -1 : 0;
29094 a->dw_attr_val.v.val_loc = lv;
29095 return 1;
29096 default:
29097 gcc_unreachable ();
29098 }
29099 }
29100
29101 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
29102 an address in .rodata section if the string literal is emitted there,
29103 or remove the containing location list or replace DW_AT_const_value
29104 with DW_AT_location and empty location expression, if it isn't found
29105 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
29106 to something that has been emitted in the current CU. */
29107
29108 static void
29109 resolve_addr (dw_die_ref die)
29110 {
29111 dw_die_ref c;
29112 dw_attr_node *a;
29113 dw_loc_list_ref *curr, *start, loc;
29114 unsigned ix;
29115 bool remove_AT_byte_size = false;
29116
29117 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29118 switch (AT_class (a))
29119 {
29120 case dw_val_class_loc_list:
29121 start = curr = AT_loc_list_ptr (a);
29122 loc = *curr;
29123 gcc_assert (loc);
29124 /* The same list can be referenced more than once. See if we have
29125 already recorded the result from a previous pass. */
29126 if (loc->replaced)
29127 *curr = loc->dw_loc_next;
29128 else if (!loc->resolved_addr)
29129 {
29130 /* As things stand, we do not expect or allow one die to
29131 reference a suffix of another die's location list chain.
29132 References must be identical or completely separate.
29133 There is therefore no need to cache the result of this
29134 pass on any list other than the first; doing so
29135 would lead to unnecessary writes. */
29136 while (*curr)
29137 {
29138 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29139 if (!resolve_addr_in_expr (a, (*curr)->expr))
29140 {
29141 dw_loc_list_ref next = (*curr)->dw_loc_next;
29142 dw_loc_descr_ref l = (*curr)->expr;
29143
29144 if (next && (*curr)->ll_symbol)
29145 {
29146 gcc_assert (!next->ll_symbol);
29147 next->ll_symbol = (*curr)->ll_symbol;
29148 }
29149 if (dwarf_split_debug_info)
29150 remove_loc_list_addr_table_entries (l);
29151 *curr = next;
29152 }
29153 else
29154 {
29155 mark_base_types ((*curr)->expr);
29156 curr = &(*curr)->dw_loc_next;
29157 }
29158 }
29159 if (loc == *start)
29160 loc->resolved_addr = 1;
29161 else
29162 {
29163 loc->replaced = 1;
29164 loc->dw_loc_next = *start;
29165 }
29166 }
29167 if (!*start)
29168 {
29169 remove_AT (die, a->dw_attr);
29170 ix--;
29171 }
29172 break;
29173 case dw_val_class_loc:
29174 {
29175 dw_loc_descr_ref l = AT_loc (a);
29176 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29177 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29178 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29179 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29180 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29181 with DW_FORM_ref referencing the same DIE as
29182 DW_OP_GNU_variable_value used to reference. */
29183 if (a->dw_attr == DW_AT_string_length
29184 && l
29185 && l->dw_loc_opc == DW_OP_GNU_variable_value
29186 && (l->dw_loc_next == NULL
29187 || (l->dw_loc_next->dw_loc_next == NULL
29188 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29189 {
29190 switch (optimize_string_length (a))
29191 {
29192 case -1:
29193 remove_AT (die, a->dw_attr);
29194 ix--;
29195 /* If we drop DW_AT_string_length, we need to drop also
29196 DW_AT_{string_length_,}byte_size. */
29197 remove_AT_byte_size = true;
29198 continue;
29199 default:
29200 break;
29201 case 1:
29202 /* Even if we keep the optimized DW_AT_string_length,
29203 it might have changed AT_class, so process it again. */
29204 ix--;
29205 continue;
29206 }
29207 }
29208 /* For -gdwarf-2 don't attempt to optimize
29209 DW_AT_data_member_location containing
29210 DW_OP_plus_uconst - older consumers might
29211 rely on it being that op instead of a more complex,
29212 but shorter, location description. */
29213 if ((dwarf_version > 2
29214 || a->dw_attr != DW_AT_data_member_location
29215 || l == NULL
29216 || l->dw_loc_opc != DW_OP_plus_uconst
29217 || l->dw_loc_next != NULL)
29218 && !resolve_addr_in_expr (a, l))
29219 {
29220 if (dwarf_split_debug_info)
29221 remove_loc_list_addr_table_entries (l);
29222 if (l != NULL
29223 && l->dw_loc_next == NULL
29224 && l->dw_loc_opc == DW_OP_addr
29225 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29226 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29227 && a->dw_attr == DW_AT_location)
29228 {
29229 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29230 remove_AT (die, a->dw_attr);
29231 ix--;
29232 optimize_location_into_implicit_ptr (die, decl);
29233 break;
29234 }
29235 if (a->dw_attr == DW_AT_string_length)
29236 /* If we drop DW_AT_string_length, we need to drop also
29237 DW_AT_{string_length_,}byte_size. */
29238 remove_AT_byte_size = true;
29239 remove_AT (die, a->dw_attr);
29240 ix--;
29241 }
29242 else
29243 mark_base_types (l);
29244 }
29245 break;
29246 case dw_val_class_addr:
29247 if (a->dw_attr == DW_AT_const_value
29248 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29249 {
29250 if (AT_index (a) != NOT_INDEXED)
29251 remove_addr_table_entry (a->dw_attr_val.val_entry);
29252 remove_AT (die, a->dw_attr);
29253 ix--;
29254 }
29255 if ((die->die_tag == DW_TAG_call_site
29256 && a->dw_attr == DW_AT_call_origin)
29257 || (die->die_tag == DW_TAG_GNU_call_site
29258 && a->dw_attr == DW_AT_abstract_origin))
29259 {
29260 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29261 dw_die_ref tdie = lookup_decl_die (tdecl);
29262 dw_die_ref cdie;
29263 if (tdie == NULL
29264 && DECL_EXTERNAL (tdecl)
29265 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29266 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29267 {
29268 dw_die_ref pdie = cdie;
29269 /* Make sure we don't add these DIEs into type units.
29270 We could emit skeleton DIEs for context (namespaces,
29271 outer structs/classes) and a skeleton DIE for the
29272 innermost context with DW_AT_signature pointing to the
29273 type unit. See PR78835. */
29274 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29275 pdie = pdie->die_parent;
29276 if (pdie == NULL)
29277 {
29278 /* Creating a full DIE for tdecl is overly expensive and
29279 at this point even wrong when in the LTO phase
29280 as it can end up generating new type DIEs we didn't
29281 output and thus optimize_external_refs will crash. */
29282 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29283 add_AT_flag (tdie, DW_AT_external, 1);
29284 add_AT_flag (tdie, DW_AT_declaration, 1);
29285 add_linkage_attr (tdie, tdecl);
29286 add_name_and_src_coords_attributes (tdie, tdecl, true);
29287 equate_decl_number_to_die (tdecl, tdie);
29288 }
29289 }
29290 if (tdie)
29291 {
29292 a->dw_attr_val.val_class = dw_val_class_die_ref;
29293 a->dw_attr_val.v.val_die_ref.die = tdie;
29294 a->dw_attr_val.v.val_die_ref.external = 0;
29295 }
29296 else
29297 {
29298 if (AT_index (a) != NOT_INDEXED)
29299 remove_addr_table_entry (a->dw_attr_val.val_entry);
29300 remove_AT (die, a->dw_attr);
29301 ix--;
29302 }
29303 }
29304 break;
29305 default:
29306 break;
29307 }
29308
29309 if (remove_AT_byte_size)
29310 remove_AT (die, dwarf_version >= 5
29311 ? DW_AT_string_length_byte_size
29312 : DW_AT_byte_size);
29313
29314 FOR_EACH_CHILD (die, c, resolve_addr (c));
29315 }
29316 \f
29317 /* Helper routines for optimize_location_lists.
29318 This pass tries to share identical local lists in .debug_loc
29319 section. */
29320
29321 /* Iteratively hash operands of LOC opcode into HSTATE. */
29322
29323 static void
29324 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29325 {
29326 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29327 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29328
29329 switch (loc->dw_loc_opc)
29330 {
29331 case DW_OP_const4u:
29332 case DW_OP_const8u:
29333 if (loc->dtprel)
29334 goto hash_addr;
29335 /* FALLTHRU */
29336 case DW_OP_const1u:
29337 case DW_OP_const1s:
29338 case DW_OP_const2u:
29339 case DW_OP_const2s:
29340 case DW_OP_const4s:
29341 case DW_OP_const8s:
29342 case DW_OP_constu:
29343 case DW_OP_consts:
29344 case DW_OP_pick:
29345 case DW_OP_plus_uconst:
29346 case DW_OP_breg0:
29347 case DW_OP_breg1:
29348 case DW_OP_breg2:
29349 case DW_OP_breg3:
29350 case DW_OP_breg4:
29351 case DW_OP_breg5:
29352 case DW_OP_breg6:
29353 case DW_OP_breg7:
29354 case DW_OP_breg8:
29355 case DW_OP_breg9:
29356 case DW_OP_breg10:
29357 case DW_OP_breg11:
29358 case DW_OP_breg12:
29359 case DW_OP_breg13:
29360 case DW_OP_breg14:
29361 case DW_OP_breg15:
29362 case DW_OP_breg16:
29363 case DW_OP_breg17:
29364 case DW_OP_breg18:
29365 case DW_OP_breg19:
29366 case DW_OP_breg20:
29367 case DW_OP_breg21:
29368 case DW_OP_breg22:
29369 case DW_OP_breg23:
29370 case DW_OP_breg24:
29371 case DW_OP_breg25:
29372 case DW_OP_breg26:
29373 case DW_OP_breg27:
29374 case DW_OP_breg28:
29375 case DW_OP_breg29:
29376 case DW_OP_breg30:
29377 case DW_OP_breg31:
29378 case DW_OP_regx:
29379 case DW_OP_fbreg:
29380 case DW_OP_piece:
29381 case DW_OP_deref_size:
29382 case DW_OP_xderef_size:
29383 hstate.add_object (val1->v.val_int);
29384 break;
29385 case DW_OP_skip:
29386 case DW_OP_bra:
29387 {
29388 int offset;
29389
29390 gcc_assert (val1->val_class == dw_val_class_loc);
29391 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29392 hstate.add_object (offset);
29393 }
29394 break;
29395 case DW_OP_implicit_value:
29396 hstate.add_object (val1->v.val_unsigned);
29397 switch (val2->val_class)
29398 {
29399 case dw_val_class_const:
29400 hstate.add_object (val2->v.val_int);
29401 break;
29402 case dw_val_class_vec:
29403 {
29404 unsigned int elt_size = val2->v.val_vec.elt_size;
29405 unsigned int len = val2->v.val_vec.length;
29406
29407 hstate.add_int (elt_size);
29408 hstate.add_int (len);
29409 hstate.add (val2->v.val_vec.array, len * elt_size);
29410 }
29411 break;
29412 case dw_val_class_const_double:
29413 hstate.add_object (val2->v.val_double.low);
29414 hstate.add_object (val2->v.val_double.high);
29415 break;
29416 case dw_val_class_wide_int:
29417 hstate.add (val2->v.val_wide->get_val (),
29418 get_full_len (*val2->v.val_wide)
29419 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29420 break;
29421 case dw_val_class_addr:
29422 inchash::add_rtx (val2->v.val_addr, hstate);
29423 break;
29424 default:
29425 gcc_unreachable ();
29426 }
29427 break;
29428 case DW_OP_bregx:
29429 case DW_OP_bit_piece:
29430 hstate.add_object (val1->v.val_int);
29431 hstate.add_object (val2->v.val_int);
29432 break;
29433 case DW_OP_addr:
29434 hash_addr:
29435 if (loc->dtprel)
29436 {
29437 unsigned char dtprel = 0xd1;
29438 hstate.add_object (dtprel);
29439 }
29440 inchash::add_rtx (val1->v.val_addr, hstate);
29441 break;
29442 case DW_OP_GNU_addr_index:
29443 case DW_OP_GNU_const_index:
29444 {
29445 if (loc->dtprel)
29446 {
29447 unsigned char dtprel = 0xd1;
29448 hstate.add_object (dtprel);
29449 }
29450 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29451 }
29452 break;
29453 case DW_OP_implicit_pointer:
29454 case DW_OP_GNU_implicit_pointer:
29455 hstate.add_int (val2->v.val_int);
29456 break;
29457 case DW_OP_entry_value:
29458 case DW_OP_GNU_entry_value:
29459 hstate.add_object (val1->v.val_loc);
29460 break;
29461 case DW_OP_regval_type:
29462 case DW_OP_deref_type:
29463 case DW_OP_GNU_regval_type:
29464 case DW_OP_GNU_deref_type:
29465 {
29466 unsigned int byte_size
29467 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29468 unsigned int encoding
29469 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29470 hstate.add_object (val1->v.val_int);
29471 hstate.add_object (byte_size);
29472 hstate.add_object (encoding);
29473 }
29474 break;
29475 case DW_OP_convert:
29476 case DW_OP_reinterpret:
29477 case DW_OP_GNU_convert:
29478 case DW_OP_GNU_reinterpret:
29479 if (val1->val_class == dw_val_class_unsigned_const)
29480 {
29481 hstate.add_object (val1->v.val_unsigned);
29482 break;
29483 }
29484 /* FALLTHRU */
29485 case DW_OP_const_type:
29486 case DW_OP_GNU_const_type:
29487 {
29488 unsigned int byte_size
29489 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29490 unsigned int encoding
29491 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29492 hstate.add_object (byte_size);
29493 hstate.add_object (encoding);
29494 if (loc->dw_loc_opc != DW_OP_const_type
29495 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29496 break;
29497 hstate.add_object (val2->val_class);
29498 switch (val2->val_class)
29499 {
29500 case dw_val_class_const:
29501 hstate.add_object (val2->v.val_int);
29502 break;
29503 case dw_val_class_vec:
29504 {
29505 unsigned int elt_size = val2->v.val_vec.elt_size;
29506 unsigned int len = val2->v.val_vec.length;
29507
29508 hstate.add_object (elt_size);
29509 hstate.add_object (len);
29510 hstate.add (val2->v.val_vec.array, len * elt_size);
29511 }
29512 break;
29513 case dw_val_class_const_double:
29514 hstate.add_object (val2->v.val_double.low);
29515 hstate.add_object (val2->v.val_double.high);
29516 break;
29517 case dw_val_class_wide_int:
29518 hstate.add (val2->v.val_wide->get_val (),
29519 get_full_len (*val2->v.val_wide)
29520 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29521 break;
29522 default:
29523 gcc_unreachable ();
29524 }
29525 }
29526 break;
29527
29528 default:
29529 /* Other codes have no operands. */
29530 break;
29531 }
29532 }
29533
29534 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29535
29536 static inline void
29537 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29538 {
29539 dw_loc_descr_ref l;
29540 bool sizes_computed = false;
29541 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29542 size_of_locs (loc);
29543
29544 for (l = loc; l != NULL; l = l->dw_loc_next)
29545 {
29546 enum dwarf_location_atom opc = l->dw_loc_opc;
29547 hstate.add_object (opc);
29548 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29549 {
29550 size_of_locs (loc);
29551 sizes_computed = true;
29552 }
29553 hash_loc_operands (l, hstate);
29554 }
29555 }
29556
29557 /* Compute hash of the whole location list LIST_HEAD. */
29558
29559 static inline void
29560 hash_loc_list (dw_loc_list_ref list_head)
29561 {
29562 dw_loc_list_ref curr = list_head;
29563 inchash::hash hstate;
29564
29565 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29566 {
29567 hstate.add (curr->begin, strlen (curr->begin) + 1);
29568 hstate.add (curr->end, strlen (curr->end) + 1);
29569 if (curr->section)
29570 hstate.add (curr->section, strlen (curr->section) + 1);
29571 hash_locs (curr->expr, hstate);
29572 }
29573 list_head->hash = hstate.end ();
29574 }
29575
29576 /* Return true if X and Y opcodes have the same operands. */
29577
29578 static inline bool
29579 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29580 {
29581 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29582 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29583 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29584 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29585
29586 switch (x->dw_loc_opc)
29587 {
29588 case DW_OP_const4u:
29589 case DW_OP_const8u:
29590 if (x->dtprel)
29591 goto hash_addr;
29592 /* FALLTHRU */
29593 case DW_OP_const1u:
29594 case DW_OP_const1s:
29595 case DW_OP_const2u:
29596 case DW_OP_const2s:
29597 case DW_OP_const4s:
29598 case DW_OP_const8s:
29599 case DW_OP_constu:
29600 case DW_OP_consts:
29601 case DW_OP_pick:
29602 case DW_OP_plus_uconst:
29603 case DW_OP_breg0:
29604 case DW_OP_breg1:
29605 case DW_OP_breg2:
29606 case DW_OP_breg3:
29607 case DW_OP_breg4:
29608 case DW_OP_breg5:
29609 case DW_OP_breg6:
29610 case DW_OP_breg7:
29611 case DW_OP_breg8:
29612 case DW_OP_breg9:
29613 case DW_OP_breg10:
29614 case DW_OP_breg11:
29615 case DW_OP_breg12:
29616 case DW_OP_breg13:
29617 case DW_OP_breg14:
29618 case DW_OP_breg15:
29619 case DW_OP_breg16:
29620 case DW_OP_breg17:
29621 case DW_OP_breg18:
29622 case DW_OP_breg19:
29623 case DW_OP_breg20:
29624 case DW_OP_breg21:
29625 case DW_OP_breg22:
29626 case DW_OP_breg23:
29627 case DW_OP_breg24:
29628 case DW_OP_breg25:
29629 case DW_OP_breg26:
29630 case DW_OP_breg27:
29631 case DW_OP_breg28:
29632 case DW_OP_breg29:
29633 case DW_OP_breg30:
29634 case DW_OP_breg31:
29635 case DW_OP_regx:
29636 case DW_OP_fbreg:
29637 case DW_OP_piece:
29638 case DW_OP_deref_size:
29639 case DW_OP_xderef_size:
29640 return valx1->v.val_int == valy1->v.val_int;
29641 case DW_OP_skip:
29642 case DW_OP_bra:
29643 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29644 can cause irrelevant differences in dw_loc_addr. */
29645 gcc_assert (valx1->val_class == dw_val_class_loc
29646 && valy1->val_class == dw_val_class_loc
29647 && (dwarf_split_debug_info
29648 || x->dw_loc_addr == y->dw_loc_addr));
29649 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29650 case DW_OP_implicit_value:
29651 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29652 || valx2->val_class != valy2->val_class)
29653 return false;
29654 switch (valx2->val_class)
29655 {
29656 case dw_val_class_const:
29657 return valx2->v.val_int == valy2->v.val_int;
29658 case dw_val_class_vec:
29659 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29660 && valx2->v.val_vec.length == valy2->v.val_vec.length
29661 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29662 valx2->v.val_vec.elt_size
29663 * valx2->v.val_vec.length) == 0;
29664 case dw_val_class_const_double:
29665 return valx2->v.val_double.low == valy2->v.val_double.low
29666 && valx2->v.val_double.high == valy2->v.val_double.high;
29667 case dw_val_class_wide_int:
29668 return *valx2->v.val_wide == *valy2->v.val_wide;
29669 case dw_val_class_addr:
29670 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29671 default:
29672 gcc_unreachable ();
29673 }
29674 case DW_OP_bregx:
29675 case DW_OP_bit_piece:
29676 return valx1->v.val_int == valy1->v.val_int
29677 && valx2->v.val_int == valy2->v.val_int;
29678 case DW_OP_addr:
29679 hash_addr:
29680 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29681 case DW_OP_GNU_addr_index:
29682 case DW_OP_GNU_const_index:
29683 {
29684 rtx ax1 = valx1->val_entry->addr.rtl;
29685 rtx ay1 = valy1->val_entry->addr.rtl;
29686 return rtx_equal_p (ax1, ay1);
29687 }
29688 case DW_OP_implicit_pointer:
29689 case DW_OP_GNU_implicit_pointer:
29690 return valx1->val_class == dw_val_class_die_ref
29691 && valx1->val_class == valy1->val_class
29692 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29693 && valx2->v.val_int == valy2->v.val_int;
29694 case DW_OP_entry_value:
29695 case DW_OP_GNU_entry_value:
29696 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29697 case DW_OP_const_type:
29698 case DW_OP_GNU_const_type:
29699 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29700 || valx2->val_class != valy2->val_class)
29701 return false;
29702 switch (valx2->val_class)
29703 {
29704 case dw_val_class_const:
29705 return valx2->v.val_int == valy2->v.val_int;
29706 case dw_val_class_vec:
29707 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29708 && valx2->v.val_vec.length == valy2->v.val_vec.length
29709 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29710 valx2->v.val_vec.elt_size
29711 * valx2->v.val_vec.length) == 0;
29712 case dw_val_class_const_double:
29713 return valx2->v.val_double.low == valy2->v.val_double.low
29714 && valx2->v.val_double.high == valy2->v.val_double.high;
29715 case dw_val_class_wide_int:
29716 return *valx2->v.val_wide == *valy2->v.val_wide;
29717 default:
29718 gcc_unreachable ();
29719 }
29720 case DW_OP_regval_type:
29721 case DW_OP_deref_type:
29722 case DW_OP_GNU_regval_type:
29723 case DW_OP_GNU_deref_type:
29724 return valx1->v.val_int == valy1->v.val_int
29725 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29726 case DW_OP_convert:
29727 case DW_OP_reinterpret:
29728 case DW_OP_GNU_convert:
29729 case DW_OP_GNU_reinterpret:
29730 if (valx1->val_class != valy1->val_class)
29731 return false;
29732 if (valx1->val_class == dw_val_class_unsigned_const)
29733 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29734 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29735 case DW_OP_GNU_parameter_ref:
29736 return valx1->val_class == dw_val_class_die_ref
29737 && valx1->val_class == valy1->val_class
29738 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29739 default:
29740 /* Other codes have no operands. */
29741 return true;
29742 }
29743 }
29744
29745 /* Return true if DWARF location expressions X and Y are the same. */
29746
29747 static inline bool
29748 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29749 {
29750 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29751 if (x->dw_loc_opc != y->dw_loc_opc
29752 || x->dtprel != y->dtprel
29753 || !compare_loc_operands (x, y))
29754 break;
29755 return x == NULL && y == NULL;
29756 }
29757
29758 /* Hashtable helpers. */
29759
29760 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29761 {
29762 static inline hashval_t hash (const dw_loc_list_struct *);
29763 static inline bool equal (const dw_loc_list_struct *,
29764 const dw_loc_list_struct *);
29765 };
29766
29767 /* Return precomputed hash of location list X. */
29768
29769 inline hashval_t
29770 loc_list_hasher::hash (const dw_loc_list_struct *x)
29771 {
29772 return x->hash;
29773 }
29774
29775 /* Return true if location lists A and B are the same. */
29776
29777 inline bool
29778 loc_list_hasher::equal (const dw_loc_list_struct *a,
29779 const dw_loc_list_struct *b)
29780 {
29781 if (a == b)
29782 return 1;
29783 if (a->hash != b->hash)
29784 return 0;
29785 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29786 if (strcmp (a->begin, b->begin) != 0
29787 || strcmp (a->end, b->end) != 0
29788 || (a->section == NULL) != (b->section == NULL)
29789 || (a->section && strcmp (a->section, b->section) != 0)
29790 || !compare_locs (a->expr, b->expr))
29791 break;
29792 return a == NULL && b == NULL;
29793 }
29794
29795 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29796
29797
29798 /* Recursively optimize location lists referenced from DIE
29799 children and share them whenever possible. */
29800
29801 static void
29802 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29803 {
29804 dw_die_ref c;
29805 dw_attr_node *a;
29806 unsigned ix;
29807 dw_loc_list_struct **slot;
29808
29809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29810 if (AT_class (a) == dw_val_class_loc_list)
29811 {
29812 dw_loc_list_ref list = AT_loc_list (a);
29813 /* TODO: perform some optimizations here, before hashing
29814 it and storing into the hash table. */
29815 hash_loc_list (list);
29816 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29817 if (*slot == NULL)
29818 *slot = list;
29819 else
29820 a->dw_attr_val.v.val_loc_list = *slot;
29821 }
29822
29823 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29824 }
29825
29826
29827 /* Recursively assign each location list a unique index into the debug_addr
29828 section. */
29829
29830 static void
29831 index_location_lists (dw_die_ref die)
29832 {
29833 dw_die_ref c;
29834 dw_attr_node *a;
29835 unsigned ix;
29836
29837 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29838 if (AT_class (a) == dw_val_class_loc_list)
29839 {
29840 dw_loc_list_ref list = AT_loc_list (a);
29841 dw_loc_list_ref curr;
29842 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29843 {
29844 /* Don't index an entry that has already been indexed
29845 or won't be output. */
29846 if (curr->begin_entry != NULL
29847 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29848 continue;
29849
29850 curr->begin_entry
29851 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29852 }
29853 }
29854
29855 FOR_EACH_CHILD (die, c, index_location_lists (c));
29856 }
29857
29858 /* Optimize location lists referenced from DIE
29859 children and share them whenever possible. */
29860
29861 static void
29862 optimize_location_lists (dw_die_ref die)
29863 {
29864 loc_list_hash_type htab (500);
29865 optimize_location_lists_1 (die, &htab);
29866 }
29867 \f
29868 /* Traverse the limbo die list, and add parent/child links. The only
29869 dies without parents that should be here are concrete instances of
29870 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29871 For concrete instances, we can get the parent die from the abstract
29872 instance. */
29873
29874 static void
29875 flush_limbo_die_list (void)
29876 {
29877 limbo_die_node *node;
29878
29879 /* get_context_die calls force_decl_die, which can put new DIEs on the
29880 limbo list in LTO mode when nested functions are put in a different
29881 partition than that of their parent function. */
29882 while ((node = limbo_die_list))
29883 {
29884 dw_die_ref die = node->die;
29885 limbo_die_list = node->next;
29886
29887 if (die->die_parent == NULL)
29888 {
29889 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29890
29891 if (origin && origin->die_parent)
29892 add_child_die (origin->die_parent, die);
29893 else if (is_cu_die (die))
29894 ;
29895 else if (seen_error ())
29896 /* It's OK to be confused by errors in the input. */
29897 add_child_die (comp_unit_die (), die);
29898 else
29899 {
29900 /* In certain situations, the lexical block containing a
29901 nested function can be optimized away, which results
29902 in the nested function die being orphaned. Likewise
29903 with the return type of that nested function. Force
29904 this to be a child of the containing function.
29905
29906 It may happen that even the containing function got fully
29907 inlined and optimized out. In that case we are lost and
29908 assign the empty child. This should not be big issue as
29909 the function is likely unreachable too. */
29910 gcc_assert (node->created_for);
29911
29912 if (DECL_P (node->created_for))
29913 origin = get_context_die (DECL_CONTEXT (node->created_for));
29914 else if (TYPE_P (node->created_for))
29915 origin = scope_die_for (node->created_for, comp_unit_die ());
29916 else
29917 origin = comp_unit_die ();
29918
29919 add_child_die (origin, die);
29920 }
29921 }
29922 }
29923 }
29924
29925 /* Reset DIEs so we can output them again. */
29926
29927 static void
29928 reset_dies (dw_die_ref die)
29929 {
29930 dw_die_ref c;
29931
29932 /* Remove stuff we re-generate. */
29933 die->die_mark = 0;
29934 die->die_offset = 0;
29935 die->die_abbrev = 0;
29936 remove_AT (die, DW_AT_sibling);
29937
29938 FOR_EACH_CHILD (die, c, reset_dies (c));
29939 }
29940
29941 /* Output stuff that dwarf requires at the end of every file,
29942 and generate the DWARF-2 debugging info. */
29943
29944 static void
29945 dwarf2out_finish (const char *)
29946 {
29947 comdat_type_node *ctnode;
29948 dw_die_ref main_comp_unit_die;
29949 unsigned char checksum[16];
29950 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29951
29952 /* Flush out any latecomers to the limbo party. */
29953 flush_limbo_die_list ();
29954
29955 if (flag_checking)
29956 {
29957 verify_die (comp_unit_die ());
29958 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29959 verify_die (node->die);
29960 }
29961
29962 /* We shouldn't have any symbols with delayed asm names for
29963 DIEs generated after early finish. */
29964 gcc_assert (deferred_asm_name == NULL);
29965
29966 gen_remaining_tmpl_value_param_die_attribute ();
29967
29968 if (flag_generate_lto || flag_generate_offload)
29969 {
29970 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29971
29972 /* Prune stuff so that dwarf2out_finish runs successfully
29973 for the fat part of the object. */
29974 reset_dies (comp_unit_die ());
29975 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29976 reset_dies (node->die);
29977
29978 hash_table<comdat_type_hasher> comdat_type_table (100);
29979 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29980 {
29981 comdat_type_node **slot
29982 = comdat_type_table.find_slot (ctnode, INSERT);
29983
29984 /* Don't reset types twice. */
29985 if (*slot != HTAB_EMPTY_ENTRY)
29986 continue;
29987
29988 /* Add a pointer to the line table for the main compilation unit
29989 so that the debugger can make sense of DW_AT_decl_file
29990 attributes. */
29991 if (debug_info_level >= DINFO_LEVEL_TERSE)
29992 reset_dies (ctnode->root_die);
29993
29994 *slot = ctnode;
29995 }
29996
29997 /* Reset die CU symbol so we don't output it twice. */
29998 comp_unit_die ()->die_id.die_symbol = NULL;
29999
30000 /* Remove DW_AT_macro from the early output. */
30001 if (have_macinfo)
30002 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
30003
30004 /* Remove indirect string decisions. */
30005 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
30006 }
30007
30008 #if ENABLE_ASSERT_CHECKING
30009 {
30010 dw_die_ref die = comp_unit_die (), c;
30011 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
30012 }
30013 #endif
30014 resolve_addr (comp_unit_die ());
30015 move_marked_base_types ();
30016
30017 /* Initialize sections and labels used for actual assembler output. */
30018 unsigned generation = init_sections_and_labels (false);
30019
30020 /* Traverse the DIE's and add sibling attributes to those DIE's that
30021 have children. */
30022 add_sibling_attributes (comp_unit_die ());
30023 limbo_die_node *node;
30024 for (node = cu_die_list; node; node = node->next)
30025 add_sibling_attributes (node->die);
30026 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30027 add_sibling_attributes (ctnode->root_die);
30028
30029 /* When splitting DWARF info, we put some attributes in the
30030 skeleton compile_unit DIE that remains in the .o, while
30031 most attributes go in the DWO compile_unit_die. */
30032 if (dwarf_split_debug_info)
30033 {
30034 limbo_die_node *cu;
30035 main_comp_unit_die = gen_compile_unit_die (NULL);
30036 if (dwarf_version >= 5)
30037 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
30038 cu = limbo_die_list;
30039 gcc_assert (cu->die == main_comp_unit_die);
30040 limbo_die_list = limbo_die_list->next;
30041 cu->next = cu_die_list;
30042 cu_die_list = cu;
30043 }
30044 else
30045 main_comp_unit_die = comp_unit_die ();
30046
30047 /* Output a terminator label for the .text section. */
30048 switch_to_section (text_section);
30049 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
30050 if (cold_text_section)
30051 {
30052 switch_to_section (cold_text_section);
30053 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
30054 }
30055
30056 /* We can only use the low/high_pc attributes if all of the code was
30057 in .text. */
30058 if (!have_multiple_function_sections
30059 || (dwarf_version < 3 && dwarf_strict))
30060 {
30061 /* Don't add if the CU has no associated code. */
30062 if (text_section_used)
30063 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
30064 text_end_label, true);
30065 }
30066 else
30067 {
30068 unsigned fde_idx;
30069 dw_fde_ref fde;
30070 bool range_list_added = false;
30071
30072 if (text_section_used)
30073 add_ranges_by_labels (main_comp_unit_die, text_section_label,
30074 text_end_label, &range_list_added, true);
30075 if (cold_text_section_used)
30076 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
30077 cold_end_label, &range_list_added, true);
30078
30079 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
30080 {
30081 if (DECL_IGNORED_P (fde->decl))
30082 continue;
30083 if (!fde->in_std_section)
30084 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
30085 fde->dw_fde_end, &range_list_added,
30086 true);
30087 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
30088 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
30089 fde->dw_fde_second_end, &range_list_added,
30090 true);
30091 }
30092
30093 if (range_list_added)
30094 {
30095 /* We need to give .debug_loc and .debug_ranges an appropriate
30096 "base address". Use zero so that these addresses become
30097 absolute. Historically, we've emitted the unexpected
30098 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
30099 Emit both to give time for other tools to adapt. */
30100 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
30101 if (! dwarf_strict && dwarf_version < 4)
30102 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
30103
30104 add_ranges (NULL);
30105 }
30106 }
30107
30108 /* AIX Assembler inserts the length, so adjust the reference to match the
30109 offset expected by debuggers. */
30110 strcpy (dl_section_ref, debug_line_section_label);
30111 if (XCOFF_DEBUGGING_INFO)
30112 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
30113
30114 if (debug_info_level >= DINFO_LEVEL_TERSE)
30115 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
30116 dl_section_ref);
30117
30118 if (have_macinfo)
30119 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30120 macinfo_section_label);
30121
30122 if (dwarf_split_debug_info)
30123 {
30124 if (have_location_lists)
30125 {
30126 if (dwarf_version >= 5)
30127 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30128 loc_section_label);
30129 /* optimize_location_lists calculates the size of the lists,
30130 so index them first, and assign indices to the entries.
30131 Although optimize_location_lists will remove entries from
30132 the table, it only does so for duplicates, and therefore
30133 only reduces ref_counts to 1. */
30134 index_location_lists (comp_unit_die ());
30135 }
30136
30137 if (addr_index_table != NULL)
30138 {
30139 unsigned int index = 0;
30140 addr_index_table
30141 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30142 (&index);
30143 }
30144 }
30145
30146 loc_list_idx = 0;
30147 if (have_location_lists)
30148 {
30149 optimize_location_lists (comp_unit_die ());
30150 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30151 if (dwarf_version >= 5 && dwarf_split_debug_info)
30152 assign_location_list_indexes (comp_unit_die ());
30153 }
30154
30155 save_macinfo_strings ();
30156
30157 if (dwarf_split_debug_info)
30158 {
30159 unsigned int index = 0;
30160
30161 /* Add attributes common to skeleton compile_units and
30162 type_units. Because these attributes include strings, it
30163 must be done before freezing the string table. Top-level
30164 skeleton die attrs are added when the skeleton type unit is
30165 created, so ensure it is created by this point. */
30166 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30167 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30168 }
30169
30170 /* Output all of the compilation units. We put the main one last so that
30171 the offsets are available to output_pubnames. */
30172 for (node = cu_die_list; node; node = node->next)
30173 output_comp_unit (node->die, 0, NULL);
30174
30175 hash_table<comdat_type_hasher> comdat_type_table (100);
30176 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30177 {
30178 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30179
30180 /* Don't output duplicate types. */
30181 if (*slot != HTAB_EMPTY_ENTRY)
30182 continue;
30183
30184 /* Add a pointer to the line table for the main compilation unit
30185 so that the debugger can make sense of DW_AT_decl_file
30186 attributes. */
30187 if (debug_info_level >= DINFO_LEVEL_TERSE)
30188 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30189 (!dwarf_split_debug_info
30190 ? dl_section_ref
30191 : debug_skeleton_line_section_label));
30192
30193 output_comdat_type_unit (ctnode);
30194 *slot = ctnode;
30195 }
30196
30197 if (dwarf_split_debug_info)
30198 {
30199 int mark;
30200 struct md5_ctx ctx;
30201
30202 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30203 index_rnglists ();
30204
30205 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30206 md5_init_ctx (&ctx);
30207 mark = 0;
30208 die_checksum (comp_unit_die (), &ctx, &mark);
30209 unmark_all_dies (comp_unit_die ());
30210 md5_finish_ctx (&ctx, checksum);
30211
30212 if (dwarf_version < 5)
30213 {
30214 /* Use the first 8 bytes of the checksum as the dwo_id,
30215 and add it to both comp-unit DIEs. */
30216 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30217 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30218 }
30219
30220 /* Add the base offset of the ranges table to the skeleton
30221 comp-unit DIE. */
30222 if (!vec_safe_is_empty (ranges_table))
30223 {
30224 if (dwarf_version >= 5)
30225 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30226 ranges_base_label);
30227 else
30228 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30229 ranges_section_label);
30230 }
30231
30232 switch_to_section (debug_addr_section);
30233 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30234 output_addr_table ();
30235 }
30236
30237 /* Output the main compilation unit if non-empty or if .debug_macinfo
30238 or .debug_macro will be emitted. */
30239 output_comp_unit (comp_unit_die (), have_macinfo,
30240 dwarf_split_debug_info ? checksum : NULL);
30241
30242 if (dwarf_split_debug_info && info_section_emitted)
30243 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30244
30245 /* Output the abbreviation table. */
30246 if (vec_safe_length (abbrev_die_table) != 1)
30247 {
30248 switch_to_section (debug_abbrev_section);
30249 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30250 output_abbrev_section ();
30251 }
30252
30253 /* Output location list section if necessary. */
30254 if (have_location_lists)
30255 {
30256 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30257 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30258 /* Output the location lists info. */
30259 switch_to_section (debug_loc_section);
30260 if (dwarf_version >= 5)
30261 {
30262 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30263 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30264 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30265 dw2_asm_output_data (4, 0xffffffff,
30266 "Initial length escape value indicating "
30267 "64-bit DWARF extension");
30268 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30269 "Length of Location Lists");
30270 ASM_OUTPUT_LABEL (asm_out_file, l1);
30271 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30272 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30273 dw2_asm_output_data (1, 0, "Segment Size");
30274 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30275 "Offset Entry Count");
30276 }
30277 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30278 if (dwarf_version >= 5 && dwarf_split_debug_info)
30279 {
30280 unsigned int save_loc_list_idx = loc_list_idx;
30281 loc_list_idx = 0;
30282 output_loclists_offsets (comp_unit_die ());
30283 gcc_assert (save_loc_list_idx == loc_list_idx);
30284 }
30285 output_location_lists (comp_unit_die ());
30286 if (dwarf_version >= 5)
30287 ASM_OUTPUT_LABEL (asm_out_file, l2);
30288 }
30289
30290 output_pubtables ();
30291
30292 /* Output the address range information if a CU (.debug_info section)
30293 was emitted. We output an empty table even if we had no functions
30294 to put in it. This because the consumer has no way to tell the
30295 difference between an empty table that we omitted and failure to
30296 generate a table that would have contained data. */
30297 if (info_section_emitted)
30298 {
30299 switch_to_section (debug_aranges_section);
30300 output_aranges ();
30301 }
30302
30303 /* Output ranges section if necessary. */
30304 if (!vec_safe_is_empty (ranges_table))
30305 {
30306 if (dwarf_version >= 5)
30307 output_rnglists (generation);
30308 else
30309 output_ranges ();
30310 }
30311
30312 /* Have to end the macro section. */
30313 if (have_macinfo)
30314 {
30315 switch_to_section (debug_macinfo_section);
30316 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30317 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30318 : debug_skeleton_line_section_label, false);
30319 dw2_asm_output_data (1, 0, "End compilation unit");
30320 }
30321
30322 /* Output the source line correspondence table. We must do this
30323 even if there is no line information. Otherwise, on an empty
30324 translation unit, we will generate a present, but empty,
30325 .debug_info section. IRIX 6.5 `nm' will then complain when
30326 examining the file. This is done late so that any filenames
30327 used by the debug_info section are marked as 'used'. */
30328 switch_to_section (debug_line_section);
30329 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30330 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30331 output_line_info (false);
30332
30333 if (dwarf_split_debug_info && info_section_emitted)
30334 {
30335 switch_to_section (debug_skeleton_line_section);
30336 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30337 output_line_info (true);
30338 }
30339
30340 /* If we emitted any indirect strings, output the string table too. */
30341 if (debug_str_hash || skeleton_debug_str_hash)
30342 output_indirect_strings ();
30343 if (debug_line_str_hash)
30344 {
30345 switch_to_section (debug_line_str_section);
30346 const enum dwarf_form form = DW_FORM_line_strp;
30347 debug_line_str_hash->traverse<enum dwarf_form,
30348 output_indirect_string> (form);
30349 }
30350 }
30351
30352 /* Returns a hash value for X (which really is a variable_value_struct). */
30353
30354 inline hashval_t
30355 variable_value_hasher::hash (variable_value_struct *x)
30356 {
30357 return (hashval_t) x->decl_id;
30358 }
30359
30360 /* Return nonzero if decl_id of variable_value_struct X is the same as
30361 UID of decl Y. */
30362
30363 inline bool
30364 variable_value_hasher::equal (variable_value_struct *x, tree y)
30365 {
30366 return x->decl_id == DECL_UID (y);
30367 }
30368
30369 /* Helper function for resolve_variable_value, handle
30370 DW_OP_GNU_variable_value in one location expression.
30371 Return true if exprloc has been changed into loclist. */
30372
30373 static bool
30374 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30375 {
30376 dw_loc_descr_ref next;
30377 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30378 {
30379 next = loc->dw_loc_next;
30380 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30381 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30382 continue;
30383
30384 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30385 if (DECL_CONTEXT (decl) != current_function_decl)
30386 continue;
30387
30388 dw_die_ref ref = lookup_decl_die (decl);
30389 if (ref)
30390 {
30391 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30392 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30393 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30394 continue;
30395 }
30396 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30397 if (l == NULL)
30398 continue;
30399 if (l->dw_loc_next)
30400 {
30401 if (AT_class (a) != dw_val_class_loc)
30402 continue;
30403 switch (a->dw_attr)
30404 {
30405 /* Following attributes allow both exprloc and loclist
30406 classes, so we can change them into a loclist. */
30407 case DW_AT_location:
30408 case DW_AT_string_length:
30409 case DW_AT_return_addr:
30410 case DW_AT_data_member_location:
30411 case DW_AT_frame_base:
30412 case DW_AT_segment:
30413 case DW_AT_static_link:
30414 case DW_AT_use_location:
30415 case DW_AT_vtable_elem_location:
30416 if (prev)
30417 {
30418 prev->dw_loc_next = NULL;
30419 prepend_loc_descr_to_each (l, AT_loc (a));
30420 }
30421 if (next)
30422 add_loc_descr_to_each (l, next);
30423 a->dw_attr_val.val_class = dw_val_class_loc_list;
30424 a->dw_attr_val.val_entry = NULL;
30425 a->dw_attr_val.v.val_loc_list = l;
30426 have_location_lists = true;
30427 return true;
30428 /* Following attributes allow both exprloc and reference,
30429 so if the whole expression is DW_OP_GNU_variable_value alone
30430 we could transform it into reference. */
30431 case DW_AT_byte_size:
30432 case DW_AT_bit_size:
30433 case DW_AT_lower_bound:
30434 case DW_AT_upper_bound:
30435 case DW_AT_bit_stride:
30436 case DW_AT_count:
30437 case DW_AT_allocated:
30438 case DW_AT_associated:
30439 case DW_AT_byte_stride:
30440 if (prev == NULL && next == NULL)
30441 break;
30442 /* FALLTHRU */
30443 default:
30444 if (dwarf_strict)
30445 continue;
30446 break;
30447 }
30448 /* Create DW_TAG_variable that we can refer to. */
30449 gen_decl_die (decl, NULL_TREE, NULL,
30450 lookup_decl_die (current_function_decl));
30451 ref = lookup_decl_die (decl);
30452 if (ref)
30453 {
30454 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30455 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30456 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30457 }
30458 continue;
30459 }
30460 if (prev)
30461 {
30462 prev->dw_loc_next = l->expr;
30463 add_loc_descr (&prev->dw_loc_next, next);
30464 free_loc_descr (loc, NULL);
30465 next = prev->dw_loc_next;
30466 }
30467 else
30468 {
30469 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30470 add_loc_descr (&loc, next);
30471 next = loc;
30472 }
30473 loc = prev;
30474 }
30475 return false;
30476 }
30477
30478 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30479
30480 static void
30481 resolve_variable_value (dw_die_ref die)
30482 {
30483 dw_attr_node *a;
30484 dw_loc_list_ref loc;
30485 unsigned ix;
30486
30487 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30488 switch (AT_class (a))
30489 {
30490 case dw_val_class_loc:
30491 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30492 break;
30493 /* FALLTHRU */
30494 case dw_val_class_loc_list:
30495 loc = AT_loc_list (a);
30496 gcc_assert (loc);
30497 for (; loc; loc = loc->dw_loc_next)
30498 resolve_variable_value_in_expr (a, loc->expr);
30499 break;
30500 default:
30501 break;
30502 }
30503 }
30504
30505 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30506 temporaries in the current function. */
30507
30508 static void
30509 resolve_variable_values (void)
30510 {
30511 if (!variable_value_hash || !current_function_decl)
30512 return;
30513
30514 struct variable_value_struct *node
30515 = variable_value_hash->find_with_hash (current_function_decl,
30516 DECL_UID (current_function_decl));
30517
30518 if (node == NULL)
30519 return;
30520
30521 unsigned int i;
30522 dw_die_ref die;
30523 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30524 resolve_variable_value (die);
30525 }
30526
30527 /* Helper function for note_variable_value, handle one location
30528 expression. */
30529
30530 static void
30531 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30532 {
30533 for (; loc; loc = loc->dw_loc_next)
30534 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30535 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30536 {
30537 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30538 dw_die_ref ref = lookup_decl_die (decl);
30539 if (! ref && (flag_generate_lto || flag_generate_offload))
30540 {
30541 /* ??? This is somewhat a hack because we do not create DIEs
30542 for variables not in BLOCK trees early but when generating
30543 early LTO output we need the dw_val_class_decl_ref to be
30544 fully resolved. For fat LTO objects we'd also like to
30545 undo this after LTO dwarf output. */
30546 gcc_assert (DECL_CONTEXT (decl));
30547 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30548 gcc_assert (ctx != NULL);
30549 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30550 ref = lookup_decl_die (decl);
30551 gcc_assert (ref != NULL);
30552 }
30553 if (ref)
30554 {
30555 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30556 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30557 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30558 continue;
30559 }
30560 if (VAR_P (decl)
30561 && DECL_CONTEXT (decl)
30562 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30563 && lookup_decl_die (DECL_CONTEXT (decl)))
30564 {
30565 if (!variable_value_hash)
30566 variable_value_hash
30567 = hash_table<variable_value_hasher>::create_ggc (10);
30568
30569 tree fndecl = DECL_CONTEXT (decl);
30570 struct variable_value_struct *node;
30571 struct variable_value_struct **slot
30572 = variable_value_hash->find_slot_with_hash (fndecl,
30573 DECL_UID (fndecl),
30574 INSERT);
30575 if (*slot == NULL)
30576 {
30577 node = ggc_cleared_alloc<variable_value_struct> ();
30578 node->decl_id = DECL_UID (fndecl);
30579 *slot = node;
30580 }
30581 else
30582 node = *slot;
30583
30584 vec_safe_push (node->dies, die);
30585 }
30586 }
30587 }
30588
30589 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30590 with dw_val_class_decl_ref operand. */
30591
30592 static void
30593 note_variable_value (dw_die_ref die)
30594 {
30595 dw_die_ref c;
30596 dw_attr_node *a;
30597 dw_loc_list_ref loc;
30598 unsigned ix;
30599
30600 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30601 switch (AT_class (a))
30602 {
30603 case dw_val_class_loc_list:
30604 loc = AT_loc_list (a);
30605 gcc_assert (loc);
30606 if (!loc->noted_variable_value)
30607 {
30608 loc->noted_variable_value = 1;
30609 for (; loc; loc = loc->dw_loc_next)
30610 note_variable_value_in_expr (die, loc->expr);
30611 }
30612 break;
30613 case dw_val_class_loc:
30614 note_variable_value_in_expr (die, AT_loc (a));
30615 break;
30616 default:
30617 break;
30618 }
30619
30620 /* Mark children. */
30621 FOR_EACH_CHILD (die, c, note_variable_value (c));
30622 }
30623
30624 /* Perform any cleanups needed after the early debug generation pass
30625 has run. */
30626
30627 static void
30628 dwarf2out_early_finish (const char *filename)
30629 {
30630 set_early_dwarf s;
30631
30632 /* PCH might result in DW_AT_producer string being restored from the
30633 header compilation, so always fill it with empty string initially
30634 and overwrite only here. */
30635 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30636 producer_string = gen_producer_string ();
30637 producer->dw_attr_val.v.val_str->refcount--;
30638 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30639
30640 /* Add the name for the main input file now. We delayed this from
30641 dwarf2out_init to avoid complications with PCH. */
30642 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30643 add_comp_dir_attribute (comp_unit_die ());
30644
30645 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30646 DW_AT_comp_dir into .debug_line_str section. */
30647 if (!DWARF2_ASM_LINE_DEBUG_INFO
30648 && dwarf_version >= 5
30649 && DWARF5_USE_DEBUG_LINE_STR)
30650 {
30651 for (int i = 0; i < 2; i++)
30652 {
30653 dw_attr_node *a = get_AT (comp_unit_die (),
30654 i ? DW_AT_comp_dir : DW_AT_name);
30655 if (a == NULL
30656 || AT_class (a) != dw_val_class_str
30657 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30658 continue;
30659
30660 if (! debug_line_str_hash)
30661 debug_line_str_hash
30662 = hash_table<indirect_string_hasher>::create_ggc (10);
30663
30664 struct indirect_string_node *node
30665 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30666 set_indirect_string (node);
30667 node->form = DW_FORM_line_strp;
30668 a->dw_attr_val.v.val_str->refcount--;
30669 a->dw_attr_val.v.val_str = node;
30670 }
30671 }
30672
30673 /* With LTO early dwarf was really finished at compile-time, so make
30674 sure to adjust the phase after annotating the LTRANS CU DIE. */
30675 if (in_lto_p)
30676 {
30677 early_dwarf_finished = true;
30678 return;
30679 }
30680
30681 /* Walk through the list of incomplete types again, trying once more to
30682 emit full debugging info for them. */
30683 retry_incomplete_types ();
30684
30685 /* The point here is to flush out the limbo list so that it is empty
30686 and we don't need to stream it for LTO. */
30687 flush_limbo_die_list ();
30688
30689 gen_scheduled_generic_parms_dies ();
30690 gen_remaining_tmpl_value_param_die_attribute ();
30691
30692 /* Add DW_AT_linkage_name for all deferred DIEs. */
30693 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30694 {
30695 tree decl = node->created_for;
30696 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30697 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30698 ended up in deferred_asm_name before we knew it was
30699 constant and never written to disk. */
30700 && DECL_ASSEMBLER_NAME (decl))
30701 {
30702 add_linkage_attr (node->die, decl);
30703 move_linkage_attr (node->die);
30704 }
30705 }
30706 deferred_asm_name = NULL;
30707
30708 if (flag_eliminate_unused_debug_types)
30709 prune_unused_types ();
30710
30711 /* Generate separate COMDAT sections for type DIEs. */
30712 if (use_debug_types)
30713 {
30714 break_out_comdat_types (comp_unit_die ());
30715
30716 /* Each new type_unit DIE was added to the limbo die list when created.
30717 Since these have all been added to comdat_type_list, clear the
30718 limbo die list. */
30719 limbo_die_list = NULL;
30720
30721 /* For each new comdat type unit, copy declarations for incomplete
30722 types to make the new unit self-contained (i.e., no direct
30723 references to the main compile unit). */
30724 for (comdat_type_node *ctnode = comdat_type_list;
30725 ctnode != NULL; ctnode = ctnode->next)
30726 copy_decls_for_unworthy_types (ctnode->root_die);
30727 copy_decls_for_unworthy_types (comp_unit_die ());
30728
30729 /* In the process of copying declarations from one unit to another,
30730 we may have left some declarations behind that are no longer
30731 referenced. Prune them. */
30732 prune_unused_types ();
30733 }
30734
30735 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30736 with dw_val_class_decl_ref operand. */
30737 note_variable_value (comp_unit_die ());
30738 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30739 note_variable_value (node->die);
30740 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30741 ctnode = ctnode->next)
30742 note_variable_value (ctnode->root_die);
30743 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30744 note_variable_value (node->die);
30745
30746 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30747 both the main_cu and all skeleton TUs. Making this call unconditional
30748 would end up either adding a second copy of the AT_pubnames attribute, or
30749 requiring a special case in add_top_level_skeleton_die_attrs. */
30750 if (!dwarf_split_debug_info)
30751 add_AT_pubnames (comp_unit_die ());
30752
30753 /* The early debug phase is now finished. */
30754 early_dwarf_finished = true;
30755
30756 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30757 if (!flag_generate_lto && !flag_generate_offload)
30758 return;
30759
30760 /* Now as we are going to output for LTO initialize sections and labels
30761 to the LTO variants. We don't need a random-seed postfix as other
30762 LTO sections as linking the LTO debug sections into one in a partial
30763 link is fine. */
30764 init_sections_and_labels (true);
30765
30766 /* The output below is modeled after dwarf2out_finish with all
30767 location related output removed and some LTO specific changes.
30768 Some refactoring might make both smaller and easier to match up. */
30769
30770 /* Traverse the DIE's and add add sibling attributes to those DIE's
30771 that have children. */
30772 add_sibling_attributes (comp_unit_die ());
30773 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30774 add_sibling_attributes (node->die);
30775 for (comdat_type_node *ctnode = comdat_type_list;
30776 ctnode != NULL; ctnode = ctnode->next)
30777 add_sibling_attributes (ctnode->root_die);
30778
30779 if (have_macinfo)
30780 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30781 macinfo_section_label);
30782
30783 save_macinfo_strings ();
30784
30785 /* Output all of the compilation units. We put the main one last so that
30786 the offsets are available to output_pubnames. */
30787 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30788 output_comp_unit (node->die, 0, NULL);
30789
30790 hash_table<comdat_type_hasher> comdat_type_table (100);
30791 for (comdat_type_node *ctnode = comdat_type_list;
30792 ctnode != NULL; ctnode = ctnode->next)
30793 {
30794 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30795
30796 /* Don't output duplicate types. */
30797 if (*slot != HTAB_EMPTY_ENTRY)
30798 continue;
30799
30800 /* Add a pointer to the line table for the main compilation unit
30801 so that the debugger can make sense of DW_AT_decl_file
30802 attributes. */
30803 if (debug_info_level >= DINFO_LEVEL_TERSE)
30804 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30805 (!dwarf_split_debug_info
30806 ? debug_line_section_label
30807 : debug_skeleton_line_section_label));
30808
30809 output_comdat_type_unit (ctnode);
30810 *slot = ctnode;
30811 }
30812
30813 /* Stick a unique symbol to the main debuginfo section. */
30814 compute_comp_unit_symbol (comp_unit_die ());
30815
30816 /* Output the main compilation unit. We always need it if only for
30817 the CU symbol. */
30818 output_comp_unit (comp_unit_die (), true, NULL);
30819
30820 /* Output the abbreviation table. */
30821 if (vec_safe_length (abbrev_die_table) != 1)
30822 {
30823 switch_to_section (debug_abbrev_section);
30824 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30825 output_abbrev_section ();
30826 }
30827
30828 /* Have to end the macro section. */
30829 if (have_macinfo)
30830 {
30831 /* We have to save macinfo state if we need to output it again
30832 for the FAT part of the object. */
30833 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30834 if (flag_fat_lto_objects)
30835 macinfo_table = macinfo_table->copy ();
30836
30837 switch_to_section (debug_macinfo_section);
30838 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30839 output_macinfo (debug_skeleton_line_section_label, true);
30840 dw2_asm_output_data (1, 0, "End compilation unit");
30841
30842 /* Emit a skeleton debug_line section. */
30843 switch_to_section (debug_skeleton_line_section);
30844 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30845 output_line_info (true);
30846
30847 if (flag_fat_lto_objects)
30848 {
30849 vec_free (macinfo_table);
30850 macinfo_table = saved_macinfo_table;
30851 }
30852 }
30853
30854
30855 /* If we emitted any indirect strings, output the string table too. */
30856 if (debug_str_hash || skeleton_debug_str_hash)
30857 output_indirect_strings ();
30858
30859 /* Switch back to the text section. */
30860 switch_to_section (text_section);
30861 }
30862
30863 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30864 within the same process. For use by toplev::finalize. */
30865
30866 void
30867 dwarf2out_c_finalize (void)
30868 {
30869 last_var_location_insn = NULL;
30870 cached_next_real_insn = NULL;
30871 used_rtx_array = NULL;
30872 incomplete_types = NULL;
30873 decl_scope_table = NULL;
30874 debug_info_section = NULL;
30875 debug_skeleton_info_section = NULL;
30876 debug_abbrev_section = NULL;
30877 debug_skeleton_abbrev_section = NULL;
30878 debug_aranges_section = NULL;
30879 debug_addr_section = NULL;
30880 debug_macinfo_section = NULL;
30881 debug_line_section = NULL;
30882 debug_skeleton_line_section = NULL;
30883 debug_loc_section = NULL;
30884 debug_pubnames_section = NULL;
30885 debug_pubtypes_section = NULL;
30886 debug_str_section = NULL;
30887 debug_line_str_section = NULL;
30888 debug_str_dwo_section = NULL;
30889 debug_str_offsets_section = NULL;
30890 debug_ranges_section = NULL;
30891 debug_frame_section = NULL;
30892 fde_vec = NULL;
30893 debug_str_hash = NULL;
30894 debug_line_str_hash = NULL;
30895 skeleton_debug_str_hash = NULL;
30896 dw2_string_counter = 0;
30897 have_multiple_function_sections = false;
30898 text_section_used = false;
30899 cold_text_section_used = false;
30900 cold_text_section = NULL;
30901 current_unit_personality = NULL;
30902
30903 early_dwarf = false;
30904 early_dwarf_finished = false;
30905
30906 next_die_offset = 0;
30907 single_comp_unit_die = NULL;
30908 comdat_type_list = NULL;
30909 limbo_die_list = NULL;
30910 file_table = NULL;
30911 decl_die_table = NULL;
30912 common_block_die_table = NULL;
30913 decl_loc_table = NULL;
30914 call_arg_locations = NULL;
30915 call_arg_loc_last = NULL;
30916 call_site_count = -1;
30917 tail_call_site_count = -1;
30918 cached_dw_loc_list_table = NULL;
30919 abbrev_die_table = NULL;
30920 delete dwarf_proc_stack_usage_map;
30921 dwarf_proc_stack_usage_map = NULL;
30922 line_info_label_num = 0;
30923 cur_line_info_table = NULL;
30924 text_section_line_info = NULL;
30925 cold_text_section_line_info = NULL;
30926 separate_line_info = NULL;
30927 info_section_emitted = false;
30928 pubname_table = NULL;
30929 pubtype_table = NULL;
30930 macinfo_table = NULL;
30931 ranges_table = NULL;
30932 ranges_by_label = NULL;
30933 rnglist_idx = 0;
30934 have_location_lists = false;
30935 loclabel_num = 0;
30936 poc_label_num = 0;
30937 last_emitted_file = NULL;
30938 label_num = 0;
30939 tmpl_value_parm_die_table = NULL;
30940 generic_type_instances = NULL;
30941 frame_pointer_fb_offset = 0;
30942 frame_pointer_fb_offset_valid = false;
30943 base_types.release ();
30944 XDELETEVEC (producer_string);
30945 producer_string = NULL;
30946 }
30947
30948 #include "gt-dwarf2out.h"