re PR c++/60336 (empty struct value is passed differently in C and C++)
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
97
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
103
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
107
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
111
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
114
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
127
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
135
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
140
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
144
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
150
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
157
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static unsigned macinfo_label_base = 1;
168 static GTY(()) section *debug_line_section;
169 static GTY(()) section *debug_skeleton_line_section;
170 static GTY(()) section *debug_loc_section;
171 static GTY(()) section *debug_pubnames_section;
172 static GTY(()) section *debug_pubtypes_section;
173 static GTY(()) section *debug_str_section;
174 static GTY(()) section *debug_line_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
179
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 40
182
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
187
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
190
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
194
195 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
196 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
197 #endif
198
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
202
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
210
211
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
216
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
218
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
225 };
226
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
228 {
229 typedef const char *compare_type;
230
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
233 };
234
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
236
237 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
238
239 /* With split_debug_info, both the comp_dir and dwo_name go in the
240 main object file, rather than the dwo, similar to the force_direct
241 parameter elsewhere but with additional complications:
242
243 1) The string is needed in both the main object file and the dwo.
244 That is, the comp_dir and dwo_name will appear in both places.
245
246 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
247 DW_FORM_line_strp or DW_FORM_GNU_str_index.
248
249 3) GCC chooses the form to use late, depending on the size and
250 reference count.
251
252 Rather than forcing the all debug string handling functions and
253 callers to deal with these complications, simply use a separate,
254 special-cased string table for any attribute that should go in the
255 main object file. This limits the complexity to just the places
256 that need it. */
257
258 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
259
260 static GTY(()) int dw2_string_counter;
261
262 /* True if the compilation unit places functions in more than one section. */
263 static GTY(()) bool have_multiple_function_sections = false;
264
265 /* Whether the default text and cold text sections have been used at all. */
266 static GTY(()) bool text_section_used = false;
267 static GTY(()) bool cold_text_section_used = false;
268
269 /* The default cold text section. */
270 static GTY(()) section *cold_text_section;
271
272 /* The DIE for C++14 'auto' in a function return type. */
273 static GTY(()) dw_die_ref auto_die;
274
275 /* The DIE for C++14 'decltype(auto)' in a function return type. */
276 static GTY(()) dw_die_ref decltype_auto_die;
277
278 /* Forward declarations for functions defined in this file. */
279
280 static void output_call_frame_info (int);
281 static void dwarf2out_note_section_used (void);
282
283 /* Personality decl of current unit. Used only when assembler does not support
284 personality CFI. */
285 static GTY(()) rtx current_unit_personality;
286
287 /* Whether an eh_frame section is required. */
288 static GTY(()) bool do_eh_frame = false;
289
290 /* .debug_rnglists next index. */
291 static unsigned int rnglist_idx;
292
293 /* Data and reference forms for relocatable data. */
294 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
295 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
296
297 #ifndef DEBUG_FRAME_SECTION
298 #define DEBUG_FRAME_SECTION ".debug_frame"
299 #endif
300
301 #ifndef FUNC_BEGIN_LABEL
302 #define FUNC_BEGIN_LABEL "LFB"
303 #endif
304
305 #ifndef FUNC_END_LABEL
306 #define FUNC_END_LABEL "LFE"
307 #endif
308
309 #ifndef PROLOGUE_END_LABEL
310 #define PROLOGUE_END_LABEL "LPE"
311 #endif
312
313 #ifndef EPILOGUE_BEGIN_LABEL
314 #define EPILOGUE_BEGIN_LABEL "LEB"
315 #endif
316
317 #ifndef FRAME_BEGIN_LABEL
318 #define FRAME_BEGIN_LABEL "Lframe"
319 #endif
320 #define CIE_AFTER_SIZE_LABEL "LSCIE"
321 #define CIE_END_LABEL "LECIE"
322 #define FDE_LABEL "LSFDE"
323 #define FDE_AFTER_SIZE_LABEL "LASFDE"
324 #define FDE_END_LABEL "LEFDE"
325 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
326 #define LINE_NUMBER_END_LABEL "LELT"
327 #define LN_PROLOG_AS_LABEL "LASLTP"
328 #define LN_PROLOG_END_LABEL "LELTP"
329 #define DIE_LABEL_PREFIX "DW"
330 \f
331 /* Match the base name of a file to the base name of a compilation unit. */
332
333 static int
334 matches_main_base (const char *path)
335 {
336 /* Cache the last query. */
337 static const char *last_path = NULL;
338 static int last_match = 0;
339 if (path != last_path)
340 {
341 const char *base;
342 int length = base_of_path (path, &base);
343 last_path = path;
344 last_match = (length == main_input_baselength
345 && memcmp (base, main_input_basename, length) == 0);
346 }
347 return last_match;
348 }
349
350 #ifdef DEBUG_DEBUG_STRUCT
351
352 static int
353 dump_struct_debug (tree type, enum debug_info_usage usage,
354 enum debug_struct_file criterion, int generic,
355 int matches, int result)
356 {
357 /* Find the type name. */
358 tree type_decl = TYPE_STUB_DECL (type);
359 tree t = type_decl;
360 const char *name = 0;
361 if (TREE_CODE (t) == TYPE_DECL)
362 t = DECL_NAME (t);
363 if (t)
364 name = IDENTIFIER_POINTER (t);
365
366 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
367 criterion,
368 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
369 matches ? "bas" : "hdr",
370 generic ? "gen" : "ord",
371 usage == DINFO_USAGE_DFN ? ";" :
372 usage == DINFO_USAGE_DIR_USE ? "." : "*",
373 result,
374 (void*) type_decl, name);
375 return result;
376 }
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378 dump_struct_debug (type, usage, criterion, generic, matches, result)
379
380 #else
381
382 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
383 (result)
384
385 #endif
386
387 /* Get the number of HOST_WIDE_INTs needed to represent the precision
388 of the number. Some constants have a large uniform precision, so
389 we get the precision needed for the actual value of the number. */
390
391 static unsigned int
392 get_full_len (const wide_int &op)
393 {
394 int prec = wi::min_precision (op, UNSIGNED);
395 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
396 / HOST_BITS_PER_WIDE_INT);
397 }
398
399 static bool
400 should_emit_struct_debug (tree type, enum debug_info_usage usage)
401 {
402 enum debug_struct_file criterion;
403 tree type_decl;
404 bool generic = lang_hooks.types.generic_p (type);
405
406 if (generic)
407 criterion = debug_struct_generic[usage];
408 else
409 criterion = debug_struct_ordinary[usage];
410
411 if (criterion == DINFO_STRUCT_FILE_NONE)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 if (criterion == DINFO_STRUCT_FILE_ANY)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
415
416 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
417
418 if (type_decl != NULL)
419 {
420 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
421 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
422
423 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
424 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
425 }
426
427 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
428 }
429 \f
430 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
431 switch to the data section instead, and write out a synthetic start label
432 for collect2 the first time around. */
433
434 static void
435 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
436 {
437 if (eh_frame_section == 0)
438 {
439 int flags;
440
441 if (EH_TABLES_CAN_BE_READ_ONLY)
442 {
443 int fde_encoding;
444 int per_encoding;
445 int lsda_encoding;
446
447 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
448 /*global=*/0);
449 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
450 /*global=*/1);
451 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
452 /*global=*/0);
453 flags = ((! flag_pic
454 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
455 && (fde_encoding & 0x70) != DW_EH_PE_aligned
456 && (per_encoding & 0x70) != DW_EH_PE_absptr
457 && (per_encoding & 0x70) != DW_EH_PE_aligned
458 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
459 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
460 ? 0 : SECTION_WRITE);
461 }
462 else
463 flags = SECTION_WRITE;
464
465 #ifdef EH_FRAME_SECTION_NAME
466 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
467 #else
468 eh_frame_section = ((flags == SECTION_WRITE)
469 ? data_section : readonly_data_section);
470 #endif /* EH_FRAME_SECTION_NAME */
471 }
472
473 switch_to_section (eh_frame_section);
474
475 #ifdef EH_FRAME_THROUGH_COLLECT2
476 /* We have no special eh_frame section. Emit special labels to guide
477 collect2. */
478 if (!back)
479 {
480 tree label = get_file_function_name ("F");
481 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
482 targetm.asm_out.globalize_label (asm_out_file,
483 IDENTIFIER_POINTER (label));
484 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
485 }
486 #endif
487 }
488
489 /* Switch [BACK] to the eh or debug frame table section, depending on
490 FOR_EH. */
491
492 static void
493 switch_to_frame_table_section (int for_eh, bool back)
494 {
495 if (for_eh)
496 switch_to_eh_frame_section (back);
497 else
498 {
499 if (!debug_frame_section)
500 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
501 SECTION_DEBUG, NULL);
502 switch_to_section (debug_frame_section);
503 }
504 }
505
506 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
507
508 enum dw_cfi_oprnd_type
509 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
510 {
511 switch (cfi)
512 {
513 case DW_CFA_nop:
514 case DW_CFA_GNU_window_save:
515 case DW_CFA_remember_state:
516 case DW_CFA_restore_state:
517 return dw_cfi_oprnd_unused;
518
519 case DW_CFA_set_loc:
520 case DW_CFA_advance_loc1:
521 case DW_CFA_advance_loc2:
522 case DW_CFA_advance_loc4:
523 case DW_CFA_MIPS_advance_loc8:
524 return dw_cfi_oprnd_addr;
525
526 case DW_CFA_offset:
527 case DW_CFA_offset_extended:
528 case DW_CFA_def_cfa:
529 case DW_CFA_offset_extended_sf:
530 case DW_CFA_def_cfa_sf:
531 case DW_CFA_restore:
532 case DW_CFA_restore_extended:
533 case DW_CFA_undefined:
534 case DW_CFA_same_value:
535 case DW_CFA_def_cfa_register:
536 case DW_CFA_register:
537 case DW_CFA_expression:
538 case DW_CFA_val_expression:
539 return dw_cfi_oprnd_reg_num;
540
541 case DW_CFA_def_cfa_offset:
542 case DW_CFA_GNU_args_size:
543 case DW_CFA_def_cfa_offset_sf:
544 return dw_cfi_oprnd_offset;
545
546 case DW_CFA_def_cfa_expression:
547 return dw_cfi_oprnd_loc;
548
549 default:
550 gcc_unreachable ();
551 }
552 }
553
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
555
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
558 {
559 switch (cfi)
560 {
561 case DW_CFA_def_cfa:
562 case DW_CFA_def_cfa_sf:
563 case DW_CFA_offset:
564 case DW_CFA_offset_extended_sf:
565 case DW_CFA_offset_extended:
566 return dw_cfi_oprnd_offset;
567
568 case DW_CFA_register:
569 return dw_cfi_oprnd_reg_num;
570
571 case DW_CFA_expression:
572 case DW_CFA_val_expression:
573 return dw_cfi_oprnd_loc;
574
575 default:
576 return dw_cfi_oprnd_unused;
577 }
578 }
579
580 /* Output one FDE. */
581
582 static void
583 output_fde (dw_fde_ref fde, bool for_eh, bool second,
584 char *section_start_label, int fde_encoding, char *augmentation,
585 bool any_lsda_needed, int lsda_encoding)
586 {
587 const char *begin, *end;
588 static unsigned int j;
589 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
590
591 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
592 /* empty */ 0);
593 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
594 for_eh + j);
595 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
596 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
597 if (!XCOFF_DEBUGGING_INFO || for_eh)
598 {
599 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
600 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
601 " indicating 64-bit DWARF extension");
602 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
603 "FDE Length");
604 }
605 ASM_OUTPUT_LABEL (asm_out_file, l1);
606
607 if (for_eh)
608 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
609 else
610 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
611 debug_frame_section, "FDE CIE offset");
612
613 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
614 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
615
616 if (for_eh)
617 {
618 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
619 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
620 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
621 "FDE initial location");
622 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
623 end, begin, "FDE address range");
624 }
625 else
626 {
627 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
628 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 }
630
631 if (augmentation[0])
632 {
633 if (any_lsda_needed)
634 {
635 int size = size_of_encoded_value (lsda_encoding);
636
637 if (lsda_encoding == DW_EH_PE_aligned)
638 {
639 int offset = ( 4 /* Length */
640 + 4 /* CIE offset */
641 + 2 * size_of_encoded_value (fde_encoding)
642 + 1 /* Augmentation size */ );
643 int pad = -offset & (PTR_SIZE - 1);
644
645 size += pad;
646 gcc_assert (size_of_uleb128 (size) == 1);
647 }
648
649 dw2_asm_output_data_uleb128 (size, "Augmentation size");
650
651 if (fde->uses_eh_lsda)
652 {
653 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
654 fde->funcdef_number);
655 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
656 gen_rtx_SYMBOL_REF (Pmode, l1),
657 false,
658 "Language Specific Data Area");
659 }
660 else
661 {
662 if (lsda_encoding == DW_EH_PE_aligned)
663 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
664 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
665 "Language Specific Data Area (none)");
666 }
667 }
668 else
669 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 }
671
672 /* Loop through the Call Frame Instructions associated with this FDE. */
673 fde->dw_fde_current_label = begin;
674 {
675 size_t from, until, i;
676
677 from = 0;
678 until = vec_safe_length (fde->dw_fde_cfi);
679
680 if (fde->dw_fde_second_begin == NULL)
681 ;
682 else if (!second)
683 until = fde->dw_fde_switch_cfi_index;
684 else
685 from = fde->dw_fde_switch_cfi_index;
686
687 for (i = from; i < until; i++)
688 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 }
690
691 /* If we are to emit a ref/link from function bodies to their frame tables,
692 do it now. This is typically performed to make sure that tables
693 associated with functions are dragged with them and not discarded in
694 garbage collecting links. We need to do this on a per function basis to
695 cope with -ffunction-sections. */
696
697 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
698 /* Switch to the function section, emit the ref to the tables, and
699 switch *back* into the table section. */
700 switch_to_section (function_section (fde->decl));
701 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
702 switch_to_frame_table_section (for_eh, true);
703 #endif
704
705 /* Pad the FDE out to an address sized boundary. */
706 ASM_OUTPUT_ALIGN (asm_out_file,
707 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
708 ASM_OUTPUT_LABEL (asm_out_file, l2);
709
710 j += 2;
711 }
712
713 /* Return true if frame description entry FDE is needed for EH. */
714
715 static bool
716 fde_needed_for_eh_p (dw_fde_ref fde)
717 {
718 if (flag_asynchronous_unwind_tables)
719 return true;
720
721 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
722 return true;
723
724 if (fde->uses_eh_lsda)
725 return true;
726
727 /* If exceptions are enabled, we have collected nothrow info. */
728 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
729 return false;
730
731 return true;
732 }
733
734 /* Output the call frame information used to record information
735 that relates to calculating the frame pointer, and records the
736 location of saved registers. */
737
738 static void
739 output_call_frame_info (int for_eh)
740 {
741 unsigned int i;
742 dw_fde_ref fde;
743 dw_cfi_ref cfi;
744 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
745 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
746 bool any_lsda_needed = false;
747 char augmentation[6];
748 int augmentation_size;
749 int fde_encoding = DW_EH_PE_absptr;
750 int per_encoding = DW_EH_PE_absptr;
751 int lsda_encoding = DW_EH_PE_absptr;
752 int return_reg;
753 rtx personality = NULL;
754 int dw_cie_version;
755
756 /* Don't emit a CIE if there won't be any FDEs. */
757 if (!fde_vec)
758 return;
759
760 /* Nothing to do if the assembler's doing it all. */
761 if (dwarf2out_do_cfi_asm ())
762 return;
763
764 /* If we don't have any functions we'll want to unwind out of, don't emit
765 any EH unwind information. If we make FDEs linkonce, we may have to
766 emit an empty label for an FDE that wouldn't otherwise be emitted. We
767 want to avoid having an FDE kept around when the function it refers to
768 is discarded. Example where this matters: a primary function template
769 in C++ requires EH information, an explicit specialization doesn't. */
770 if (for_eh)
771 {
772 bool any_eh_needed = false;
773
774 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
775 {
776 if (fde->uses_eh_lsda)
777 any_eh_needed = any_lsda_needed = true;
778 else if (fde_needed_for_eh_p (fde))
779 any_eh_needed = true;
780 else if (TARGET_USES_WEAK_UNWIND_INFO)
781 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 }
783
784 if (!any_eh_needed)
785 return;
786 }
787
788 /* We're going to be generating comments, so turn on app. */
789 if (flag_debug_asm)
790 app_enable ();
791
792 /* Switch to the proper frame section, first time. */
793 switch_to_frame_table_section (for_eh, false);
794
795 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
796 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
797
798 /* Output the CIE. */
799 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
800 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
801 if (!XCOFF_DEBUGGING_INFO || for_eh)
802 {
803 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
804 dw2_asm_output_data (4, 0xffffffff,
805 "Initial length escape value indicating 64-bit DWARF extension");
806 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
807 "Length of Common Information Entry");
808 }
809 ASM_OUTPUT_LABEL (asm_out_file, l1);
810
811 /* Now that the CIE pointer is PC-relative for EH,
812 use 0 to identify the CIE. */
813 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
814 (for_eh ? 0 : DWARF_CIE_ID),
815 "CIE Identifier Tag");
816
817 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
818 use CIE version 1, unless that would produce incorrect results
819 due to overflowing the return register column. */
820 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
821 dw_cie_version = 1;
822 if (return_reg >= 256 || dwarf_version > 2)
823 dw_cie_version = 3;
824 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
825
826 augmentation[0] = 0;
827 augmentation_size = 0;
828
829 personality = current_unit_personality;
830 if (for_eh)
831 {
832 char *p;
833
834 /* Augmentation:
835 z Indicates that a uleb128 is present to size the
836 augmentation section.
837 L Indicates the encoding (and thus presence) of
838 an LSDA pointer in the FDE augmentation.
839 R Indicates a non-default pointer encoding for
840 FDE code pointers.
841 P Indicates the presence of an encoding + language
842 personality routine in the CIE augmentation. */
843
844 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
845 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
846 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
847
848 p = augmentation + 1;
849 if (personality)
850 {
851 *p++ = 'P';
852 augmentation_size += 1 + size_of_encoded_value (per_encoding);
853 assemble_external_libcall (personality);
854 }
855 if (any_lsda_needed)
856 {
857 *p++ = 'L';
858 augmentation_size += 1;
859 }
860 if (fde_encoding != DW_EH_PE_absptr)
861 {
862 *p++ = 'R';
863 augmentation_size += 1;
864 }
865 if (p > augmentation + 1)
866 {
867 augmentation[0] = 'z';
868 *p = '\0';
869 }
870
871 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
872 if (personality && per_encoding == DW_EH_PE_aligned)
873 {
874 int offset = ( 4 /* Length */
875 + 4 /* CIE Id */
876 + 1 /* CIE version */
877 + strlen (augmentation) + 1 /* Augmentation */
878 + size_of_uleb128 (1) /* Code alignment */
879 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
880 + 1 /* RA column */
881 + 1 /* Augmentation size */
882 + 1 /* Personality encoding */ );
883 int pad = -offset & (PTR_SIZE - 1);
884
885 augmentation_size += pad;
886
887 /* Augmentations should be small, so there's scarce need to
888 iterate for a solution. Die if we exceed one uleb128 byte. */
889 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
890 }
891 }
892
893 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
894 if (dw_cie_version >= 4)
895 {
896 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
897 dw2_asm_output_data (1, 0, "CIE Segment Size");
898 }
899 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
900 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
901 "CIE Data Alignment Factor");
902
903 if (dw_cie_version == 1)
904 dw2_asm_output_data (1, return_reg, "CIE RA Column");
905 else
906 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
907
908 if (augmentation[0])
909 {
910 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
911 if (personality)
912 {
913 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
914 eh_data_format_name (per_encoding));
915 dw2_asm_output_encoded_addr_rtx (per_encoding,
916 personality,
917 true, NULL);
918 }
919
920 if (any_lsda_needed)
921 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
922 eh_data_format_name (lsda_encoding));
923
924 if (fde_encoding != DW_EH_PE_absptr)
925 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
926 eh_data_format_name (fde_encoding));
927 }
928
929 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
930 output_cfi (cfi, NULL, for_eh);
931
932 /* Pad the CIE out to an address sized boundary. */
933 ASM_OUTPUT_ALIGN (asm_out_file,
934 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
935 ASM_OUTPUT_LABEL (asm_out_file, l2);
936
937 /* Loop through all of the FDE's. */
938 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
939 {
940 unsigned int k;
941
942 /* Don't emit EH unwind info for leaf functions that don't need it. */
943 if (for_eh && !fde_needed_for_eh_p (fde))
944 continue;
945
946 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
947 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
948 augmentation, any_lsda_needed, lsda_encoding);
949 }
950
951 if (for_eh && targetm.terminate_dw2_eh_frame_info)
952 dw2_asm_output_data (4, 0, "End of Table");
953
954 /* Turn off app to make assembly quicker. */
955 if (flag_debug_asm)
956 app_disable ();
957 }
958
959 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
960
961 static void
962 dwarf2out_do_cfi_startproc (bool second)
963 {
964 int enc;
965 rtx ref;
966 rtx personality = get_personality_function (current_function_decl);
967
968 fprintf (asm_out_file, "\t.cfi_startproc\n");
969
970 if (personality)
971 {
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
973 ref = personality;
974
975 /* ??? The GAS support isn't entirely consistent. We have to
976 handle indirect support ourselves, but PC-relative is done
977 in the assembler. Further, the assembler can't handle any
978 of the weirder relocation types. */
979 if (enc & DW_EH_PE_indirect)
980 ref = dw2_force_const_mem (ref, true);
981
982 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
983 output_addr_const (asm_out_file, ref);
984 fputc ('\n', asm_out_file);
985 }
986
987 if (crtl->uses_eh_lsda)
988 {
989 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
990
991 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
992 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
993 current_function_funcdef_no);
994 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
995 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
996
997 if (enc & DW_EH_PE_indirect)
998 ref = dw2_force_const_mem (ref, true);
999
1000 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1001 output_addr_const (asm_out_file, ref);
1002 fputc ('\n', asm_out_file);
1003 }
1004 }
1005
1006 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1007 this allocation may be done before pass_final. */
1008
1009 dw_fde_ref
1010 dwarf2out_alloc_current_fde (void)
1011 {
1012 dw_fde_ref fde;
1013
1014 fde = ggc_cleared_alloc<dw_fde_node> ();
1015 fde->decl = current_function_decl;
1016 fde->funcdef_number = current_function_funcdef_no;
1017 fde->fde_index = vec_safe_length (fde_vec);
1018 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1019 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1020 fde->nothrow = crtl->nothrow;
1021 fde->drap_reg = INVALID_REGNUM;
1022 fde->vdrap_reg = INVALID_REGNUM;
1023
1024 /* Record the FDE associated with this function. */
1025 cfun->fde = fde;
1026 vec_safe_push (fde_vec, fde);
1027
1028 return fde;
1029 }
1030
1031 /* Output a marker (i.e. a label) for the beginning of a function, before
1032 the prologue. */
1033
1034 void
1035 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1036 unsigned int column ATTRIBUTE_UNUSED,
1037 const char *file ATTRIBUTE_UNUSED)
1038 {
1039 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1040 char * dup_label;
1041 dw_fde_ref fde;
1042 section *fnsec;
1043 bool do_frame;
1044
1045 current_function_func_begin_label = NULL;
1046
1047 do_frame = dwarf2out_do_frame ();
1048
1049 /* ??? current_function_func_begin_label is also used by except.c for
1050 call-site information. We must emit this label if it might be used. */
1051 if (!do_frame
1052 && (!flag_exceptions
1053 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1054 return;
1055
1056 fnsec = function_section (current_function_decl);
1057 switch_to_section (fnsec);
1058 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1059 current_function_funcdef_no);
1060 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1061 current_function_funcdef_no);
1062 dup_label = xstrdup (label);
1063 current_function_func_begin_label = dup_label;
1064
1065 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1066 if (!do_frame)
1067 return;
1068
1069 /* Unlike the debug version, the EH version of frame unwind info is a per-
1070 function setting so we need to record whether we need it for the unit. */
1071 do_eh_frame |= dwarf2out_do_eh_frame ();
1072
1073 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1074 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1075 would include pass_dwarf2_frame. If we've not created the FDE yet,
1076 do so now. */
1077 fde = cfun->fde;
1078 if (fde == NULL)
1079 fde = dwarf2out_alloc_current_fde ();
1080
1081 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1082 fde->dw_fde_begin = dup_label;
1083 fde->dw_fde_current_label = dup_label;
1084 fde->in_std_section = (fnsec == text_section
1085 || (cold_text_section && fnsec == cold_text_section));
1086
1087 /* We only want to output line number information for the genuine dwarf2
1088 prologue case, not the eh frame case. */
1089 #ifdef DWARF2_DEBUGGING_INFO
1090 if (file)
1091 dwarf2out_source_line (line, column, file, 0, true);
1092 #endif
1093
1094 if (dwarf2out_do_cfi_asm ())
1095 dwarf2out_do_cfi_startproc (false);
1096 else
1097 {
1098 rtx personality = get_personality_function (current_function_decl);
1099 if (!current_unit_personality)
1100 current_unit_personality = personality;
1101
1102 /* We cannot keep a current personality per function as without CFI
1103 asm, at the point where we emit the CFI data, there is no current
1104 function anymore. */
1105 if (personality && current_unit_personality != personality)
1106 sorry ("multiple EH personalities are supported only with assemblers "
1107 "supporting .cfi_personality directive");
1108 }
1109 }
1110
1111 /* Output a marker (i.e. a label) for the end of the generated code
1112 for a function prologue. This gets called *after* the prologue code has
1113 been generated. */
1114
1115 void
1116 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1117 const char *file ATTRIBUTE_UNUSED)
1118 {
1119 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1120
1121 /* Output a label to mark the endpoint of the code generated for this
1122 function. */
1123 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1124 current_function_funcdef_no);
1125 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1126 current_function_funcdef_no);
1127 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1128 }
1129
1130 /* Output a marker (i.e. a label) for the beginning of the generated code
1131 for a function epilogue. This gets called *before* the prologue code has
1132 been generated. */
1133
1134 void
1135 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1136 const char *file ATTRIBUTE_UNUSED)
1137 {
1138 dw_fde_ref fde = cfun->fde;
1139 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1140
1141 if (fde->dw_fde_vms_begin_epilogue)
1142 return;
1143
1144 /* Output a label to mark the endpoint of the code generated for this
1145 function. */
1146 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1147 current_function_funcdef_no);
1148 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1149 current_function_funcdef_no);
1150 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1151 }
1152
1153 /* Output a marker (i.e. a label) for the absolute end of the generated code
1154 for a function definition. This gets called *after* the epilogue code has
1155 been generated. */
1156
1157 void
1158 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1159 const char *file ATTRIBUTE_UNUSED)
1160 {
1161 dw_fde_ref fde;
1162 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1163
1164 last_var_location_insn = NULL;
1165 cached_next_real_insn = NULL;
1166
1167 if (dwarf2out_do_cfi_asm ())
1168 fprintf (asm_out_file, "\t.cfi_endproc\n");
1169
1170 /* Output a label to mark the endpoint of the code generated for this
1171 function. */
1172 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1173 current_function_funcdef_no);
1174 ASM_OUTPUT_LABEL (asm_out_file, label);
1175 fde = cfun->fde;
1176 gcc_assert (fde != NULL);
1177 if (fde->dw_fde_second_begin == NULL)
1178 fde->dw_fde_end = xstrdup (label);
1179 }
1180
1181 void
1182 dwarf2out_frame_finish (void)
1183 {
1184 /* Output call frame information. */
1185 if (targetm.debug_unwind_info () == UI_DWARF2)
1186 output_call_frame_info (0);
1187
1188 /* Output another copy for the unwinder. */
1189 if (do_eh_frame)
1190 output_call_frame_info (1);
1191 }
1192
1193 /* Note that the current function section is being used for code. */
1194
1195 static void
1196 dwarf2out_note_section_used (void)
1197 {
1198 section *sec = current_function_section ();
1199 if (sec == text_section)
1200 text_section_used = true;
1201 else if (sec == cold_text_section)
1202 cold_text_section_used = true;
1203 }
1204
1205 static void var_location_switch_text_section (void);
1206 static void set_cur_line_info_table (section *);
1207
1208 void
1209 dwarf2out_switch_text_section (void)
1210 {
1211 section *sect;
1212 dw_fde_ref fde = cfun->fde;
1213
1214 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1215
1216 if (!in_cold_section_p)
1217 {
1218 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1221 }
1222 else
1223 {
1224 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1225 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1226 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1227 }
1228 have_multiple_function_sections = true;
1229
1230 /* There is no need to mark used sections when not debugging. */
1231 if (cold_text_section != NULL)
1232 dwarf2out_note_section_used ();
1233
1234 if (dwarf2out_do_cfi_asm ())
1235 fprintf (asm_out_file, "\t.cfi_endproc\n");
1236
1237 /* Now do the real section switch. */
1238 sect = current_function_section ();
1239 switch_to_section (sect);
1240
1241 fde->second_in_std_section
1242 = (sect == text_section
1243 || (cold_text_section && sect == cold_text_section));
1244
1245 if (dwarf2out_do_cfi_asm ())
1246 dwarf2out_do_cfi_startproc (true);
1247
1248 var_location_switch_text_section ();
1249
1250 if (cold_text_section != NULL)
1251 set_cur_line_info_table (sect);
1252 }
1253 \f
1254 /* And now, the subset of the debugging information support code necessary
1255 for emitting location expressions. */
1256
1257 /* Data about a single source file. */
1258 struct GTY((for_user)) dwarf_file_data {
1259 const char * filename;
1260 int emitted_number;
1261 };
1262
1263 /* Describe an entry into the .debug_addr section. */
1264
1265 enum ate_kind {
1266 ate_kind_rtx,
1267 ate_kind_rtx_dtprel,
1268 ate_kind_label
1269 };
1270
1271 struct GTY((for_user)) addr_table_entry {
1272 enum ate_kind kind;
1273 unsigned int refcount;
1274 unsigned int index;
1275 union addr_table_entry_struct_union
1276 {
1277 rtx GTY ((tag ("0"))) rtl;
1278 char * GTY ((tag ("1"))) label;
1279 }
1280 GTY ((desc ("%1.kind"))) addr;
1281 };
1282
1283 /* Location lists are ranges + location descriptions for that range,
1284 so you can track variables that are in different places over
1285 their entire life. */
1286 typedef struct GTY(()) dw_loc_list_struct {
1287 dw_loc_list_ref dw_loc_next;
1288 const char *begin; /* Label and addr_entry for start of range */
1289 addr_table_entry *begin_entry;
1290 const char *end; /* Label for end of range */
1291 char *ll_symbol; /* Label for beginning of location list.
1292 Only on head of list */
1293 const char *section; /* Section this loclist is relative to */
1294 dw_loc_descr_ref expr;
1295 hashval_t hash;
1296 /* True if all addresses in this and subsequent lists are known to be
1297 resolved. */
1298 bool resolved_addr;
1299 /* True if this list has been replaced by dw_loc_next. */
1300 bool replaced;
1301 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1302 section. */
1303 unsigned char emitted : 1;
1304 /* True if hash field is index rather than hash value. */
1305 unsigned char num_assigned : 1;
1306 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1307 unsigned char offset_emitted : 1;
1308 /* True if note_variable_value_in_expr has been called on it. */
1309 unsigned char noted_variable_value : 1;
1310 /* True if the range should be emitted even if begin and end
1311 are the same. */
1312 bool force;
1313 } dw_loc_list_node;
1314
1315 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1316 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1317
1318 /* Convert a DWARF stack opcode into its string name. */
1319
1320 static const char *
1321 dwarf_stack_op_name (unsigned int op)
1322 {
1323 const char *name = get_DW_OP_name (op);
1324
1325 if (name != NULL)
1326 return name;
1327
1328 return "OP_<unknown>";
1329 }
1330
1331 /* Return a pointer to a newly allocated location description. Location
1332 descriptions are simple expression terms that can be strung
1333 together to form more complicated location (address) descriptions. */
1334
1335 static inline dw_loc_descr_ref
1336 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1337 unsigned HOST_WIDE_INT oprnd2)
1338 {
1339 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1340
1341 descr->dw_loc_opc = op;
1342 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1343 descr->dw_loc_oprnd1.val_entry = NULL;
1344 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1345 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1346 descr->dw_loc_oprnd2.val_entry = NULL;
1347 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1348
1349 return descr;
1350 }
1351
1352 /* Return a pointer to a newly allocated location description for
1353 REG and OFFSET. */
1354
1355 static inline dw_loc_descr_ref
1356 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1357 {
1358 if (reg <= 31)
1359 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1360 offset, 0);
1361 else
1362 return new_loc_descr (DW_OP_bregx, reg, offset);
1363 }
1364
1365 /* Add a location description term to a location description expression. */
1366
1367 static inline void
1368 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1369 {
1370 dw_loc_descr_ref *d;
1371
1372 /* Find the end of the chain. */
1373 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1374 ;
1375
1376 *d = descr;
1377 }
1378
1379 /* Compare two location operands for exact equality. */
1380
1381 static bool
1382 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1383 {
1384 if (a->val_class != b->val_class)
1385 return false;
1386 switch (a->val_class)
1387 {
1388 case dw_val_class_none:
1389 return true;
1390 case dw_val_class_addr:
1391 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1392
1393 case dw_val_class_offset:
1394 case dw_val_class_unsigned_const:
1395 case dw_val_class_const:
1396 case dw_val_class_unsigned_const_implicit:
1397 case dw_val_class_const_implicit:
1398 case dw_val_class_range_list:
1399 /* These are all HOST_WIDE_INT, signed or unsigned. */
1400 return a->v.val_unsigned == b->v.val_unsigned;
1401
1402 case dw_val_class_loc:
1403 return a->v.val_loc == b->v.val_loc;
1404 case dw_val_class_loc_list:
1405 return a->v.val_loc_list == b->v.val_loc_list;
1406 case dw_val_class_die_ref:
1407 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1408 case dw_val_class_fde_ref:
1409 return a->v.val_fde_index == b->v.val_fde_index;
1410 case dw_val_class_lbl_id:
1411 case dw_val_class_lineptr:
1412 case dw_val_class_macptr:
1413 case dw_val_class_loclistsptr:
1414 case dw_val_class_high_pc:
1415 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1416 case dw_val_class_str:
1417 return a->v.val_str == b->v.val_str;
1418 case dw_val_class_flag:
1419 return a->v.val_flag == b->v.val_flag;
1420 case dw_val_class_file:
1421 case dw_val_class_file_implicit:
1422 return a->v.val_file == b->v.val_file;
1423 case dw_val_class_decl_ref:
1424 return a->v.val_decl_ref == b->v.val_decl_ref;
1425
1426 case dw_val_class_const_double:
1427 return (a->v.val_double.high == b->v.val_double.high
1428 && a->v.val_double.low == b->v.val_double.low);
1429
1430 case dw_val_class_wide_int:
1431 return *a->v.val_wide == *b->v.val_wide;
1432
1433 case dw_val_class_vec:
1434 {
1435 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1436 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1437
1438 return (a_len == b_len
1439 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1440 }
1441
1442 case dw_val_class_data8:
1443 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1444
1445 case dw_val_class_vms_delta:
1446 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1447 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1448
1449 case dw_val_class_discr_value:
1450 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1451 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1452 case dw_val_class_discr_list:
1453 /* It makes no sense comparing two discriminant value lists. */
1454 return false;
1455 }
1456 gcc_unreachable ();
1457 }
1458
1459 /* Compare two location atoms for exact equality. */
1460
1461 static bool
1462 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1463 {
1464 if (a->dw_loc_opc != b->dw_loc_opc)
1465 return false;
1466
1467 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1468 address size, but since we always allocate cleared storage it
1469 should be zero for other types of locations. */
1470 if (a->dtprel != b->dtprel)
1471 return false;
1472
1473 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1474 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1475 }
1476
1477 /* Compare two complete location expressions for exact equality. */
1478
1479 bool
1480 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1481 {
1482 while (1)
1483 {
1484 if (a == b)
1485 return true;
1486 if (a == NULL || b == NULL)
1487 return false;
1488 if (!loc_descr_equal_p_1 (a, b))
1489 return false;
1490
1491 a = a->dw_loc_next;
1492 b = b->dw_loc_next;
1493 }
1494 }
1495
1496
1497 /* Add a constant OFFSET to a location expression. */
1498
1499 static void
1500 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1501 {
1502 dw_loc_descr_ref loc;
1503 HOST_WIDE_INT *p;
1504
1505 gcc_assert (*list_head != NULL);
1506
1507 if (!offset)
1508 return;
1509
1510 /* Find the end of the chain. */
1511 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1512 ;
1513
1514 p = NULL;
1515 if (loc->dw_loc_opc == DW_OP_fbreg
1516 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1517 p = &loc->dw_loc_oprnd1.v.val_int;
1518 else if (loc->dw_loc_opc == DW_OP_bregx)
1519 p = &loc->dw_loc_oprnd2.v.val_int;
1520
1521 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1522 offset. Don't optimize if an signed integer overflow would happen. */
1523 if (p != NULL
1524 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1525 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1526 *p += offset;
1527
1528 else if (offset > 0)
1529 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1530
1531 else
1532 {
1533 loc->dw_loc_next
1534 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1535 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1536 }
1537 }
1538
1539 /* Add a constant OFFSET to a location list. */
1540
1541 static void
1542 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1543 {
1544 dw_loc_list_ref d;
1545 for (d = list_head; d != NULL; d = d->dw_loc_next)
1546 loc_descr_plus_const (&d->expr, offset);
1547 }
1548
1549 #define DWARF_REF_SIZE \
1550 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1551
1552 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1553 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1554 DW_FORM_data16 with 128 bits. */
1555 #define DWARF_LARGEST_DATA_FORM_BITS \
1556 (dwarf_version >= 5 ? 128 : 64)
1557
1558 /* Utility inline function for construction of ops that were GNU extension
1559 before DWARF 5. */
1560 static inline enum dwarf_location_atom
1561 dwarf_OP (enum dwarf_location_atom op)
1562 {
1563 switch (op)
1564 {
1565 case DW_OP_implicit_pointer:
1566 if (dwarf_version < 5)
1567 return DW_OP_GNU_implicit_pointer;
1568 break;
1569
1570 case DW_OP_entry_value:
1571 if (dwarf_version < 5)
1572 return DW_OP_GNU_entry_value;
1573 break;
1574
1575 case DW_OP_const_type:
1576 if (dwarf_version < 5)
1577 return DW_OP_GNU_const_type;
1578 break;
1579
1580 case DW_OP_regval_type:
1581 if (dwarf_version < 5)
1582 return DW_OP_GNU_regval_type;
1583 break;
1584
1585 case DW_OP_deref_type:
1586 if (dwarf_version < 5)
1587 return DW_OP_GNU_deref_type;
1588 break;
1589
1590 case DW_OP_convert:
1591 if (dwarf_version < 5)
1592 return DW_OP_GNU_convert;
1593 break;
1594
1595 case DW_OP_reinterpret:
1596 if (dwarf_version < 5)
1597 return DW_OP_GNU_reinterpret;
1598 break;
1599
1600 default:
1601 break;
1602 }
1603 return op;
1604 }
1605
1606 /* Similarly for attributes. */
1607 static inline enum dwarf_attribute
1608 dwarf_AT (enum dwarf_attribute at)
1609 {
1610 switch (at)
1611 {
1612 case DW_AT_call_return_pc:
1613 if (dwarf_version < 5)
1614 return DW_AT_low_pc;
1615 break;
1616
1617 case DW_AT_call_tail_call:
1618 if (dwarf_version < 5)
1619 return DW_AT_GNU_tail_call;
1620 break;
1621
1622 case DW_AT_call_origin:
1623 if (dwarf_version < 5)
1624 return DW_AT_abstract_origin;
1625 break;
1626
1627 case DW_AT_call_target:
1628 if (dwarf_version < 5)
1629 return DW_AT_GNU_call_site_target;
1630 break;
1631
1632 case DW_AT_call_target_clobbered:
1633 if (dwarf_version < 5)
1634 return DW_AT_GNU_call_site_target_clobbered;
1635 break;
1636
1637 case DW_AT_call_parameter:
1638 if (dwarf_version < 5)
1639 return DW_AT_abstract_origin;
1640 break;
1641
1642 case DW_AT_call_value:
1643 if (dwarf_version < 5)
1644 return DW_AT_GNU_call_site_value;
1645 break;
1646
1647 case DW_AT_call_data_value:
1648 if (dwarf_version < 5)
1649 return DW_AT_GNU_call_site_data_value;
1650 break;
1651
1652 case DW_AT_call_all_calls:
1653 if (dwarf_version < 5)
1654 return DW_AT_GNU_all_call_sites;
1655 break;
1656
1657 case DW_AT_call_all_tail_calls:
1658 if (dwarf_version < 5)
1659 return DW_AT_GNU_all_tail_call_sites;
1660 break;
1661
1662 case DW_AT_dwo_name:
1663 if (dwarf_version < 5)
1664 return DW_AT_GNU_dwo_name;
1665 break;
1666
1667 default:
1668 break;
1669 }
1670 return at;
1671 }
1672
1673 /* And similarly for tags. */
1674 static inline enum dwarf_tag
1675 dwarf_TAG (enum dwarf_tag tag)
1676 {
1677 switch (tag)
1678 {
1679 case DW_TAG_call_site:
1680 if (dwarf_version < 5)
1681 return DW_TAG_GNU_call_site;
1682 break;
1683
1684 case DW_TAG_call_site_parameter:
1685 if (dwarf_version < 5)
1686 return DW_TAG_GNU_call_site_parameter;
1687 break;
1688
1689 default:
1690 break;
1691 }
1692 return tag;
1693 }
1694
1695 static unsigned long int get_base_type_offset (dw_die_ref);
1696
1697 /* Return the size of a location descriptor. */
1698
1699 static unsigned long
1700 size_of_loc_descr (dw_loc_descr_ref loc)
1701 {
1702 unsigned long size = 1;
1703
1704 switch (loc->dw_loc_opc)
1705 {
1706 case DW_OP_addr:
1707 size += DWARF2_ADDR_SIZE;
1708 break;
1709 case DW_OP_GNU_addr_index:
1710 case DW_OP_GNU_const_index:
1711 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1712 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1713 break;
1714 case DW_OP_const1u:
1715 case DW_OP_const1s:
1716 size += 1;
1717 break;
1718 case DW_OP_const2u:
1719 case DW_OP_const2s:
1720 size += 2;
1721 break;
1722 case DW_OP_const4u:
1723 case DW_OP_const4s:
1724 size += 4;
1725 break;
1726 case DW_OP_const8u:
1727 case DW_OP_const8s:
1728 size += 8;
1729 break;
1730 case DW_OP_constu:
1731 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1732 break;
1733 case DW_OP_consts:
1734 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1735 break;
1736 case DW_OP_pick:
1737 size += 1;
1738 break;
1739 case DW_OP_plus_uconst:
1740 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1741 break;
1742 case DW_OP_skip:
1743 case DW_OP_bra:
1744 size += 2;
1745 break;
1746 case DW_OP_breg0:
1747 case DW_OP_breg1:
1748 case DW_OP_breg2:
1749 case DW_OP_breg3:
1750 case DW_OP_breg4:
1751 case DW_OP_breg5:
1752 case DW_OP_breg6:
1753 case DW_OP_breg7:
1754 case DW_OP_breg8:
1755 case DW_OP_breg9:
1756 case DW_OP_breg10:
1757 case DW_OP_breg11:
1758 case DW_OP_breg12:
1759 case DW_OP_breg13:
1760 case DW_OP_breg14:
1761 case DW_OP_breg15:
1762 case DW_OP_breg16:
1763 case DW_OP_breg17:
1764 case DW_OP_breg18:
1765 case DW_OP_breg19:
1766 case DW_OP_breg20:
1767 case DW_OP_breg21:
1768 case DW_OP_breg22:
1769 case DW_OP_breg23:
1770 case DW_OP_breg24:
1771 case DW_OP_breg25:
1772 case DW_OP_breg26:
1773 case DW_OP_breg27:
1774 case DW_OP_breg28:
1775 case DW_OP_breg29:
1776 case DW_OP_breg30:
1777 case DW_OP_breg31:
1778 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1779 break;
1780 case DW_OP_regx:
1781 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1782 break;
1783 case DW_OP_fbreg:
1784 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1785 break;
1786 case DW_OP_bregx:
1787 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1788 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1789 break;
1790 case DW_OP_piece:
1791 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1792 break;
1793 case DW_OP_bit_piece:
1794 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1795 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1796 break;
1797 case DW_OP_deref_size:
1798 case DW_OP_xderef_size:
1799 size += 1;
1800 break;
1801 case DW_OP_call2:
1802 size += 2;
1803 break;
1804 case DW_OP_call4:
1805 size += 4;
1806 break;
1807 case DW_OP_call_ref:
1808 case DW_OP_GNU_variable_value:
1809 size += DWARF_REF_SIZE;
1810 break;
1811 case DW_OP_implicit_value:
1812 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1813 + loc->dw_loc_oprnd1.v.val_unsigned;
1814 break;
1815 case DW_OP_implicit_pointer:
1816 case DW_OP_GNU_implicit_pointer:
1817 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1818 break;
1819 case DW_OP_entry_value:
1820 case DW_OP_GNU_entry_value:
1821 {
1822 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1823 size += size_of_uleb128 (op_size) + op_size;
1824 break;
1825 }
1826 case DW_OP_const_type:
1827 case DW_OP_GNU_const_type:
1828 {
1829 unsigned long o
1830 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1831 size += size_of_uleb128 (o) + 1;
1832 switch (loc->dw_loc_oprnd2.val_class)
1833 {
1834 case dw_val_class_vec:
1835 size += loc->dw_loc_oprnd2.v.val_vec.length
1836 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1837 break;
1838 case dw_val_class_const:
1839 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1840 break;
1841 case dw_val_class_const_double:
1842 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1843 break;
1844 case dw_val_class_wide_int:
1845 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1846 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1847 break;
1848 default:
1849 gcc_unreachable ();
1850 }
1851 break;
1852 }
1853 case DW_OP_regval_type:
1854 case DW_OP_GNU_regval_type:
1855 {
1856 unsigned long o
1857 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1858 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1859 + size_of_uleb128 (o);
1860 }
1861 break;
1862 case DW_OP_deref_type:
1863 case DW_OP_GNU_deref_type:
1864 {
1865 unsigned long o
1866 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1867 size += 1 + size_of_uleb128 (o);
1868 }
1869 break;
1870 case DW_OP_convert:
1871 case DW_OP_reinterpret:
1872 case DW_OP_GNU_convert:
1873 case DW_OP_GNU_reinterpret:
1874 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1875 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1876 else
1877 {
1878 unsigned long o
1879 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1880 size += size_of_uleb128 (o);
1881 }
1882 break;
1883 case DW_OP_GNU_parameter_ref:
1884 size += 4;
1885 break;
1886 default:
1887 break;
1888 }
1889
1890 return size;
1891 }
1892
1893 /* Return the size of a series of location descriptors. */
1894
1895 unsigned long
1896 size_of_locs (dw_loc_descr_ref loc)
1897 {
1898 dw_loc_descr_ref l;
1899 unsigned long size;
1900
1901 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1902 field, to avoid writing to a PCH file. */
1903 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1904 {
1905 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1906 break;
1907 size += size_of_loc_descr (l);
1908 }
1909 if (! l)
1910 return size;
1911
1912 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1913 {
1914 l->dw_loc_addr = size;
1915 size += size_of_loc_descr (l);
1916 }
1917
1918 return size;
1919 }
1920
1921 /* Return the size of the value in a DW_AT_discr_value attribute. */
1922
1923 static int
1924 size_of_discr_value (dw_discr_value *discr_value)
1925 {
1926 if (discr_value->pos)
1927 return size_of_uleb128 (discr_value->v.uval);
1928 else
1929 return size_of_sleb128 (discr_value->v.sval);
1930 }
1931
1932 /* Return the size of the value in a DW_AT_discr_list attribute. */
1933
1934 static int
1935 size_of_discr_list (dw_discr_list_ref discr_list)
1936 {
1937 int size = 0;
1938
1939 for (dw_discr_list_ref list = discr_list;
1940 list != NULL;
1941 list = list->dw_discr_next)
1942 {
1943 /* One byte for the discriminant value descriptor, and then one or two
1944 LEB128 numbers, depending on whether it's a single case label or a
1945 range label. */
1946 size += 1;
1947 size += size_of_discr_value (&list->dw_discr_lower_bound);
1948 if (list->dw_discr_range != 0)
1949 size += size_of_discr_value (&list->dw_discr_upper_bound);
1950 }
1951 return size;
1952 }
1953
1954 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1955 static void get_ref_die_offset_label (char *, dw_die_ref);
1956 static unsigned long int get_ref_die_offset (dw_die_ref);
1957
1958 /* Output location description stack opcode's operands (if any).
1959 The for_eh_or_skip parameter controls whether register numbers are
1960 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1961 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1962 info). This should be suppressed for the cases that have not been converted
1963 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1964
1965 static void
1966 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1967 {
1968 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1969 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1970
1971 switch (loc->dw_loc_opc)
1972 {
1973 #ifdef DWARF2_DEBUGGING_INFO
1974 case DW_OP_const2u:
1975 case DW_OP_const2s:
1976 dw2_asm_output_data (2, val1->v.val_int, NULL);
1977 break;
1978 case DW_OP_const4u:
1979 if (loc->dtprel)
1980 {
1981 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1982 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1983 val1->v.val_addr);
1984 fputc ('\n', asm_out_file);
1985 break;
1986 }
1987 /* FALLTHRU */
1988 case DW_OP_const4s:
1989 dw2_asm_output_data (4, val1->v.val_int, NULL);
1990 break;
1991 case DW_OP_const8u:
1992 if (loc->dtprel)
1993 {
1994 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1995 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1996 val1->v.val_addr);
1997 fputc ('\n', asm_out_file);
1998 break;
1999 }
2000 /* FALLTHRU */
2001 case DW_OP_const8s:
2002 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2003 dw2_asm_output_data (8, val1->v.val_int, NULL);
2004 break;
2005 case DW_OP_skip:
2006 case DW_OP_bra:
2007 {
2008 int offset;
2009
2010 gcc_assert (val1->val_class == dw_val_class_loc);
2011 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2012
2013 dw2_asm_output_data (2, offset, NULL);
2014 }
2015 break;
2016 case DW_OP_implicit_value:
2017 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2018 switch (val2->val_class)
2019 {
2020 case dw_val_class_const:
2021 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2022 break;
2023 case dw_val_class_vec:
2024 {
2025 unsigned int elt_size = val2->v.val_vec.elt_size;
2026 unsigned int len = val2->v.val_vec.length;
2027 unsigned int i;
2028 unsigned char *p;
2029
2030 if (elt_size > sizeof (HOST_WIDE_INT))
2031 {
2032 elt_size /= 2;
2033 len *= 2;
2034 }
2035 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2036 i < len;
2037 i++, p += elt_size)
2038 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2039 "fp or vector constant word %u", i);
2040 }
2041 break;
2042 case dw_val_class_const_double:
2043 {
2044 unsigned HOST_WIDE_INT first, second;
2045
2046 if (WORDS_BIG_ENDIAN)
2047 {
2048 first = val2->v.val_double.high;
2049 second = val2->v.val_double.low;
2050 }
2051 else
2052 {
2053 first = val2->v.val_double.low;
2054 second = val2->v.val_double.high;
2055 }
2056 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2057 first, NULL);
2058 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2059 second, NULL);
2060 }
2061 break;
2062 case dw_val_class_wide_int:
2063 {
2064 int i;
2065 int len = get_full_len (*val2->v.val_wide);
2066 if (WORDS_BIG_ENDIAN)
2067 for (i = len - 1; i >= 0; --i)
2068 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2069 val2->v.val_wide->elt (i), NULL);
2070 else
2071 for (i = 0; i < len; ++i)
2072 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2073 val2->v.val_wide->elt (i), NULL);
2074 }
2075 break;
2076 case dw_val_class_addr:
2077 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2078 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2079 break;
2080 default:
2081 gcc_unreachable ();
2082 }
2083 break;
2084 #else
2085 case DW_OP_const2u:
2086 case DW_OP_const2s:
2087 case DW_OP_const4u:
2088 case DW_OP_const4s:
2089 case DW_OP_const8u:
2090 case DW_OP_const8s:
2091 case DW_OP_skip:
2092 case DW_OP_bra:
2093 case DW_OP_implicit_value:
2094 /* We currently don't make any attempt to make sure these are
2095 aligned properly like we do for the main unwind info, so
2096 don't support emitting things larger than a byte if we're
2097 only doing unwinding. */
2098 gcc_unreachable ();
2099 #endif
2100 case DW_OP_const1u:
2101 case DW_OP_const1s:
2102 dw2_asm_output_data (1, val1->v.val_int, NULL);
2103 break;
2104 case DW_OP_constu:
2105 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2106 break;
2107 case DW_OP_consts:
2108 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2109 break;
2110 case DW_OP_pick:
2111 dw2_asm_output_data (1, val1->v.val_int, NULL);
2112 break;
2113 case DW_OP_plus_uconst:
2114 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2115 break;
2116 case DW_OP_breg0:
2117 case DW_OP_breg1:
2118 case DW_OP_breg2:
2119 case DW_OP_breg3:
2120 case DW_OP_breg4:
2121 case DW_OP_breg5:
2122 case DW_OP_breg6:
2123 case DW_OP_breg7:
2124 case DW_OP_breg8:
2125 case DW_OP_breg9:
2126 case DW_OP_breg10:
2127 case DW_OP_breg11:
2128 case DW_OP_breg12:
2129 case DW_OP_breg13:
2130 case DW_OP_breg14:
2131 case DW_OP_breg15:
2132 case DW_OP_breg16:
2133 case DW_OP_breg17:
2134 case DW_OP_breg18:
2135 case DW_OP_breg19:
2136 case DW_OP_breg20:
2137 case DW_OP_breg21:
2138 case DW_OP_breg22:
2139 case DW_OP_breg23:
2140 case DW_OP_breg24:
2141 case DW_OP_breg25:
2142 case DW_OP_breg26:
2143 case DW_OP_breg27:
2144 case DW_OP_breg28:
2145 case DW_OP_breg29:
2146 case DW_OP_breg30:
2147 case DW_OP_breg31:
2148 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2149 break;
2150 case DW_OP_regx:
2151 {
2152 unsigned r = val1->v.val_unsigned;
2153 if (for_eh_or_skip >= 0)
2154 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2155 gcc_assert (size_of_uleb128 (r)
2156 == size_of_uleb128 (val1->v.val_unsigned));
2157 dw2_asm_output_data_uleb128 (r, NULL);
2158 }
2159 break;
2160 case DW_OP_fbreg:
2161 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2162 break;
2163 case DW_OP_bregx:
2164 {
2165 unsigned r = val1->v.val_unsigned;
2166 if (for_eh_or_skip >= 0)
2167 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2168 gcc_assert (size_of_uleb128 (r)
2169 == size_of_uleb128 (val1->v.val_unsigned));
2170 dw2_asm_output_data_uleb128 (r, NULL);
2171 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2172 }
2173 break;
2174 case DW_OP_piece:
2175 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2176 break;
2177 case DW_OP_bit_piece:
2178 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2179 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2180 break;
2181 case DW_OP_deref_size:
2182 case DW_OP_xderef_size:
2183 dw2_asm_output_data (1, val1->v.val_int, NULL);
2184 break;
2185
2186 case DW_OP_addr:
2187 if (loc->dtprel)
2188 {
2189 if (targetm.asm_out.output_dwarf_dtprel)
2190 {
2191 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2192 DWARF2_ADDR_SIZE,
2193 val1->v.val_addr);
2194 fputc ('\n', asm_out_file);
2195 }
2196 else
2197 gcc_unreachable ();
2198 }
2199 else
2200 {
2201 #ifdef DWARF2_DEBUGGING_INFO
2202 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2203 #else
2204 gcc_unreachable ();
2205 #endif
2206 }
2207 break;
2208
2209 case DW_OP_GNU_addr_index:
2210 case DW_OP_GNU_const_index:
2211 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2212 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2213 "(index into .debug_addr)");
2214 break;
2215
2216 case DW_OP_call2:
2217 case DW_OP_call4:
2218 {
2219 unsigned long die_offset
2220 = get_ref_die_offset (val1->v.val_die_ref.die);
2221 /* Make sure the offset has been computed and that we can encode it as
2222 an operand. */
2223 gcc_assert (die_offset > 0
2224 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2225 ? 0xffff
2226 : 0xffffffff));
2227 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2228 die_offset, NULL);
2229 }
2230 break;
2231
2232 case DW_OP_call_ref:
2233 case DW_OP_GNU_variable_value:
2234 {
2235 char label[MAX_ARTIFICIAL_LABEL_BYTES
2236 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2237 gcc_assert (val1->val_class == dw_val_class_die_ref);
2238 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2239 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2240 }
2241 break;
2242
2243 case DW_OP_implicit_pointer:
2244 case DW_OP_GNU_implicit_pointer:
2245 {
2246 char label[MAX_ARTIFICIAL_LABEL_BYTES
2247 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2248 gcc_assert (val1->val_class == dw_val_class_die_ref);
2249 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2250 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2251 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2252 }
2253 break;
2254
2255 case DW_OP_entry_value:
2256 case DW_OP_GNU_entry_value:
2257 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2258 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2259 break;
2260
2261 case DW_OP_const_type:
2262 case DW_OP_GNU_const_type:
2263 {
2264 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2265 gcc_assert (o);
2266 dw2_asm_output_data_uleb128 (o, NULL);
2267 switch (val2->val_class)
2268 {
2269 case dw_val_class_const:
2270 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2271 dw2_asm_output_data (1, l, NULL);
2272 dw2_asm_output_data (l, val2->v.val_int, NULL);
2273 break;
2274 case dw_val_class_vec:
2275 {
2276 unsigned int elt_size = val2->v.val_vec.elt_size;
2277 unsigned int len = val2->v.val_vec.length;
2278 unsigned int i;
2279 unsigned char *p;
2280
2281 l = len * elt_size;
2282 dw2_asm_output_data (1, l, NULL);
2283 if (elt_size > sizeof (HOST_WIDE_INT))
2284 {
2285 elt_size /= 2;
2286 len *= 2;
2287 }
2288 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2289 i < len;
2290 i++, p += elt_size)
2291 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2292 "fp or vector constant word %u", i);
2293 }
2294 break;
2295 case dw_val_class_const_double:
2296 {
2297 unsigned HOST_WIDE_INT first, second;
2298 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2299
2300 dw2_asm_output_data (1, 2 * l, NULL);
2301 if (WORDS_BIG_ENDIAN)
2302 {
2303 first = val2->v.val_double.high;
2304 second = val2->v.val_double.low;
2305 }
2306 else
2307 {
2308 first = val2->v.val_double.low;
2309 second = val2->v.val_double.high;
2310 }
2311 dw2_asm_output_data (l, first, NULL);
2312 dw2_asm_output_data (l, second, NULL);
2313 }
2314 break;
2315 case dw_val_class_wide_int:
2316 {
2317 int i;
2318 int len = get_full_len (*val2->v.val_wide);
2319 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2320
2321 dw2_asm_output_data (1, len * l, NULL);
2322 if (WORDS_BIG_ENDIAN)
2323 for (i = len - 1; i >= 0; --i)
2324 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2325 else
2326 for (i = 0; i < len; ++i)
2327 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2328 }
2329 break;
2330 default:
2331 gcc_unreachable ();
2332 }
2333 }
2334 break;
2335 case DW_OP_regval_type:
2336 case DW_OP_GNU_regval_type:
2337 {
2338 unsigned r = val1->v.val_unsigned;
2339 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2340 gcc_assert (o);
2341 if (for_eh_or_skip >= 0)
2342 {
2343 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2344 gcc_assert (size_of_uleb128 (r)
2345 == size_of_uleb128 (val1->v.val_unsigned));
2346 }
2347 dw2_asm_output_data_uleb128 (r, NULL);
2348 dw2_asm_output_data_uleb128 (o, NULL);
2349 }
2350 break;
2351 case DW_OP_deref_type:
2352 case DW_OP_GNU_deref_type:
2353 {
2354 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2355 gcc_assert (o);
2356 dw2_asm_output_data (1, val1->v.val_int, NULL);
2357 dw2_asm_output_data_uleb128 (o, NULL);
2358 }
2359 break;
2360 case DW_OP_convert:
2361 case DW_OP_reinterpret:
2362 case DW_OP_GNU_convert:
2363 case DW_OP_GNU_reinterpret:
2364 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2365 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2366 else
2367 {
2368 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2369 gcc_assert (o);
2370 dw2_asm_output_data_uleb128 (o, NULL);
2371 }
2372 break;
2373
2374 case DW_OP_GNU_parameter_ref:
2375 {
2376 unsigned long o;
2377 gcc_assert (val1->val_class == dw_val_class_die_ref);
2378 o = get_ref_die_offset (val1->v.val_die_ref.die);
2379 dw2_asm_output_data (4, o, NULL);
2380 }
2381 break;
2382
2383 default:
2384 /* Other codes have no operands. */
2385 break;
2386 }
2387 }
2388
2389 /* Output a sequence of location operations.
2390 The for_eh_or_skip parameter controls whether register numbers are
2391 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2392 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2393 info). This should be suppressed for the cases that have not been converted
2394 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2395
2396 void
2397 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2398 {
2399 for (; loc != NULL; loc = loc->dw_loc_next)
2400 {
2401 enum dwarf_location_atom opc = loc->dw_loc_opc;
2402 /* Output the opcode. */
2403 if (for_eh_or_skip >= 0
2404 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2405 {
2406 unsigned r = (opc - DW_OP_breg0);
2407 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2408 gcc_assert (r <= 31);
2409 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2410 }
2411 else if (for_eh_or_skip >= 0
2412 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2413 {
2414 unsigned r = (opc - DW_OP_reg0);
2415 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2416 gcc_assert (r <= 31);
2417 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2418 }
2419
2420 dw2_asm_output_data (1, opc,
2421 "%s", dwarf_stack_op_name (opc));
2422
2423 /* Output the operand(s) (if any). */
2424 output_loc_operands (loc, for_eh_or_skip);
2425 }
2426 }
2427
2428 /* Output location description stack opcode's operands (if any).
2429 The output is single bytes on a line, suitable for .cfi_escape. */
2430
2431 static void
2432 output_loc_operands_raw (dw_loc_descr_ref loc)
2433 {
2434 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2435 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2436
2437 switch (loc->dw_loc_opc)
2438 {
2439 case DW_OP_addr:
2440 case DW_OP_GNU_addr_index:
2441 case DW_OP_GNU_const_index:
2442 case DW_OP_implicit_value:
2443 /* We cannot output addresses in .cfi_escape, only bytes. */
2444 gcc_unreachable ();
2445
2446 case DW_OP_const1u:
2447 case DW_OP_const1s:
2448 case DW_OP_pick:
2449 case DW_OP_deref_size:
2450 case DW_OP_xderef_size:
2451 fputc (',', asm_out_file);
2452 dw2_asm_output_data_raw (1, val1->v.val_int);
2453 break;
2454
2455 case DW_OP_const2u:
2456 case DW_OP_const2s:
2457 fputc (',', asm_out_file);
2458 dw2_asm_output_data_raw (2, val1->v.val_int);
2459 break;
2460
2461 case DW_OP_const4u:
2462 case DW_OP_const4s:
2463 fputc (',', asm_out_file);
2464 dw2_asm_output_data_raw (4, val1->v.val_int);
2465 break;
2466
2467 case DW_OP_const8u:
2468 case DW_OP_const8s:
2469 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2470 fputc (',', asm_out_file);
2471 dw2_asm_output_data_raw (8, val1->v.val_int);
2472 break;
2473
2474 case DW_OP_skip:
2475 case DW_OP_bra:
2476 {
2477 int offset;
2478
2479 gcc_assert (val1->val_class == dw_val_class_loc);
2480 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2481
2482 fputc (',', asm_out_file);
2483 dw2_asm_output_data_raw (2, offset);
2484 }
2485 break;
2486
2487 case DW_OP_regx:
2488 {
2489 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2490 gcc_assert (size_of_uleb128 (r)
2491 == size_of_uleb128 (val1->v.val_unsigned));
2492 fputc (',', asm_out_file);
2493 dw2_asm_output_data_uleb128_raw (r);
2494 }
2495 break;
2496
2497 case DW_OP_constu:
2498 case DW_OP_plus_uconst:
2499 case DW_OP_piece:
2500 fputc (',', asm_out_file);
2501 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2502 break;
2503
2504 case DW_OP_bit_piece:
2505 fputc (',', asm_out_file);
2506 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2507 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2508 break;
2509
2510 case DW_OP_consts:
2511 case DW_OP_breg0:
2512 case DW_OP_breg1:
2513 case DW_OP_breg2:
2514 case DW_OP_breg3:
2515 case DW_OP_breg4:
2516 case DW_OP_breg5:
2517 case DW_OP_breg6:
2518 case DW_OP_breg7:
2519 case DW_OP_breg8:
2520 case DW_OP_breg9:
2521 case DW_OP_breg10:
2522 case DW_OP_breg11:
2523 case DW_OP_breg12:
2524 case DW_OP_breg13:
2525 case DW_OP_breg14:
2526 case DW_OP_breg15:
2527 case DW_OP_breg16:
2528 case DW_OP_breg17:
2529 case DW_OP_breg18:
2530 case DW_OP_breg19:
2531 case DW_OP_breg20:
2532 case DW_OP_breg21:
2533 case DW_OP_breg22:
2534 case DW_OP_breg23:
2535 case DW_OP_breg24:
2536 case DW_OP_breg25:
2537 case DW_OP_breg26:
2538 case DW_OP_breg27:
2539 case DW_OP_breg28:
2540 case DW_OP_breg29:
2541 case DW_OP_breg30:
2542 case DW_OP_breg31:
2543 case DW_OP_fbreg:
2544 fputc (',', asm_out_file);
2545 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2546 break;
2547
2548 case DW_OP_bregx:
2549 {
2550 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2551 gcc_assert (size_of_uleb128 (r)
2552 == size_of_uleb128 (val1->v.val_unsigned));
2553 fputc (',', asm_out_file);
2554 dw2_asm_output_data_uleb128_raw (r);
2555 fputc (',', asm_out_file);
2556 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2557 }
2558 break;
2559
2560 case DW_OP_implicit_pointer:
2561 case DW_OP_entry_value:
2562 case DW_OP_const_type:
2563 case DW_OP_regval_type:
2564 case DW_OP_deref_type:
2565 case DW_OP_convert:
2566 case DW_OP_reinterpret:
2567 case DW_OP_GNU_implicit_pointer:
2568 case DW_OP_GNU_entry_value:
2569 case DW_OP_GNU_const_type:
2570 case DW_OP_GNU_regval_type:
2571 case DW_OP_GNU_deref_type:
2572 case DW_OP_GNU_convert:
2573 case DW_OP_GNU_reinterpret:
2574 case DW_OP_GNU_parameter_ref:
2575 gcc_unreachable ();
2576 break;
2577
2578 default:
2579 /* Other codes have no operands. */
2580 break;
2581 }
2582 }
2583
2584 void
2585 output_loc_sequence_raw (dw_loc_descr_ref loc)
2586 {
2587 while (1)
2588 {
2589 enum dwarf_location_atom opc = loc->dw_loc_opc;
2590 /* Output the opcode. */
2591 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2592 {
2593 unsigned r = (opc - DW_OP_breg0);
2594 r = DWARF2_FRAME_REG_OUT (r, 1);
2595 gcc_assert (r <= 31);
2596 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2597 }
2598 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2599 {
2600 unsigned r = (opc - DW_OP_reg0);
2601 r = DWARF2_FRAME_REG_OUT (r, 1);
2602 gcc_assert (r <= 31);
2603 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2604 }
2605 /* Output the opcode. */
2606 fprintf (asm_out_file, "%#x", opc);
2607 output_loc_operands_raw (loc);
2608
2609 if (!loc->dw_loc_next)
2610 break;
2611 loc = loc->dw_loc_next;
2612
2613 fputc (',', asm_out_file);
2614 }
2615 }
2616
2617 /* This function builds a dwarf location descriptor sequence from a
2618 dw_cfa_location, adding the given OFFSET to the result of the
2619 expression. */
2620
2621 struct dw_loc_descr_node *
2622 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2623 {
2624 struct dw_loc_descr_node *head, *tmp;
2625
2626 offset += cfa->offset;
2627
2628 if (cfa->indirect)
2629 {
2630 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2631 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2632 head->dw_loc_oprnd1.val_entry = NULL;
2633 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2634 add_loc_descr (&head, tmp);
2635 if (offset != 0)
2636 {
2637 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2638 add_loc_descr (&head, tmp);
2639 }
2640 }
2641 else
2642 head = new_reg_loc_descr (cfa->reg, offset);
2643
2644 return head;
2645 }
2646
2647 /* This function builds a dwarf location descriptor sequence for
2648 the address at OFFSET from the CFA when stack is aligned to
2649 ALIGNMENT byte. */
2650
2651 struct dw_loc_descr_node *
2652 build_cfa_aligned_loc (dw_cfa_location *cfa,
2653 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2654 {
2655 struct dw_loc_descr_node *head;
2656 unsigned int dwarf_fp
2657 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2658
2659 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2660 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2661 {
2662 head = new_reg_loc_descr (dwarf_fp, 0);
2663 add_loc_descr (&head, int_loc_descriptor (alignment));
2664 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2665 loc_descr_plus_const (&head, offset);
2666 }
2667 else
2668 head = new_reg_loc_descr (dwarf_fp, offset);
2669 return head;
2670 }
2671 \f
2672 /* And now, the support for symbolic debugging information. */
2673
2674 /* .debug_str support. */
2675
2676 static void dwarf2out_init (const char *);
2677 static void dwarf2out_finish (const char *);
2678 static void dwarf2out_early_finish (const char *);
2679 static void dwarf2out_assembly_start (void);
2680 static void dwarf2out_define (unsigned int, const char *);
2681 static void dwarf2out_undef (unsigned int, const char *);
2682 static void dwarf2out_start_source_file (unsigned, const char *);
2683 static void dwarf2out_end_source_file (unsigned);
2684 static void dwarf2out_function_decl (tree);
2685 static void dwarf2out_begin_block (unsigned, unsigned);
2686 static void dwarf2out_end_block (unsigned, unsigned);
2687 static bool dwarf2out_ignore_block (const_tree);
2688 static void dwarf2out_early_global_decl (tree);
2689 static void dwarf2out_late_global_decl (tree);
2690 static void dwarf2out_type_decl (tree, int);
2691 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2692 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2693 dw_die_ref);
2694 static void dwarf2out_abstract_function (tree);
2695 static void dwarf2out_var_location (rtx_insn *);
2696 static void dwarf2out_size_function (tree);
2697 static void dwarf2out_begin_function (tree);
2698 static void dwarf2out_end_function (unsigned int);
2699 static void dwarf2out_register_main_translation_unit (tree unit);
2700 static void dwarf2out_set_name (tree, tree);
2701 static void dwarf2out_register_external_die (tree decl, const char *sym,
2702 unsigned HOST_WIDE_INT off);
2703 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2704 unsigned HOST_WIDE_INT *off);
2705
2706 /* The debug hooks structure. */
2707
2708 const struct gcc_debug_hooks dwarf2_debug_hooks =
2709 {
2710 dwarf2out_init,
2711 dwarf2out_finish,
2712 dwarf2out_early_finish,
2713 dwarf2out_assembly_start,
2714 dwarf2out_define,
2715 dwarf2out_undef,
2716 dwarf2out_start_source_file,
2717 dwarf2out_end_source_file,
2718 dwarf2out_begin_block,
2719 dwarf2out_end_block,
2720 dwarf2out_ignore_block,
2721 dwarf2out_source_line,
2722 dwarf2out_begin_prologue,
2723 #if VMS_DEBUGGING_INFO
2724 dwarf2out_vms_end_prologue,
2725 dwarf2out_vms_begin_epilogue,
2726 #else
2727 debug_nothing_int_charstar,
2728 debug_nothing_int_charstar,
2729 #endif
2730 dwarf2out_end_epilogue,
2731 dwarf2out_begin_function,
2732 dwarf2out_end_function, /* end_function */
2733 dwarf2out_register_main_translation_unit,
2734 dwarf2out_function_decl, /* function_decl */
2735 dwarf2out_early_global_decl,
2736 dwarf2out_late_global_decl,
2737 dwarf2out_type_decl, /* type_decl */
2738 dwarf2out_imported_module_or_decl,
2739 dwarf2out_die_ref_for_decl,
2740 dwarf2out_register_external_die,
2741 debug_nothing_tree, /* deferred_inline_function */
2742 /* The DWARF 2 backend tries to reduce debugging bloat by not
2743 emitting the abstract description of inline functions until
2744 something tries to reference them. */
2745 dwarf2out_abstract_function, /* outlining_inline_function */
2746 debug_nothing_rtx_code_label, /* label */
2747 debug_nothing_int, /* handle_pch */
2748 dwarf2out_var_location,
2749 dwarf2out_size_function, /* size_function */
2750 dwarf2out_switch_text_section,
2751 dwarf2out_set_name,
2752 1, /* start_end_main_source_file */
2753 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2754 };
2755
2756 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2757 {
2758 dwarf2out_init,
2759 debug_nothing_charstar,
2760 debug_nothing_charstar,
2761 dwarf2out_assembly_start,
2762 debug_nothing_int_charstar,
2763 debug_nothing_int_charstar,
2764 debug_nothing_int_charstar,
2765 debug_nothing_int,
2766 debug_nothing_int_int, /* begin_block */
2767 debug_nothing_int_int, /* end_block */
2768 debug_true_const_tree, /* ignore_block */
2769 dwarf2out_source_line, /* source_line */
2770 debug_nothing_int_int_charstar, /* begin_prologue */
2771 debug_nothing_int_charstar, /* end_prologue */
2772 debug_nothing_int_charstar, /* begin_epilogue */
2773 debug_nothing_int_charstar, /* end_epilogue */
2774 debug_nothing_tree, /* begin_function */
2775 debug_nothing_int, /* end_function */
2776 debug_nothing_tree, /* register_main_translation_unit */
2777 debug_nothing_tree, /* function_decl */
2778 debug_nothing_tree, /* early_global_decl */
2779 debug_nothing_tree, /* late_global_decl */
2780 debug_nothing_tree_int, /* type_decl */
2781 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2782 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2783 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2784 debug_nothing_tree, /* deferred_inline_function */
2785 debug_nothing_tree, /* outlining_inline_function */
2786 debug_nothing_rtx_code_label, /* label */
2787 debug_nothing_int, /* handle_pch */
2788 debug_nothing_rtx_insn, /* var_location */
2789 debug_nothing_tree, /* size_function */
2790 debug_nothing_void, /* switch_text_section */
2791 debug_nothing_tree_tree, /* set_name */
2792 0, /* start_end_main_source_file */
2793 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2794 };
2795 \f
2796 /* NOTE: In the comments in this file, many references are made to
2797 "Debugging Information Entries". This term is abbreviated as `DIE'
2798 throughout the remainder of this file. */
2799
2800 /* An internal representation of the DWARF output is built, and then
2801 walked to generate the DWARF debugging info. The walk of the internal
2802 representation is done after the entire program has been compiled.
2803 The types below are used to describe the internal representation. */
2804
2805 /* Whether to put type DIEs into their own section .debug_types instead
2806 of making them part of the .debug_info section. Only supported for
2807 Dwarf V4 or higher and the user didn't disable them through
2808 -fno-debug-types-section. It is more efficient to put them in a
2809 separate comdat sections since the linker will then be able to
2810 remove duplicates. But not all tools support .debug_types sections
2811 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2812 it is DW_UT_type unit type in .debug_info section. */
2813
2814 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2815
2816 /* Various DIE's use offsets relative to the beginning of the
2817 .debug_info section to refer to each other. */
2818
2819 typedef long int dw_offset;
2820
2821 struct comdat_type_node;
2822
2823 /* The entries in the line_info table more-or-less mirror the opcodes
2824 that are used in the real dwarf line table. Arrays of these entries
2825 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2826 supported. */
2827
2828 enum dw_line_info_opcode {
2829 /* Emit DW_LNE_set_address; the operand is the label index. */
2830 LI_set_address,
2831
2832 /* Emit a row to the matrix with the given line. This may be done
2833 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2834 special opcodes. */
2835 LI_set_line,
2836
2837 /* Emit a DW_LNS_set_file. */
2838 LI_set_file,
2839
2840 /* Emit a DW_LNS_set_column. */
2841 LI_set_column,
2842
2843 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2844 LI_negate_stmt,
2845
2846 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2847 LI_set_prologue_end,
2848 LI_set_epilogue_begin,
2849
2850 /* Emit a DW_LNE_set_discriminator. */
2851 LI_set_discriminator
2852 };
2853
2854 typedef struct GTY(()) dw_line_info_struct {
2855 enum dw_line_info_opcode opcode;
2856 unsigned int val;
2857 } dw_line_info_entry;
2858
2859
2860 struct GTY(()) dw_line_info_table {
2861 /* The label that marks the end of this section. */
2862 const char *end_label;
2863
2864 /* The values for the last row of the matrix, as collected in the table.
2865 These are used to minimize the changes to the next row. */
2866 unsigned int file_num;
2867 unsigned int line_num;
2868 unsigned int column_num;
2869 int discrim_num;
2870 bool is_stmt;
2871 bool in_use;
2872
2873 vec<dw_line_info_entry, va_gc> *entries;
2874 };
2875
2876
2877 /* Each DIE attribute has a field specifying the attribute kind,
2878 a link to the next attribute in the chain, and an attribute value.
2879 Attributes are typically linked below the DIE they modify. */
2880
2881 typedef struct GTY(()) dw_attr_struct {
2882 enum dwarf_attribute dw_attr;
2883 dw_val_node dw_attr_val;
2884 }
2885 dw_attr_node;
2886
2887
2888 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2889 The children of each node form a circular list linked by
2890 die_sib. die_child points to the node *before* the "first" child node. */
2891
2892 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2893 union die_symbol_or_type_node
2894 {
2895 const char * GTY ((tag ("0"))) die_symbol;
2896 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2897 }
2898 GTY ((desc ("%0.comdat_type_p"))) die_id;
2899 vec<dw_attr_node, va_gc> *die_attr;
2900 dw_die_ref die_parent;
2901 dw_die_ref die_child;
2902 dw_die_ref die_sib;
2903 dw_die_ref die_definition; /* ref from a specification to its definition */
2904 dw_offset die_offset;
2905 unsigned long die_abbrev;
2906 int die_mark;
2907 unsigned int decl_id;
2908 enum dwarf_tag die_tag;
2909 /* Die is used and must not be pruned as unused. */
2910 BOOL_BITFIELD die_perennial_p : 1;
2911 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2912 /* For an external ref to die_symbol if die_offset contains an extra
2913 offset to that symbol. */
2914 BOOL_BITFIELD with_offset : 1;
2915 /* Whether this DIE was removed from the DIE tree, for example via
2916 prune_unused_types. We don't consider those present from the
2917 DIE lookup routines. */
2918 BOOL_BITFIELD removed : 1;
2919 /* Lots of spare bits. */
2920 }
2921 die_node;
2922
2923 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2924 static bool early_dwarf;
2925 static bool early_dwarf_finished;
2926 struct set_early_dwarf {
2927 bool saved;
2928 set_early_dwarf () : saved(early_dwarf)
2929 {
2930 gcc_assert (! early_dwarf_finished);
2931 early_dwarf = true;
2932 }
2933 ~set_early_dwarf () { early_dwarf = saved; }
2934 };
2935
2936 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2937 #define FOR_EACH_CHILD(die, c, expr) do { \
2938 c = die->die_child; \
2939 if (c) do { \
2940 c = c->die_sib; \
2941 expr; \
2942 } while (c != die->die_child); \
2943 } while (0)
2944
2945 /* The pubname structure */
2946
2947 typedef struct GTY(()) pubname_struct {
2948 dw_die_ref die;
2949 const char *name;
2950 }
2951 pubname_entry;
2952
2953
2954 struct GTY(()) dw_ranges {
2955 const char *label;
2956 /* If this is positive, it's a block number, otherwise it's a
2957 bitwise-negated index into dw_ranges_by_label. */
2958 int num;
2959 /* Index for the range list for DW_FORM_rnglistx. */
2960 unsigned int idx : 31;
2961 /* True if this range might be possibly in a different section
2962 from previous entry. */
2963 unsigned int maybe_new_sec : 1;
2964 };
2965
2966 /* A structure to hold a macinfo entry. */
2967
2968 typedef struct GTY(()) macinfo_struct {
2969 unsigned char code;
2970 unsigned HOST_WIDE_INT lineno;
2971 const char *info;
2972 }
2973 macinfo_entry;
2974
2975
2976 struct GTY(()) dw_ranges_by_label {
2977 const char *begin;
2978 const char *end;
2979 };
2980
2981 /* The comdat type node structure. */
2982 struct GTY(()) comdat_type_node
2983 {
2984 dw_die_ref root_die;
2985 dw_die_ref type_die;
2986 dw_die_ref skeleton_die;
2987 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2988 comdat_type_node *next;
2989 };
2990
2991 /* A list of DIEs for which we can't determine ancestry (parent_die
2992 field) just yet. Later in dwarf2out_finish we will fill in the
2993 missing bits. */
2994 typedef struct GTY(()) limbo_die_struct {
2995 dw_die_ref die;
2996 /* The tree for which this DIE was created. We use this to
2997 determine ancestry later. */
2998 tree created_for;
2999 struct limbo_die_struct *next;
3000 }
3001 limbo_die_node;
3002
3003 typedef struct skeleton_chain_struct
3004 {
3005 dw_die_ref old_die;
3006 dw_die_ref new_die;
3007 struct skeleton_chain_struct *parent;
3008 }
3009 skeleton_chain_node;
3010
3011 /* Define a macro which returns nonzero for a TYPE_DECL which was
3012 implicitly generated for a type.
3013
3014 Note that, unlike the C front-end (which generates a NULL named
3015 TYPE_DECL node for each complete tagged type, each array type,
3016 and each function type node created) the C++ front-end generates
3017 a _named_ TYPE_DECL node for each tagged type node created.
3018 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3019 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3020 front-end, but for each type, tagged or not. */
3021
3022 #define TYPE_DECL_IS_STUB(decl) \
3023 (DECL_NAME (decl) == NULL_TREE \
3024 || (DECL_ARTIFICIAL (decl) \
3025 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3026 /* This is necessary for stub decls that \
3027 appear in nested inline functions. */ \
3028 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3029 && (decl_ultimate_origin (decl) \
3030 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3031
3032 /* Information concerning the compilation unit's programming
3033 language, and compiler version. */
3034
3035 /* Fixed size portion of the DWARF compilation unit header. */
3036 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3037 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3038 + (dwarf_version >= 5 ? 4 : 3))
3039
3040 /* Fixed size portion of the DWARF comdat type unit header. */
3041 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3042 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3043 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3044
3045 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3046 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3047 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3048
3049 /* Fixed size portion of public names info. */
3050 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3051
3052 /* Fixed size portion of the address range info. */
3053 #define DWARF_ARANGES_HEADER_SIZE \
3054 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3055 DWARF2_ADDR_SIZE * 2) \
3056 - DWARF_INITIAL_LENGTH_SIZE)
3057
3058 /* Size of padding portion in the address range info. It must be
3059 aligned to twice the pointer size. */
3060 #define DWARF_ARANGES_PAD_SIZE \
3061 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3062 DWARF2_ADDR_SIZE * 2) \
3063 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3064
3065 /* Use assembler line directives if available. */
3066 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3067 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3068 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3069 #else
3070 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3071 #endif
3072 #endif
3073
3074 /* Minimum line offset in a special line info. opcode.
3075 This value was chosen to give a reasonable range of values. */
3076 #define DWARF_LINE_BASE -10
3077
3078 /* First special line opcode - leave room for the standard opcodes. */
3079 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3080
3081 /* Range of line offsets in a special line info. opcode. */
3082 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3083
3084 /* Flag that indicates the initial value of the is_stmt_start flag.
3085 In the present implementation, we do not mark any lines as
3086 the beginning of a source statement, because that information
3087 is not made available by the GCC front-end. */
3088 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3089
3090 /* Maximum number of operations per instruction bundle. */
3091 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3092 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3093 #endif
3094
3095 /* This location is used by calc_die_sizes() to keep track
3096 the offset of each DIE within the .debug_info section. */
3097 static unsigned long next_die_offset;
3098
3099 /* Record the root of the DIE's built for the current compilation unit. */
3100 static GTY(()) dw_die_ref single_comp_unit_die;
3101
3102 /* A list of type DIEs that have been separated into comdat sections. */
3103 static GTY(()) comdat_type_node *comdat_type_list;
3104
3105 /* A list of CU DIEs that have been separated. */
3106 static GTY(()) limbo_die_node *cu_die_list;
3107
3108 /* A list of DIEs with a NULL parent waiting to be relocated. */
3109 static GTY(()) limbo_die_node *limbo_die_list;
3110
3111 /* A list of DIEs for which we may have to generate
3112 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3113 static GTY(()) limbo_die_node *deferred_asm_name;
3114
3115 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3116 {
3117 typedef const char *compare_type;
3118
3119 static hashval_t hash (dwarf_file_data *);
3120 static bool equal (dwarf_file_data *, const char *);
3121 };
3122
3123 /* Filenames referenced by this compilation unit. */
3124 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3125
3126 struct decl_die_hasher : ggc_ptr_hash<die_node>
3127 {
3128 typedef tree compare_type;
3129
3130 static hashval_t hash (die_node *);
3131 static bool equal (die_node *, tree);
3132 };
3133 /* A hash table of references to DIE's that describe declarations.
3134 The key is a DECL_UID() which is a unique number identifying each decl. */
3135 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3136
3137 struct GTY ((for_user)) variable_value_struct {
3138 unsigned int decl_id;
3139 vec<dw_die_ref, va_gc> *dies;
3140 };
3141
3142 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3143 {
3144 typedef tree compare_type;
3145
3146 static hashval_t hash (variable_value_struct *);
3147 static bool equal (variable_value_struct *, tree);
3148 };
3149 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3150 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3151 DECL_CONTEXT of the referenced VAR_DECLs. */
3152 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3153
3154 struct block_die_hasher : ggc_ptr_hash<die_struct>
3155 {
3156 static hashval_t hash (die_struct *);
3157 static bool equal (die_struct *, die_struct *);
3158 };
3159
3160 /* A hash table of references to DIE's that describe COMMON blocks.
3161 The key is DECL_UID() ^ die_parent. */
3162 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3163
3164 typedef struct GTY(()) die_arg_entry_struct {
3165 dw_die_ref die;
3166 tree arg;
3167 } die_arg_entry;
3168
3169
3170 /* Node of the variable location list. */
3171 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3172 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3173 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3174 in mode of the EXPR_LIST node and first EXPR_LIST operand
3175 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3176 location or NULL for padding. For larger bitsizes,
3177 mode is 0 and first operand is a CONCAT with bitsize
3178 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3179 NULL as second operand. */
3180 rtx GTY (()) loc;
3181 const char * GTY (()) label;
3182 struct var_loc_node * GTY (()) next;
3183 };
3184
3185 /* Variable location list. */
3186 struct GTY ((for_user)) var_loc_list_def {
3187 struct var_loc_node * GTY (()) first;
3188
3189 /* Pointer to the last but one or last element of the
3190 chained list. If the list is empty, both first and
3191 last are NULL, if the list contains just one node
3192 or the last node certainly is not redundant, it points
3193 to the last node, otherwise points to the last but one.
3194 Do not mark it for GC because it is marked through the chain. */
3195 struct var_loc_node * GTY ((skip ("%h"))) last;
3196
3197 /* Pointer to the last element before section switch,
3198 if NULL, either sections weren't switched or first
3199 is after section switch. */
3200 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3201
3202 /* DECL_UID of the variable decl. */
3203 unsigned int decl_id;
3204 };
3205 typedef struct var_loc_list_def var_loc_list;
3206
3207 /* Call argument location list. */
3208 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3209 rtx GTY (()) call_arg_loc_note;
3210 const char * GTY (()) label;
3211 tree GTY (()) block;
3212 bool tail_call_p;
3213 rtx GTY (()) symbol_ref;
3214 struct call_arg_loc_node * GTY (()) next;
3215 };
3216
3217
3218 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3219 {
3220 typedef const_tree compare_type;
3221
3222 static hashval_t hash (var_loc_list *);
3223 static bool equal (var_loc_list *, const_tree);
3224 };
3225
3226 /* Table of decl location linked lists. */
3227 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3228
3229 /* Head and tail of call_arg_loc chain. */
3230 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3231 static struct call_arg_loc_node *call_arg_loc_last;
3232
3233 /* Number of call sites in the current function. */
3234 static int call_site_count = -1;
3235 /* Number of tail call sites in the current function. */
3236 static int tail_call_site_count = -1;
3237
3238 /* A cached location list. */
3239 struct GTY ((for_user)) cached_dw_loc_list_def {
3240 /* The DECL_UID of the decl that this entry describes. */
3241 unsigned int decl_id;
3242
3243 /* The cached location list. */
3244 dw_loc_list_ref loc_list;
3245 };
3246 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3247
3248 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3249 {
3250
3251 typedef const_tree compare_type;
3252
3253 static hashval_t hash (cached_dw_loc_list *);
3254 static bool equal (cached_dw_loc_list *, const_tree);
3255 };
3256
3257 /* Table of cached location lists. */
3258 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3259
3260 /* A vector of references to DIE's that are uniquely identified by their tag,
3261 presence/absence of children DIE's, and list of attribute/value pairs. */
3262 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3263
3264 /* A hash map to remember the stack usage for DWARF procedures. The value
3265 stored is the stack size difference between before the DWARF procedure
3266 invokation and after it returned. In other words, for a DWARF procedure
3267 that consumes N stack slots and that pushes M ones, this stores M - N. */
3268 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3269
3270 /* A global counter for generating labels for line number data. */
3271 static unsigned int line_info_label_num;
3272
3273 /* The current table to which we should emit line number information
3274 for the current function. This will be set up at the beginning of
3275 assembly for the function. */
3276 static GTY(()) dw_line_info_table *cur_line_info_table;
3277
3278 /* The two default tables of line number info. */
3279 static GTY(()) dw_line_info_table *text_section_line_info;
3280 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3281
3282 /* The set of all non-default tables of line number info. */
3283 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3284
3285 /* A flag to tell pubnames/types export if there is an info section to
3286 refer to. */
3287 static bool info_section_emitted;
3288
3289 /* A pointer to the base of a table that contains a list of publicly
3290 accessible names. */
3291 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3292
3293 /* A pointer to the base of a table that contains a list of publicly
3294 accessible types. */
3295 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3296
3297 /* A pointer to the base of a table that contains a list of macro
3298 defines/undefines (and file start/end markers). */
3299 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3300
3301 /* True if .debug_macinfo or .debug_macros section is going to be
3302 emitted. */
3303 #define have_macinfo \
3304 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3305 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3306 && !macinfo_table->is_empty ())
3307
3308 /* Vector of dies for which we should generate .debug_ranges info. */
3309 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3310
3311 /* Vector of pairs of labels referenced in ranges_table. */
3312 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3313
3314 /* Whether we have location lists that need outputting */
3315 static GTY(()) bool have_location_lists;
3316
3317 /* Unique label counter. */
3318 static GTY(()) unsigned int loclabel_num;
3319
3320 /* Unique label counter for point-of-call tables. */
3321 static GTY(()) unsigned int poc_label_num;
3322
3323 /* The last file entry emitted by maybe_emit_file(). */
3324 static GTY(()) struct dwarf_file_data * last_emitted_file;
3325
3326 /* Number of internal labels generated by gen_internal_sym(). */
3327 static GTY(()) int label_num;
3328
3329 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3330
3331 /* Instances of generic types for which we need to generate debug
3332 info that describe their generic parameters and arguments. That
3333 generation needs to happen once all types are properly laid out so
3334 we do it at the end of compilation. */
3335 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3336
3337 /* Offset from the "steady-state frame pointer" to the frame base,
3338 within the current function. */
3339 static HOST_WIDE_INT frame_pointer_fb_offset;
3340 static bool frame_pointer_fb_offset_valid;
3341
3342 static vec<dw_die_ref> base_types;
3343
3344 /* Flags to represent a set of attribute classes for attributes that represent
3345 a scalar value (bounds, pointers, ...). */
3346 enum dw_scalar_form
3347 {
3348 dw_scalar_form_constant = 0x01,
3349 dw_scalar_form_exprloc = 0x02,
3350 dw_scalar_form_reference = 0x04
3351 };
3352
3353 /* Forward declarations for functions defined in this file. */
3354
3355 static int is_pseudo_reg (const_rtx);
3356 static tree type_main_variant (tree);
3357 static int is_tagged_type (const_tree);
3358 static const char *dwarf_tag_name (unsigned);
3359 static const char *dwarf_attr_name (unsigned);
3360 static const char *dwarf_form_name (unsigned);
3361 static tree decl_ultimate_origin (const_tree);
3362 static tree decl_class_context (tree);
3363 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3364 static inline enum dw_val_class AT_class (dw_attr_node *);
3365 static inline unsigned int AT_index (dw_attr_node *);
3366 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3367 static inline unsigned AT_flag (dw_attr_node *);
3368 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3369 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3370 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3371 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3372 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3373 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3374 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3375 unsigned int, unsigned char *);
3376 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3377 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3378 static inline const char *AT_string (dw_attr_node *);
3379 static enum dwarf_form AT_string_form (dw_attr_node *);
3380 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3381 static void add_AT_specification (dw_die_ref, dw_die_ref);
3382 static inline dw_die_ref AT_ref (dw_attr_node *);
3383 static inline int AT_ref_external (dw_attr_node *);
3384 static inline void set_AT_ref_external (dw_attr_node *, int);
3385 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3386 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3387 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3388 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3389 dw_loc_list_ref);
3390 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3391 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3392 static void remove_addr_table_entry (addr_table_entry *);
3393 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3394 static inline rtx AT_addr (dw_attr_node *);
3395 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3396 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3397 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3398 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3399 const char *);
3400 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3401 unsigned HOST_WIDE_INT);
3402 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3403 unsigned long, bool);
3404 static inline const char *AT_lbl (dw_attr_node *);
3405 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3406 static const char *get_AT_low_pc (dw_die_ref);
3407 static const char *get_AT_hi_pc (dw_die_ref);
3408 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3409 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3410 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3411 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3412 static bool is_cxx (void);
3413 static bool is_cxx (const_tree);
3414 static bool is_fortran (void);
3415 static bool is_ada (void);
3416 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3417 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3418 static void add_child_die (dw_die_ref, dw_die_ref);
3419 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3420 static dw_die_ref lookup_type_die (tree);
3421 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3422 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3423 static void equate_type_number_to_die (tree, dw_die_ref);
3424 static dw_die_ref lookup_decl_die (tree);
3425 static var_loc_list *lookup_decl_loc (const_tree);
3426 static void equate_decl_number_to_die (tree, dw_die_ref);
3427 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3428 static void print_spaces (FILE *);
3429 static void print_die (dw_die_ref, FILE *);
3430 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3431 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3432 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3433 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3434 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3435 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3436 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3437 struct md5_ctx *, int *);
3438 struct checksum_attributes;
3439 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3440 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3441 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3442 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3443 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3444 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3445 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3446 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3447 static int is_type_die (dw_die_ref);
3448 static int is_comdat_die (dw_die_ref);
3449 static inline bool is_template_instantiation (dw_die_ref);
3450 static int is_declaration_die (dw_die_ref);
3451 static int should_move_die_to_comdat (dw_die_ref);
3452 static dw_die_ref clone_as_declaration (dw_die_ref);
3453 static dw_die_ref clone_die (dw_die_ref);
3454 static dw_die_ref clone_tree (dw_die_ref);
3455 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3456 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3457 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3458 static dw_die_ref generate_skeleton (dw_die_ref);
3459 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3460 dw_die_ref,
3461 dw_die_ref);
3462 static void break_out_comdat_types (dw_die_ref);
3463 static void copy_decls_for_unworthy_types (dw_die_ref);
3464
3465 static void add_sibling_attributes (dw_die_ref);
3466 static void output_location_lists (dw_die_ref);
3467 static int constant_size (unsigned HOST_WIDE_INT);
3468 static unsigned long size_of_die (dw_die_ref);
3469 static void calc_die_sizes (dw_die_ref);
3470 static void calc_base_type_die_sizes (void);
3471 static void mark_dies (dw_die_ref);
3472 static void unmark_dies (dw_die_ref);
3473 static void unmark_all_dies (dw_die_ref);
3474 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3475 static unsigned long size_of_aranges (void);
3476 static enum dwarf_form value_format (dw_attr_node *);
3477 static void output_value_format (dw_attr_node *);
3478 static void output_abbrev_section (void);
3479 static void output_die_abbrevs (unsigned long, dw_die_ref);
3480 static void output_die (dw_die_ref);
3481 static void output_compilation_unit_header (enum dwarf_unit_type);
3482 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3483 static void output_comdat_type_unit (comdat_type_node *);
3484 static const char *dwarf2_name (tree, int);
3485 static void add_pubname (tree, dw_die_ref);
3486 static void add_enumerator_pubname (const char *, dw_die_ref);
3487 static void add_pubname_string (const char *, dw_die_ref);
3488 static void add_pubtype (tree, dw_die_ref);
3489 static void output_pubnames (vec<pubname_entry, va_gc> *);
3490 static void output_aranges (void);
3491 static unsigned int add_ranges (const_tree, bool = false);
3492 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3493 bool *, bool);
3494 static void output_ranges (void);
3495 static dw_line_info_table *new_line_info_table (void);
3496 static void output_line_info (bool);
3497 static void output_file_names (void);
3498 static dw_die_ref base_type_die (tree, bool);
3499 static int is_base_type (tree);
3500 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3501 static int decl_quals (const_tree);
3502 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3503 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3504 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3505 static int type_is_enum (const_tree);
3506 static unsigned int dbx_reg_number (const_rtx);
3507 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3508 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3509 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3510 enum var_init_status);
3511 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3512 enum var_init_status);
3513 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3514 enum var_init_status);
3515 static int is_based_loc (const_rtx);
3516 static bool resolve_one_addr (rtx *);
3517 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3518 enum var_init_status);
3519 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3520 enum var_init_status);
3521 struct loc_descr_context;
3522 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3523 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3524 static dw_loc_list_ref loc_list_from_tree (tree, int,
3525 struct loc_descr_context *);
3526 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3527 struct loc_descr_context *);
3528 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3529 static tree field_type (const_tree);
3530 static unsigned int simple_type_align_in_bits (const_tree);
3531 static unsigned int simple_decl_align_in_bits (const_tree);
3532 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3533 struct vlr_context;
3534 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3535 HOST_WIDE_INT *);
3536 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3537 dw_loc_list_ref);
3538 static void add_data_member_location_attribute (dw_die_ref, tree,
3539 struct vlr_context *);
3540 static bool add_const_value_attribute (dw_die_ref, rtx);
3541 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3542 static void insert_wide_int (const wide_int &, unsigned char *, int);
3543 static void insert_float (const_rtx, unsigned char *);
3544 static rtx rtl_for_decl_location (tree);
3545 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3546 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3547 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3548 static void add_name_attribute (dw_die_ref, const char *);
3549 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3550 static void add_comp_dir_attribute (dw_die_ref);
3551 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3552 struct loc_descr_context *);
3553 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3554 struct loc_descr_context *);
3555 static void add_subscript_info (dw_die_ref, tree, bool);
3556 static void add_byte_size_attribute (dw_die_ref, tree);
3557 static void add_alignment_attribute (dw_die_ref, tree);
3558 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3559 struct vlr_context *);
3560 static void add_bit_size_attribute (dw_die_ref, tree);
3561 static void add_prototyped_attribute (dw_die_ref, tree);
3562 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3563 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3564 static void add_src_coords_attributes (dw_die_ref, tree);
3565 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3566 static void add_discr_value (dw_die_ref, dw_discr_value *);
3567 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3568 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3569 static void push_decl_scope (tree);
3570 static void pop_decl_scope (void);
3571 static dw_die_ref scope_die_for (tree, dw_die_ref);
3572 static inline int local_scope_p (dw_die_ref);
3573 static inline int class_scope_p (dw_die_ref);
3574 static inline int class_or_namespace_scope_p (dw_die_ref);
3575 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3576 static void add_calling_convention_attribute (dw_die_ref, tree);
3577 static const char *type_tag (const_tree);
3578 static tree member_declared_type (const_tree);
3579 #if 0
3580 static const char *decl_start_label (tree);
3581 #endif
3582 static void gen_array_type_die (tree, dw_die_ref);
3583 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3584 #if 0
3585 static void gen_entry_point_die (tree, dw_die_ref);
3586 #endif
3587 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3588 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3589 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3590 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3591 static void gen_formal_types_die (tree, dw_die_ref);
3592 static void gen_subprogram_die (tree, dw_die_ref);
3593 static void gen_variable_die (tree, tree, dw_die_ref);
3594 static void gen_const_die (tree, dw_die_ref);
3595 static void gen_label_die (tree, dw_die_ref);
3596 static void gen_lexical_block_die (tree, dw_die_ref);
3597 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3598 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3599 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3600 static dw_die_ref gen_compile_unit_die (const char *);
3601 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3602 static void gen_member_die (tree, dw_die_ref);
3603 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3604 enum debug_info_usage);
3605 static void gen_subroutine_type_die (tree, dw_die_ref);
3606 static void gen_typedef_die (tree, dw_die_ref);
3607 static void gen_type_die (tree, dw_die_ref);
3608 static void gen_block_die (tree, dw_die_ref);
3609 static void decls_for_scope (tree, dw_die_ref);
3610 static bool is_naming_typedef_decl (const_tree);
3611 static inline dw_die_ref get_context_die (tree);
3612 static void gen_namespace_die (tree, dw_die_ref);
3613 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3614 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3615 static dw_die_ref force_decl_die (tree);
3616 static dw_die_ref force_type_die (tree);
3617 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3618 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3619 static struct dwarf_file_data * lookup_filename (const char *);
3620 static void retry_incomplete_types (void);
3621 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3622 static void gen_generic_params_dies (tree);
3623 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3624 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3625 static void splice_child_die (dw_die_ref, dw_die_ref);
3626 static int file_info_cmp (const void *, const void *);
3627 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3628 const char *, const char *);
3629 static void output_loc_list (dw_loc_list_ref);
3630 static char *gen_internal_sym (const char *);
3631 static bool want_pubnames (void);
3632
3633 static void prune_unmark_dies (dw_die_ref);
3634 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3635 static void prune_unused_types_mark (dw_die_ref, int);
3636 static void prune_unused_types_walk (dw_die_ref);
3637 static void prune_unused_types_walk_attribs (dw_die_ref);
3638 static void prune_unused_types_prune (dw_die_ref);
3639 static void prune_unused_types (void);
3640 static int maybe_emit_file (struct dwarf_file_data *fd);
3641 static inline const char *AT_vms_delta1 (dw_attr_node *);
3642 static inline const char *AT_vms_delta2 (dw_attr_node *);
3643 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3644 const char *, const char *);
3645 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3646 static void gen_remaining_tmpl_value_param_die_attribute (void);
3647 static bool generic_type_p (tree);
3648 static void schedule_generic_params_dies_gen (tree t);
3649 static void gen_scheduled_generic_parms_dies (void);
3650 static void resolve_variable_values (void);
3651
3652 static const char *comp_dir_string (void);
3653
3654 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3655
3656 /* enum for tracking thread-local variables whose address is really an offset
3657 relative to the TLS pointer, which will need link-time relocation, but will
3658 not need relocation by the DWARF consumer. */
3659
3660 enum dtprel_bool
3661 {
3662 dtprel_false = 0,
3663 dtprel_true = 1
3664 };
3665
3666 /* Return the operator to use for an address of a variable. For dtprel_true, we
3667 use DW_OP_const*. For regular variables, which need both link-time
3668 relocation and consumer-level relocation (e.g., to account for shared objects
3669 loaded at a random address), we use DW_OP_addr*. */
3670
3671 static inline enum dwarf_location_atom
3672 dw_addr_op (enum dtprel_bool dtprel)
3673 {
3674 if (dtprel == dtprel_true)
3675 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3676 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3677 else
3678 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3679 }
3680
3681 /* Return a pointer to a newly allocated address location description. If
3682 dwarf_split_debug_info is true, then record the address with the appropriate
3683 relocation. */
3684 static inline dw_loc_descr_ref
3685 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3686 {
3687 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3688
3689 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3690 ref->dw_loc_oprnd1.v.val_addr = addr;
3691 ref->dtprel = dtprel;
3692 if (dwarf_split_debug_info)
3693 ref->dw_loc_oprnd1.val_entry
3694 = add_addr_table_entry (addr,
3695 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3696 else
3697 ref->dw_loc_oprnd1.val_entry = NULL;
3698
3699 return ref;
3700 }
3701
3702 /* Section names used to hold DWARF debugging information. */
3703
3704 #ifndef DEBUG_INFO_SECTION
3705 #define DEBUG_INFO_SECTION ".debug_info"
3706 #endif
3707 #ifndef DEBUG_DWO_INFO_SECTION
3708 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3709 #endif
3710 #ifndef DEBUG_LTO_INFO_SECTION
3711 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3712 #endif
3713 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3714 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3715 #endif
3716 #ifndef DEBUG_ABBREV_SECTION
3717 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3718 #endif
3719 #ifndef DEBUG_LTO_ABBREV_SECTION
3720 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3721 #endif
3722 #ifndef DEBUG_DWO_ABBREV_SECTION
3723 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3724 #endif
3725 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3726 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3727 #endif
3728 #ifndef DEBUG_ARANGES_SECTION
3729 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3730 #endif
3731 #ifndef DEBUG_ADDR_SECTION
3732 #define DEBUG_ADDR_SECTION ".debug_addr"
3733 #endif
3734 #ifndef DEBUG_MACINFO_SECTION
3735 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3736 #endif
3737 #ifndef DEBUG_LTO_MACINFO_SECTION
3738 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3739 #endif
3740 #ifndef DEBUG_DWO_MACINFO_SECTION
3741 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3742 #endif
3743 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3744 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3745 #endif
3746 #ifndef DEBUG_MACRO_SECTION
3747 #define DEBUG_MACRO_SECTION ".debug_macro"
3748 #endif
3749 #ifndef DEBUG_LTO_MACRO_SECTION
3750 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3751 #endif
3752 #ifndef DEBUG_DWO_MACRO_SECTION
3753 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3754 #endif
3755 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3756 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3757 #endif
3758 #ifndef DEBUG_LINE_SECTION
3759 #define DEBUG_LINE_SECTION ".debug_line"
3760 #endif
3761 #ifndef DEBUG_LTO_LINE_SECTION
3762 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3763 #endif
3764 #ifndef DEBUG_DWO_LINE_SECTION
3765 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3766 #endif
3767 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3768 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3769 #endif
3770 #ifndef DEBUG_LOC_SECTION
3771 #define DEBUG_LOC_SECTION ".debug_loc"
3772 #endif
3773 #ifndef DEBUG_DWO_LOC_SECTION
3774 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3775 #endif
3776 #ifndef DEBUG_LOCLISTS_SECTION
3777 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3778 #endif
3779 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3780 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3781 #endif
3782 #ifndef DEBUG_PUBNAMES_SECTION
3783 #define DEBUG_PUBNAMES_SECTION \
3784 ((debug_generate_pub_sections == 2) \
3785 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3786 #endif
3787 #ifndef DEBUG_PUBTYPES_SECTION
3788 #define DEBUG_PUBTYPES_SECTION \
3789 ((debug_generate_pub_sections == 2) \
3790 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3791 #endif
3792 #ifndef DEBUG_STR_OFFSETS_SECTION
3793 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3794 #endif
3795 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3796 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3797 #endif
3798 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3799 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3800 #endif
3801 #ifndef DEBUG_STR_SECTION
3802 #define DEBUG_STR_SECTION ".debug_str"
3803 #endif
3804 #ifndef DEBUG_LTO_STR_SECTION
3805 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3806 #endif
3807 #ifndef DEBUG_STR_DWO_SECTION
3808 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3809 #endif
3810 #ifndef DEBUG_LTO_STR_DWO_SECTION
3811 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3812 #endif
3813 #ifndef DEBUG_RANGES_SECTION
3814 #define DEBUG_RANGES_SECTION ".debug_ranges"
3815 #endif
3816 #ifndef DEBUG_RNGLISTS_SECTION
3817 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3818 #endif
3819 #ifndef DEBUG_LINE_STR_SECTION
3820 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3821 #endif
3822 #ifndef DEBUG_LTO_LINE_STR_SECTION
3823 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3824 #endif
3825
3826 /* Standard ELF section names for compiled code and data. */
3827 #ifndef TEXT_SECTION_NAME
3828 #define TEXT_SECTION_NAME ".text"
3829 #endif
3830
3831 /* Section flags for .debug_str section. */
3832 #define DEBUG_STR_SECTION_FLAGS \
3833 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3834 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3835 : SECTION_DEBUG)
3836
3837 /* Section flags for .debug_str.dwo section. */
3838 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3839
3840 /* Attribute used to refer to the macro section. */
3841 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3842 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3843
3844 /* Labels we insert at beginning sections we can reference instead of
3845 the section names themselves. */
3846
3847 #ifndef TEXT_SECTION_LABEL
3848 #define TEXT_SECTION_LABEL "Ltext"
3849 #endif
3850 #ifndef COLD_TEXT_SECTION_LABEL
3851 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3852 #endif
3853 #ifndef DEBUG_LINE_SECTION_LABEL
3854 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3855 #endif
3856 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3857 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3858 #endif
3859 #ifndef DEBUG_INFO_SECTION_LABEL
3860 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3861 #endif
3862 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3863 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3864 #endif
3865 #ifndef DEBUG_ABBREV_SECTION_LABEL
3866 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3867 #endif
3868 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3869 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3870 #endif
3871 #ifndef DEBUG_ADDR_SECTION_LABEL
3872 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3873 #endif
3874 #ifndef DEBUG_LOC_SECTION_LABEL
3875 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3876 #endif
3877 #ifndef DEBUG_RANGES_SECTION_LABEL
3878 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3879 #endif
3880 #ifndef DEBUG_MACINFO_SECTION_LABEL
3881 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3882 #endif
3883 #ifndef DEBUG_MACRO_SECTION_LABEL
3884 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3885 #endif
3886 #define SKELETON_COMP_DIE_ABBREV 1
3887 #define SKELETON_TYPE_DIE_ABBREV 2
3888
3889 /* Definitions of defaults for formats and names of various special
3890 (artificial) labels which may be generated within this file (when the -g
3891 options is used and DWARF2_DEBUGGING_INFO is in effect.
3892 If necessary, these may be overridden from within the tm.h file, but
3893 typically, overriding these defaults is unnecessary. */
3894
3895 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3899 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3900 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3901 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3902 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3903 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3904 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3905 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3906 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3907 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3908 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3909 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3910
3911 #ifndef TEXT_END_LABEL
3912 #define TEXT_END_LABEL "Letext"
3913 #endif
3914 #ifndef COLD_END_LABEL
3915 #define COLD_END_LABEL "Letext_cold"
3916 #endif
3917 #ifndef BLOCK_BEGIN_LABEL
3918 #define BLOCK_BEGIN_LABEL "LBB"
3919 #endif
3920 #ifndef BLOCK_END_LABEL
3921 #define BLOCK_END_LABEL "LBE"
3922 #endif
3923 #ifndef LINE_CODE_LABEL
3924 #define LINE_CODE_LABEL "LM"
3925 #endif
3926
3927 \f
3928 /* Return the root of the DIE's built for the current compilation unit. */
3929 static dw_die_ref
3930 comp_unit_die (void)
3931 {
3932 if (!single_comp_unit_die)
3933 single_comp_unit_die = gen_compile_unit_die (NULL);
3934 return single_comp_unit_die;
3935 }
3936
3937 /* We allow a language front-end to designate a function that is to be
3938 called to "demangle" any name before it is put into a DIE. */
3939
3940 static const char *(*demangle_name_func) (const char *);
3941
3942 void
3943 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3944 {
3945 demangle_name_func = func;
3946 }
3947
3948 /* Test if rtl node points to a pseudo register. */
3949
3950 static inline int
3951 is_pseudo_reg (const_rtx rtl)
3952 {
3953 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3954 || (GET_CODE (rtl) == SUBREG
3955 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3956 }
3957
3958 /* Return a reference to a type, with its const and volatile qualifiers
3959 removed. */
3960
3961 static inline tree
3962 type_main_variant (tree type)
3963 {
3964 type = TYPE_MAIN_VARIANT (type);
3965
3966 /* ??? There really should be only one main variant among any group of
3967 variants of a given type (and all of the MAIN_VARIANT values for all
3968 members of the group should point to that one type) but sometimes the C
3969 front-end messes this up for array types, so we work around that bug
3970 here. */
3971 if (TREE_CODE (type) == ARRAY_TYPE)
3972 while (type != TYPE_MAIN_VARIANT (type))
3973 type = TYPE_MAIN_VARIANT (type);
3974
3975 return type;
3976 }
3977
3978 /* Return nonzero if the given type node represents a tagged type. */
3979
3980 static inline int
3981 is_tagged_type (const_tree type)
3982 {
3983 enum tree_code code = TREE_CODE (type);
3984
3985 return (code == RECORD_TYPE || code == UNION_TYPE
3986 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3987 }
3988
3989 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3990
3991 static void
3992 get_ref_die_offset_label (char *label, dw_die_ref ref)
3993 {
3994 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3995 }
3996
3997 /* Return die_offset of a DIE reference to a base type. */
3998
3999 static unsigned long int
4000 get_base_type_offset (dw_die_ref ref)
4001 {
4002 if (ref->die_offset)
4003 return ref->die_offset;
4004 if (comp_unit_die ()->die_abbrev)
4005 {
4006 calc_base_type_die_sizes ();
4007 gcc_assert (ref->die_offset);
4008 }
4009 return ref->die_offset;
4010 }
4011
4012 /* Return die_offset of a DIE reference other than base type. */
4013
4014 static unsigned long int
4015 get_ref_die_offset (dw_die_ref ref)
4016 {
4017 gcc_assert (ref->die_offset);
4018 return ref->die_offset;
4019 }
4020
4021 /* Convert a DIE tag into its string name. */
4022
4023 static const char *
4024 dwarf_tag_name (unsigned int tag)
4025 {
4026 const char *name = get_DW_TAG_name (tag);
4027
4028 if (name != NULL)
4029 return name;
4030
4031 return "DW_TAG_<unknown>";
4032 }
4033
4034 /* Convert a DWARF attribute code into its string name. */
4035
4036 static const char *
4037 dwarf_attr_name (unsigned int attr)
4038 {
4039 const char *name;
4040
4041 switch (attr)
4042 {
4043 #if VMS_DEBUGGING_INFO
4044 case DW_AT_HP_prologue:
4045 return "DW_AT_HP_prologue";
4046 #else
4047 case DW_AT_MIPS_loop_unroll_factor:
4048 return "DW_AT_MIPS_loop_unroll_factor";
4049 #endif
4050
4051 #if VMS_DEBUGGING_INFO
4052 case DW_AT_HP_epilogue:
4053 return "DW_AT_HP_epilogue";
4054 #else
4055 case DW_AT_MIPS_stride:
4056 return "DW_AT_MIPS_stride";
4057 #endif
4058 }
4059
4060 name = get_DW_AT_name (attr);
4061
4062 if (name != NULL)
4063 return name;
4064
4065 return "DW_AT_<unknown>";
4066 }
4067
4068 /* Convert a DWARF value form code into its string name. */
4069
4070 static const char *
4071 dwarf_form_name (unsigned int form)
4072 {
4073 const char *name = get_DW_FORM_name (form);
4074
4075 if (name != NULL)
4076 return name;
4077
4078 return "DW_FORM_<unknown>";
4079 }
4080 \f
4081 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4082 instance of an inlined instance of a decl which is local to an inline
4083 function, so we have to trace all of the way back through the origin chain
4084 to find out what sort of node actually served as the original seed for the
4085 given block. */
4086
4087 static tree
4088 decl_ultimate_origin (const_tree decl)
4089 {
4090 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4091 return NULL_TREE;
4092
4093 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4094 we're trying to output the abstract instance of this function. */
4095 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4096 return NULL_TREE;
4097
4098 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4099 most distant ancestor, this should never happen. */
4100 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4101
4102 return DECL_ABSTRACT_ORIGIN (decl);
4103 }
4104
4105 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4106 of a virtual function may refer to a base class, so we check the 'this'
4107 parameter. */
4108
4109 static tree
4110 decl_class_context (tree decl)
4111 {
4112 tree context = NULL_TREE;
4113
4114 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4115 context = DECL_CONTEXT (decl);
4116 else
4117 context = TYPE_MAIN_VARIANT
4118 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4119
4120 if (context && !TYPE_P (context))
4121 context = NULL_TREE;
4122
4123 return context;
4124 }
4125 \f
4126 /* Add an attribute/value pair to a DIE. */
4127
4128 static inline void
4129 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4130 {
4131 /* Maybe this should be an assert? */
4132 if (die == NULL)
4133 return;
4134
4135 if (flag_checking)
4136 {
4137 /* Check we do not add duplicate attrs. Can't use get_AT here
4138 because that recurses to the specification/abstract origin DIE. */
4139 dw_attr_node *a;
4140 unsigned ix;
4141 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4142 gcc_assert (a->dw_attr != attr->dw_attr);
4143 }
4144
4145 vec_safe_reserve (die->die_attr, 1);
4146 vec_safe_push (die->die_attr, *attr);
4147 }
4148
4149 static inline enum dw_val_class
4150 AT_class (dw_attr_node *a)
4151 {
4152 return a->dw_attr_val.val_class;
4153 }
4154
4155 /* Return the index for any attribute that will be referenced with a
4156 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4157 are stored in dw_attr_val.v.val_str for reference counting
4158 pruning. */
4159
4160 static inline unsigned int
4161 AT_index (dw_attr_node *a)
4162 {
4163 if (AT_class (a) == dw_val_class_str)
4164 return a->dw_attr_val.v.val_str->index;
4165 else if (a->dw_attr_val.val_entry != NULL)
4166 return a->dw_attr_val.val_entry->index;
4167 return NOT_INDEXED;
4168 }
4169
4170 /* Add a flag value attribute to a DIE. */
4171
4172 static inline void
4173 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4174 {
4175 dw_attr_node attr;
4176
4177 attr.dw_attr = attr_kind;
4178 attr.dw_attr_val.val_class = dw_val_class_flag;
4179 attr.dw_attr_val.val_entry = NULL;
4180 attr.dw_attr_val.v.val_flag = flag;
4181 add_dwarf_attr (die, &attr);
4182 }
4183
4184 static inline unsigned
4185 AT_flag (dw_attr_node *a)
4186 {
4187 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4188 return a->dw_attr_val.v.val_flag;
4189 }
4190
4191 /* Add a signed integer attribute value to a DIE. */
4192
4193 static inline void
4194 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4195 {
4196 dw_attr_node attr;
4197
4198 attr.dw_attr = attr_kind;
4199 attr.dw_attr_val.val_class = dw_val_class_const;
4200 attr.dw_attr_val.val_entry = NULL;
4201 attr.dw_attr_val.v.val_int = int_val;
4202 add_dwarf_attr (die, &attr);
4203 }
4204
4205 static inline HOST_WIDE_INT
4206 AT_int (dw_attr_node *a)
4207 {
4208 gcc_assert (a && (AT_class (a) == dw_val_class_const
4209 || AT_class (a) == dw_val_class_const_implicit));
4210 return a->dw_attr_val.v.val_int;
4211 }
4212
4213 /* Add an unsigned integer attribute value to a DIE. */
4214
4215 static inline void
4216 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4217 unsigned HOST_WIDE_INT unsigned_val)
4218 {
4219 dw_attr_node attr;
4220
4221 attr.dw_attr = attr_kind;
4222 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4223 attr.dw_attr_val.val_entry = NULL;
4224 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4225 add_dwarf_attr (die, &attr);
4226 }
4227
4228 static inline unsigned HOST_WIDE_INT
4229 AT_unsigned (dw_attr_node *a)
4230 {
4231 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4232 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4233 return a->dw_attr_val.v.val_unsigned;
4234 }
4235
4236 /* Add an unsigned wide integer attribute value to a DIE. */
4237
4238 static inline void
4239 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4240 const wide_int& w)
4241 {
4242 dw_attr_node attr;
4243
4244 attr.dw_attr = attr_kind;
4245 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4246 attr.dw_attr_val.val_entry = NULL;
4247 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4248 *attr.dw_attr_val.v.val_wide = w;
4249 add_dwarf_attr (die, &attr);
4250 }
4251
4252 /* Add an unsigned double integer attribute value to a DIE. */
4253
4254 static inline void
4255 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4256 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4257 {
4258 dw_attr_node attr;
4259
4260 attr.dw_attr = attr_kind;
4261 attr.dw_attr_val.val_class = dw_val_class_const_double;
4262 attr.dw_attr_val.val_entry = NULL;
4263 attr.dw_attr_val.v.val_double.high = high;
4264 attr.dw_attr_val.v.val_double.low = low;
4265 add_dwarf_attr (die, &attr);
4266 }
4267
4268 /* Add a floating point attribute value to a DIE and return it. */
4269
4270 static inline void
4271 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4272 unsigned int length, unsigned int elt_size, unsigned char *array)
4273 {
4274 dw_attr_node attr;
4275
4276 attr.dw_attr = attr_kind;
4277 attr.dw_attr_val.val_class = dw_val_class_vec;
4278 attr.dw_attr_val.val_entry = NULL;
4279 attr.dw_attr_val.v.val_vec.length = length;
4280 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4281 attr.dw_attr_val.v.val_vec.array = array;
4282 add_dwarf_attr (die, &attr);
4283 }
4284
4285 /* Add an 8-byte data attribute value to a DIE. */
4286
4287 static inline void
4288 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4289 unsigned char data8[8])
4290 {
4291 dw_attr_node attr;
4292
4293 attr.dw_attr = attr_kind;
4294 attr.dw_attr_val.val_class = dw_val_class_data8;
4295 attr.dw_attr_val.val_entry = NULL;
4296 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4297 add_dwarf_attr (die, &attr);
4298 }
4299
4300 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4301 dwarf_split_debug_info, address attributes in dies destined for the
4302 final executable have force_direct set to avoid using indexed
4303 references. */
4304
4305 static inline void
4306 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4307 bool force_direct)
4308 {
4309 dw_attr_node attr;
4310 char * lbl_id;
4311
4312 lbl_id = xstrdup (lbl_low);
4313 attr.dw_attr = DW_AT_low_pc;
4314 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4315 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4316 if (dwarf_split_debug_info && !force_direct)
4317 attr.dw_attr_val.val_entry
4318 = add_addr_table_entry (lbl_id, ate_kind_label);
4319 else
4320 attr.dw_attr_val.val_entry = NULL;
4321 add_dwarf_attr (die, &attr);
4322
4323 attr.dw_attr = DW_AT_high_pc;
4324 if (dwarf_version < 4)
4325 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4326 else
4327 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4328 lbl_id = xstrdup (lbl_high);
4329 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4330 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4331 && dwarf_split_debug_info && !force_direct)
4332 attr.dw_attr_val.val_entry
4333 = add_addr_table_entry (lbl_id, ate_kind_label);
4334 else
4335 attr.dw_attr_val.val_entry = NULL;
4336 add_dwarf_attr (die, &attr);
4337 }
4338
4339 /* Hash and equality functions for debug_str_hash. */
4340
4341 hashval_t
4342 indirect_string_hasher::hash (indirect_string_node *x)
4343 {
4344 return htab_hash_string (x->str);
4345 }
4346
4347 bool
4348 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4349 {
4350 return strcmp (x1->str, x2) == 0;
4351 }
4352
4353 /* Add STR to the given string hash table. */
4354
4355 static struct indirect_string_node *
4356 find_AT_string_in_table (const char *str,
4357 hash_table<indirect_string_hasher> *table)
4358 {
4359 struct indirect_string_node *node;
4360
4361 indirect_string_node **slot
4362 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4363 if (*slot == NULL)
4364 {
4365 node = ggc_cleared_alloc<indirect_string_node> ();
4366 node->str = ggc_strdup (str);
4367 *slot = node;
4368 }
4369 else
4370 node = *slot;
4371
4372 node->refcount++;
4373 return node;
4374 }
4375
4376 /* Add STR to the indirect string hash table. */
4377
4378 static struct indirect_string_node *
4379 find_AT_string (const char *str)
4380 {
4381 if (! debug_str_hash)
4382 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4383
4384 return find_AT_string_in_table (str, debug_str_hash);
4385 }
4386
4387 /* Add a string attribute value to a DIE. */
4388
4389 static inline void
4390 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4391 {
4392 dw_attr_node attr;
4393 struct indirect_string_node *node;
4394
4395 node = find_AT_string (str);
4396
4397 attr.dw_attr = attr_kind;
4398 attr.dw_attr_val.val_class = dw_val_class_str;
4399 attr.dw_attr_val.val_entry = NULL;
4400 attr.dw_attr_val.v.val_str = node;
4401 add_dwarf_attr (die, &attr);
4402 }
4403
4404 static inline const char *
4405 AT_string (dw_attr_node *a)
4406 {
4407 gcc_assert (a && AT_class (a) == dw_val_class_str);
4408 return a->dw_attr_val.v.val_str->str;
4409 }
4410
4411 /* Call this function directly to bypass AT_string_form's logic to put
4412 the string inline in the die. */
4413
4414 static void
4415 set_indirect_string (struct indirect_string_node *node)
4416 {
4417 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4418 /* Already indirect is a no op. */
4419 if (node->form == DW_FORM_strp
4420 || node->form == DW_FORM_line_strp
4421 || node->form == DW_FORM_GNU_str_index)
4422 {
4423 gcc_assert (node->label);
4424 return;
4425 }
4426 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4427 ++dw2_string_counter;
4428 node->label = xstrdup (label);
4429
4430 if (!dwarf_split_debug_info)
4431 {
4432 node->form = DW_FORM_strp;
4433 node->index = NOT_INDEXED;
4434 }
4435 else
4436 {
4437 node->form = DW_FORM_GNU_str_index;
4438 node->index = NO_INDEX_ASSIGNED;
4439 }
4440 }
4441
4442 /* A helper function for dwarf2out_finish, called to reset indirect
4443 string decisions done for early LTO dwarf output before fat object
4444 dwarf output. */
4445
4446 int
4447 reset_indirect_string (indirect_string_node **h, void *)
4448 {
4449 struct indirect_string_node *node = *h;
4450 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4451 {
4452 free (node->label);
4453 node->label = NULL;
4454 node->form = (dwarf_form) 0;
4455 node->index = 0;
4456 }
4457 return 1;
4458 }
4459
4460 /* Find out whether a string should be output inline in DIE
4461 or out-of-line in .debug_str section. */
4462
4463 static enum dwarf_form
4464 find_string_form (struct indirect_string_node *node)
4465 {
4466 unsigned int len;
4467
4468 if (node->form)
4469 return node->form;
4470
4471 len = strlen (node->str) + 1;
4472
4473 /* If the string is shorter or equal to the size of the reference, it is
4474 always better to put it inline. */
4475 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4476 return node->form = DW_FORM_string;
4477
4478 /* If we cannot expect the linker to merge strings in .debug_str
4479 section, only put it into .debug_str if it is worth even in this
4480 single module. */
4481 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4482 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4483 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4484 return node->form = DW_FORM_string;
4485
4486 set_indirect_string (node);
4487
4488 return node->form;
4489 }
4490
4491 /* Find out whether the string referenced from the attribute should be
4492 output inline in DIE or out-of-line in .debug_str section. */
4493
4494 static enum dwarf_form
4495 AT_string_form (dw_attr_node *a)
4496 {
4497 gcc_assert (a && AT_class (a) == dw_val_class_str);
4498 return find_string_form (a->dw_attr_val.v.val_str);
4499 }
4500
4501 /* Add a DIE reference attribute value to a DIE. */
4502
4503 static inline void
4504 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4505 {
4506 dw_attr_node attr;
4507 gcc_checking_assert (targ_die != NULL);
4508
4509 /* With LTO we can end up trying to reference something we didn't create
4510 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4511 if (targ_die == NULL)
4512 return;
4513
4514 attr.dw_attr = attr_kind;
4515 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4516 attr.dw_attr_val.val_entry = NULL;
4517 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4518 attr.dw_attr_val.v.val_die_ref.external = 0;
4519 add_dwarf_attr (die, &attr);
4520 }
4521
4522 /* Change DIE reference REF to point to NEW_DIE instead. */
4523
4524 static inline void
4525 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4526 {
4527 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4528 ref->dw_attr_val.v.val_die_ref.die = new_die;
4529 ref->dw_attr_val.v.val_die_ref.external = 0;
4530 }
4531
4532 /* Add an AT_specification attribute to a DIE, and also make the back
4533 pointer from the specification to the definition. */
4534
4535 static inline void
4536 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4537 {
4538 add_AT_die_ref (die, DW_AT_specification, targ_die);
4539 gcc_assert (!targ_die->die_definition);
4540 targ_die->die_definition = die;
4541 }
4542
4543 static inline dw_die_ref
4544 AT_ref (dw_attr_node *a)
4545 {
4546 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4547 return a->dw_attr_val.v.val_die_ref.die;
4548 }
4549
4550 static inline int
4551 AT_ref_external (dw_attr_node *a)
4552 {
4553 if (a && AT_class (a) == dw_val_class_die_ref)
4554 return a->dw_attr_val.v.val_die_ref.external;
4555
4556 return 0;
4557 }
4558
4559 static inline void
4560 set_AT_ref_external (dw_attr_node *a, int i)
4561 {
4562 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4563 a->dw_attr_val.v.val_die_ref.external = i;
4564 }
4565
4566 /* Add an FDE reference attribute value to a DIE. */
4567
4568 static inline void
4569 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4570 {
4571 dw_attr_node attr;
4572
4573 attr.dw_attr = attr_kind;
4574 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4575 attr.dw_attr_val.val_entry = NULL;
4576 attr.dw_attr_val.v.val_fde_index = targ_fde;
4577 add_dwarf_attr (die, &attr);
4578 }
4579
4580 /* Add a location description attribute value to a DIE. */
4581
4582 static inline void
4583 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4584 {
4585 dw_attr_node attr;
4586
4587 attr.dw_attr = attr_kind;
4588 attr.dw_attr_val.val_class = dw_val_class_loc;
4589 attr.dw_attr_val.val_entry = NULL;
4590 attr.dw_attr_val.v.val_loc = loc;
4591 add_dwarf_attr (die, &attr);
4592 }
4593
4594 static inline dw_loc_descr_ref
4595 AT_loc (dw_attr_node *a)
4596 {
4597 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4598 return a->dw_attr_val.v.val_loc;
4599 }
4600
4601 static inline void
4602 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4603 {
4604 dw_attr_node attr;
4605
4606 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4607 return;
4608
4609 attr.dw_attr = attr_kind;
4610 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4611 attr.dw_attr_val.val_entry = NULL;
4612 attr.dw_attr_val.v.val_loc_list = loc_list;
4613 add_dwarf_attr (die, &attr);
4614 have_location_lists = true;
4615 }
4616
4617 static inline dw_loc_list_ref
4618 AT_loc_list (dw_attr_node *a)
4619 {
4620 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4621 return a->dw_attr_val.v.val_loc_list;
4622 }
4623
4624 static inline dw_loc_list_ref *
4625 AT_loc_list_ptr (dw_attr_node *a)
4626 {
4627 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4628 return &a->dw_attr_val.v.val_loc_list;
4629 }
4630
4631 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4632 {
4633 static hashval_t hash (addr_table_entry *);
4634 static bool equal (addr_table_entry *, addr_table_entry *);
4635 };
4636
4637 /* Table of entries into the .debug_addr section. */
4638
4639 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4640
4641 /* Hash an address_table_entry. */
4642
4643 hashval_t
4644 addr_hasher::hash (addr_table_entry *a)
4645 {
4646 inchash::hash hstate;
4647 switch (a->kind)
4648 {
4649 case ate_kind_rtx:
4650 hstate.add_int (0);
4651 break;
4652 case ate_kind_rtx_dtprel:
4653 hstate.add_int (1);
4654 break;
4655 case ate_kind_label:
4656 return htab_hash_string (a->addr.label);
4657 default:
4658 gcc_unreachable ();
4659 }
4660 inchash::add_rtx (a->addr.rtl, hstate);
4661 return hstate.end ();
4662 }
4663
4664 /* Determine equality for two address_table_entries. */
4665
4666 bool
4667 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4668 {
4669 if (a1->kind != a2->kind)
4670 return 0;
4671 switch (a1->kind)
4672 {
4673 case ate_kind_rtx:
4674 case ate_kind_rtx_dtprel:
4675 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4676 case ate_kind_label:
4677 return strcmp (a1->addr.label, a2->addr.label) == 0;
4678 default:
4679 gcc_unreachable ();
4680 }
4681 }
4682
4683 /* Initialize an addr_table_entry. */
4684
4685 void
4686 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4687 {
4688 e->kind = kind;
4689 switch (kind)
4690 {
4691 case ate_kind_rtx:
4692 case ate_kind_rtx_dtprel:
4693 e->addr.rtl = (rtx) addr;
4694 break;
4695 case ate_kind_label:
4696 e->addr.label = (char *) addr;
4697 break;
4698 }
4699 e->refcount = 0;
4700 e->index = NO_INDEX_ASSIGNED;
4701 }
4702
4703 /* Add attr to the address table entry to the table. Defer setting an
4704 index until output time. */
4705
4706 static addr_table_entry *
4707 add_addr_table_entry (void *addr, enum ate_kind kind)
4708 {
4709 addr_table_entry *node;
4710 addr_table_entry finder;
4711
4712 gcc_assert (dwarf_split_debug_info);
4713 if (! addr_index_table)
4714 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4715 init_addr_table_entry (&finder, kind, addr);
4716 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4717
4718 if (*slot == HTAB_EMPTY_ENTRY)
4719 {
4720 node = ggc_cleared_alloc<addr_table_entry> ();
4721 init_addr_table_entry (node, kind, addr);
4722 *slot = node;
4723 }
4724 else
4725 node = *slot;
4726
4727 node->refcount++;
4728 return node;
4729 }
4730
4731 /* Remove an entry from the addr table by decrementing its refcount.
4732 Strictly, decrementing the refcount would be enough, but the
4733 assertion that the entry is actually in the table has found
4734 bugs. */
4735
4736 static void
4737 remove_addr_table_entry (addr_table_entry *entry)
4738 {
4739 gcc_assert (dwarf_split_debug_info && addr_index_table);
4740 /* After an index is assigned, the table is frozen. */
4741 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4742 entry->refcount--;
4743 }
4744
4745 /* Given a location list, remove all addresses it refers to from the
4746 address_table. */
4747
4748 static void
4749 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4750 {
4751 for (; descr; descr = descr->dw_loc_next)
4752 if (descr->dw_loc_oprnd1.val_entry != NULL)
4753 {
4754 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4755 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4756 }
4757 }
4758
4759 /* A helper function for dwarf2out_finish called through
4760 htab_traverse. Assign an addr_table_entry its index. All entries
4761 must be collected into the table when this function is called,
4762 because the indexing code relies on htab_traverse to traverse nodes
4763 in the same order for each run. */
4764
4765 int
4766 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4767 {
4768 addr_table_entry *node = *h;
4769
4770 /* Don't index unreferenced nodes. */
4771 if (node->refcount == 0)
4772 return 1;
4773
4774 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4775 node->index = *index;
4776 *index += 1;
4777
4778 return 1;
4779 }
4780
4781 /* Add an address constant attribute value to a DIE. When using
4782 dwarf_split_debug_info, address attributes in dies destined for the
4783 final executable should be direct references--setting the parameter
4784 force_direct ensures this behavior. */
4785
4786 static inline void
4787 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4788 bool force_direct)
4789 {
4790 dw_attr_node attr;
4791
4792 attr.dw_attr = attr_kind;
4793 attr.dw_attr_val.val_class = dw_val_class_addr;
4794 attr.dw_attr_val.v.val_addr = addr;
4795 if (dwarf_split_debug_info && !force_direct)
4796 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4797 else
4798 attr.dw_attr_val.val_entry = NULL;
4799 add_dwarf_attr (die, &attr);
4800 }
4801
4802 /* Get the RTX from to an address DIE attribute. */
4803
4804 static inline rtx
4805 AT_addr (dw_attr_node *a)
4806 {
4807 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4808 return a->dw_attr_val.v.val_addr;
4809 }
4810
4811 /* Add a file attribute value to a DIE. */
4812
4813 static inline void
4814 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4815 struct dwarf_file_data *fd)
4816 {
4817 dw_attr_node attr;
4818
4819 attr.dw_attr = attr_kind;
4820 attr.dw_attr_val.val_class = dw_val_class_file;
4821 attr.dw_attr_val.val_entry = NULL;
4822 attr.dw_attr_val.v.val_file = fd;
4823 add_dwarf_attr (die, &attr);
4824 }
4825
4826 /* Get the dwarf_file_data from a file DIE attribute. */
4827
4828 static inline struct dwarf_file_data *
4829 AT_file (dw_attr_node *a)
4830 {
4831 gcc_assert (a && (AT_class (a) == dw_val_class_file
4832 || AT_class (a) == dw_val_class_file_implicit));
4833 return a->dw_attr_val.v.val_file;
4834 }
4835
4836 /* Add a vms delta attribute value to a DIE. */
4837
4838 static inline void
4839 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4840 const char *lbl1, const char *lbl2)
4841 {
4842 dw_attr_node attr;
4843
4844 attr.dw_attr = attr_kind;
4845 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4846 attr.dw_attr_val.val_entry = NULL;
4847 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4848 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4849 add_dwarf_attr (die, &attr);
4850 }
4851
4852 /* Add a label identifier attribute value to a DIE. */
4853
4854 static inline void
4855 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4856 const char *lbl_id)
4857 {
4858 dw_attr_node attr;
4859
4860 attr.dw_attr = attr_kind;
4861 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4862 attr.dw_attr_val.val_entry = NULL;
4863 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4864 if (dwarf_split_debug_info)
4865 attr.dw_attr_val.val_entry
4866 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4867 ate_kind_label);
4868 add_dwarf_attr (die, &attr);
4869 }
4870
4871 /* Add a section offset attribute value to a DIE, an offset into the
4872 debug_line section. */
4873
4874 static inline void
4875 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4876 const char *label)
4877 {
4878 dw_attr_node attr;
4879
4880 attr.dw_attr = attr_kind;
4881 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4882 attr.dw_attr_val.val_entry = NULL;
4883 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4884 add_dwarf_attr (die, &attr);
4885 }
4886
4887 /* Add a section offset attribute value to a DIE, an offset into the
4888 debug_loclists section. */
4889
4890 static inline void
4891 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4892 const char *label)
4893 {
4894 dw_attr_node attr;
4895
4896 attr.dw_attr = attr_kind;
4897 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4898 attr.dw_attr_val.val_entry = NULL;
4899 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4900 add_dwarf_attr (die, &attr);
4901 }
4902
4903 /* Add a section offset attribute value to a DIE, an offset into the
4904 debug_macinfo section. */
4905
4906 static inline void
4907 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4908 const char *label)
4909 {
4910 dw_attr_node attr;
4911
4912 attr.dw_attr = attr_kind;
4913 attr.dw_attr_val.val_class = dw_val_class_macptr;
4914 attr.dw_attr_val.val_entry = NULL;
4915 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4916 add_dwarf_attr (die, &attr);
4917 }
4918
4919 /* Add an offset attribute value to a DIE. */
4920
4921 static inline void
4922 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4923 unsigned HOST_WIDE_INT offset)
4924 {
4925 dw_attr_node attr;
4926
4927 attr.dw_attr = attr_kind;
4928 attr.dw_attr_val.val_class = dw_val_class_offset;
4929 attr.dw_attr_val.val_entry = NULL;
4930 attr.dw_attr_val.v.val_offset = offset;
4931 add_dwarf_attr (die, &attr);
4932 }
4933
4934 /* Add a range_list attribute value to a DIE. When using
4935 dwarf_split_debug_info, address attributes in dies destined for the
4936 final executable should be direct references--setting the parameter
4937 force_direct ensures this behavior. */
4938
4939 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4940 #define RELOCATED_OFFSET (NULL)
4941
4942 static void
4943 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4944 long unsigned int offset, bool force_direct)
4945 {
4946 dw_attr_node attr;
4947
4948 attr.dw_attr = attr_kind;
4949 attr.dw_attr_val.val_class = dw_val_class_range_list;
4950 /* For the range_list attribute, use val_entry to store whether the
4951 offset should follow split-debug-info or normal semantics. This
4952 value is read in output_range_list_offset. */
4953 if (dwarf_split_debug_info && !force_direct)
4954 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4955 else
4956 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4957 attr.dw_attr_val.v.val_offset = offset;
4958 add_dwarf_attr (die, &attr);
4959 }
4960
4961 /* Return the start label of a delta attribute. */
4962
4963 static inline const char *
4964 AT_vms_delta1 (dw_attr_node *a)
4965 {
4966 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4967 return a->dw_attr_val.v.val_vms_delta.lbl1;
4968 }
4969
4970 /* Return the end label of a delta attribute. */
4971
4972 static inline const char *
4973 AT_vms_delta2 (dw_attr_node *a)
4974 {
4975 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4976 return a->dw_attr_val.v.val_vms_delta.lbl2;
4977 }
4978
4979 static inline const char *
4980 AT_lbl (dw_attr_node *a)
4981 {
4982 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4983 || AT_class (a) == dw_val_class_lineptr
4984 || AT_class (a) == dw_val_class_macptr
4985 || AT_class (a) == dw_val_class_loclistsptr
4986 || AT_class (a) == dw_val_class_high_pc));
4987 return a->dw_attr_val.v.val_lbl_id;
4988 }
4989
4990 /* Get the attribute of type attr_kind. */
4991
4992 static dw_attr_node *
4993 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4994 {
4995 dw_attr_node *a;
4996 unsigned ix;
4997 dw_die_ref spec = NULL;
4998
4999 if (! die)
5000 return NULL;
5001
5002 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5003 if (a->dw_attr == attr_kind)
5004 return a;
5005 else if (a->dw_attr == DW_AT_specification
5006 || a->dw_attr == DW_AT_abstract_origin)
5007 spec = AT_ref (a);
5008
5009 if (spec)
5010 return get_AT (spec, attr_kind);
5011
5012 return NULL;
5013 }
5014
5015 /* Returns the parent of the declaration of DIE. */
5016
5017 static dw_die_ref
5018 get_die_parent (dw_die_ref die)
5019 {
5020 dw_die_ref t;
5021
5022 if (!die)
5023 return NULL;
5024
5025 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5026 || (t = get_AT_ref (die, DW_AT_specification)))
5027 die = t;
5028
5029 return die->die_parent;
5030 }
5031
5032 /* Return the "low pc" attribute value, typically associated with a subprogram
5033 DIE. Return null if the "low pc" attribute is either not present, or if it
5034 cannot be represented as an assembler label identifier. */
5035
5036 static inline const char *
5037 get_AT_low_pc (dw_die_ref die)
5038 {
5039 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5040
5041 return a ? AT_lbl (a) : NULL;
5042 }
5043
5044 /* Return the "high pc" attribute value, typically associated with a subprogram
5045 DIE. Return null if the "high pc" attribute is either not present, or if it
5046 cannot be represented as an assembler label identifier. */
5047
5048 static inline const char *
5049 get_AT_hi_pc (dw_die_ref die)
5050 {
5051 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5052
5053 return a ? AT_lbl (a) : NULL;
5054 }
5055
5056 /* Return the value of the string attribute designated by ATTR_KIND, or
5057 NULL if it is not present. */
5058
5059 static inline const char *
5060 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5061 {
5062 dw_attr_node *a = get_AT (die, attr_kind);
5063
5064 return a ? AT_string (a) : NULL;
5065 }
5066
5067 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5068 if it is not present. */
5069
5070 static inline int
5071 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5072 {
5073 dw_attr_node *a = get_AT (die, attr_kind);
5074
5075 return a ? AT_flag (a) : 0;
5076 }
5077
5078 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5079 if it is not present. */
5080
5081 static inline unsigned
5082 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5083 {
5084 dw_attr_node *a = get_AT (die, attr_kind);
5085
5086 return a ? AT_unsigned (a) : 0;
5087 }
5088
5089 static inline dw_die_ref
5090 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5091 {
5092 dw_attr_node *a = get_AT (die, attr_kind);
5093
5094 return a ? AT_ref (a) : NULL;
5095 }
5096
5097 static inline struct dwarf_file_data *
5098 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5099 {
5100 dw_attr_node *a = get_AT (die, attr_kind);
5101
5102 return a ? AT_file (a) : NULL;
5103 }
5104
5105 /* Return TRUE if the language is C++. */
5106
5107 static inline bool
5108 is_cxx (void)
5109 {
5110 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5111
5112 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5113 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5114 }
5115
5116 /* Return TRUE if DECL was created by the C++ frontend. */
5117
5118 static bool
5119 is_cxx (const_tree decl)
5120 {
5121 if (in_lto_p)
5122 {
5123 const_tree context = get_ultimate_context (decl);
5124 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5125 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5126 }
5127 return is_cxx ();
5128 }
5129
5130 /* Return TRUE if the language is Fortran. */
5131
5132 static inline bool
5133 is_fortran (void)
5134 {
5135 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5136
5137 return (lang == DW_LANG_Fortran77
5138 || lang == DW_LANG_Fortran90
5139 || lang == DW_LANG_Fortran95
5140 || lang == DW_LANG_Fortran03
5141 || lang == DW_LANG_Fortran08);
5142 }
5143
5144 static inline bool
5145 is_fortran (const_tree decl)
5146 {
5147 if (in_lto_p)
5148 {
5149 const_tree context = get_ultimate_context (decl);
5150 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5151 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5152 "GNU Fortran", 11) == 0
5153 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5154 "GNU F77") == 0);
5155 }
5156 return is_fortran ();
5157 }
5158
5159 /* Return TRUE if the language is Ada. */
5160
5161 static inline bool
5162 is_ada (void)
5163 {
5164 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5165
5166 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5167 }
5168
5169 /* Remove the specified attribute if present. Return TRUE if removal
5170 was successful. */
5171
5172 static bool
5173 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5174 {
5175 dw_attr_node *a;
5176 unsigned ix;
5177
5178 if (! die)
5179 return false;
5180
5181 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5182 if (a->dw_attr == attr_kind)
5183 {
5184 if (AT_class (a) == dw_val_class_str)
5185 if (a->dw_attr_val.v.val_str->refcount)
5186 a->dw_attr_val.v.val_str->refcount--;
5187
5188 /* vec::ordered_remove should help reduce the number of abbrevs
5189 that are needed. */
5190 die->die_attr->ordered_remove (ix);
5191 return true;
5192 }
5193 return false;
5194 }
5195
5196 /* Remove CHILD from its parent. PREV must have the property that
5197 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5198
5199 static void
5200 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5201 {
5202 gcc_assert (child->die_parent == prev->die_parent);
5203 gcc_assert (prev->die_sib == child);
5204 if (prev == child)
5205 {
5206 gcc_assert (child->die_parent->die_child == child);
5207 prev = NULL;
5208 }
5209 else
5210 prev->die_sib = child->die_sib;
5211 if (child->die_parent->die_child == child)
5212 child->die_parent->die_child = prev;
5213 child->die_sib = NULL;
5214 }
5215
5216 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5217 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5218
5219 static void
5220 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5221 {
5222 dw_die_ref parent = old_child->die_parent;
5223
5224 gcc_assert (parent == prev->die_parent);
5225 gcc_assert (prev->die_sib == old_child);
5226
5227 new_child->die_parent = parent;
5228 if (prev == old_child)
5229 {
5230 gcc_assert (parent->die_child == old_child);
5231 new_child->die_sib = new_child;
5232 }
5233 else
5234 {
5235 prev->die_sib = new_child;
5236 new_child->die_sib = old_child->die_sib;
5237 }
5238 if (old_child->die_parent->die_child == old_child)
5239 old_child->die_parent->die_child = new_child;
5240 old_child->die_sib = NULL;
5241 }
5242
5243 /* Move all children from OLD_PARENT to NEW_PARENT. */
5244
5245 static void
5246 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5247 {
5248 dw_die_ref c;
5249 new_parent->die_child = old_parent->die_child;
5250 old_parent->die_child = NULL;
5251 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5252 }
5253
5254 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5255 matches TAG. */
5256
5257 static void
5258 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5259 {
5260 dw_die_ref c;
5261
5262 c = die->die_child;
5263 if (c) do {
5264 dw_die_ref prev = c;
5265 c = c->die_sib;
5266 while (c->die_tag == tag)
5267 {
5268 remove_child_with_prev (c, prev);
5269 c->die_parent = NULL;
5270 /* Might have removed every child. */
5271 if (die->die_child == NULL)
5272 return;
5273 c = prev->die_sib;
5274 }
5275 } while (c != die->die_child);
5276 }
5277
5278 /* Add a CHILD_DIE as the last child of DIE. */
5279
5280 static void
5281 add_child_die (dw_die_ref die, dw_die_ref child_die)
5282 {
5283 /* FIXME this should probably be an assert. */
5284 if (! die || ! child_die)
5285 return;
5286 gcc_assert (die != child_die);
5287
5288 child_die->die_parent = die;
5289 if (die->die_child)
5290 {
5291 child_die->die_sib = die->die_child->die_sib;
5292 die->die_child->die_sib = child_die;
5293 }
5294 else
5295 child_die->die_sib = child_die;
5296 die->die_child = child_die;
5297 }
5298
5299 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5300
5301 static void
5302 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5303 dw_die_ref after_die)
5304 {
5305 gcc_assert (die
5306 && child_die
5307 && after_die
5308 && die->die_child
5309 && die != child_die);
5310
5311 child_die->die_parent = die;
5312 child_die->die_sib = after_die->die_sib;
5313 after_die->die_sib = child_die;
5314 if (die->die_child == after_die)
5315 die->die_child = child_die;
5316 }
5317
5318 /* Unassociate CHILD from its parent, and make its parent be
5319 NEW_PARENT. */
5320
5321 static void
5322 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5323 {
5324 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5325 if (p->die_sib == child)
5326 {
5327 remove_child_with_prev (child, p);
5328 break;
5329 }
5330 add_child_die (new_parent, child);
5331 }
5332
5333 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5334 is the specification, to the end of PARENT's list of children.
5335 This is done by removing and re-adding it. */
5336
5337 static void
5338 splice_child_die (dw_die_ref parent, dw_die_ref child)
5339 {
5340 /* We want the declaration DIE from inside the class, not the
5341 specification DIE at toplevel. */
5342 if (child->die_parent != parent)
5343 {
5344 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5345
5346 if (tmp)
5347 child = tmp;
5348 }
5349
5350 gcc_assert (child->die_parent == parent
5351 || (child->die_parent
5352 == get_AT_ref (parent, DW_AT_specification)));
5353
5354 reparent_child (child, parent);
5355 }
5356
5357 /* Create and return a new die with TAG_VALUE as tag. */
5358
5359 static inline dw_die_ref
5360 new_die_raw (enum dwarf_tag tag_value)
5361 {
5362 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5363 die->die_tag = tag_value;
5364 return die;
5365 }
5366
5367 /* Create and return a new die with a parent of PARENT_DIE. If
5368 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5369 associated tree T must be supplied to determine parenthood
5370 later. */
5371
5372 static inline dw_die_ref
5373 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5374 {
5375 dw_die_ref die = new_die_raw (tag_value);
5376
5377 if (parent_die != NULL)
5378 add_child_die (parent_die, die);
5379 else
5380 {
5381 limbo_die_node *limbo_node;
5382
5383 /* No DIEs created after early dwarf should end up in limbo,
5384 because the limbo list should not persist past LTO
5385 streaming. */
5386 if (tag_value != DW_TAG_compile_unit
5387 /* These are allowed because they're generated while
5388 breaking out COMDAT units late. */
5389 && tag_value != DW_TAG_type_unit
5390 && tag_value != DW_TAG_skeleton_unit
5391 && !early_dwarf
5392 /* Allow nested functions to live in limbo because they will
5393 only temporarily live there, as decls_for_scope will fix
5394 them up. */
5395 && (TREE_CODE (t) != FUNCTION_DECL
5396 || !decl_function_context (t))
5397 /* Same as nested functions above but for types. Types that
5398 are local to a function will be fixed in
5399 decls_for_scope. */
5400 && (!RECORD_OR_UNION_TYPE_P (t)
5401 || !TYPE_CONTEXT (t)
5402 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5403 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5404 especially in the ltrans stage, but once we implement LTO
5405 dwarf streaming, we should remove this exception. */
5406 && !in_lto_p)
5407 {
5408 fprintf (stderr, "symbol ended up in limbo too late:");
5409 debug_generic_stmt (t);
5410 gcc_unreachable ();
5411 }
5412
5413 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5414 limbo_node->die = die;
5415 limbo_node->created_for = t;
5416 limbo_node->next = limbo_die_list;
5417 limbo_die_list = limbo_node;
5418 }
5419
5420 return die;
5421 }
5422
5423 /* Return the DIE associated with the given type specifier. */
5424
5425 static inline dw_die_ref
5426 lookup_type_die (tree type)
5427 {
5428 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5429 if (die && die->removed)
5430 {
5431 TYPE_SYMTAB_DIE (type) = NULL;
5432 return NULL;
5433 }
5434 return die;
5435 }
5436
5437 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5438 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5439 anonymous type instead the one of the naming typedef. */
5440
5441 static inline dw_die_ref
5442 strip_naming_typedef (tree type, dw_die_ref type_die)
5443 {
5444 if (type
5445 && TREE_CODE (type) == RECORD_TYPE
5446 && type_die
5447 && type_die->die_tag == DW_TAG_typedef
5448 && is_naming_typedef_decl (TYPE_NAME (type)))
5449 type_die = get_AT_ref (type_die, DW_AT_type);
5450 return type_die;
5451 }
5452
5453 /* Like lookup_type_die, but if type is an anonymous type named by a
5454 typedef[1], return the DIE of the anonymous type instead the one of
5455 the naming typedef. This is because in gen_typedef_die, we did
5456 equate the anonymous struct named by the typedef with the DIE of
5457 the naming typedef. So by default, lookup_type_die on an anonymous
5458 struct yields the DIE of the naming typedef.
5459
5460 [1]: Read the comment of is_naming_typedef_decl to learn about what
5461 a naming typedef is. */
5462
5463 static inline dw_die_ref
5464 lookup_type_die_strip_naming_typedef (tree type)
5465 {
5466 dw_die_ref die = lookup_type_die (type);
5467 return strip_naming_typedef (type, die);
5468 }
5469
5470 /* Equate a DIE to a given type specifier. */
5471
5472 static inline void
5473 equate_type_number_to_die (tree type, dw_die_ref type_die)
5474 {
5475 TYPE_SYMTAB_DIE (type) = type_die;
5476 }
5477
5478 /* Returns a hash value for X (which really is a die_struct). */
5479
5480 inline hashval_t
5481 decl_die_hasher::hash (die_node *x)
5482 {
5483 return (hashval_t) x->decl_id;
5484 }
5485
5486 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5487
5488 inline bool
5489 decl_die_hasher::equal (die_node *x, tree y)
5490 {
5491 return (x->decl_id == DECL_UID (y));
5492 }
5493
5494 /* Return the DIE associated with a given declaration. */
5495
5496 static inline dw_die_ref
5497 lookup_decl_die (tree decl)
5498 {
5499 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5500 NO_INSERT);
5501 if (!die)
5502 return NULL;
5503 if ((*die)->removed)
5504 {
5505 decl_die_table->clear_slot (die);
5506 return NULL;
5507 }
5508 return *die;
5509 }
5510
5511
5512 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5513 style reference. Return true if we found one refering to a DIE for
5514 DECL, otherwise return false. */
5515
5516 static bool
5517 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5518 unsigned HOST_WIDE_INT *off)
5519 {
5520 dw_die_ref die;
5521
5522 if (flag_wpa && !decl_die_table)
5523 return false;
5524
5525 if (TREE_CODE (decl) == BLOCK)
5526 die = BLOCK_DIE (decl);
5527 else
5528 die = lookup_decl_die (decl);
5529 if (!die)
5530 return false;
5531
5532 /* During WPA stage we currently use DIEs to store the
5533 decl <-> label + offset map. That's quite inefficient but it
5534 works for now. */
5535 if (flag_wpa)
5536 {
5537 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5538 if (!ref)
5539 {
5540 gcc_assert (die == comp_unit_die ());
5541 return false;
5542 }
5543 *off = ref->die_offset;
5544 *sym = ref->die_id.die_symbol;
5545 return true;
5546 }
5547
5548 /* Similar to get_ref_die_offset_label, but using the "correct"
5549 label. */
5550 *off = die->die_offset;
5551 while (die->die_parent)
5552 die = die->die_parent;
5553 /* For the containing CU DIE we compute a die_symbol in
5554 compute_comp_unit_symbol. */
5555 gcc_assert (die->die_tag == DW_TAG_compile_unit
5556 && die->die_id.die_symbol != NULL);
5557 *sym = die->die_id.die_symbol;
5558 return true;
5559 }
5560
5561 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5562
5563 static void
5564 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5565 const char *symbol, HOST_WIDE_INT offset)
5566 {
5567 /* Create a fake DIE that contains the reference. Don't use
5568 new_die because we don't want to end up in the limbo list. */
5569 dw_die_ref ref = new_die_raw (die->die_tag);
5570 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5571 ref->die_offset = offset;
5572 ref->with_offset = 1;
5573 add_AT_die_ref (die, attr_kind, ref);
5574 }
5575
5576 /* Create a DIE for DECL if required and add a reference to a DIE
5577 at SYMBOL + OFFSET which contains attributes dumped early. */
5578
5579 static void
5580 dwarf2out_register_external_die (tree decl, const char *sym,
5581 unsigned HOST_WIDE_INT off)
5582 {
5583 if (debug_info_level == DINFO_LEVEL_NONE)
5584 return;
5585
5586 if (flag_wpa && !decl_die_table)
5587 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5588
5589 dw_die_ref die
5590 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5591 gcc_assert (!die);
5592
5593 tree ctx;
5594 dw_die_ref parent = NULL;
5595 /* Need to lookup a DIE for the decls context - the containing
5596 function or translation unit. */
5597 if (TREE_CODE (decl) == BLOCK)
5598 {
5599 ctx = BLOCK_SUPERCONTEXT (decl);
5600 /* ??? We do not output DIEs for all scopes thus skip as
5601 many DIEs as needed. */
5602 while (TREE_CODE (ctx) == BLOCK
5603 && !BLOCK_DIE (ctx))
5604 ctx = BLOCK_SUPERCONTEXT (ctx);
5605 }
5606 else
5607 ctx = DECL_CONTEXT (decl);
5608 while (ctx && TYPE_P (ctx))
5609 ctx = TYPE_CONTEXT (ctx);
5610 if (ctx)
5611 {
5612 if (TREE_CODE (ctx) == BLOCK)
5613 parent = BLOCK_DIE (ctx);
5614 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5615 /* Keep the 1:1 association during WPA. */
5616 && !flag_wpa)
5617 /* Otherwise all late annotations go to the main CU which
5618 imports the original CUs. */
5619 parent = comp_unit_die ();
5620 else if (TREE_CODE (ctx) == FUNCTION_DECL
5621 && TREE_CODE (decl) != PARM_DECL
5622 && TREE_CODE (decl) != BLOCK)
5623 /* Leave function local entities parent determination to when
5624 we process scope vars. */
5625 ;
5626 else
5627 parent = lookup_decl_die (ctx);
5628 }
5629 else
5630 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5631 Handle this case gracefully by globalizing stuff. */
5632 parent = comp_unit_die ();
5633 /* Create a DIE "stub". */
5634 switch (TREE_CODE (decl))
5635 {
5636 case TRANSLATION_UNIT_DECL:
5637 if (! flag_wpa)
5638 {
5639 die = comp_unit_die ();
5640 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5641 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5642 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5643 to create a DIE for the original CUs. */
5644 return;
5645 }
5646 /* Keep the 1:1 association during WPA. */
5647 die = new_die (DW_TAG_compile_unit, NULL, decl);
5648 break;
5649 case NAMESPACE_DECL:
5650 if (is_fortran (decl))
5651 die = new_die (DW_TAG_module, parent, decl);
5652 else
5653 die = new_die (DW_TAG_namespace, parent, decl);
5654 break;
5655 case FUNCTION_DECL:
5656 die = new_die (DW_TAG_subprogram, parent, decl);
5657 break;
5658 case VAR_DECL:
5659 die = new_die (DW_TAG_variable, parent, decl);
5660 break;
5661 case RESULT_DECL:
5662 die = new_die (DW_TAG_variable, parent, decl);
5663 break;
5664 case PARM_DECL:
5665 die = new_die (DW_TAG_formal_parameter, parent, decl);
5666 break;
5667 case CONST_DECL:
5668 die = new_die (DW_TAG_constant, parent, decl);
5669 break;
5670 case LABEL_DECL:
5671 die = new_die (DW_TAG_label, parent, decl);
5672 break;
5673 case BLOCK:
5674 die = new_die (DW_TAG_lexical_block, parent, decl);
5675 break;
5676 default:
5677 gcc_unreachable ();
5678 }
5679 if (TREE_CODE (decl) == BLOCK)
5680 BLOCK_DIE (decl) = die;
5681 else
5682 equate_decl_number_to_die (decl, die);
5683
5684 /* Add a reference to the DIE providing early debug at $sym + off. */
5685 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5686 }
5687
5688 /* Returns a hash value for X (which really is a var_loc_list). */
5689
5690 inline hashval_t
5691 decl_loc_hasher::hash (var_loc_list *x)
5692 {
5693 return (hashval_t) x->decl_id;
5694 }
5695
5696 /* Return nonzero if decl_id of var_loc_list X is the same as
5697 UID of decl *Y. */
5698
5699 inline bool
5700 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5701 {
5702 return (x->decl_id == DECL_UID (y));
5703 }
5704
5705 /* Return the var_loc list associated with a given declaration. */
5706
5707 static inline var_loc_list *
5708 lookup_decl_loc (const_tree decl)
5709 {
5710 if (!decl_loc_table)
5711 return NULL;
5712 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5713 }
5714
5715 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5716
5717 inline hashval_t
5718 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5719 {
5720 return (hashval_t) x->decl_id;
5721 }
5722
5723 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5724 UID of decl *Y. */
5725
5726 inline bool
5727 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5728 {
5729 return (x->decl_id == DECL_UID (y));
5730 }
5731
5732 /* Equate a DIE to a particular declaration. */
5733
5734 static void
5735 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5736 {
5737 unsigned int decl_id = DECL_UID (decl);
5738
5739 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5740 decl_die->decl_id = decl_id;
5741 }
5742
5743 /* Return how many bits covers PIECE EXPR_LIST. */
5744
5745 static HOST_WIDE_INT
5746 decl_piece_bitsize (rtx piece)
5747 {
5748 int ret = (int) GET_MODE (piece);
5749 if (ret)
5750 return ret;
5751 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5752 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5753 return INTVAL (XEXP (XEXP (piece, 0), 0));
5754 }
5755
5756 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5757
5758 static rtx *
5759 decl_piece_varloc_ptr (rtx piece)
5760 {
5761 if ((int) GET_MODE (piece))
5762 return &XEXP (piece, 0);
5763 else
5764 return &XEXP (XEXP (piece, 0), 1);
5765 }
5766
5767 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5768 Next is the chain of following piece nodes. */
5769
5770 static rtx_expr_list *
5771 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5772 {
5773 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5774 return alloc_EXPR_LIST (bitsize, loc_note, next);
5775 else
5776 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5777 GEN_INT (bitsize),
5778 loc_note), next);
5779 }
5780
5781 /* Return rtx that should be stored into loc field for
5782 LOC_NOTE and BITPOS/BITSIZE. */
5783
5784 static rtx
5785 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5786 HOST_WIDE_INT bitsize)
5787 {
5788 if (bitsize != -1)
5789 {
5790 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5791 if (bitpos != 0)
5792 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5793 }
5794 return loc_note;
5795 }
5796
5797 /* This function either modifies location piece list *DEST in
5798 place (if SRC and INNER is NULL), or copies location piece list
5799 *SRC to *DEST while modifying it. Location BITPOS is modified
5800 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5801 not copied and if needed some padding around it is added.
5802 When modifying in place, DEST should point to EXPR_LIST where
5803 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5804 to the start of the whole list and INNER points to the EXPR_LIST
5805 where earlier pieces cover PIECE_BITPOS bits. */
5806
5807 static void
5808 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5809 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5810 HOST_WIDE_INT bitsize, rtx loc_note)
5811 {
5812 HOST_WIDE_INT diff;
5813 bool copy = inner != NULL;
5814
5815 if (copy)
5816 {
5817 /* First copy all nodes preceding the current bitpos. */
5818 while (src != inner)
5819 {
5820 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5821 decl_piece_bitsize (*src), NULL_RTX);
5822 dest = &XEXP (*dest, 1);
5823 src = &XEXP (*src, 1);
5824 }
5825 }
5826 /* Add padding if needed. */
5827 if (bitpos != piece_bitpos)
5828 {
5829 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5830 copy ? NULL_RTX : *dest);
5831 dest = &XEXP (*dest, 1);
5832 }
5833 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5834 {
5835 gcc_assert (!copy);
5836 /* A piece with correct bitpos and bitsize already exist,
5837 just update the location for it and return. */
5838 *decl_piece_varloc_ptr (*dest) = loc_note;
5839 return;
5840 }
5841 /* Add the piece that changed. */
5842 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5843 dest = &XEXP (*dest, 1);
5844 /* Skip over pieces that overlap it. */
5845 diff = bitpos - piece_bitpos + bitsize;
5846 if (!copy)
5847 src = dest;
5848 while (diff > 0 && *src)
5849 {
5850 rtx piece = *src;
5851 diff -= decl_piece_bitsize (piece);
5852 if (copy)
5853 src = &XEXP (piece, 1);
5854 else
5855 {
5856 *src = XEXP (piece, 1);
5857 free_EXPR_LIST_node (piece);
5858 }
5859 }
5860 /* Add padding if needed. */
5861 if (diff < 0 && *src)
5862 {
5863 if (!copy)
5864 dest = src;
5865 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5866 dest = &XEXP (*dest, 1);
5867 }
5868 if (!copy)
5869 return;
5870 /* Finally copy all nodes following it. */
5871 while (*src)
5872 {
5873 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5874 decl_piece_bitsize (*src), NULL_RTX);
5875 dest = &XEXP (*dest, 1);
5876 src = &XEXP (*src, 1);
5877 }
5878 }
5879
5880 /* Add a variable location node to the linked list for DECL. */
5881
5882 static struct var_loc_node *
5883 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5884 {
5885 unsigned int decl_id;
5886 var_loc_list *temp;
5887 struct var_loc_node *loc = NULL;
5888 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5889
5890 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5891 {
5892 tree realdecl = DECL_DEBUG_EXPR (decl);
5893 if (handled_component_p (realdecl)
5894 || (TREE_CODE (realdecl) == MEM_REF
5895 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5896 {
5897 HOST_WIDE_INT maxsize;
5898 bool reverse;
5899 tree innerdecl
5900 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5901 &reverse);
5902 if (!DECL_P (innerdecl)
5903 || DECL_IGNORED_P (innerdecl)
5904 || TREE_STATIC (innerdecl)
5905 || bitsize <= 0
5906 || bitpos + bitsize > 256
5907 || bitsize != maxsize)
5908 return NULL;
5909 decl = innerdecl;
5910 }
5911 }
5912
5913 decl_id = DECL_UID (decl);
5914 var_loc_list **slot
5915 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5916 if (*slot == NULL)
5917 {
5918 temp = ggc_cleared_alloc<var_loc_list> ();
5919 temp->decl_id = decl_id;
5920 *slot = temp;
5921 }
5922 else
5923 temp = *slot;
5924
5925 /* For PARM_DECLs try to keep around the original incoming value,
5926 even if that means we'll emit a zero-range .debug_loc entry. */
5927 if (temp->last
5928 && temp->first == temp->last
5929 && TREE_CODE (decl) == PARM_DECL
5930 && NOTE_P (temp->first->loc)
5931 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5932 && DECL_INCOMING_RTL (decl)
5933 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5934 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5935 == GET_CODE (DECL_INCOMING_RTL (decl))
5936 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5937 && (bitsize != -1
5938 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5939 NOTE_VAR_LOCATION_LOC (loc_note))
5940 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5941 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5942 {
5943 loc = ggc_cleared_alloc<var_loc_node> ();
5944 temp->first->next = loc;
5945 temp->last = loc;
5946 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5947 }
5948 else if (temp->last)
5949 {
5950 struct var_loc_node *last = temp->last, *unused = NULL;
5951 rtx *piece_loc = NULL, last_loc_note;
5952 HOST_WIDE_INT piece_bitpos = 0;
5953 if (last->next)
5954 {
5955 last = last->next;
5956 gcc_assert (last->next == NULL);
5957 }
5958 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5959 {
5960 piece_loc = &last->loc;
5961 do
5962 {
5963 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5964 if (piece_bitpos + cur_bitsize > bitpos)
5965 break;
5966 piece_bitpos += cur_bitsize;
5967 piece_loc = &XEXP (*piece_loc, 1);
5968 }
5969 while (*piece_loc);
5970 }
5971 /* TEMP->LAST here is either pointer to the last but one or
5972 last element in the chained list, LAST is pointer to the
5973 last element. */
5974 if (label && strcmp (last->label, label) == 0)
5975 {
5976 /* For SRA optimized variables if there weren't any real
5977 insns since last note, just modify the last node. */
5978 if (piece_loc != NULL)
5979 {
5980 adjust_piece_list (piece_loc, NULL, NULL,
5981 bitpos, piece_bitpos, bitsize, loc_note);
5982 return NULL;
5983 }
5984 /* If the last note doesn't cover any instructions, remove it. */
5985 if (temp->last != last)
5986 {
5987 temp->last->next = NULL;
5988 unused = last;
5989 last = temp->last;
5990 gcc_assert (strcmp (last->label, label) != 0);
5991 }
5992 else
5993 {
5994 gcc_assert (temp->first == temp->last
5995 || (temp->first->next == temp->last
5996 && TREE_CODE (decl) == PARM_DECL));
5997 memset (temp->last, '\0', sizeof (*temp->last));
5998 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5999 return temp->last;
6000 }
6001 }
6002 if (bitsize == -1 && NOTE_P (last->loc))
6003 last_loc_note = last->loc;
6004 else if (piece_loc != NULL
6005 && *piece_loc != NULL_RTX
6006 && piece_bitpos == bitpos
6007 && decl_piece_bitsize (*piece_loc) == bitsize)
6008 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6009 else
6010 last_loc_note = NULL_RTX;
6011 /* If the current location is the same as the end of the list,
6012 and either both or neither of the locations is uninitialized,
6013 we have nothing to do. */
6014 if (last_loc_note == NULL_RTX
6015 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6016 NOTE_VAR_LOCATION_LOC (loc_note)))
6017 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6018 != NOTE_VAR_LOCATION_STATUS (loc_note))
6019 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6020 == VAR_INIT_STATUS_UNINITIALIZED)
6021 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6022 == VAR_INIT_STATUS_UNINITIALIZED))))
6023 {
6024 /* Add LOC to the end of list and update LAST. If the last
6025 element of the list has been removed above, reuse its
6026 memory for the new node, otherwise allocate a new one. */
6027 if (unused)
6028 {
6029 loc = unused;
6030 memset (loc, '\0', sizeof (*loc));
6031 }
6032 else
6033 loc = ggc_cleared_alloc<var_loc_node> ();
6034 if (bitsize == -1 || piece_loc == NULL)
6035 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6036 else
6037 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6038 bitpos, piece_bitpos, bitsize, loc_note);
6039 last->next = loc;
6040 /* Ensure TEMP->LAST will point either to the new last but one
6041 element of the chain, or to the last element in it. */
6042 if (last != temp->last)
6043 temp->last = last;
6044 }
6045 else if (unused)
6046 ggc_free (unused);
6047 }
6048 else
6049 {
6050 loc = ggc_cleared_alloc<var_loc_node> ();
6051 temp->first = loc;
6052 temp->last = loc;
6053 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6054 }
6055 return loc;
6056 }
6057 \f
6058 /* Keep track of the number of spaces used to indent the
6059 output of the debugging routines that print the structure of
6060 the DIE internal representation. */
6061 static int print_indent;
6062
6063 /* Indent the line the number of spaces given by print_indent. */
6064
6065 static inline void
6066 print_spaces (FILE *outfile)
6067 {
6068 fprintf (outfile, "%*s", print_indent, "");
6069 }
6070
6071 /* Print a type signature in hex. */
6072
6073 static inline void
6074 print_signature (FILE *outfile, char *sig)
6075 {
6076 int i;
6077
6078 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6079 fprintf (outfile, "%02x", sig[i] & 0xff);
6080 }
6081
6082 static inline void
6083 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6084 {
6085 if (discr_value->pos)
6086 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6087 else
6088 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6089 }
6090
6091 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6092
6093 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6094 RECURSE, output location descriptor operations. */
6095
6096 static void
6097 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6098 {
6099 switch (val->val_class)
6100 {
6101 case dw_val_class_addr:
6102 fprintf (outfile, "address");
6103 break;
6104 case dw_val_class_offset:
6105 fprintf (outfile, "offset");
6106 break;
6107 case dw_val_class_loc:
6108 fprintf (outfile, "location descriptor");
6109 if (val->v.val_loc == NULL)
6110 fprintf (outfile, " -> <null>\n");
6111 else if (recurse)
6112 {
6113 fprintf (outfile, ":\n");
6114 print_indent += 4;
6115 print_loc_descr (val->v.val_loc, outfile);
6116 print_indent -= 4;
6117 }
6118 else
6119 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6120 break;
6121 case dw_val_class_loc_list:
6122 fprintf (outfile, "location list -> label:%s",
6123 val->v.val_loc_list->ll_symbol);
6124 break;
6125 case dw_val_class_range_list:
6126 fprintf (outfile, "range list");
6127 break;
6128 case dw_val_class_const:
6129 case dw_val_class_const_implicit:
6130 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6131 break;
6132 case dw_val_class_unsigned_const:
6133 case dw_val_class_unsigned_const_implicit:
6134 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6135 break;
6136 case dw_val_class_const_double:
6137 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6138 HOST_WIDE_INT_PRINT_UNSIGNED")",
6139 val->v.val_double.high,
6140 val->v.val_double.low);
6141 break;
6142 case dw_val_class_wide_int:
6143 {
6144 int i = val->v.val_wide->get_len ();
6145 fprintf (outfile, "constant (");
6146 gcc_assert (i > 0);
6147 if (val->v.val_wide->elt (i - 1) == 0)
6148 fprintf (outfile, "0x");
6149 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6150 val->v.val_wide->elt (--i));
6151 while (--i >= 0)
6152 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6153 val->v.val_wide->elt (i));
6154 fprintf (outfile, ")");
6155 break;
6156 }
6157 case dw_val_class_vec:
6158 fprintf (outfile, "floating-point or vector constant");
6159 break;
6160 case dw_val_class_flag:
6161 fprintf (outfile, "%u", val->v.val_flag);
6162 break;
6163 case dw_val_class_die_ref:
6164 if (val->v.val_die_ref.die != NULL)
6165 {
6166 dw_die_ref die = val->v.val_die_ref.die;
6167
6168 if (die->comdat_type_p)
6169 {
6170 fprintf (outfile, "die -> signature: ");
6171 print_signature (outfile,
6172 die->die_id.die_type_node->signature);
6173 }
6174 else if (die->die_id.die_symbol)
6175 {
6176 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6177 if (die->with_offset)
6178 fprintf (outfile, " + %ld", die->die_offset);
6179 }
6180 else
6181 fprintf (outfile, "die -> %ld", die->die_offset);
6182 fprintf (outfile, " (%p)", (void *) die);
6183 }
6184 else
6185 fprintf (outfile, "die -> <null>");
6186 break;
6187 case dw_val_class_vms_delta:
6188 fprintf (outfile, "delta: @slotcount(%s-%s)",
6189 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6190 break;
6191 case dw_val_class_lbl_id:
6192 case dw_val_class_lineptr:
6193 case dw_val_class_macptr:
6194 case dw_val_class_loclistsptr:
6195 case dw_val_class_high_pc:
6196 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6197 break;
6198 case dw_val_class_str:
6199 if (val->v.val_str->str != NULL)
6200 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6201 else
6202 fprintf (outfile, "<null>");
6203 break;
6204 case dw_val_class_file:
6205 case dw_val_class_file_implicit:
6206 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6207 val->v.val_file->emitted_number);
6208 break;
6209 case dw_val_class_data8:
6210 {
6211 int i;
6212
6213 for (i = 0; i < 8; i++)
6214 fprintf (outfile, "%02x", val->v.val_data8[i]);
6215 break;
6216 }
6217 case dw_val_class_discr_value:
6218 print_discr_value (outfile, &val->v.val_discr_value);
6219 break;
6220 case dw_val_class_discr_list:
6221 for (dw_discr_list_ref node = val->v.val_discr_list;
6222 node != NULL;
6223 node = node->dw_discr_next)
6224 {
6225 if (node->dw_discr_range)
6226 {
6227 fprintf (outfile, " .. ");
6228 print_discr_value (outfile, &node->dw_discr_lower_bound);
6229 print_discr_value (outfile, &node->dw_discr_upper_bound);
6230 }
6231 else
6232 print_discr_value (outfile, &node->dw_discr_lower_bound);
6233
6234 if (node->dw_discr_next != NULL)
6235 fprintf (outfile, " | ");
6236 }
6237 default:
6238 break;
6239 }
6240 }
6241
6242 /* Likewise, for a DIE attribute. */
6243
6244 static void
6245 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6246 {
6247 print_dw_val (&a->dw_attr_val, recurse, outfile);
6248 }
6249
6250
6251 /* Print the list of operands in the LOC location description to OUTFILE. This
6252 routine is a debugging aid only. */
6253
6254 static void
6255 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6256 {
6257 dw_loc_descr_ref l = loc;
6258
6259 if (loc == NULL)
6260 {
6261 print_spaces (outfile);
6262 fprintf (outfile, "<null>\n");
6263 return;
6264 }
6265
6266 for (l = loc; l != NULL; l = l->dw_loc_next)
6267 {
6268 print_spaces (outfile);
6269 fprintf (outfile, "(%p) %s",
6270 (void *) l,
6271 dwarf_stack_op_name (l->dw_loc_opc));
6272 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6273 {
6274 fprintf (outfile, " ");
6275 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6276 }
6277 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6278 {
6279 fprintf (outfile, ", ");
6280 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6281 }
6282 fprintf (outfile, "\n");
6283 }
6284 }
6285
6286 /* Print the information associated with a given DIE, and its children.
6287 This routine is a debugging aid only. */
6288
6289 static void
6290 print_die (dw_die_ref die, FILE *outfile)
6291 {
6292 dw_attr_node *a;
6293 dw_die_ref c;
6294 unsigned ix;
6295
6296 print_spaces (outfile);
6297 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6298 die->die_offset, dwarf_tag_name (die->die_tag),
6299 (void*) die);
6300 print_spaces (outfile);
6301 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6302 fprintf (outfile, " offset: %ld", die->die_offset);
6303 fprintf (outfile, " mark: %d\n", die->die_mark);
6304
6305 if (die->comdat_type_p)
6306 {
6307 print_spaces (outfile);
6308 fprintf (outfile, " signature: ");
6309 print_signature (outfile, die->die_id.die_type_node->signature);
6310 fprintf (outfile, "\n");
6311 }
6312
6313 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6314 {
6315 print_spaces (outfile);
6316 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6317
6318 print_attribute (a, true, outfile);
6319 fprintf (outfile, "\n");
6320 }
6321
6322 if (die->die_child != NULL)
6323 {
6324 print_indent += 4;
6325 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6326 print_indent -= 4;
6327 }
6328 if (print_indent == 0)
6329 fprintf (outfile, "\n");
6330 }
6331
6332 /* Print the list of operations in the LOC location description. */
6333
6334 DEBUG_FUNCTION void
6335 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6336 {
6337 print_loc_descr (loc, stderr);
6338 }
6339
6340 /* Print the information collected for a given DIE. */
6341
6342 DEBUG_FUNCTION void
6343 debug_dwarf_die (dw_die_ref die)
6344 {
6345 print_die (die, stderr);
6346 }
6347
6348 DEBUG_FUNCTION void
6349 debug (die_struct &ref)
6350 {
6351 print_die (&ref, stderr);
6352 }
6353
6354 DEBUG_FUNCTION void
6355 debug (die_struct *ptr)
6356 {
6357 if (ptr)
6358 debug (*ptr);
6359 else
6360 fprintf (stderr, "<nil>\n");
6361 }
6362
6363
6364 /* Print all DWARF information collected for the compilation unit.
6365 This routine is a debugging aid only. */
6366
6367 DEBUG_FUNCTION void
6368 debug_dwarf (void)
6369 {
6370 print_indent = 0;
6371 print_die (comp_unit_die (), stderr);
6372 }
6373
6374 /* Verify the DIE tree structure. */
6375
6376 DEBUG_FUNCTION void
6377 verify_die (dw_die_ref die)
6378 {
6379 gcc_assert (!die->die_mark);
6380 if (die->die_parent == NULL
6381 && die->die_sib == NULL)
6382 return;
6383 /* Verify the die_sib list is cyclic. */
6384 dw_die_ref x = die;
6385 do
6386 {
6387 x->die_mark = 1;
6388 x = x->die_sib;
6389 }
6390 while (x && !x->die_mark);
6391 gcc_assert (x == die);
6392 x = die;
6393 do
6394 {
6395 /* Verify all dies have the same parent. */
6396 gcc_assert (x->die_parent == die->die_parent);
6397 if (x->die_child)
6398 {
6399 /* Verify the child has the proper parent and recurse. */
6400 gcc_assert (x->die_child->die_parent == x);
6401 verify_die (x->die_child);
6402 }
6403 x->die_mark = 0;
6404 x = x->die_sib;
6405 }
6406 while (x && x->die_mark);
6407 }
6408
6409 /* Sanity checks on DIEs. */
6410
6411 static void
6412 check_die (dw_die_ref die)
6413 {
6414 unsigned ix;
6415 dw_attr_node *a;
6416 bool inline_found = false;
6417 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6418 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6419 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6420 {
6421 switch (a->dw_attr)
6422 {
6423 case DW_AT_inline:
6424 if (a->dw_attr_val.v.val_unsigned)
6425 inline_found = true;
6426 break;
6427 case DW_AT_location:
6428 ++n_location;
6429 break;
6430 case DW_AT_low_pc:
6431 ++n_low_pc;
6432 break;
6433 case DW_AT_high_pc:
6434 ++n_high_pc;
6435 break;
6436 case DW_AT_artificial:
6437 ++n_artificial;
6438 break;
6439 case DW_AT_decl_column:
6440 ++n_decl_column;
6441 break;
6442 case DW_AT_decl_line:
6443 ++n_decl_line;
6444 break;
6445 case DW_AT_decl_file:
6446 ++n_decl_file;
6447 break;
6448 default:
6449 break;
6450 }
6451 }
6452 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6453 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6454 {
6455 fprintf (stderr, "Duplicate attributes in DIE:\n");
6456 debug_dwarf_die (die);
6457 gcc_unreachable ();
6458 }
6459 if (inline_found)
6460 {
6461 /* A debugging information entry that is a member of an abstract
6462 instance tree [that has DW_AT_inline] should not contain any
6463 attributes which describe aspects of the subroutine which vary
6464 between distinct inlined expansions or distinct out-of-line
6465 expansions. */
6466 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6467 gcc_assert (a->dw_attr != DW_AT_low_pc
6468 && a->dw_attr != DW_AT_high_pc
6469 && a->dw_attr != DW_AT_location
6470 && a->dw_attr != DW_AT_frame_base
6471 && a->dw_attr != DW_AT_call_all_calls
6472 && a->dw_attr != DW_AT_GNU_all_call_sites);
6473 }
6474 }
6475 \f
6476 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6477 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6478 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6479
6480 /* Calculate the checksum of a location expression. */
6481
6482 static inline void
6483 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6484 {
6485 int tem;
6486 inchash::hash hstate;
6487 hashval_t hash;
6488
6489 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6490 CHECKSUM (tem);
6491 hash_loc_operands (loc, hstate);
6492 hash = hstate.end();
6493 CHECKSUM (hash);
6494 }
6495
6496 /* Calculate the checksum of an attribute. */
6497
6498 static void
6499 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6500 {
6501 dw_loc_descr_ref loc;
6502 rtx r;
6503
6504 CHECKSUM (at->dw_attr);
6505
6506 /* We don't care that this was compiled with a different compiler
6507 snapshot; if the output is the same, that's what matters. */
6508 if (at->dw_attr == DW_AT_producer)
6509 return;
6510
6511 switch (AT_class (at))
6512 {
6513 case dw_val_class_const:
6514 case dw_val_class_const_implicit:
6515 CHECKSUM (at->dw_attr_val.v.val_int);
6516 break;
6517 case dw_val_class_unsigned_const:
6518 case dw_val_class_unsigned_const_implicit:
6519 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6520 break;
6521 case dw_val_class_const_double:
6522 CHECKSUM (at->dw_attr_val.v.val_double);
6523 break;
6524 case dw_val_class_wide_int:
6525 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6526 get_full_len (*at->dw_attr_val.v.val_wide)
6527 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6528 break;
6529 case dw_val_class_vec:
6530 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6531 (at->dw_attr_val.v.val_vec.length
6532 * at->dw_attr_val.v.val_vec.elt_size));
6533 break;
6534 case dw_val_class_flag:
6535 CHECKSUM (at->dw_attr_val.v.val_flag);
6536 break;
6537 case dw_val_class_str:
6538 CHECKSUM_STRING (AT_string (at));
6539 break;
6540
6541 case dw_val_class_addr:
6542 r = AT_addr (at);
6543 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6544 CHECKSUM_STRING (XSTR (r, 0));
6545 break;
6546
6547 case dw_val_class_offset:
6548 CHECKSUM (at->dw_attr_val.v.val_offset);
6549 break;
6550
6551 case dw_val_class_loc:
6552 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6553 loc_checksum (loc, ctx);
6554 break;
6555
6556 case dw_val_class_die_ref:
6557 die_checksum (AT_ref (at), ctx, mark);
6558 break;
6559
6560 case dw_val_class_fde_ref:
6561 case dw_val_class_vms_delta:
6562 case dw_val_class_lbl_id:
6563 case dw_val_class_lineptr:
6564 case dw_val_class_macptr:
6565 case dw_val_class_loclistsptr:
6566 case dw_val_class_high_pc:
6567 break;
6568
6569 case dw_val_class_file:
6570 case dw_val_class_file_implicit:
6571 CHECKSUM_STRING (AT_file (at)->filename);
6572 break;
6573
6574 case dw_val_class_data8:
6575 CHECKSUM (at->dw_attr_val.v.val_data8);
6576 break;
6577
6578 default:
6579 break;
6580 }
6581 }
6582
6583 /* Calculate the checksum of a DIE. */
6584
6585 static void
6586 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6587 {
6588 dw_die_ref c;
6589 dw_attr_node *a;
6590 unsigned ix;
6591
6592 /* To avoid infinite recursion. */
6593 if (die->die_mark)
6594 {
6595 CHECKSUM (die->die_mark);
6596 return;
6597 }
6598 die->die_mark = ++(*mark);
6599
6600 CHECKSUM (die->die_tag);
6601
6602 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6603 attr_checksum (a, ctx, mark);
6604
6605 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6606 }
6607
6608 #undef CHECKSUM
6609 #undef CHECKSUM_BLOCK
6610 #undef CHECKSUM_STRING
6611
6612 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6613 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6614 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6615 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6616 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6617 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6618 #define CHECKSUM_ATTR(FOO) \
6619 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6620
6621 /* Calculate the checksum of a number in signed LEB128 format. */
6622
6623 static void
6624 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6625 {
6626 unsigned char byte;
6627 bool more;
6628
6629 while (1)
6630 {
6631 byte = (value & 0x7f);
6632 value >>= 7;
6633 more = !((value == 0 && (byte & 0x40) == 0)
6634 || (value == -1 && (byte & 0x40) != 0));
6635 if (more)
6636 byte |= 0x80;
6637 CHECKSUM (byte);
6638 if (!more)
6639 break;
6640 }
6641 }
6642
6643 /* Calculate the checksum of a number in unsigned LEB128 format. */
6644
6645 static void
6646 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6647 {
6648 while (1)
6649 {
6650 unsigned char byte = (value & 0x7f);
6651 value >>= 7;
6652 if (value != 0)
6653 /* More bytes to follow. */
6654 byte |= 0x80;
6655 CHECKSUM (byte);
6656 if (value == 0)
6657 break;
6658 }
6659 }
6660
6661 /* Checksum the context of the DIE. This adds the names of any
6662 surrounding namespaces or structures to the checksum. */
6663
6664 static void
6665 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6666 {
6667 const char *name;
6668 dw_die_ref spec;
6669 int tag = die->die_tag;
6670
6671 if (tag != DW_TAG_namespace
6672 && tag != DW_TAG_structure_type
6673 && tag != DW_TAG_class_type)
6674 return;
6675
6676 name = get_AT_string (die, DW_AT_name);
6677
6678 spec = get_AT_ref (die, DW_AT_specification);
6679 if (spec != NULL)
6680 die = spec;
6681
6682 if (die->die_parent != NULL)
6683 checksum_die_context (die->die_parent, ctx);
6684
6685 CHECKSUM_ULEB128 ('C');
6686 CHECKSUM_ULEB128 (tag);
6687 if (name != NULL)
6688 CHECKSUM_STRING (name);
6689 }
6690
6691 /* Calculate the checksum of a location expression. */
6692
6693 static inline void
6694 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6695 {
6696 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6697 were emitted as a DW_FORM_sdata instead of a location expression. */
6698 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6699 {
6700 CHECKSUM_ULEB128 (DW_FORM_sdata);
6701 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6702 return;
6703 }
6704
6705 /* Otherwise, just checksum the raw location expression. */
6706 while (loc != NULL)
6707 {
6708 inchash::hash hstate;
6709 hashval_t hash;
6710
6711 CHECKSUM_ULEB128 (loc->dtprel);
6712 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6713 hash_loc_operands (loc, hstate);
6714 hash = hstate.end ();
6715 CHECKSUM (hash);
6716 loc = loc->dw_loc_next;
6717 }
6718 }
6719
6720 /* Calculate the checksum of an attribute. */
6721
6722 static void
6723 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6724 struct md5_ctx *ctx, int *mark)
6725 {
6726 dw_loc_descr_ref loc;
6727 rtx r;
6728
6729 if (AT_class (at) == dw_val_class_die_ref)
6730 {
6731 dw_die_ref target_die = AT_ref (at);
6732
6733 /* For pointer and reference types, we checksum only the (qualified)
6734 name of the target type (if there is a name). For friend entries,
6735 we checksum only the (qualified) name of the target type or function.
6736 This allows the checksum to remain the same whether the target type
6737 is complete or not. */
6738 if ((at->dw_attr == DW_AT_type
6739 && (tag == DW_TAG_pointer_type
6740 || tag == DW_TAG_reference_type
6741 || tag == DW_TAG_rvalue_reference_type
6742 || tag == DW_TAG_ptr_to_member_type))
6743 || (at->dw_attr == DW_AT_friend
6744 && tag == DW_TAG_friend))
6745 {
6746 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6747
6748 if (name_attr != NULL)
6749 {
6750 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6751
6752 if (decl == NULL)
6753 decl = target_die;
6754 CHECKSUM_ULEB128 ('N');
6755 CHECKSUM_ULEB128 (at->dw_attr);
6756 if (decl->die_parent != NULL)
6757 checksum_die_context (decl->die_parent, ctx);
6758 CHECKSUM_ULEB128 ('E');
6759 CHECKSUM_STRING (AT_string (name_attr));
6760 return;
6761 }
6762 }
6763
6764 /* For all other references to another DIE, we check to see if the
6765 target DIE has already been visited. If it has, we emit a
6766 backward reference; if not, we descend recursively. */
6767 if (target_die->die_mark > 0)
6768 {
6769 CHECKSUM_ULEB128 ('R');
6770 CHECKSUM_ULEB128 (at->dw_attr);
6771 CHECKSUM_ULEB128 (target_die->die_mark);
6772 }
6773 else
6774 {
6775 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6776
6777 if (decl == NULL)
6778 decl = target_die;
6779 target_die->die_mark = ++(*mark);
6780 CHECKSUM_ULEB128 ('T');
6781 CHECKSUM_ULEB128 (at->dw_attr);
6782 if (decl->die_parent != NULL)
6783 checksum_die_context (decl->die_parent, ctx);
6784 die_checksum_ordered (target_die, ctx, mark);
6785 }
6786 return;
6787 }
6788
6789 CHECKSUM_ULEB128 ('A');
6790 CHECKSUM_ULEB128 (at->dw_attr);
6791
6792 switch (AT_class (at))
6793 {
6794 case dw_val_class_const:
6795 case dw_val_class_const_implicit:
6796 CHECKSUM_ULEB128 (DW_FORM_sdata);
6797 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6798 break;
6799
6800 case dw_val_class_unsigned_const:
6801 case dw_val_class_unsigned_const_implicit:
6802 CHECKSUM_ULEB128 (DW_FORM_sdata);
6803 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6804 break;
6805
6806 case dw_val_class_const_double:
6807 CHECKSUM_ULEB128 (DW_FORM_block);
6808 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6809 CHECKSUM (at->dw_attr_val.v.val_double);
6810 break;
6811
6812 case dw_val_class_wide_int:
6813 CHECKSUM_ULEB128 (DW_FORM_block);
6814 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6815 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6816 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6817 get_full_len (*at->dw_attr_val.v.val_wide)
6818 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6819 break;
6820
6821 case dw_val_class_vec:
6822 CHECKSUM_ULEB128 (DW_FORM_block);
6823 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6824 * at->dw_attr_val.v.val_vec.elt_size);
6825 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6826 (at->dw_attr_val.v.val_vec.length
6827 * at->dw_attr_val.v.val_vec.elt_size));
6828 break;
6829
6830 case dw_val_class_flag:
6831 CHECKSUM_ULEB128 (DW_FORM_flag);
6832 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6833 break;
6834
6835 case dw_val_class_str:
6836 CHECKSUM_ULEB128 (DW_FORM_string);
6837 CHECKSUM_STRING (AT_string (at));
6838 break;
6839
6840 case dw_val_class_addr:
6841 r = AT_addr (at);
6842 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6843 CHECKSUM_ULEB128 (DW_FORM_string);
6844 CHECKSUM_STRING (XSTR (r, 0));
6845 break;
6846
6847 case dw_val_class_offset:
6848 CHECKSUM_ULEB128 (DW_FORM_sdata);
6849 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6850 break;
6851
6852 case dw_val_class_loc:
6853 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6854 loc_checksum_ordered (loc, ctx);
6855 break;
6856
6857 case dw_val_class_fde_ref:
6858 case dw_val_class_lbl_id:
6859 case dw_val_class_lineptr:
6860 case dw_val_class_macptr:
6861 case dw_val_class_loclistsptr:
6862 case dw_val_class_high_pc:
6863 break;
6864
6865 case dw_val_class_file:
6866 case dw_val_class_file_implicit:
6867 CHECKSUM_ULEB128 (DW_FORM_string);
6868 CHECKSUM_STRING (AT_file (at)->filename);
6869 break;
6870
6871 case dw_val_class_data8:
6872 CHECKSUM (at->dw_attr_val.v.val_data8);
6873 break;
6874
6875 default:
6876 break;
6877 }
6878 }
6879
6880 struct checksum_attributes
6881 {
6882 dw_attr_node *at_name;
6883 dw_attr_node *at_type;
6884 dw_attr_node *at_friend;
6885 dw_attr_node *at_accessibility;
6886 dw_attr_node *at_address_class;
6887 dw_attr_node *at_alignment;
6888 dw_attr_node *at_allocated;
6889 dw_attr_node *at_artificial;
6890 dw_attr_node *at_associated;
6891 dw_attr_node *at_binary_scale;
6892 dw_attr_node *at_bit_offset;
6893 dw_attr_node *at_bit_size;
6894 dw_attr_node *at_bit_stride;
6895 dw_attr_node *at_byte_size;
6896 dw_attr_node *at_byte_stride;
6897 dw_attr_node *at_const_value;
6898 dw_attr_node *at_containing_type;
6899 dw_attr_node *at_count;
6900 dw_attr_node *at_data_location;
6901 dw_attr_node *at_data_member_location;
6902 dw_attr_node *at_decimal_scale;
6903 dw_attr_node *at_decimal_sign;
6904 dw_attr_node *at_default_value;
6905 dw_attr_node *at_digit_count;
6906 dw_attr_node *at_discr;
6907 dw_attr_node *at_discr_list;
6908 dw_attr_node *at_discr_value;
6909 dw_attr_node *at_encoding;
6910 dw_attr_node *at_endianity;
6911 dw_attr_node *at_explicit;
6912 dw_attr_node *at_is_optional;
6913 dw_attr_node *at_location;
6914 dw_attr_node *at_lower_bound;
6915 dw_attr_node *at_mutable;
6916 dw_attr_node *at_ordering;
6917 dw_attr_node *at_picture_string;
6918 dw_attr_node *at_prototyped;
6919 dw_attr_node *at_small;
6920 dw_attr_node *at_segment;
6921 dw_attr_node *at_string_length;
6922 dw_attr_node *at_string_length_bit_size;
6923 dw_attr_node *at_string_length_byte_size;
6924 dw_attr_node *at_threads_scaled;
6925 dw_attr_node *at_upper_bound;
6926 dw_attr_node *at_use_location;
6927 dw_attr_node *at_use_UTF8;
6928 dw_attr_node *at_variable_parameter;
6929 dw_attr_node *at_virtuality;
6930 dw_attr_node *at_visibility;
6931 dw_attr_node *at_vtable_elem_location;
6932 };
6933
6934 /* Collect the attributes that we will want to use for the checksum. */
6935
6936 static void
6937 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6938 {
6939 dw_attr_node *a;
6940 unsigned ix;
6941
6942 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6943 {
6944 switch (a->dw_attr)
6945 {
6946 case DW_AT_name:
6947 attrs->at_name = a;
6948 break;
6949 case DW_AT_type:
6950 attrs->at_type = a;
6951 break;
6952 case DW_AT_friend:
6953 attrs->at_friend = a;
6954 break;
6955 case DW_AT_accessibility:
6956 attrs->at_accessibility = a;
6957 break;
6958 case DW_AT_address_class:
6959 attrs->at_address_class = a;
6960 break;
6961 case DW_AT_alignment:
6962 attrs->at_alignment = a;
6963 break;
6964 case DW_AT_allocated:
6965 attrs->at_allocated = a;
6966 break;
6967 case DW_AT_artificial:
6968 attrs->at_artificial = a;
6969 break;
6970 case DW_AT_associated:
6971 attrs->at_associated = a;
6972 break;
6973 case DW_AT_binary_scale:
6974 attrs->at_binary_scale = a;
6975 break;
6976 case DW_AT_bit_offset:
6977 attrs->at_bit_offset = a;
6978 break;
6979 case DW_AT_bit_size:
6980 attrs->at_bit_size = a;
6981 break;
6982 case DW_AT_bit_stride:
6983 attrs->at_bit_stride = a;
6984 break;
6985 case DW_AT_byte_size:
6986 attrs->at_byte_size = a;
6987 break;
6988 case DW_AT_byte_stride:
6989 attrs->at_byte_stride = a;
6990 break;
6991 case DW_AT_const_value:
6992 attrs->at_const_value = a;
6993 break;
6994 case DW_AT_containing_type:
6995 attrs->at_containing_type = a;
6996 break;
6997 case DW_AT_count:
6998 attrs->at_count = a;
6999 break;
7000 case DW_AT_data_location:
7001 attrs->at_data_location = a;
7002 break;
7003 case DW_AT_data_member_location:
7004 attrs->at_data_member_location = a;
7005 break;
7006 case DW_AT_decimal_scale:
7007 attrs->at_decimal_scale = a;
7008 break;
7009 case DW_AT_decimal_sign:
7010 attrs->at_decimal_sign = a;
7011 break;
7012 case DW_AT_default_value:
7013 attrs->at_default_value = a;
7014 break;
7015 case DW_AT_digit_count:
7016 attrs->at_digit_count = a;
7017 break;
7018 case DW_AT_discr:
7019 attrs->at_discr = a;
7020 break;
7021 case DW_AT_discr_list:
7022 attrs->at_discr_list = a;
7023 break;
7024 case DW_AT_discr_value:
7025 attrs->at_discr_value = a;
7026 break;
7027 case DW_AT_encoding:
7028 attrs->at_encoding = a;
7029 break;
7030 case DW_AT_endianity:
7031 attrs->at_endianity = a;
7032 break;
7033 case DW_AT_explicit:
7034 attrs->at_explicit = a;
7035 break;
7036 case DW_AT_is_optional:
7037 attrs->at_is_optional = a;
7038 break;
7039 case DW_AT_location:
7040 attrs->at_location = a;
7041 break;
7042 case DW_AT_lower_bound:
7043 attrs->at_lower_bound = a;
7044 break;
7045 case DW_AT_mutable:
7046 attrs->at_mutable = a;
7047 break;
7048 case DW_AT_ordering:
7049 attrs->at_ordering = a;
7050 break;
7051 case DW_AT_picture_string:
7052 attrs->at_picture_string = a;
7053 break;
7054 case DW_AT_prototyped:
7055 attrs->at_prototyped = a;
7056 break;
7057 case DW_AT_small:
7058 attrs->at_small = a;
7059 break;
7060 case DW_AT_segment:
7061 attrs->at_segment = a;
7062 break;
7063 case DW_AT_string_length:
7064 attrs->at_string_length = a;
7065 break;
7066 case DW_AT_string_length_bit_size:
7067 attrs->at_string_length_bit_size = a;
7068 break;
7069 case DW_AT_string_length_byte_size:
7070 attrs->at_string_length_byte_size = a;
7071 break;
7072 case DW_AT_threads_scaled:
7073 attrs->at_threads_scaled = a;
7074 break;
7075 case DW_AT_upper_bound:
7076 attrs->at_upper_bound = a;
7077 break;
7078 case DW_AT_use_location:
7079 attrs->at_use_location = a;
7080 break;
7081 case DW_AT_use_UTF8:
7082 attrs->at_use_UTF8 = a;
7083 break;
7084 case DW_AT_variable_parameter:
7085 attrs->at_variable_parameter = a;
7086 break;
7087 case DW_AT_virtuality:
7088 attrs->at_virtuality = a;
7089 break;
7090 case DW_AT_visibility:
7091 attrs->at_visibility = a;
7092 break;
7093 case DW_AT_vtable_elem_location:
7094 attrs->at_vtable_elem_location = a;
7095 break;
7096 default:
7097 break;
7098 }
7099 }
7100 }
7101
7102 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7103
7104 static void
7105 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7106 {
7107 dw_die_ref c;
7108 dw_die_ref decl;
7109 struct checksum_attributes attrs;
7110
7111 CHECKSUM_ULEB128 ('D');
7112 CHECKSUM_ULEB128 (die->die_tag);
7113
7114 memset (&attrs, 0, sizeof (attrs));
7115
7116 decl = get_AT_ref (die, DW_AT_specification);
7117 if (decl != NULL)
7118 collect_checksum_attributes (&attrs, decl);
7119 collect_checksum_attributes (&attrs, die);
7120
7121 CHECKSUM_ATTR (attrs.at_name);
7122 CHECKSUM_ATTR (attrs.at_accessibility);
7123 CHECKSUM_ATTR (attrs.at_address_class);
7124 CHECKSUM_ATTR (attrs.at_allocated);
7125 CHECKSUM_ATTR (attrs.at_artificial);
7126 CHECKSUM_ATTR (attrs.at_associated);
7127 CHECKSUM_ATTR (attrs.at_binary_scale);
7128 CHECKSUM_ATTR (attrs.at_bit_offset);
7129 CHECKSUM_ATTR (attrs.at_bit_size);
7130 CHECKSUM_ATTR (attrs.at_bit_stride);
7131 CHECKSUM_ATTR (attrs.at_byte_size);
7132 CHECKSUM_ATTR (attrs.at_byte_stride);
7133 CHECKSUM_ATTR (attrs.at_const_value);
7134 CHECKSUM_ATTR (attrs.at_containing_type);
7135 CHECKSUM_ATTR (attrs.at_count);
7136 CHECKSUM_ATTR (attrs.at_data_location);
7137 CHECKSUM_ATTR (attrs.at_data_member_location);
7138 CHECKSUM_ATTR (attrs.at_decimal_scale);
7139 CHECKSUM_ATTR (attrs.at_decimal_sign);
7140 CHECKSUM_ATTR (attrs.at_default_value);
7141 CHECKSUM_ATTR (attrs.at_digit_count);
7142 CHECKSUM_ATTR (attrs.at_discr);
7143 CHECKSUM_ATTR (attrs.at_discr_list);
7144 CHECKSUM_ATTR (attrs.at_discr_value);
7145 CHECKSUM_ATTR (attrs.at_encoding);
7146 CHECKSUM_ATTR (attrs.at_endianity);
7147 CHECKSUM_ATTR (attrs.at_explicit);
7148 CHECKSUM_ATTR (attrs.at_is_optional);
7149 CHECKSUM_ATTR (attrs.at_location);
7150 CHECKSUM_ATTR (attrs.at_lower_bound);
7151 CHECKSUM_ATTR (attrs.at_mutable);
7152 CHECKSUM_ATTR (attrs.at_ordering);
7153 CHECKSUM_ATTR (attrs.at_picture_string);
7154 CHECKSUM_ATTR (attrs.at_prototyped);
7155 CHECKSUM_ATTR (attrs.at_small);
7156 CHECKSUM_ATTR (attrs.at_segment);
7157 CHECKSUM_ATTR (attrs.at_string_length);
7158 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7159 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7160 CHECKSUM_ATTR (attrs.at_threads_scaled);
7161 CHECKSUM_ATTR (attrs.at_upper_bound);
7162 CHECKSUM_ATTR (attrs.at_use_location);
7163 CHECKSUM_ATTR (attrs.at_use_UTF8);
7164 CHECKSUM_ATTR (attrs.at_variable_parameter);
7165 CHECKSUM_ATTR (attrs.at_virtuality);
7166 CHECKSUM_ATTR (attrs.at_visibility);
7167 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7168 CHECKSUM_ATTR (attrs.at_type);
7169 CHECKSUM_ATTR (attrs.at_friend);
7170 CHECKSUM_ATTR (attrs.at_alignment);
7171
7172 /* Checksum the child DIEs. */
7173 c = die->die_child;
7174 if (c) do {
7175 dw_attr_node *name_attr;
7176
7177 c = c->die_sib;
7178 name_attr = get_AT (c, DW_AT_name);
7179 if (is_template_instantiation (c))
7180 {
7181 /* Ignore instantiations of member type and function templates. */
7182 }
7183 else if (name_attr != NULL
7184 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7185 {
7186 /* Use a shallow checksum for named nested types and member
7187 functions. */
7188 CHECKSUM_ULEB128 ('S');
7189 CHECKSUM_ULEB128 (c->die_tag);
7190 CHECKSUM_STRING (AT_string (name_attr));
7191 }
7192 else
7193 {
7194 /* Use a deep checksum for other children. */
7195 /* Mark this DIE so it gets processed when unmarking. */
7196 if (c->die_mark == 0)
7197 c->die_mark = -1;
7198 die_checksum_ordered (c, ctx, mark);
7199 }
7200 } while (c != die->die_child);
7201
7202 CHECKSUM_ULEB128 (0);
7203 }
7204
7205 /* Add a type name and tag to a hash. */
7206 static void
7207 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7208 {
7209 CHECKSUM_ULEB128 (tag);
7210 CHECKSUM_STRING (name);
7211 }
7212
7213 #undef CHECKSUM
7214 #undef CHECKSUM_STRING
7215 #undef CHECKSUM_ATTR
7216 #undef CHECKSUM_LEB128
7217 #undef CHECKSUM_ULEB128
7218
7219 /* Generate the type signature for DIE. This is computed by generating an
7220 MD5 checksum over the DIE's tag, its relevant attributes, and its
7221 children. Attributes that are references to other DIEs are processed
7222 by recursion, using the MARK field to prevent infinite recursion.
7223 If the DIE is nested inside a namespace or another type, we also
7224 need to include that context in the signature. The lower 64 bits
7225 of the resulting MD5 checksum comprise the signature. */
7226
7227 static void
7228 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7229 {
7230 int mark;
7231 const char *name;
7232 unsigned char checksum[16];
7233 struct md5_ctx ctx;
7234 dw_die_ref decl;
7235 dw_die_ref parent;
7236
7237 name = get_AT_string (die, DW_AT_name);
7238 decl = get_AT_ref (die, DW_AT_specification);
7239 parent = get_die_parent (die);
7240
7241 /* First, compute a signature for just the type name (and its surrounding
7242 context, if any. This is stored in the type unit DIE for link-time
7243 ODR (one-definition rule) checking. */
7244
7245 if (is_cxx () && name != NULL)
7246 {
7247 md5_init_ctx (&ctx);
7248
7249 /* Checksum the names of surrounding namespaces and structures. */
7250 if (parent != NULL)
7251 checksum_die_context (parent, &ctx);
7252
7253 /* Checksum the current DIE. */
7254 die_odr_checksum (die->die_tag, name, &ctx);
7255 md5_finish_ctx (&ctx, checksum);
7256
7257 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7258 }
7259
7260 /* Next, compute the complete type signature. */
7261
7262 md5_init_ctx (&ctx);
7263 mark = 1;
7264 die->die_mark = mark;
7265
7266 /* Checksum the names of surrounding namespaces and structures. */
7267 if (parent != NULL)
7268 checksum_die_context (parent, &ctx);
7269
7270 /* Checksum the DIE and its children. */
7271 die_checksum_ordered (die, &ctx, &mark);
7272 unmark_all_dies (die);
7273 md5_finish_ctx (&ctx, checksum);
7274
7275 /* Store the signature in the type node and link the type DIE and the
7276 type node together. */
7277 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7278 DWARF_TYPE_SIGNATURE_SIZE);
7279 die->comdat_type_p = true;
7280 die->die_id.die_type_node = type_node;
7281 type_node->type_die = die;
7282
7283 /* If the DIE is a specification, link its declaration to the type node
7284 as well. */
7285 if (decl != NULL)
7286 {
7287 decl->comdat_type_p = true;
7288 decl->die_id.die_type_node = type_node;
7289 }
7290 }
7291
7292 /* Do the location expressions look same? */
7293 static inline int
7294 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7295 {
7296 return loc1->dw_loc_opc == loc2->dw_loc_opc
7297 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7298 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7299 }
7300
7301 /* Do the values look the same? */
7302 static int
7303 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7304 {
7305 dw_loc_descr_ref loc1, loc2;
7306 rtx r1, r2;
7307
7308 if (v1->val_class != v2->val_class)
7309 return 0;
7310
7311 switch (v1->val_class)
7312 {
7313 case dw_val_class_const:
7314 case dw_val_class_const_implicit:
7315 return v1->v.val_int == v2->v.val_int;
7316 case dw_val_class_unsigned_const:
7317 case dw_val_class_unsigned_const_implicit:
7318 return v1->v.val_unsigned == v2->v.val_unsigned;
7319 case dw_val_class_const_double:
7320 return v1->v.val_double.high == v2->v.val_double.high
7321 && v1->v.val_double.low == v2->v.val_double.low;
7322 case dw_val_class_wide_int:
7323 return *v1->v.val_wide == *v2->v.val_wide;
7324 case dw_val_class_vec:
7325 if (v1->v.val_vec.length != v2->v.val_vec.length
7326 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7327 return 0;
7328 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7329 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7330 return 0;
7331 return 1;
7332 case dw_val_class_flag:
7333 return v1->v.val_flag == v2->v.val_flag;
7334 case dw_val_class_str:
7335 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7336
7337 case dw_val_class_addr:
7338 r1 = v1->v.val_addr;
7339 r2 = v2->v.val_addr;
7340 if (GET_CODE (r1) != GET_CODE (r2))
7341 return 0;
7342 return !rtx_equal_p (r1, r2);
7343
7344 case dw_val_class_offset:
7345 return v1->v.val_offset == v2->v.val_offset;
7346
7347 case dw_val_class_loc:
7348 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7349 loc1 && loc2;
7350 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7351 if (!same_loc_p (loc1, loc2, mark))
7352 return 0;
7353 return !loc1 && !loc2;
7354
7355 case dw_val_class_die_ref:
7356 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7357
7358 case dw_val_class_fde_ref:
7359 case dw_val_class_vms_delta:
7360 case dw_val_class_lbl_id:
7361 case dw_val_class_lineptr:
7362 case dw_val_class_macptr:
7363 case dw_val_class_loclistsptr:
7364 case dw_val_class_high_pc:
7365 return 1;
7366
7367 case dw_val_class_file:
7368 case dw_val_class_file_implicit:
7369 return v1->v.val_file == v2->v.val_file;
7370
7371 case dw_val_class_data8:
7372 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7373
7374 default:
7375 return 1;
7376 }
7377 }
7378
7379 /* Do the attributes look the same? */
7380
7381 static int
7382 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7383 {
7384 if (at1->dw_attr != at2->dw_attr)
7385 return 0;
7386
7387 /* We don't care that this was compiled with a different compiler
7388 snapshot; if the output is the same, that's what matters. */
7389 if (at1->dw_attr == DW_AT_producer)
7390 return 1;
7391
7392 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7393 }
7394
7395 /* Do the dies look the same? */
7396
7397 static int
7398 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7399 {
7400 dw_die_ref c1, c2;
7401 dw_attr_node *a1;
7402 unsigned ix;
7403
7404 /* To avoid infinite recursion. */
7405 if (die1->die_mark)
7406 return die1->die_mark == die2->die_mark;
7407 die1->die_mark = die2->die_mark = ++(*mark);
7408
7409 if (die1->die_tag != die2->die_tag)
7410 return 0;
7411
7412 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7413 return 0;
7414
7415 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7416 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7417 return 0;
7418
7419 c1 = die1->die_child;
7420 c2 = die2->die_child;
7421 if (! c1)
7422 {
7423 if (c2)
7424 return 0;
7425 }
7426 else
7427 for (;;)
7428 {
7429 if (!same_die_p (c1, c2, mark))
7430 return 0;
7431 c1 = c1->die_sib;
7432 c2 = c2->die_sib;
7433 if (c1 == die1->die_child)
7434 {
7435 if (c2 == die2->die_child)
7436 break;
7437 else
7438 return 0;
7439 }
7440 }
7441
7442 return 1;
7443 }
7444
7445 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7446 children, and set die_symbol. */
7447
7448 static void
7449 compute_comp_unit_symbol (dw_die_ref unit_die)
7450 {
7451 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7452 const char *base = die_name ? lbasename (die_name) : "anonymous";
7453 char *name = XALLOCAVEC (char, strlen (base) + 64);
7454 char *p;
7455 int i, mark;
7456 unsigned char checksum[16];
7457 struct md5_ctx ctx;
7458
7459 /* Compute the checksum of the DIE, then append part of it as hex digits to
7460 the name filename of the unit. */
7461
7462 md5_init_ctx (&ctx);
7463 mark = 0;
7464 die_checksum (unit_die, &ctx, &mark);
7465 unmark_all_dies (unit_die);
7466 md5_finish_ctx (&ctx, checksum);
7467
7468 /* When we this for comp_unit_die () we have a DW_AT_name that might
7469 not start with a letter but with anything valid for filenames and
7470 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7471 character is not a letter. */
7472 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7473 clean_symbol_name (name);
7474
7475 p = name + strlen (name);
7476 for (i = 0; i < 4; i++)
7477 {
7478 sprintf (p, "%.2x", checksum[i]);
7479 p += 2;
7480 }
7481
7482 unit_die->die_id.die_symbol = xstrdup (name);
7483 }
7484
7485 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7486
7487 static int
7488 is_type_die (dw_die_ref die)
7489 {
7490 switch (die->die_tag)
7491 {
7492 case DW_TAG_array_type:
7493 case DW_TAG_class_type:
7494 case DW_TAG_interface_type:
7495 case DW_TAG_enumeration_type:
7496 case DW_TAG_pointer_type:
7497 case DW_TAG_reference_type:
7498 case DW_TAG_rvalue_reference_type:
7499 case DW_TAG_string_type:
7500 case DW_TAG_structure_type:
7501 case DW_TAG_subroutine_type:
7502 case DW_TAG_union_type:
7503 case DW_TAG_ptr_to_member_type:
7504 case DW_TAG_set_type:
7505 case DW_TAG_subrange_type:
7506 case DW_TAG_base_type:
7507 case DW_TAG_const_type:
7508 case DW_TAG_file_type:
7509 case DW_TAG_packed_type:
7510 case DW_TAG_volatile_type:
7511 case DW_TAG_typedef:
7512 return 1;
7513 default:
7514 return 0;
7515 }
7516 }
7517
7518 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7519 Basically, we want to choose the bits that are likely to be shared between
7520 compilations (types) and leave out the bits that are specific to individual
7521 compilations (functions). */
7522
7523 static int
7524 is_comdat_die (dw_die_ref c)
7525 {
7526 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7527 we do for stabs. The advantage is a greater likelihood of sharing between
7528 objects that don't include headers in the same order (and therefore would
7529 put the base types in a different comdat). jason 8/28/00 */
7530
7531 if (c->die_tag == DW_TAG_base_type)
7532 return 0;
7533
7534 if (c->die_tag == DW_TAG_pointer_type
7535 || c->die_tag == DW_TAG_reference_type
7536 || c->die_tag == DW_TAG_rvalue_reference_type
7537 || c->die_tag == DW_TAG_const_type
7538 || c->die_tag == DW_TAG_volatile_type)
7539 {
7540 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7541
7542 return t ? is_comdat_die (t) : 0;
7543 }
7544
7545 return is_type_die (c);
7546 }
7547
7548 /* Returns true iff C is a compile-unit DIE. */
7549
7550 static inline bool
7551 is_cu_die (dw_die_ref c)
7552 {
7553 return c && (c->die_tag == DW_TAG_compile_unit
7554 || c->die_tag == DW_TAG_skeleton_unit);
7555 }
7556
7557 /* Returns true iff C is a unit DIE of some sort. */
7558
7559 static inline bool
7560 is_unit_die (dw_die_ref c)
7561 {
7562 return c && (c->die_tag == DW_TAG_compile_unit
7563 || c->die_tag == DW_TAG_partial_unit
7564 || c->die_tag == DW_TAG_type_unit
7565 || c->die_tag == DW_TAG_skeleton_unit);
7566 }
7567
7568 /* Returns true iff C is a namespace DIE. */
7569
7570 static inline bool
7571 is_namespace_die (dw_die_ref c)
7572 {
7573 return c && c->die_tag == DW_TAG_namespace;
7574 }
7575
7576 /* Returns true iff C is a class or structure DIE. */
7577
7578 static inline bool
7579 is_class_die (dw_die_ref c)
7580 {
7581 return c && (c->die_tag == DW_TAG_class_type
7582 || c->die_tag == DW_TAG_structure_type);
7583 }
7584
7585 /* Return non-zero if this DIE is a template parameter. */
7586
7587 static inline bool
7588 is_template_parameter (dw_die_ref die)
7589 {
7590 switch (die->die_tag)
7591 {
7592 case DW_TAG_template_type_param:
7593 case DW_TAG_template_value_param:
7594 case DW_TAG_GNU_template_template_param:
7595 case DW_TAG_GNU_template_parameter_pack:
7596 return true;
7597 default:
7598 return false;
7599 }
7600 }
7601
7602 /* Return non-zero if this DIE represents a template instantiation. */
7603
7604 static inline bool
7605 is_template_instantiation (dw_die_ref die)
7606 {
7607 dw_die_ref c;
7608
7609 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7610 return false;
7611 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7612 return false;
7613 }
7614
7615 static char *
7616 gen_internal_sym (const char *prefix)
7617 {
7618 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7619
7620 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7621 return xstrdup (buf);
7622 }
7623
7624 /* Return non-zero if this DIE is a declaration. */
7625
7626 static int
7627 is_declaration_die (dw_die_ref die)
7628 {
7629 dw_attr_node *a;
7630 unsigned ix;
7631
7632 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7633 if (a->dw_attr == DW_AT_declaration)
7634 return 1;
7635
7636 return 0;
7637 }
7638
7639 /* Return non-zero if this DIE is nested inside a subprogram. */
7640
7641 static int
7642 is_nested_in_subprogram (dw_die_ref die)
7643 {
7644 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7645
7646 if (decl == NULL)
7647 decl = die;
7648 return local_scope_p (decl);
7649 }
7650
7651 /* Return non-zero if this DIE contains a defining declaration of a
7652 subprogram. */
7653
7654 static int
7655 contains_subprogram_definition (dw_die_ref die)
7656 {
7657 dw_die_ref c;
7658
7659 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7660 return 1;
7661 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7662 return 0;
7663 }
7664
7665 /* Return non-zero if this is a type DIE that should be moved to a
7666 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7667 unit type. */
7668
7669 static int
7670 should_move_die_to_comdat (dw_die_ref die)
7671 {
7672 switch (die->die_tag)
7673 {
7674 case DW_TAG_class_type:
7675 case DW_TAG_structure_type:
7676 case DW_TAG_enumeration_type:
7677 case DW_TAG_union_type:
7678 /* Don't move declarations, inlined instances, types nested in a
7679 subprogram, or types that contain subprogram definitions. */
7680 if (is_declaration_die (die)
7681 || get_AT (die, DW_AT_abstract_origin)
7682 || is_nested_in_subprogram (die)
7683 || contains_subprogram_definition (die))
7684 return 0;
7685 return 1;
7686 case DW_TAG_array_type:
7687 case DW_TAG_interface_type:
7688 case DW_TAG_pointer_type:
7689 case DW_TAG_reference_type:
7690 case DW_TAG_rvalue_reference_type:
7691 case DW_TAG_string_type:
7692 case DW_TAG_subroutine_type:
7693 case DW_TAG_ptr_to_member_type:
7694 case DW_TAG_set_type:
7695 case DW_TAG_subrange_type:
7696 case DW_TAG_base_type:
7697 case DW_TAG_const_type:
7698 case DW_TAG_file_type:
7699 case DW_TAG_packed_type:
7700 case DW_TAG_volatile_type:
7701 case DW_TAG_typedef:
7702 default:
7703 return 0;
7704 }
7705 }
7706
7707 /* Make a clone of DIE. */
7708
7709 static dw_die_ref
7710 clone_die (dw_die_ref die)
7711 {
7712 dw_die_ref clone = new_die_raw (die->die_tag);
7713 dw_attr_node *a;
7714 unsigned ix;
7715
7716 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7717 add_dwarf_attr (clone, a);
7718
7719 return clone;
7720 }
7721
7722 /* Make a clone of the tree rooted at DIE. */
7723
7724 static dw_die_ref
7725 clone_tree (dw_die_ref die)
7726 {
7727 dw_die_ref c;
7728 dw_die_ref clone = clone_die (die);
7729
7730 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7731
7732 return clone;
7733 }
7734
7735 /* Make a clone of DIE as a declaration. */
7736
7737 static dw_die_ref
7738 clone_as_declaration (dw_die_ref die)
7739 {
7740 dw_die_ref clone;
7741 dw_die_ref decl;
7742 dw_attr_node *a;
7743 unsigned ix;
7744
7745 /* If the DIE is already a declaration, just clone it. */
7746 if (is_declaration_die (die))
7747 return clone_die (die);
7748
7749 /* If the DIE is a specification, just clone its declaration DIE. */
7750 decl = get_AT_ref (die, DW_AT_specification);
7751 if (decl != NULL)
7752 {
7753 clone = clone_die (decl);
7754 if (die->comdat_type_p)
7755 add_AT_die_ref (clone, DW_AT_signature, die);
7756 return clone;
7757 }
7758
7759 clone = new_die_raw (die->die_tag);
7760
7761 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7762 {
7763 /* We don't want to copy over all attributes.
7764 For example we don't want DW_AT_byte_size because otherwise we will no
7765 longer have a declaration and GDB will treat it as a definition. */
7766
7767 switch (a->dw_attr)
7768 {
7769 case DW_AT_abstract_origin:
7770 case DW_AT_artificial:
7771 case DW_AT_containing_type:
7772 case DW_AT_external:
7773 case DW_AT_name:
7774 case DW_AT_type:
7775 case DW_AT_virtuality:
7776 case DW_AT_linkage_name:
7777 case DW_AT_MIPS_linkage_name:
7778 add_dwarf_attr (clone, a);
7779 break;
7780 case DW_AT_byte_size:
7781 case DW_AT_alignment:
7782 default:
7783 break;
7784 }
7785 }
7786
7787 if (die->comdat_type_p)
7788 add_AT_die_ref (clone, DW_AT_signature, die);
7789
7790 add_AT_flag (clone, DW_AT_declaration, 1);
7791 return clone;
7792 }
7793
7794
7795 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7796
7797 struct decl_table_entry
7798 {
7799 dw_die_ref orig;
7800 dw_die_ref copy;
7801 };
7802
7803 /* Helpers to manipulate hash table of copied declarations. */
7804
7805 /* Hashtable helpers. */
7806
7807 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7808 {
7809 typedef die_struct *compare_type;
7810 static inline hashval_t hash (const decl_table_entry *);
7811 static inline bool equal (const decl_table_entry *, const die_struct *);
7812 };
7813
7814 inline hashval_t
7815 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7816 {
7817 return htab_hash_pointer (entry->orig);
7818 }
7819
7820 inline bool
7821 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7822 const die_struct *entry2)
7823 {
7824 return entry1->orig == entry2;
7825 }
7826
7827 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7828
7829 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7830 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7831 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7832 to check if the ancestor has already been copied into UNIT. */
7833
7834 static dw_die_ref
7835 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7836 decl_hash_type *decl_table)
7837 {
7838 dw_die_ref parent = die->die_parent;
7839 dw_die_ref new_parent = unit;
7840 dw_die_ref copy;
7841 decl_table_entry **slot = NULL;
7842 struct decl_table_entry *entry = NULL;
7843
7844 if (decl_table)
7845 {
7846 /* Check if the entry has already been copied to UNIT. */
7847 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7848 INSERT);
7849 if (*slot != HTAB_EMPTY_ENTRY)
7850 {
7851 entry = *slot;
7852 return entry->copy;
7853 }
7854
7855 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7856 entry = XCNEW (struct decl_table_entry);
7857 entry->orig = die;
7858 entry->copy = NULL;
7859 *slot = entry;
7860 }
7861
7862 if (parent != NULL)
7863 {
7864 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7865 if (spec != NULL)
7866 parent = spec;
7867 if (!is_unit_die (parent))
7868 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7869 }
7870
7871 copy = clone_as_declaration (die);
7872 add_child_die (new_parent, copy);
7873
7874 if (decl_table)
7875 {
7876 /* Record the pointer to the copy. */
7877 entry->copy = copy;
7878 }
7879
7880 return copy;
7881 }
7882 /* Copy the declaration context to the new type unit DIE. This includes
7883 any surrounding namespace or type declarations. If the DIE has an
7884 AT_specification attribute, it also includes attributes and children
7885 attached to the specification, and returns a pointer to the original
7886 parent of the declaration DIE. Returns NULL otherwise. */
7887
7888 static dw_die_ref
7889 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7890 {
7891 dw_die_ref decl;
7892 dw_die_ref new_decl;
7893 dw_die_ref orig_parent = NULL;
7894
7895 decl = get_AT_ref (die, DW_AT_specification);
7896 if (decl == NULL)
7897 decl = die;
7898 else
7899 {
7900 unsigned ix;
7901 dw_die_ref c;
7902 dw_attr_node *a;
7903
7904 /* The original DIE will be changed to a declaration, and must
7905 be moved to be a child of the original declaration DIE. */
7906 orig_parent = decl->die_parent;
7907
7908 /* Copy the type node pointer from the new DIE to the original
7909 declaration DIE so we can forward references later. */
7910 decl->comdat_type_p = true;
7911 decl->die_id.die_type_node = die->die_id.die_type_node;
7912
7913 remove_AT (die, DW_AT_specification);
7914
7915 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7916 {
7917 if (a->dw_attr != DW_AT_name
7918 && a->dw_attr != DW_AT_declaration
7919 && a->dw_attr != DW_AT_external)
7920 add_dwarf_attr (die, a);
7921 }
7922
7923 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7924 }
7925
7926 if (decl->die_parent != NULL
7927 && !is_unit_die (decl->die_parent))
7928 {
7929 new_decl = copy_ancestor_tree (unit, decl, NULL);
7930 if (new_decl != NULL)
7931 {
7932 remove_AT (new_decl, DW_AT_signature);
7933 add_AT_specification (die, new_decl);
7934 }
7935 }
7936
7937 return orig_parent;
7938 }
7939
7940 /* Generate the skeleton ancestor tree for the given NODE, then clone
7941 the DIE and add the clone into the tree. */
7942
7943 static void
7944 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7945 {
7946 if (node->new_die != NULL)
7947 return;
7948
7949 node->new_die = clone_as_declaration (node->old_die);
7950
7951 if (node->parent != NULL)
7952 {
7953 generate_skeleton_ancestor_tree (node->parent);
7954 add_child_die (node->parent->new_die, node->new_die);
7955 }
7956 }
7957
7958 /* Generate a skeleton tree of DIEs containing any declarations that are
7959 found in the original tree. We traverse the tree looking for declaration
7960 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7961
7962 static void
7963 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7964 {
7965 skeleton_chain_node node;
7966 dw_die_ref c;
7967 dw_die_ref first;
7968 dw_die_ref prev = NULL;
7969 dw_die_ref next = NULL;
7970
7971 node.parent = parent;
7972
7973 first = c = parent->old_die->die_child;
7974 if (c)
7975 next = c->die_sib;
7976 if (c) do {
7977 if (prev == NULL || prev->die_sib == c)
7978 prev = c;
7979 c = next;
7980 next = (c == first ? NULL : c->die_sib);
7981 node.old_die = c;
7982 node.new_die = NULL;
7983 if (is_declaration_die (c))
7984 {
7985 if (is_template_instantiation (c))
7986 {
7987 /* Instantiated templates do not need to be cloned into the
7988 type unit. Just move the DIE and its children back to
7989 the skeleton tree (in the main CU). */
7990 remove_child_with_prev (c, prev);
7991 add_child_die (parent->new_die, c);
7992 c = prev;
7993 }
7994 else if (c->comdat_type_p)
7995 {
7996 /* This is the skeleton of earlier break_out_comdat_types
7997 type. Clone the existing DIE, but keep the children
7998 under the original (which is in the main CU). */
7999 dw_die_ref clone = clone_die (c);
8000
8001 replace_child (c, clone, prev);
8002 generate_skeleton_ancestor_tree (parent);
8003 add_child_die (parent->new_die, c);
8004 c = clone;
8005 continue;
8006 }
8007 else
8008 {
8009 /* Clone the existing DIE, move the original to the skeleton
8010 tree (which is in the main CU), and put the clone, with
8011 all the original's children, where the original came from
8012 (which is about to be moved to the type unit). */
8013 dw_die_ref clone = clone_die (c);
8014 move_all_children (c, clone);
8015
8016 /* If the original has a DW_AT_object_pointer attribute,
8017 it would now point to a child DIE just moved to the
8018 cloned tree, so we need to remove that attribute from
8019 the original. */
8020 remove_AT (c, DW_AT_object_pointer);
8021
8022 replace_child (c, clone, prev);
8023 generate_skeleton_ancestor_tree (parent);
8024 add_child_die (parent->new_die, c);
8025 node.old_die = clone;
8026 node.new_die = c;
8027 c = clone;
8028 }
8029 }
8030 generate_skeleton_bottom_up (&node);
8031 } while (next != NULL);
8032 }
8033
8034 /* Wrapper function for generate_skeleton_bottom_up. */
8035
8036 static dw_die_ref
8037 generate_skeleton (dw_die_ref die)
8038 {
8039 skeleton_chain_node node;
8040
8041 node.old_die = die;
8042 node.new_die = NULL;
8043 node.parent = NULL;
8044
8045 /* If this type definition is nested inside another type,
8046 and is not an instantiation of a template, always leave
8047 at least a declaration in its place. */
8048 if (die->die_parent != NULL
8049 && is_type_die (die->die_parent)
8050 && !is_template_instantiation (die))
8051 node.new_die = clone_as_declaration (die);
8052
8053 generate_skeleton_bottom_up (&node);
8054 return node.new_die;
8055 }
8056
8057 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8058 declaration. The original DIE is moved to a new compile unit so that
8059 existing references to it follow it to the new location. If any of the
8060 original DIE's descendants is a declaration, we need to replace the
8061 original DIE with a skeleton tree and move the declarations back into the
8062 skeleton tree. */
8063
8064 static dw_die_ref
8065 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8066 dw_die_ref prev)
8067 {
8068 dw_die_ref skeleton, orig_parent;
8069
8070 /* Copy the declaration context to the type unit DIE. If the returned
8071 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8072 that DIE. */
8073 orig_parent = copy_declaration_context (unit, child);
8074
8075 skeleton = generate_skeleton (child);
8076 if (skeleton == NULL)
8077 remove_child_with_prev (child, prev);
8078 else
8079 {
8080 skeleton->comdat_type_p = true;
8081 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8082
8083 /* If the original DIE was a specification, we need to put
8084 the skeleton under the parent DIE of the declaration.
8085 This leaves the original declaration in the tree, but
8086 it will be pruned later since there are no longer any
8087 references to it. */
8088 if (orig_parent != NULL)
8089 {
8090 remove_child_with_prev (child, prev);
8091 add_child_die (orig_parent, skeleton);
8092 }
8093 else
8094 replace_child (child, skeleton, prev);
8095 }
8096
8097 return skeleton;
8098 }
8099
8100 static void
8101 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8102 comdat_type_node *type_node,
8103 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8104
8105 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8106 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8107 DWARF procedure references in the DW_AT_location attribute. */
8108
8109 static dw_die_ref
8110 copy_dwarf_procedure (dw_die_ref die,
8111 comdat_type_node *type_node,
8112 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8113 {
8114 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8115
8116 /* DWARF procedures are not supposed to have children... */
8117 gcc_assert (die->die_child == NULL);
8118
8119 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8120 gcc_assert (vec_safe_length (die->die_attr) == 1
8121 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8122
8123 /* Do not copy more than once DWARF procedures. */
8124 bool existed;
8125 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8126 if (existed)
8127 return die_copy;
8128
8129 die_copy = clone_die (die);
8130 add_child_die (type_node->root_die, die_copy);
8131 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8132 return die_copy;
8133 }
8134
8135 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8136 procedures in DIE's attributes. */
8137
8138 static void
8139 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8140 comdat_type_node *type_node,
8141 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8142 {
8143 dw_attr_node *a;
8144 unsigned i;
8145
8146 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8147 {
8148 dw_loc_descr_ref loc;
8149
8150 if (a->dw_attr_val.val_class != dw_val_class_loc)
8151 continue;
8152
8153 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8154 {
8155 switch (loc->dw_loc_opc)
8156 {
8157 case DW_OP_call2:
8158 case DW_OP_call4:
8159 case DW_OP_call_ref:
8160 gcc_assert (loc->dw_loc_oprnd1.val_class
8161 == dw_val_class_die_ref);
8162 loc->dw_loc_oprnd1.v.val_die_ref.die
8163 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8164 type_node,
8165 copied_dwarf_procs);
8166
8167 default:
8168 break;
8169 }
8170 }
8171 }
8172 }
8173
8174 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8175 rewrite references to point to the copies.
8176
8177 References are looked for in DIE's attributes and recursively in all its
8178 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8179 mapping from old DWARF procedures to their copy. It is used not to copy
8180 twice the same DWARF procedure under TYPE_NODE. */
8181
8182 static void
8183 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8184 comdat_type_node *type_node,
8185 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8186 {
8187 dw_die_ref c;
8188
8189 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8190 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8191 type_node,
8192 copied_dwarf_procs));
8193 }
8194
8195 /* Traverse the DIE and set up additional .debug_types or .debug_info
8196 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8197 section. */
8198
8199 static void
8200 break_out_comdat_types (dw_die_ref die)
8201 {
8202 dw_die_ref c;
8203 dw_die_ref first;
8204 dw_die_ref prev = NULL;
8205 dw_die_ref next = NULL;
8206 dw_die_ref unit = NULL;
8207
8208 first = c = die->die_child;
8209 if (c)
8210 next = c->die_sib;
8211 if (c) do {
8212 if (prev == NULL || prev->die_sib == c)
8213 prev = c;
8214 c = next;
8215 next = (c == first ? NULL : c->die_sib);
8216 if (should_move_die_to_comdat (c))
8217 {
8218 dw_die_ref replacement;
8219 comdat_type_node *type_node;
8220
8221 /* Break out nested types into their own type units. */
8222 break_out_comdat_types (c);
8223
8224 /* Create a new type unit DIE as the root for the new tree, and
8225 add it to the list of comdat types. */
8226 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8227 add_AT_unsigned (unit, DW_AT_language,
8228 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8229 type_node = ggc_cleared_alloc<comdat_type_node> ();
8230 type_node->root_die = unit;
8231 type_node->next = comdat_type_list;
8232 comdat_type_list = type_node;
8233
8234 /* Generate the type signature. */
8235 generate_type_signature (c, type_node);
8236
8237 /* Copy the declaration context, attributes, and children of the
8238 declaration into the new type unit DIE, then remove this DIE
8239 from the main CU (or replace it with a skeleton if necessary). */
8240 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8241 type_node->skeleton_die = replacement;
8242
8243 /* Add the DIE to the new compunit. */
8244 add_child_die (unit, c);
8245
8246 /* Types can reference DWARF procedures for type size or data location
8247 expressions. Calls in DWARF expressions cannot target procedures
8248 that are not in the same section. So we must copy DWARF procedures
8249 along with this type and then rewrite references to them. */
8250 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8251 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8252
8253 if (replacement != NULL)
8254 c = replacement;
8255 }
8256 else if (c->die_tag == DW_TAG_namespace
8257 || c->die_tag == DW_TAG_class_type
8258 || c->die_tag == DW_TAG_structure_type
8259 || c->die_tag == DW_TAG_union_type)
8260 {
8261 /* Look for nested types that can be broken out. */
8262 break_out_comdat_types (c);
8263 }
8264 } while (next != NULL);
8265 }
8266
8267 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8268 Enter all the cloned children into the hash table decl_table. */
8269
8270 static dw_die_ref
8271 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8272 {
8273 dw_die_ref c;
8274 dw_die_ref clone;
8275 struct decl_table_entry *entry;
8276 decl_table_entry **slot;
8277
8278 if (die->die_tag == DW_TAG_subprogram)
8279 clone = clone_as_declaration (die);
8280 else
8281 clone = clone_die (die);
8282
8283 slot = decl_table->find_slot_with_hash (die,
8284 htab_hash_pointer (die), INSERT);
8285
8286 /* Assert that DIE isn't in the hash table yet. If it would be there
8287 before, the ancestors would be necessarily there as well, therefore
8288 clone_tree_partial wouldn't be called. */
8289 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8290
8291 entry = XCNEW (struct decl_table_entry);
8292 entry->orig = die;
8293 entry->copy = clone;
8294 *slot = entry;
8295
8296 if (die->die_tag != DW_TAG_subprogram)
8297 FOR_EACH_CHILD (die, c,
8298 add_child_die (clone, clone_tree_partial (c, decl_table)));
8299
8300 return clone;
8301 }
8302
8303 /* Walk the DIE and its children, looking for references to incomplete
8304 or trivial types that are unmarked (i.e., that are not in the current
8305 type_unit). */
8306
8307 static void
8308 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8309 {
8310 dw_die_ref c;
8311 dw_attr_node *a;
8312 unsigned ix;
8313
8314 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8315 {
8316 if (AT_class (a) == dw_val_class_die_ref)
8317 {
8318 dw_die_ref targ = AT_ref (a);
8319 decl_table_entry **slot;
8320 struct decl_table_entry *entry;
8321
8322 if (targ->die_mark != 0 || targ->comdat_type_p)
8323 continue;
8324
8325 slot = decl_table->find_slot_with_hash (targ,
8326 htab_hash_pointer (targ),
8327 INSERT);
8328
8329 if (*slot != HTAB_EMPTY_ENTRY)
8330 {
8331 /* TARG has already been copied, so we just need to
8332 modify the reference to point to the copy. */
8333 entry = *slot;
8334 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8335 }
8336 else
8337 {
8338 dw_die_ref parent = unit;
8339 dw_die_ref copy = clone_die (targ);
8340
8341 /* Record in DECL_TABLE that TARG has been copied.
8342 Need to do this now, before the recursive call,
8343 because DECL_TABLE may be expanded and SLOT
8344 would no longer be a valid pointer. */
8345 entry = XCNEW (struct decl_table_entry);
8346 entry->orig = targ;
8347 entry->copy = copy;
8348 *slot = entry;
8349
8350 /* If TARG is not a declaration DIE, we need to copy its
8351 children. */
8352 if (!is_declaration_die (targ))
8353 {
8354 FOR_EACH_CHILD (
8355 targ, c,
8356 add_child_die (copy,
8357 clone_tree_partial (c, decl_table)));
8358 }
8359
8360 /* Make sure the cloned tree is marked as part of the
8361 type unit. */
8362 mark_dies (copy);
8363
8364 /* If TARG has surrounding context, copy its ancestor tree
8365 into the new type unit. */
8366 if (targ->die_parent != NULL
8367 && !is_unit_die (targ->die_parent))
8368 parent = copy_ancestor_tree (unit, targ->die_parent,
8369 decl_table);
8370
8371 add_child_die (parent, copy);
8372 a->dw_attr_val.v.val_die_ref.die = copy;
8373
8374 /* Make sure the newly-copied DIE is walked. If it was
8375 installed in a previously-added context, it won't
8376 get visited otherwise. */
8377 if (parent != unit)
8378 {
8379 /* Find the highest point of the newly-added tree,
8380 mark each node along the way, and walk from there. */
8381 parent->die_mark = 1;
8382 while (parent->die_parent
8383 && parent->die_parent->die_mark == 0)
8384 {
8385 parent = parent->die_parent;
8386 parent->die_mark = 1;
8387 }
8388 copy_decls_walk (unit, parent, decl_table);
8389 }
8390 }
8391 }
8392 }
8393
8394 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8395 }
8396
8397 /* Copy declarations for "unworthy" types into the new comdat section.
8398 Incomplete types, modified types, and certain other types aren't broken
8399 out into comdat sections of their own, so they don't have a signature,
8400 and we need to copy the declaration into the same section so that we
8401 don't have an external reference. */
8402
8403 static void
8404 copy_decls_for_unworthy_types (dw_die_ref unit)
8405 {
8406 mark_dies (unit);
8407 decl_hash_type decl_table (10);
8408 copy_decls_walk (unit, unit, &decl_table);
8409 unmark_dies (unit);
8410 }
8411
8412 /* Traverse the DIE and add a sibling attribute if it may have the
8413 effect of speeding up access to siblings. To save some space,
8414 avoid generating sibling attributes for DIE's without children. */
8415
8416 static void
8417 add_sibling_attributes (dw_die_ref die)
8418 {
8419 dw_die_ref c;
8420
8421 if (! die->die_child)
8422 return;
8423
8424 if (die->die_parent && die != die->die_parent->die_child)
8425 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8426
8427 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8428 }
8429
8430 /* Output all location lists for the DIE and its children. */
8431
8432 static void
8433 output_location_lists (dw_die_ref die)
8434 {
8435 dw_die_ref c;
8436 dw_attr_node *a;
8437 unsigned ix;
8438
8439 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8440 if (AT_class (a) == dw_val_class_loc_list)
8441 output_loc_list (AT_loc_list (a));
8442
8443 FOR_EACH_CHILD (die, c, output_location_lists (c));
8444 }
8445
8446 /* During assign_location_list_indexes and output_loclists_offset the
8447 current index, after it the number of assigned indexes (i.e. how
8448 large the .debug_loclists* offset table should be). */
8449 static unsigned int loc_list_idx;
8450
8451 /* Output all location list offsets for the DIE and its children. */
8452
8453 static void
8454 output_loclists_offsets (dw_die_ref die)
8455 {
8456 dw_die_ref c;
8457 dw_attr_node *a;
8458 unsigned ix;
8459
8460 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8461 if (AT_class (a) == dw_val_class_loc_list)
8462 {
8463 dw_loc_list_ref l = AT_loc_list (a);
8464 if (l->offset_emitted)
8465 continue;
8466 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8467 loc_section_label, NULL);
8468 gcc_assert (l->hash == loc_list_idx);
8469 loc_list_idx++;
8470 l->offset_emitted = true;
8471 }
8472
8473 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8474 }
8475
8476 /* Recursively set indexes of location lists. */
8477
8478 static void
8479 assign_location_list_indexes (dw_die_ref die)
8480 {
8481 dw_die_ref c;
8482 dw_attr_node *a;
8483 unsigned ix;
8484
8485 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8486 if (AT_class (a) == dw_val_class_loc_list)
8487 {
8488 dw_loc_list_ref list = AT_loc_list (a);
8489 if (!list->num_assigned)
8490 {
8491 list->num_assigned = true;
8492 list->hash = loc_list_idx++;
8493 }
8494 }
8495
8496 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8497 }
8498
8499 /* We want to limit the number of external references, because they are
8500 larger than local references: a relocation takes multiple words, and
8501 even a sig8 reference is always eight bytes, whereas a local reference
8502 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8503 So if we encounter multiple external references to the same type DIE, we
8504 make a local typedef stub for it and redirect all references there.
8505
8506 This is the element of the hash table for keeping track of these
8507 references. */
8508
8509 struct external_ref
8510 {
8511 dw_die_ref type;
8512 dw_die_ref stub;
8513 unsigned n_refs;
8514 };
8515
8516 /* Hashtable helpers. */
8517
8518 struct external_ref_hasher : free_ptr_hash <external_ref>
8519 {
8520 static inline hashval_t hash (const external_ref *);
8521 static inline bool equal (const external_ref *, const external_ref *);
8522 };
8523
8524 inline hashval_t
8525 external_ref_hasher::hash (const external_ref *r)
8526 {
8527 dw_die_ref die = r->type;
8528 hashval_t h = 0;
8529
8530 /* We can't use the address of the DIE for hashing, because
8531 that will make the order of the stub DIEs non-deterministic. */
8532 if (! die->comdat_type_p)
8533 /* We have a symbol; use it to compute a hash. */
8534 h = htab_hash_string (die->die_id.die_symbol);
8535 else
8536 {
8537 /* We have a type signature; use a subset of the bits as the hash.
8538 The 8-byte signature is at least as large as hashval_t. */
8539 comdat_type_node *type_node = die->die_id.die_type_node;
8540 memcpy (&h, type_node->signature, sizeof (h));
8541 }
8542 return h;
8543 }
8544
8545 inline bool
8546 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8547 {
8548 return r1->type == r2->type;
8549 }
8550
8551 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8552
8553 /* Return a pointer to the external_ref for references to DIE. */
8554
8555 static struct external_ref *
8556 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8557 {
8558 struct external_ref ref, *ref_p;
8559 external_ref **slot;
8560
8561 ref.type = die;
8562 slot = map->find_slot (&ref, INSERT);
8563 if (*slot != HTAB_EMPTY_ENTRY)
8564 return *slot;
8565
8566 ref_p = XCNEW (struct external_ref);
8567 ref_p->type = die;
8568 *slot = ref_p;
8569 return ref_p;
8570 }
8571
8572 /* Subroutine of optimize_external_refs, below.
8573
8574 If we see a type skeleton, record it as our stub. If we see external
8575 references, remember how many we've seen. */
8576
8577 static void
8578 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8579 {
8580 dw_die_ref c;
8581 dw_attr_node *a;
8582 unsigned ix;
8583 struct external_ref *ref_p;
8584
8585 if (is_type_die (die)
8586 && (c = get_AT_ref (die, DW_AT_signature)))
8587 {
8588 /* This is a local skeleton; use it for local references. */
8589 ref_p = lookup_external_ref (map, c);
8590 ref_p->stub = die;
8591 }
8592
8593 /* Scan the DIE references, and remember any that refer to DIEs from
8594 other CUs (i.e. those which are not marked). */
8595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8596 if (AT_class (a) == dw_val_class_die_ref
8597 && (c = AT_ref (a))->die_mark == 0
8598 && is_type_die (c))
8599 {
8600 ref_p = lookup_external_ref (map, c);
8601 ref_p->n_refs++;
8602 }
8603
8604 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8605 }
8606
8607 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8608 points to an external_ref, DATA is the CU we're processing. If we don't
8609 already have a local stub, and we have multiple refs, build a stub. */
8610
8611 int
8612 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8613 {
8614 struct external_ref *ref_p = *slot;
8615
8616 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8617 {
8618 /* We have multiple references to this type, so build a small stub.
8619 Both of these forms are a bit dodgy from the perspective of the
8620 DWARF standard, since technically they should have names. */
8621 dw_die_ref cu = data;
8622 dw_die_ref type = ref_p->type;
8623 dw_die_ref stub = NULL;
8624
8625 if (type->comdat_type_p)
8626 {
8627 /* If we refer to this type via sig8, use AT_signature. */
8628 stub = new_die (type->die_tag, cu, NULL_TREE);
8629 add_AT_die_ref (stub, DW_AT_signature, type);
8630 }
8631 else
8632 {
8633 /* Otherwise, use a typedef with no name. */
8634 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8635 add_AT_die_ref (stub, DW_AT_type, type);
8636 }
8637
8638 stub->die_mark++;
8639 ref_p->stub = stub;
8640 }
8641 return 1;
8642 }
8643
8644 /* DIE is a unit; look through all the DIE references to see if there are
8645 any external references to types, and if so, create local stubs for
8646 them which will be applied in build_abbrev_table. This is useful because
8647 references to local DIEs are smaller. */
8648
8649 static external_ref_hash_type *
8650 optimize_external_refs (dw_die_ref die)
8651 {
8652 external_ref_hash_type *map = new external_ref_hash_type (10);
8653 optimize_external_refs_1 (die, map);
8654 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8655 return map;
8656 }
8657
8658 /* The following 3 variables are temporaries that are computed only during the
8659 build_abbrev_table call and used and released during the following
8660 optimize_abbrev_table call. */
8661
8662 /* First abbrev_id that can be optimized based on usage. */
8663 static unsigned int abbrev_opt_start;
8664
8665 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8666 abbrev_id smaller than this, because they must be already sized
8667 during build_abbrev_table). */
8668 static unsigned int abbrev_opt_base_type_end;
8669
8670 /* Vector of usage counts during build_abbrev_table. Indexed by
8671 abbrev_id - abbrev_opt_start. */
8672 static vec<unsigned int> abbrev_usage_count;
8673
8674 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8675 static vec<dw_die_ref> sorted_abbrev_dies;
8676
8677 /* The format of each DIE (and its attribute value pairs) is encoded in an
8678 abbreviation table. This routine builds the abbreviation table and assigns
8679 a unique abbreviation id for each abbreviation entry. The children of each
8680 die are visited recursively. */
8681
8682 static void
8683 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8684 {
8685 unsigned int abbrev_id = 0;
8686 dw_die_ref c;
8687 dw_attr_node *a;
8688 unsigned ix;
8689 dw_die_ref abbrev;
8690
8691 /* Scan the DIE references, and replace any that refer to
8692 DIEs from other CUs (i.e. those which are not marked) with
8693 the local stubs we built in optimize_external_refs. */
8694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8695 if (AT_class (a) == dw_val_class_die_ref
8696 && (c = AT_ref (a))->die_mark == 0)
8697 {
8698 struct external_ref *ref_p;
8699 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8700
8701 ref_p = lookup_external_ref (extern_map, c);
8702 if (ref_p->stub && ref_p->stub != die)
8703 change_AT_die_ref (a, ref_p->stub);
8704 else
8705 /* We aren't changing this reference, so mark it external. */
8706 set_AT_ref_external (a, 1);
8707 }
8708
8709 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8710 {
8711 dw_attr_node *die_a, *abbrev_a;
8712 unsigned ix;
8713 bool ok = true;
8714
8715 if (abbrev_id == 0)
8716 continue;
8717 if (abbrev->die_tag != die->die_tag)
8718 continue;
8719 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8720 continue;
8721
8722 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8723 continue;
8724
8725 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8726 {
8727 abbrev_a = &(*abbrev->die_attr)[ix];
8728 if ((abbrev_a->dw_attr != die_a->dw_attr)
8729 || (value_format (abbrev_a) != value_format (die_a)))
8730 {
8731 ok = false;
8732 break;
8733 }
8734 }
8735 if (ok)
8736 break;
8737 }
8738
8739 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8740 {
8741 vec_safe_push (abbrev_die_table, die);
8742 if (abbrev_opt_start)
8743 abbrev_usage_count.safe_push (0);
8744 }
8745 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8746 {
8747 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8748 sorted_abbrev_dies.safe_push (die);
8749 }
8750
8751 die->die_abbrev = abbrev_id;
8752 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8753 }
8754
8755 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8756 by die_abbrev's usage count, from the most commonly used
8757 abbreviation to the least. */
8758
8759 static int
8760 die_abbrev_cmp (const void *p1, const void *p2)
8761 {
8762 dw_die_ref die1 = *(const dw_die_ref *) p1;
8763 dw_die_ref die2 = *(const dw_die_ref *) p2;
8764
8765 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8766 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8767
8768 if (die1->die_abbrev >= abbrev_opt_base_type_end
8769 && die2->die_abbrev >= abbrev_opt_base_type_end)
8770 {
8771 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8772 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8773 return -1;
8774 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8775 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8776 return 1;
8777 }
8778
8779 /* Stabilize the sort. */
8780 if (die1->die_abbrev < die2->die_abbrev)
8781 return -1;
8782 if (die1->die_abbrev > die2->die_abbrev)
8783 return 1;
8784
8785 return 0;
8786 }
8787
8788 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8789 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8790 into dw_val_class_const_implicit or
8791 dw_val_class_unsigned_const_implicit. */
8792
8793 static void
8794 optimize_implicit_const (unsigned int first_id, unsigned int end,
8795 vec<bool> &implicit_consts)
8796 {
8797 /* It never makes sense if there is just one DIE using the abbreviation. */
8798 if (end < first_id + 2)
8799 return;
8800
8801 dw_attr_node *a;
8802 unsigned ix, i;
8803 dw_die_ref die = sorted_abbrev_dies[first_id];
8804 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8805 if (implicit_consts[ix])
8806 {
8807 enum dw_val_class new_class = dw_val_class_none;
8808 switch (AT_class (a))
8809 {
8810 case dw_val_class_unsigned_const:
8811 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8812 continue;
8813
8814 /* The .debug_abbrev section will grow by
8815 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8816 in all the DIEs using that abbreviation. */
8817 if (constant_size (AT_unsigned (a)) * (end - first_id)
8818 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8819 continue;
8820
8821 new_class = dw_val_class_unsigned_const_implicit;
8822 break;
8823
8824 case dw_val_class_const:
8825 new_class = dw_val_class_const_implicit;
8826 break;
8827
8828 case dw_val_class_file:
8829 new_class = dw_val_class_file_implicit;
8830 break;
8831
8832 default:
8833 continue;
8834 }
8835 for (i = first_id; i < end; i++)
8836 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8837 = new_class;
8838 }
8839 }
8840
8841 /* Attempt to optimize abbreviation table from abbrev_opt_start
8842 abbreviation above. */
8843
8844 static void
8845 optimize_abbrev_table (void)
8846 {
8847 if (abbrev_opt_start
8848 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8849 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8850 {
8851 auto_vec<bool, 32> implicit_consts;
8852 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8853
8854 unsigned int abbrev_id = abbrev_opt_start - 1;
8855 unsigned int first_id = ~0U;
8856 unsigned int last_abbrev_id = 0;
8857 unsigned int i;
8858 dw_die_ref die;
8859 if (abbrev_opt_base_type_end > abbrev_opt_start)
8860 abbrev_id = abbrev_opt_base_type_end - 1;
8861 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8862 most commonly used abbreviations come first. */
8863 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8864 {
8865 dw_attr_node *a;
8866 unsigned ix;
8867
8868 /* If calc_base_type_die_sizes has been called, the CU and
8869 base types after it can't be optimized, because we've already
8870 calculated their DIE offsets. We've sorted them first. */
8871 if (die->die_abbrev < abbrev_opt_base_type_end)
8872 continue;
8873 if (die->die_abbrev != last_abbrev_id)
8874 {
8875 last_abbrev_id = die->die_abbrev;
8876 if (dwarf_version >= 5 && first_id != ~0U)
8877 optimize_implicit_const (first_id, i, implicit_consts);
8878 abbrev_id++;
8879 (*abbrev_die_table)[abbrev_id] = die;
8880 if (dwarf_version >= 5)
8881 {
8882 first_id = i;
8883 implicit_consts.truncate (0);
8884
8885 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8886 switch (AT_class (a))
8887 {
8888 case dw_val_class_const:
8889 case dw_val_class_unsigned_const:
8890 case dw_val_class_file:
8891 implicit_consts.safe_push (true);
8892 break;
8893 default:
8894 implicit_consts.safe_push (false);
8895 break;
8896 }
8897 }
8898 }
8899 else if (dwarf_version >= 5)
8900 {
8901 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8902 if (!implicit_consts[ix])
8903 continue;
8904 else
8905 {
8906 dw_attr_node *other_a
8907 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8908 if (!dw_val_equal_p (&a->dw_attr_val,
8909 &other_a->dw_attr_val))
8910 implicit_consts[ix] = false;
8911 }
8912 }
8913 die->die_abbrev = abbrev_id;
8914 }
8915 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8916 if (dwarf_version >= 5 && first_id != ~0U)
8917 optimize_implicit_const (first_id, i, implicit_consts);
8918 }
8919
8920 abbrev_opt_start = 0;
8921 abbrev_opt_base_type_end = 0;
8922 abbrev_usage_count.release ();
8923 sorted_abbrev_dies.release ();
8924 }
8925 \f
8926 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8927
8928 static int
8929 constant_size (unsigned HOST_WIDE_INT value)
8930 {
8931 int log;
8932
8933 if (value == 0)
8934 log = 0;
8935 else
8936 log = floor_log2 (value);
8937
8938 log = log / 8;
8939 log = 1 << (floor_log2 (log) + 1);
8940
8941 return log;
8942 }
8943
8944 /* Return the size of a DIE as it is represented in the
8945 .debug_info section. */
8946
8947 static unsigned long
8948 size_of_die (dw_die_ref die)
8949 {
8950 unsigned long size = 0;
8951 dw_attr_node *a;
8952 unsigned ix;
8953 enum dwarf_form form;
8954
8955 size += size_of_uleb128 (die->die_abbrev);
8956 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8957 {
8958 switch (AT_class (a))
8959 {
8960 case dw_val_class_addr:
8961 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8962 {
8963 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8964 size += size_of_uleb128 (AT_index (a));
8965 }
8966 else
8967 size += DWARF2_ADDR_SIZE;
8968 break;
8969 case dw_val_class_offset:
8970 size += DWARF_OFFSET_SIZE;
8971 break;
8972 case dw_val_class_loc:
8973 {
8974 unsigned long lsize = size_of_locs (AT_loc (a));
8975
8976 /* Block length. */
8977 if (dwarf_version >= 4)
8978 size += size_of_uleb128 (lsize);
8979 else
8980 size += constant_size (lsize);
8981 size += lsize;
8982 }
8983 break;
8984 case dw_val_class_loc_list:
8985 if (dwarf_split_debug_info && dwarf_version >= 5)
8986 {
8987 gcc_assert (AT_loc_list (a)->num_assigned);
8988 size += size_of_uleb128 (AT_loc_list (a)->hash);
8989 }
8990 else
8991 size += DWARF_OFFSET_SIZE;
8992 break;
8993 case dw_val_class_range_list:
8994 if (value_format (a) == DW_FORM_rnglistx)
8995 {
8996 gcc_assert (rnglist_idx);
8997 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8998 size += size_of_uleb128 (r->idx);
8999 }
9000 else
9001 size += DWARF_OFFSET_SIZE;
9002 break;
9003 case dw_val_class_const:
9004 size += size_of_sleb128 (AT_int (a));
9005 break;
9006 case dw_val_class_unsigned_const:
9007 {
9008 int csize = constant_size (AT_unsigned (a));
9009 if (dwarf_version == 3
9010 && a->dw_attr == DW_AT_data_member_location
9011 && csize >= 4)
9012 size += size_of_uleb128 (AT_unsigned (a));
9013 else
9014 size += csize;
9015 }
9016 break;
9017 case dw_val_class_const_implicit:
9018 case dw_val_class_unsigned_const_implicit:
9019 case dw_val_class_file_implicit:
9020 /* These occupy no size in the DIE, just an extra sleb128 in
9021 .debug_abbrev. */
9022 break;
9023 case dw_val_class_const_double:
9024 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9025 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9026 size++; /* block */
9027 break;
9028 case dw_val_class_wide_int:
9029 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9030 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9031 if (get_full_len (*a->dw_attr_val.v.val_wide)
9032 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9033 size++; /* block */
9034 break;
9035 case dw_val_class_vec:
9036 size += constant_size (a->dw_attr_val.v.val_vec.length
9037 * a->dw_attr_val.v.val_vec.elt_size)
9038 + a->dw_attr_val.v.val_vec.length
9039 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9040 break;
9041 case dw_val_class_flag:
9042 if (dwarf_version >= 4)
9043 /* Currently all add_AT_flag calls pass in 1 as last argument,
9044 so DW_FORM_flag_present can be used. If that ever changes,
9045 we'll need to use DW_FORM_flag and have some optimization
9046 in build_abbrev_table that will change those to
9047 DW_FORM_flag_present if it is set to 1 in all DIEs using
9048 the same abbrev entry. */
9049 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9050 else
9051 size += 1;
9052 break;
9053 case dw_val_class_die_ref:
9054 if (AT_ref_external (a))
9055 {
9056 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9057 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9058 is sized by target address length, whereas in DWARF3
9059 it's always sized as an offset. */
9060 if (use_debug_types)
9061 size += DWARF_TYPE_SIGNATURE_SIZE;
9062 else if (dwarf_version == 2)
9063 size += DWARF2_ADDR_SIZE;
9064 else
9065 size += DWARF_OFFSET_SIZE;
9066 }
9067 else
9068 size += DWARF_OFFSET_SIZE;
9069 break;
9070 case dw_val_class_fde_ref:
9071 size += DWARF_OFFSET_SIZE;
9072 break;
9073 case dw_val_class_lbl_id:
9074 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9075 {
9076 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9077 size += size_of_uleb128 (AT_index (a));
9078 }
9079 else
9080 size += DWARF2_ADDR_SIZE;
9081 break;
9082 case dw_val_class_lineptr:
9083 case dw_val_class_macptr:
9084 case dw_val_class_loclistsptr:
9085 size += DWARF_OFFSET_SIZE;
9086 break;
9087 case dw_val_class_str:
9088 form = AT_string_form (a);
9089 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9090 size += DWARF_OFFSET_SIZE;
9091 else if (form == DW_FORM_GNU_str_index)
9092 size += size_of_uleb128 (AT_index (a));
9093 else
9094 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9095 break;
9096 case dw_val_class_file:
9097 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9098 break;
9099 case dw_val_class_data8:
9100 size += 8;
9101 break;
9102 case dw_val_class_vms_delta:
9103 size += DWARF_OFFSET_SIZE;
9104 break;
9105 case dw_val_class_high_pc:
9106 size += DWARF2_ADDR_SIZE;
9107 break;
9108 case dw_val_class_discr_value:
9109 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9110 break;
9111 case dw_val_class_discr_list:
9112 {
9113 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9114
9115 /* This is a block, so we have the block length and then its
9116 data. */
9117 size += constant_size (block_size) + block_size;
9118 }
9119 break;
9120 default:
9121 gcc_unreachable ();
9122 }
9123 }
9124
9125 return size;
9126 }
9127
9128 /* Size the debugging information associated with a given DIE. Visits the
9129 DIE's children recursively. Updates the global variable next_die_offset, on
9130 each time through. Uses the current value of next_die_offset to update the
9131 die_offset field in each DIE. */
9132
9133 static void
9134 calc_die_sizes (dw_die_ref die)
9135 {
9136 dw_die_ref c;
9137
9138 gcc_assert (die->die_offset == 0
9139 || (unsigned long int) die->die_offset == next_die_offset);
9140 die->die_offset = next_die_offset;
9141 next_die_offset += size_of_die (die);
9142
9143 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9144
9145 if (die->die_child != NULL)
9146 /* Count the null byte used to terminate sibling lists. */
9147 next_die_offset += 1;
9148 }
9149
9150 /* Size just the base type children at the start of the CU.
9151 This is needed because build_abbrev needs to size locs
9152 and sizing of type based stack ops needs to know die_offset
9153 values for the base types. */
9154
9155 static void
9156 calc_base_type_die_sizes (void)
9157 {
9158 unsigned long die_offset = (dwarf_split_debug_info
9159 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9160 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9161 unsigned int i;
9162 dw_die_ref base_type;
9163 #if ENABLE_ASSERT_CHECKING
9164 dw_die_ref prev = comp_unit_die ()->die_child;
9165 #endif
9166
9167 die_offset += size_of_die (comp_unit_die ());
9168 for (i = 0; base_types.iterate (i, &base_type); i++)
9169 {
9170 #if ENABLE_ASSERT_CHECKING
9171 gcc_assert (base_type->die_offset == 0
9172 && prev->die_sib == base_type
9173 && base_type->die_child == NULL
9174 && base_type->die_abbrev);
9175 prev = base_type;
9176 #endif
9177 if (abbrev_opt_start
9178 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9179 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9180 base_type->die_offset = die_offset;
9181 die_offset += size_of_die (base_type);
9182 }
9183 }
9184
9185 /* Set the marks for a die and its children. We do this so
9186 that we know whether or not a reference needs to use FORM_ref_addr; only
9187 DIEs in the same CU will be marked. We used to clear out the offset
9188 and use that as the flag, but ran into ordering problems. */
9189
9190 static void
9191 mark_dies (dw_die_ref die)
9192 {
9193 dw_die_ref c;
9194
9195 gcc_assert (!die->die_mark);
9196
9197 die->die_mark = 1;
9198 FOR_EACH_CHILD (die, c, mark_dies (c));
9199 }
9200
9201 /* Clear the marks for a die and its children. */
9202
9203 static void
9204 unmark_dies (dw_die_ref die)
9205 {
9206 dw_die_ref c;
9207
9208 if (! use_debug_types)
9209 gcc_assert (die->die_mark);
9210
9211 die->die_mark = 0;
9212 FOR_EACH_CHILD (die, c, unmark_dies (c));
9213 }
9214
9215 /* Clear the marks for a die, its children and referred dies. */
9216
9217 static void
9218 unmark_all_dies (dw_die_ref die)
9219 {
9220 dw_die_ref c;
9221 dw_attr_node *a;
9222 unsigned ix;
9223
9224 if (!die->die_mark)
9225 return;
9226 die->die_mark = 0;
9227
9228 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9229
9230 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9231 if (AT_class (a) == dw_val_class_die_ref)
9232 unmark_all_dies (AT_ref (a));
9233 }
9234
9235 /* Calculate if the entry should appear in the final output file. It may be
9236 from a pruned a type. */
9237
9238 static bool
9239 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9240 {
9241 /* By limiting gnu pubnames to definitions only, gold can generate a
9242 gdb index without entries for declarations, which don't include
9243 enough information to be useful. */
9244 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9245 return false;
9246
9247 if (table == pubname_table)
9248 {
9249 /* Enumerator names are part of the pubname table, but the
9250 parent DW_TAG_enumeration_type die may have been pruned.
9251 Don't output them if that is the case. */
9252 if (p->die->die_tag == DW_TAG_enumerator &&
9253 (p->die->die_parent == NULL
9254 || !p->die->die_parent->die_perennial_p))
9255 return false;
9256
9257 /* Everything else in the pubname table is included. */
9258 return true;
9259 }
9260
9261 /* The pubtypes table shouldn't include types that have been
9262 pruned. */
9263 return (p->die->die_offset != 0
9264 || !flag_eliminate_unused_debug_types);
9265 }
9266
9267 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9268 generated for the compilation unit. */
9269
9270 static unsigned long
9271 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9272 {
9273 unsigned long size;
9274 unsigned i;
9275 pubname_entry *p;
9276 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9277
9278 size = DWARF_PUBNAMES_HEADER_SIZE;
9279 FOR_EACH_VEC_ELT (*names, i, p)
9280 if (include_pubname_in_output (names, p))
9281 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9282
9283 size += DWARF_OFFSET_SIZE;
9284 return size;
9285 }
9286
9287 /* Return the size of the information in the .debug_aranges section. */
9288
9289 static unsigned long
9290 size_of_aranges (void)
9291 {
9292 unsigned long size;
9293
9294 size = DWARF_ARANGES_HEADER_SIZE;
9295
9296 /* Count the address/length pair for this compilation unit. */
9297 if (text_section_used)
9298 size += 2 * DWARF2_ADDR_SIZE;
9299 if (cold_text_section_used)
9300 size += 2 * DWARF2_ADDR_SIZE;
9301 if (have_multiple_function_sections)
9302 {
9303 unsigned fde_idx;
9304 dw_fde_ref fde;
9305
9306 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9307 {
9308 if (DECL_IGNORED_P (fde->decl))
9309 continue;
9310 if (!fde->in_std_section)
9311 size += 2 * DWARF2_ADDR_SIZE;
9312 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9313 size += 2 * DWARF2_ADDR_SIZE;
9314 }
9315 }
9316
9317 /* Count the two zero words used to terminated the address range table. */
9318 size += 2 * DWARF2_ADDR_SIZE;
9319 return size;
9320 }
9321 \f
9322 /* Select the encoding of an attribute value. */
9323
9324 static enum dwarf_form
9325 value_format (dw_attr_node *a)
9326 {
9327 switch (AT_class (a))
9328 {
9329 case dw_val_class_addr:
9330 /* Only very few attributes allow DW_FORM_addr. */
9331 switch (a->dw_attr)
9332 {
9333 case DW_AT_low_pc:
9334 case DW_AT_high_pc:
9335 case DW_AT_entry_pc:
9336 case DW_AT_trampoline:
9337 return (AT_index (a) == NOT_INDEXED
9338 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9339 default:
9340 break;
9341 }
9342 switch (DWARF2_ADDR_SIZE)
9343 {
9344 case 1:
9345 return DW_FORM_data1;
9346 case 2:
9347 return DW_FORM_data2;
9348 case 4:
9349 return DW_FORM_data4;
9350 case 8:
9351 return DW_FORM_data8;
9352 default:
9353 gcc_unreachable ();
9354 }
9355 case dw_val_class_loc_list:
9356 if (dwarf_split_debug_info
9357 && dwarf_version >= 5
9358 && AT_loc_list (a)->num_assigned)
9359 return DW_FORM_loclistx;
9360 /* FALLTHRU */
9361 case dw_val_class_range_list:
9362 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9363 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9364 care about sizes of .debug* sections in shared libraries and
9365 executables and don't take into account relocations that affect just
9366 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9367 table in the .debug_rnglists section. */
9368 if (dwarf_split_debug_info
9369 && dwarf_version >= 5
9370 && AT_class (a) == dw_val_class_range_list
9371 && rnglist_idx
9372 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9373 return DW_FORM_rnglistx;
9374 if (dwarf_version >= 4)
9375 return DW_FORM_sec_offset;
9376 /* FALLTHRU */
9377 case dw_val_class_vms_delta:
9378 case dw_val_class_offset:
9379 switch (DWARF_OFFSET_SIZE)
9380 {
9381 case 4:
9382 return DW_FORM_data4;
9383 case 8:
9384 return DW_FORM_data8;
9385 default:
9386 gcc_unreachable ();
9387 }
9388 case dw_val_class_loc:
9389 if (dwarf_version >= 4)
9390 return DW_FORM_exprloc;
9391 switch (constant_size (size_of_locs (AT_loc (a))))
9392 {
9393 case 1:
9394 return DW_FORM_block1;
9395 case 2:
9396 return DW_FORM_block2;
9397 case 4:
9398 return DW_FORM_block4;
9399 default:
9400 gcc_unreachable ();
9401 }
9402 case dw_val_class_const:
9403 return DW_FORM_sdata;
9404 case dw_val_class_unsigned_const:
9405 switch (constant_size (AT_unsigned (a)))
9406 {
9407 case 1:
9408 return DW_FORM_data1;
9409 case 2:
9410 return DW_FORM_data2;
9411 case 4:
9412 /* In DWARF3 DW_AT_data_member_location with
9413 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9414 constant, so we need to use DW_FORM_udata if we need
9415 a large constant. */
9416 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9417 return DW_FORM_udata;
9418 return DW_FORM_data4;
9419 case 8:
9420 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9421 return DW_FORM_udata;
9422 return DW_FORM_data8;
9423 default:
9424 gcc_unreachable ();
9425 }
9426 case dw_val_class_const_implicit:
9427 case dw_val_class_unsigned_const_implicit:
9428 case dw_val_class_file_implicit:
9429 return DW_FORM_implicit_const;
9430 case dw_val_class_const_double:
9431 switch (HOST_BITS_PER_WIDE_INT)
9432 {
9433 case 8:
9434 return DW_FORM_data2;
9435 case 16:
9436 return DW_FORM_data4;
9437 case 32:
9438 return DW_FORM_data8;
9439 case 64:
9440 if (dwarf_version >= 5)
9441 return DW_FORM_data16;
9442 /* FALLTHRU */
9443 default:
9444 return DW_FORM_block1;
9445 }
9446 case dw_val_class_wide_int:
9447 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9448 {
9449 case 8:
9450 return DW_FORM_data1;
9451 case 16:
9452 return DW_FORM_data2;
9453 case 32:
9454 return DW_FORM_data4;
9455 case 64:
9456 return DW_FORM_data8;
9457 case 128:
9458 if (dwarf_version >= 5)
9459 return DW_FORM_data16;
9460 /* FALLTHRU */
9461 default:
9462 return DW_FORM_block1;
9463 }
9464 case dw_val_class_vec:
9465 switch (constant_size (a->dw_attr_val.v.val_vec.length
9466 * a->dw_attr_val.v.val_vec.elt_size))
9467 {
9468 case 1:
9469 return DW_FORM_block1;
9470 case 2:
9471 return DW_FORM_block2;
9472 case 4:
9473 return DW_FORM_block4;
9474 default:
9475 gcc_unreachable ();
9476 }
9477 case dw_val_class_flag:
9478 if (dwarf_version >= 4)
9479 {
9480 /* Currently all add_AT_flag calls pass in 1 as last argument,
9481 so DW_FORM_flag_present can be used. If that ever changes,
9482 we'll need to use DW_FORM_flag and have some optimization
9483 in build_abbrev_table that will change those to
9484 DW_FORM_flag_present if it is set to 1 in all DIEs using
9485 the same abbrev entry. */
9486 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9487 return DW_FORM_flag_present;
9488 }
9489 return DW_FORM_flag;
9490 case dw_val_class_die_ref:
9491 if (AT_ref_external (a))
9492 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9493 else
9494 return DW_FORM_ref;
9495 case dw_val_class_fde_ref:
9496 return DW_FORM_data;
9497 case dw_val_class_lbl_id:
9498 return (AT_index (a) == NOT_INDEXED
9499 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9500 case dw_val_class_lineptr:
9501 case dw_val_class_macptr:
9502 case dw_val_class_loclistsptr:
9503 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9504 case dw_val_class_str:
9505 return AT_string_form (a);
9506 case dw_val_class_file:
9507 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9508 {
9509 case 1:
9510 return DW_FORM_data1;
9511 case 2:
9512 return DW_FORM_data2;
9513 case 4:
9514 return DW_FORM_data4;
9515 default:
9516 gcc_unreachable ();
9517 }
9518
9519 case dw_val_class_data8:
9520 return DW_FORM_data8;
9521
9522 case dw_val_class_high_pc:
9523 switch (DWARF2_ADDR_SIZE)
9524 {
9525 case 1:
9526 return DW_FORM_data1;
9527 case 2:
9528 return DW_FORM_data2;
9529 case 4:
9530 return DW_FORM_data4;
9531 case 8:
9532 return DW_FORM_data8;
9533 default:
9534 gcc_unreachable ();
9535 }
9536
9537 case dw_val_class_discr_value:
9538 return (a->dw_attr_val.v.val_discr_value.pos
9539 ? DW_FORM_udata
9540 : DW_FORM_sdata);
9541 case dw_val_class_discr_list:
9542 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9543 {
9544 case 1:
9545 return DW_FORM_block1;
9546 case 2:
9547 return DW_FORM_block2;
9548 case 4:
9549 return DW_FORM_block4;
9550 default:
9551 gcc_unreachable ();
9552 }
9553
9554 default:
9555 gcc_unreachable ();
9556 }
9557 }
9558
9559 /* Output the encoding of an attribute value. */
9560
9561 static void
9562 output_value_format (dw_attr_node *a)
9563 {
9564 enum dwarf_form form = value_format (a);
9565
9566 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9567 }
9568
9569 /* Given a die and id, produce the appropriate abbreviations. */
9570
9571 static void
9572 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9573 {
9574 unsigned ix;
9575 dw_attr_node *a_attr;
9576
9577 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9578 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9579 dwarf_tag_name (abbrev->die_tag));
9580
9581 if (abbrev->die_child != NULL)
9582 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9583 else
9584 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9585
9586 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9587 {
9588 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9589 dwarf_attr_name (a_attr->dw_attr));
9590 output_value_format (a_attr);
9591 if (value_format (a_attr) == DW_FORM_implicit_const)
9592 {
9593 if (AT_class (a_attr) == dw_val_class_file_implicit)
9594 {
9595 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9596 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9597 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9598 }
9599 else
9600 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9601 }
9602 }
9603
9604 dw2_asm_output_data (1, 0, NULL);
9605 dw2_asm_output_data (1, 0, NULL);
9606 }
9607
9608
9609 /* Output the .debug_abbrev section which defines the DIE abbreviation
9610 table. */
9611
9612 static void
9613 output_abbrev_section (void)
9614 {
9615 unsigned int abbrev_id;
9616 dw_die_ref abbrev;
9617
9618 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9619 if (abbrev_id != 0)
9620 output_die_abbrevs (abbrev_id, abbrev);
9621
9622 /* Terminate the table. */
9623 dw2_asm_output_data (1, 0, NULL);
9624 }
9625
9626 /* Return a new location list, given the begin and end range, and the
9627 expression. */
9628
9629 static inline dw_loc_list_ref
9630 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9631 const char *section)
9632 {
9633 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9634
9635 retlist->begin = begin;
9636 retlist->begin_entry = NULL;
9637 retlist->end = end;
9638 retlist->expr = expr;
9639 retlist->section = section;
9640
9641 return retlist;
9642 }
9643
9644 /* Generate a new internal symbol for this location list node, if it
9645 hasn't got one yet. */
9646
9647 static inline void
9648 gen_llsym (dw_loc_list_ref list)
9649 {
9650 gcc_assert (!list->ll_symbol);
9651 list->ll_symbol = gen_internal_sym ("LLST");
9652 }
9653
9654 /* Output the location list given to us. */
9655
9656 static void
9657 output_loc_list (dw_loc_list_ref list_head)
9658 {
9659 if (list_head->emitted)
9660 return;
9661 list_head->emitted = true;
9662
9663 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9664
9665 dw_loc_list_ref curr = list_head;
9666 const char *last_section = NULL;
9667 const char *base_label = NULL;
9668
9669 /* Walk the location list, and output each range + expression. */
9670 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9671 {
9672 unsigned long size;
9673 /* Don't output an entry that starts and ends at the same address. */
9674 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9675 continue;
9676 size = size_of_locs (curr->expr);
9677 /* If the expression is too large, drop it on the floor. We could
9678 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9679 in the expression, but >= 64KB expressions for a single value
9680 in a single range are unlikely very useful. */
9681 if (dwarf_version < 5 && size > 0xffff)
9682 continue;
9683 if (dwarf_version >= 5)
9684 {
9685 if (dwarf_split_debug_info)
9686 {
9687 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9688 uleb128 index into .debug_addr and uleb128 length. */
9689 dw2_asm_output_data (1, DW_LLE_startx_length,
9690 "DW_LLE_startx_length (%s)",
9691 list_head->ll_symbol);
9692 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9693 "Location list range start index "
9694 "(%s)", curr->begin);
9695 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9696 For that case we probably need to emit DW_LLE_startx_endx,
9697 but we'd need 2 .debug_addr entries rather than just one. */
9698 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9699 "Location list length (%s)",
9700 list_head->ll_symbol);
9701 }
9702 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9703 {
9704 /* If all code is in .text section, the base address is
9705 already provided by the CU attributes. Use
9706 DW_LLE_offset_pair where both addresses are uleb128 encoded
9707 offsets against that base. */
9708 dw2_asm_output_data (1, DW_LLE_offset_pair,
9709 "DW_LLE_offset_pair (%s)",
9710 list_head->ll_symbol);
9711 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9712 "Location list begin address (%s)",
9713 list_head->ll_symbol);
9714 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9715 "Location list end address (%s)",
9716 list_head->ll_symbol);
9717 }
9718 else if (HAVE_AS_LEB128)
9719 {
9720 /* Otherwise, find out how many consecutive entries could share
9721 the same base entry. If just one, emit DW_LLE_start_length,
9722 otherwise emit DW_LLE_base_address for the base address
9723 followed by a series of DW_LLE_offset_pair. */
9724 if (last_section == NULL || curr->section != last_section)
9725 {
9726 dw_loc_list_ref curr2;
9727 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9728 curr2 = curr2->dw_loc_next)
9729 {
9730 if (strcmp (curr2->begin, curr2->end) == 0
9731 && !curr2->force)
9732 continue;
9733 break;
9734 }
9735 if (curr2 == NULL || curr->section != curr2->section)
9736 last_section = NULL;
9737 else
9738 {
9739 last_section = curr->section;
9740 base_label = curr->begin;
9741 dw2_asm_output_data (1, DW_LLE_base_address,
9742 "DW_LLE_base_address (%s)",
9743 list_head->ll_symbol);
9744 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9745 "Base address (%s)",
9746 list_head->ll_symbol);
9747 }
9748 }
9749 /* Only one entry with the same base address. Use
9750 DW_LLE_start_length with absolute address and uleb128
9751 length. */
9752 if (last_section == NULL)
9753 {
9754 dw2_asm_output_data (1, DW_LLE_start_length,
9755 "DW_LLE_start_length (%s)",
9756 list_head->ll_symbol);
9757 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9758 "Location list begin address (%s)",
9759 list_head->ll_symbol);
9760 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9761 "Location list length "
9762 "(%s)", list_head->ll_symbol);
9763 }
9764 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9765 DW_LLE_base_address. */
9766 else
9767 {
9768 dw2_asm_output_data (1, DW_LLE_offset_pair,
9769 "DW_LLE_offset_pair (%s)",
9770 list_head->ll_symbol);
9771 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9772 "Location list begin address "
9773 "(%s)", list_head->ll_symbol);
9774 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9775 "Location list end address "
9776 "(%s)", list_head->ll_symbol);
9777 }
9778 }
9779 /* The assembler does not support .uleb128 directive. Emit
9780 DW_LLE_start_end with a pair of absolute addresses. */
9781 else
9782 {
9783 dw2_asm_output_data (1, DW_LLE_start_end,
9784 "DW_LLE_start_end (%s)",
9785 list_head->ll_symbol);
9786 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9787 "Location list begin address (%s)",
9788 list_head->ll_symbol);
9789 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9790 "Location list end address (%s)",
9791 list_head->ll_symbol);
9792 }
9793 }
9794 else if (dwarf_split_debug_info)
9795 {
9796 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9797 and 4 byte length. */
9798 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9799 "Location list start/length entry (%s)",
9800 list_head->ll_symbol);
9801 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9802 "Location list range start index (%s)",
9803 curr->begin);
9804 /* The length field is 4 bytes. If we ever need to support
9805 an 8-byte length, we can add a new DW_LLE code or fall back
9806 to DW_LLE_GNU_start_end_entry. */
9807 dw2_asm_output_delta (4, curr->end, curr->begin,
9808 "Location list range length (%s)",
9809 list_head->ll_symbol);
9810 }
9811 else if (!have_multiple_function_sections)
9812 {
9813 /* Pair of relative addresses against start of text section. */
9814 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9815 "Location list begin address (%s)",
9816 list_head->ll_symbol);
9817 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9818 "Location list end address (%s)",
9819 list_head->ll_symbol);
9820 }
9821 else
9822 {
9823 /* Pair of absolute addresses. */
9824 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9825 "Location list begin address (%s)",
9826 list_head->ll_symbol);
9827 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9828 "Location list end address (%s)",
9829 list_head->ll_symbol);
9830 }
9831
9832 /* Output the block length for this list of location operations. */
9833 if (dwarf_version >= 5)
9834 dw2_asm_output_data_uleb128 (size, "Location expression size");
9835 else
9836 {
9837 gcc_assert (size <= 0xffff);
9838 dw2_asm_output_data (2, size, "Location expression size");
9839 }
9840
9841 output_loc_sequence (curr->expr, -1);
9842 }
9843
9844 /* And finally list termination. */
9845 if (dwarf_version >= 5)
9846 dw2_asm_output_data (1, DW_LLE_end_of_list,
9847 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9848 else if (dwarf_split_debug_info)
9849 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9850 "Location list terminator (%s)",
9851 list_head->ll_symbol);
9852 else
9853 {
9854 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9855 "Location list terminator begin (%s)",
9856 list_head->ll_symbol);
9857 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9858 "Location list terminator end (%s)",
9859 list_head->ll_symbol);
9860 }
9861 }
9862
9863 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9864 section. Emit a relocated reference if val_entry is NULL, otherwise,
9865 emit an indirect reference. */
9866
9867 static void
9868 output_range_list_offset (dw_attr_node *a)
9869 {
9870 const char *name = dwarf_attr_name (a->dw_attr);
9871
9872 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9873 {
9874 if (dwarf_version >= 5)
9875 {
9876 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9877 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9878 debug_ranges_section, "%s", name);
9879 }
9880 else
9881 {
9882 char *p = strchr (ranges_section_label, '\0');
9883 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9884 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9885 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9886 debug_ranges_section, "%s", name);
9887 *p = '\0';
9888 }
9889 }
9890 else if (dwarf_version >= 5)
9891 {
9892 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9893 gcc_assert (rnglist_idx);
9894 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9895 }
9896 else
9897 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9898 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9899 "%s (offset from %s)", name, ranges_section_label);
9900 }
9901
9902 /* Output the offset into the debug_loc section. */
9903
9904 static void
9905 output_loc_list_offset (dw_attr_node *a)
9906 {
9907 char *sym = AT_loc_list (a)->ll_symbol;
9908
9909 gcc_assert (sym);
9910 if (!dwarf_split_debug_info)
9911 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9912 "%s", dwarf_attr_name (a->dw_attr));
9913 else if (dwarf_version >= 5)
9914 {
9915 gcc_assert (AT_loc_list (a)->num_assigned);
9916 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9917 dwarf_attr_name (a->dw_attr),
9918 sym);
9919 }
9920 else
9921 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9922 "%s", dwarf_attr_name (a->dw_attr));
9923 }
9924
9925 /* Output an attribute's index or value appropriately. */
9926
9927 static void
9928 output_attr_index_or_value (dw_attr_node *a)
9929 {
9930 const char *name = dwarf_attr_name (a->dw_attr);
9931
9932 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9933 {
9934 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9935 return;
9936 }
9937 switch (AT_class (a))
9938 {
9939 case dw_val_class_addr:
9940 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9941 break;
9942 case dw_val_class_high_pc:
9943 case dw_val_class_lbl_id:
9944 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9945 break;
9946 default:
9947 gcc_unreachable ();
9948 }
9949 }
9950
9951 /* Output a type signature. */
9952
9953 static inline void
9954 output_signature (const char *sig, const char *name)
9955 {
9956 int i;
9957
9958 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9959 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9960 }
9961
9962 /* Output a discriminant value. */
9963
9964 static inline void
9965 output_discr_value (dw_discr_value *discr_value, const char *name)
9966 {
9967 if (discr_value->pos)
9968 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9969 else
9970 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9971 }
9972
9973 /* Output the DIE and its attributes. Called recursively to generate
9974 the definitions of each child DIE. */
9975
9976 static void
9977 output_die (dw_die_ref die)
9978 {
9979 dw_attr_node *a;
9980 dw_die_ref c;
9981 unsigned long size;
9982 unsigned ix;
9983
9984 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9985 (unsigned long)die->die_offset,
9986 dwarf_tag_name (die->die_tag));
9987
9988 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9989 {
9990 const char *name = dwarf_attr_name (a->dw_attr);
9991
9992 switch (AT_class (a))
9993 {
9994 case dw_val_class_addr:
9995 output_attr_index_or_value (a);
9996 break;
9997
9998 case dw_val_class_offset:
9999 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10000 "%s", name);
10001 break;
10002
10003 case dw_val_class_range_list:
10004 output_range_list_offset (a);
10005 break;
10006
10007 case dw_val_class_loc:
10008 size = size_of_locs (AT_loc (a));
10009
10010 /* Output the block length for this list of location operations. */
10011 if (dwarf_version >= 4)
10012 dw2_asm_output_data_uleb128 (size, "%s", name);
10013 else
10014 dw2_asm_output_data (constant_size (size), size, "%s", name);
10015
10016 output_loc_sequence (AT_loc (a), -1);
10017 break;
10018
10019 case dw_val_class_const:
10020 /* ??? It would be slightly more efficient to use a scheme like is
10021 used for unsigned constants below, but gdb 4.x does not sign
10022 extend. Gdb 5.x does sign extend. */
10023 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10024 break;
10025
10026 case dw_val_class_unsigned_const:
10027 {
10028 int csize = constant_size (AT_unsigned (a));
10029 if (dwarf_version == 3
10030 && a->dw_attr == DW_AT_data_member_location
10031 && csize >= 4)
10032 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10033 else
10034 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10035 }
10036 break;
10037
10038 case dw_val_class_const_implicit:
10039 if (flag_debug_asm)
10040 fprintf (asm_out_file, "\t\t\t%s %s ("
10041 HOST_WIDE_INT_PRINT_DEC ")\n",
10042 ASM_COMMENT_START, name, AT_int (a));
10043 break;
10044
10045 case dw_val_class_unsigned_const_implicit:
10046 if (flag_debug_asm)
10047 fprintf (asm_out_file, "\t\t\t%s %s ("
10048 HOST_WIDE_INT_PRINT_HEX ")\n",
10049 ASM_COMMENT_START, name, AT_unsigned (a));
10050 break;
10051
10052 case dw_val_class_const_double:
10053 {
10054 unsigned HOST_WIDE_INT first, second;
10055
10056 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10057 dw2_asm_output_data (1,
10058 HOST_BITS_PER_DOUBLE_INT
10059 / HOST_BITS_PER_CHAR,
10060 NULL);
10061
10062 if (WORDS_BIG_ENDIAN)
10063 {
10064 first = a->dw_attr_val.v.val_double.high;
10065 second = a->dw_attr_val.v.val_double.low;
10066 }
10067 else
10068 {
10069 first = a->dw_attr_val.v.val_double.low;
10070 second = a->dw_attr_val.v.val_double.high;
10071 }
10072
10073 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10074 first, "%s", name);
10075 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10076 second, NULL);
10077 }
10078 break;
10079
10080 case dw_val_class_wide_int:
10081 {
10082 int i;
10083 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10084 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10085 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10086 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10087 * l, NULL);
10088
10089 if (WORDS_BIG_ENDIAN)
10090 for (i = len - 1; i >= 0; --i)
10091 {
10092 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10093 "%s", name);
10094 name = "";
10095 }
10096 else
10097 for (i = 0; i < len; ++i)
10098 {
10099 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10100 "%s", name);
10101 name = "";
10102 }
10103 }
10104 break;
10105
10106 case dw_val_class_vec:
10107 {
10108 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10109 unsigned int len = a->dw_attr_val.v.val_vec.length;
10110 unsigned int i;
10111 unsigned char *p;
10112
10113 dw2_asm_output_data (constant_size (len * elt_size),
10114 len * elt_size, "%s", name);
10115 if (elt_size > sizeof (HOST_WIDE_INT))
10116 {
10117 elt_size /= 2;
10118 len *= 2;
10119 }
10120 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10121 i < len;
10122 i++, p += elt_size)
10123 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10124 "fp or vector constant word %u", i);
10125 break;
10126 }
10127
10128 case dw_val_class_flag:
10129 if (dwarf_version >= 4)
10130 {
10131 /* Currently all add_AT_flag calls pass in 1 as last argument,
10132 so DW_FORM_flag_present can be used. If that ever changes,
10133 we'll need to use DW_FORM_flag and have some optimization
10134 in build_abbrev_table that will change those to
10135 DW_FORM_flag_present if it is set to 1 in all DIEs using
10136 the same abbrev entry. */
10137 gcc_assert (AT_flag (a) == 1);
10138 if (flag_debug_asm)
10139 fprintf (asm_out_file, "\t\t\t%s %s\n",
10140 ASM_COMMENT_START, name);
10141 break;
10142 }
10143 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10144 break;
10145
10146 case dw_val_class_loc_list:
10147 output_loc_list_offset (a);
10148 break;
10149
10150 case dw_val_class_die_ref:
10151 if (AT_ref_external (a))
10152 {
10153 if (AT_ref (a)->comdat_type_p)
10154 {
10155 comdat_type_node *type_node
10156 = AT_ref (a)->die_id.die_type_node;
10157
10158 gcc_assert (type_node);
10159 output_signature (type_node->signature, name);
10160 }
10161 else
10162 {
10163 const char *sym = AT_ref (a)->die_id.die_symbol;
10164 int size;
10165
10166 gcc_assert (sym);
10167 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10168 length, whereas in DWARF3 it's always sized as an
10169 offset. */
10170 if (dwarf_version == 2)
10171 size = DWARF2_ADDR_SIZE;
10172 else
10173 size = DWARF_OFFSET_SIZE;
10174 /* ??? We cannot unconditionally output die_offset if
10175 non-zero - others might create references to those
10176 DIEs via symbols.
10177 And we do not clear its DIE offset after outputting it
10178 (and the label refers to the actual DIEs, not the
10179 DWARF CU unit header which is when using label + offset
10180 would be the correct thing to do).
10181 ??? This is the reason for the with_offset flag. */
10182 if (AT_ref (a)->with_offset)
10183 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10184 debug_info_section, "%s", name);
10185 else
10186 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10187 name);
10188 }
10189 }
10190 else
10191 {
10192 gcc_assert (AT_ref (a)->die_offset);
10193 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10194 "%s", name);
10195 }
10196 break;
10197
10198 case dw_val_class_fde_ref:
10199 {
10200 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10201
10202 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10203 a->dw_attr_val.v.val_fde_index * 2);
10204 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10205 "%s", name);
10206 }
10207 break;
10208
10209 case dw_val_class_vms_delta:
10210 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10211 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10212 AT_vms_delta2 (a), AT_vms_delta1 (a),
10213 "%s", name);
10214 #else
10215 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10216 AT_vms_delta2 (a), AT_vms_delta1 (a),
10217 "%s", name);
10218 #endif
10219 break;
10220
10221 case dw_val_class_lbl_id:
10222 output_attr_index_or_value (a);
10223 break;
10224
10225 case dw_val_class_lineptr:
10226 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10227 debug_line_section, "%s", name);
10228 break;
10229
10230 case dw_val_class_macptr:
10231 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10232 debug_macinfo_section, "%s", name);
10233 break;
10234
10235 case dw_val_class_loclistsptr:
10236 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10237 debug_loc_section, "%s", name);
10238 break;
10239
10240 case dw_val_class_str:
10241 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10242 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10243 a->dw_attr_val.v.val_str->label,
10244 debug_str_section,
10245 "%s: \"%s\"", name, AT_string (a));
10246 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10247 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10248 a->dw_attr_val.v.val_str->label,
10249 debug_line_str_section,
10250 "%s: \"%s\"", name, AT_string (a));
10251 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10252 dw2_asm_output_data_uleb128 (AT_index (a),
10253 "%s: \"%s\"", name, AT_string (a));
10254 else
10255 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10256 break;
10257
10258 case dw_val_class_file:
10259 {
10260 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10261
10262 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10263 a->dw_attr_val.v.val_file->filename);
10264 break;
10265 }
10266
10267 case dw_val_class_file_implicit:
10268 if (flag_debug_asm)
10269 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10270 ASM_COMMENT_START, name,
10271 maybe_emit_file (a->dw_attr_val.v.val_file),
10272 a->dw_attr_val.v.val_file->filename);
10273 break;
10274
10275 case dw_val_class_data8:
10276 {
10277 int i;
10278
10279 for (i = 0; i < 8; i++)
10280 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10281 i == 0 ? "%s" : NULL, name);
10282 break;
10283 }
10284
10285 case dw_val_class_high_pc:
10286 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10287 get_AT_low_pc (die), "DW_AT_high_pc");
10288 break;
10289
10290 case dw_val_class_discr_value:
10291 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10292 break;
10293
10294 case dw_val_class_discr_list:
10295 {
10296 dw_discr_list_ref list = AT_discr_list (a);
10297 const int size = size_of_discr_list (list);
10298
10299 /* This is a block, so output its length first. */
10300 dw2_asm_output_data (constant_size (size), size,
10301 "%s: block size", name);
10302
10303 for (; list != NULL; list = list->dw_discr_next)
10304 {
10305 /* One byte for the discriminant value descriptor, and then as
10306 many LEB128 numbers as required. */
10307 if (list->dw_discr_range)
10308 dw2_asm_output_data (1, DW_DSC_range,
10309 "%s: DW_DSC_range", name);
10310 else
10311 dw2_asm_output_data (1, DW_DSC_label,
10312 "%s: DW_DSC_label", name);
10313
10314 output_discr_value (&list->dw_discr_lower_bound, name);
10315 if (list->dw_discr_range)
10316 output_discr_value (&list->dw_discr_upper_bound, name);
10317 }
10318 break;
10319 }
10320
10321 default:
10322 gcc_unreachable ();
10323 }
10324 }
10325
10326 FOR_EACH_CHILD (die, c, output_die (c));
10327
10328 /* Add null byte to terminate sibling list. */
10329 if (die->die_child != NULL)
10330 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10331 (unsigned long) die->die_offset);
10332 }
10333
10334 /* Output the compilation unit that appears at the beginning of the
10335 .debug_info section, and precedes the DIE descriptions. */
10336
10337 static void
10338 output_compilation_unit_header (enum dwarf_unit_type ut)
10339 {
10340 if (!XCOFF_DEBUGGING_INFO)
10341 {
10342 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10343 dw2_asm_output_data (4, 0xffffffff,
10344 "Initial length escape value indicating 64-bit DWARF extension");
10345 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10346 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10347 "Length of Compilation Unit Info");
10348 }
10349
10350 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10351 if (dwarf_version >= 5)
10352 {
10353 const char *name;
10354 switch (ut)
10355 {
10356 case DW_UT_compile: name = "DW_UT_compile"; break;
10357 case DW_UT_type: name = "DW_UT_type"; break;
10358 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10359 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10360 default: gcc_unreachable ();
10361 }
10362 dw2_asm_output_data (1, ut, "%s", name);
10363 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10364 }
10365 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10366 debug_abbrev_section,
10367 "Offset Into Abbrev. Section");
10368 if (dwarf_version < 5)
10369 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10370 }
10371
10372 /* Output the compilation unit DIE and its children. */
10373
10374 static void
10375 output_comp_unit (dw_die_ref die, int output_if_empty,
10376 const unsigned char *dwo_id)
10377 {
10378 const char *secname, *oldsym;
10379 char *tmp;
10380
10381 /* Unless we are outputting main CU, we may throw away empty ones. */
10382 if (!output_if_empty && die->die_child == NULL)
10383 return;
10384
10385 /* Even if there are no children of this DIE, we must output the information
10386 about the compilation unit. Otherwise, on an empty translation unit, we
10387 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10388 will then complain when examining the file. First mark all the DIEs in
10389 this CU so we know which get local refs. */
10390 mark_dies (die);
10391
10392 external_ref_hash_type *extern_map = optimize_external_refs (die);
10393
10394 /* For now, optimize only the main CU, in order to optimize the rest
10395 we'd need to see all of them earlier. Leave the rest for post-linking
10396 tools like DWZ. */
10397 if (die == comp_unit_die ())
10398 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10399
10400 build_abbrev_table (die, extern_map);
10401
10402 optimize_abbrev_table ();
10403
10404 delete extern_map;
10405
10406 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10407 next_die_offset = (dwo_id
10408 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10409 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10410 calc_die_sizes (die);
10411
10412 oldsym = die->die_id.die_symbol;
10413 if (oldsym && die->comdat_type_p)
10414 {
10415 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10416
10417 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10418 secname = tmp;
10419 die->die_id.die_symbol = NULL;
10420 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10421 }
10422 else
10423 {
10424 switch_to_section (debug_info_section);
10425 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10426 info_section_emitted = true;
10427 }
10428
10429 /* For LTO cross unit DIE refs we want a symbol on the start of the
10430 debuginfo section, not on the CU DIE. */
10431 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10432 {
10433 /* ??? No way to get visibility assembled without a decl. */
10434 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10435 get_identifier (oldsym), char_type_node);
10436 TREE_PUBLIC (decl) = true;
10437 TREE_STATIC (decl) = true;
10438 DECL_ARTIFICIAL (decl) = true;
10439 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10440 DECL_VISIBILITY_SPECIFIED (decl) = true;
10441 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10442 #ifdef ASM_WEAKEN_LABEL
10443 /* We prefer a .weak because that handles duplicates from duplicate
10444 archive members in a graceful way. */
10445 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10446 #else
10447 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10448 #endif
10449 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10450 }
10451
10452 /* Output debugging information. */
10453 output_compilation_unit_header (dwo_id
10454 ? DW_UT_split_compile : DW_UT_compile);
10455 if (dwarf_version >= 5)
10456 {
10457 if (dwo_id != NULL)
10458 for (int i = 0; i < 8; i++)
10459 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10460 }
10461 output_die (die);
10462
10463 /* Leave the marks on the main CU, so we can check them in
10464 output_pubnames. */
10465 if (oldsym)
10466 {
10467 unmark_dies (die);
10468 die->die_id.die_symbol = oldsym;
10469 }
10470 }
10471
10472 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10473 and .debug_pubtypes. This is configured per-target, but can be
10474 overridden by the -gpubnames or -gno-pubnames options. */
10475
10476 static inline bool
10477 want_pubnames (void)
10478 {
10479 if (debug_info_level <= DINFO_LEVEL_TERSE)
10480 return false;
10481 if (debug_generate_pub_sections != -1)
10482 return debug_generate_pub_sections;
10483 return targetm.want_debug_pub_sections;
10484 }
10485
10486 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10487
10488 static void
10489 add_AT_pubnames (dw_die_ref die)
10490 {
10491 if (want_pubnames ())
10492 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10493 }
10494
10495 /* Add a string attribute value to a skeleton DIE. */
10496
10497 static inline void
10498 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10499 const char *str)
10500 {
10501 dw_attr_node attr;
10502 struct indirect_string_node *node;
10503
10504 if (! skeleton_debug_str_hash)
10505 skeleton_debug_str_hash
10506 = hash_table<indirect_string_hasher>::create_ggc (10);
10507
10508 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10509 find_string_form (node);
10510 if (node->form == DW_FORM_GNU_str_index)
10511 node->form = DW_FORM_strp;
10512
10513 attr.dw_attr = attr_kind;
10514 attr.dw_attr_val.val_class = dw_val_class_str;
10515 attr.dw_attr_val.val_entry = NULL;
10516 attr.dw_attr_val.v.val_str = node;
10517 add_dwarf_attr (die, &attr);
10518 }
10519
10520 /* Helper function to generate top-level dies for skeleton debug_info and
10521 debug_types. */
10522
10523 static void
10524 add_top_level_skeleton_die_attrs (dw_die_ref die)
10525 {
10526 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10527 const char *comp_dir = comp_dir_string ();
10528
10529 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10530 if (comp_dir != NULL)
10531 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10532 add_AT_pubnames (die);
10533 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10534 }
10535
10536 /* Output skeleton debug sections that point to the dwo file. */
10537
10538 static void
10539 output_skeleton_debug_sections (dw_die_ref comp_unit,
10540 const unsigned char *dwo_id)
10541 {
10542 /* These attributes will be found in the full debug_info section. */
10543 remove_AT (comp_unit, DW_AT_producer);
10544 remove_AT (comp_unit, DW_AT_language);
10545
10546 switch_to_section (debug_skeleton_info_section);
10547 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10548
10549 /* Produce the skeleton compilation-unit header. This one differs enough from
10550 a normal CU header that it's better not to call output_compilation_unit
10551 header. */
10552 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10553 dw2_asm_output_data (4, 0xffffffff,
10554 "Initial length escape value indicating 64-bit "
10555 "DWARF extension");
10556
10557 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10558 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10559 - DWARF_INITIAL_LENGTH_SIZE
10560 + size_of_die (comp_unit),
10561 "Length of Compilation Unit Info");
10562 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10563 if (dwarf_version >= 5)
10564 {
10565 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10566 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10567 }
10568 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10569 debug_skeleton_abbrev_section,
10570 "Offset Into Abbrev. Section");
10571 if (dwarf_version < 5)
10572 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10573 else
10574 for (int i = 0; i < 8; i++)
10575 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10576
10577 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10578 output_die (comp_unit);
10579
10580 /* Build the skeleton debug_abbrev section. */
10581 switch_to_section (debug_skeleton_abbrev_section);
10582 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10583
10584 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10585
10586 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10587 }
10588
10589 /* Output a comdat type unit DIE and its children. */
10590
10591 static void
10592 output_comdat_type_unit (comdat_type_node *node)
10593 {
10594 const char *secname;
10595 char *tmp;
10596 int i;
10597 #if defined (OBJECT_FORMAT_ELF)
10598 tree comdat_key;
10599 #endif
10600
10601 /* First mark all the DIEs in this CU so we know which get local refs. */
10602 mark_dies (node->root_die);
10603
10604 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10605
10606 build_abbrev_table (node->root_die, extern_map);
10607
10608 delete extern_map;
10609 extern_map = NULL;
10610
10611 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10612 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10613 calc_die_sizes (node->root_die);
10614
10615 #if defined (OBJECT_FORMAT_ELF)
10616 if (dwarf_version >= 5)
10617 {
10618 if (!dwarf_split_debug_info)
10619 secname = ".debug_info";
10620 else
10621 secname = ".debug_info.dwo";
10622 }
10623 else if (!dwarf_split_debug_info)
10624 secname = ".debug_types";
10625 else
10626 secname = ".debug_types.dwo";
10627
10628 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10629 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10630 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10631 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10632 comdat_key = get_identifier (tmp);
10633 targetm.asm_out.named_section (secname,
10634 SECTION_DEBUG | SECTION_LINKONCE,
10635 comdat_key);
10636 #else
10637 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10638 sprintf (tmp, (dwarf_version >= 5
10639 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10640 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10641 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10642 secname = tmp;
10643 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10644 #endif
10645
10646 /* Output debugging information. */
10647 output_compilation_unit_header (dwarf_split_debug_info
10648 ? DW_UT_split_type : DW_UT_type);
10649 output_signature (node->signature, "Type Signature");
10650 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10651 "Offset to Type DIE");
10652 output_die (node->root_die);
10653
10654 unmark_dies (node->root_die);
10655 }
10656
10657 /* Return the DWARF2/3 pubname associated with a decl. */
10658
10659 static const char *
10660 dwarf2_name (tree decl, int scope)
10661 {
10662 if (DECL_NAMELESS (decl))
10663 return NULL;
10664 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10665 }
10666
10667 /* Add a new entry to .debug_pubnames if appropriate. */
10668
10669 static void
10670 add_pubname_string (const char *str, dw_die_ref die)
10671 {
10672 pubname_entry e;
10673
10674 e.die = die;
10675 e.name = xstrdup (str);
10676 vec_safe_push (pubname_table, e);
10677 }
10678
10679 static void
10680 add_pubname (tree decl, dw_die_ref die)
10681 {
10682 if (!want_pubnames ())
10683 return;
10684
10685 /* Don't add items to the table when we expect that the consumer will have
10686 just read the enclosing die. For example, if the consumer is looking at a
10687 class_member, it will either be inside the class already, or will have just
10688 looked up the class to find the member. Either way, searching the class is
10689 faster than searching the index. */
10690 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10691 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10692 {
10693 const char *name = dwarf2_name (decl, 1);
10694
10695 if (name)
10696 add_pubname_string (name, die);
10697 }
10698 }
10699
10700 /* Add an enumerator to the pubnames section. */
10701
10702 static void
10703 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10704 {
10705 pubname_entry e;
10706
10707 gcc_assert (scope_name);
10708 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10709 e.die = die;
10710 vec_safe_push (pubname_table, e);
10711 }
10712
10713 /* Add a new entry to .debug_pubtypes if appropriate. */
10714
10715 static void
10716 add_pubtype (tree decl, dw_die_ref die)
10717 {
10718 pubname_entry e;
10719
10720 if (!want_pubnames ())
10721 return;
10722
10723 if ((TREE_PUBLIC (decl)
10724 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10725 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10726 {
10727 tree scope = NULL;
10728 const char *scope_name = "";
10729 const char *sep = is_cxx () ? "::" : ".";
10730 const char *name;
10731
10732 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10733 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10734 {
10735 scope_name = lang_hooks.dwarf_name (scope, 1);
10736 if (scope_name != NULL && scope_name[0] != '\0')
10737 scope_name = concat (scope_name, sep, NULL);
10738 else
10739 scope_name = "";
10740 }
10741
10742 if (TYPE_P (decl))
10743 name = type_tag (decl);
10744 else
10745 name = lang_hooks.dwarf_name (decl, 1);
10746
10747 /* If we don't have a name for the type, there's no point in adding
10748 it to the table. */
10749 if (name != NULL && name[0] != '\0')
10750 {
10751 e.die = die;
10752 e.name = concat (scope_name, name, NULL);
10753 vec_safe_push (pubtype_table, e);
10754 }
10755
10756 /* Although it might be more consistent to add the pubinfo for the
10757 enumerators as their dies are created, they should only be added if the
10758 enum type meets the criteria above. So rather than re-check the parent
10759 enum type whenever an enumerator die is created, just output them all
10760 here. This isn't protected by the name conditional because anonymous
10761 enums don't have names. */
10762 if (die->die_tag == DW_TAG_enumeration_type)
10763 {
10764 dw_die_ref c;
10765
10766 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10767 }
10768 }
10769 }
10770
10771 /* Output a single entry in the pubnames table. */
10772
10773 static void
10774 output_pubname (dw_offset die_offset, pubname_entry *entry)
10775 {
10776 dw_die_ref die = entry->die;
10777 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10778
10779 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10780
10781 if (debug_generate_pub_sections == 2)
10782 {
10783 /* This logic follows gdb's method for determining the value of the flag
10784 byte. */
10785 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10786 switch (die->die_tag)
10787 {
10788 case DW_TAG_typedef:
10789 case DW_TAG_base_type:
10790 case DW_TAG_subrange_type:
10791 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10792 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10793 break;
10794 case DW_TAG_enumerator:
10795 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10796 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10797 if (!is_cxx ())
10798 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10799 break;
10800 case DW_TAG_subprogram:
10801 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10802 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10803 if (!is_ada ())
10804 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10805 break;
10806 case DW_TAG_constant:
10807 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10808 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10809 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10810 break;
10811 case DW_TAG_variable:
10812 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10813 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10814 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10815 break;
10816 case DW_TAG_namespace:
10817 case DW_TAG_imported_declaration:
10818 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10819 break;
10820 case DW_TAG_class_type:
10821 case DW_TAG_interface_type:
10822 case DW_TAG_structure_type:
10823 case DW_TAG_union_type:
10824 case DW_TAG_enumeration_type:
10825 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10826 if (!is_cxx ())
10827 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10828 break;
10829 default:
10830 /* An unusual tag. Leave the flag-byte empty. */
10831 break;
10832 }
10833 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10834 "GDB-index flags");
10835 }
10836
10837 dw2_asm_output_nstring (entry->name, -1, "external name");
10838 }
10839
10840
10841 /* Output the public names table used to speed up access to externally
10842 visible names; or the public types table used to find type definitions. */
10843
10844 static void
10845 output_pubnames (vec<pubname_entry, va_gc> *names)
10846 {
10847 unsigned i;
10848 unsigned long pubnames_length = size_of_pubnames (names);
10849 pubname_entry *pub;
10850
10851 if (!XCOFF_DEBUGGING_INFO)
10852 {
10853 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10854 dw2_asm_output_data (4, 0xffffffff,
10855 "Initial length escape value indicating 64-bit DWARF extension");
10856 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10857 "Pub Info Length");
10858 }
10859
10860 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10861 dw2_asm_output_data (2, 2, "DWARF Version");
10862
10863 if (dwarf_split_debug_info)
10864 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10865 debug_skeleton_info_section,
10866 "Offset of Compilation Unit Info");
10867 else
10868 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10869 debug_info_section,
10870 "Offset of Compilation Unit Info");
10871 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10872 "Compilation Unit Length");
10873
10874 FOR_EACH_VEC_ELT (*names, i, pub)
10875 {
10876 if (include_pubname_in_output (names, pub))
10877 {
10878 dw_offset die_offset = pub->die->die_offset;
10879
10880 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10881 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10882 gcc_assert (pub->die->die_mark);
10883
10884 /* If we're putting types in their own .debug_types sections,
10885 the .debug_pubtypes table will still point to the compile
10886 unit (not the type unit), so we want to use the offset of
10887 the skeleton DIE (if there is one). */
10888 if (pub->die->comdat_type_p && names == pubtype_table)
10889 {
10890 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10891
10892 if (type_node != NULL)
10893 die_offset = (type_node->skeleton_die != NULL
10894 ? type_node->skeleton_die->die_offset
10895 : comp_unit_die ()->die_offset);
10896 }
10897
10898 output_pubname (die_offset, pub);
10899 }
10900 }
10901
10902 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10903 }
10904
10905 /* Output public names and types tables if necessary. */
10906
10907 static void
10908 output_pubtables (void)
10909 {
10910 if (!want_pubnames () || !info_section_emitted)
10911 return;
10912
10913 switch_to_section (debug_pubnames_section);
10914 output_pubnames (pubname_table);
10915 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10916 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10917 simply won't look for the section. */
10918 switch_to_section (debug_pubtypes_section);
10919 output_pubnames (pubtype_table);
10920 }
10921
10922
10923 /* Output the information that goes into the .debug_aranges table.
10924 Namely, define the beginning and ending address range of the
10925 text section generated for this compilation unit. */
10926
10927 static void
10928 output_aranges (void)
10929 {
10930 unsigned i;
10931 unsigned long aranges_length = size_of_aranges ();
10932
10933 if (!XCOFF_DEBUGGING_INFO)
10934 {
10935 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10936 dw2_asm_output_data (4, 0xffffffff,
10937 "Initial length escape value indicating 64-bit DWARF extension");
10938 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10939 "Length of Address Ranges Info");
10940 }
10941
10942 /* Version number for aranges is still 2, even up to DWARF5. */
10943 dw2_asm_output_data (2, 2, "DWARF Version");
10944 if (dwarf_split_debug_info)
10945 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10946 debug_skeleton_info_section,
10947 "Offset of Compilation Unit Info");
10948 else
10949 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10950 debug_info_section,
10951 "Offset of Compilation Unit Info");
10952 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10953 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10954
10955 /* We need to align to twice the pointer size here. */
10956 if (DWARF_ARANGES_PAD_SIZE)
10957 {
10958 /* Pad using a 2 byte words so that padding is correct for any
10959 pointer size. */
10960 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10961 2 * DWARF2_ADDR_SIZE);
10962 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10963 dw2_asm_output_data (2, 0, NULL);
10964 }
10965
10966 /* It is necessary not to output these entries if the sections were
10967 not used; if the sections were not used, the length will be 0 and
10968 the address may end up as 0 if the section is discarded by ld
10969 --gc-sections, leaving an invalid (0, 0) entry that can be
10970 confused with the terminator. */
10971 if (text_section_used)
10972 {
10973 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10974 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10975 text_section_label, "Length");
10976 }
10977 if (cold_text_section_used)
10978 {
10979 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10980 "Address");
10981 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10982 cold_text_section_label, "Length");
10983 }
10984
10985 if (have_multiple_function_sections)
10986 {
10987 unsigned fde_idx;
10988 dw_fde_ref fde;
10989
10990 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10991 {
10992 if (DECL_IGNORED_P (fde->decl))
10993 continue;
10994 if (!fde->in_std_section)
10995 {
10996 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10997 "Address");
10998 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10999 fde->dw_fde_begin, "Length");
11000 }
11001 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11002 {
11003 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11004 "Address");
11005 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11006 fde->dw_fde_second_begin, "Length");
11007 }
11008 }
11009 }
11010
11011 /* Output the terminator words. */
11012 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11013 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11014 }
11015
11016 /* Add a new entry to .debug_ranges. Return its index into
11017 ranges_table vector. */
11018
11019 static unsigned int
11020 add_ranges_num (int num, bool maybe_new_sec)
11021 {
11022 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11023 vec_safe_push (ranges_table, r);
11024 return vec_safe_length (ranges_table) - 1;
11025 }
11026
11027 /* Add a new entry to .debug_ranges corresponding to a block, or a
11028 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11029 this entry might be in a different section from previous range. */
11030
11031 static unsigned int
11032 add_ranges (const_tree block, bool maybe_new_sec)
11033 {
11034 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11035 }
11036
11037 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11038 chain, or middle entry of a chain that will be directly referred to. */
11039
11040 static void
11041 note_rnglist_head (unsigned int offset)
11042 {
11043 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11044 return;
11045 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11046 }
11047
11048 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11049 When using dwarf_split_debug_info, address attributes in dies destined
11050 for the final executable should be direct references--setting the
11051 parameter force_direct ensures this behavior. */
11052
11053 static void
11054 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11055 bool *added, bool force_direct)
11056 {
11057 unsigned int in_use = vec_safe_length (ranges_by_label);
11058 unsigned int offset;
11059 dw_ranges_by_label rbl = { begin, end };
11060 vec_safe_push (ranges_by_label, rbl);
11061 offset = add_ranges_num (-(int)in_use - 1, true);
11062 if (!*added)
11063 {
11064 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11065 *added = true;
11066 note_rnglist_head (offset);
11067 }
11068 }
11069
11070 /* Emit .debug_ranges section. */
11071
11072 static void
11073 output_ranges (void)
11074 {
11075 unsigned i;
11076 static const char *const start_fmt = "Offset %#x";
11077 const char *fmt = start_fmt;
11078 dw_ranges *r;
11079
11080 switch_to_section (debug_ranges_section);
11081 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11082 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11083 {
11084 int block_num = r->num;
11085
11086 if (block_num > 0)
11087 {
11088 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11089 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11090
11091 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11092 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11093
11094 /* If all code is in the text section, then the compilation
11095 unit base address defaults to DW_AT_low_pc, which is the
11096 base of the text section. */
11097 if (!have_multiple_function_sections)
11098 {
11099 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11100 text_section_label,
11101 fmt, i * 2 * DWARF2_ADDR_SIZE);
11102 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11103 text_section_label, NULL);
11104 }
11105
11106 /* Otherwise, the compilation unit base address is zero,
11107 which allows us to use absolute addresses, and not worry
11108 about whether the target supports cross-section
11109 arithmetic. */
11110 else
11111 {
11112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11113 fmt, i * 2 * DWARF2_ADDR_SIZE);
11114 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11115 }
11116
11117 fmt = NULL;
11118 }
11119
11120 /* Negative block_num stands for an index into ranges_by_label. */
11121 else if (block_num < 0)
11122 {
11123 int lab_idx = - block_num - 1;
11124
11125 if (!have_multiple_function_sections)
11126 {
11127 gcc_unreachable ();
11128 #if 0
11129 /* If we ever use add_ranges_by_labels () for a single
11130 function section, all we have to do is to take out
11131 the #if 0 above. */
11132 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11133 (*ranges_by_label)[lab_idx].begin,
11134 text_section_label,
11135 fmt, i * 2 * DWARF2_ADDR_SIZE);
11136 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11137 (*ranges_by_label)[lab_idx].end,
11138 text_section_label, NULL);
11139 #endif
11140 }
11141 else
11142 {
11143 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11144 (*ranges_by_label)[lab_idx].begin,
11145 fmt, i * 2 * DWARF2_ADDR_SIZE);
11146 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11147 (*ranges_by_label)[lab_idx].end,
11148 NULL);
11149 }
11150 }
11151 else
11152 {
11153 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11154 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11155 fmt = start_fmt;
11156 }
11157 }
11158 }
11159
11160 /* Non-zero if .debug_line_str should be used for .debug_line section
11161 strings or strings that are likely shareable with those. */
11162 #define DWARF5_USE_DEBUG_LINE_STR \
11163 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11164 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11165 /* FIXME: there is no .debug_line_str.dwo section, \
11166 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11167 && !dwarf_split_debug_info)
11168
11169 /* Assign .debug_rnglists indexes. */
11170
11171 static void
11172 index_rnglists (void)
11173 {
11174 unsigned i;
11175 dw_ranges *r;
11176
11177 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11178 if (r->label)
11179 r->idx = rnglist_idx++;
11180 }
11181
11182 /* Emit .debug_rnglists section. */
11183
11184 static void
11185 output_rnglists (void)
11186 {
11187 unsigned i;
11188 dw_ranges *r;
11189 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11190 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11191 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11192
11193 switch_to_section (debug_ranges_section);
11194 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11195 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11196 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11197 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11198 dw2_asm_output_data (4, 0xffffffff,
11199 "Initial length escape value indicating "
11200 "64-bit DWARF extension");
11201 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11202 "Length of Range Lists");
11203 ASM_OUTPUT_LABEL (asm_out_file, l1);
11204 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11205 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11206 dw2_asm_output_data (1, 0, "Segment Size");
11207 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11208 about relocation sizes and primarily care about the size of .debug*
11209 sections in linked shared libraries and executables, then
11210 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11211 into it are usually larger than just DW_FORM_sec_offset offsets
11212 into the .debug_rnglists section. */
11213 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11214 "Offset Entry Count");
11215 if (dwarf_split_debug_info)
11216 {
11217 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11218 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11219 if (r->label)
11220 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11221 ranges_base_label, NULL);
11222 }
11223
11224 const char *lab = "";
11225 unsigned int len = vec_safe_length (ranges_table);
11226 const char *base = NULL;
11227 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11228 {
11229 int block_num = r->num;
11230
11231 if (r->label)
11232 {
11233 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11234 lab = r->label;
11235 }
11236 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11237 base = NULL;
11238 if (block_num > 0)
11239 {
11240 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11241 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11242
11243 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11244 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11245
11246 if (HAVE_AS_LEB128)
11247 {
11248 /* If all code is in the text section, then the compilation
11249 unit base address defaults to DW_AT_low_pc, which is the
11250 base of the text section. */
11251 if (!have_multiple_function_sections)
11252 {
11253 dw2_asm_output_data (1, DW_RLE_offset_pair,
11254 "DW_RLE_offset_pair (%s)", lab);
11255 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11256 "Range begin address (%s)", lab);
11257 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11258 "Range end address (%s)", lab);
11259 continue;
11260 }
11261 if (base == NULL)
11262 {
11263 dw_ranges *r2 = NULL;
11264 if (i < len - 1)
11265 r2 = &(*ranges_table)[i + 1];
11266 if (r2
11267 && r2->num != 0
11268 && r2->label == NULL
11269 && !r2->maybe_new_sec)
11270 {
11271 dw2_asm_output_data (1, DW_RLE_base_address,
11272 "DW_RLE_base_address (%s)", lab);
11273 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11274 "Base address (%s)", lab);
11275 strcpy (basebuf, blabel);
11276 base = basebuf;
11277 }
11278 }
11279 if (base)
11280 {
11281 dw2_asm_output_data (1, DW_RLE_offset_pair,
11282 "DW_RLE_offset_pair (%s)", lab);
11283 dw2_asm_output_delta_uleb128 (blabel, base,
11284 "Range begin address (%s)", lab);
11285 dw2_asm_output_delta_uleb128 (elabel, base,
11286 "Range end address (%s)", lab);
11287 continue;
11288 }
11289 dw2_asm_output_data (1, DW_RLE_start_length,
11290 "DW_RLE_start_length (%s)", lab);
11291 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11292 "Range begin address (%s)", lab);
11293 dw2_asm_output_delta_uleb128 (elabel, blabel,
11294 "Range length (%s)", lab);
11295 }
11296 else
11297 {
11298 dw2_asm_output_data (1, DW_RLE_start_end,
11299 "DW_RLE_start_end (%s)", lab);
11300 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11301 "Range begin address (%s)", lab);
11302 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11303 "Range end address (%s)", lab);
11304 }
11305 }
11306
11307 /* Negative block_num stands for an index into ranges_by_label. */
11308 else if (block_num < 0)
11309 {
11310 int lab_idx = - block_num - 1;
11311 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11312 const char *elabel = (*ranges_by_label)[lab_idx].end;
11313
11314 if (!have_multiple_function_sections)
11315 gcc_unreachable ();
11316 if (HAVE_AS_LEB128)
11317 {
11318 dw2_asm_output_data (1, DW_RLE_start_length,
11319 "DW_RLE_start_length (%s)", lab);
11320 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11321 "Range begin address (%s)", lab);
11322 dw2_asm_output_delta_uleb128 (elabel, blabel,
11323 "Range length (%s)", lab);
11324 }
11325 else
11326 {
11327 dw2_asm_output_data (1, DW_RLE_start_end,
11328 "DW_RLE_start_end (%s)", lab);
11329 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11330 "Range begin address (%s)", lab);
11331 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11332 "Range end address (%s)", lab);
11333 }
11334 }
11335 else
11336 dw2_asm_output_data (1, DW_RLE_end_of_list,
11337 "DW_RLE_end_of_list (%s)", lab);
11338 }
11339 ASM_OUTPUT_LABEL (asm_out_file, l2);
11340 }
11341
11342 /* Data structure containing information about input files. */
11343 struct file_info
11344 {
11345 const char *path; /* Complete file name. */
11346 const char *fname; /* File name part. */
11347 int length; /* Length of entire string. */
11348 struct dwarf_file_data * file_idx; /* Index in input file table. */
11349 int dir_idx; /* Index in directory table. */
11350 };
11351
11352 /* Data structure containing information about directories with source
11353 files. */
11354 struct dir_info
11355 {
11356 const char *path; /* Path including directory name. */
11357 int length; /* Path length. */
11358 int prefix; /* Index of directory entry which is a prefix. */
11359 int count; /* Number of files in this directory. */
11360 int dir_idx; /* Index of directory used as base. */
11361 };
11362
11363 /* Callback function for file_info comparison. We sort by looking at
11364 the directories in the path. */
11365
11366 static int
11367 file_info_cmp (const void *p1, const void *p2)
11368 {
11369 const struct file_info *const s1 = (const struct file_info *) p1;
11370 const struct file_info *const s2 = (const struct file_info *) p2;
11371 const unsigned char *cp1;
11372 const unsigned char *cp2;
11373
11374 /* Take care of file names without directories. We need to make sure that
11375 we return consistent values to qsort since some will get confused if
11376 we return the same value when identical operands are passed in opposite
11377 orders. So if neither has a directory, return 0 and otherwise return
11378 1 or -1 depending on which one has the directory. */
11379 if ((s1->path == s1->fname || s2->path == s2->fname))
11380 return (s2->path == s2->fname) - (s1->path == s1->fname);
11381
11382 cp1 = (const unsigned char *) s1->path;
11383 cp2 = (const unsigned char *) s2->path;
11384
11385 while (1)
11386 {
11387 ++cp1;
11388 ++cp2;
11389 /* Reached the end of the first path? If so, handle like above. */
11390 if ((cp1 == (const unsigned char *) s1->fname)
11391 || (cp2 == (const unsigned char *) s2->fname))
11392 return ((cp2 == (const unsigned char *) s2->fname)
11393 - (cp1 == (const unsigned char *) s1->fname));
11394
11395 /* Character of current path component the same? */
11396 else if (*cp1 != *cp2)
11397 return *cp1 - *cp2;
11398 }
11399 }
11400
11401 struct file_name_acquire_data
11402 {
11403 struct file_info *files;
11404 int used_files;
11405 int max_files;
11406 };
11407
11408 /* Traversal function for the hash table. */
11409
11410 int
11411 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11412 {
11413 struct dwarf_file_data *d = *slot;
11414 struct file_info *fi;
11415 const char *f;
11416
11417 gcc_assert (fnad->max_files >= d->emitted_number);
11418
11419 if (! d->emitted_number)
11420 return 1;
11421
11422 gcc_assert (fnad->max_files != fnad->used_files);
11423
11424 fi = fnad->files + fnad->used_files++;
11425
11426 /* Skip all leading "./". */
11427 f = d->filename;
11428 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11429 f += 2;
11430
11431 /* Create a new array entry. */
11432 fi->path = f;
11433 fi->length = strlen (f);
11434 fi->file_idx = d;
11435
11436 /* Search for the file name part. */
11437 f = strrchr (f, DIR_SEPARATOR);
11438 #if defined (DIR_SEPARATOR_2)
11439 {
11440 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11441
11442 if (g != NULL)
11443 {
11444 if (f == NULL || f < g)
11445 f = g;
11446 }
11447 }
11448 #endif
11449
11450 fi->fname = f == NULL ? fi->path : f + 1;
11451 return 1;
11452 }
11453
11454 /* Helper function for output_file_names. Emit a FORM encoded
11455 string STR, with assembly comment start ENTRY_KIND and
11456 index IDX */
11457
11458 static void
11459 output_line_string (enum dwarf_form form, const char *str,
11460 const char *entry_kind, unsigned int idx)
11461 {
11462 switch (form)
11463 {
11464 case DW_FORM_string:
11465 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11466 break;
11467 case DW_FORM_line_strp:
11468 if (!debug_line_str_hash)
11469 debug_line_str_hash
11470 = hash_table<indirect_string_hasher>::create_ggc (10);
11471
11472 struct indirect_string_node *node;
11473 node = find_AT_string_in_table (str, debug_line_str_hash);
11474 set_indirect_string (node);
11475 node->form = form;
11476 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11477 debug_line_str_section, "%s: %#x: \"%s\"",
11478 entry_kind, 0, node->str);
11479 break;
11480 default:
11481 gcc_unreachable ();
11482 }
11483 }
11484
11485 /* Output the directory table and the file name table. We try to minimize
11486 the total amount of memory needed. A heuristic is used to avoid large
11487 slowdowns with many input files. */
11488
11489 static void
11490 output_file_names (void)
11491 {
11492 struct file_name_acquire_data fnad;
11493 int numfiles;
11494 struct file_info *files;
11495 struct dir_info *dirs;
11496 int *saved;
11497 int *savehere;
11498 int *backmap;
11499 int ndirs;
11500 int idx_offset;
11501 int i;
11502
11503 if (!last_emitted_file)
11504 {
11505 if (dwarf_version >= 5)
11506 {
11507 dw2_asm_output_data (1, 0, "Directory entry format count");
11508 dw2_asm_output_data_uleb128 (0, "Directories count");
11509 dw2_asm_output_data (1, 0, "File name entry format count");
11510 dw2_asm_output_data_uleb128 (0, "File names count");
11511 }
11512 else
11513 {
11514 dw2_asm_output_data (1, 0, "End directory table");
11515 dw2_asm_output_data (1, 0, "End file name table");
11516 }
11517 return;
11518 }
11519
11520 numfiles = last_emitted_file->emitted_number;
11521
11522 /* Allocate the various arrays we need. */
11523 files = XALLOCAVEC (struct file_info, numfiles);
11524 dirs = XALLOCAVEC (struct dir_info, numfiles);
11525
11526 fnad.files = files;
11527 fnad.used_files = 0;
11528 fnad.max_files = numfiles;
11529 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11530 gcc_assert (fnad.used_files == fnad.max_files);
11531
11532 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11533
11534 /* Find all the different directories used. */
11535 dirs[0].path = files[0].path;
11536 dirs[0].length = files[0].fname - files[0].path;
11537 dirs[0].prefix = -1;
11538 dirs[0].count = 1;
11539 dirs[0].dir_idx = 0;
11540 files[0].dir_idx = 0;
11541 ndirs = 1;
11542
11543 for (i = 1; i < numfiles; i++)
11544 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11545 && memcmp (dirs[ndirs - 1].path, files[i].path,
11546 dirs[ndirs - 1].length) == 0)
11547 {
11548 /* Same directory as last entry. */
11549 files[i].dir_idx = ndirs - 1;
11550 ++dirs[ndirs - 1].count;
11551 }
11552 else
11553 {
11554 int j;
11555
11556 /* This is a new directory. */
11557 dirs[ndirs].path = files[i].path;
11558 dirs[ndirs].length = files[i].fname - files[i].path;
11559 dirs[ndirs].count = 1;
11560 dirs[ndirs].dir_idx = ndirs;
11561 files[i].dir_idx = ndirs;
11562
11563 /* Search for a prefix. */
11564 dirs[ndirs].prefix = -1;
11565 for (j = 0; j < ndirs; j++)
11566 if (dirs[j].length < dirs[ndirs].length
11567 && dirs[j].length > 1
11568 && (dirs[ndirs].prefix == -1
11569 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11570 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11571 dirs[ndirs].prefix = j;
11572
11573 ++ndirs;
11574 }
11575
11576 /* Now to the actual work. We have to find a subset of the directories which
11577 allow expressing the file name using references to the directory table
11578 with the least amount of characters. We do not do an exhaustive search
11579 where we would have to check out every combination of every single
11580 possible prefix. Instead we use a heuristic which provides nearly optimal
11581 results in most cases and never is much off. */
11582 saved = XALLOCAVEC (int, ndirs);
11583 savehere = XALLOCAVEC (int, ndirs);
11584
11585 memset (saved, '\0', ndirs * sizeof (saved[0]));
11586 for (i = 0; i < ndirs; i++)
11587 {
11588 int j;
11589 int total;
11590
11591 /* We can always save some space for the current directory. But this
11592 does not mean it will be enough to justify adding the directory. */
11593 savehere[i] = dirs[i].length;
11594 total = (savehere[i] - saved[i]) * dirs[i].count;
11595
11596 for (j = i + 1; j < ndirs; j++)
11597 {
11598 savehere[j] = 0;
11599 if (saved[j] < dirs[i].length)
11600 {
11601 /* Determine whether the dirs[i] path is a prefix of the
11602 dirs[j] path. */
11603 int k;
11604
11605 k = dirs[j].prefix;
11606 while (k != -1 && k != (int) i)
11607 k = dirs[k].prefix;
11608
11609 if (k == (int) i)
11610 {
11611 /* Yes it is. We can possibly save some memory by
11612 writing the filenames in dirs[j] relative to
11613 dirs[i]. */
11614 savehere[j] = dirs[i].length;
11615 total += (savehere[j] - saved[j]) * dirs[j].count;
11616 }
11617 }
11618 }
11619
11620 /* Check whether we can save enough to justify adding the dirs[i]
11621 directory. */
11622 if (total > dirs[i].length + 1)
11623 {
11624 /* It's worthwhile adding. */
11625 for (j = i; j < ndirs; j++)
11626 if (savehere[j] > 0)
11627 {
11628 /* Remember how much we saved for this directory so far. */
11629 saved[j] = savehere[j];
11630
11631 /* Remember the prefix directory. */
11632 dirs[j].dir_idx = i;
11633 }
11634 }
11635 }
11636
11637 /* Emit the directory name table. */
11638 idx_offset = dirs[0].length > 0 ? 1 : 0;
11639 enum dwarf_form str_form = DW_FORM_string;
11640 enum dwarf_form idx_form = DW_FORM_udata;
11641 if (dwarf_version >= 5)
11642 {
11643 const char *comp_dir = comp_dir_string ();
11644 if (comp_dir == NULL)
11645 comp_dir = "";
11646 dw2_asm_output_data (1, 1, "Directory entry format count");
11647 if (DWARF5_USE_DEBUG_LINE_STR)
11648 str_form = DW_FORM_line_strp;
11649 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11650 dw2_asm_output_data_uleb128 (str_form, "%s",
11651 get_DW_FORM_name (str_form));
11652 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11653 if (str_form == DW_FORM_string)
11654 {
11655 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11656 for (i = 1 - idx_offset; i < ndirs; i++)
11657 dw2_asm_output_nstring (dirs[i].path,
11658 dirs[i].length
11659 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11660 "Directory Entry: %#x", i + idx_offset);
11661 }
11662 else
11663 {
11664 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11665 for (i = 1 - idx_offset; i < ndirs; i++)
11666 {
11667 const char *str
11668 = ggc_alloc_string (dirs[i].path,
11669 dirs[i].length
11670 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11671 output_line_string (str_form, str, "Directory Entry",
11672 (unsigned) i + idx_offset);
11673 }
11674 }
11675 }
11676 else
11677 {
11678 for (i = 1 - idx_offset; i < ndirs; i++)
11679 dw2_asm_output_nstring (dirs[i].path,
11680 dirs[i].length
11681 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11682 "Directory Entry: %#x", i + idx_offset);
11683
11684 dw2_asm_output_data (1, 0, "End directory table");
11685 }
11686
11687 /* We have to emit them in the order of emitted_number since that's
11688 used in the debug info generation. To do this efficiently we
11689 generate a back-mapping of the indices first. */
11690 backmap = XALLOCAVEC (int, numfiles);
11691 for (i = 0; i < numfiles; i++)
11692 backmap[files[i].file_idx->emitted_number - 1] = i;
11693
11694 if (dwarf_version >= 5)
11695 {
11696 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11697 if (filename0 == NULL)
11698 filename0 = "";
11699 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11700 DW_FORM_data2. Choose one based on the number of directories
11701 and how much space would they occupy in each encoding.
11702 If we have at most 256 directories, all indexes fit into
11703 a single byte, so DW_FORM_data1 is most compact (if there
11704 are at most 128 directories, DW_FORM_udata would be as
11705 compact as that, but not shorter and slower to decode). */
11706 if (ndirs + idx_offset <= 256)
11707 idx_form = DW_FORM_data1;
11708 /* If there are more than 65536 directories, we have to use
11709 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11710 Otherwise, compute what space would occupy if all the indexes
11711 used DW_FORM_udata - sum - and compare that to how large would
11712 be DW_FORM_data2 encoding, and pick the more efficient one. */
11713 else if (ndirs + idx_offset <= 65536)
11714 {
11715 unsigned HOST_WIDE_INT sum = 1;
11716 for (i = 0; i < numfiles; i++)
11717 {
11718 int file_idx = backmap[i];
11719 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11720 sum += size_of_uleb128 (dir_idx);
11721 }
11722 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11723 idx_form = DW_FORM_data2;
11724 }
11725 #ifdef VMS_DEBUGGING_INFO
11726 dw2_asm_output_data (1, 4, "File name entry format count");
11727 #else
11728 dw2_asm_output_data (1, 2, "File name entry format count");
11729 #endif
11730 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11731 dw2_asm_output_data_uleb128 (str_form, "%s",
11732 get_DW_FORM_name (str_form));
11733 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11734 "DW_LNCT_directory_index");
11735 dw2_asm_output_data_uleb128 (idx_form, "%s",
11736 get_DW_FORM_name (idx_form));
11737 #ifdef VMS_DEBUGGING_INFO
11738 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11739 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11740 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11741 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11742 #endif
11743 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11744
11745 output_line_string (str_form, filename0, "File Entry", 0);
11746
11747 /* Include directory index. */
11748 if (idx_form != DW_FORM_udata)
11749 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11750 0, NULL);
11751 else
11752 dw2_asm_output_data_uleb128 (0, NULL);
11753
11754 #ifdef VMS_DEBUGGING_INFO
11755 dw2_asm_output_data_uleb128 (0, NULL);
11756 dw2_asm_output_data_uleb128 (0, NULL);
11757 #endif
11758 }
11759
11760 /* Now write all the file names. */
11761 for (i = 0; i < numfiles; i++)
11762 {
11763 int file_idx = backmap[i];
11764 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11765
11766 #ifdef VMS_DEBUGGING_INFO
11767 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11768
11769 /* Setting these fields can lead to debugger miscomparisons,
11770 but VMS Debug requires them to be set correctly. */
11771
11772 int ver;
11773 long long cdt;
11774 long siz;
11775 int maxfilelen = (strlen (files[file_idx].path)
11776 + dirs[dir_idx].length
11777 + MAX_VMS_VERSION_LEN + 1);
11778 char *filebuf = XALLOCAVEC (char, maxfilelen);
11779
11780 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11781 snprintf (filebuf, maxfilelen, "%s;%d",
11782 files[file_idx].path + dirs[dir_idx].length, ver);
11783
11784 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11785
11786 /* Include directory index. */
11787 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11788 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11789 dir_idx + idx_offset, NULL);
11790 else
11791 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11792
11793 /* Modification time. */
11794 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11795 &cdt, 0, 0, 0) == 0)
11796 ? cdt : 0, NULL);
11797
11798 /* File length in bytes. */
11799 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11800 0, &siz, 0, 0) == 0)
11801 ? siz : 0, NULL);
11802 #else
11803 output_line_string (str_form,
11804 files[file_idx].path + dirs[dir_idx].length,
11805 "File Entry", (unsigned) i + 1);
11806
11807 /* Include directory index. */
11808 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11809 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11810 dir_idx + idx_offset, NULL);
11811 else
11812 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11813
11814 if (dwarf_version >= 5)
11815 continue;
11816
11817 /* Modification time. */
11818 dw2_asm_output_data_uleb128 (0, NULL);
11819
11820 /* File length in bytes. */
11821 dw2_asm_output_data_uleb128 (0, NULL);
11822 #endif /* VMS_DEBUGGING_INFO */
11823 }
11824
11825 if (dwarf_version < 5)
11826 dw2_asm_output_data (1, 0, "End file name table");
11827 }
11828
11829
11830 /* Output one line number table into the .debug_line section. */
11831
11832 static void
11833 output_one_line_info_table (dw_line_info_table *table)
11834 {
11835 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11836 unsigned int current_line = 1;
11837 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11838 dw_line_info_entry *ent;
11839 size_t i;
11840
11841 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11842 {
11843 switch (ent->opcode)
11844 {
11845 case LI_set_address:
11846 /* ??? Unfortunately, we have little choice here currently, and
11847 must always use the most general form. GCC does not know the
11848 address delta itself, so we can't use DW_LNS_advance_pc. Many
11849 ports do have length attributes which will give an upper bound
11850 on the address range. We could perhaps use length attributes
11851 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11852 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11853
11854 /* This can handle any delta. This takes
11855 4+DWARF2_ADDR_SIZE bytes. */
11856 dw2_asm_output_data (1, 0, "set address %s", line_label);
11857 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11858 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11859 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11860 break;
11861
11862 case LI_set_line:
11863 if (ent->val == current_line)
11864 {
11865 /* We still need to start a new row, so output a copy insn. */
11866 dw2_asm_output_data (1, DW_LNS_copy,
11867 "copy line %u", current_line);
11868 }
11869 else
11870 {
11871 int line_offset = ent->val - current_line;
11872 int line_delta = line_offset - DWARF_LINE_BASE;
11873
11874 current_line = ent->val;
11875 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11876 {
11877 /* This can handle deltas from -10 to 234, using the current
11878 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11879 This takes 1 byte. */
11880 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11881 "line %u", current_line);
11882 }
11883 else
11884 {
11885 /* This can handle any delta. This takes at least 4 bytes,
11886 depending on the value being encoded. */
11887 dw2_asm_output_data (1, DW_LNS_advance_line,
11888 "advance to line %u", current_line);
11889 dw2_asm_output_data_sleb128 (line_offset, NULL);
11890 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11891 }
11892 }
11893 break;
11894
11895 case LI_set_file:
11896 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11897 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11898 break;
11899
11900 case LI_set_column:
11901 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11902 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11903 break;
11904
11905 case LI_negate_stmt:
11906 current_is_stmt = !current_is_stmt;
11907 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11908 "is_stmt %d", current_is_stmt);
11909 break;
11910
11911 case LI_set_prologue_end:
11912 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11913 "set prologue end");
11914 break;
11915
11916 case LI_set_epilogue_begin:
11917 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11918 "set epilogue begin");
11919 break;
11920
11921 case LI_set_discriminator:
11922 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11923 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11924 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11925 dw2_asm_output_data_uleb128 (ent->val, NULL);
11926 break;
11927 }
11928 }
11929
11930 /* Emit debug info for the address of the end of the table. */
11931 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11932 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11933 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11934 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11935
11936 dw2_asm_output_data (1, 0, "end sequence");
11937 dw2_asm_output_data_uleb128 (1, NULL);
11938 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11939 }
11940
11941 /* Output the source line number correspondence information. This
11942 information goes into the .debug_line section. */
11943
11944 static void
11945 output_line_info (bool prologue_only)
11946 {
11947 static unsigned int generation;
11948 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11949 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11950 bool saw_one = false;
11951 int opc;
11952
11953 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11954 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11955 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11956 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11957
11958 if (!XCOFF_DEBUGGING_INFO)
11959 {
11960 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11961 dw2_asm_output_data (4, 0xffffffff,
11962 "Initial length escape value indicating 64-bit DWARF extension");
11963 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11964 "Length of Source Line Info");
11965 }
11966
11967 ASM_OUTPUT_LABEL (asm_out_file, l1);
11968
11969 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11970 if (dwarf_version >= 5)
11971 {
11972 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11973 dw2_asm_output_data (1, 0, "Segment Size");
11974 }
11975 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11976 ASM_OUTPUT_LABEL (asm_out_file, p1);
11977
11978 /* Define the architecture-dependent minimum instruction length (in bytes).
11979 In this implementation of DWARF, this field is used for information
11980 purposes only. Since GCC generates assembly language, we have no
11981 a priori knowledge of how many instruction bytes are generated for each
11982 source line, and therefore can use only the DW_LNE_set_address and
11983 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11984 this as '1', which is "correct enough" for all architectures,
11985 and don't let the target override. */
11986 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11987
11988 if (dwarf_version >= 4)
11989 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11990 "Maximum Operations Per Instruction");
11991 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11992 "Default is_stmt_start flag");
11993 dw2_asm_output_data (1, DWARF_LINE_BASE,
11994 "Line Base Value (Special Opcodes)");
11995 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11996 "Line Range Value (Special Opcodes)");
11997 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11998 "Special Opcode Base");
11999
12000 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12001 {
12002 int n_op_args;
12003 switch (opc)
12004 {
12005 case DW_LNS_advance_pc:
12006 case DW_LNS_advance_line:
12007 case DW_LNS_set_file:
12008 case DW_LNS_set_column:
12009 case DW_LNS_fixed_advance_pc:
12010 case DW_LNS_set_isa:
12011 n_op_args = 1;
12012 break;
12013 default:
12014 n_op_args = 0;
12015 break;
12016 }
12017
12018 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12019 opc, n_op_args);
12020 }
12021
12022 /* Write out the information about the files we use. */
12023 output_file_names ();
12024 ASM_OUTPUT_LABEL (asm_out_file, p2);
12025 if (prologue_only)
12026 {
12027 /* Output the marker for the end of the line number info. */
12028 ASM_OUTPUT_LABEL (asm_out_file, l2);
12029 return;
12030 }
12031
12032 if (separate_line_info)
12033 {
12034 dw_line_info_table *table;
12035 size_t i;
12036
12037 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12038 if (table->in_use)
12039 {
12040 output_one_line_info_table (table);
12041 saw_one = true;
12042 }
12043 }
12044 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12045 {
12046 output_one_line_info_table (cold_text_section_line_info);
12047 saw_one = true;
12048 }
12049
12050 /* ??? Some Darwin linkers crash on a .debug_line section with no
12051 sequences. Further, merely a DW_LNE_end_sequence entry is not
12052 sufficient -- the address column must also be initialized.
12053 Make sure to output at least one set_address/end_sequence pair,
12054 choosing .text since that section is always present. */
12055 if (text_section_line_info->in_use || !saw_one)
12056 output_one_line_info_table (text_section_line_info);
12057
12058 /* Output the marker for the end of the line number info. */
12059 ASM_OUTPUT_LABEL (asm_out_file, l2);
12060 }
12061 \f
12062 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12063
12064 static inline bool
12065 need_endianity_attribute_p (bool reverse)
12066 {
12067 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12068 }
12069
12070 /* Given a pointer to a tree node for some base type, return a pointer to
12071 a DIE that describes the given type. REVERSE is true if the type is
12072 to be interpreted in the reverse storage order wrt the target order.
12073
12074 This routine must only be called for GCC type nodes that correspond to
12075 Dwarf base (fundamental) types. */
12076
12077 static dw_die_ref
12078 base_type_die (tree type, bool reverse)
12079 {
12080 dw_die_ref base_type_result;
12081 enum dwarf_type encoding;
12082 bool fpt_used = false;
12083 struct fixed_point_type_info fpt_info;
12084 tree type_bias = NULL_TREE;
12085
12086 /* If this is a subtype that should not be emitted as a subrange type,
12087 use the base type. See subrange_type_for_debug_p. */
12088 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12089 type = TREE_TYPE (type);
12090
12091 switch (TREE_CODE (type))
12092 {
12093 case INTEGER_TYPE:
12094 if ((dwarf_version >= 4 || !dwarf_strict)
12095 && TYPE_NAME (type)
12096 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12097 && DECL_IS_BUILTIN (TYPE_NAME (type))
12098 && DECL_NAME (TYPE_NAME (type)))
12099 {
12100 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12101 if (strcmp (name, "char16_t") == 0
12102 || strcmp (name, "char32_t") == 0)
12103 {
12104 encoding = DW_ATE_UTF;
12105 break;
12106 }
12107 }
12108 if ((dwarf_version >= 3 || !dwarf_strict)
12109 && lang_hooks.types.get_fixed_point_type_info)
12110 {
12111 memset (&fpt_info, 0, sizeof (fpt_info));
12112 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12113 {
12114 fpt_used = true;
12115 encoding = ((TYPE_UNSIGNED (type))
12116 ? DW_ATE_unsigned_fixed
12117 : DW_ATE_signed_fixed);
12118 break;
12119 }
12120 }
12121 if (TYPE_STRING_FLAG (type))
12122 {
12123 if (TYPE_UNSIGNED (type))
12124 encoding = DW_ATE_unsigned_char;
12125 else
12126 encoding = DW_ATE_signed_char;
12127 }
12128 else if (TYPE_UNSIGNED (type))
12129 encoding = DW_ATE_unsigned;
12130 else
12131 encoding = DW_ATE_signed;
12132
12133 if (!dwarf_strict
12134 && lang_hooks.types.get_type_bias)
12135 type_bias = lang_hooks.types.get_type_bias (type);
12136 break;
12137
12138 case REAL_TYPE:
12139 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12140 {
12141 if (dwarf_version >= 3 || !dwarf_strict)
12142 encoding = DW_ATE_decimal_float;
12143 else
12144 encoding = DW_ATE_lo_user;
12145 }
12146 else
12147 encoding = DW_ATE_float;
12148 break;
12149
12150 case FIXED_POINT_TYPE:
12151 if (!(dwarf_version >= 3 || !dwarf_strict))
12152 encoding = DW_ATE_lo_user;
12153 else if (TYPE_UNSIGNED (type))
12154 encoding = DW_ATE_unsigned_fixed;
12155 else
12156 encoding = DW_ATE_signed_fixed;
12157 break;
12158
12159 /* Dwarf2 doesn't know anything about complex ints, so use
12160 a user defined type for it. */
12161 case COMPLEX_TYPE:
12162 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12163 encoding = DW_ATE_complex_float;
12164 else
12165 encoding = DW_ATE_lo_user;
12166 break;
12167
12168 case BOOLEAN_TYPE:
12169 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12170 encoding = DW_ATE_boolean;
12171 break;
12172
12173 default:
12174 /* No other TREE_CODEs are Dwarf fundamental types. */
12175 gcc_unreachable ();
12176 }
12177
12178 base_type_result = new_die_raw (DW_TAG_base_type);
12179
12180 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12181 int_size_in_bytes (type));
12182 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12183
12184 if (need_endianity_attribute_p (reverse))
12185 add_AT_unsigned (base_type_result, DW_AT_endianity,
12186 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12187
12188 add_alignment_attribute (base_type_result, type);
12189
12190 if (fpt_used)
12191 {
12192 switch (fpt_info.scale_factor_kind)
12193 {
12194 case fixed_point_scale_factor_binary:
12195 add_AT_int (base_type_result, DW_AT_binary_scale,
12196 fpt_info.scale_factor.binary);
12197 break;
12198
12199 case fixed_point_scale_factor_decimal:
12200 add_AT_int (base_type_result, DW_AT_decimal_scale,
12201 fpt_info.scale_factor.decimal);
12202 break;
12203
12204 case fixed_point_scale_factor_arbitrary:
12205 /* Arbitrary scale factors cannot be described in standard DWARF,
12206 yet. */
12207 if (!dwarf_strict)
12208 {
12209 /* Describe the scale factor as a rational constant. */
12210 const dw_die_ref scale_factor
12211 = new_die (DW_TAG_constant, comp_unit_die (), type);
12212
12213 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12214 fpt_info.scale_factor.arbitrary.numerator);
12215 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12216 fpt_info.scale_factor.arbitrary.denominator);
12217
12218 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12219 }
12220 break;
12221
12222 default:
12223 gcc_unreachable ();
12224 }
12225 }
12226
12227 if (type_bias)
12228 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12229 dw_scalar_form_constant
12230 | dw_scalar_form_exprloc
12231 | dw_scalar_form_reference,
12232 NULL);
12233
12234 return base_type_result;
12235 }
12236
12237 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12238 named 'auto' in its type: return true for it, false otherwise. */
12239
12240 static inline bool
12241 is_cxx_auto (tree type)
12242 {
12243 if (is_cxx ())
12244 {
12245 tree name = TYPE_IDENTIFIER (type);
12246 if (name == get_identifier ("auto")
12247 || name == get_identifier ("decltype(auto)"))
12248 return true;
12249 }
12250 return false;
12251 }
12252
12253 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12254 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12255
12256 static inline int
12257 is_base_type (tree type)
12258 {
12259 switch (TREE_CODE (type))
12260 {
12261 case INTEGER_TYPE:
12262 case REAL_TYPE:
12263 case FIXED_POINT_TYPE:
12264 case COMPLEX_TYPE:
12265 case BOOLEAN_TYPE:
12266 case POINTER_BOUNDS_TYPE:
12267 return 1;
12268
12269 case VOID_TYPE:
12270 case ARRAY_TYPE:
12271 case RECORD_TYPE:
12272 case UNION_TYPE:
12273 case QUAL_UNION_TYPE:
12274 case ENUMERAL_TYPE:
12275 case FUNCTION_TYPE:
12276 case METHOD_TYPE:
12277 case POINTER_TYPE:
12278 case REFERENCE_TYPE:
12279 case NULLPTR_TYPE:
12280 case OFFSET_TYPE:
12281 case LANG_TYPE:
12282 case VECTOR_TYPE:
12283 return 0;
12284
12285 default:
12286 if (is_cxx_auto (type))
12287 return 0;
12288 gcc_unreachable ();
12289 }
12290
12291 return 0;
12292 }
12293
12294 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12295 node, return the size in bits for the type if it is a constant, or else
12296 return the alignment for the type if the type's size is not constant, or
12297 else return BITS_PER_WORD if the type actually turns out to be an
12298 ERROR_MARK node. */
12299
12300 static inline unsigned HOST_WIDE_INT
12301 simple_type_size_in_bits (const_tree type)
12302 {
12303 if (TREE_CODE (type) == ERROR_MARK)
12304 return BITS_PER_WORD;
12305 else if (TYPE_SIZE (type) == NULL_TREE)
12306 return 0;
12307 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12308 return tree_to_uhwi (TYPE_SIZE (type));
12309 else
12310 return TYPE_ALIGN (type);
12311 }
12312
12313 /* Similarly, but return an offset_int instead of UHWI. */
12314
12315 static inline offset_int
12316 offset_int_type_size_in_bits (const_tree type)
12317 {
12318 if (TREE_CODE (type) == ERROR_MARK)
12319 return BITS_PER_WORD;
12320 else if (TYPE_SIZE (type) == NULL_TREE)
12321 return 0;
12322 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12323 return wi::to_offset (TYPE_SIZE (type));
12324 else
12325 return TYPE_ALIGN (type);
12326 }
12327
12328 /* Given a pointer to a tree node for a subrange type, return a pointer
12329 to a DIE that describes the given type. */
12330
12331 static dw_die_ref
12332 subrange_type_die (tree type, tree low, tree high, tree bias,
12333 dw_die_ref context_die)
12334 {
12335 dw_die_ref subrange_die;
12336 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12337
12338 if (context_die == NULL)
12339 context_die = comp_unit_die ();
12340
12341 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12342
12343 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12344 {
12345 /* The size of the subrange type and its base type do not match,
12346 so we need to generate a size attribute for the subrange type. */
12347 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12348 }
12349
12350 add_alignment_attribute (subrange_die, type);
12351
12352 if (low)
12353 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12354 if (high)
12355 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12356 if (bias && !dwarf_strict)
12357 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12358 dw_scalar_form_constant
12359 | dw_scalar_form_exprloc
12360 | dw_scalar_form_reference,
12361 NULL);
12362
12363 return subrange_die;
12364 }
12365
12366 /* Returns the (const and/or volatile) cv_qualifiers associated with
12367 the decl node. This will normally be augmented with the
12368 cv_qualifiers of the underlying type in add_type_attribute. */
12369
12370 static int
12371 decl_quals (const_tree decl)
12372 {
12373 return ((TREE_READONLY (decl)
12374 /* The C++ front-end correctly marks reference-typed
12375 variables as readonly, but from a language (and debug
12376 info) standpoint they are not const-qualified. */
12377 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12378 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12379 | (TREE_THIS_VOLATILE (decl)
12380 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12381 }
12382
12383 /* Determine the TYPE whose qualifiers match the largest strict subset
12384 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12385 qualifiers outside QUAL_MASK. */
12386
12387 static int
12388 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12389 {
12390 tree t;
12391 int best_rank = 0, best_qual = 0, max_rank;
12392
12393 type_quals &= qual_mask;
12394 max_rank = popcount_hwi (type_quals) - 1;
12395
12396 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12397 t = TYPE_NEXT_VARIANT (t))
12398 {
12399 int q = TYPE_QUALS (t) & qual_mask;
12400
12401 if ((q & type_quals) == q && q != type_quals
12402 && check_base_type (t, type))
12403 {
12404 int rank = popcount_hwi (q);
12405
12406 if (rank > best_rank)
12407 {
12408 best_rank = rank;
12409 best_qual = q;
12410 }
12411 }
12412 }
12413
12414 return best_qual;
12415 }
12416
12417 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12418 static const dwarf_qual_info_t dwarf_qual_info[] =
12419 {
12420 { TYPE_QUAL_CONST, DW_TAG_const_type },
12421 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12422 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12423 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12424 };
12425 static const unsigned int dwarf_qual_info_size
12426 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12427
12428 /* If DIE is a qualified DIE of some base DIE with the same parent,
12429 return the base DIE, otherwise return NULL. Set MASK to the
12430 qualifiers added compared to the returned DIE. */
12431
12432 static dw_die_ref
12433 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12434 {
12435 unsigned int i;
12436 for (i = 0; i < dwarf_qual_info_size; i++)
12437 if (die->die_tag == dwarf_qual_info[i].t)
12438 break;
12439 if (i == dwarf_qual_info_size)
12440 return NULL;
12441 if (vec_safe_length (die->die_attr) != 1)
12442 return NULL;
12443 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12444 if (type == NULL || type->die_parent != die->die_parent)
12445 return NULL;
12446 *mask |= dwarf_qual_info[i].q;
12447 if (depth)
12448 {
12449 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12450 if (ret)
12451 return ret;
12452 }
12453 return type;
12454 }
12455
12456 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12457 entry that chains the modifiers specified by CV_QUALS in front of the
12458 given type. REVERSE is true if the type is to be interpreted in the
12459 reverse storage order wrt the target order. */
12460
12461 static dw_die_ref
12462 modified_type_die (tree type, int cv_quals, bool reverse,
12463 dw_die_ref context_die)
12464 {
12465 enum tree_code code = TREE_CODE (type);
12466 dw_die_ref mod_type_die;
12467 dw_die_ref sub_die = NULL;
12468 tree item_type = NULL;
12469 tree qualified_type;
12470 tree name, low, high;
12471 dw_die_ref mod_scope;
12472 /* Only these cv-qualifiers are currently handled. */
12473 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12474 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12475 ENCODE_QUAL_ADDR_SPACE(~0U));
12476 const bool reverse_base_type
12477 = need_endianity_attribute_p (reverse) && is_base_type (type);
12478
12479 if (code == ERROR_MARK)
12480 return NULL;
12481
12482 if (lang_hooks.types.get_debug_type)
12483 {
12484 tree debug_type = lang_hooks.types.get_debug_type (type);
12485
12486 if (debug_type != NULL_TREE && debug_type != type)
12487 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12488 }
12489
12490 cv_quals &= cv_qual_mask;
12491
12492 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12493 tag modifier (and not an attribute) old consumers won't be able
12494 to handle it. */
12495 if (dwarf_version < 3)
12496 cv_quals &= ~TYPE_QUAL_RESTRICT;
12497
12498 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12499 if (dwarf_version < 5)
12500 cv_quals &= ~TYPE_QUAL_ATOMIC;
12501
12502 /* See if we already have the appropriately qualified variant of
12503 this type. */
12504 qualified_type = get_qualified_type (type, cv_quals);
12505
12506 if (qualified_type == sizetype)
12507 {
12508 /* Try not to expose the internal sizetype type's name. */
12509 if (TYPE_NAME (qualified_type)
12510 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12511 {
12512 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12513
12514 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12515 && (TYPE_PRECISION (t)
12516 == TYPE_PRECISION (qualified_type))
12517 && (TYPE_UNSIGNED (t)
12518 == TYPE_UNSIGNED (qualified_type)));
12519 qualified_type = t;
12520 }
12521 else if (qualified_type == sizetype
12522 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12523 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12524 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12525 qualified_type = size_type_node;
12526 }
12527
12528 /* If we do, then we can just use its DIE, if it exists. */
12529 if (qualified_type)
12530 {
12531 mod_type_die = lookup_type_die (qualified_type);
12532
12533 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12534 dealt with specially: the DIE with the attribute, if it exists, is
12535 placed immediately after the regular DIE for the same base type. */
12536 if (mod_type_die
12537 && (!reverse_base_type
12538 || ((mod_type_die = mod_type_die->die_sib) != NULL
12539 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12540 return mod_type_die;
12541 }
12542
12543 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12544
12545 /* Handle C typedef types. */
12546 if (name
12547 && TREE_CODE (name) == TYPE_DECL
12548 && DECL_ORIGINAL_TYPE (name)
12549 && !DECL_ARTIFICIAL (name))
12550 {
12551 tree dtype = TREE_TYPE (name);
12552
12553 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12554 if (qualified_type == dtype && !reverse_base_type)
12555 {
12556 tree origin = decl_ultimate_origin (name);
12557
12558 /* Typedef variants that have an abstract origin don't get their own
12559 type DIE (see gen_typedef_die), so fall back on the ultimate
12560 abstract origin instead. */
12561 if (origin != NULL && origin != name)
12562 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12563 context_die);
12564
12565 /* For a named type, use the typedef. */
12566 gen_type_die (qualified_type, context_die);
12567 return lookup_type_die (qualified_type);
12568 }
12569 else
12570 {
12571 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12572 dquals &= cv_qual_mask;
12573 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12574 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12575 /* cv-unqualified version of named type. Just use
12576 the unnamed type to which it refers. */
12577 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12578 reverse, context_die);
12579 /* Else cv-qualified version of named type; fall through. */
12580 }
12581 }
12582
12583 mod_scope = scope_die_for (type, context_die);
12584
12585 if (cv_quals)
12586 {
12587 int sub_quals = 0, first_quals = 0;
12588 unsigned i;
12589 dw_die_ref first = NULL, last = NULL;
12590
12591 /* Determine a lesser qualified type that most closely matches
12592 this one. Then generate DW_TAG_* entries for the remaining
12593 qualifiers. */
12594 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12595 cv_qual_mask);
12596 if (sub_quals && use_debug_types)
12597 {
12598 bool needed = false;
12599 /* If emitting type units, make sure the order of qualifiers
12600 is canonical. Thus, start from unqualified type if
12601 an earlier qualifier is missing in sub_quals, but some later
12602 one is present there. */
12603 for (i = 0; i < dwarf_qual_info_size; i++)
12604 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12605 needed = true;
12606 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12607 {
12608 sub_quals = 0;
12609 break;
12610 }
12611 }
12612 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12613 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12614 {
12615 /* As not all intermediate qualified DIEs have corresponding
12616 tree types, ensure that qualified DIEs in the same scope
12617 as their DW_AT_type are emitted after their DW_AT_type,
12618 only with other qualified DIEs for the same type possibly
12619 in between them. Determine the range of such qualified
12620 DIEs now (first being the base type, last being corresponding
12621 last qualified DIE for it). */
12622 unsigned int count = 0;
12623 first = qualified_die_p (mod_type_die, &first_quals,
12624 dwarf_qual_info_size);
12625 if (first == NULL)
12626 first = mod_type_die;
12627 gcc_assert ((first_quals & ~sub_quals) == 0);
12628 for (count = 0, last = first;
12629 count < (1U << dwarf_qual_info_size);
12630 count++, last = last->die_sib)
12631 {
12632 int quals = 0;
12633 if (last == mod_scope->die_child)
12634 break;
12635 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12636 != first)
12637 break;
12638 }
12639 }
12640
12641 for (i = 0; i < dwarf_qual_info_size; i++)
12642 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12643 {
12644 dw_die_ref d;
12645 if (first && first != last)
12646 {
12647 for (d = first->die_sib; ; d = d->die_sib)
12648 {
12649 int quals = 0;
12650 qualified_die_p (d, &quals, dwarf_qual_info_size);
12651 if (quals == (first_quals | dwarf_qual_info[i].q))
12652 break;
12653 if (d == last)
12654 {
12655 d = NULL;
12656 break;
12657 }
12658 }
12659 if (d)
12660 {
12661 mod_type_die = d;
12662 continue;
12663 }
12664 }
12665 if (first)
12666 {
12667 d = new_die_raw (dwarf_qual_info[i].t);
12668 add_child_die_after (mod_scope, d, last);
12669 last = d;
12670 }
12671 else
12672 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12673 if (mod_type_die)
12674 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12675 mod_type_die = d;
12676 first_quals |= dwarf_qual_info[i].q;
12677 }
12678 }
12679 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12680 {
12681 dwarf_tag tag = DW_TAG_pointer_type;
12682 if (code == REFERENCE_TYPE)
12683 {
12684 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12685 tag = DW_TAG_rvalue_reference_type;
12686 else
12687 tag = DW_TAG_reference_type;
12688 }
12689 mod_type_die = new_die (tag, mod_scope, type);
12690
12691 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12692 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12693 add_alignment_attribute (mod_type_die, type);
12694 item_type = TREE_TYPE (type);
12695
12696 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12697 if (!ADDR_SPACE_GENERIC_P (as))
12698 {
12699 int action = targetm.addr_space.debug (as);
12700 if (action >= 0)
12701 {
12702 /* Positive values indicate an address_class. */
12703 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12704 }
12705 else
12706 {
12707 /* Negative values indicate an (inverted) segment base reg. */
12708 dw_loc_descr_ref d
12709 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12710 add_AT_loc (mod_type_die, DW_AT_segment, d);
12711 }
12712 }
12713 }
12714 else if (code == INTEGER_TYPE
12715 && TREE_TYPE (type) != NULL_TREE
12716 && subrange_type_for_debug_p (type, &low, &high))
12717 {
12718 tree bias = NULL_TREE;
12719 if (lang_hooks.types.get_type_bias)
12720 bias = lang_hooks.types.get_type_bias (type);
12721 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12722 item_type = TREE_TYPE (type);
12723 }
12724 else if (is_base_type (type))
12725 {
12726 mod_type_die = base_type_die (type, reverse);
12727
12728 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12729 if (reverse_base_type)
12730 {
12731 dw_die_ref after_die
12732 = modified_type_die (type, cv_quals, false, context_die);
12733 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12734 }
12735 else
12736 add_child_die (comp_unit_die (), mod_type_die);
12737
12738 add_pubtype (type, mod_type_die);
12739 }
12740 else
12741 {
12742 gen_type_die (type, context_die);
12743
12744 /* We have to get the type_main_variant here (and pass that to the
12745 `lookup_type_die' routine) because the ..._TYPE node we have
12746 might simply be a *copy* of some original type node (where the
12747 copy was created to help us keep track of typedef names) and
12748 that copy might have a different TYPE_UID from the original
12749 ..._TYPE node. */
12750 if (TREE_CODE (type) == FUNCTION_TYPE
12751 || TREE_CODE (type) == METHOD_TYPE)
12752 {
12753 /* For function/method types, can't just use type_main_variant here,
12754 because that can have different ref-qualifiers for C++,
12755 but try to canonicalize. */
12756 tree main = TYPE_MAIN_VARIANT (type);
12757 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12758 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12759 && check_base_type (t, main)
12760 && check_lang_type (t, type))
12761 return lookup_type_die (t);
12762 return lookup_type_die (type);
12763 }
12764 else if (TREE_CODE (type) != VECTOR_TYPE
12765 && TREE_CODE (type) != ARRAY_TYPE)
12766 return lookup_type_die (type_main_variant (type));
12767 else
12768 /* Vectors have the debugging information in the type,
12769 not the main variant. */
12770 return lookup_type_die (type);
12771 }
12772
12773 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12774 don't output a DW_TAG_typedef, since there isn't one in the
12775 user's program; just attach a DW_AT_name to the type.
12776 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12777 if the base type already has the same name. */
12778 if (name
12779 && ((TREE_CODE (name) != TYPE_DECL
12780 && (qualified_type == TYPE_MAIN_VARIANT (type)
12781 || (cv_quals == TYPE_UNQUALIFIED)))
12782 || (TREE_CODE (name) == TYPE_DECL
12783 && TREE_TYPE (name) == qualified_type
12784 && DECL_NAME (name))))
12785 {
12786 if (TREE_CODE (name) == TYPE_DECL)
12787 /* Could just call add_name_and_src_coords_attributes here,
12788 but since this is a builtin type it doesn't have any
12789 useful source coordinates anyway. */
12790 name = DECL_NAME (name);
12791 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12792 }
12793 /* This probably indicates a bug. */
12794 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12795 {
12796 name = TYPE_IDENTIFIER (type);
12797 add_name_attribute (mod_type_die,
12798 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12799 }
12800
12801 if (qualified_type && !reverse_base_type)
12802 equate_type_number_to_die (qualified_type, mod_type_die);
12803
12804 if (item_type)
12805 /* We must do this after the equate_type_number_to_die call, in case
12806 this is a recursive type. This ensures that the modified_type_die
12807 recursion will terminate even if the type is recursive. Recursive
12808 types are possible in Ada. */
12809 sub_die = modified_type_die (item_type,
12810 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12811 reverse,
12812 context_die);
12813
12814 if (sub_die != NULL)
12815 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12816
12817 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12818 if (TYPE_ARTIFICIAL (type))
12819 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12820
12821 return mod_type_die;
12822 }
12823
12824 /* Generate DIEs for the generic parameters of T.
12825 T must be either a generic type or a generic function.
12826 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12827
12828 static void
12829 gen_generic_params_dies (tree t)
12830 {
12831 tree parms, args;
12832 int parms_num, i;
12833 dw_die_ref die = NULL;
12834 int non_default;
12835
12836 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12837 return;
12838
12839 if (TYPE_P (t))
12840 die = lookup_type_die (t);
12841 else if (DECL_P (t))
12842 die = lookup_decl_die (t);
12843
12844 gcc_assert (die);
12845
12846 parms = lang_hooks.get_innermost_generic_parms (t);
12847 if (!parms)
12848 /* T has no generic parameter. It means T is neither a generic type
12849 or function. End of story. */
12850 return;
12851
12852 parms_num = TREE_VEC_LENGTH (parms);
12853 args = lang_hooks.get_innermost_generic_args (t);
12854 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12855 non_default = int_cst_value (TREE_CHAIN (args));
12856 else
12857 non_default = TREE_VEC_LENGTH (args);
12858 for (i = 0; i < parms_num; i++)
12859 {
12860 tree parm, arg, arg_pack_elems;
12861 dw_die_ref parm_die;
12862
12863 parm = TREE_VEC_ELT (parms, i);
12864 arg = TREE_VEC_ELT (args, i);
12865 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12866 gcc_assert (parm && TREE_VALUE (parm) && arg);
12867
12868 if (parm && TREE_VALUE (parm) && arg)
12869 {
12870 /* If PARM represents a template parameter pack,
12871 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12872 by DW_TAG_template_*_parameter DIEs for the argument
12873 pack elements of ARG. Note that ARG would then be
12874 an argument pack. */
12875 if (arg_pack_elems)
12876 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12877 arg_pack_elems,
12878 die);
12879 else
12880 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12881 true /* emit name */, die);
12882 if (i >= non_default)
12883 add_AT_flag (parm_die, DW_AT_default_value, 1);
12884 }
12885 }
12886 }
12887
12888 /* Create and return a DIE for PARM which should be
12889 the representation of a generic type parameter.
12890 For instance, in the C++ front end, PARM would be a template parameter.
12891 ARG is the argument to PARM.
12892 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12893 name of the PARM.
12894 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12895 as a child node. */
12896
12897 static dw_die_ref
12898 generic_parameter_die (tree parm, tree arg,
12899 bool emit_name_p,
12900 dw_die_ref parent_die)
12901 {
12902 dw_die_ref tmpl_die = NULL;
12903 const char *name = NULL;
12904
12905 if (!parm || !DECL_NAME (parm) || !arg)
12906 return NULL;
12907
12908 /* We support non-type generic parameters and arguments,
12909 type generic parameters and arguments, as well as
12910 generic generic parameters (a.k.a. template template parameters in C++)
12911 and arguments. */
12912 if (TREE_CODE (parm) == PARM_DECL)
12913 /* PARM is a nontype generic parameter */
12914 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12915 else if (TREE_CODE (parm) == TYPE_DECL)
12916 /* PARM is a type generic parameter. */
12917 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12918 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12919 /* PARM is a generic generic parameter.
12920 Its DIE is a GNU extension. It shall have a
12921 DW_AT_name attribute to represent the name of the template template
12922 parameter, and a DW_AT_GNU_template_name attribute to represent the
12923 name of the template template argument. */
12924 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12925 parent_die, parm);
12926 else
12927 gcc_unreachable ();
12928
12929 if (tmpl_die)
12930 {
12931 tree tmpl_type;
12932
12933 /* If PARM is a generic parameter pack, it means we are
12934 emitting debug info for a template argument pack element.
12935 In other terms, ARG is a template argument pack element.
12936 In that case, we don't emit any DW_AT_name attribute for
12937 the die. */
12938 if (emit_name_p)
12939 {
12940 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12941 gcc_assert (name);
12942 add_AT_string (tmpl_die, DW_AT_name, name);
12943 }
12944
12945 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12946 {
12947 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12948 TMPL_DIE should have a child DW_AT_type attribute that is set
12949 to the type of the argument to PARM, which is ARG.
12950 If PARM is a type generic parameter, TMPL_DIE should have a
12951 child DW_AT_type that is set to ARG. */
12952 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12953 add_type_attribute (tmpl_die, tmpl_type,
12954 (TREE_THIS_VOLATILE (tmpl_type)
12955 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12956 false, parent_die);
12957 }
12958 else
12959 {
12960 /* So TMPL_DIE is a DIE representing a
12961 a generic generic template parameter, a.k.a template template
12962 parameter in C++ and arg is a template. */
12963
12964 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12965 to the name of the argument. */
12966 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12967 if (name)
12968 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12969 }
12970
12971 if (TREE_CODE (parm) == PARM_DECL)
12972 /* So PARM is a non-type generic parameter.
12973 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12974 attribute of TMPL_DIE which value represents the value
12975 of ARG.
12976 We must be careful here:
12977 The value of ARG might reference some function decls.
12978 We might currently be emitting debug info for a generic
12979 type and types are emitted before function decls, we don't
12980 know if the function decls referenced by ARG will actually be
12981 emitted after cgraph computations.
12982 So must defer the generation of the DW_AT_const_value to
12983 after cgraph is ready. */
12984 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12985 }
12986
12987 return tmpl_die;
12988 }
12989
12990 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12991 PARM_PACK must be a template parameter pack. The returned DIE
12992 will be child DIE of PARENT_DIE. */
12993
12994 static dw_die_ref
12995 template_parameter_pack_die (tree parm_pack,
12996 tree parm_pack_args,
12997 dw_die_ref parent_die)
12998 {
12999 dw_die_ref die;
13000 int j;
13001
13002 gcc_assert (parent_die && parm_pack);
13003
13004 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13005 add_name_and_src_coords_attributes (die, parm_pack);
13006 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13007 generic_parameter_die (parm_pack,
13008 TREE_VEC_ELT (parm_pack_args, j),
13009 false /* Don't emit DW_AT_name */,
13010 die);
13011 return die;
13012 }
13013
13014 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13015 an enumerated type. */
13016
13017 static inline int
13018 type_is_enum (const_tree type)
13019 {
13020 return TREE_CODE (type) == ENUMERAL_TYPE;
13021 }
13022
13023 /* Return the DBX register number described by a given RTL node. */
13024
13025 static unsigned int
13026 dbx_reg_number (const_rtx rtl)
13027 {
13028 unsigned regno = REGNO (rtl);
13029
13030 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13031
13032 #ifdef LEAF_REG_REMAP
13033 if (crtl->uses_only_leaf_regs)
13034 {
13035 int leaf_reg = LEAF_REG_REMAP (regno);
13036 if (leaf_reg != -1)
13037 regno = (unsigned) leaf_reg;
13038 }
13039 #endif
13040
13041 regno = DBX_REGISTER_NUMBER (regno);
13042 gcc_assert (regno != INVALID_REGNUM);
13043 return regno;
13044 }
13045
13046 /* Optionally add a DW_OP_piece term to a location description expression.
13047 DW_OP_piece is only added if the location description expression already
13048 doesn't end with DW_OP_piece. */
13049
13050 static void
13051 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13052 {
13053 dw_loc_descr_ref loc;
13054
13055 if (*list_head != NULL)
13056 {
13057 /* Find the end of the chain. */
13058 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13059 ;
13060
13061 if (loc->dw_loc_opc != DW_OP_piece)
13062 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13063 }
13064 }
13065
13066 /* Return a location descriptor that designates a machine register or
13067 zero if there is none. */
13068
13069 static dw_loc_descr_ref
13070 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13071 {
13072 rtx regs;
13073
13074 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13075 return 0;
13076
13077 /* We only use "frame base" when we're sure we're talking about the
13078 post-prologue local stack frame. We do this by *not* running
13079 register elimination until this point, and recognizing the special
13080 argument pointer and soft frame pointer rtx's.
13081 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13082 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13083 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13084 {
13085 dw_loc_descr_ref result = NULL;
13086
13087 if (dwarf_version >= 4 || !dwarf_strict)
13088 {
13089 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13090 initialized);
13091 if (result)
13092 add_loc_descr (&result,
13093 new_loc_descr (DW_OP_stack_value, 0, 0));
13094 }
13095 return result;
13096 }
13097
13098 regs = targetm.dwarf_register_span (rtl);
13099
13100 if (REG_NREGS (rtl) > 1 || regs)
13101 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13102 else
13103 {
13104 unsigned int dbx_regnum = dbx_reg_number (rtl);
13105 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13106 return 0;
13107 return one_reg_loc_descriptor (dbx_regnum, initialized);
13108 }
13109 }
13110
13111 /* Return a location descriptor that designates a machine register for
13112 a given hard register number. */
13113
13114 static dw_loc_descr_ref
13115 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13116 {
13117 dw_loc_descr_ref reg_loc_descr;
13118
13119 if (regno <= 31)
13120 reg_loc_descr
13121 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13122 else
13123 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13124
13125 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13126 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13127
13128 return reg_loc_descr;
13129 }
13130
13131 /* Given an RTL of a register, return a location descriptor that
13132 designates a value that spans more than one register. */
13133
13134 static dw_loc_descr_ref
13135 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13136 enum var_init_status initialized)
13137 {
13138 int size, i;
13139 dw_loc_descr_ref loc_result = NULL;
13140
13141 /* Simple, contiguous registers. */
13142 if (regs == NULL_RTX)
13143 {
13144 unsigned reg = REGNO (rtl);
13145 int nregs;
13146
13147 #ifdef LEAF_REG_REMAP
13148 if (crtl->uses_only_leaf_regs)
13149 {
13150 int leaf_reg = LEAF_REG_REMAP (reg);
13151 if (leaf_reg != -1)
13152 reg = (unsigned) leaf_reg;
13153 }
13154 #endif
13155
13156 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13157 nregs = REG_NREGS (rtl);
13158
13159 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13160
13161 loc_result = NULL;
13162 while (nregs--)
13163 {
13164 dw_loc_descr_ref t;
13165
13166 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13167 VAR_INIT_STATUS_INITIALIZED);
13168 add_loc_descr (&loc_result, t);
13169 add_loc_descr_op_piece (&loc_result, size);
13170 ++reg;
13171 }
13172 return loc_result;
13173 }
13174
13175 /* Now onto stupid register sets in non contiguous locations. */
13176
13177 gcc_assert (GET_CODE (regs) == PARALLEL);
13178
13179 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13180 loc_result = NULL;
13181
13182 for (i = 0; i < XVECLEN (regs, 0); ++i)
13183 {
13184 dw_loc_descr_ref t;
13185
13186 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13187 VAR_INIT_STATUS_INITIALIZED);
13188 add_loc_descr (&loc_result, t);
13189 add_loc_descr_op_piece (&loc_result, size);
13190 }
13191
13192 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13193 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13194 return loc_result;
13195 }
13196
13197 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13198
13199 /* Return a location descriptor that designates a constant i,
13200 as a compound operation from constant (i >> shift), constant shift
13201 and DW_OP_shl. */
13202
13203 static dw_loc_descr_ref
13204 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13205 {
13206 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13207 add_loc_descr (&ret, int_loc_descriptor (shift));
13208 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13209 return ret;
13210 }
13211
13212 /* Return a location descriptor that designates a constant. */
13213
13214 static dw_loc_descr_ref
13215 int_loc_descriptor (HOST_WIDE_INT i)
13216 {
13217 enum dwarf_location_atom op;
13218
13219 /* Pick the smallest representation of a constant, rather than just
13220 defaulting to the LEB encoding. */
13221 if (i >= 0)
13222 {
13223 int clz = clz_hwi (i);
13224 int ctz = ctz_hwi (i);
13225 if (i <= 31)
13226 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13227 else if (i <= 0xff)
13228 op = DW_OP_const1u;
13229 else if (i <= 0xffff)
13230 op = DW_OP_const2u;
13231 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13232 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13233 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13234 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13235 while DW_OP_const4u is 5 bytes. */
13236 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13237 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13238 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13239 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13240 while DW_OP_const4u is 5 bytes. */
13241 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13242
13243 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13244 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13245 <= 4)
13246 {
13247 /* As i >= 2**31, the double cast above will yield a negative number.
13248 Since wrapping is defined in DWARF expressions we can output big
13249 positive integers as small negative ones, regardless of the size
13250 of host wide ints.
13251
13252 Here, since the evaluator will handle 32-bit values and since i >=
13253 2**31, we know it's going to be interpreted as a negative literal:
13254 store it this way if we can do better than 5 bytes this way. */
13255 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13256 }
13257 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13258 op = DW_OP_const4u;
13259
13260 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13261 least 6 bytes: see if we can do better before falling back to it. */
13262 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13263 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13264 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13265 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13266 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13267 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13268 >= HOST_BITS_PER_WIDE_INT)
13269 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13270 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13271 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13272 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13273 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13274 && size_of_uleb128 (i) > 6)
13275 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13276 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13277 else
13278 op = DW_OP_constu;
13279 }
13280 else
13281 {
13282 if (i >= -0x80)
13283 op = DW_OP_const1s;
13284 else if (i >= -0x8000)
13285 op = DW_OP_const2s;
13286 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13287 {
13288 if (size_of_int_loc_descriptor (i) < 5)
13289 {
13290 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13291 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13292 return ret;
13293 }
13294 op = DW_OP_const4s;
13295 }
13296 else
13297 {
13298 if (size_of_int_loc_descriptor (i)
13299 < (unsigned long) 1 + size_of_sleb128 (i))
13300 {
13301 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13302 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13303 return ret;
13304 }
13305 op = DW_OP_consts;
13306 }
13307 }
13308
13309 return new_loc_descr (op, i, 0);
13310 }
13311
13312 /* Likewise, for unsigned constants. */
13313
13314 static dw_loc_descr_ref
13315 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13316 {
13317 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13318 const unsigned HOST_WIDE_INT max_uint
13319 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13320
13321 /* If possible, use the clever signed constants handling. */
13322 if (i <= max_int)
13323 return int_loc_descriptor ((HOST_WIDE_INT) i);
13324
13325 /* Here, we are left with positive numbers that cannot be represented as
13326 HOST_WIDE_INT, i.e.:
13327 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13328
13329 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13330 whereas may be better to output a negative integer: thanks to integer
13331 wrapping, we know that:
13332 x = x - 2 ** DWARF2_ADDR_SIZE
13333 = x - 2 * (max (HOST_WIDE_INT) + 1)
13334 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13335 small negative integers. Let's try that in cases it will clearly improve
13336 the encoding: there is no gain turning DW_OP_const4u into
13337 DW_OP_const4s. */
13338 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13339 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13340 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13341 {
13342 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13343
13344 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13345 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13346 const HOST_WIDE_INT second_shift
13347 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13348
13349 /* So we finally have:
13350 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13351 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13352 return int_loc_descriptor (second_shift);
13353 }
13354
13355 /* Last chance: fallback to a simple constant operation. */
13356 return new_loc_descr
13357 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13358 ? DW_OP_const4u
13359 : DW_OP_const8u,
13360 i, 0);
13361 }
13362
13363 /* Generate and return a location description that computes the unsigned
13364 comparison of the two stack top entries (a OP b where b is the top-most
13365 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13366 LE_EXPR, GT_EXPR or GE_EXPR. */
13367
13368 static dw_loc_descr_ref
13369 uint_comparison_loc_list (enum tree_code kind)
13370 {
13371 enum dwarf_location_atom op, flip_op;
13372 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13373
13374 switch (kind)
13375 {
13376 case LT_EXPR:
13377 op = DW_OP_lt;
13378 break;
13379 case LE_EXPR:
13380 op = DW_OP_le;
13381 break;
13382 case GT_EXPR:
13383 op = DW_OP_gt;
13384 break;
13385 case GE_EXPR:
13386 op = DW_OP_ge;
13387 break;
13388 default:
13389 gcc_unreachable ();
13390 }
13391
13392 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13393 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13394
13395 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13396 possible to perform unsigned comparisons: we just have to distinguish
13397 three cases:
13398
13399 1. when a and b have the same sign (as signed integers); then we should
13400 return: a OP(signed) b;
13401
13402 2. when a is a negative signed integer while b is a positive one, then a
13403 is a greater unsigned integer than b; likewise when a and b's roles
13404 are flipped.
13405
13406 So first, compare the sign of the two operands. */
13407 ret = new_loc_descr (DW_OP_over, 0, 0);
13408 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13409 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13410 /* If they have different signs (i.e. they have different sign bits), then
13411 the stack top value has now the sign bit set and thus it's smaller than
13412 zero. */
13413 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13414 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13415 add_loc_descr (&ret, bra_node);
13416
13417 /* We are in case 1. At this point, we know both operands have the same
13418 sign, to it's safe to use the built-in signed comparison. */
13419 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13420 add_loc_descr (&ret, jmp_node);
13421
13422 /* We are in case 2. Here, we know both operands do not have the same sign,
13423 so we have to flip the signed comparison. */
13424 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13425 tmp = new_loc_descr (flip_op, 0, 0);
13426 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13427 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13428 add_loc_descr (&ret, tmp);
13429
13430 /* This dummy operation is necessary to make the two branches join. */
13431 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13432 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13433 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13434 add_loc_descr (&ret, tmp);
13435
13436 return ret;
13437 }
13438
13439 /* Likewise, but takes the location description lists (might be destructive on
13440 them). Return NULL if either is NULL or if concatenation fails. */
13441
13442 static dw_loc_list_ref
13443 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13444 enum tree_code kind)
13445 {
13446 if (left == NULL || right == NULL)
13447 return NULL;
13448
13449 add_loc_list (&left, right);
13450 if (left == NULL)
13451 return NULL;
13452
13453 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13454 return left;
13455 }
13456
13457 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13458 without actually allocating it. */
13459
13460 static unsigned long
13461 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13462 {
13463 return size_of_int_loc_descriptor (i >> shift)
13464 + size_of_int_loc_descriptor (shift)
13465 + 1;
13466 }
13467
13468 /* Return size_of_locs (int_loc_descriptor (i)) without
13469 actually allocating it. */
13470
13471 static unsigned long
13472 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13473 {
13474 unsigned long s;
13475
13476 if (i >= 0)
13477 {
13478 int clz, ctz;
13479 if (i <= 31)
13480 return 1;
13481 else if (i <= 0xff)
13482 return 2;
13483 else if (i <= 0xffff)
13484 return 3;
13485 clz = clz_hwi (i);
13486 ctz = ctz_hwi (i);
13487 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13488 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13489 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13490 - clz - 5);
13491 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13492 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13493 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13494 - clz - 8);
13495 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13496 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13497 <= 4)
13498 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13499 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13500 return 5;
13501 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13502 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13503 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13504 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13505 - clz - 8);
13506 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13507 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13508 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13509 - clz - 16);
13510 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13511 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13512 && s > 6)
13513 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13514 - clz - 32);
13515 else
13516 return 1 + s;
13517 }
13518 else
13519 {
13520 if (i >= -0x80)
13521 return 2;
13522 else if (i >= -0x8000)
13523 return 3;
13524 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13525 {
13526 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13527 {
13528 s = size_of_int_loc_descriptor (-i) + 1;
13529 if (s < 5)
13530 return s;
13531 }
13532 return 5;
13533 }
13534 else
13535 {
13536 unsigned long r = 1 + size_of_sleb128 (i);
13537 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13538 {
13539 s = size_of_int_loc_descriptor (-i) + 1;
13540 if (s < r)
13541 return s;
13542 }
13543 return r;
13544 }
13545 }
13546 }
13547
13548 /* Return loc description representing "address" of integer value.
13549 This can appear only as toplevel expression. */
13550
13551 static dw_loc_descr_ref
13552 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13553 {
13554 int litsize;
13555 dw_loc_descr_ref loc_result = NULL;
13556
13557 if (!(dwarf_version >= 4 || !dwarf_strict))
13558 return NULL;
13559
13560 litsize = size_of_int_loc_descriptor (i);
13561 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13562 is more compact. For DW_OP_stack_value we need:
13563 litsize + 1 (DW_OP_stack_value)
13564 and for DW_OP_implicit_value:
13565 1 (DW_OP_implicit_value) + 1 (length) + size. */
13566 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13567 {
13568 loc_result = int_loc_descriptor (i);
13569 add_loc_descr (&loc_result,
13570 new_loc_descr (DW_OP_stack_value, 0, 0));
13571 return loc_result;
13572 }
13573
13574 loc_result = new_loc_descr (DW_OP_implicit_value,
13575 size, 0);
13576 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13577 loc_result->dw_loc_oprnd2.v.val_int = i;
13578 return loc_result;
13579 }
13580
13581 /* Return a location descriptor that designates a base+offset location. */
13582
13583 static dw_loc_descr_ref
13584 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13585 enum var_init_status initialized)
13586 {
13587 unsigned int regno;
13588 dw_loc_descr_ref result;
13589 dw_fde_ref fde = cfun->fde;
13590
13591 /* We only use "frame base" when we're sure we're talking about the
13592 post-prologue local stack frame. We do this by *not* running
13593 register elimination until this point, and recognizing the special
13594 argument pointer and soft frame pointer rtx's. */
13595 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13596 {
13597 rtx elim = (ira_use_lra_p
13598 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13599 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13600
13601 if (elim != reg)
13602 {
13603 if (GET_CODE (elim) == PLUS)
13604 {
13605 offset += INTVAL (XEXP (elim, 1));
13606 elim = XEXP (elim, 0);
13607 }
13608 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13609 && (elim == hard_frame_pointer_rtx
13610 || elim == stack_pointer_rtx))
13611 || elim == (frame_pointer_needed
13612 ? hard_frame_pointer_rtx
13613 : stack_pointer_rtx));
13614
13615 /* If drap register is used to align stack, use frame
13616 pointer + offset to access stack variables. If stack
13617 is aligned without drap, use stack pointer + offset to
13618 access stack variables. */
13619 if (crtl->stack_realign_tried
13620 && reg == frame_pointer_rtx)
13621 {
13622 int base_reg
13623 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13624 ? HARD_FRAME_POINTER_REGNUM
13625 : REGNO (elim));
13626 return new_reg_loc_descr (base_reg, offset);
13627 }
13628
13629 gcc_assert (frame_pointer_fb_offset_valid);
13630 offset += frame_pointer_fb_offset;
13631 return new_loc_descr (DW_OP_fbreg, offset, 0);
13632 }
13633 }
13634
13635 regno = REGNO (reg);
13636 #ifdef LEAF_REG_REMAP
13637 if (crtl->uses_only_leaf_regs)
13638 {
13639 int leaf_reg = LEAF_REG_REMAP (regno);
13640 if (leaf_reg != -1)
13641 regno = (unsigned) leaf_reg;
13642 }
13643 #endif
13644 regno = DWARF_FRAME_REGNUM (regno);
13645
13646 if (!optimize && fde
13647 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13648 {
13649 /* Use cfa+offset to represent the location of arguments passed
13650 on the stack when drap is used to align stack.
13651 Only do this when not optimizing, for optimized code var-tracking
13652 is supposed to track where the arguments live and the register
13653 used as vdrap or drap in some spot might be used for something
13654 else in other part of the routine. */
13655 return new_loc_descr (DW_OP_fbreg, offset, 0);
13656 }
13657
13658 if (regno <= 31)
13659 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13660 offset, 0);
13661 else
13662 result = new_loc_descr (DW_OP_bregx, regno, offset);
13663
13664 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13665 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13666
13667 return result;
13668 }
13669
13670 /* Return true if this RTL expression describes a base+offset calculation. */
13671
13672 static inline int
13673 is_based_loc (const_rtx rtl)
13674 {
13675 return (GET_CODE (rtl) == PLUS
13676 && ((REG_P (XEXP (rtl, 0))
13677 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13678 && CONST_INT_P (XEXP (rtl, 1)))));
13679 }
13680
13681 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13682 failed. */
13683
13684 static dw_loc_descr_ref
13685 tls_mem_loc_descriptor (rtx mem)
13686 {
13687 tree base;
13688 dw_loc_descr_ref loc_result;
13689
13690 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13691 return NULL;
13692
13693 base = get_base_address (MEM_EXPR (mem));
13694 if (base == NULL
13695 || !VAR_P (base)
13696 || !DECL_THREAD_LOCAL_P (base))
13697 return NULL;
13698
13699 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13700 if (loc_result == NULL)
13701 return NULL;
13702
13703 if (MEM_OFFSET (mem))
13704 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13705
13706 return loc_result;
13707 }
13708
13709 /* Output debug info about reason why we failed to expand expression as dwarf
13710 expression. */
13711
13712 static void
13713 expansion_failed (tree expr, rtx rtl, char const *reason)
13714 {
13715 if (dump_file && (dump_flags & TDF_DETAILS))
13716 {
13717 fprintf (dump_file, "Failed to expand as dwarf: ");
13718 if (expr)
13719 print_generic_expr (dump_file, expr, dump_flags);
13720 if (rtl)
13721 {
13722 fprintf (dump_file, "\n");
13723 print_rtl (dump_file, rtl);
13724 }
13725 fprintf (dump_file, "\nReason: %s\n", reason);
13726 }
13727 }
13728
13729 /* Helper function for const_ok_for_output. */
13730
13731 static bool
13732 const_ok_for_output_1 (rtx rtl)
13733 {
13734 if (targetm.const_not_ok_for_debug_p (rtl))
13735 {
13736 if (GET_CODE (rtl) != UNSPEC)
13737 {
13738 expansion_failed (NULL_TREE, rtl,
13739 "Expression rejected for debug by the backend.\n");
13740 return false;
13741 }
13742
13743 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13744 the target hook doesn't explicitly allow it in debug info, assume
13745 we can't express it in the debug info. */
13746 /* Don't complain about TLS UNSPECs, those are just too hard to
13747 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13748 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13749 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13750 if (flag_checking
13751 && (XVECLEN (rtl, 0) == 0
13752 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13753 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13754 inform (current_function_decl
13755 ? DECL_SOURCE_LOCATION (current_function_decl)
13756 : UNKNOWN_LOCATION,
13757 #if NUM_UNSPEC_VALUES > 0
13758 "non-delegitimized UNSPEC %s (%d) found in variable location",
13759 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13760 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13761 XINT (rtl, 1));
13762 #else
13763 "non-delegitimized UNSPEC %d found in variable location",
13764 XINT (rtl, 1));
13765 #endif
13766 expansion_failed (NULL_TREE, rtl,
13767 "UNSPEC hasn't been delegitimized.\n");
13768 return false;
13769 }
13770
13771 /* FIXME: Refer to PR60655. It is possible for simplification
13772 of rtl expressions in var tracking to produce such expressions.
13773 We should really identify / validate expressions
13774 enclosed in CONST that can be handled by assemblers on various
13775 targets and only handle legitimate cases here. */
13776 switch (GET_CODE (rtl))
13777 {
13778 case SYMBOL_REF:
13779 break;
13780 case NOT:
13781 case NEG:
13782 return false;
13783 default:
13784 return true;
13785 }
13786
13787 if (CONSTANT_POOL_ADDRESS_P (rtl))
13788 {
13789 bool marked;
13790 get_pool_constant_mark (rtl, &marked);
13791 /* If all references to this pool constant were optimized away,
13792 it was not output and thus we can't represent it. */
13793 if (!marked)
13794 {
13795 expansion_failed (NULL_TREE, rtl,
13796 "Constant was removed from constant pool.\n");
13797 return false;
13798 }
13799 }
13800
13801 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13802 return false;
13803
13804 /* Avoid references to external symbols in debug info, on several targets
13805 the linker might even refuse to link when linking a shared library,
13806 and in many other cases the relocations for .debug_info/.debug_loc are
13807 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13808 to be defined within the same shared library or executable are fine. */
13809 if (SYMBOL_REF_EXTERNAL_P (rtl))
13810 {
13811 tree decl = SYMBOL_REF_DECL (rtl);
13812
13813 if (decl == NULL || !targetm.binds_local_p (decl))
13814 {
13815 expansion_failed (NULL_TREE, rtl,
13816 "Symbol not defined in current TU.\n");
13817 return false;
13818 }
13819 }
13820
13821 return true;
13822 }
13823
13824 /* Return true if constant RTL can be emitted in DW_OP_addr or
13825 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13826 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13827
13828 static bool
13829 const_ok_for_output (rtx rtl)
13830 {
13831 if (GET_CODE (rtl) == SYMBOL_REF)
13832 return const_ok_for_output_1 (rtl);
13833
13834 if (GET_CODE (rtl) == CONST)
13835 {
13836 subrtx_var_iterator::array_type array;
13837 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13838 if (!const_ok_for_output_1 (*iter))
13839 return false;
13840 return true;
13841 }
13842
13843 return true;
13844 }
13845
13846 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13847 if possible, NULL otherwise. */
13848
13849 static dw_die_ref
13850 base_type_for_mode (machine_mode mode, bool unsignedp)
13851 {
13852 dw_die_ref type_die;
13853 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13854
13855 if (type == NULL)
13856 return NULL;
13857 switch (TREE_CODE (type))
13858 {
13859 case INTEGER_TYPE:
13860 case REAL_TYPE:
13861 break;
13862 default:
13863 return NULL;
13864 }
13865 type_die = lookup_type_die (type);
13866 if (!type_die)
13867 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13868 comp_unit_die ());
13869 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13870 return NULL;
13871 return type_die;
13872 }
13873
13874 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13875 type matching MODE, or, if MODE is narrower than or as wide as
13876 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13877 possible. */
13878
13879 static dw_loc_descr_ref
13880 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13881 {
13882 machine_mode outer_mode = mode;
13883 dw_die_ref type_die;
13884 dw_loc_descr_ref cvt;
13885
13886 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13887 {
13888 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13889 return op;
13890 }
13891 type_die = base_type_for_mode (outer_mode, 1);
13892 if (type_die == NULL)
13893 return NULL;
13894 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13895 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13896 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13897 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13898 add_loc_descr (&op, cvt);
13899 return op;
13900 }
13901
13902 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13903
13904 static dw_loc_descr_ref
13905 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13906 dw_loc_descr_ref op1)
13907 {
13908 dw_loc_descr_ref ret = op0;
13909 add_loc_descr (&ret, op1);
13910 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13911 if (STORE_FLAG_VALUE != 1)
13912 {
13913 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13914 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13915 }
13916 return ret;
13917 }
13918
13919 /* Subroutine of scompare_loc_descriptor for the case in which we're
13920 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13921 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13922
13923 static dw_loc_descr_ref
13924 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13925 scalar_int_mode op_mode,
13926 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13927 {
13928 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13929 dw_loc_descr_ref cvt;
13930
13931 if (type_die == NULL)
13932 return NULL;
13933 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13934 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13935 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13936 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13937 add_loc_descr (&op0, cvt);
13938 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13939 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13940 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13941 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13942 add_loc_descr (&op1, cvt);
13943 return compare_loc_descriptor (op, op0, op1);
13944 }
13945
13946 /* Subroutine of scompare_loc_descriptor for the case in which we're
13947 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13948 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13949
13950 static dw_loc_descr_ref
13951 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13952 scalar_int_mode op_mode,
13953 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13954 {
13955 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13956 /* For eq/ne, if the operands are known to be zero-extended,
13957 there is no need to do the fancy shifting up. */
13958 if (op == DW_OP_eq || op == DW_OP_ne)
13959 {
13960 dw_loc_descr_ref last0, last1;
13961 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13962 ;
13963 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13964 ;
13965 /* deref_size zero extends, and for constants we can check
13966 whether they are zero extended or not. */
13967 if (((last0->dw_loc_opc == DW_OP_deref_size
13968 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13969 || (CONST_INT_P (XEXP (rtl, 0))
13970 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13971 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13972 && ((last1->dw_loc_opc == DW_OP_deref_size
13973 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13974 || (CONST_INT_P (XEXP (rtl, 1))
13975 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13976 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13977 return compare_loc_descriptor (op, op0, op1);
13978
13979 /* EQ/NE comparison against constant in narrower type than
13980 DWARF2_ADDR_SIZE can be performed either as
13981 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13982 DW_OP_{eq,ne}
13983 or
13984 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13985 DW_OP_{eq,ne}. Pick whatever is shorter. */
13986 if (CONST_INT_P (XEXP (rtl, 1))
13987 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13988 && (size_of_int_loc_descriptor (shift) + 1
13989 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13990 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13991 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13992 & GET_MODE_MASK (op_mode))))
13993 {
13994 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13995 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13996 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13997 & GET_MODE_MASK (op_mode));
13998 return compare_loc_descriptor (op, op0, op1);
13999 }
14000 }
14001 add_loc_descr (&op0, int_loc_descriptor (shift));
14002 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14003 if (CONST_INT_P (XEXP (rtl, 1)))
14004 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14005 else
14006 {
14007 add_loc_descr (&op1, int_loc_descriptor (shift));
14008 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14009 }
14010 return compare_loc_descriptor (op, op0, op1);
14011 }
14012
14013 /* Return location descriptor for unsigned comparison OP RTL. */
14014
14015 static dw_loc_descr_ref
14016 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14017 machine_mode mem_mode)
14018 {
14019 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14020 dw_loc_descr_ref op0, op1;
14021
14022 if (op_mode == VOIDmode)
14023 op_mode = GET_MODE (XEXP (rtl, 1));
14024 if (op_mode == VOIDmode)
14025 return NULL;
14026
14027 scalar_int_mode int_op_mode;
14028 if (dwarf_strict
14029 && dwarf_version < 5
14030 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14031 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14032 return NULL;
14033
14034 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14035 VAR_INIT_STATUS_INITIALIZED);
14036 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14037 VAR_INIT_STATUS_INITIALIZED);
14038
14039 if (op0 == NULL || op1 == NULL)
14040 return NULL;
14041
14042 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14043 {
14044 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14045 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14046
14047 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14048 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14049 }
14050 return compare_loc_descriptor (op, op0, op1);
14051 }
14052
14053 /* Return location descriptor for unsigned comparison OP RTL. */
14054
14055 static dw_loc_descr_ref
14056 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14057 machine_mode mem_mode)
14058 {
14059 dw_loc_descr_ref op0, op1;
14060
14061 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14062 if (test_op_mode == VOIDmode)
14063 test_op_mode = GET_MODE (XEXP (rtl, 1));
14064
14065 scalar_int_mode op_mode;
14066 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14067 return NULL;
14068
14069 if (dwarf_strict
14070 && dwarf_version < 5
14071 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14072 return NULL;
14073
14074 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14075 VAR_INIT_STATUS_INITIALIZED);
14076 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14077 VAR_INIT_STATUS_INITIALIZED);
14078
14079 if (op0 == NULL || op1 == NULL)
14080 return NULL;
14081
14082 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14083 {
14084 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14085 dw_loc_descr_ref last0, last1;
14086 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14087 ;
14088 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14089 ;
14090 if (CONST_INT_P (XEXP (rtl, 0)))
14091 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14092 /* deref_size zero extends, so no need to mask it again. */
14093 else if (last0->dw_loc_opc != DW_OP_deref_size
14094 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14095 {
14096 add_loc_descr (&op0, int_loc_descriptor (mask));
14097 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14098 }
14099 if (CONST_INT_P (XEXP (rtl, 1)))
14100 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14101 /* deref_size zero extends, so no need to mask it again. */
14102 else if (last1->dw_loc_opc != DW_OP_deref_size
14103 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14104 {
14105 add_loc_descr (&op1, int_loc_descriptor (mask));
14106 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14107 }
14108 }
14109 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14110 {
14111 HOST_WIDE_INT bias = 1;
14112 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14113 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14114 if (CONST_INT_P (XEXP (rtl, 1)))
14115 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14116 + INTVAL (XEXP (rtl, 1)));
14117 else
14118 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14119 bias, 0));
14120 }
14121 return compare_loc_descriptor (op, op0, op1);
14122 }
14123
14124 /* Return location descriptor for {U,S}{MIN,MAX}. */
14125
14126 static dw_loc_descr_ref
14127 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14128 machine_mode mem_mode)
14129 {
14130 enum dwarf_location_atom op;
14131 dw_loc_descr_ref op0, op1, ret;
14132 dw_loc_descr_ref bra_node, drop_node;
14133
14134 scalar_int_mode int_mode;
14135 if (dwarf_strict
14136 && dwarf_version < 5
14137 && (!is_a <scalar_int_mode> (mode, &int_mode)
14138 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14139 return NULL;
14140
14141 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14142 VAR_INIT_STATUS_INITIALIZED);
14143 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14144 VAR_INIT_STATUS_INITIALIZED);
14145
14146 if (op0 == NULL || op1 == NULL)
14147 return NULL;
14148
14149 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14150 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14151 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14152 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14153 {
14154 /* Checked by the caller. */
14155 int_mode = as_a <scalar_int_mode> (mode);
14156 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14157 {
14158 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14159 add_loc_descr (&op0, int_loc_descriptor (mask));
14160 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14161 add_loc_descr (&op1, int_loc_descriptor (mask));
14162 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14163 }
14164 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14165 {
14166 HOST_WIDE_INT bias = 1;
14167 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14168 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14169 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14170 }
14171 }
14172 else if (is_a <scalar_int_mode> (mode, &int_mode)
14173 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14174 {
14175 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14176 add_loc_descr (&op0, int_loc_descriptor (shift));
14177 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14178 add_loc_descr (&op1, int_loc_descriptor (shift));
14179 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14180 }
14181 else if (is_a <scalar_int_mode> (mode, &int_mode)
14182 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14183 {
14184 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14185 dw_loc_descr_ref cvt;
14186 if (type_die == NULL)
14187 return NULL;
14188 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14189 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14190 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14191 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14192 add_loc_descr (&op0, cvt);
14193 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14194 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14195 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14196 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14197 add_loc_descr (&op1, cvt);
14198 }
14199
14200 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14201 op = DW_OP_lt;
14202 else
14203 op = DW_OP_gt;
14204 ret = op0;
14205 add_loc_descr (&ret, op1);
14206 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14207 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14208 add_loc_descr (&ret, bra_node);
14209 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14210 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14211 add_loc_descr (&ret, drop_node);
14212 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14213 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14214 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14215 && is_a <scalar_int_mode> (mode, &int_mode)
14216 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14217 ret = convert_descriptor_to_mode (int_mode, ret);
14218 return ret;
14219 }
14220
14221 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14222 but after converting arguments to type_die, afterwards
14223 convert back to unsigned. */
14224
14225 static dw_loc_descr_ref
14226 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14227 scalar_int_mode mode, machine_mode mem_mode)
14228 {
14229 dw_loc_descr_ref cvt, op0, op1;
14230
14231 if (type_die == NULL)
14232 return NULL;
14233 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14234 VAR_INIT_STATUS_INITIALIZED);
14235 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14236 VAR_INIT_STATUS_INITIALIZED);
14237 if (op0 == NULL || op1 == NULL)
14238 return NULL;
14239 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14240 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14241 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14242 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14243 add_loc_descr (&op0, cvt);
14244 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14245 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14246 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14247 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14248 add_loc_descr (&op1, cvt);
14249 add_loc_descr (&op0, op1);
14250 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14251 return convert_descriptor_to_mode (mode, op0);
14252 }
14253
14254 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14255 const0 is DW_OP_lit0 or corresponding typed constant,
14256 const1 is DW_OP_lit1 or corresponding typed constant
14257 and constMSB is constant with just the MSB bit set
14258 for the mode):
14259 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14260 L1: const0 DW_OP_swap
14261 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14262 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14263 L3: DW_OP_drop
14264 L4: DW_OP_nop
14265
14266 CTZ is similar:
14267 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14268 L1: const0 DW_OP_swap
14269 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14270 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14271 L3: DW_OP_drop
14272 L4: DW_OP_nop
14273
14274 FFS is similar:
14275 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14276 L1: const1 DW_OP_swap
14277 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14278 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14279 L3: DW_OP_drop
14280 L4: DW_OP_nop */
14281
14282 static dw_loc_descr_ref
14283 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14284 machine_mode mem_mode)
14285 {
14286 dw_loc_descr_ref op0, ret, tmp;
14287 HOST_WIDE_INT valv;
14288 dw_loc_descr_ref l1jump, l1label;
14289 dw_loc_descr_ref l2jump, l2label;
14290 dw_loc_descr_ref l3jump, l3label;
14291 dw_loc_descr_ref l4jump, l4label;
14292 rtx msb;
14293
14294 if (GET_MODE (XEXP (rtl, 0)) != mode)
14295 return NULL;
14296
14297 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14298 VAR_INIT_STATUS_INITIALIZED);
14299 if (op0 == NULL)
14300 return NULL;
14301 ret = op0;
14302 if (GET_CODE (rtl) == CLZ)
14303 {
14304 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14305 valv = GET_MODE_BITSIZE (mode);
14306 }
14307 else if (GET_CODE (rtl) == FFS)
14308 valv = 0;
14309 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14310 valv = GET_MODE_BITSIZE (mode);
14311 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14312 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14313 add_loc_descr (&ret, l1jump);
14314 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14315 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14316 VAR_INIT_STATUS_INITIALIZED);
14317 if (tmp == NULL)
14318 return NULL;
14319 add_loc_descr (&ret, tmp);
14320 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14321 add_loc_descr (&ret, l4jump);
14322 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14323 ? const1_rtx : const0_rtx,
14324 mode, mem_mode,
14325 VAR_INIT_STATUS_INITIALIZED);
14326 if (l1label == NULL)
14327 return NULL;
14328 add_loc_descr (&ret, l1label);
14329 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14330 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14331 add_loc_descr (&ret, l2label);
14332 if (GET_CODE (rtl) != CLZ)
14333 msb = const1_rtx;
14334 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14335 msb = GEN_INT (HOST_WIDE_INT_1U
14336 << (GET_MODE_BITSIZE (mode) - 1));
14337 else
14338 msb = immed_wide_int_const
14339 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14340 GET_MODE_PRECISION (mode)), mode);
14341 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14342 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14343 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14344 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14345 else
14346 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14347 VAR_INIT_STATUS_INITIALIZED);
14348 if (tmp == NULL)
14349 return NULL;
14350 add_loc_descr (&ret, tmp);
14351 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14352 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14353 add_loc_descr (&ret, l3jump);
14354 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14355 VAR_INIT_STATUS_INITIALIZED);
14356 if (tmp == NULL)
14357 return NULL;
14358 add_loc_descr (&ret, tmp);
14359 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14360 ? DW_OP_shl : DW_OP_shr, 0, 0));
14361 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14362 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14363 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14364 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14365 add_loc_descr (&ret, l2jump);
14366 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14367 add_loc_descr (&ret, l3label);
14368 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14369 add_loc_descr (&ret, l4label);
14370 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14371 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14372 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14373 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14374 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14375 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14376 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14377 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14378 return ret;
14379 }
14380
14381 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14382 const1 is DW_OP_lit1 or corresponding typed constant):
14383 const0 DW_OP_swap
14384 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14385 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14386 L2: DW_OP_drop
14387
14388 PARITY is similar:
14389 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14390 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14391 L2: DW_OP_drop */
14392
14393 static dw_loc_descr_ref
14394 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14395 machine_mode mem_mode)
14396 {
14397 dw_loc_descr_ref op0, ret, tmp;
14398 dw_loc_descr_ref l1jump, l1label;
14399 dw_loc_descr_ref l2jump, l2label;
14400
14401 if (GET_MODE (XEXP (rtl, 0)) != mode)
14402 return NULL;
14403
14404 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14405 VAR_INIT_STATUS_INITIALIZED);
14406 if (op0 == NULL)
14407 return NULL;
14408 ret = op0;
14409 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14410 VAR_INIT_STATUS_INITIALIZED);
14411 if (tmp == NULL)
14412 return NULL;
14413 add_loc_descr (&ret, tmp);
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14415 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14416 add_loc_descr (&ret, l1label);
14417 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14418 add_loc_descr (&ret, l2jump);
14419 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14420 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14421 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14422 VAR_INIT_STATUS_INITIALIZED);
14423 if (tmp == NULL)
14424 return NULL;
14425 add_loc_descr (&ret, tmp);
14426 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14427 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14428 ? DW_OP_plus : DW_OP_xor, 0, 0));
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14430 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14431 VAR_INIT_STATUS_INITIALIZED);
14432 add_loc_descr (&ret, tmp);
14433 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14434 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14435 add_loc_descr (&ret, l1jump);
14436 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14437 add_loc_descr (&ret, l2label);
14438 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14439 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14440 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14441 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14442 return ret;
14443 }
14444
14445 /* BSWAP (constS is initial shift count, either 56 or 24):
14446 constS const0
14447 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14448 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14449 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14450 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14451 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14452
14453 static dw_loc_descr_ref
14454 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14455 machine_mode mem_mode)
14456 {
14457 dw_loc_descr_ref op0, ret, tmp;
14458 dw_loc_descr_ref l1jump, l1label;
14459 dw_loc_descr_ref l2jump, l2label;
14460
14461 if (BITS_PER_UNIT != 8
14462 || (GET_MODE_BITSIZE (mode) != 32
14463 && GET_MODE_BITSIZE (mode) != 64))
14464 return NULL;
14465
14466 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14467 VAR_INIT_STATUS_INITIALIZED);
14468 if (op0 == NULL)
14469 return NULL;
14470
14471 ret = op0;
14472 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14473 mode, mem_mode,
14474 VAR_INIT_STATUS_INITIALIZED);
14475 if (tmp == NULL)
14476 return NULL;
14477 add_loc_descr (&ret, tmp);
14478 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14479 VAR_INIT_STATUS_INITIALIZED);
14480 if (tmp == NULL)
14481 return NULL;
14482 add_loc_descr (&ret, tmp);
14483 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14484 add_loc_descr (&ret, l1label);
14485 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14486 mode, mem_mode,
14487 VAR_INIT_STATUS_INITIALIZED);
14488 add_loc_descr (&ret, tmp);
14489 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14490 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14491 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14492 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14493 VAR_INIT_STATUS_INITIALIZED);
14494 if (tmp == NULL)
14495 return NULL;
14496 add_loc_descr (&ret, tmp);
14497 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14498 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14500 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14502 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14503 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14504 VAR_INIT_STATUS_INITIALIZED);
14505 add_loc_descr (&ret, tmp);
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14507 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14508 add_loc_descr (&ret, l2jump);
14509 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14510 VAR_INIT_STATUS_INITIALIZED);
14511 add_loc_descr (&ret, tmp);
14512 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14514 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14515 add_loc_descr (&ret, l1jump);
14516 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14517 add_loc_descr (&ret, l2label);
14518 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14519 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14520 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14521 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14522 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14523 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14524 return ret;
14525 }
14526
14527 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14528 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14529 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14530 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14531
14532 ROTATERT is similar:
14533 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14534 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14535 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14536
14537 static dw_loc_descr_ref
14538 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14539 machine_mode mem_mode)
14540 {
14541 rtx rtlop1 = XEXP (rtl, 1);
14542 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14543 int i;
14544
14545 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14546 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14547 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14548 VAR_INIT_STATUS_INITIALIZED);
14549 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14550 VAR_INIT_STATUS_INITIALIZED);
14551 if (op0 == NULL || op1 == NULL)
14552 return NULL;
14553 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14554 for (i = 0; i < 2; i++)
14555 {
14556 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14557 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14558 mode, mem_mode,
14559 VAR_INIT_STATUS_INITIALIZED);
14560 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14561 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14562 ? DW_OP_const4u
14563 : HOST_BITS_PER_WIDE_INT == 64
14564 ? DW_OP_const8u : DW_OP_constu,
14565 GET_MODE_MASK (mode), 0);
14566 else
14567 mask[i] = NULL;
14568 if (mask[i] == NULL)
14569 return NULL;
14570 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14571 }
14572 ret = op0;
14573 add_loc_descr (&ret, op1);
14574 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14575 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14576 if (GET_CODE (rtl) == ROTATERT)
14577 {
14578 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14579 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14580 GET_MODE_BITSIZE (mode), 0));
14581 }
14582 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14583 if (mask[0] != NULL)
14584 add_loc_descr (&ret, mask[0]);
14585 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14586 if (mask[1] != NULL)
14587 {
14588 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14589 add_loc_descr (&ret, mask[1]);
14590 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14591 }
14592 if (GET_CODE (rtl) == ROTATE)
14593 {
14594 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14595 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14596 GET_MODE_BITSIZE (mode), 0));
14597 }
14598 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14599 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14600 return ret;
14601 }
14602
14603 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14604 for DEBUG_PARAMETER_REF RTL. */
14605
14606 static dw_loc_descr_ref
14607 parameter_ref_descriptor (rtx rtl)
14608 {
14609 dw_loc_descr_ref ret;
14610 dw_die_ref ref;
14611
14612 if (dwarf_strict)
14613 return NULL;
14614 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14615 /* With LTO during LTRANS we get the late DIE that refers to the early
14616 DIE, thus we add another indirection here. This seems to confuse
14617 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14618 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14619 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14620 if (ref)
14621 {
14622 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14623 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14624 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14625 }
14626 else
14627 {
14628 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14629 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14630 }
14631 return ret;
14632 }
14633
14634 /* The following routine converts the RTL for a variable or parameter
14635 (resident in memory) into an equivalent Dwarf representation of a
14636 mechanism for getting the address of that same variable onto the top of a
14637 hypothetical "address evaluation" stack.
14638
14639 When creating memory location descriptors, we are effectively transforming
14640 the RTL for a memory-resident object into its Dwarf postfix expression
14641 equivalent. This routine recursively descends an RTL tree, turning
14642 it into Dwarf postfix code as it goes.
14643
14644 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14645
14646 MEM_MODE is the mode of the memory reference, needed to handle some
14647 autoincrement addressing modes.
14648
14649 Return 0 if we can't represent the location. */
14650
14651 dw_loc_descr_ref
14652 mem_loc_descriptor (rtx rtl, machine_mode mode,
14653 machine_mode mem_mode,
14654 enum var_init_status initialized)
14655 {
14656 dw_loc_descr_ref mem_loc_result = NULL;
14657 enum dwarf_location_atom op;
14658 dw_loc_descr_ref op0, op1;
14659 rtx inner = NULL_RTX;
14660
14661 if (mode == VOIDmode)
14662 mode = GET_MODE (rtl);
14663
14664 /* Note that for a dynamically sized array, the location we will generate a
14665 description of here will be the lowest numbered location which is
14666 actually within the array. That's *not* necessarily the same as the
14667 zeroth element of the array. */
14668
14669 rtl = targetm.delegitimize_address (rtl);
14670
14671 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14672 return NULL;
14673
14674 scalar_int_mode int_mode, inner_mode, op1_mode;
14675 switch (GET_CODE (rtl))
14676 {
14677 case POST_INC:
14678 case POST_DEC:
14679 case POST_MODIFY:
14680 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14681
14682 case SUBREG:
14683 /* The case of a subreg may arise when we have a local (register)
14684 variable or a formal (register) parameter which doesn't quite fill
14685 up an entire register. For now, just assume that it is
14686 legitimate to make the Dwarf info refer to the whole register which
14687 contains the given subreg. */
14688 if (!subreg_lowpart_p (rtl))
14689 break;
14690 inner = SUBREG_REG (rtl);
14691 /* FALLTHRU */
14692 case TRUNCATE:
14693 if (inner == NULL_RTX)
14694 inner = XEXP (rtl, 0);
14695 if (is_a <scalar_int_mode> (mode, &int_mode)
14696 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14697 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14698 #ifdef POINTERS_EXTEND_UNSIGNED
14699 || (int_mode == Pmode && mem_mode != VOIDmode)
14700 #endif
14701 )
14702 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14703 {
14704 mem_loc_result = mem_loc_descriptor (inner,
14705 inner_mode,
14706 mem_mode, initialized);
14707 break;
14708 }
14709 if (dwarf_strict && dwarf_version < 5)
14710 break;
14711 if (is_a <scalar_int_mode> (mode, &int_mode)
14712 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14713 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14714 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14715 {
14716 dw_die_ref type_die;
14717 dw_loc_descr_ref cvt;
14718
14719 mem_loc_result = mem_loc_descriptor (inner,
14720 GET_MODE (inner),
14721 mem_mode, initialized);
14722 if (mem_loc_result == NULL)
14723 break;
14724 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14725 if (type_die == NULL)
14726 {
14727 mem_loc_result = NULL;
14728 break;
14729 }
14730 if (GET_MODE_SIZE (mode)
14731 != GET_MODE_SIZE (GET_MODE (inner)))
14732 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14733 else
14734 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14735 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14736 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14737 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14738 add_loc_descr (&mem_loc_result, cvt);
14739 if (is_a <scalar_int_mode> (mode, &int_mode)
14740 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14741 {
14742 /* Convert it to untyped afterwards. */
14743 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14744 add_loc_descr (&mem_loc_result, cvt);
14745 }
14746 }
14747 break;
14748
14749 case REG:
14750 if (!is_a <scalar_int_mode> (mode, &int_mode)
14751 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14752 && rtl != arg_pointer_rtx
14753 && rtl != frame_pointer_rtx
14754 #ifdef POINTERS_EXTEND_UNSIGNED
14755 && (int_mode != Pmode || mem_mode == VOIDmode)
14756 #endif
14757 ))
14758 {
14759 dw_die_ref type_die;
14760 unsigned int dbx_regnum;
14761
14762 if (dwarf_strict && dwarf_version < 5)
14763 break;
14764 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14765 break;
14766 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14767 if (type_die == NULL)
14768 break;
14769
14770 dbx_regnum = dbx_reg_number (rtl);
14771 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14772 break;
14773 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14774 dbx_regnum, 0);
14775 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14776 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14777 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14778 break;
14779 }
14780 /* Whenever a register number forms a part of the description of the
14781 method for calculating the (dynamic) address of a memory resident
14782 object, DWARF rules require the register number be referred to as
14783 a "base register". This distinction is not based in any way upon
14784 what category of register the hardware believes the given register
14785 belongs to. This is strictly DWARF terminology we're dealing with
14786 here. Note that in cases where the location of a memory-resident
14787 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14788 OP_CONST (0)) the actual DWARF location descriptor that we generate
14789 may just be OP_BASEREG (basereg). This may look deceptively like
14790 the object in question was allocated to a register (rather than in
14791 memory) so DWARF consumers need to be aware of the subtle
14792 distinction between OP_REG and OP_BASEREG. */
14793 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14794 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14795 else if (stack_realign_drap
14796 && crtl->drap_reg
14797 && crtl->args.internal_arg_pointer == rtl
14798 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14799 {
14800 /* If RTL is internal_arg_pointer, which has been optimized
14801 out, use DRAP instead. */
14802 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14803 VAR_INIT_STATUS_INITIALIZED);
14804 }
14805 break;
14806
14807 case SIGN_EXTEND:
14808 case ZERO_EXTEND:
14809 if (!is_a <scalar_int_mode> (mode, &int_mode)
14810 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14811 break;
14812 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14813 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14814 if (op0 == 0)
14815 break;
14816 else if (GET_CODE (rtl) == ZERO_EXTEND
14817 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14818 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14819 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14820 to expand zero extend as two shifts instead of
14821 masking. */
14822 && GET_MODE_SIZE (inner_mode) <= 4)
14823 {
14824 mem_loc_result = op0;
14825 add_loc_descr (&mem_loc_result,
14826 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14827 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14828 }
14829 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14830 {
14831 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14832 shift *= BITS_PER_UNIT;
14833 if (GET_CODE (rtl) == SIGN_EXTEND)
14834 op = DW_OP_shra;
14835 else
14836 op = DW_OP_shr;
14837 mem_loc_result = op0;
14838 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14839 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14840 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14841 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14842 }
14843 else if (!dwarf_strict || dwarf_version >= 5)
14844 {
14845 dw_die_ref type_die1, type_die2;
14846 dw_loc_descr_ref cvt;
14847
14848 type_die1 = base_type_for_mode (inner_mode,
14849 GET_CODE (rtl) == ZERO_EXTEND);
14850 if (type_die1 == NULL)
14851 break;
14852 type_die2 = base_type_for_mode (int_mode, 1);
14853 if (type_die2 == NULL)
14854 break;
14855 mem_loc_result = op0;
14856 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14857 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14858 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14859 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14860 add_loc_descr (&mem_loc_result, cvt);
14861 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14862 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14863 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14864 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14865 add_loc_descr (&mem_loc_result, cvt);
14866 }
14867 break;
14868
14869 case MEM:
14870 {
14871 rtx new_rtl = avoid_constant_pool_reference (rtl);
14872 if (new_rtl != rtl)
14873 {
14874 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14875 initialized);
14876 if (mem_loc_result != NULL)
14877 return mem_loc_result;
14878 }
14879 }
14880 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14881 get_address_mode (rtl), mode,
14882 VAR_INIT_STATUS_INITIALIZED);
14883 if (mem_loc_result == NULL)
14884 mem_loc_result = tls_mem_loc_descriptor (rtl);
14885 if (mem_loc_result != NULL)
14886 {
14887 if (!is_a <scalar_int_mode> (mode, &int_mode)
14888 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14889 {
14890 dw_die_ref type_die;
14891 dw_loc_descr_ref deref;
14892
14893 if (dwarf_strict && dwarf_version < 5)
14894 return NULL;
14895 type_die
14896 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14897 if (type_die == NULL)
14898 return NULL;
14899 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14900 GET_MODE_SIZE (mode), 0);
14901 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14902 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14903 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14904 add_loc_descr (&mem_loc_result, deref);
14905 }
14906 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14907 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14908 else
14909 add_loc_descr (&mem_loc_result,
14910 new_loc_descr (DW_OP_deref_size,
14911 GET_MODE_SIZE (int_mode), 0));
14912 }
14913 break;
14914
14915 case LO_SUM:
14916 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14917
14918 case LABEL_REF:
14919 /* Some ports can transform a symbol ref into a label ref, because
14920 the symbol ref is too far away and has to be dumped into a constant
14921 pool. */
14922 case CONST:
14923 case SYMBOL_REF:
14924 if (!is_a <scalar_int_mode> (mode, &int_mode)
14925 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14926 #ifdef POINTERS_EXTEND_UNSIGNED
14927 && (int_mode != Pmode || mem_mode == VOIDmode)
14928 #endif
14929 ))
14930 break;
14931 if (GET_CODE (rtl) == SYMBOL_REF
14932 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14933 {
14934 dw_loc_descr_ref temp;
14935
14936 /* If this is not defined, we have no way to emit the data. */
14937 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14938 break;
14939
14940 temp = new_addr_loc_descr (rtl, dtprel_true);
14941
14942 /* We check for DWARF 5 here because gdb did not implement
14943 DW_OP_form_tls_address until after 7.12. */
14944 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14945 ? DW_OP_form_tls_address
14946 : DW_OP_GNU_push_tls_address),
14947 0, 0);
14948 add_loc_descr (&mem_loc_result, temp);
14949
14950 break;
14951 }
14952
14953 if (!const_ok_for_output (rtl))
14954 {
14955 if (GET_CODE (rtl) == CONST)
14956 switch (GET_CODE (XEXP (rtl, 0)))
14957 {
14958 case NOT:
14959 op = DW_OP_not;
14960 goto try_const_unop;
14961 case NEG:
14962 op = DW_OP_neg;
14963 goto try_const_unop;
14964 try_const_unop:
14965 rtx arg;
14966 arg = XEXP (XEXP (rtl, 0), 0);
14967 if (!CONSTANT_P (arg))
14968 arg = gen_rtx_CONST (int_mode, arg);
14969 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
14970 initialized);
14971 if (op0)
14972 {
14973 mem_loc_result = op0;
14974 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14975 }
14976 break;
14977 default:
14978 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14979 mem_mode, initialized);
14980 break;
14981 }
14982 break;
14983 }
14984
14985 symref:
14986 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14987 vec_safe_push (used_rtx_array, rtl);
14988 break;
14989
14990 case CONCAT:
14991 case CONCATN:
14992 case VAR_LOCATION:
14993 case DEBUG_IMPLICIT_PTR:
14994 expansion_failed (NULL_TREE, rtl,
14995 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14996 return 0;
14997
14998 case ENTRY_VALUE:
14999 if (dwarf_strict && dwarf_version < 5)
15000 return NULL;
15001 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15002 {
15003 if (!is_a <scalar_int_mode> (mode, &int_mode)
15004 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15005 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15006 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15007 else
15008 {
15009 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15010 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15011 return NULL;
15012 op0 = one_reg_loc_descriptor (dbx_regnum,
15013 VAR_INIT_STATUS_INITIALIZED);
15014 }
15015 }
15016 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15017 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15018 {
15019 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15020 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15021 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15022 return NULL;
15023 }
15024 else
15025 gcc_unreachable ();
15026 if (op0 == NULL)
15027 return NULL;
15028 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15029 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15030 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15031 break;
15032
15033 case DEBUG_PARAMETER_REF:
15034 mem_loc_result = parameter_ref_descriptor (rtl);
15035 break;
15036
15037 case PRE_MODIFY:
15038 /* Extract the PLUS expression nested inside and fall into
15039 PLUS code below. */
15040 rtl = XEXP (rtl, 1);
15041 goto plus;
15042
15043 case PRE_INC:
15044 case PRE_DEC:
15045 /* Turn these into a PLUS expression and fall into the PLUS code
15046 below. */
15047 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15048 gen_int_mode (GET_CODE (rtl) == PRE_INC
15049 ? GET_MODE_UNIT_SIZE (mem_mode)
15050 : -GET_MODE_UNIT_SIZE (mem_mode),
15051 mode));
15052
15053 /* fall through */
15054
15055 case PLUS:
15056 plus:
15057 if (is_based_loc (rtl)
15058 && is_a <scalar_int_mode> (mode, &int_mode)
15059 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15060 || XEXP (rtl, 0) == arg_pointer_rtx
15061 || XEXP (rtl, 0) == frame_pointer_rtx))
15062 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15063 INTVAL (XEXP (rtl, 1)),
15064 VAR_INIT_STATUS_INITIALIZED);
15065 else
15066 {
15067 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15068 VAR_INIT_STATUS_INITIALIZED);
15069 if (mem_loc_result == 0)
15070 break;
15071
15072 if (CONST_INT_P (XEXP (rtl, 1))
15073 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15074 <= DWARF2_ADDR_SIZE))
15075 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15076 else
15077 {
15078 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15079 VAR_INIT_STATUS_INITIALIZED);
15080 if (op1 == 0)
15081 return NULL;
15082 add_loc_descr (&mem_loc_result, op1);
15083 add_loc_descr (&mem_loc_result,
15084 new_loc_descr (DW_OP_plus, 0, 0));
15085 }
15086 }
15087 break;
15088
15089 /* If a pseudo-reg is optimized away, it is possible for it to
15090 be replaced with a MEM containing a multiply or shift. */
15091 case MINUS:
15092 op = DW_OP_minus;
15093 goto do_binop;
15094
15095 case MULT:
15096 op = DW_OP_mul;
15097 goto do_binop;
15098
15099 case DIV:
15100 if ((!dwarf_strict || dwarf_version >= 5)
15101 && is_a <scalar_int_mode> (mode, &int_mode)
15102 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15103 {
15104 mem_loc_result = typed_binop (DW_OP_div, rtl,
15105 base_type_for_mode (mode, 0),
15106 int_mode, mem_mode);
15107 break;
15108 }
15109 op = DW_OP_div;
15110 goto do_binop;
15111
15112 case UMOD:
15113 op = DW_OP_mod;
15114 goto do_binop;
15115
15116 case ASHIFT:
15117 op = DW_OP_shl;
15118 goto do_shift;
15119
15120 case ASHIFTRT:
15121 op = DW_OP_shra;
15122 goto do_shift;
15123
15124 case LSHIFTRT:
15125 op = DW_OP_shr;
15126 goto do_shift;
15127
15128 do_shift:
15129 if (!is_a <scalar_int_mode> (mode, &int_mode))
15130 break;
15131 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15132 VAR_INIT_STATUS_INITIALIZED);
15133 {
15134 rtx rtlop1 = XEXP (rtl, 1);
15135 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15136 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15137 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15138 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15139 VAR_INIT_STATUS_INITIALIZED);
15140 }
15141
15142 if (op0 == 0 || op1 == 0)
15143 break;
15144
15145 mem_loc_result = op0;
15146 add_loc_descr (&mem_loc_result, op1);
15147 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15148 break;
15149
15150 case AND:
15151 op = DW_OP_and;
15152 goto do_binop;
15153
15154 case IOR:
15155 op = DW_OP_or;
15156 goto do_binop;
15157
15158 case XOR:
15159 op = DW_OP_xor;
15160 goto do_binop;
15161
15162 do_binop:
15163 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15164 VAR_INIT_STATUS_INITIALIZED);
15165 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15166 VAR_INIT_STATUS_INITIALIZED);
15167
15168 if (op0 == 0 || op1 == 0)
15169 break;
15170
15171 mem_loc_result = op0;
15172 add_loc_descr (&mem_loc_result, op1);
15173 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15174 break;
15175
15176 case MOD:
15177 if ((!dwarf_strict || dwarf_version >= 5)
15178 && is_a <scalar_int_mode> (mode, &int_mode)
15179 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15180 {
15181 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15182 base_type_for_mode (mode, 0),
15183 int_mode, mem_mode);
15184 break;
15185 }
15186
15187 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15188 VAR_INIT_STATUS_INITIALIZED);
15189 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15190 VAR_INIT_STATUS_INITIALIZED);
15191
15192 if (op0 == 0 || op1 == 0)
15193 break;
15194
15195 mem_loc_result = op0;
15196 add_loc_descr (&mem_loc_result, op1);
15197 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15198 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15199 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15200 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15201 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15202 break;
15203
15204 case UDIV:
15205 if ((!dwarf_strict || dwarf_version >= 5)
15206 && is_a <scalar_int_mode> (mode, &int_mode))
15207 {
15208 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15209 {
15210 op = DW_OP_div;
15211 goto do_binop;
15212 }
15213 mem_loc_result = typed_binop (DW_OP_div, rtl,
15214 base_type_for_mode (int_mode, 1),
15215 int_mode, mem_mode);
15216 }
15217 break;
15218
15219 case NOT:
15220 op = DW_OP_not;
15221 goto do_unop;
15222
15223 case ABS:
15224 op = DW_OP_abs;
15225 goto do_unop;
15226
15227 case NEG:
15228 op = DW_OP_neg;
15229 goto do_unop;
15230
15231 do_unop:
15232 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15233 VAR_INIT_STATUS_INITIALIZED);
15234
15235 if (op0 == 0)
15236 break;
15237
15238 mem_loc_result = op0;
15239 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15240 break;
15241
15242 case CONST_INT:
15243 if (!is_a <scalar_int_mode> (mode, &int_mode)
15244 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15245 #ifdef POINTERS_EXTEND_UNSIGNED
15246 || (int_mode == Pmode
15247 && mem_mode != VOIDmode
15248 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15249 #endif
15250 )
15251 {
15252 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15253 break;
15254 }
15255 if ((!dwarf_strict || dwarf_version >= 5)
15256 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15257 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15258 {
15259 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15260 scalar_int_mode amode;
15261 if (type_die == NULL)
15262 return NULL;
15263 if (INTVAL (rtl) >= 0
15264 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15265 .exists (&amode))
15266 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15267 /* const DW_OP_convert <XXX> vs.
15268 DW_OP_const_type <XXX, 1, const>. */
15269 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15270 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15271 {
15272 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15273 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15274 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15275 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15276 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15277 add_loc_descr (&mem_loc_result, op0);
15278 return mem_loc_result;
15279 }
15280 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15281 INTVAL (rtl));
15282 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15283 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15284 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15285 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15286 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15287 else
15288 {
15289 mem_loc_result->dw_loc_oprnd2.val_class
15290 = dw_val_class_const_double;
15291 mem_loc_result->dw_loc_oprnd2.v.val_double
15292 = double_int::from_shwi (INTVAL (rtl));
15293 }
15294 }
15295 break;
15296
15297 case CONST_DOUBLE:
15298 if (!dwarf_strict || dwarf_version >= 5)
15299 {
15300 dw_die_ref type_die;
15301
15302 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15303 CONST_DOUBLE rtx could represent either a large integer
15304 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15305 the value is always a floating point constant.
15306
15307 When it is an integer, a CONST_DOUBLE is used whenever
15308 the constant requires 2 HWIs to be adequately represented.
15309 We output CONST_DOUBLEs as blocks. */
15310 if (mode == VOIDmode
15311 || (GET_MODE (rtl) == VOIDmode
15312 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15313 break;
15314 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15315 if (type_die == NULL)
15316 return NULL;
15317 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15318 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15319 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15320 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15321 #if TARGET_SUPPORTS_WIDE_INT == 0
15322 if (!SCALAR_FLOAT_MODE_P (mode))
15323 {
15324 mem_loc_result->dw_loc_oprnd2.val_class
15325 = dw_val_class_const_double;
15326 mem_loc_result->dw_loc_oprnd2.v.val_double
15327 = rtx_to_double_int (rtl);
15328 }
15329 else
15330 #endif
15331 {
15332 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15333 unsigned int length = GET_MODE_SIZE (float_mode);
15334 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15335
15336 insert_float (rtl, array);
15337 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15338 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15339 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15340 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15341 }
15342 }
15343 break;
15344
15345 case CONST_WIDE_INT:
15346 if (!dwarf_strict || dwarf_version >= 5)
15347 {
15348 dw_die_ref type_die;
15349
15350 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15351 if (type_die == NULL)
15352 return NULL;
15353 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15354 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15355 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15356 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15357 mem_loc_result->dw_loc_oprnd2.val_class
15358 = dw_val_class_wide_int;
15359 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15360 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15361 }
15362 break;
15363
15364 case EQ:
15365 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15366 break;
15367
15368 case GE:
15369 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15370 break;
15371
15372 case GT:
15373 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15374 break;
15375
15376 case LE:
15377 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15378 break;
15379
15380 case LT:
15381 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15382 break;
15383
15384 case NE:
15385 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15386 break;
15387
15388 case GEU:
15389 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15390 break;
15391
15392 case GTU:
15393 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15394 break;
15395
15396 case LEU:
15397 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15398 break;
15399
15400 case LTU:
15401 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15402 break;
15403
15404 case UMIN:
15405 case UMAX:
15406 if (!SCALAR_INT_MODE_P (mode))
15407 break;
15408 /* FALLTHRU */
15409 case SMIN:
15410 case SMAX:
15411 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15412 break;
15413
15414 case ZERO_EXTRACT:
15415 case SIGN_EXTRACT:
15416 if (CONST_INT_P (XEXP (rtl, 1))
15417 && CONST_INT_P (XEXP (rtl, 2))
15418 && is_a <scalar_int_mode> (mode, &int_mode)
15419 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15420 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15421 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15422 && ((unsigned) INTVAL (XEXP (rtl, 1))
15423 + (unsigned) INTVAL (XEXP (rtl, 2))
15424 <= GET_MODE_BITSIZE (int_mode)))
15425 {
15426 int shift, size;
15427 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15428 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15429 if (op0 == 0)
15430 break;
15431 if (GET_CODE (rtl) == SIGN_EXTRACT)
15432 op = DW_OP_shra;
15433 else
15434 op = DW_OP_shr;
15435 mem_loc_result = op0;
15436 size = INTVAL (XEXP (rtl, 1));
15437 shift = INTVAL (XEXP (rtl, 2));
15438 if (BITS_BIG_ENDIAN)
15439 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15440 if (shift + size != (int) DWARF2_ADDR_SIZE)
15441 {
15442 add_loc_descr (&mem_loc_result,
15443 int_loc_descriptor (DWARF2_ADDR_SIZE
15444 - shift - size));
15445 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15446 }
15447 if (size != (int) DWARF2_ADDR_SIZE)
15448 {
15449 add_loc_descr (&mem_loc_result,
15450 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15451 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15452 }
15453 }
15454 break;
15455
15456 case IF_THEN_ELSE:
15457 {
15458 dw_loc_descr_ref op2, bra_node, drop_node;
15459 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15460 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15461 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15462 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15463 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15464 VAR_INIT_STATUS_INITIALIZED);
15465 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15466 VAR_INIT_STATUS_INITIALIZED);
15467 if (op0 == NULL || op1 == NULL || op2 == NULL)
15468 break;
15469
15470 mem_loc_result = op1;
15471 add_loc_descr (&mem_loc_result, op2);
15472 add_loc_descr (&mem_loc_result, op0);
15473 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15474 add_loc_descr (&mem_loc_result, bra_node);
15475 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15476 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15477 add_loc_descr (&mem_loc_result, drop_node);
15478 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15479 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15480 }
15481 break;
15482
15483 case FLOAT_EXTEND:
15484 case FLOAT_TRUNCATE:
15485 case FLOAT:
15486 case UNSIGNED_FLOAT:
15487 case FIX:
15488 case UNSIGNED_FIX:
15489 if (!dwarf_strict || dwarf_version >= 5)
15490 {
15491 dw_die_ref type_die;
15492 dw_loc_descr_ref cvt;
15493
15494 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15495 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15496 if (op0 == NULL)
15497 break;
15498 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15499 && (GET_CODE (rtl) == FLOAT
15500 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15501 {
15502 type_die = base_type_for_mode (int_mode,
15503 GET_CODE (rtl) == UNSIGNED_FLOAT);
15504 if (type_die == NULL)
15505 break;
15506 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15507 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15508 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15509 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15510 add_loc_descr (&op0, cvt);
15511 }
15512 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15513 if (type_die == NULL)
15514 break;
15515 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15516 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15517 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15518 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15519 add_loc_descr (&op0, cvt);
15520 if (is_a <scalar_int_mode> (mode, &int_mode)
15521 && (GET_CODE (rtl) == FIX
15522 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15523 {
15524 op0 = convert_descriptor_to_mode (int_mode, op0);
15525 if (op0 == NULL)
15526 break;
15527 }
15528 mem_loc_result = op0;
15529 }
15530 break;
15531
15532 case CLZ:
15533 case CTZ:
15534 case FFS:
15535 if (is_a <scalar_int_mode> (mode, &int_mode))
15536 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15537 break;
15538
15539 case POPCOUNT:
15540 case PARITY:
15541 if (is_a <scalar_int_mode> (mode, &int_mode))
15542 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15543 break;
15544
15545 case BSWAP:
15546 if (is_a <scalar_int_mode> (mode, &int_mode))
15547 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15548 break;
15549
15550 case ROTATE:
15551 case ROTATERT:
15552 if (is_a <scalar_int_mode> (mode, &int_mode))
15553 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15554 break;
15555
15556 case COMPARE:
15557 /* In theory, we could implement the above. */
15558 /* DWARF cannot represent the unsigned compare operations
15559 natively. */
15560 case SS_MULT:
15561 case US_MULT:
15562 case SS_DIV:
15563 case US_DIV:
15564 case SS_PLUS:
15565 case US_PLUS:
15566 case SS_MINUS:
15567 case US_MINUS:
15568 case SS_NEG:
15569 case US_NEG:
15570 case SS_ABS:
15571 case SS_ASHIFT:
15572 case US_ASHIFT:
15573 case SS_TRUNCATE:
15574 case US_TRUNCATE:
15575 case UNORDERED:
15576 case ORDERED:
15577 case UNEQ:
15578 case UNGE:
15579 case UNGT:
15580 case UNLE:
15581 case UNLT:
15582 case LTGT:
15583 case FRACT_CONVERT:
15584 case UNSIGNED_FRACT_CONVERT:
15585 case SAT_FRACT:
15586 case UNSIGNED_SAT_FRACT:
15587 case SQRT:
15588 case ASM_OPERANDS:
15589 case VEC_MERGE:
15590 case VEC_SELECT:
15591 case VEC_CONCAT:
15592 case VEC_DUPLICATE:
15593 case VEC_SERIES:
15594 case UNSPEC:
15595 case HIGH:
15596 case FMA:
15597 case STRICT_LOW_PART:
15598 case CONST_VECTOR:
15599 case CONST_FIXED:
15600 case CLRSB:
15601 case CLOBBER:
15602 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15603 can't express it in the debug info. This can happen e.g. with some
15604 TLS UNSPECs. */
15605 break;
15606
15607 case CONST_STRING:
15608 resolve_one_addr (&rtl);
15609 goto symref;
15610
15611 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15612 the expression. An UNSPEC rtx represents a raw DWARF operation,
15613 new_loc_descr is called for it to build the operation directly.
15614 Otherwise mem_loc_descriptor is called recursively. */
15615 case PARALLEL:
15616 {
15617 int index = 0;
15618 dw_loc_descr_ref exp_result = NULL;
15619
15620 for (; index < XVECLEN (rtl, 0); index++)
15621 {
15622 rtx elem = XVECEXP (rtl, 0, index);
15623 if (GET_CODE (elem) == UNSPEC)
15624 {
15625 /* Each DWARF operation UNSPEC contain two operands, if
15626 one operand is not used for the operation, const0_rtx is
15627 passed. */
15628 gcc_assert (XVECLEN (elem, 0) == 2);
15629
15630 HOST_WIDE_INT dw_op = XINT (elem, 1);
15631 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15632 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15633 exp_result
15634 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15635 oprnd2);
15636 }
15637 else
15638 exp_result
15639 = mem_loc_descriptor (elem, mode, mem_mode,
15640 VAR_INIT_STATUS_INITIALIZED);
15641
15642 if (!mem_loc_result)
15643 mem_loc_result = exp_result;
15644 else
15645 add_loc_descr (&mem_loc_result, exp_result);
15646 }
15647
15648 break;
15649 }
15650
15651 default:
15652 if (flag_checking)
15653 {
15654 print_rtl (stderr, rtl);
15655 gcc_unreachable ();
15656 }
15657 break;
15658 }
15659
15660 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15661 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15662
15663 return mem_loc_result;
15664 }
15665
15666 /* Return a descriptor that describes the concatenation of two locations.
15667 This is typically a complex variable. */
15668
15669 static dw_loc_descr_ref
15670 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15671 {
15672 dw_loc_descr_ref cc_loc_result = NULL;
15673 dw_loc_descr_ref x0_ref
15674 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15675 dw_loc_descr_ref x1_ref
15676 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15677
15678 if (x0_ref == 0 || x1_ref == 0)
15679 return 0;
15680
15681 cc_loc_result = x0_ref;
15682 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15683
15684 add_loc_descr (&cc_loc_result, x1_ref);
15685 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15686
15687 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15688 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15689
15690 return cc_loc_result;
15691 }
15692
15693 /* Return a descriptor that describes the concatenation of N
15694 locations. */
15695
15696 static dw_loc_descr_ref
15697 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15698 {
15699 unsigned int i;
15700 dw_loc_descr_ref cc_loc_result = NULL;
15701 unsigned int n = XVECLEN (concatn, 0);
15702
15703 for (i = 0; i < n; ++i)
15704 {
15705 dw_loc_descr_ref ref;
15706 rtx x = XVECEXP (concatn, 0, i);
15707
15708 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15709 if (ref == NULL)
15710 return NULL;
15711
15712 add_loc_descr (&cc_loc_result, ref);
15713 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15714 }
15715
15716 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15717 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15718
15719 return cc_loc_result;
15720 }
15721
15722 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15723 for DEBUG_IMPLICIT_PTR RTL. */
15724
15725 static dw_loc_descr_ref
15726 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15727 {
15728 dw_loc_descr_ref ret;
15729 dw_die_ref ref;
15730
15731 if (dwarf_strict && dwarf_version < 5)
15732 return NULL;
15733 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15734 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15735 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15736 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15737 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15738 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15739 if (ref)
15740 {
15741 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15742 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15743 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15744 }
15745 else
15746 {
15747 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15748 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15749 }
15750 return ret;
15751 }
15752
15753 /* Output a proper Dwarf location descriptor for a variable or parameter
15754 which is either allocated in a register or in a memory location. For a
15755 register, we just generate an OP_REG and the register number. For a
15756 memory location we provide a Dwarf postfix expression describing how to
15757 generate the (dynamic) address of the object onto the address stack.
15758
15759 MODE is mode of the decl if this loc_descriptor is going to be used in
15760 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15761 allowed, VOIDmode otherwise.
15762
15763 If we don't know how to describe it, return 0. */
15764
15765 static dw_loc_descr_ref
15766 loc_descriptor (rtx rtl, machine_mode mode,
15767 enum var_init_status initialized)
15768 {
15769 dw_loc_descr_ref loc_result = NULL;
15770 scalar_int_mode int_mode;
15771
15772 switch (GET_CODE (rtl))
15773 {
15774 case SUBREG:
15775 /* The case of a subreg may arise when we have a local (register)
15776 variable or a formal (register) parameter which doesn't quite fill
15777 up an entire register. For now, just assume that it is
15778 legitimate to make the Dwarf info refer to the whole register which
15779 contains the given subreg. */
15780 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15781 loc_result = loc_descriptor (SUBREG_REG (rtl),
15782 GET_MODE (SUBREG_REG (rtl)), initialized);
15783 else
15784 goto do_default;
15785 break;
15786
15787 case REG:
15788 loc_result = reg_loc_descriptor (rtl, initialized);
15789 break;
15790
15791 case MEM:
15792 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15793 GET_MODE (rtl), initialized);
15794 if (loc_result == NULL)
15795 loc_result = tls_mem_loc_descriptor (rtl);
15796 if (loc_result == NULL)
15797 {
15798 rtx new_rtl = avoid_constant_pool_reference (rtl);
15799 if (new_rtl != rtl)
15800 loc_result = loc_descriptor (new_rtl, mode, initialized);
15801 }
15802 break;
15803
15804 case CONCAT:
15805 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15806 initialized);
15807 break;
15808
15809 case CONCATN:
15810 loc_result = concatn_loc_descriptor (rtl, initialized);
15811 break;
15812
15813 case VAR_LOCATION:
15814 /* Single part. */
15815 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15816 {
15817 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15818 if (GET_CODE (loc) == EXPR_LIST)
15819 loc = XEXP (loc, 0);
15820 loc_result = loc_descriptor (loc, mode, initialized);
15821 break;
15822 }
15823
15824 rtl = XEXP (rtl, 1);
15825 /* FALLTHRU */
15826
15827 case PARALLEL:
15828 {
15829 rtvec par_elems = XVEC (rtl, 0);
15830 int num_elem = GET_NUM_ELEM (par_elems);
15831 machine_mode mode;
15832 int i;
15833
15834 /* Create the first one, so we have something to add to. */
15835 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15836 VOIDmode, initialized);
15837 if (loc_result == NULL)
15838 return NULL;
15839 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15840 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15841 for (i = 1; i < num_elem; i++)
15842 {
15843 dw_loc_descr_ref temp;
15844
15845 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15846 VOIDmode, initialized);
15847 if (temp == NULL)
15848 return NULL;
15849 add_loc_descr (&loc_result, temp);
15850 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15851 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15852 }
15853 }
15854 break;
15855
15856 case CONST_INT:
15857 if (mode != VOIDmode && mode != BLKmode)
15858 {
15859 int_mode = as_a <scalar_int_mode> (mode);
15860 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15861 INTVAL (rtl));
15862 }
15863 break;
15864
15865 case CONST_DOUBLE:
15866 if (mode == VOIDmode)
15867 mode = GET_MODE (rtl);
15868
15869 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15870 {
15871 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15872
15873 /* Note that a CONST_DOUBLE rtx could represent either an integer
15874 or a floating-point constant. A CONST_DOUBLE is used whenever
15875 the constant requires more than one word in order to be
15876 adequately represented. We output CONST_DOUBLEs as blocks. */
15877 scalar_mode smode = as_a <scalar_mode> (mode);
15878 loc_result = new_loc_descr (DW_OP_implicit_value,
15879 GET_MODE_SIZE (smode), 0);
15880 #if TARGET_SUPPORTS_WIDE_INT == 0
15881 if (!SCALAR_FLOAT_MODE_P (smode))
15882 {
15883 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15884 loc_result->dw_loc_oprnd2.v.val_double
15885 = rtx_to_double_int (rtl);
15886 }
15887 else
15888 #endif
15889 {
15890 unsigned int length = GET_MODE_SIZE (smode);
15891 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15892
15893 insert_float (rtl, array);
15894 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15895 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15896 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15897 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15898 }
15899 }
15900 break;
15901
15902 case CONST_WIDE_INT:
15903 if (mode == VOIDmode)
15904 mode = GET_MODE (rtl);
15905
15906 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15907 {
15908 int_mode = as_a <scalar_int_mode> (mode);
15909 loc_result = new_loc_descr (DW_OP_implicit_value,
15910 GET_MODE_SIZE (int_mode), 0);
15911 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15912 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15913 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15914 }
15915 break;
15916
15917 case CONST_VECTOR:
15918 if (mode == VOIDmode)
15919 mode = GET_MODE (rtl);
15920
15921 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15922 {
15923 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15924 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15925 unsigned char *array
15926 = ggc_vec_alloc<unsigned char> (length * elt_size);
15927 unsigned int i;
15928 unsigned char *p;
15929 machine_mode imode = GET_MODE_INNER (mode);
15930
15931 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15932 switch (GET_MODE_CLASS (mode))
15933 {
15934 case MODE_VECTOR_INT:
15935 for (i = 0, p = array; i < length; i++, p += elt_size)
15936 {
15937 rtx elt = CONST_VECTOR_ELT (rtl, i);
15938 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15939 }
15940 break;
15941
15942 case MODE_VECTOR_FLOAT:
15943 for (i = 0, p = array; i < length; i++, p += elt_size)
15944 {
15945 rtx elt = CONST_VECTOR_ELT (rtl, i);
15946 insert_float (elt, p);
15947 }
15948 break;
15949
15950 default:
15951 gcc_unreachable ();
15952 }
15953
15954 loc_result = new_loc_descr (DW_OP_implicit_value,
15955 length * elt_size, 0);
15956 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15957 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15958 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15959 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15960 }
15961 break;
15962
15963 case CONST:
15964 if (mode == VOIDmode
15965 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15966 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15967 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15968 {
15969 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15970 break;
15971 }
15972 /* FALLTHROUGH */
15973 case SYMBOL_REF:
15974 if (!const_ok_for_output (rtl))
15975 break;
15976 /* FALLTHROUGH */
15977 case LABEL_REF:
15978 if (is_a <scalar_int_mode> (mode, &int_mode)
15979 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15980 && (dwarf_version >= 4 || !dwarf_strict))
15981 {
15982 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15983 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15984 vec_safe_push (used_rtx_array, rtl);
15985 }
15986 break;
15987
15988 case DEBUG_IMPLICIT_PTR:
15989 loc_result = implicit_ptr_descriptor (rtl, 0);
15990 break;
15991
15992 case PLUS:
15993 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15994 && CONST_INT_P (XEXP (rtl, 1)))
15995 {
15996 loc_result
15997 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15998 break;
15999 }
16000 /* FALLTHRU */
16001 do_default:
16002 default:
16003 if ((is_a <scalar_int_mode> (mode, &int_mode)
16004 && GET_MODE (rtl) == int_mode
16005 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16006 && dwarf_version >= 4)
16007 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16008 {
16009 /* Value expression. */
16010 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16011 if (loc_result)
16012 add_loc_descr (&loc_result,
16013 new_loc_descr (DW_OP_stack_value, 0, 0));
16014 }
16015 break;
16016 }
16017
16018 return loc_result;
16019 }
16020
16021 /* We need to figure out what section we should use as the base for the
16022 address ranges where a given location is valid.
16023 1. If this particular DECL has a section associated with it, use that.
16024 2. If this function has a section associated with it, use that.
16025 3. Otherwise, use the text section.
16026 XXX: If you split a variable across multiple sections, we won't notice. */
16027
16028 static const char *
16029 secname_for_decl (const_tree decl)
16030 {
16031 const char *secname;
16032
16033 if (VAR_OR_FUNCTION_DECL_P (decl)
16034 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16035 && DECL_SECTION_NAME (decl))
16036 secname = DECL_SECTION_NAME (decl);
16037 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16038 secname = DECL_SECTION_NAME (current_function_decl);
16039 else if (cfun && in_cold_section_p)
16040 secname = crtl->subsections.cold_section_label;
16041 else
16042 secname = text_section_label;
16043
16044 return secname;
16045 }
16046
16047 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16048
16049 static bool
16050 decl_by_reference_p (tree decl)
16051 {
16052 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16053 || VAR_P (decl))
16054 && DECL_BY_REFERENCE (decl));
16055 }
16056
16057 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16058 for VARLOC. */
16059
16060 static dw_loc_descr_ref
16061 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16062 enum var_init_status initialized)
16063 {
16064 int have_address = 0;
16065 dw_loc_descr_ref descr;
16066 machine_mode mode;
16067
16068 if (want_address != 2)
16069 {
16070 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16071 /* Single part. */
16072 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16073 {
16074 varloc = PAT_VAR_LOCATION_LOC (varloc);
16075 if (GET_CODE (varloc) == EXPR_LIST)
16076 varloc = XEXP (varloc, 0);
16077 mode = GET_MODE (varloc);
16078 if (MEM_P (varloc))
16079 {
16080 rtx addr = XEXP (varloc, 0);
16081 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16082 mode, initialized);
16083 if (descr)
16084 have_address = 1;
16085 else
16086 {
16087 rtx x = avoid_constant_pool_reference (varloc);
16088 if (x != varloc)
16089 descr = mem_loc_descriptor (x, mode, VOIDmode,
16090 initialized);
16091 }
16092 }
16093 else
16094 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16095 }
16096 else
16097 return 0;
16098 }
16099 else
16100 {
16101 if (GET_CODE (varloc) == VAR_LOCATION)
16102 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16103 else
16104 mode = DECL_MODE (loc);
16105 descr = loc_descriptor (varloc, mode, initialized);
16106 have_address = 1;
16107 }
16108
16109 if (!descr)
16110 return 0;
16111
16112 if (want_address == 2 && !have_address
16113 && (dwarf_version >= 4 || !dwarf_strict))
16114 {
16115 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16116 {
16117 expansion_failed (loc, NULL_RTX,
16118 "DWARF address size mismatch");
16119 return 0;
16120 }
16121 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16122 have_address = 1;
16123 }
16124 /* Show if we can't fill the request for an address. */
16125 if (want_address && !have_address)
16126 {
16127 expansion_failed (loc, NULL_RTX,
16128 "Want address and only have value");
16129 return 0;
16130 }
16131
16132 /* If we've got an address and don't want one, dereference. */
16133 if (!want_address && have_address)
16134 {
16135 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16136 enum dwarf_location_atom op;
16137
16138 if (size > DWARF2_ADDR_SIZE || size == -1)
16139 {
16140 expansion_failed (loc, NULL_RTX,
16141 "DWARF address size mismatch");
16142 return 0;
16143 }
16144 else if (size == DWARF2_ADDR_SIZE)
16145 op = DW_OP_deref;
16146 else
16147 op = DW_OP_deref_size;
16148
16149 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16150 }
16151
16152 return descr;
16153 }
16154
16155 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16156 if it is not possible. */
16157
16158 static dw_loc_descr_ref
16159 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16160 {
16161 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16162 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16163 else if (dwarf_version >= 3 || !dwarf_strict)
16164 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16165 else
16166 return NULL;
16167 }
16168
16169 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16170 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16171
16172 static dw_loc_descr_ref
16173 dw_sra_loc_expr (tree decl, rtx loc)
16174 {
16175 rtx p;
16176 unsigned HOST_WIDE_INT padsize = 0;
16177 dw_loc_descr_ref descr, *descr_tail;
16178 unsigned HOST_WIDE_INT decl_size;
16179 rtx varloc;
16180 enum var_init_status initialized;
16181
16182 if (DECL_SIZE (decl) == NULL
16183 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16184 return NULL;
16185
16186 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16187 descr = NULL;
16188 descr_tail = &descr;
16189
16190 for (p = loc; p; p = XEXP (p, 1))
16191 {
16192 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16193 rtx loc_note = *decl_piece_varloc_ptr (p);
16194 dw_loc_descr_ref cur_descr;
16195 dw_loc_descr_ref *tail, last = NULL;
16196 unsigned HOST_WIDE_INT opsize = 0;
16197
16198 if (loc_note == NULL_RTX
16199 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16200 {
16201 padsize += bitsize;
16202 continue;
16203 }
16204 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16205 varloc = NOTE_VAR_LOCATION (loc_note);
16206 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16207 if (cur_descr == NULL)
16208 {
16209 padsize += bitsize;
16210 continue;
16211 }
16212
16213 /* Check that cur_descr either doesn't use
16214 DW_OP_*piece operations, or their sum is equal
16215 to bitsize. Otherwise we can't embed it. */
16216 for (tail = &cur_descr; *tail != NULL;
16217 tail = &(*tail)->dw_loc_next)
16218 if ((*tail)->dw_loc_opc == DW_OP_piece)
16219 {
16220 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16221 * BITS_PER_UNIT;
16222 last = *tail;
16223 }
16224 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16225 {
16226 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16227 last = *tail;
16228 }
16229
16230 if (last != NULL && opsize != bitsize)
16231 {
16232 padsize += bitsize;
16233 /* Discard the current piece of the descriptor and release any
16234 addr_table entries it uses. */
16235 remove_loc_list_addr_table_entries (cur_descr);
16236 continue;
16237 }
16238
16239 /* If there is a hole, add DW_OP_*piece after empty DWARF
16240 expression, which means that those bits are optimized out. */
16241 if (padsize)
16242 {
16243 if (padsize > decl_size)
16244 {
16245 remove_loc_list_addr_table_entries (cur_descr);
16246 goto discard_descr;
16247 }
16248 decl_size -= padsize;
16249 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16250 if (*descr_tail == NULL)
16251 {
16252 remove_loc_list_addr_table_entries (cur_descr);
16253 goto discard_descr;
16254 }
16255 descr_tail = &(*descr_tail)->dw_loc_next;
16256 padsize = 0;
16257 }
16258 *descr_tail = cur_descr;
16259 descr_tail = tail;
16260 if (bitsize > decl_size)
16261 goto discard_descr;
16262 decl_size -= bitsize;
16263 if (last == NULL)
16264 {
16265 HOST_WIDE_INT offset = 0;
16266 if (GET_CODE (varloc) == VAR_LOCATION
16267 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16268 {
16269 varloc = PAT_VAR_LOCATION_LOC (varloc);
16270 if (GET_CODE (varloc) == EXPR_LIST)
16271 varloc = XEXP (varloc, 0);
16272 }
16273 do
16274 {
16275 if (GET_CODE (varloc) == CONST
16276 || GET_CODE (varloc) == SIGN_EXTEND
16277 || GET_CODE (varloc) == ZERO_EXTEND)
16278 varloc = XEXP (varloc, 0);
16279 else if (GET_CODE (varloc) == SUBREG)
16280 varloc = SUBREG_REG (varloc);
16281 else
16282 break;
16283 }
16284 while (1);
16285 /* DW_OP_bit_size offset should be zero for register
16286 or implicit location descriptions and empty location
16287 descriptions, but for memory addresses needs big endian
16288 adjustment. */
16289 if (MEM_P (varloc))
16290 {
16291 unsigned HOST_WIDE_INT memsize
16292 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16293 if (memsize != bitsize)
16294 {
16295 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16296 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16297 goto discard_descr;
16298 if (memsize < bitsize)
16299 goto discard_descr;
16300 if (BITS_BIG_ENDIAN)
16301 offset = memsize - bitsize;
16302 }
16303 }
16304
16305 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16306 if (*descr_tail == NULL)
16307 goto discard_descr;
16308 descr_tail = &(*descr_tail)->dw_loc_next;
16309 }
16310 }
16311
16312 /* If there were any non-empty expressions, add padding till the end of
16313 the decl. */
16314 if (descr != NULL && decl_size != 0)
16315 {
16316 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16317 if (*descr_tail == NULL)
16318 goto discard_descr;
16319 }
16320 return descr;
16321
16322 discard_descr:
16323 /* Discard the descriptor and release any addr_table entries it uses. */
16324 remove_loc_list_addr_table_entries (descr);
16325 return NULL;
16326 }
16327
16328 /* Return the dwarf representation of the location list LOC_LIST of
16329 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16330 function. */
16331
16332 static dw_loc_list_ref
16333 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16334 {
16335 const char *endname, *secname;
16336 rtx varloc;
16337 enum var_init_status initialized;
16338 struct var_loc_node *node;
16339 dw_loc_descr_ref descr;
16340 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16341 dw_loc_list_ref list = NULL;
16342 dw_loc_list_ref *listp = &list;
16343
16344 /* Now that we know what section we are using for a base,
16345 actually construct the list of locations.
16346 The first location information is what is passed to the
16347 function that creates the location list, and the remaining
16348 locations just get added on to that list.
16349 Note that we only know the start address for a location
16350 (IE location changes), so to build the range, we use
16351 the range [current location start, next location start].
16352 This means we have to special case the last node, and generate
16353 a range of [last location start, end of function label]. */
16354
16355 if (cfun && crtl->has_bb_partition)
16356 {
16357 bool save_in_cold_section_p = in_cold_section_p;
16358 in_cold_section_p = first_function_block_is_cold;
16359 if (loc_list->last_before_switch == NULL)
16360 in_cold_section_p = !in_cold_section_p;
16361 secname = secname_for_decl (decl);
16362 in_cold_section_p = save_in_cold_section_p;
16363 }
16364 else
16365 secname = secname_for_decl (decl);
16366
16367 for (node = loc_list->first; node; node = node->next)
16368 {
16369 bool range_across_switch = false;
16370 if (GET_CODE (node->loc) == EXPR_LIST
16371 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16372 {
16373 if (GET_CODE (node->loc) == EXPR_LIST)
16374 {
16375 descr = NULL;
16376 /* This requires DW_OP_{,bit_}piece, which is not usable
16377 inside DWARF expressions. */
16378 if (want_address == 2)
16379 descr = dw_sra_loc_expr (decl, node->loc);
16380 }
16381 else
16382 {
16383 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16384 varloc = NOTE_VAR_LOCATION (node->loc);
16385 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16386 }
16387 if (descr)
16388 {
16389 /* If section switch happens in between node->label
16390 and node->next->label (or end of function) and
16391 we can't emit it as a single entry list,
16392 emit two ranges, first one ending at the end
16393 of first partition and second one starting at the
16394 beginning of second partition. */
16395 if (node == loc_list->last_before_switch
16396 && (node != loc_list->first || loc_list->first->next)
16397 && current_function_decl)
16398 {
16399 endname = cfun->fde->dw_fde_end;
16400 range_across_switch = true;
16401 }
16402 /* The variable has a location between NODE->LABEL and
16403 NODE->NEXT->LABEL. */
16404 else if (node->next)
16405 endname = node->next->label;
16406 /* If the variable has a location at the last label
16407 it keeps its location until the end of function. */
16408 else if (!current_function_decl)
16409 endname = text_end_label;
16410 else
16411 {
16412 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16413 current_function_funcdef_no);
16414 endname = ggc_strdup (label_id);
16415 }
16416
16417 *listp = new_loc_list (descr, node->label, endname, secname);
16418 if (TREE_CODE (decl) == PARM_DECL
16419 && node == loc_list->first
16420 && NOTE_P (node->loc)
16421 && strcmp (node->label, endname) == 0)
16422 (*listp)->force = true;
16423 listp = &(*listp)->dw_loc_next;
16424 }
16425 }
16426
16427 if (cfun
16428 && crtl->has_bb_partition
16429 && node == loc_list->last_before_switch)
16430 {
16431 bool save_in_cold_section_p = in_cold_section_p;
16432 in_cold_section_p = !first_function_block_is_cold;
16433 secname = secname_for_decl (decl);
16434 in_cold_section_p = save_in_cold_section_p;
16435 }
16436
16437 if (range_across_switch)
16438 {
16439 if (GET_CODE (node->loc) == EXPR_LIST)
16440 descr = dw_sra_loc_expr (decl, node->loc);
16441 else
16442 {
16443 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16444 varloc = NOTE_VAR_LOCATION (node->loc);
16445 descr = dw_loc_list_1 (decl, varloc, want_address,
16446 initialized);
16447 }
16448 gcc_assert (descr);
16449 /* The variable has a location between NODE->LABEL and
16450 NODE->NEXT->LABEL. */
16451 if (node->next)
16452 endname = node->next->label;
16453 else
16454 endname = cfun->fde->dw_fde_second_end;
16455 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin,
16456 endname, secname);
16457 listp = &(*listp)->dw_loc_next;
16458 }
16459 }
16460
16461 /* Try to avoid the overhead of a location list emitting a location
16462 expression instead, but only if we didn't have more than one
16463 location entry in the first place. If some entries were not
16464 representable, we don't want to pretend a single entry that was
16465 applies to the entire scope in which the variable is
16466 available. */
16467 if (list && loc_list->first->next)
16468 gen_llsym (list);
16469
16470 return list;
16471 }
16472
16473 /* Return if the loc_list has only single element and thus can be represented
16474 as location description. */
16475
16476 static bool
16477 single_element_loc_list_p (dw_loc_list_ref list)
16478 {
16479 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16480 return !list->ll_symbol;
16481 }
16482
16483 /* Duplicate a single element of location list. */
16484
16485 static inline dw_loc_descr_ref
16486 copy_loc_descr (dw_loc_descr_ref ref)
16487 {
16488 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16489 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16490 return copy;
16491 }
16492
16493 /* To each location in list LIST append loc descr REF. */
16494
16495 static void
16496 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16497 {
16498 dw_loc_descr_ref copy;
16499 add_loc_descr (&list->expr, ref);
16500 list = list->dw_loc_next;
16501 while (list)
16502 {
16503 copy = copy_loc_descr (ref);
16504 add_loc_descr (&list->expr, copy);
16505 while (copy->dw_loc_next)
16506 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16507 list = list->dw_loc_next;
16508 }
16509 }
16510
16511 /* To each location in list LIST prepend loc descr REF. */
16512
16513 static void
16514 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16515 {
16516 dw_loc_descr_ref copy;
16517 dw_loc_descr_ref ref_end = list->expr;
16518 add_loc_descr (&ref, list->expr);
16519 list->expr = ref;
16520 list = list->dw_loc_next;
16521 while (list)
16522 {
16523 dw_loc_descr_ref end = list->expr;
16524 list->expr = copy = copy_loc_descr (ref);
16525 while (copy->dw_loc_next != ref_end)
16526 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16527 copy->dw_loc_next = end;
16528 list = list->dw_loc_next;
16529 }
16530 }
16531
16532 /* Given two lists RET and LIST
16533 produce location list that is result of adding expression in LIST
16534 to expression in RET on each position in program.
16535 Might be destructive on both RET and LIST.
16536
16537 TODO: We handle only simple cases of RET or LIST having at most one
16538 element. General case would involve sorting the lists in program order
16539 and merging them that will need some additional work.
16540 Adding that will improve quality of debug info especially for SRA-ed
16541 structures. */
16542
16543 static void
16544 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16545 {
16546 if (!list)
16547 return;
16548 if (!*ret)
16549 {
16550 *ret = list;
16551 return;
16552 }
16553 if (!list->dw_loc_next)
16554 {
16555 add_loc_descr_to_each (*ret, list->expr);
16556 return;
16557 }
16558 if (!(*ret)->dw_loc_next)
16559 {
16560 prepend_loc_descr_to_each (list, (*ret)->expr);
16561 *ret = list;
16562 return;
16563 }
16564 expansion_failed (NULL_TREE, NULL_RTX,
16565 "Don't know how to merge two non-trivial"
16566 " location lists.\n");
16567 *ret = NULL;
16568 return;
16569 }
16570
16571 /* LOC is constant expression. Try a luck, look it up in constant
16572 pool and return its loc_descr of its address. */
16573
16574 static dw_loc_descr_ref
16575 cst_pool_loc_descr (tree loc)
16576 {
16577 /* Get an RTL for this, if something has been emitted. */
16578 rtx rtl = lookup_constant_def (loc);
16579
16580 if (!rtl || !MEM_P (rtl))
16581 {
16582 gcc_assert (!rtl);
16583 return 0;
16584 }
16585 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16586
16587 /* TODO: We might get more coverage if we was actually delaying expansion
16588 of all expressions till end of compilation when constant pools are fully
16589 populated. */
16590 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16591 {
16592 expansion_failed (loc, NULL_RTX,
16593 "CST value in contant pool but not marked.");
16594 return 0;
16595 }
16596 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16597 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16598 }
16599
16600 /* Return dw_loc_list representing address of addr_expr LOC
16601 by looking for inner INDIRECT_REF expression and turning
16602 it into simple arithmetics.
16603
16604 See loc_list_from_tree for the meaning of CONTEXT. */
16605
16606 static dw_loc_list_ref
16607 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16608 loc_descr_context *context)
16609 {
16610 tree obj, offset;
16611 HOST_WIDE_INT bitsize, bitpos, bytepos;
16612 machine_mode mode;
16613 int unsignedp, reversep, volatilep = 0;
16614 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16615
16616 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16617 &bitsize, &bitpos, &offset, &mode,
16618 &unsignedp, &reversep, &volatilep);
16619 STRIP_NOPS (obj);
16620 if (bitpos % BITS_PER_UNIT)
16621 {
16622 expansion_failed (loc, NULL_RTX, "bitfield access");
16623 return 0;
16624 }
16625 if (!INDIRECT_REF_P (obj))
16626 {
16627 expansion_failed (obj,
16628 NULL_RTX, "no indirect ref in inner refrence");
16629 return 0;
16630 }
16631 if (!offset && !bitpos)
16632 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16633 context);
16634 else if (toplev
16635 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16636 && (dwarf_version >= 4 || !dwarf_strict))
16637 {
16638 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16639 if (!list_ret)
16640 return 0;
16641 if (offset)
16642 {
16643 /* Variable offset. */
16644 list_ret1 = loc_list_from_tree (offset, 0, context);
16645 if (list_ret1 == 0)
16646 return 0;
16647 add_loc_list (&list_ret, list_ret1);
16648 if (!list_ret)
16649 return 0;
16650 add_loc_descr_to_each (list_ret,
16651 new_loc_descr (DW_OP_plus, 0, 0));
16652 }
16653 bytepos = bitpos / BITS_PER_UNIT;
16654 if (bytepos > 0)
16655 add_loc_descr_to_each (list_ret,
16656 new_loc_descr (DW_OP_plus_uconst,
16657 bytepos, 0));
16658 else if (bytepos < 0)
16659 loc_list_plus_const (list_ret, bytepos);
16660 add_loc_descr_to_each (list_ret,
16661 new_loc_descr (DW_OP_stack_value, 0, 0));
16662 }
16663 return list_ret;
16664 }
16665
16666 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16667 all operations from LOC are nops, move to the last one. Insert in NOPS all
16668 operations that are skipped. */
16669
16670 static void
16671 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16672 hash_set<dw_loc_descr_ref> &nops)
16673 {
16674 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16675 {
16676 nops.add (loc);
16677 loc = loc->dw_loc_next;
16678 }
16679 }
16680
16681 /* Helper for loc_descr_without_nops: free the location description operation
16682 P. */
16683
16684 bool
16685 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16686 {
16687 ggc_free (loc);
16688 return true;
16689 }
16690
16691 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16692 finishes LOC. */
16693
16694 static void
16695 loc_descr_without_nops (dw_loc_descr_ref &loc)
16696 {
16697 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16698 return;
16699
16700 /* Set of all DW_OP_nop operations we remove. */
16701 hash_set<dw_loc_descr_ref> nops;
16702
16703 /* First, strip all prefix NOP operations in order to keep the head of the
16704 operations list. */
16705 loc_descr_to_next_no_nop (loc, nops);
16706
16707 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16708 {
16709 /* For control flow operations: strip "prefix" nops in destination
16710 labels. */
16711 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16712 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16713 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16714 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16715
16716 /* Do the same for the operations that follow, then move to the next
16717 iteration. */
16718 if (cur->dw_loc_next != NULL)
16719 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16720 cur = cur->dw_loc_next;
16721 }
16722
16723 nops.traverse<void *, free_loc_descr> (NULL);
16724 }
16725
16726
16727 struct dwarf_procedure_info;
16728
16729 /* Helper structure for location descriptions generation. */
16730 struct loc_descr_context
16731 {
16732 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16733 NULL_TREE if DW_OP_push_object_address in invalid for this location
16734 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16735 tree context_type;
16736 /* The ..._DECL node that should be translated as a
16737 DW_OP_push_object_address operation. */
16738 tree base_decl;
16739 /* Information about the DWARF procedure we are currently generating. NULL if
16740 we are not generating a DWARF procedure. */
16741 struct dwarf_procedure_info *dpi;
16742 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16743 by consumer. Used for DW_TAG_generic_subrange attributes. */
16744 bool placeholder_arg;
16745 /* True if PLACEHOLDER_EXPR has been seen. */
16746 bool placeholder_seen;
16747 };
16748
16749 /* DWARF procedures generation
16750
16751 DWARF expressions (aka. location descriptions) are used to encode variable
16752 things such as sizes or offsets. Such computations can have redundant parts
16753 that can be factorized in order to reduce the size of the output debug
16754 information. This is the whole point of DWARF procedures.
16755
16756 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16757 already factorized into functions ("size functions") in order to handle very
16758 big and complex types. Such functions are quite simple: they have integral
16759 arguments, they return an integral result and their body contains only a
16760 return statement with arithmetic expressions. This is the only kind of
16761 function we are interested in translating into DWARF procedures, here.
16762
16763 DWARF expressions and DWARF procedure are executed using a stack, so we have
16764 to define some calling convention for them to interact. Let's say that:
16765
16766 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16767 all arguments in reverse order (right-to-left) so that when the DWARF
16768 procedure execution starts, the first argument is the top of the stack.
16769
16770 - Then, when returning, the DWARF procedure must have consumed all arguments
16771 on the stack, must have pushed the result and touched nothing else.
16772
16773 - Each integral argument and the result are integral types can be hold in a
16774 single stack slot.
16775
16776 - We call "frame offset" the number of stack slots that are "under DWARF
16777 procedure control": it includes the arguments slots, the temporaries and
16778 the result slot. Thus, it is equal to the number of arguments when the
16779 procedure execution starts and must be equal to one (the result) when it
16780 returns. */
16781
16782 /* Helper structure used when generating operations for a DWARF procedure. */
16783 struct dwarf_procedure_info
16784 {
16785 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16786 currently translated. */
16787 tree fndecl;
16788 /* The number of arguments FNDECL takes. */
16789 unsigned args_count;
16790 };
16791
16792 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16793 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16794 equate it to this DIE. */
16795
16796 static dw_die_ref
16797 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16798 dw_die_ref parent_die)
16799 {
16800 dw_die_ref dwarf_proc_die;
16801
16802 if ((dwarf_version < 3 && dwarf_strict)
16803 || location == NULL)
16804 return NULL;
16805
16806 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16807 if (fndecl)
16808 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16809 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16810 return dwarf_proc_die;
16811 }
16812
16813 /* Return whether TYPE is a supported type as a DWARF procedure argument
16814 type or return type (we handle only scalar types and pointer types that
16815 aren't wider than the DWARF expression evaluation stack. */
16816
16817 static bool
16818 is_handled_procedure_type (tree type)
16819 {
16820 return ((INTEGRAL_TYPE_P (type)
16821 || TREE_CODE (type) == OFFSET_TYPE
16822 || TREE_CODE (type) == POINTER_TYPE)
16823 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16824 }
16825
16826 /* Helper for resolve_args_picking: do the same but stop when coming across
16827 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16828 offset *before* evaluating the corresponding operation. */
16829
16830 static bool
16831 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16832 struct dwarf_procedure_info *dpi,
16833 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16834 {
16835 /* The "frame_offset" identifier is already used to name a macro... */
16836 unsigned frame_offset_ = initial_frame_offset;
16837 dw_loc_descr_ref l;
16838
16839 for (l = loc; l != NULL;)
16840 {
16841 bool existed;
16842 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16843
16844 /* If we already met this node, there is nothing to compute anymore. */
16845 if (existed)
16846 {
16847 /* Make sure that the stack size is consistent wherever the execution
16848 flow comes from. */
16849 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16850 break;
16851 }
16852 l_frame_offset = frame_offset_;
16853
16854 /* If needed, relocate the picking offset with respect to the frame
16855 offset. */
16856 if (l->frame_offset_rel)
16857 {
16858 unsigned HOST_WIDE_INT off;
16859 switch (l->dw_loc_opc)
16860 {
16861 case DW_OP_pick:
16862 off = l->dw_loc_oprnd1.v.val_unsigned;
16863 break;
16864 case DW_OP_dup:
16865 off = 0;
16866 break;
16867 case DW_OP_over:
16868 off = 1;
16869 break;
16870 default:
16871 gcc_unreachable ();
16872 }
16873 /* frame_offset_ is the size of the current stack frame, including
16874 incoming arguments. Besides, the arguments are pushed
16875 right-to-left. Thus, in order to access the Nth argument from
16876 this operation node, the picking has to skip temporaries *plus*
16877 one stack slot per argument (0 for the first one, 1 for the second
16878 one, etc.).
16879
16880 The targetted argument number (N) is already set as the operand,
16881 and the number of temporaries can be computed with:
16882 frame_offsets_ - dpi->args_count */
16883 off += frame_offset_ - dpi->args_count;
16884
16885 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16886 if (off > 255)
16887 return false;
16888
16889 if (off == 0)
16890 {
16891 l->dw_loc_opc = DW_OP_dup;
16892 l->dw_loc_oprnd1.v.val_unsigned = 0;
16893 }
16894 else if (off == 1)
16895 {
16896 l->dw_loc_opc = DW_OP_over;
16897 l->dw_loc_oprnd1.v.val_unsigned = 0;
16898 }
16899 else
16900 {
16901 l->dw_loc_opc = DW_OP_pick;
16902 l->dw_loc_oprnd1.v.val_unsigned = off;
16903 }
16904 }
16905
16906 /* Update frame_offset according to the effect the current operation has
16907 on the stack. */
16908 switch (l->dw_loc_opc)
16909 {
16910 case DW_OP_deref:
16911 case DW_OP_swap:
16912 case DW_OP_rot:
16913 case DW_OP_abs:
16914 case DW_OP_neg:
16915 case DW_OP_not:
16916 case DW_OP_plus_uconst:
16917 case DW_OP_skip:
16918 case DW_OP_reg0:
16919 case DW_OP_reg1:
16920 case DW_OP_reg2:
16921 case DW_OP_reg3:
16922 case DW_OP_reg4:
16923 case DW_OP_reg5:
16924 case DW_OP_reg6:
16925 case DW_OP_reg7:
16926 case DW_OP_reg8:
16927 case DW_OP_reg9:
16928 case DW_OP_reg10:
16929 case DW_OP_reg11:
16930 case DW_OP_reg12:
16931 case DW_OP_reg13:
16932 case DW_OP_reg14:
16933 case DW_OP_reg15:
16934 case DW_OP_reg16:
16935 case DW_OP_reg17:
16936 case DW_OP_reg18:
16937 case DW_OP_reg19:
16938 case DW_OP_reg20:
16939 case DW_OP_reg21:
16940 case DW_OP_reg22:
16941 case DW_OP_reg23:
16942 case DW_OP_reg24:
16943 case DW_OP_reg25:
16944 case DW_OP_reg26:
16945 case DW_OP_reg27:
16946 case DW_OP_reg28:
16947 case DW_OP_reg29:
16948 case DW_OP_reg30:
16949 case DW_OP_reg31:
16950 case DW_OP_bregx:
16951 case DW_OP_piece:
16952 case DW_OP_deref_size:
16953 case DW_OP_nop:
16954 case DW_OP_bit_piece:
16955 case DW_OP_implicit_value:
16956 case DW_OP_stack_value:
16957 break;
16958
16959 case DW_OP_addr:
16960 case DW_OP_const1u:
16961 case DW_OP_const1s:
16962 case DW_OP_const2u:
16963 case DW_OP_const2s:
16964 case DW_OP_const4u:
16965 case DW_OP_const4s:
16966 case DW_OP_const8u:
16967 case DW_OP_const8s:
16968 case DW_OP_constu:
16969 case DW_OP_consts:
16970 case DW_OP_dup:
16971 case DW_OP_over:
16972 case DW_OP_pick:
16973 case DW_OP_lit0:
16974 case DW_OP_lit1:
16975 case DW_OP_lit2:
16976 case DW_OP_lit3:
16977 case DW_OP_lit4:
16978 case DW_OP_lit5:
16979 case DW_OP_lit6:
16980 case DW_OP_lit7:
16981 case DW_OP_lit8:
16982 case DW_OP_lit9:
16983 case DW_OP_lit10:
16984 case DW_OP_lit11:
16985 case DW_OP_lit12:
16986 case DW_OP_lit13:
16987 case DW_OP_lit14:
16988 case DW_OP_lit15:
16989 case DW_OP_lit16:
16990 case DW_OP_lit17:
16991 case DW_OP_lit18:
16992 case DW_OP_lit19:
16993 case DW_OP_lit20:
16994 case DW_OP_lit21:
16995 case DW_OP_lit22:
16996 case DW_OP_lit23:
16997 case DW_OP_lit24:
16998 case DW_OP_lit25:
16999 case DW_OP_lit26:
17000 case DW_OP_lit27:
17001 case DW_OP_lit28:
17002 case DW_OP_lit29:
17003 case DW_OP_lit30:
17004 case DW_OP_lit31:
17005 case DW_OP_breg0:
17006 case DW_OP_breg1:
17007 case DW_OP_breg2:
17008 case DW_OP_breg3:
17009 case DW_OP_breg4:
17010 case DW_OP_breg5:
17011 case DW_OP_breg6:
17012 case DW_OP_breg7:
17013 case DW_OP_breg8:
17014 case DW_OP_breg9:
17015 case DW_OP_breg10:
17016 case DW_OP_breg11:
17017 case DW_OP_breg12:
17018 case DW_OP_breg13:
17019 case DW_OP_breg14:
17020 case DW_OP_breg15:
17021 case DW_OP_breg16:
17022 case DW_OP_breg17:
17023 case DW_OP_breg18:
17024 case DW_OP_breg19:
17025 case DW_OP_breg20:
17026 case DW_OP_breg21:
17027 case DW_OP_breg22:
17028 case DW_OP_breg23:
17029 case DW_OP_breg24:
17030 case DW_OP_breg25:
17031 case DW_OP_breg26:
17032 case DW_OP_breg27:
17033 case DW_OP_breg28:
17034 case DW_OP_breg29:
17035 case DW_OP_breg30:
17036 case DW_OP_breg31:
17037 case DW_OP_fbreg:
17038 case DW_OP_push_object_address:
17039 case DW_OP_call_frame_cfa:
17040 case DW_OP_GNU_variable_value:
17041 ++frame_offset_;
17042 break;
17043
17044 case DW_OP_drop:
17045 case DW_OP_xderef:
17046 case DW_OP_and:
17047 case DW_OP_div:
17048 case DW_OP_minus:
17049 case DW_OP_mod:
17050 case DW_OP_mul:
17051 case DW_OP_or:
17052 case DW_OP_plus:
17053 case DW_OP_shl:
17054 case DW_OP_shr:
17055 case DW_OP_shra:
17056 case DW_OP_xor:
17057 case DW_OP_bra:
17058 case DW_OP_eq:
17059 case DW_OP_ge:
17060 case DW_OP_gt:
17061 case DW_OP_le:
17062 case DW_OP_lt:
17063 case DW_OP_ne:
17064 case DW_OP_regx:
17065 case DW_OP_xderef_size:
17066 --frame_offset_;
17067 break;
17068
17069 case DW_OP_call2:
17070 case DW_OP_call4:
17071 case DW_OP_call_ref:
17072 {
17073 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17074 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17075
17076 if (stack_usage == NULL)
17077 return false;
17078 frame_offset_ += *stack_usage;
17079 break;
17080 }
17081
17082 case DW_OP_implicit_pointer:
17083 case DW_OP_entry_value:
17084 case DW_OP_const_type:
17085 case DW_OP_regval_type:
17086 case DW_OP_deref_type:
17087 case DW_OP_convert:
17088 case DW_OP_reinterpret:
17089 case DW_OP_form_tls_address:
17090 case DW_OP_GNU_push_tls_address:
17091 case DW_OP_GNU_uninit:
17092 case DW_OP_GNU_encoded_addr:
17093 case DW_OP_GNU_implicit_pointer:
17094 case DW_OP_GNU_entry_value:
17095 case DW_OP_GNU_const_type:
17096 case DW_OP_GNU_regval_type:
17097 case DW_OP_GNU_deref_type:
17098 case DW_OP_GNU_convert:
17099 case DW_OP_GNU_reinterpret:
17100 case DW_OP_GNU_parameter_ref:
17101 /* loc_list_from_tree will probably not output these operations for
17102 size functions, so assume they will not appear here. */
17103 /* Fall through... */
17104
17105 default:
17106 gcc_unreachable ();
17107 }
17108
17109 /* Now, follow the control flow (except subroutine calls). */
17110 switch (l->dw_loc_opc)
17111 {
17112 case DW_OP_bra:
17113 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17114 frame_offsets))
17115 return false;
17116 /* Fall through. */
17117
17118 case DW_OP_skip:
17119 l = l->dw_loc_oprnd1.v.val_loc;
17120 break;
17121
17122 case DW_OP_stack_value:
17123 return true;
17124
17125 default:
17126 l = l->dw_loc_next;
17127 break;
17128 }
17129 }
17130
17131 return true;
17132 }
17133
17134 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17135 operations) in order to resolve the operand of DW_OP_pick operations that
17136 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17137 offset *before* LOC is executed. Return if all relocations were
17138 successful. */
17139
17140 static bool
17141 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17142 struct dwarf_procedure_info *dpi)
17143 {
17144 /* Associate to all visited operations the frame offset *before* evaluating
17145 this operation. */
17146 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17147
17148 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17149 frame_offsets);
17150 }
17151
17152 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17153 Return NULL if it is not possible. */
17154
17155 static dw_die_ref
17156 function_to_dwarf_procedure (tree fndecl)
17157 {
17158 struct loc_descr_context ctx;
17159 struct dwarf_procedure_info dpi;
17160 dw_die_ref dwarf_proc_die;
17161 tree tree_body = DECL_SAVED_TREE (fndecl);
17162 dw_loc_descr_ref loc_body, epilogue;
17163
17164 tree cursor;
17165 unsigned i;
17166
17167 /* Do not generate multiple DWARF procedures for the same function
17168 declaration. */
17169 dwarf_proc_die = lookup_decl_die (fndecl);
17170 if (dwarf_proc_die != NULL)
17171 return dwarf_proc_die;
17172
17173 /* DWARF procedures are available starting with the DWARFv3 standard. */
17174 if (dwarf_version < 3 && dwarf_strict)
17175 return NULL;
17176
17177 /* We handle only functions for which we still have a body, that return a
17178 supported type and that takes arguments with supported types. Note that
17179 there is no point translating functions that return nothing. */
17180 if (tree_body == NULL_TREE
17181 || DECL_RESULT (fndecl) == NULL_TREE
17182 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17183 return NULL;
17184
17185 for (cursor = DECL_ARGUMENTS (fndecl);
17186 cursor != NULL_TREE;
17187 cursor = TREE_CHAIN (cursor))
17188 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17189 return NULL;
17190
17191 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17192 if (TREE_CODE (tree_body) != RETURN_EXPR)
17193 return NULL;
17194 tree_body = TREE_OPERAND (tree_body, 0);
17195 if (TREE_CODE (tree_body) != MODIFY_EXPR
17196 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17197 return NULL;
17198 tree_body = TREE_OPERAND (tree_body, 1);
17199
17200 /* Try to translate the body expression itself. Note that this will probably
17201 cause an infinite recursion if its call graph has a cycle. This is very
17202 unlikely for size functions, however, so don't bother with such things at
17203 the moment. */
17204 ctx.context_type = NULL_TREE;
17205 ctx.base_decl = NULL_TREE;
17206 ctx.dpi = &dpi;
17207 ctx.placeholder_arg = false;
17208 ctx.placeholder_seen = false;
17209 dpi.fndecl = fndecl;
17210 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17211 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17212 if (!loc_body)
17213 return NULL;
17214
17215 /* After evaluating all operands in "loc_body", we should still have on the
17216 stack all arguments plus the desired function result (top of the stack).
17217 Generate code in order to keep only the result in our stack frame. */
17218 epilogue = NULL;
17219 for (i = 0; i < dpi.args_count; ++i)
17220 {
17221 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17222 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17223 op_couple->dw_loc_next->dw_loc_next = epilogue;
17224 epilogue = op_couple;
17225 }
17226 add_loc_descr (&loc_body, epilogue);
17227 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17228 return NULL;
17229
17230 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17231 because they are considered useful. Now there is an epilogue, they are
17232 not anymore, so give it another try. */
17233 loc_descr_without_nops (loc_body);
17234
17235 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17236 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17237 though, given that size functions do not come from source, so they should
17238 not have a dedicated DW_TAG_subprogram DIE. */
17239 dwarf_proc_die
17240 = new_dwarf_proc_die (loc_body, fndecl,
17241 get_context_die (DECL_CONTEXT (fndecl)));
17242
17243 /* The called DWARF procedure consumes one stack slot per argument and
17244 returns one stack slot. */
17245 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17246
17247 return dwarf_proc_die;
17248 }
17249
17250
17251 /* Generate Dwarf location list representing LOC.
17252 If WANT_ADDRESS is false, expression computing LOC will be computed
17253 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17254 if WANT_ADDRESS is 2, expression computing address useable in location
17255 will be returned (i.e. DW_OP_reg can be used
17256 to refer to register values).
17257
17258 CONTEXT provides information to customize the location descriptions
17259 generation. Its context_type field specifies what type is implicitly
17260 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17261 will not be generated.
17262
17263 Its DPI field determines whether we are generating a DWARF expression for a
17264 DWARF procedure, so PARM_DECL references are processed specifically.
17265
17266 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17267 and dpi fields were null. */
17268
17269 static dw_loc_list_ref
17270 loc_list_from_tree_1 (tree loc, int want_address,
17271 struct loc_descr_context *context)
17272 {
17273 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17274 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17275 int have_address = 0;
17276 enum dwarf_location_atom op;
17277
17278 /* ??? Most of the time we do not take proper care for sign/zero
17279 extending the values properly. Hopefully this won't be a real
17280 problem... */
17281
17282 if (context != NULL
17283 && context->base_decl == loc
17284 && want_address == 0)
17285 {
17286 if (dwarf_version >= 3 || !dwarf_strict)
17287 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17288 NULL, NULL, NULL);
17289 else
17290 return NULL;
17291 }
17292
17293 switch (TREE_CODE (loc))
17294 {
17295 case ERROR_MARK:
17296 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17297 return 0;
17298
17299 case PLACEHOLDER_EXPR:
17300 /* This case involves extracting fields from an object to determine the
17301 position of other fields. It is supposed to appear only as the first
17302 operand of COMPONENT_REF nodes and to reference precisely the type
17303 that the context allows. */
17304 if (context != NULL
17305 && TREE_TYPE (loc) == context->context_type
17306 && want_address >= 1)
17307 {
17308 if (dwarf_version >= 3 || !dwarf_strict)
17309 {
17310 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17311 have_address = 1;
17312 break;
17313 }
17314 else
17315 return NULL;
17316 }
17317 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17318 the single argument passed by consumer. */
17319 else if (context != NULL
17320 && context->placeholder_arg
17321 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17322 && want_address == 0)
17323 {
17324 ret = new_loc_descr (DW_OP_pick, 0, 0);
17325 ret->frame_offset_rel = 1;
17326 context->placeholder_seen = true;
17327 break;
17328 }
17329 else
17330 expansion_failed (loc, NULL_RTX,
17331 "PLACEHOLDER_EXPR for an unexpected type");
17332 break;
17333
17334 case CALL_EXPR:
17335 {
17336 const int nargs = call_expr_nargs (loc);
17337 tree callee = get_callee_fndecl (loc);
17338 int i;
17339 dw_die_ref dwarf_proc;
17340
17341 if (callee == NULL_TREE)
17342 goto call_expansion_failed;
17343
17344 /* We handle only functions that return an integer. */
17345 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17346 goto call_expansion_failed;
17347
17348 dwarf_proc = function_to_dwarf_procedure (callee);
17349 if (dwarf_proc == NULL)
17350 goto call_expansion_failed;
17351
17352 /* Evaluate arguments right-to-left so that the first argument will
17353 be the top-most one on the stack. */
17354 for (i = nargs - 1; i >= 0; --i)
17355 {
17356 dw_loc_descr_ref loc_descr
17357 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17358 context);
17359
17360 if (loc_descr == NULL)
17361 goto call_expansion_failed;
17362
17363 add_loc_descr (&ret, loc_descr);
17364 }
17365
17366 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17367 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17368 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17369 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17370 add_loc_descr (&ret, ret1);
17371 break;
17372
17373 call_expansion_failed:
17374 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17375 /* There are no opcodes for these operations. */
17376 return 0;
17377 }
17378
17379 case PREINCREMENT_EXPR:
17380 case PREDECREMENT_EXPR:
17381 case POSTINCREMENT_EXPR:
17382 case POSTDECREMENT_EXPR:
17383 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17384 /* There are no opcodes for these operations. */
17385 return 0;
17386
17387 case ADDR_EXPR:
17388 /* If we already want an address, see if there is INDIRECT_REF inside
17389 e.g. for &this->field. */
17390 if (want_address)
17391 {
17392 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17393 (loc, want_address == 2, context);
17394 if (list_ret)
17395 have_address = 1;
17396 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17397 && (ret = cst_pool_loc_descr (loc)))
17398 have_address = 1;
17399 }
17400 /* Otherwise, process the argument and look for the address. */
17401 if (!list_ret && !ret)
17402 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17403 else
17404 {
17405 if (want_address)
17406 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17407 return NULL;
17408 }
17409 break;
17410
17411 case VAR_DECL:
17412 if (DECL_THREAD_LOCAL_P (loc))
17413 {
17414 rtx rtl;
17415 enum dwarf_location_atom tls_op;
17416 enum dtprel_bool dtprel = dtprel_false;
17417
17418 if (targetm.have_tls)
17419 {
17420 /* If this is not defined, we have no way to emit the
17421 data. */
17422 if (!targetm.asm_out.output_dwarf_dtprel)
17423 return 0;
17424
17425 /* The way DW_OP_GNU_push_tls_address is specified, we
17426 can only look up addresses of objects in the current
17427 module. We used DW_OP_addr as first op, but that's
17428 wrong, because DW_OP_addr is relocated by the debug
17429 info consumer, while DW_OP_GNU_push_tls_address
17430 operand shouldn't be. */
17431 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17432 return 0;
17433 dtprel = dtprel_true;
17434 /* We check for DWARF 5 here because gdb did not implement
17435 DW_OP_form_tls_address until after 7.12. */
17436 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17437 : DW_OP_GNU_push_tls_address);
17438 }
17439 else
17440 {
17441 if (!targetm.emutls.debug_form_tls_address
17442 || !(dwarf_version >= 3 || !dwarf_strict))
17443 return 0;
17444 /* We stuffed the control variable into the DECL_VALUE_EXPR
17445 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17446 no longer appear in gimple code. We used the control
17447 variable in specific so that we could pick it up here. */
17448 loc = DECL_VALUE_EXPR (loc);
17449 tls_op = DW_OP_form_tls_address;
17450 }
17451
17452 rtl = rtl_for_decl_location (loc);
17453 if (rtl == NULL_RTX)
17454 return 0;
17455
17456 if (!MEM_P (rtl))
17457 return 0;
17458 rtl = XEXP (rtl, 0);
17459 if (! CONSTANT_P (rtl))
17460 return 0;
17461
17462 ret = new_addr_loc_descr (rtl, dtprel);
17463 ret1 = new_loc_descr (tls_op, 0, 0);
17464 add_loc_descr (&ret, ret1);
17465
17466 have_address = 1;
17467 break;
17468 }
17469 /* FALLTHRU */
17470
17471 case PARM_DECL:
17472 if (context != NULL && context->dpi != NULL
17473 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17474 {
17475 /* We are generating code for a DWARF procedure and we want to access
17476 one of its arguments: find the appropriate argument offset and let
17477 the resolve_args_picking pass compute the offset that complies
17478 with the stack frame size. */
17479 unsigned i = 0;
17480 tree cursor;
17481
17482 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17483 cursor != NULL_TREE && cursor != loc;
17484 cursor = TREE_CHAIN (cursor), ++i)
17485 ;
17486 /* If we are translating a DWARF procedure, all referenced parameters
17487 must belong to the current function. */
17488 gcc_assert (cursor != NULL_TREE);
17489
17490 ret = new_loc_descr (DW_OP_pick, i, 0);
17491 ret->frame_offset_rel = 1;
17492 break;
17493 }
17494 /* FALLTHRU */
17495
17496 case RESULT_DECL:
17497 if (DECL_HAS_VALUE_EXPR_P (loc))
17498 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17499 want_address, context);
17500 /* FALLTHRU */
17501
17502 case FUNCTION_DECL:
17503 {
17504 rtx rtl;
17505 var_loc_list *loc_list = lookup_decl_loc (loc);
17506
17507 if (loc_list && loc_list->first)
17508 {
17509 list_ret = dw_loc_list (loc_list, loc, want_address);
17510 have_address = want_address != 0;
17511 break;
17512 }
17513 rtl = rtl_for_decl_location (loc);
17514 if (rtl == NULL_RTX)
17515 {
17516 if (TREE_CODE (loc) != FUNCTION_DECL
17517 && early_dwarf
17518 && current_function_decl
17519 && want_address != 1
17520 && ! DECL_IGNORED_P (loc)
17521 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17522 || POINTER_TYPE_P (TREE_TYPE (loc)))
17523 && DECL_CONTEXT (loc) == current_function_decl
17524 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17525 <= DWARF2_ADDR_SIZE))
17526 {
17527 dw_die_ref ref = lookup_decl_die (loc);
17528 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17529 if (ref)
17530 {
17531 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17532 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17533 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17534 }
17535 else
17536 {
17537 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17538 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17539 }
17540 break;
17541 }
17542 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17543 return 0;
17544 }
17545 else if (CONST_INT_P (rtl))
17546 {
17547 HOST_WIDE_INT val = INTVAL (rtl);
17548 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17549 val &= GET_MODE_MASK (DECL_MODE (loc));
17550 ret = int_loc_descriptor (val);
17551 }
17552 else if (GET_CODE (rtl) == CONST_STRING)
17553 {
17554 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17555 return 0;
17556 }
17557 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17558 ret = new_addr_loc_descr (rtl, dtprel_false);
17559 else
17560 {
17561 machine_mode mode, mem_mode;
17562
17563 /* Certain constructs can only be represented at top-level. */
17564 if (want_address == 2)
17565 {
17566 ret = loc_descriptor (rtl, VOIDmode,
17567 VAR_INIT_STATUS_INITIALIZED);
17568 have_address = 1;
17569 }
17570 else
17571 {
17572 mode = GET_MODE (rtl);
17573 mem_mode = VOIDmode;
17574 if (MEM_P (rtl))
17575 {
17576 mem_mode = mode;
17577 mode = get_address_mode (rtl);
17578 rtl = XEXP (rtl, 0);
17579 have_address = 1;
17580 }
17581 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17582 VAR_INIT_STATUS_INITIALIZED);
17583 }
17584 if (!ret)
17585 expansion_failed (loc, rtl,
17586 "failed to produce loc descriptor for rtl");
17587 }
17588 }
17589 break;
17590
17591 case MEM_REF:
17592 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17593 {
17594 have_address = 1;
17595 goto do_plus;
17596 }
17597 /* Fallthru. */
17598 case INDIRECT_REF:
17599 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17600 have_address = 1;
17601 break;
17602
17603 case TARGET_MEM_REF:
17604 case SSA_NAME:
17605 case DEBUG_EXPR_DECL:
17606 return NULL;
17607
17608 case COMPOUND_EXPR:
17609 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17610 context);
17611
17612 CASE_CONVERT:
17613 case VIEW_CONVERT_EXPR:
17614 case SAVE_EXPR:
17615 case MODIFY_EXPR:
17616 case NON_LVALUE_EXPR:
17617 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17618 context);
17619
17620 case COMPONENT_REF:
17621 case BIT_FIELD_REF:
17622 case ARRAY_REF:
17623 case ARRAY_RANGE_REF:
17624 case REALPART_EXPR:
17625 case IMAGPART_EXPR:
17626 {
17627 tree obj, offset;
17628 HOST_WIDE_INT bitsize, bitpos, bytepos;
17629 machine_mode mode;
17630 int unsignedp, reversep, volatilep = 0;
17631
17632 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17633 &unsignedp, &reversep, &volatilep);
17634
17635 gcc_assert (obj != loc);
17636
17637 list_ret = loc_list_from_tree_1 (obj,
17638 want_address == 2
17639 && !bitpos && !offset ? 2 : 1,
17640 context);
17641 /* TODO: We can extract value of the small expression via shifting even
17642 for nonzero bitpos. */
17643 if (list_ret == 0)
17644 return 0;
17645 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17646 {
17647 expansion_failed (loc, NULL_RTX,
17648 "bitfield access");
17649 return 0;
17650 }
17651
17652 if (offset != NULL_TREE)
17653 {
17654 /* Variable offset. */
17655 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17656 if (list_ret1 == 0)
17657 return 0;
17658 add_loc_list (&list_ret, list_ret1);
17659 if (!list_ret)
17660 return 0;
17661 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17662 }
17663
17664 bytepos = bitpos / BITS_PER_UNIT;
17665 if (bytepos > 0)
17666 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17667 else if (bytepos < 0)
17668 loc_list_plus_const (list_ret, bytepos);
17669
17670 have_address = 1;
17671 break;
17672 }
17673
17674 case INTEGER_CST:
17675 if ((want_address || !tree_fits_shwi_p (loc))
17676 && (ret = cst_pool_loc_descr (loc)))
17677 have_address = 1;
17678 else if (want_address == 2
17679 && tree_fits_shwi_p (loc)
17680 && (ret = address_of_int_loc_descriptor
17681 (int_size_in_bytes (TREE_TYPE (loc)),
17682 tree_to_shwi (loc))))
17683 have_address = 1;
17684 else if (tree_fits_shwi_p (loc))
17685 ret = int_loc_descriptor (tree_to_shwi (loc));
17686 else if (tree_fits_uhwi_p (loc))
17687 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17688 else
17689 {
17690 expansion_failed (loc, NULL_RTX,
17691 "Integer operand is not host integer");
17692 return 0;
17693 }
17694 break;
17695
17696 case CONSTRUCTOR:
17697 case REAL_CST:
17698 case STRING_CST:
17699 case COMPLEX_CST:
17700 if ((ret = cst_pool_loc_descr (loc)))
17701 have_address = 1;
17702 else if (TREE_CODE (loc) == CONSTRUCTOR)
17703 {
17704 tree type = TREE_TYPE (loc);
17705 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17706 unsigned HOST_WIDE_INT offset = 0;
17707 unsigned HOST_WIDE_INT cnt;
17708 constructor_elt *ce;
17709
17710 if (TREE_CODE (type) == RECORD_TYPE)
17711 {
17712 /* This is very limited, but it's enough to output
17713 pointers to member functions, as long as the
17714 referenced function is defined in the current
17715 translation unit. */
17716 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17717 {
17718 tree val = ce->value;
17719
17720 tree field = ce->index;
17721
17722 if (val)
17723 STRIP_NOPS (val);
17724
17725 if (!field || DECL_BIT_FIELD (field))
17726 {
17727 expansion_failed (loc, NULL_RTX,
17728 "bitfield in record type constructor");
17729 size = offset = (unsigned HOST_WIDE_INT)-1;
17730 ret = NULL;
17731 break;
17732 }
17733
17734 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17735 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17736 gcc_assert (pos + fieldsize <= size);
17737 if (pos < offset)
17738 {
17739 expansion_failed (loc, NULL_RTX,
17740 "out-of-order fields in record constructor");
17741 size = offset = (unsigned HOST_WIDE_INT)-1;
17742 ret = NULL;
17743 break;
17744 }
17745 if (pos > offset)
17746 {
17747 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17748 add_loc_descr (&ret, ret1);
17749 offset = pos;
17750 }
17751 if (val && fieldsize != 0)
17752 {
17753 ret1 = loc_descriptor_from_tree (val, want_address, context);
17754 if (!ret1)
17755 {
17756 expansion_failed (loc, NULL_RTX,
17757 "unsupported expression in field");
17758 size = offset = (unsigned HOST_WIDE_INT)-1;
17759 ret = NULL;
17760 break;
17761 }
17762 add_loc_descr (&ret, ret1);
17763 }
17764 if (fieldsize)
17765 {
17766 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17767 add_loc_descr (&ret, ret1);
17768 offset = pos + fieldsize;
17769 }
17770 }
17771
17772 if (offset != size)
17773 {
17774 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17775 add_loc_descr (&ret, ret1);
17776 offset = size;
17777 }
17778
17779 have_address = !!want_address;
17780 }
17781 else
17782 expansion_failed (loc, NULL_RTX,
17783 "constructor of non-record type");
17784 }
17785 else
17786 /* We can construct small constants here using int_loc_descriptor. */
17787 expansion_failed (loc, NULL_RTX,
17788 "constructor or constant not in constant pool");
17789 break;
17790
17791 case TRUTH_AND_EXPR:
17792 case TRUTH_ANDIF_EXPR:
17793 case BIT_AND_EXPR:
17794 op = DW_OP_and;
17795 goto do_binop;
17796
17797 case TRUTH_XOR_EXPR:
17798 case BIT_XOR_EXPR:
17799 op = DW_OP_xor;
17800 goto do_binop;
17801
17802 case TRUTH_OR_EXPR:
17803 case TRUTH_ORIF_EXPR:
17804 case BIT_IOR_EXPR:
17805 op = DW_OP_or;
17806 goto do_binop;
17807
17808 case FLOOR_DIV_EXPR:
17809 case CEIL_DIV_EXPR:
17810 case ROUND_DIV_EXPR:
17811 case TRUNC_DIV_EXPR:
17812 case EXACT_DIV_EXPR:
17813 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17814 return 0;
17815 op = DW_OP_div;
17816 goto do_binop;
17817
17818 case MINUS_EXPR:
17819 op = DW_OP_minus;
17820 goto do_binop;
17821
17822 case FLOOR_MOD_EXPR:
17823 case CEIL_MOD_EXPR:
17824 case ROUND_MOD_EXPR:
17825 case TRUNC_MOD_EXPR:
17826 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17827 {
17828 op = DW_OP_mod;
17829 goto do_binop;
17830 }
17831 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17832 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17833 if (list_ret == 0 || list_ret1 == 0)
17834 return 0;
17835
17836 add_loc_list (&list_ret, list_ret1);
17837 if (list_ret == 0)
17838 return 0;
17839 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17840 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17841 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17842 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17843 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17844 break;
17845
17846 case MULT_EXPR:
17847 op = DW_OP_mul;
17848 goto do_binop;
17849
17850 case LSHIFT_EXPR:
17851 op = DW_OP_shl;
17852 goto do_binop;
17853
17854 case RSHIFT_EXPR:
17855 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17856 goto do_binop;
17857
17858 case POINTER_PLUS_EXPR:
17859 case PLUS_EXPR:
17860 do_plus:
17861 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17862 {
17863 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17864 smarter to encode their opposite. The DW_OP_plus_uconst operation
17865 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17866 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17867 bytes, Y being the size of the operation that pushes the opposite
17868 of the addend. So let's choose the smallest representation. */
17869 const tree tree_addend = TREE_OPERAND (loc, 1);
17870 offset_int wi_addend;
17871 HOST_WIDE_INT shwi_addend;
17872 dw_loc_descr_ref loc_naddend;
17873
17874 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17875 if (list_ret == 0)
17876 return 0;
17877
17878 /* Try to get the literal to push. It is the opposite of the addend,
17879 so as we rely on wrapping during DWARF evaluation, first decode
17880 the literal as a "DWARF-sized" signed number. */
17881 wi_addend = wi::to_offset (tree_addend);
17882 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17883 shwi_addend = wi_addend.to_shwi ();
17884 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17885 ? int_loc_descriptor (-shwi_addend)
17886 : NULL;
17887
17888 if (loc_naddend != NULL
17889 && ((unsigned) size_of_uleb128 (shwi_addend)
17890 > size_of_loc_descr (loc_naddend)))
17891 {
17892 add_loc_descr_to_each (list_ret, loc_naddend);
17893 add_loc_descr_to_each (list_ret,
17894 new_loc_descr (DW_OP_minus, 0, 0));
17895 }
17896 else
17897 {
17898 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17899 {
17900 loc_naddend = loc_cur;
17901 loc_cur = loc_cur->dw_loc_next;
17902 ggc_free (loc_naddend);
17903 }
17904 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17905 }
17906 break;
17907 }
17908
17909 op = DW_OP_plus;
17910 goto do_binop;
17911
17912 case LE_EXPR:
17913 op = DW_OP_le;
17914 goto do_comp_binop;
17915
17916 case GE_EXPR:
17917 op = DW_OP_ge;
17918 goto do_comp_binop;
17919
17920 case LT_EXPR:
17921 op = DW_OP_lt;
17922 goto do_comp_binop;
17923
17924 case GT_EXPR:
17925 op = DW_OP_gt;
17926 goto do_comp_binop;
17927
17928 do_comp_binop:
17929 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17930 {
17931 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17932 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17933 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17934 TREE_CODE (loc));
17935 break;
17936 }
17937 else
17938 goto do_binop;
17939
17940 case EQ_EXPR:
17941 op = DW_OP_eq;
17942 goto do_binop;
17943
17944 case NE_EXPR:
17945 op = DW_OP_ne;
17946 goto do_binop;
17947
17948 do_binop:
17949 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17950 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17951 if (list_ret == 0 || list_ret1 == 0)
17952 return 0;
17953
17954 add_loc_list (&list_ret, list_ret1);
17955 if (list_ret == 0)
17956 return 0;
17957 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17958 break;
17959
17960 case TRUTH_NOT_EXPR:
17961 case BIT_NOT_EXPR:
17962 op = DW_OP_not;
17963 goto do_unop;
17964
17965 case ABS_EXPR:
17966 op = DW_OP_abs;
17967 goto do_unop;
17968
17969 case NEGATE_EXPR:
17970 op = DW_OP_neg;
17971 goto do_unop;
17972
17973 do_unop:
17974 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17975 if (list_ret == 0)
17976 return 0;
17977
17978 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17979 break;
17980
17981 case MIN_EXPR:
17982 case MAX_EXPR:
17983 {
17984 const enum tree_code code =
17985 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17986
17987 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17988 build2 (code, integer_type_node,
17989 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17990 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17991 }
17992
17993 /* fall through */
17994
17995 case COND_EXPR:
17996 {
17997 dw_loc_descr_ref lhs
17998 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17999 dw_loc_list_ref rhs
18000 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18001 dw_loc_descr_ref bra_node, jump_node, tmp;
18002
18003 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18004 if (list_ret == 0 || lhs == 0 || rhs == 0)
18005 return 0;
18006
18007 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18008 add_loc_descr_to_each (list_ret, bra_node);
18009
18010 add_loc_list (&list_ret, rhs);
18011 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18012 add_loc_descr_to_each (list_ret, jump_node);
18013
18014 add_loc_descr_to_each (list_ret, lhs);
18015 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18016 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18017
18018 /* ??? Need a node to point the skip at. Use a nop. */
18019 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18020 add_loc_descr_to_each (list_ret, tmp);
18021 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18022 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18023 }
18024 break;
18025
18026 case FIX_TRUNC_EXPR:
18027 return 0;
18028
18029 default:
18030 /* Leave front-end specific codes as simply unknown. This comes
18031 up, for instance, with the C STMT_EXPR. */
18032 if ((unsigned int) TREE_CODE (loc)
18033 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18034 {
18035 expansion_failed (loc, NULL_RTX,
18036 "language specific tree node");
18037 return 0;
18038 }
18039
18040 /* Otherwise this is a generic code; we should just lists all of
18041 these explicitly. We forgot one. */
18042 if (flag_checking)
18043 gcc_unreachable ();
18044
18045 /* In a release build, we want to degrade gracefully: better to
18046 generate incomplete debugging information than to crash. */
18047 return NULL;
18048 }
18049
18050 if (!ret && !list_ret)
18051 return 0;
18052
18053 if (want_address == 2 && !have_address
18054 && (dwarf_version >= 4 || !dwarf_strict))
18055 {
18056 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18057 {
18058 expansion_failed (loc, NULL_RTX,
18059 "DWARF address size mismatch");
18060 return 0;
18061 }
18062 if (ret)
18063 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18064 else
18065 add_loc_descr_to_each (list_ret,
18066 new_loc_descr (DW_OP_stack_value, 0, 0));
18067 have_address = 1;
18068 }
18069 /* Show if we can't fill the request for an address. */
18070 if (want_address && !have_address)
18071 {
18072 expansion_failed (loc, NULL_RTX,
18073 "Want address and only have value");
18074 return 0;
18075 }
18076
18077 gcc_assert (!ret || !list_ret);
18078
18079 /* If we've got an address and don't want one, dereference. */
18080 if (!want_address && have_address)
18081 {
18082 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18083
18084 if (size > DWARF2_ADDR_SIZE || size == -1)
18085 {
18086 expansion_failed (loc, NULL_RTX,
18087 "DWARF address size mismatch");
18088 return 0;
18089 }
18090 else if (size == DWARF2_ADDR_SIZE)
18091 op = DW_OP_deref;
18092 else
18093 op = DW_OP_deref_size;
18094
18095 if (ret)
18096 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18097 else
18098 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18099 }
18100 if (ret)
18101 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18102
18103 return list_ret;
18104 }
18105
18106 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18107 expressions. */
18108
18109 static dw_loc_list_ref
18110 loc_list_from_tree (tree loc, int want_address,
18111 struct loc_descr_context *context)
18112 {
18113 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18114
18115 for (dw_loc_list_ref loc_cur = result;
18116 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18117 loc_descr_without_nops (loc_cur->expr);
18118 return result;
18119 }
18120
18121 /* Same as above but return only single location expression. */
18122 static dw_loc_descr_ref
18123 loc_descriptor_from_tree (tree loc, int want_address,
18124 struct loc_descr_context *context)
18125 {
18126 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18127 if (!ret)
18128 return NULL;
18129 if (ret->dw_loc_next)
18130 {
18131 expansion_failed (loc, NULL_RTX,
18132 "Location list where only loc descriptor needed");
18133 return NULL;
18134 }
18135 return ret->expr;
18136 }
18137
18138 /* Given a value, round it up to the lowest multiple of `boundary'
18139 which is not less than the value itself. */
18140
18141 static inline HOST_WIDE_INT
18142 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18143 {
18144 return (((value + boundary - 1) / boundary) * boundary);
18145 }
18146
18147 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18148 pointer to the declared type for the relevant field variable, or return
18149 `integer_type_node' if the given node turns out to be an
18150 ERROR_MARK node. */
18151
18152 static inline tree
18153 field_type (const_tree decl)
18154 {
18155 tree type;
18156
18157 if (TREE_CODE (decl) == ERROR_MARK)
18158 return integer_type_node;
18159
18160 type = DECL_BIT_FIELD_TYPE (decl);
18161 if (type == NULL_TREE)
18162 type = TREE_TYPE (decl);
18163
18164 return type;
18165 }
18166
18167 /* Given a pointer to a tree node, return the alignment in bits for
18168 it, or else return BITS_PER_WORD if the node actually turns out to
18169 be an ERROR_MARK node. */
18170
18171 static inline unsigned
18172 simple_type_align_in_bits (const_tree type)
18173 {
18174 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18175 }
18176
18177 static inline unsigned
18178 simple_decl_align_in_bits (const_tree decl)
18179 {
18180 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18181 }
18182
18183 /* Return the result of rounding T up to ALIGN. */
18184
18185 static inline offset_int
18186 round_up_to_align (const offset_int &t, unsigned int align)
18187 {
18188 return wi::udiv_trunc (t + align - 1, align) * align;
18189 }
18190
18191 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18192 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18193 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18194 if we fail to return the size in one of these two forms. */
18195
18196 static dw_loc_descr_ref
18197 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18198 {
18199 tree tree_size;
18200 struct loc_descr_context ctx;
18201
18202 /* Return a constant integer in priority, if possible. */
18203 *cst_size = int_size_in_bytes (type);
18204 if (*cst_size != -1)
18205 return NULL;
18206
18207 ctx.context_type = const_cast<tree> (type);
18208 ctx.base_decl = NULL_TREE;
18209 ctx.dpi = NULL;
18210 ctx.placeholder_arg = false;
18211 ctx.placeholder_seen = false;
18212
18213 type = TYPE_MAIN_VARIANT (type);
18214 tree_size = TYPE_SIZE_UNIT (type);
18215 return ((tree_size != NULL_TREE)
18216 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18217 : NULL);
18218 }
18219
18220 /* Helper structure for RECORD_TYPE processing. */
18221 struct vlr_context
18222 {
18223 /* Root RECORD_TYPE. It is needed to generate data member location
18224 descriptions in variable-length records (VLR), but also to cope with
18225 variants, which are composed of nested structures multiplexed with
18226 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18227 function processing a FIELD_DECL, it is required to be non null. */
18228 tree struct_type;
18229 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18230 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18231 this variant part as part of the root record (in storage units). For
18232 regular records, it must be NULL_TREE. */
18233 tree variant_part_offset;
18234 };
18235
18236 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18237 addressed byte of the "containing object" for the given FIELD_DECL. If
18238 possible, return a native constant through CST_OFFSET (in which case NULL is
18239 returned); otherwise return a DWARF expression that computes the offset.
18240
18241 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18242 that offset is, either because the argument turns out to be a pointer to an
18243 ERROR_MARK node, or because the offset expression is too complex for us.
18244
18245 CTX is required: see the comment for VLR_CONTEXT. */
18246
18247 static dw_loc_descr_ref
18248 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18249 HOST_WIDE_INT *cst_offset)
18250 {
18251 tree tree_result;
18252 dw_loc_list_ref loc_result;
18253
18254 *cst_offset = 0;
18255
18256 if (TREE_CODE (decl) == ERROR_MARK)
18257 return NULL;
18258 else
18259 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18260
18261 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18262 case. */
18263 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18264 return NULL;
18265
18266 #ifdef PCC_BITFIELD_TYPE_MATTERS
18267 /* We used to handle only constant offsets in all cases. Now, we handle
18268 properly dynamic byte offsets only when PCC bitfield type doesn't
18269 matter. */
18270 if (PCC_BITFIELD_TYPE_MATTERS
18271 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18272 {
18273 offset_int object_offset_in_bits;
18274 offset_int object_offset_in_bytes;
18275 offset_int bitpos_int;
18276 tree type;
18277 tree field_size_tree;
18278 offset_int deepest_bitpos;
18279 offset_int field_size_in_bits;
18280 unsigned int type_align_in_bits;
18281 unsigned int decl_align_in_bits;
18282 offset_int type_size_in_bits;
18283
18284 bitpos_int = wi::to_offset (bit_position (decl));
18285 type = field_type (decl);
18286 type_size_in_bits = offset_int_type_size_in_bits (type);
18287 type_align_in_bits = simple_type_align_in_bits (type);
18288
18289 field_size_tree = DECL_SIZE (decl);
18290
18291 /* The size could be unspecified if there was an error, or for
18292 a flexible array member. */
18293 if (!field_size_tree)
18294 field_size_tree = bitsize_zero_node;
18295
18296 /* If the size of the field is not constant, use the type size. */
18297 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18298 field_size_in_bits = wi::to_offset (field_size_tree);
18299 else
18300 field_size_in_bits = type_size_in_bits;
18301
18302 decl_align_in_bits = simple_decl_align_in_bits (decl);
18303
18304 /* The GCC front-end doesn't make any attempt to keep track of the
18305 starting bit offset (relative to the start of the containing
18306 structure type) of the hypothetical "containing object" for a
18307 bit-field. Thus, when computing the byte offset value for the
18308 start of the "containing object" of a bit-field, we must deduce
18309 this information on our own. This can be rather tricky to do in
18310 some cases. For example, handling the following structure type
18311 definition when compiling for an i386/i486 target (which only
18312 aligns long long's to 32-bit boundaries) can be very tricky:
18313
18314 struct S { int field1; long long field2:31; };
18315
18316 Fortunately, there is a simple rule-of-thumb which can be used
18317 in such cases. When compiling for an i386/i486, GCC will
18318 allocate 8 bytes for the structure shown above. It decides to
18319 do this based upon one simple rule for bit-field allocation.
18320 GCC allocates each "containing object" for each bit-field at
18321 the first (i.e. lowest addressed) legitimate alignment boundary
18322 (based upon the required minimum alignment for the declared
18323 type of the field) which it can possibly use, subject to the
18324 condition that there is still enough available space remaining
18325 in the containing object (when allocated at the selected point)
18326 to fully accommodate all of the bits of the bit-field itself.
18327
18328 This simple rule makes it obvious why GCC allocates 8 bytes for
18329 each object of the structure type shown above. When looking
18330 for a place to allocate the "containing object" for `field2',
18331 the compiler simply tries to allocate a 64-bit "containing
18332 object" at each successive 32-bit boundary (starting at zero)
18333 until it finds a place to allocate that 64- bit field such that
18334 at least 31 contiguous (and previously unallocated) bits remain
18335 within that selected 64 bit field. (As it turns out, for the
18336 example above, the compiler finds it is OK to allocate the
18337 "containing object" 64-bit field at bit-offset zero within the
18338 structure type.)
18339
18340 Here we attempt to work backwards from the limited set of facts
18341 we're given, and we try to deduce from those facts, where GCC
18342 must have believed that the containing object started (within
18343 the structure type). The value we deduce is then used (by the
18344 callers of this routine) to generate DW_AT_location and
18345 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18346 the case of DW_AT_location, regular fields as well). */
18347
18348 /* Figure out the bit-distance from the start of the structure to
18349 the "deepest" bit of the bit-field. */
18350 deepest_bitpos = bitpos_int + field_size_in_bits;
18351
18352 /* This is the tricky part. Use some fancy footwork to deduce
18353 where the lowest addressed bit of the containing object must
18354 be. */
18355 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18356
18357 /* Round up to type_align by default. This works best for
18358 bitfields. */
18359 object_offset_in_bits
18360 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18361
18362 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18363 {
18364 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18365
18366 /* Round up to decl_align instead. */
18367 object_offset_in_bits
18368 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18369 }
18370
18371 object_offset_in_bytes
18372 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18373 if (ctx->variant_part_offset == NULL_TREE)
18374 {
18375 *cst_offset = object_offset_in_bytes.to_shwi ();
18376 return NULL;
18377 }
18378 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18379 }
18380 else
18381 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18382 tree_result = byte_position (decl);
18383
18384 if (ctx->variant_part_offset != NULL_TREE)
18385 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18386 ctx->variant_part_offset, tree_result);
18387
18388 /* If the byte offset is a constant, it's simplier to handle a native
18389 constant rather than a DWARF expression. */
18390 if (TREE_CODE (tree_result) == INTEGER_CST)
18391 {
18392 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18393 return NULL;
18394 }
18395 struct loc_descr_context loc_ctx = {
18396 ctx->struct_type, /* context_type */
18397 NULL_TREE, /* base_decl */
18398 NULL, /* dpi */
18399 false, /* placeholder_arg */
18400 false /* placeholder_seen */
18401 };
18402 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18403
18404 /* We want a DWARF expression: abort if we only have a location list with
18405 multiple elements. */
18406 if (!loc_result || !single_element_loc_list_p (loc_result))
18407 return NULL;
18408 else
18409 return loc_result->expr;
18410 }
18411 \f
18412 /* The following routines define various Dwarf attributes and any data
18413 associated with them. */
18414
18415 /* Add a location description attribute value to a DIE.
18416
18417 This emits location attributes suitable for whole variables and
18418 whole parameters. Note that the location attributes for struct fields are
18419 generated by the routine `data_member_location_attribute' below. */
18420
18421 static inline void
18422 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18423 dw_loc_list_ref descr)
18424 {
18425 if (descr == 0)
18426 return;
18427 if (single_element_loc_list_p (descr))
18428 add_AT_loc (die, attr_kind, descr->expr);
18429 else
18430 add_AT_loc_list (die, attr_kind, descr);
18431 }
18432
18433 /* Add DW_AT_accessibility attribute to DIE if needed. */
18434
18435 static void
18436 add_accessibility_attribute (dw_die_ref die, tree decl)
18437 {
18438 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18439 children, otherwise the default is DW_ACCESS_public. In DWARF2
18440 the default has always been DW_ACCESS_public. */
18441 if (TREE_PROTECTED (decl))
18442 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18443 else if (TREE_PRIVATE (decl))
18444 {
18445 if (dwarf_version == 2
18446 || die->die_parent == NULL
18447 || die->die_parent->die_tag != DW_TAG_class_type)
18448 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18449 }
18450 else if (dwarf_version > 2
18451 && die->die_parent
18452 && die->die_parent->die_tag == DW_TAG_class_type)
18453 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18454 }
18455
18456 /* Attach the specialized form of location attribute used for data members of
18457 struct and union types. In the special case of a FIELD_DECL node which
18458 represents a bit-field, the "offset" part of this special location
18459 descriptor must indicate the distance in bytes from the lowest-addressed
18460 byte of the containing struct or union type to the lowest-addressed byte of
18461 the "containing object" for the bit-field. (See the `field_byte_offset'
18462 function above).
18463
18464 For any given bit-field, the "containing object" is a hypothetical object
18465 (of some integral or enum type) within which the given bit-field lives. The
18466 type of this hypothetical "containing object" is always the same as the
18467 declared type of the individual bit-field itself (for GCC anyway... the
18468 DWARF spec doesn't actually mandate this). Note that it is the size (in
18469 bytes) of the hypothetical "containing object" which will be given in the
18470 DW_AT_byte_size attribute for this bit-field. (See the
18471 `byte_size_attribute' function below.) It is also used when calculating the
18472 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18473 function below.)
18474
18475 CTX is required: see the comment for VLR_CONTEXT. */
18476
18477 static void
18478 add_data_member_location_attribute (dw_die_ref die,
18479 tree decl,
18480 struct vlr_context *ctx)
18481 {
18482 HOST_WIDE_INT offset;
18483 dw_loc_descr_ref loc_descr = 0;
18484
18485 if (TREE_CODE (decl) == TREE_BINFO)
18486 {
18487 /* We're working on the TAG_inheritance for a base class. */
18488 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18489 {
18490 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18491 aren't at a fixed offset from all (sub)objects of the same
18492 type. We need to extract the appropriate offset from our
18493 vtable. The following dwarf expression means
18494
18495 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18496
18497 This is specific to the V3 ABI, of course. */
18498
18499 dw_loc_descr_ref tmp;
18500
18501 /* Make a copy of the object address. */
18502 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18503 add_loc_descr (&loc_descr, tmp);
18504
18505 /* Extract the vtable address. */
18506 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18507 add_loc_descr (&loc_descr, tmp);
18508
18509 /* Calculate the address of the offset. */
18510 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18511 gcc_assert (offset < 0);
18512
18513 tmp = int_loc_descriptor (-offset);
18514 add_loc_descr (&loc_descr, tmp);
18515 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18516 add_loc_descr (&loc_descr, tmp);
18517
18518 /* Extract the offset. */
18519 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18520 add_loc_descr (&loc_descr, tmp);
18521
18522 /* Add it to the object address. */
18523 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18524 add_loc_descr (&loc_descr, tmp);
18525 }
18526 else
18527 offset = tree_to_shwi (BINFO_OFFSET (decl));
18528 }
18529 else
18530 {
18531 loc_descr = field_byte_offset (decl, ctx, &offset);
18532
18533 /* If loc_descr is available then we know the field offset is dynamic.
18534 However, GDB does not handle dynamic field offsets very well at the
18535 moment. */
18536 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18537 {
18538 loc_descr = NULL;
18539 offset = 0;
18540 }
18541
18542 /* Data member location evalutation starts with the base address on the
18543 stack. Compute the field offset and add it to this base address. */
18544 else if (loc_descr != NULL)
18545 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18546 }
18547
18548 if (! loc_descr)
18549 {
18550 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18551 e.g. GDB only added support to it in November 2016. For DWARF5
18552 we need newer debug info consumers anyway. We might change this
18553 to dwarf_version >= 4 once most consumers catched up. */
18554 if (dwarf_version >= 5
18555 && TREE_CODE (decl) == FIELD_DECL
18556 && DECL_BIT_FIELD_TYPE (decl))
18557 {
18558 tree off = bit_position (decl);
18559 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18560 {
18561 remove_AT (die, DW_AT_byte_size);
18562 remove_AT (die, DW_AT_bit_offset);
18563 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18564 return;
18565 }
18566 }
18567 if (dwarf_version > 2)
18568 {
18569 /* Don't need to output a location expression, just the constant. */
18570 if (offset < 0)
18571 add_AT_int (die, DW_AT_data_member_location, offset);
18572 else
18573 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18574 return;
18575 }
18576 else
18577 {
18578 enum dwarf_location_atom op;
18579
18580 /* The DWARF2 standard says that we should assume that the structure
18581 address is already on the stack, so we can specify a structure
18582 field address by using DW_OP_plus_uconst. */
18583 op = DW_OP_plus_uconst;
18584 loc_descr = new_loc_descr (op, offset, 0);
18585 }
18586 }
18587
18588 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18589 }
18590
18591 /* Writes integer values to dw_vec_const array. */
18592
18593 static void
18594 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18595 {
18596 while (size != 0)
18597 {
18598 *dest++ = val & 0xff;
18599 val >>= 8;
18600 --size;
18601 }
18602 }
18603
18604 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18605
18606 static HOST_WIDE_INT
18607 extract_int (const unsigned char *src, unsigned int size)
18608 {
18609 HOST_WIDE_INT val = 0;
18610
18611 src += size;
18612 while (size != 0)
18613 {
18614 val <<= 8;
18615 val |= *--src & 0xff;
18616 --size;
18617 }
18618 return val;
18619 }
18620
18621 /* Writes wide_int values to dw_vec_const array. */
18622
18623 static void
18624 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18625 {
18626 int i;
18627
18628 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18629 {
18630 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18631 return;
18632 }
18633
18634 /* We'd have to extend this code to support odd sizes. */
18635 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18636
18637 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18638
18639 if (WORDS_BIG_ENDIAN)
18640 for (i = n - 1; i >= 0; i--)
18641 {
18642 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18643 dest += sizeof (HOST_WIDE_INT);
18644 }
18645 else
18646 for (i = 0; i < n; i++)
18647 {
18648 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18649 dest += sizeof (HOST_WIDE_INT);
18650 }
18651 }
18652
18653 /* Writes floating point values to dw_vec_const array. */
18654
18655 static void
18656 insert_float (const_rtx rtl, unsigned char *array)
18657 {
18658 long val[4];
18659 int i;
18660 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18661
18662 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18663
18664 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18665 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18666 {
18667 insert_int (val[i], 4, array);
18668 array += 4;
18669 }
18670 }
18671
18672 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18673 does not have a "location" either in memory or in a register. These
18674 things can arise in GNU C when a constant is passed as an actual parameter
18675 to an inlined function. They can also arise in C++ where declared
18676 constants do not necessarily get memory "homes". */
18677
18678 static bool
18679 add_const_value_attribute (dw_die_ref die, rtx rtl)
18680 {
18681 switch (GET_CODE (rtl))
18682 {
18683 case CONST_INT:
18684 {
18685 HOST_WIDE_INT val = INTVAL (rtl);
18686
18687 if (val < 0)
18688 add_AT_int (die, DW_AT_const_value, val);
18689 else
18690 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18691 }
18692 return true;
18693
18694 case CONST_WIDE_INT:
18695 {
18696 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18697 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18698 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18699 wide_int w = wi::zext (w1, prec);
18700 add_AT_wide (die, DW_AT_const_value, w);
18701 }
18702 return true;
18703
18704 case CONST_DOUBLE:
18705 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18706 floating-point constant. A CONST_DOUBLE is used whenever the
18707 constant requires more than one word in order to be adequately
18708 represented. */
18709 if (TARGET_SUPPORTS_WIDE_INT == 0
18710 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18711 add_AT_double (die, DW_AT_const_value,
18712 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18713 else
18714 {
18715 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18716 unsigned int length = GET_MODE_SIZE (mode);
18717 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18718
18719 insert_float (rtl, array);
18720 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18721 }
18722 return true;
18723
18724 case CONST_VECTOR:
18725 {
18726 machine_mode mode = GET_MODE (rtl);
18727 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18728 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18729 unsigned char *array
18730 = ggc_vec_alloc<unsigned char> (length * elt_size);
18731 unsigned int i;
18732 unsigned char *p;
18733 machine_mode imode = GET_MODE_INNER (mode);
18734
18735 switch (GET_MODE_CLASS (mode))
18736 {
18737 case MODE_VECTOR_INT:
18738 for (i = 0, p = array; i < length; i++, p += elt_size)
18739 {
18740 rtx elt = CONST_VECTOR_ELT (rtl, i);
18741 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18742 }
18743 break;
18744
18745 case MODE_VECTOR_FLOAT:
18746 for (i = 0, p = array; i < length; i++, p += elt_size)
18747 {
18748 rtx elt = CONST_VECTOR_ELT (rtl, i);
18749 insert_float (elt, p);
18750 }
18751 break;
18752
18753 default:
18754 gcc_unreachable ();
18755 }
18756
18757 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18758 }
18759 return true;
18760
18761 case CONST_STRING:
18762 if (dwarf_version >= 4 || !dwarf_strict)
18763 {
18764 dw_loc_descr_ref loc_result;
18765 resolve_one_addr (&rtl);
18766 rtl_addr:
18767 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18768 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18769 add_AT_loc (die, DW_AT_location, loc_result);
18770 vec_safe_push (used_rtx_array, rtl);
18771 return true;
18772 }
18773 return false;
18774
18775 case CONST:
18776 if (CONSTANT_P (XEXP (rtl, 0)))
18777 return add_const_value_attribute (die, XEXP (rtl, 0));
18778 /* FALLTHROUGH */
18779 case SYMBOL_REF:
18780 if (!const_ok_for_output (rtl))
18781 return false;
18782 /* FALLTHROUGH */
18783 case LABEL_REF:
18784 if (dwarf_version >= 4 || !dwarf_strict)
18785 goto rtl_addr;
18786 return false;
18787
18788 case PLUS:
18789 /* In cases where an inlined instance of an inline function is passed
18790 the address of an `auto' variable (which is local to the caller) we
18791 can get a situation where the DECL_RTL of the artificial local
18792 variable (for the inlining) which acts as a stand-in for the
18793 corresponding formal parameter (of the inline function) will look
18794 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18795 exactly a compile-time constant expression, but it isn't the address
18796 of the (artificial) local variable either. Rather, it represents the
18797 *value* which the artificial local variable always has during its
18798 lifetime. We currently have no way to represent such quasi-constant
18799 values in Dwarf, so for now we just punt and generate nothing. */
18800 return false;
18801
18802 case HIGH:
18803 case CONST_FIXED:
18804 return false;
18805
18806 case MEM:
18807 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18808 && MEM_READONLY_P (rtl)
18809 && GET_MODE (rtl) == BLKmode)
18810 {
18811 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18812 return true;
18813 }
18814 return false;
18815
18816 default:
18817 /* No other kinds of rtx should be possible here. */
18818 gcc_unreachable ();
18819 }
18820 return false;
18821 }
18822
18823 /* Determine whether the evaluation of EXPR references any variables
18824 or functions which aren't otherwise used (and therefore may not be
18825 output). */
18826 static tree
18827 reference_to_unused (tree * tp, int * walk_subtrees,
18828 void * data ATTRIBUTE_UNUSED)
18829 {
18830 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18831 *walk_subtrees = 0;
18832
18833 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18834 && ! TREE_ASM_WRITTEN (*tp))
18835 return *tp;
18836 /* ??? The C++ FE emits debug information for using decls, so
18837 putting gcc_unreachable here falls over. See PR31899. For now
18838 be conservative. */
18839 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18840 return *tp;
18841 else if (VAR_P (*tp))
18842 {
18843 varpool_node *node = varpool_node::get (*tp);
18844 if (!node || !node->definition)
18845 return *tp;
18846 }
18847 else if (TREE_CODE (*tp) == FUNCTION_DECL
18848 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18849 {
18850 /* The call graph machinery must have finished analyzing,
18851 optimizing and gimplifying the CU by now.
18852 So if *TP has no call graph node associated
18853 to it, it means *TP will not be emitted. */
18854 if (!cgraph_node::get (*tp))
18855 return *tp;
18856 }
18857 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18858 return *tp;
18859
18860 return NULL_TREE;
18861 }
18862
18863 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18864 for use in a later add_const_value_attribute call. */
18865
18866 static rtx
18867 rtl_for_decl_init (tree init, tree type)
18868 {
18869 rtx rtl = NULL_RTX;
18870
18871 STRIP_NOPS (init);
18872
18873 /* If a variable is initialized with a string constant without embedded
18874 zeros, build CONST_STRING. */
18875 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18876 {
18877 tree enttype = TREE_TYPE (type);
18878 tree domain = TYPE_DOMAIN (type);
18879 scalar_int_mode mode;
18880
18881 if (is_int_mode (TYPE_MODE (enttype), &mode)
18882 && GET_MODE_SIZE (mode) == 1
18883 && domain
18884 && integer_zerop (TYPE_MIN_VALUE (domain))
18885 && compare_tree_int (TYPE_MAX_VALUE (domain),
18886 TREE_STRING_LENGTH (init) - 1) == 0
18887 && ((size_t) TREE_STRING_LENGTH (init)
18888 == strlen (TREE_STRING_POINTER (init)) + 1))
18889 {
18890 rtl = gen_rtx_CONST_STRING (VOIDmode,
18891 ggc_strdup (TREE_STRING_POINTER (init)));
18892 rtl = gen_rtx_MEM (BLKmode, rtl);
18893 MEM_READONLY_P (rtl) = 1;
18894 }
18895 }
18896 /* Other aggregates, and complex values, could be represented using
18897 CONCAT: FIXME! */
18898 else if (AGGREGATE_TYPE_P (type)
18899 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18900 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18901 || TREE_CODE (type) == COMPLEX_TYPE)
18902 ;
18903 /* Vectors only work if their mode is supported by the target.
18904 FIXME: generic vectors ought to work too. */
18905 else if (TREE_CODE (type) == VECTOR_TYPE
18906 && !VECTOR_MODE_P (TYPE_MODE (type)))
18907 ;
18908 /* If the initializer is something that we know will expand into an
18909 immediate RTL constant, expand it now. We must be careful not to
18910 reference variables which won't be output. */
18911 else if (initializer_constant_valid_p (init, type)
18912 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18913 {
18914 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18915 possible. */
18916 if (TREE_CODE (type) == VECTOR_TYPE)
18917 switch (TREE_CODE (init))
18918 {
18919 case VECTOR_CST:
18920 break;
18921 case CONSTRUCTOR:
18922 if (TREE_CONSTANT (init))
18923 {
18924 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18925 bool constant_p = true;
18926 tree value;
18927 unsigned HOST_WIDE_INT ix;
18928
18929 /* Even when ctor is constant, it might contain non-*_CST
18930 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18931 belong into VECTOR_CST nodes. */
18932 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18933 if (!CONSTANT_CLASS_P (value))
18934 {
18935 constant_p = false;
18936 break;
18937 }
18938
18939 if (constant_p)
18940 {
18941 init = build_vector_from_ctor (type, elts);
18942 break;
18943 }
18944 }
18945 /* FALLTHRU */
18946
18947 default:
18948 return NULL;
18949 }
18950
18951 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18952
18953 /* If expand_expr returns a MEM, it wasn't immediate. */
18954 gcc_assert (!rtl || !MEM_P (rtl));
18955 }
18956
18957 return rtl;
18958 }
18959
18960 /* Generate RTL for the variable DECL to represent its location. */
18961
18962 static rtx
18963 rtl_for_decl_location (tree decl)
18964 {
18965 rtx rtl;
18966
18967 /* Here we have to decide where we are going to say the parameter "lives"
18968 (as far as the debugger is concerned). We only have a couple of
18969 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18970
18971 DECL_RTL normally indicates where the parameter lives during most of the
18972 activation of the function. If optimization is enabled however, this
18973 could be either NULL or else a pseudo-reg. Both of those cases indicate
18974 that the parameter doesn't really live anywhere (as far as the code
18975 generation parts of GCC are concerned) during most of the function's
18976 activation. That will happen (for example) if the parameter is never
18977 referenced within the function.
18978
18979 We could just generate a location descriptor here for all non-NULL
18980 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18981 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18982 where DECL_RTL is NULL or is a pseudo-reg.
18983
18984 Note however that we can only get away with using DECL_INCOMING_RTL as
18985 a backup substitute for DECL_RTL in certain limited cases. In cases
18986 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18987 we can be sure that the parameter was passed using the same type as it is
18988 declared to have within the function, and that its DECL_INCOMING_RTL
18989 points us to a place where a value of that type is passed.
18990
18991 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18992 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18993 because in these cases DECL_INCOMING_RTL points us to a value of some
18994 type which is *different* from the type of the parameter itself. Thus,
18995 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18996 such cases, the debugger would end up (for example) trying to fetch a
18997 `float' from a place which actually contains the first part of a
18998 `double'. That would lead to really incorrect and confusing
18999 output at debug-time.
19000
19001 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19002 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19003 are a couple of exceptions however. On little-endian machines we can
19004 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19005 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19006 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19007 when (on a little-endian machine) a non-prototyped function has a
19008 parameter declared to be of type `short' or `char'. In such cases,
19009 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19010 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19011 passed `int' value. If the debugger then uses that address to fetch
19012 a `short' or a `char' (on a little-endian machine) the result will be
19013 the correct data, so we allow for such exceptional cases below.
19014
19015 Note that our goal here is to describe the place where the given formal
19016 parameter lives during most of the function's activation (i.e. between the
19017 end of the prologue and the start of the epilogue). We'll do that as best
19018 as we can. Note however that if the given formal parameter is modified
19019 sometime during the execution of the function, then a stack backtrace (at
19020 debug-time) will show the function as having been called with the *new*
19021 value rather than the value which was originally passed in. This happens
19022 rarely enough that it is not a major problem, but it *is* a problem, and
19023 I'd like to fix it.
19024
19025 A future version of dwarf2out.c may generate two additional attributes for
19026 any given DW_TAG_formal_parameter DIE which will describe the "passed
19027 type" and the "passed location" for the given formal parameter in addition
19028 to the attributes we now generate to indicate the "declared type" and the
19029 "active location" for each parameter. This additional set of attributes
19030 could be used by debuggers for stack backtraces. Separately, note that
19031 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19032 This happens (for example) for inlined-instances of inline function formal
19033 parameters which are never referenced. This really shouldn't be
19034 happening. All PARM_DECL nodes should get valid non-NULL
19035 DECL_INCOMING_RTL values. FIXME. */
19036
19037 /* Use DECL_RTL as the "location" unless we find something better. */
19038 rtl = DECL_RTL_IF_SET (decl);
19039
19040 /* When generating abstract instances, ignore everything except
19041 constants, symbols living in memory, and symbols living in
19042 fixed registers. */
19043 if (! reload_completed)
19044 {
19045 if (rtl
19046 && (CONSTANT_P (rtl)
19047 || (MEM_P (rtl)
19048 && CONSTANT_P (XEXP (rtl, 0)))
19049 || (REG_P (rtl)
19050 && VAR_P (decl)
19051 && TREE_STATIC (decl))))
19052 {
19053 rtl = targetm.delegitimize_address (rtl);
19054 return rtl;
19055 }
19056 rtl = NULL_RTX;
19057 }
19058 else if (TREE_CODE (decl) == PARM_DECL)
19059 {
19060 if (rtl == NULL_RTX
19061 || is_pseudo_reg (rtl)
19062 || (MEM_P (rtl)
19063 && is_pseudo_reg (XEXP (rtl, 0))
19064 && DECL_INCOMING_RTL (decl)
19065 && MEM_P (DECL_INCOMING_RTL (decl))
19066 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19067 {
19068 tree declared_type = TREE_TYPE (decl);
19069 tree passed_type = DECL_ARG_TYPE (decl);
19070 machine_mode dmode = TYPE_MODE (declared_type);
19071 machine_mode pmode = TYPE_MODE (passed_type);
19072
19073 /* This decl represents a formal parameter which was optimized out.
19074 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19075 all cases where (rtl == NULL_RTX) just below. */
19076 if (dmode == pmode)
19077 rtl = DECL_INCOMING_RTL (decl);
19078 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19079 && SCALAR_INT_MODE_P (dmode)
19080 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19081 && DECL_INCOMING_RTL (decl))
19082 {
19083 rtx inc = DECL_INCOMING_RTL (decl);
19084 if (REG_P (inc))
19085 rtl = inc;
19086 else if (MEM_P (inc))
19087 {
19088 if (BYTES_BIG_ENDIAN)
19089 rtl = adjust_address_nv (inc, dmode,
19090 GET_MODE_SIZE (pmode)
19091 - GET_MODE_SIZE (dmode));
19092 else
19093 rtl = inc;
19094 }
19095 }
19096 }
19097
19098 /* If the parm was passed in registers, but lives on the stack, then
19099 make a big endian correction if the mode of the type of the
19100 parameter is not the same as the mode of the rtl. */
19101 /* ??? This is the same series of checks that are made in dbxout.c before
19102 we reach the big endian correction code there. It isn't clear if all
19103 of these checks are necessary here, but keeping them all is the safe
19104 thing to do. */
19105 else if (MEM_P (rtl)
19106 && XEXP (rtl, 0) != const0_rtx
19107 && ! CONSTANT_P (XEXP (rtl, 0))
19108 /* Not passed in memory. */
19109 && !MEM_P (DECL_INCOMING_RTL (decl))
19110 /* Not passed by invisible reference. */
19111 && (!REG_P (XEXP (rtl, 0))
19112 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19113 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19114 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19115 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19116 #endif
19117 )
19118 /* Big endian correction check. */
19119 && BYTES_BIG_ENDIAN
19120 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19121 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19122 < UNITS_PER_WORD))
19123 {
19124 machine_mode addr_mode = get_address_mode (rtl);
19125 int offset = (UNITS_PER_WORD
19126 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19127
19128 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19129 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19130 }
19131 }
19132 else if (VAR_P (decl)
19133 && rtl
19134 && MEM_P (rtl)
19135 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19136 {
19137 machine_mode addr_mode = get_address_mode (rtl);
19138 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19139 GET_MODE (rtl));
19140
19141 /* If a variable is declared "register" yet is smaller than
19142 a register, then if we store the variable to memory, it
19143 looks like we're storing a register-sized value, when in
19144 fact we are not. We need to adjust the offset of the
19145 storage location to reflect the actual value's bytes,
19146 else gdb will not be able to display it. */
19147 if (offset != 0)
19148 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19149 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19150 }
19151
19152 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19153 and will have been substituted directly into all expressions that use it.
19154 C does not have such a concept, but C++ and other languages do. */
19155 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19156 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19157
19158 if (rtl)
19159 rtl = targetm.delegitimize_address (rtl);
19160
19161 /* If we don't look past the constant pool, we risk emitting a
19162 reference to a constant pool entry that isn't referenced from
19163 code, and thus is not emitted. */
19164 if (rtl)
19165 rtl = avoid_constant_pool_reference (rtl);
19166
19167 /* Try harder to get a rtl. If this symbol ends up not being emitted
19168 in the current CU, resolve_addr will remove the expression referencing
19169 it. */
19170 if (rtl == NULL_RTX
19171 && VAR_P (decl)
19172 && !DECL_EXTERNAL (decl)
19173 && TREE_STATIC (decl)
19174 && DECL_NAME (decl)
19175 && !DECL_HARD_REGISTER (decl)
19176 && DECL_MODE (decl) != VOIDmode)
19177 {
19178 rtl = make_decl_rtl_for_debug (decl);
19179 if (!MEM_P (rtl)
19180 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19181 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19182 rtl = NULL_RTX;
19183 }
19184
19185 return rtl;
19186 }
19187
19188 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19189 returned. If so, the decl for the COMMON block is returned, and the
19190 value is the offset into the common block for the symbol. */
19191
19192 static tree
19193 fortran_common (tree decl, HOST_WIDE_INT *value)
19194 {
19195 tree val_expr, cvar;
19196 machine_mode mode;
19197 HOST_WIDE_INT bitsize, bitpos;
19198 tree offset;
19199 int unsignedp, reversep, volatilep = 0;
19200
19201 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19202 it does not have a value (the offset into the common area), or if it
19203 is thread local (as opposed to global) then it isn't common, and shouldn't
19204 be handled as such. */
19205 if (!VAR_P (decl)
19206 || !TREE_STATIC (decl)
19207 || !DECL_HAS_VALUE_EXPR_P (decl)
19208 || !is_fortran ())
19209 return NULL_TREE;
19210
19211 val_expr = DECL_VALUE_EXPR (decl);
19212 if (TREE_CODE (val_expr) != COMPONENT_REF)
19213 return NULL_TREE;
19214
19215 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19216 &unsignedp, &reversep, &volatilep);
19217
19218 if (cvar == NULL_TREE
19219 || !VAR_P (cvar)
19220 || DECL_ARTIFICIAL (cvar)
19221 || !TREE_PUBLIC (cvar))
19222 return NULL_TREE;
19223
19224 *value = 0;
19225 if (offset != NULL)
19226 {
19227 if (!tree_fits_shwi_p (offset))
19228 return NULL_TREE;
19229 *value = tree_to_shwi (offset);
19230 }
19231 if (bitpos != 0)
19232 *value += bitpos / BITS_PER_UNIT;
19233
19234 return cvar;
19235 }
19236
19237 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19238 data attribute for a variable or a parameter. We generate the
19239 DW_AT_const_value attribute only in those cases where the given variable
19240 or parameter does not have a true "location" either in memory or in a
19241 register. This can happen (for example) when a constant is passed as an
19242 actual argument in a call to an inline function. (It's possible that
19243 these things can crop up in other ways also.) Note that one type of
19244 constant value which can be passed into an inlined function is a constant
19245 pointer. This can happen for example if an actual argument in an inlined
19246 function call evaluates to a compile-time constant address.
19247
19248 CACHE_P is true if it is worth caching the location list for DECL,
19249 so that future calls can reuse it rather than regenerate it from scratch.
19250 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19251 since we will need to refer to them each time the function is inlined. */
19252
19253 static bool
19254 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19255 {
19256 rtx rtl;
19257 dw_loc_list_ref list;
19258 var_loc_list *loc_list;
19259 cached_dw_loc_list *cache;
19260
19261 if (early_dwarf)
19262 return false;
19263
19264 if (TREE_CODE (decl) == ERROR_MARK)
19265 return false;
19266
19267 if (get_AT (die, DW_AT_location)
19268 || get_AT (die, DW_AT_const_value))
19269 return true;
19270
19271 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19272 || TREE_CODE (decl) == RESULT_DECL);
19273
19274 /* Try to get some constant RTL for this decl, and use that as the value of
19275 the location. */
19276
19277 rtl = rtl_for_decl_location (decl);
19278 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19279 && add_const_value_attribute (die, rtl))
19280 return true;
19281
19282 /* See if we have single element location list that is equivalent to
19283 a constant value. That way we are better to use add_const_value_attribute
19284 rather than expanding constant value equivalent. */
19285 loc_list = lookup_decl_loc (decl);
19286 if (loc_list
19287 && loc_list->first
19288 && loc_list->first->next == NULL
19289 && NOTE_P (loc_list->first->loc)
19290 && NOTE_VAR_LOCATION (loc_list->first->loc)
19291 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19292 {
19293 struct var_loc_node *node;
19294
19295 node = loc_list->first;
19296 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19297 if (GET_CODE (rtl) == EXPR_LIST)
19298 rtl = XEXP (rtl, 0);
19299 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19300 && add_const_value_attribute (die, rtl))
19301 return true;
19302 }
19303 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19304 list several times. See if we've already cached the contents. */
19305 list = NULL;
19306 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19307 cache_p = false;
19308 if (cache_p)
19309 {
19310 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19311 if (cache)
19312 list = cache->loc_list;
19313 }
19314 if (list == NULL)
19315 {
19316 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19317 NULL);
19318 /* It is usually worth caching this result if the decl is from
19319 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19320 if (cache_p && list && list->dw_loc_next)
19321 {
19322 cached_dw_loc_list **slot
19323 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19324 DECL_UID (decl),
19325 INSERT);
19326 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19327 cache->decl_id = DECL_UID (decl);
19328 cache->loc_list = list;
19329 *slot = cache;
19330 }
19331 }
19332 if (list)
19333 {
19334 add_AT_location_description (die, DW_AT_location, list);
19335 return true;
19336 }
19337 /* None of that worked, so it must not really have a location;
19338 try adding a constant value attribute from the DECL_INITIAL. */
19339 return tree_add_const_value_attribute_for_decl (die, decl);
19340 }
19341
19342 /* Helper function for tree_add_const_value_attribute. Natively encode
19343 initializer INIT into an array. Return true if successful. */
19344
19345 static bool
19346 native_encode_initializer (tree init, unsigned char *array, int size)
19347 {
19348 tree type;
19349
19350 if (init == NULL_TREE)
19351 return false;
19352
19353 STRIP_NOPS (init);
19354 switch (TREE_CODE (init))
19355 {
19356 case STRING_CST:
19357 type = TREE_TYPE (init);
19358 if (TREE_CODE (type) == ARRAY_TYPE)
19359 {
19360 tree enttype = TREE_TYPE (type);
19361 scalar_int_mode mode;
19362
19363 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19364 || GET_MODE_SIZE (mode) != 1)
19365 return false;
19366 if (int_size_in_bytes (type) != size)
19367 return false;
19368 if (size > TREE_STRING_LENGTH (init))
19369 {
19370 memcpy (array, TREE_STRING_POINTER (init),
19371 TREE_STRING_LENGTH (init));
19372 memset (array + TREE_STRING_LENGTH (init),
19373 '\0', size - TREE_STRING_LENGTH (init));
19374 }
19375 else
19376 memcpy (array, TREE_STRING_POINTER (init), size);
19377 return true;
19378 }
19379 return false;
19380 case CONSTRUCTOR:
19381 type = TREE_TYPE (init);
19382 if (int_size_in_bytes (type) != size)
19383 return false;
19384 if (TREE_CODE (type) == ARRAY_TYPE)
19385 {
19386 HOST_WIDE_INT min_index;
19387 unsigned HOST_WIDE_INT cnt;
19388 int curpos = 0, fieldsize;
19389 constructor_elt *ce;
19390
19391 if (TYPE_DOMAIN (type) == NULL_TREE
19392 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19393 return false;
19394
19395 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19396 if (fieldsize <= 0)
19397 return false;
19398
19399 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19400 memset (array, '\0', size);
19401 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19402 {
19403 tree val = ce->value;
19404 tree index = ce->index;
19405 int pos = curpos;
19406 if (index && TREE_CODE (index) == RANGE_EXPR)
19407 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19408 * fieldsize;
19409 else if (index)
19410 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19411
19412 if (val)
19413 {
19414 STRIP_NOPS (val);
19415 if (!native_encode_initializer (val, array + pos, fieldsize))
19416 return false;
19417 }
19418 curpos = pos + fieldsize;
19419 if (index && TREE_CODE (index) == RANGE_EXPR)
19420 {
19421 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19422 - tree_to_shwi (TREE_OPERAND (index, 0));
19423 while (count-- > 0)
19424 {
19425 if (val)
19426 memcpy (array + curpos, array + pos, fieldsize);
19427 curpos += fieldsize;
19428 }
19429 }
19430 gcc_assert (curpos <= size);
19431 }
19432 return true;
19433 }
19434 else if (TREE_CODE (type) == RECORD_TYPE
19435 || TREE_CODE (type) == UNION_TYPE)
19436 {
19437 tree field = NULL_TREE;
19438 unsigned HOST_WIDE_INT cnt;
19439 constructor_elt *ce;
19440
19441 if (int_size_in_bytes (type) != size)
19442 return false;
19443
19444 if (TREE_CODE (type) == RECORD_TYPE)
19445 field = TYPE_FIELDS (type);
19446
19447 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19448 {
19449 tree val = ce->value;
19450 int pos, fieldsize;
19451
19452 if (ce->index != 0)
19453 field = ce->index;
19454
19455 if (val)
19456 STRIP_NOPS (val);
19457
19458 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19459 return false;
19460
19461 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19462 && TYPE_DOMAIN (TREE_TYPE (field))
19463 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19464 return false;
19465 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19466 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19467 return false;
19468 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19469 pos = int_byte_position (field);
19470 gcc_assert (pos + fieldsize <= size);
19471 if (val && fieldsize != 0
19472 && !native_encode_initializer (val, array + pos, fieldsize))
19473 return false;
19474 }
19475 return true;
19476 }
19477 return false;
19478 case VIEW_CONVERT_EXPR:
19479 case NON_LVALUE_EXPR:
19480 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19481 default:
19482 return native_encode_expr (init, array, size) == size;
19483 }
19484 }
19485
19486 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19487 attribute is the const value T. */
19488
19489 static bool
19490 tree_add_const_value_attribute (dw_die_ref die, tree t)
19491 {
19492 tree init;
19493 tree type = TREE_TYPE (t);
19494 rtx rtl;
19495
19496 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19497 return false;
19498
19499 init = t;
19500 gcc_assert (!DECL_P (init));
19501
19502 if (TREE_CODE (init) == INTEGER_CST)
19503 {
19504 if (tree_fits_uhwi_p (init))
19505 {
19506 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19507 return true;
19508 }
19509 if (tree_fits_shwi_p (init))
19510 {
19511 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19512 return true;
19513 }
19514 }
19515 if (! early_dwarf)
19516 {
19517 rtl = rtl_for_decl_init (init, type);
19518 if (rtl)
19519 return add_const_value_attribute (die, rtl);
19520 }
19521 /* If the host and target are sane, try harder. */
19522 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19523 && initializer_constant_valid_p (init, type))
19524 {
19525 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19526 if (size > 0 && (int) size == size)
19527 {
19528 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19529
19530 if (native_encode_initializer (init, array, size))
19531 {
19532 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19533 return true;
19534 }
19535 ggc_free (array);
19536 }
19537 }
19538 return false;
19539 }
19540
19541 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19542 attribute is the const value of T, where T is an integral constant
19543 variable with static storage duration
19544 (so it can't be a PARM_DECL or a RESULT_DECL). */
19545
19546 static bool
19547 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19548 {
19549
19550 if (!decl
19551 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19552 || (VAR_P (decl) && !TREE_STATIC (decl)))
19553 return false;
19554
19555 if (TREE_READONLY (decl)
19556 && ! TREE_THIS_VOLATILE (decl)
19557 && DECL_INITIAL (decl))
19558 /* OK */;
19559 else
19560 return false;
19561
19562 /* Don't add DW_AT_const_value if abstract origin already has one. */
19563 if (get_AT (var_die, DW_AT_const_value))
19564 return false;
19565
19566 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19567 }
19568
19569 /* Convert the CFI instructions for the current function into a
19570 location list. This is used for DW_AT_frame_base when we targeting
19571 a dwarf2 consumer that does not support the dwarf3
19572 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19573 expressions. */
19574
19575 static dw_loc_list_ref
19576 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19577 {
19578 int ix;
19579 dw_fde_ref fde;
19580 dw_loc_list_ref list, *list_tail;
19581 dw_cfi_ref cfi;
19582 dw_cfa_location last_cfa, next_cfa;
19583 const char *start_label, *last_label, *section;
19584 dw_cfa_location remember;
19585
19586 fde = cfun->fde;
19587 gcc_assert (fde != NULL);
19588
19589 section = secname_for_decl (current_function_decl);
19590 list_tail = &list;
19591 list = NULL;
19592
19593 memset (&next_cfa, 0, sizeof (next_cfa));
19594 next_cfa.reg = INVALID_REGNUM;
19595 remember = next_cfa;
19596
19597 start_label = fde->dw_fde_begin;
19598
19599 /* ??? Bald assumption that the CIE opcode list does not contain
19600 advance opcodes. */
19601 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19602 lookup_cfa_1 (cfi, &next_cfa, &remember);
19603
19604 last_cfa = next_cfa;
19605 last_label = start_label;
19606
19607 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19608 {
19609 /* If the first partition contained no CFI adjustments, the
19610 CIE opcodes apply to the whole first partition. */
19611 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19612 fde->dw_fde_begin, fde->dw_fde_end, section);
19613 list_tail =&(*list_tail)->dw_loc_next;
19614 start_label = last_label = fde->dw_fde_second_begin;
19615 }
19616
19617 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19618 {
19619 switch (cfi->dw_cfi_opc)
19620 {
19621 case DW_CFA_set_loc:
19622 case DW_CFA_advance_loc1:
19623 case DW_CFA_advance_loc2:
19624 case DW_CFA_advance_loc4:
19625 if (!cfa_equal_p (&last_cfa, &next_cfa))
19626 {
19627 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19628 start_label, last_label, section);
19629
19630 list_tail = &(*list_tail)->dw_loc_next;
19631 last_cfa = next_cfa;
19632 start_label = last_label;
19633 }
19634 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19635 break;
19636
19637 case DW_CFA_advance_loc:
19638 /* The encoding is complex enough that we should never emit this. */
19639 gcc_unreachable ();
19640
19641 default:
19642 lookup_cfa_1 (cfi, &next_cfa, &remember);
19643 break;
19644 }
19645 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19646 {
19647 if (!cfa_equal_p (&last_cfa, &next_cfa))
19648 {
19649 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19650 start_label, last_label, section);
19651
19652 list_tail = &(*list_tail)->dw_loc_next;
19653 last_cfa = next_cfa;
19654 start_label = last_label;
19655 }
19656 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19657 start_label, fde->dw_fde_end, section);
19658 list_tail = &(*list_tail)->dw_loc_next;
19659 start_label = last_label = fde->dw_fde_second_begin;
19660 }
19661 }
19662
19663 if (!cfa_equal_p (&last_cfa, &next_cfa))
19664 {
19665 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19666 start_label, last_label, section);
19667 list_tail = &(*list_tail)->dw_loc_next;
19668 start_label = last_label;
19669 }
19670
19671 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19672 start_label,
19673 fde->dw_fde_second_begin
19674 ? fde->dw_fde_second_end : fde->dw_fde_end,
19675 section);
19676
19677 if (list && list->dw_loc_next)
19678 gen_llsym (list);
19679
19680 return list;
19681 }
19682
19683 /* Compute a displacement from the "steady-state frame pointer" to the
19684 frame base (often the same as the CFA), and store it in
19685 frame_pointer_fb_offset. OFFSET is added to the displacement
19686 before the latter is negated. */
19687
19688 static void
19689 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19690 {
19691 rtx reg, elim;
19692
19693 #ifdef FRAME_POINTER_CFA_OFFSET
19694 reg = frame_pointer_rtx;
19695 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19696 #else
19697 reg = arg_pointer_rtx;
19698 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19699 #endif
19700
19701 elim = (ira_use_lra_p
19702 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19703 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19704 if (GET_CODE (elim) == PLUS)
19705 {
19706 offset += INTVAL (XEXP (elim, 1));
19707 elim = XEXP (elim, 0);
19708 }
19709
19710 frame_pointer_fb_offset = -offset;
19711
19712 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19713 in which to eliminate. This is because it's stack pointer isn't
19714 directly accessible as a register within the ISA. To work around
19715 this, assume that while we cannot provide a proper value for
19716 frame_pointer_fb_offset, we won't need one either. */
19717 frame_pointer_fb_offset_valid
19718 = ((SUPPORTS_STACK_ALIGNMENT
19719 && (elim == hard_frame_pointer_rtx
19720 || elim == stack_pointer_rtx))
19721 || elim == (frame_pointer_needed
19722 ? hard_frame_pointer_rtx
19723 : stack_pointer_rtx));
19724 }
19725
19726 /* Generate a DW_AT_name attribute given some string value to be included as
19727 the value of the attribute. */
19728
19729 static void
19730 add_name_attribute (dw_die_ref die, const char *name_string)
19731 {
19732 if (name_string != NULL && *name_string != 0)
19733 {
19734 if (demangle_name_func)
19735 name_string = (*demangle_name_func) (name_string);
19736
19737 add_AT_string (die, DW_AT_name, name_string);
19738 }
19739 }
19740
19741 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19742 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19743 of TYPE accordingly.
19744
19745 ??? This is a temporary measure until after we're able to generate
19746 regular DWARF for the complex Ada type system. */
19747
19748 static void
19749 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19750 dw_die_ref context_die)
19751 {
19752 tree dtype;
19753 dw_die_ref dtype_die;
19754
19755 if (!lang_hooks.types.descriptive_type)
19756 return;
19757
19758 dtype = lang_hooks.types.descriptive_type (type);
19759 if (!dtype)
19760 return;
19761
19762 dtype_die = lookup_type_die (dtype);
19763 if (!dtype_die)
19764 {
19765 gen_type_die (dtype, context_die);
19766 dtype_die = lookup_type_die (dtype);
19767 gcc_assert (dtype_die);
19768 }
19769
19770 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19771 }
19772
19773 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19774
19775 static const char *
19776 comp_dir_string (void)
19777 {
19778 const char *wd;
19779 char *wd1;
19780 static const char *cached_wd = NULL;
19781
19782 if (cached_wd != NULL)
19783 return cached_wd;
19784
19785 wd = get_src_pwd ();
19786 if (wd == NULL)
19787 return NULL;
19788
19789 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19790 {
19791 int wdlen;
19792
19793 wdlen = strlen (wd);
19794 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19795 strcpy (wd1, wd);
19796 wd1 [wdlen] = DIR_SEPARATOR;
19797 wd1 [wdlen + 1] = 0;
19798 wd = wd1;
19799 }
19800
19801 cached_wd = remap_debug_filename (wd);
19802 return cached_wd;
19803 }
19804
19805 /* Generate a DW_AT_comp_dir attribute for DIE. */
19806
19807 static void
19808 add_comp_dir_attribute (dw_die_ref die)
19809 {
19810 const char * wd = comp_dir_string ();
19811 if (wd != NULL)
19812 add_AT_string (die, DW_AT_comp_dir, wd);
19813 }
19814
19815 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19816 pointer computation, ...), output a representation for that bound according
19817 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19818 loc_list_from_tree for the meaning of CONTEXT. */
19819
19820 static void
19821 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19822 int forms, struct loc_descr_context *context)
19823 {
19824 dw_die_ref context_die, decl_die;
19825 dw_loc_list_ref list;
19826 bool strip_conversions = true;
19827 bool placeholder_seen = false;
19828
19829 while (strip_conversions)
19830 switch (TREE_CODE (value))
19831 {
19832 case ERROR_MARK:
19833 case SAVE_EXPR:
19834 return;
19835
19836 CASE_CONVERT:
19837 case VIEW_CONVERT_EXPR:
19838 value = TREE_OPERAND (value, 0);
19839 break;
19840
19841 default:
19842 strip_conversions = false;
19843 break;
19844 }
19845
19846 /* If possible and permitted, output the attribute as a constant. */
19847 if ((forms & dw_scalar_form_constant) != 0
19848 && TREE_CODE (value) == INTEGER_CST)
19849 {
19850 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19851
19852 /* If HOST_WIDE_INT is big enough then represent the bound as
19853 a constant value. We need to choose a form based on
19854 whether the type is signed or unsigned. We cannot just
19855 call add_AT_unsigned if the value itself is positive
19856 (add_AT_unsigned might add the unsigned value encoded as
19857 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19858 bounds type and then sign extend any unsigned values found
19859 for signed types. This is needed only for
19860 DW_AT_{lower,upper}_bound, since for most other attributes,
19861 consumers will treat DW_FORM_data[1248] as unsigned values,
19862 regardless of the underlying type. */
19863 if (prec <= HOST_BITS_PER_WIDE_INT
19864 || tree_fits_uhwi_p (value))
19865 {
19866 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19867 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19868 else
19869 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19870 }
19871 else
19872 /* Otherwise represent the bound as an unsigned value with
19873 the precision of its type. The precision and signedness
19874 of the type will be necessary to re-interpret it
19875 unambiguously. */
19876 add_AT_wide (die, attr, wi::to_wide (value));
19877 return;
19878 }
19879
19880 /* Otherwise, if it's possible and permitted too, output a reference to
19881 another DIE. */
19882 if ((forms & dw_scalar_form_reference) != 0)
19883 {
19884 tree decl = NULL_TREE;
19885
19886 /* Some type attributes reference an outer type. For instance, the upper
19887 bound of an array may reference an embedding record (this happens in
19888 Ada). */
19889 if (TREE_CODE (value) == COMPONENT_REF
19890 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19891 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19892 decl = TREE_OPERAND (value, 1);
19893
19894 else if (VAR_P (value)
19895 || TREE_CODE (value) == PARM_DECL
19896 || TREE_CODE (value) == RESULT_DECL)
19897 decl = value;
19898
19899 if (decl != NULL_TREE)
19900 {
19901 dw_die_ref decl_die = lookup_decl_die (decl);
19902
19903 /* ??? Can this happen, or should the variable have been bound
19904 first? Probably it can, since I imagine that we try to create
19905 the types of parameters in the order in which they exist in
19906 the list, and won't have created a forward reference to a
19907 later parameter. */
19908 if (decl_die != NULL)
19909 {
19910 add_AT_die_ref (die, attr, decl_die);
19911 return;
19912 }
19913 }
19914 }
19915
19916 /* Last chance: try to create a stack operation procedure to evaluate the
19917 value. Do nothing if even that is not possible or permitted. */
19918 if ((forms & dw_scalar_form_exprloc) == 0)
19919 return;
19920
19921 list = loc_list_from_tree (value, 2, context);
19922 if (context && context->placeholder_arg)
19923 {
19924 placeholder_seen = context->placeholder_seen;
19925 context->placeholder_seen = false;
19926 }
19927 if (list == NULL || single_element_loc_list_p (list))
19928 {
19929 /* If this attribute is not a reference nor constant, it is
19930 a DWARF expression rather than location description. For that
19931 loc_list_from_tree (value, 0, &context) is needed. */
19932 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19933 if (list2 && single_element_loc_list_p (list2))
19934 {
19935 if (placeholder_seen)
19936 {
19937 struct dwarf_procedure_info dpi;
19938 dpi.fndecl = NULL_TREE;
19939 dpi.args_count = 1;
19940 if (!resolve_args_picking (list2->expr, 1, &dpi))
19941 return;
19942 }
19943 add_AT_loc (die, attr, list2->expr);
19944 return;
19945 }
19946 }
19947
19948 /* If that failed to give a single element location list, fall back to
19949 outputting this as a reference... still if permitted. */
19950 if (list == NULL
19951 || (forms & dw_scalar_form_reference) == 0
19952 || placeholder_seen)
19953 return;
19954
19955 if (current_function_decl == 0)
19956 context_die = comp_unit_die ();
19957 else
19958 context_die = lookup_decl_die (current_function_decl);
19959
19960 decl_die = new_die (DW_TAG_variable, context_die, value);
19961 add_AT_flag (decl_die, DW_AT_artificial, 1);
19962 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19963 context_die);
19964 add_AT_location_description (decl_die, DW_AT_location, list);
19965 add_AT_die_ref (die, attr, decl_die);
19966 }
19967
19968 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19969 default. */
19970
19971 static int
19972 lower_bound_default (void)
19973 {
19974 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19975 {
19976 case DW_LANG_C:
19977 case DW_LANG_C89:
19978 case DW_LANG_C99:
19979 case DW_LANG_C11:
19980 case DW_LANG_C_plus_plus:
19981 case DW_LANG_C_plus_plus_11:
19982 case DW_LANG_C_plus_plus_14:
19983 case DW_LANG_ObjC:
19984 case DW_LANG_ObjC_plus_plus:
19985 return 0;
19986 case DW_LANG_Fortran77:
19987 case DW_LANG_Fortran90:
19988 case DW_LANG_Fortran95:
19989 case DW_LANG_Fortran03:
19990 case DW_LANG_Fortran08:
19991 return 1;
19992 case DW_LANG_UPC:
19993 case DW_LANG_D:
19994 case DW_LANG_Python:
19995 return dwarf_version >= 4 ? 0 : -1;
19996 case DW_LANG_Ada95:
19997 case DW_LANG_Ada83:
19998 case DW_LANG_Cobol74:
19999 case DW_LANG_Cobol85:
20000 case DW_LANG_Modula2:
20001 case DW_LANG_PLI:
20002 return dwarf_version >= 4 ? 1 : -1;
20003 default:
20004 return -1;
20005 }
20006 }
20007
20008 /* Given a tree node describing an array bound (either lower or upper) output
20009 a representation for that bound. */
20010
20011 static void
20012 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20013 tree bound, struct loc_descr_context *context)
20014 {
20015 int dflt;
20016
20017 while (1)
20018 switch (TREE_CODE (bound))
20019 {
20020 /* Strip all conversions. */
20021 CASE_CONVERT:
20022 case VIEW_CONVERT_EXPR:
20023 bound = TREE_OPERAND (bound, 0);
20024 break;
20025
20026 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20027 are even omitted when they are the default. */
20028 case INTEGER_CST:
20029 /* If the value for this bound is the default one, we can even omit the
20030 attribute. */
20031 if (bound_attr == DW_AT_lower_bound
20032 && tree_fits_shwi_p (bound)
20033 && (dflt = lower_bound_default ()) != -1
20034 && tree_to_shwi (bound) == dflt)
20035 return;
20036
20037 /* FALLTHRU */
20038
20039 default:
20040 /* Because of the complex interaction there can be with other GNAT
20041 encodings, GDB isn't ready yet to handle proper DWARF description
20042 for self-referencial subrange bounds: let GNAT encodings do the
20043 magic in such a case. */
20044 if (is_ada ()
20045 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20046 && contains_placeholder_p (bound))
20047 return;
20048
20049 add_scalar_info (subrange_die, bound_attr, bound,
20050 dw_scalar_form_constant
20051 | dw_scalar_form_exprloc
20052 | dw_scalar_form_reference,
20053 context);
20054 return;
20055 }
20056 }
20057
20058 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20059 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20060 Note that the block of subscript information for an array type also
20061 includes information about the element type of the given array type.
20062
20063 This function reuses previously set type and bound information if
20064 available. */
20065
20066 static void
20067 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20068 {
20069 unsigned dimension_number;
20070 tree lower, upper;
20071 dw_die_ref child = type_die->die_child;
20072
20073 for (dimension_number = 0;
20074 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20075 type = TREE_TYPE (type), dimension_number++)
20076 {
20077 tree domain = TYPE_DOMAIN (type);
20078
20079 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20080 break;
20081
20082 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20083 and (in GNU C only) variable bounds. Handle all three forms
20084 here. */
20085
20086 /* Find and reuse a previously generated DW_TAG_subrange_type if
20087 available.
20088
20089 For multi-dimensional arrays, as we iterate through the
20090 various dimensions in the enclosing for loop above, we also
20091 iterate through the DIE children and pick at each
20092 DW_TAG_subrange_type previously generated (if available).
20093 Each child DW_TAG_subrange_type DIE describes the range of
20094 the current dimension. At this point we should have as many
20095 DW_TAG_subrange_type's as we have dimensions in the
20096 array. */
20097 dw_die_ref subrange_die = NULL;
20098 if (child)
20099 while (1)
20100 {
20101 child = child->die_sib;
20102 if (child->die_tag == DW_TAG_subrange_type)
20103 subrange_die = child;
20104 if (child == type_die->die_child)
20105 {
20106 /* If we wrapped around, stop looking next time. */
20107 child = NULL;
20108 break;
20109 }
20110 if (child->die_tag == DW_TAG_subrange_type)
20111 break;
20112 }
20113 if (!subrange_die)
20114 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20115
20116 if (domain)
20117 {
20118 /* We have an array type with specified bounds. */
20119 lower = TYPE_MIN_VALUE (domain);
20120 upper = TYPE_MAX_VALUE (domain);
20121
20122 /* Define the index type. */
20123 if (TREE_TYPE (domain)
20124 && !get_AT (subrange_die, DW_AT_type))
20125 {
20126 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20127 TREE_TYPE field. We can't emit debug info for this
20128 because it is an unnamed integral type. */
20129 if (TREE_CODE (domain) == INTEGER_TYPE
20130 && TYPE_NAME (domain) == NULL_TREE
20131 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20132 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20133 ;
20134 else
20135 add_type_attribute (subrange_die, TREE_TYPE (domain),
20136 TYPE_UNQUALIFIED, false, type_die);
20137 }
20138
20139 /* ??? If upper is NULL, the array has unspecified length,
20140 but it does have a lower bound. This happens with Fortran
20141 dimension arr(N:*)
20142 Since the debugger is definitely going to need to know N
20143 to produce useful results, go ahead and output the lower
20144 bound solo, and hope the debugger can cope. */
20145
20146 if (!get_AT (subrange_die, DW_AT_lower_bound))
20147 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20148 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20149 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20150 }
20151
20152 /* Otherwise we have an array type with an unspecified length. The
20153 DWARF-2 spec does not say how to handle this; let's just leave out the
20154 bounds. */
20155 }
20156 }
20157
20158 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20159
20160 static void
20161 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20162 {
20163 dw_die_ref decl_die;
20164 HOST_WIDE_INT size;
20165 dw_loc_descr_ref size_expr = NULL;
20166
20167 switch (TREE_CODE (tree_node))
20168 {
20169 case ERROR_MARK:
20170 size = 0;
20171 break;
20172 case ENUMERAL_TYPE:
20173 case RECORD_TYPE:
20174 case UNION_TYPE:
20175 case QUAL_UNION_TYPE:
20176 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20177 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20178 {
20179 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20180 return;
20181 }
20182 size_expr = type_byte_size (tree_node, &size);
20183 break;
20184 case FIELD_DECL:
20185 /* For a data member of a struct or union, the DW_AT_byte_size is
20186 generally given as the number of bytes normally allocated for an
20187 object of the *declared* type of the member itself. This is true
20188 even for bit-fields. */
20189 size = int_size_in_bytes (field_type (tree_node));
20190 break;
20191 default:
20192 gcc_unreachable ();
20193 }
20194
20195 /* Support for dynamically-sized objects was introduced by DWARFv3.
20196 At the moment, GDB does not handle variable byte sizes very well,
20197 though. */
20198 if ((dwarf_version >= 3 || !dwarf_strict)
20199 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20200 && size_expr != NULL)
20201 add_AT_loc (die, DW_AT_byte_size, size_expr);
20202
20203 /* Note that `size' might be -1 when we get to this point. If it is, that
20204 indicates that the byte size of the entity in question is variable and
20205 that we could not generate a DWARF expression that computes it. */
20206 if (size >= 0)
20207 add_AT_unsigned (die, DW_AT_byte_size, size);
20208 }
20209
20210 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20211 alignment. */
20212
20213 static void
20214 add_alignment_attribute (dw_die_ref die, tree tree_node)
20215 {
20216 if (dwarf_version < 5 && dwarf_strict)
20217 return;
20218
20219 unsigned align;
20220
20221 if (DECL_P (tree_node))
20222 {
20223 if (!DECL_USER_ALIGN (tree_node))
20224 return;
20225
20226 align = DECL_ALIGN_UNIT (tree_node);
20227 }
20228 else if (TYPE_P (tree_node))
20229 {
20230 if (!TYPE_USER_ALIGN (tree_node))
20231 return;
20232
20233 align = TYPE_ALIGN_UNIT (tree_node);
20234 }
20235 else
20236 gcc_unreachable ();
20237
20238 add_AT_unsigned (die, DW_AT_alignment, align);
20239 }
20240
20241 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20242 which specifies the distance in bits from the highest order bit of the
20243 "containing object" for the bit-field to the highest order bit of the
20244 bit-field itself.
20245
20246 For any given bit-field, the "containing object" is a hypothetical object
20247 (of some integral or enum type) within which the given bit-field lives. The
20248 type of this hypothetical "containing object" is always the same as the
20249 declared type of the individual bit-field itself. The determination of the
20250 exact location of the "containing object" for a bit-field is rather
20251 complicated. It's handled by the `field_byte_offset' function (above).
20252
20253 CTX is required: see the comment for VLR_CONTEXT.
20254
20255 Note that it is the size (in bytes) of the hypothetical "containing object"
20256 which will be given in the DW_AT_byte_size attribute for this bit-field.
20257 (See `byte_size_attribute' above). */
20258
20259 static inline void
20260 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20261 {
20262 HOST_WIDE_INT object_offset_in_bytes;
20263 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20264 HOST_WIDE_INT bitpos_int;
20265 HOST_WIDE_INT highest_order_object_bit_offset;
20266 HOST_WIDE_INT highest_order_field_bit_offset;
20267 HOST_WIDE_INT bit_offset;
20268
20269 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20270
20271 /* Must be a field and a bit field. */
20272 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20273
20274 /* We can't yet handle bit-fields whose offsets are variable, so if we
20275 encounter such things, just return without generating any attribute
20276 whatsoever. Likewise for variable or too large size. */
20277 if (! tree_fits_shwi_p (bit_position (decl))
20278 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20279 return;
20280
20281 bitpos_int = int_bit_position (decl);
20282
20283 /* Note that the bit offset is always the distance (in bits) from the
20284 highest-order bit of the "containing object" to the highest-order bit of
20285 the bit-field itself. Since the "high-order end" of any object or field
20286 is different on big-endian and little-endian machines, the computation
20287 below must take account of these differences. */
20288 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20289 highest_order_field_bit_offset = bitpos_int;
20290
20291 if (! BYTES_BIG_ENDIAN)
20292 {
20293 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20294 highest_order_object_bit_offset +=
20295 simple_type_size_in_bits (original_type);
20296 }
20297
20298 bit_offset
20299 = (! BYTES_BIG_ENDIAN
20300 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20301 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20302
20303 if (bit_offset < 0)
20304 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20305 else
20306 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20307 }
20308
20309 /* For a FIELD_DECL node which represents a bit field, output an attribute
20310 which specifies the length in bits of the given field. */
20311
20312 static inline void
20313 add_bit_size_attribute (dw_die_ref die, tree decl)
20314 {
20315 /* Must be a field and a bit field. */
20316 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20317 && DECL_BIT_FIELD_TYPE (decl));
20318
20319 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20320 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20321 }
20322
20323 /* If the compiled language is ANSI C, then add a 'prototyped'
20324 attribute, if arg types are given for the parameters of a function. */
20325
20326 static inline void
20327 add_prototyped_attribute (dw_die_ref die, tree func_type)
20328 {
20329 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20330 {
20331 case DW_LANG_C:
20332 case DW_LANG_C89:
20333 case DW_LANG_C99:
20334 case DW_LANG_C11:
20335 case DW_LANG_ObjC:
20336 if (prototype_p (func_type))
20337 add_AT_flag (die, DW_AT_prototyped, 1);
20338 break;
20339 default:
20340 break;
20341 }
20342 }
20343
20344 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20345 by looking in the type declaration, the object declaration equate table or
20346 the block mapping. */
20347
20348 static inline dw_die_ref
20349 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20350 {
20351 dw_die_ref origin_die = NULL;
20352
20353 if (DECL_P (origin))
20354 {
20355 dw_die_ref c;
20356 origin_die = lookup_decl_die (origin);
20357 /* "Unwrap" the decls DIE which we put in the imported unit context.
20358 We are looking for the abstract copy here. */
20359 if (in_lto_p
20360 && origin_die
20361 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20362 /* ??? Identify this better. */
20363 && c->with_offset)
20364 origin_die = c;
20365 }
20366 else if (TYPE_P (origin))
20367 origin_die = lookup_type_die (origin);
20368 else if (TREE_CODE (origin) == BLOCK)
20369 origin_die = BLOCK_DIE (origin);
20370
20371 /* XXX: Functions that are never lowered don't always have correct block
20372 trees (in the case of java, they simply have no block tree, in some other
20373 languages). For these functions, there is nothing we can really do to
20374 output correct debug info for inlined functions in all cases. Rather
20375 than die, we'll just produce deficient debug info now, in that we will
20376 have variables without a proper abstract origin. In the future, when all
20377 functions are lowered, we should re-add a gcc_assert (origin_die)
20378 here. */
20379
20380 if (origin_die)
20381 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20382 return origin_die;
20383 }
20384
20385 /* We do not currently support the pure_virtual attribute. */
20386
20387 static inline void
20388 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20389 {
20390 if (DECL_VINDEX (func_decl))
20391 {
20392 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20393
20394 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20395 add_AT_loc (die, DW_AT_vtable_elem_location,
20396 new_loc_descr (DW_OP_constu,
20397 tree_to_shwi (DECL_VINDEX (func_decl)),
20398 0));
20399
20400 /* GNU extension: Record what type this method came from originally. */
20401 if (debug_info_level > DINFO_LEVEL_TERSE
20402 && DECL_CONTEXT (func_decl))
20403 add_AT_die_ref (die, DW_AT_containing_type,
20404 lookup_type_die (DECL_CONTEXT (func_decl)));
20405 }
20406 }
20407 \f
20408 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20409 given decl. This used to be a vendor extension until after DWARF 4
20410 standardized it. */
20411
20412 static void
20413 add_linkage_attr (dw_die_ref die, tree decl)
20414 {
20415 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20416
20417 /* Mimic what assemble_name_raw does with a leading '*'. */
20418 if (name[0] == '*')
20419 name = &name[1];
20420
20421 if (dwarf_version >= 4)
20422 add_AT_string (die, DW_AT_linkage_name, name);
20423 else
20424 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20425 }
20426
20427 /* Add source coordinate attributes for the given decl. */
20428
20429 static void
20430 add_src_coords_attributes (dw_die_ref die, tree decl)
20431 {
20432 expanded_location s;
20433
20434 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20435 return;
20436 s = expand_location (DECL_SOURCE_LOCATION (decl));
20437 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20438 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20439 if (debug_column_info && s.column)
20440 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20441 }
20442
20443 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20444
20445 static void
20446 add_linkage_name_raw (dw_die_ref die, tree decl)
20447 {
20448 /* Defer until we have an assembler name set. */
20449 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20450 {
20451 limbo_die_node *asm_name;
20452
20453 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20454 asm_name->die = die;
20455 asm_name->created_for = decl;
20456 asm_name->next = deferred_asm_name;
20457 deferred_asm_name = asm_name;
20458 }
20459 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20460 add_linkage_attr (die, decl);
20461 }
20462
20463 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20464
20465 static void
20466 add_linkage_name (dw_die_ref die, tree decl)
20467 {
20468 if (debug_info_level > DINFO_LEVEL_NONE
20469 && VAR_OR_FUNCTION_DECL_P (decl)
20470 && TREE_PUBLIC (decl)
20471 && !(VAR_P (decl) && DECL_REGISTER (decl))
20472 && die->die_tag != DW_TAG_member)
20473 add_linkage_name_raw (die, decl);
20474 }
20475
20476 /* Add a DW_AT_name attribute and source coordinate attribute for the
20477 given decl, but only if it actually has a name. */
20478
20479 static void
20480 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20481 bool no_linkage_name)
20482 {
20483 tree decl_name;
20484
20485 decl_name = DECL_NAME (decl);
20486 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20487 {
20488 const char *name = dwarf2_name (decl, 0);
20489 if (name)
20490 add_name_attribute (die, name);
20491 if (! DECL_ARTIFICIAL (decl))
20492 add_src_coords_attributes (die, decl);
20493
20494 if (!no_linkage_name)
20495 add_linkage_name (die, decl);
20496 }
20497
20498 #ifdef VMS_DEBUGGING_INFO
20499 /* Get the function's name, as described by its RTL. This may be different
20500 from the DECL_NAME name used in the source file. */
20501 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20502 {
20503 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20504 XEXP (DECL_RTL (decl), 0), false);
20505 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20506 }
20507 #endif /* VMS_DEBUGGING_INFO */
20508 }
20509
20510 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20511
20512 static void
20513 add_discr_value (dw_die_ref die, dw_discr_value *value)
20514 {
20515 dw_attr_node attr;
20516
20517 attr.dw_attr = DW_AT_discr_value;
20518 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20519 attr.dw_attr_val.val_entry = NULL;
20520 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20521 if (value->pos)
20522 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20523 else
20524 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20525 add_dwarf_attr (die, &attr);
20526 }
20527
20528 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20529
20530 static void
20531 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20532 {
20533 dw_attr_node attr;
20534
20535 attr.dw_attr = DW_AT_discr_list;
20536 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20537 attr.dw_attr_val.val_entry = NULL;
20538 attr.dw_attr_val.v.val_discr_list = discr_list;
20539 add_dwarf_attr (die, &attr);
20540 }
20541
20542 static inline dw_discr_list_ref
20543 AT_discr_list (dw_attr_node *attr)
20544 {
20545 return attr->dw_attr_val.v.val_discr_list;
20546 }
20547
20548 #ifdef VMS_DEBUGGING_INFO
20549 /* Output the debug main pointer die for VMS */
20550
20551 void
20552 dwarf2out_vms_debug_main_pointer (void)
20553 {
20554 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20555 dw_die_ref die;
20556
20557 /* Allocate the VMS debug main subprogram die. */
20558 die = new_die_raw (DW_TAG_subprogram);
20559 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20560 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20561 current_function_funcdef_no);
20562 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20563
20564 /* Make it the first child of comp_unit_die (). */
20565 die->die_parent = comp_unit_die ();
20566 if (comp_unit_die ()->die_child)
20567 {
20568 die->die_sib = comp_unit_die ()->die_child->die_sib;
20569 comp_unit_die ()->die_child->die_sib = die;
20570 }
20571 else
20572 {
20573 die->die_sib = die;
20574 comp_unit_die ()->die_child = die;
20575 }
20576 }
20577 #endif /* VMS_DEBUGGING_INFO */
20578
20579 /* Push a new declaration scope. */
20580
20581 static void
20582 push_decl_scope (tree scope)
20583 {
20584 vec_safe_push (decl_scope_table, scope);
20585 }
20586
20587 /* Pop a declaration scope. */
20588
20589 static inline void
20590 pop_decl_scope (void)
20591 {
20592 decl_scope_table->pop ();
20593 }
20594
20595 /* walk_tree helper function for uses_local_type, below. */
20596
20597 static tree
20598 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20599 {
20600 if (!TYPE_P (*tp))
20601 *walk_subtrees = 0;
20602 else
20603 {
20604 tree name = TYPE_NAME (*tp);
20605 if (name && DECL_P (name) && decl_function_context (name))
20606 return *tp;
20607 }
20608 return NULL_TREE;
20609 }
20610
20611 /* If TYPE involves a function-local type (including a local typedef to a
20612 non-local type), returns that type; otherwise returns NULL_TREE. */
20613
20614 static tree
20615 uses_local_type (tree type)
20616 {
20617 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20618 return used;
20619 }
20620
20621 /* Return the DIE for the scope that immediately contains this type.
20622 Non-named types that do not involve a function-local type get global
20623 scope. Named types nested in namespaces or other types get their
20624 containing scope. All other types (i.e. function-local named types) get
20625 the current active scope. */
20626
20627 static dw_die_ref
20628 scope_die_for (tree t, dw_die_ref context_die)
20629 {
20630 dw_die_ref scope_die = NULL;
20631 tree containing_scope;
20632
20633 /* Non-types always go in the current scope. */
20634 gcc_assert (TYPE_P (t));
20635
20636 /* Use the scope of the typedef, rather than the scope of the type
20637 it refers to. */
20638 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20639 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20640 else
20641 containing_scope = TYPE_CONTEXT (t);
20642
20643 /* Use the containing namespace if there is one. */
20644 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20645 {
20646 if (context_die == lookup_decl_die (containing_scope))
20647 /* OK */;
20648 else if (debug_info_level > DINFO_LEVEL_TERSE)
20649 context_die = get_context_die (containing_scope);
20650 else
20651 containing_scope = NULL_TREE;
20652 }
20653
20654 /* Ignore function type "scopes" from the C frontend. They mean that
20655 a tagged type is local to a parmlist of a function declarator, but
20656 that isn't useful to DWARF. */
20657 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20658 containing_scope = NULL_TREE;
20659
20660 if (SCOPE_FILE_SCOPE_P (containing_scope))
20661 {
20662 /* If T uses a local type keep it local as well, to avoid references
20663 to function-local DIEs from outside the function. */
20664 if (current_function_decl && uses_local_type (t))
20665 scope_die = context_die;
20666 else
20667 scope_die = comp_unit_die ();
20668 }
20669 else if (TYPE_P (containing_scope))
20670 {
20671 /* For types, we can just look up the appropriate DIE. */
20672 if (debug_info_level > DINFO_LEVEL_TERSE)
20673 scope_die = get_context_die (containing_scope);
20674 else
20675 {
20676 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20677 if (scope_die == NULL)
20678 scope_die = comp_unit_die ();
20679 }
20680 }
20681 else
20682 scope_die = context_die;
20683
20684 return scope_die;
20685 }
20686
20687 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20688
20689 static inline int
20690 local_scope_p (dw_die_ref context_die)
20691 {
20692 for (; context_die; context_die = context_die->die_parent)
20693 if (context_die->die_tag == DW_TAG_inlined_subroutine
20694 || context_die->die_tag == DW_TAG_subprogram)
20695 return 1;
20696
20697 return 0;
20698 }
20699
20700 /* Returns nonzero if CONTEXT_DIE is a class. */
20701
20702 static inline int
20703 class_scope_p (dw_die_ref context_die)
20704 {
20705 return (context_die
20706 && (context_die->die_tag == DW_TAG_structure_type
20707 || context_die->die_tag == DW_TAG_class_type
20708 || context_die->die_tag == DW_TAG_interface_type
20709 || context_die->die_tag == DW_TAG_union_type));
20710 }
20711
20712 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20713 whether or not to treat a DIE in this context as a declaration. */
20714
20715 static inline int
20716 class_or_namespace_scope_p (dw_die_ref context_die)
20717 {
20718 return (class_scope_p (context_die)
20719 || (context_die && context_die->die_tag == DW_TAG_namespace));
20720 }
20721
20722 /* Many forms of DIEs require a "type description" attribute. This
20723 routine locates the proper "type descriptor" die for the type given
20724 by 'type' plus any additional qualifiers given by 'cv_quals', and
20725 adds a DW_AT_type attribute below the given die. */
20726
20727 static void
20728 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20729 bool reverse, dw_die_ref context_die)
20730 {
20731 enum tree_code code = TREE_CODE (type);
20732 dw_die_ref type_die = NULL;
20733
20734 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20735 or fixed-point type, use the inner type. This is because we have no
20736 support for unnamed types in base_type_die. This can happen if this is
20737 an Ada subrange type. Correct solution is emit a subrange type die. */
20738 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20739 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20740 type = TREE_TYPE (type), code = TREE_CODE (type);
20741
20742 if (code == ERROR_MARK
20743 /* Handle a special case. For functions whose return type is void, we
20744 generate *no* type attribute. (Note that no object may have type
20745 `void', so this only applies to function return types). */
20746 || code == VOID_TYPE)
20747 return;
20748
20749 type_die = modified_type_die (type,
20750 cv_quals | TYPE_QUALS (type),
20751 reverse,
20752 context_die);
20753
20754 if (type_die != NULL)
20755 add_AT_die_ref (object_die, DW_AT_type, type_die);
20756 }
20757
20758 /* Given an object die, add the calling convention attribute for the
20759 function call type. */
20760 static void
20761 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20762 {
20763 enum dwarf_calling_convention value = DW_CC_normal;
20764
20765 value = ((enum dwarf_calling_convention)
20766 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20767
20768 if (is_fortran ()
20769 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20770 {
20771 /* DWARF 2 doesn't provide a way to identify a program's source-level
20772 entry point. DW_AT_calling_convention attributes are only meant
20773 to describe functions' calling conventions. However, lacking a
20774 better way to signal the Fortran main program, we used this for
20775 a long time, following existing custom. Now, DWARF 4 has
20776 DW_AT_main_subprogram, which we add below, but some tools still
20777 rely on the old way, which we thus keep. */
20778 value = DW_CC_program;
20779
20780 if (dwarf_version >= 4 || !dwarf_strict)
20781 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20782 }
20783
20784 /* Only add the attribute if the backend requests it, and
20785 is not DW_CC_normal. */
20786 if (value && (value != DW_CC_normal))
20787 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20788 }
20789
20790 /* Given a tree pointer to a struct, class, union, or enum type node, return
20791 a pointer to the (string) tag name for the given type, or zero if the type
20792 was declared without a tag. */
20793
20794 static const char *
20795 type_tag (const_tree type)
20796 {
20797 const char *name = 0;
20798
20799 if (TYPE_NAME (type) != 0)
20800 {
20801 tree t = 0;
20802
20803 /* Find the IDENTIFIER_NODE for the type name. */
20804 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20805 && !TYPE_NAMELESS (type))
20806 t = TYPE_NAME (type);
20807
20808 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20809 a TYPE_DECL node, regardless of whether or not a `typedef' was
20810 involved. */
20811 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20812 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20813 {
20814 /* We want to be extra verbose. Don't call dwarf_name if
20815 DECL_NAME isn't set. The default hook for decl_printable_name
20816 doesn't like that, and in this context it's correct to return
20817 0, instead of "<anonymous>" or the like. */
20818 if (DECL_NAME (TYPE_NAME (type))
20819 && !DECL_NAMELESS (TYPE_NAME (type)))
20820 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20821 }
20822
20823 /* Now get the name as a string, or invent one. */
20824 if (!name && t != 0)
20825 name = IDENTIFIER_POINTER (t);
20826 }
20827
20828 return (name == 0 || *name == '\0') ? 0 : name;
20829 }
20830
20831 /* Return the type associated with a data member, make a special check
20832 for bit field types. */
20833
20834 static inline tree
20835 member_declared_type (const_tree member)
20836 {
20837 return (DECL_BIT_FIELD_TYPE (member)
20838 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20839 }
20840
20841 /* Get the decl's label, as described by its RTL. This may be different
20842 from the DECL_NAME name used in the source file. */
20843
20844 #if 0
20845 static const char *
20846 decl_start_label (tree decl)
20847 {
20848 rtx x;
20849 const char *fnname;
20850
20851 x = DECL_RTL (decl);
20852 gcc_assert (MEM_P (x));
20853
20854 x = XEXP (x, 0);
20855 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20856
20857 fnname = XSTR (x, 0);
20858 return fnname;
20859 }
20860 #endif
20861 \f
20862 /* For variable-length arrays that have been previously generated, but
20863 may be incomplete due to missing subscript info, fill the subscript
20864 info. Return TRUE if this is one of those cases. */
20865 static bool
20866 fill_variable_array_bounds (tree type)
20867 {
20868 if (TREE_ASM_WRITTEN (type)
20869 && TREE_CODE (type) == ARRAY_TYPE
20870 && variably_modified_type_p (type, NULL))
20871 {
20872 dw_die_ref array_die = lookup_type_die (type);
20873 if (!array_die)
20874 return false;
20875 add_subscript_info (array_die, type, !is_ada ());
20876 return true;
20877 }
20878 return false;
20879 }
20880
20881 /* These routines generate the internal representation of the DIE's for
20882 the compilation unit. Debugging information is collected by walking
20883 the declaration trees passed in from dwarf2out_decl(). */
20884
20885 static void
20886 gen_array_type_die (tree type, dw_die_ref context_die)
20887 {
20888 dw_die_ref array_die;
20889
20890 /* GNU compilers represent multidimensional array types as sequences of one
20891 dimensional array types whose element types are themselves array types.
20892 We sometimes squish that down to a single array_type DIE with multiple
20893 subscripts in the Dwarf debugging info. The draft Dwarf specification
20894 say that we are allowed to do this kind of compression in C, because
20895 there is no difference between an array of arrays and a multidimensional
20896 array. We don't do this for Ada to remain as close as possible to the
20897 actual representation, which is especially important against the language
20898 flexibilty wrt arrays of variable size. */
20899
20900 bool collapse_nested_arrays = !is_ada ();
20901
20902 if (fill_variable_array_bounds (type))
20903 return;
20904
20905 dw_die_ref scope_die = scope_die_for (type, context_die);
20906 tree element_type;
20907
20908 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20909 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20910 if (TYPE_STRING_FLAG (type)
20911 && TREE_CODE (type) == ARRAY_TYPE
20912 && is_fortran ()
20913 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20914 {
20915 HOST_WIDE_INT size;
20916
20917 array_die = new_die (DW_TAG_string_type, scope_die, type);
20918 add_name_attribute (array_die, type_tag (type));
20919 equate_type_number_to_die (type, array_die);
20920 size = int_size_in_bytes (type);
20921 if (size >= 0)
20922 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20923 /* ??? We can't annotate types late, but for LTO we may not
20924 generate a location early either (gfortran.dg/save_6.f90). */
20925 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20926 && TYPE_DOMAIN (type) != NULL_TREE
20927 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20928 {
20929 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20930 tree rszdecl = szdecl;
20931
20932 size = int_size_in_bytes (TREE_TYPE (szdecl));
20933 if (!DECL_P (szdecl))
20934 {
20935 if (TREE_CODE (szdecl) == INDIRECT_REF
20936 && DECL_P (TREE_OPERAND (szdecl, 0)))
20937 {
20938 rszdecl = TREE_OPERAND (szdecl, 0);
20939 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20940 != DWARF2_ADDR_SIZE)
20941 size = 0;
20942 }
20943 else
20944 size = 0;
20945 }
20946 if (size > 0)
20947 {
20948 dw_loc_list_ref loc
20949 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20950 NULL);
20951 if (loc)
20952 {
20953 add_AT_location_description (array_die, DW_AT_string_length,
20954 loc);
20955 if (size != DWARF2_ADDR_SIZE)
20956 add_AT_unsigned (array_die, dwarf_version >= 5
20957 ? DW_AT_string_length_byte_size
20958 : DW_AT_byte_size, size);
20959 }
20960 }
20961 }
20962 return;
20963 }
20964
20965 array_die = new_die (DW_TAG_array_type, scope_die, type);
20966 add_name_attribute (array_die, type_tag (type));
20967 equate_type_number_to_die (type, array_die);
20968
20969 if (TREE_CODE (type) == VECTOR_TYPE)
20970 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20971
20972 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20973 if (is_fortran ()
20974 && TREE_CODE (type) == ARRAY_TYPE
20975 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20976 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20977 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20978
20979 #if 0
20980 /* We default the array ordering. Debuggers will probably do the right
20981 things even if DW_AT_ordering is not present. It's not even an issue
20982 until we start to get into multidimensional arrays anyway. If a debugger
20983 is ever caught doing the Wrong Thing for multi-dimensional arrays,
20984 then we'll have to put the DW_AT_ordering attribute back in. (But if
20985 and when we find out that we need to put these in, we will only do so
20986 for multidimensional arrays. */
20987 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20988 #endif
20989
20990 if (TREE_CODE (type) == VECTOR_TYPE)
20991 {
20992 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20993 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20994 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20995 add_bound_info (subrange_die, DW_AT_upper_bound,
20996 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20997 }
20998 else
20999 add_subscript_info (array_die, type, collapse_nested_arrays);
21000
21001 /* Add representation of the type of the elements of this array type and
21002 emit the corresponding DIE if we haven't done it already. */
21003 element_type = TREE_TYPE (type);
21004 if (collapse_nested_arrays)
21005 while (TREE_CODE (element_type) == ARRAY_TYPE)
21006 {
21007 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21008 break;
21009 element_type = TREE_TYPE (element_type);
21010 }
21011
21012 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21013 TREE_CODE (type) == ARRAY_TYPE
21014 && TYPE_REVERSE_STORAGE_ORDER (type),
21015 context_die);
21016
21017 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21018 if (TYPE_ARTIFICIAL (type))
21019 add_AT_flag (array_die, DW_AT_artificial, 1);
21020
21021 if (get_AT (array_die, DW_AT_name))
21022 add_pubtype (type, array_die);
21023
21024 add_alignment_attribute (array_die, type);
21025 }
21026
21027 /* This routine generates DIE for array with hidden descriptor, details
21028 are filled into *info by a langhook. */
21029
21030 static void
21031 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21032 dw_die_ref context_die)
21033 {
21034 const dw_die_ref scope_die = scope_die_for (type, context_die);
21035 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21036 struct loc_descr_context context = { type, info->base_decl, NULL,
21037 false, false };
21038 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21039 int dim;
21040
21041 add_name_attribute (array_die, type_tag (type));
21042 equate_type_number_to_die (type, array_die);
21043
21044 if (info->ndimensions > 1)
21045 switch (info->ordering)
21046 {
21047 case array_descr_ordering_row_major:
21048 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21049 break;
21050 case array_descr_ordering_column_major:
21051 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21052 break;
21053 default:
21054 break;
21055 }
21056
21057 if (dwarf_version >= 3 || !dwarf_strict)
21058 {
21059 if (info->data_location)
21060 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21061 dw_scalar_form_exprloc, &context);
21062 if (info->associated)
21063 add_scalar_info (array_die, DW_AT_associated, info->associated,
21064 dw_scalar_form_constant
21065 | dw_scalar_form_exprloc
21066 | dw_scalar_form_reference, &context);
21067 if (info->allocated)
21068 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21069 dw_scalar_form_constant
21070 | dw_scalar_form_exprloc
21071 | dw_scalar_form_reference, &context);
21072 if (info->stride)
21073 {
21074 const enum dwarf_attribute attr
21075 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21076 const int forms
21077 = (info->stride_in_bits)
21078 ? dw_scalar_form_constant
21079 : (dw_scalar_form_constant
21080 | dw_scalar_form_exprloc
21081 | dw_scalar_form_reference);
21082
21083 add_scalar_info (array_die, attr, info->stride, forms, &context);
21084 }
21085 }
21086 if (dwarf_version >= 5)
21087 {
21088 if (info->rank)
21089 {
21090 add_scalar_info (array_die, DW_AT_rank, info->rank,
21091 dw_scalar_form_constant
21092 | dw_scalar_form_exprloc, &context);
21093 subrange_tag = DW_TAG_generic_subrange;
21094 context.placeholder_arg = true;
21095 }
21096 }
21097
21098 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21099
21100 for (dim = 0; dim < info->ndimensions; dim++)
21101 {
21102 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21103
21104 if (info->dimen[dim].bounds_type)
21105 add_type_attribute (subrange_die,
21106 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21107 false, context_die);
21108 if (info->dimen[dim].lower_bound)
21109 add_bound_info (subrange_die, DW_AT_lower_bound,
21110 info->dimen[dim].lower_bound, &context);
21111 if (info->dimen[dim].upper_bound)
21112 add_bound_info (subrange_die, DW_AT_upper_bound,
21113 info->dimen[dim].upper_bound, &context);
21114 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21115 add_scalar_info (subrange_die, DW_AT_byte_stride,
21116 info->dimen[dim].stride,
21117 dw_scalar_form_constant
21118 | dw_scalar_form_exprloc
21119 | dw_scalar_form_reference,
21120 &context);
21121 }
21122
21123 gen_type_die (info->element_type, context_die);
21124 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21125 TREE_CODE (type) == ARRAY_TYPE
21126 && TYPE_REVERSE_STORAGE_ORDER (type),
21127 context_die);
21128
21129 if (get_AT (array_die, DW_AT_name))
21130 add_pubtype (type, array_die);
21131
21132 add_alignment_attribute (array_die, type);
21133 }
21134
21135 #if 0
21136 static void
21137 gen_entry_point_die (tree decl, dw_die_ref context_die)
21138 {
21139 tree origin = decl_ultimate_origin (decl);
21140 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21141
21142 if (origin != NULL)
21143 add_abstract_origin_attribute (decl_die, origin);
21144 else
21145 {
21146 add_name_and_src_coords_attributes (decl_die, decl);
21147 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21148 TYPE_UNQUALIFIED, false, context_die);
21149 }
21150
21151 if (DECL_ABSTRACT_P (decl))
21152 equate_decl_number_to_die (decl, decl_die);
21153 else
21154 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21155 }
21156 #endif
21157
21158 /* Walk through the list of incomplete types again, trying once more to
21159 emit full debugging info for them. */
21160
21161 static void
21162 retry_incomplete_types (void)
21163 {
21164 set_early_dwarf s;
21165 int i;
21166
21167 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21168 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21169 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21170 vec_safe_truncate (incomplete_types, 0);
21171 }
21172
21173 /* Determine what tag to use for a record type. */
21174
21175 static enum dwarf_tag
21176 record_type_tag (tree type)
21177 {
21178 if (! lang_hooks.types.classify_record)
21179 return DW_TAG_structure_type;
21180
21181 switch (lang_hooks.types.classify_record (type))
21182 {
21183 case RECORD_IS_STRUCT:
21184 return DW_TAG_structure_type;
21185
21186 case RECORD_IS_CLASS:
21187 return DW_TAG_class_type;
21188
21189 case RECORD_IS_INTERFACE:
21190 if (dwarf_version >= 3 || !dwarf_strict)
21191 return DW_TAG_interface_type;
21192 return DW_TAG_structure_type;
21193
21194 default:
21195 gcc_unreachable ();
21196 }
21197 }
21198
21199 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21200 include all of the information about the enumeration values also. Each
21201 enumerated type name/value is listed as a child of the enumerated type
21202 DIE. */
21203
21204 static dw_die_ref
21205 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21206 {
21207 dw_die_ref type_die = lookup_type_die (type);
21208
21209 if (type_die == NULL)
21210 {
21211 type_die = new_die (DW_TAG_enumeration_type,
21212 scope_die_for (type, context_die), type);
21213 equate_type_number_to_die (type, type_die);
21214 add_name_attribute (type_die, type_tag (type));
21215 if (dwarf_version >= 4 || !dwarf_strict)
21216 {
21217 if (ENUM_IS_SCOPED (type))
21218 add_AT_flag (type_die, DW_AT_enum_class, 1);
21219 if (ENUM_IS_OPAQUE (type))
21220 add_AT_flag (type_die, DW_AT_declaration, 1);
21221 }
21222 if (!dwarf_strict)
21223 add_AT_unsigned (type_die, DW_AT_encoding,
21224 TYPE_UNSIGNED (type)
21225 ? DW_ATE_unsigned
21226 : DW_ATE_signed);
21227 }
21228 else if (! TYPE_SIZE (type))
21229 return type_die;
21230 else
21231 remove_AT (type_die, DW_AT_declaration);
21232
21233 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21234 given enum type is incomplete, do not generate the DW_AT_byte_size
21235 attribute or the DW_AT_element_list attribute. */
21236 if (TYPE_SIZE (type))
21237 {
21238 tree link;
21239
21240 TREE_ASM_WRITTEN (type) = 1;
21241 add_byte_size_attribute (type_die, type);
21242 add_alignment_attribute (type_die, type);
21243 if (dwarf_version >= 3 || !dwarf_strict)
21244 {
21245 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21246 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21247 context_die);
21248 }
21249 if (TYPE_STUB_DECL (type) != NULL_TREE)
21250 {
21251 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21252 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21253 }
21254
21255 /* If the first reference to this type was as the return type of an
21256 inline function, then it may not have a parent. Fix this now. */
21257 if (type_die->die_parent == NULL)
21258 add_child_die (scope_die_for (type, context_die), type_die);
21259
21260 for (link = TYPE_VALUES (type);
21261 link != NULL; link = TREE_CHAIN (link))
21262 {
21263 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21264 tree value = TREE_VALUE (link);
21265
21266 add_name_attribute (enum_die,
21267 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21268
21269 if (TREE_CODE (value) == CONST_DECL)
21270 value = DECL_INITIAL (value);
21271
21272 if (simple_type_size_in_bits (TREE_TYPE (value))
21273 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21274 {
21275 /* For constant forms created by add_AT_unsigned DWARF
21276 consumers (GDB, elfutils, etc.) always zero extend
21277 the value. Only when the actual value is negative
21278 do we need to use add_AT_int to generate a constant
21279 form that can represent negative values. */
21280 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21281 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21282 add_AT_unsigned (enum_die, DW_AT_const_value,
21283 (unsigned HOST_WIDE_INT) val);
21284 else
21285 add_AT_int (enum_die, DW_AT_const_value, val);
21286 }
21287 else
21288 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21289 that here. TODO: This should be re-worked to use correct
21290 signed/unsigned double tags for all cases. */
21291 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21292 }
21293
21294 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21295 if (TYPE_ARTIFICIAL (type))
21296 add_AT_flag (type_die, DW_AT_artificial, 1);
21297 }
21298 else
21299 add_AT_flag (type_die, DW_AT_declaration, 1);
21300
21301 add_pubtype (type, type_die);
21302
21303 return type_die;
21304 }
21305
21306 /* Generate a DIE to represent either a real live formal parameter decl or to
21307 represent just the type of some formal parameter position in some function
21308 type.
21309
21310 Note that this routine is a bit unusual because its argument may be a
21311 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21312 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21313 node. If it's the former then this function is being called to output a
21314 DIE to represent a formal parameter object (or some inlining thereof). If
21315 it's the latter, then this function is only being called to output a
21316 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21317 argument type of some subprogram type.
21318 If EMIT_NAME_P is true, name and source coordinate attributes
21319 are emitted. */
21320
21321 static dw_die_ref
21322 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21323 dw_die_ref context_die)
21324 {
21325 tree node_or_origin = node ? node : origin;
21326 tree ultimate_origin;
21327 dw_die_ref parm_die = NULL;
21328
21329 if (DECL_P (node_or_origin))
21330 {
21331 parm_die = lookup_decl_die (node);
21332
21333 /* If the contexts differ, we may not be talking about the same
21334 thing.
21335 ??? When in LTO the DIE parent is the "abstract" copy and the
21336 context_die is the specification "copy". But this whole block
21337 should eventually be no longer needed. */
21338 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21339 {
21340 if (!DECL_ABSTRACT_P (node))
21341 {
21342 /* This can happen when creating an inlined instance, in
21343 which case we need to create a new DIE that will get
21344 annotated with DW_AT_abstract_origin. */
21345 parm_die = NULL;
21346 }
21347 else
21348 gcc_unreachable ();
21349 }
21350
21351 if (parm_die && parm_die->die_parent == NULL)
21352 {
21353 /* Check that parm_die already has the right attributes that
21354 we would have added below. If any attributes are
21355 missing, fall through to add them. */
21356 if (! DECL_ABSTRACT_P (node_or_origin)
21357 && !get_AT (parm_die, DW_AT_location)
21358 && !get_AT (parm_die, DW_AT_const_value))
21359 /* We are missing location info, and are about to add it. */
21360 ;
21361 else
21362 {
21363 add_child_die (context_die, parm_die);
21364 return parm_die;
21365 }
21366 }
21367 }
21368
21369 /* If we have a previously generated DIE, use it, unless this is an
21370 concrete instance (origin != NULL), in which case we need a new
21371 DIE with a corresponding DW_AT_abstract_origin. */
21372 bool reusing_die;
21373 if (parm_die && origin == NULL)
21374 reusing_die = true;
21375 else
21376 {
21377 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21378 reusing_die = false;
21379 }
21380
21381 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21382 {
21383 case tcc_declaration:
21384 ultimate_origin = decl_ultimate_origin (node_or_origin);
21385 if (node || ultimate_origin)
21386 origin = ultimate_origin;
21387
21388 if (reusing_die)
21389 goto add_location;
21390
21391 if (origin != NULL)
21392 add_abstract_origin_attribute (parm_die, origin);
21393 else if (emit_name_p)
21394 add_name_and_src_coords_attributes (parm_die, node);
21395 if (origin == NULL
21396 || (! DECL_ABSTRACT_P (node_or_origin)
21397 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21398 decl_function_context
21399 (node_or_origin))))
21400 {
21401 tree type = TREE_TYPE (node_or_origin);
21402 if (decl_by_reference_p (node_or_origin))
21403 add_type_attribute (parm_die, TREE_TYPE (type),
21404 TYPE_UNQUALIFIED,
21405 false, context_die);
21406 else
21407 add_type_attribute (parm_die, type,
21408 decl_quals (node_or_origin),
21409 false, context_die);
21410 }
21411 if (origin == NULL && DECL_ARTIFICIAL (node))
21412 add_AT_flag (parm_die, DW_AT_artificial, 1);
21413 add_location:
21414 if (node && node != origin)
21415 equate_decl_number_to_die (node, parm_die);
21416 if (! DECL_ABSTRACT_P (node_or_origin))
21417 add_location_or_const_value_attribute (parm_die, node_or_origin,
21418 node == NULL);
21419
21420 break;
21421
21422 case tcc_type:
21423 /* We were called with some kind of a ..._TYPE node. */
21424 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21425 context_die);
21426 break;
21427
21428 default:
21429 gcc_unreachable ();
21430 }
21431
21432 return parm_die;
21433 }
21434
21435 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21436 children DW_TAG_formal_parameter DIEs representing the arguments of the
21437 parameter pack.
21438
21439 PARM_PACK must be a function parameter pack.
21440 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21441 must point to the subsequent arguments of the function PACK_ARG belongs to.
21442 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21443 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21444 following the last one for which a DIE was generated. */
21445
21446 static dw_die_ref
21447 gen_formal_parameter_pack_die (tree parm_pack,
21448 tree pack_arg,
21449 dw_die_ref subr_die,
21450 tree *next_arg)
21451 {
21452 tree arg;
21453 dw_die_ref parm_pack_die;
21454
21455 gcc_assert (parm_pack
21456 && lang_hooks.function_parameter_pack_p (parm_pack)
21457 && subr_die);
21458
21459 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21460 add_src_coords_attributes (parm_pack_die, parm_pack);
21461
21462 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21463 {
21464 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21465 parm_pack))
21466 break;
21467 gen_formal_parameter_die (arg, NULL,
21468 false /* Don't emit name attribute. */,
21469 parm_pack_die);
21470 }
21471 if (next_arg)
21472 *next_arg = arg;
21473 return parm_pack_die;
21474 }
21475
21476 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21477 at the end of an (ANSI prototyped) formal parameters list. */
21478
21479 static void
21480 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21481 {
21482 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21483 }
21484
21485 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21486 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21487 parameters as specified in some function type specification (except for
21488 those which appear as part of a function *definition*). */
21489
21490 static void
21491 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21492 {
21493 tree link;
21494 tree formal_type = NULL;
21495 tree first_parm_type;
21496 tree arg;
21497
21498 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21499 {
21500 arg = DECL_ARGUMENTS (function_or_method_type);
21501 function_or_method_type = TREE_TYPE (function_or_method_type);
21502 }
21503 else
21504 arg = NULL_TREE;
21505
21506 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21507
21508 /* Make our first pass over the list of formal parameter types and output a
21509 DW_TAG_formal_parameter DIE for each one. */
21510 for (link = first_parm_type; link; )
21511 {
21512 dw_die_ref parm_die;
21513
21514 formal_type = TREE_VALUE (link);
21515 if (formal_type == void_type_node)
21516 break;
21517
21518 /* Output a (nameless) DIE to represent the formal parameter itself. */
21519 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21520 {
21521 parm_die = gen_formal_parameter_die (formal_type, NULL,
21522 true /* Emit name attribute. */,
21523 context_die);
21524 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21525 && link == first_parm_type)
21526 {
21527 add_AT_flag (parm_die, DW_AT_artificial, 1);
21528 if (dwarf_version >= 3 || !dwarf_strict)
21529 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21530 }
21531 else if (arg && DECL_ARTIFICIAL (arg))
21532 add_AT_flag (parm_die, DW_AT_artificial, 1);
21533 }
21534
21535 link = TREE_CHAIN (link);
21536 if (arg)
21537 arg = DECL_CHAIN (arg);
21538 }
21539
21540 /* If this function type has an ellipsis, add a
21541 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21542 if (formal_type != void_type_node)
21543 gen_unspecified_parameters_die (function_or_method_type, context_die);
21544
21545 /* Make our second (and final) pass over the list of formal parameter types
21546 and output DIEs to represent those types (as necessary). */
21547 for (link = TYPE_ARG_TYPES (function_or_method_type);
21548 link && TREE_VALUE (link);
21549 link = TREE_CHAIN (link))
21550 gen_type_die (TREE_VALUE (link), context_die);
21551 }
21552
21553 /* We want to generate the DIE for TYPE so that we can generate the
21554 die for MEMBER, which has been defined; we will need to refer back
21555 to the member declaration nested within TYPE. If we're trying to
21556 generate minimal debug info for TYPE, processing TYPE won't do the
21557 trick; we need to attach the member declaration by hand. */
21558
21559 static void
21560 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21561 {
21562 gen_type_die (type, context_die);
21563
21564 /* If we're trying to avoid duplicate debug info, we may not have
21565 emitted the member decl for this function. Emit it now. */
21566 if (TYPE_STUB_DECL (type)
21567 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21568 && ! lookup_decl_die (member))
21569 {
21570 dw_die_ref type_die;
21571 gcc_assert (!decl_ultimate_origin (member));
21572
21573 push_decl_scope (type);
21574 type_die = lookup_type_die_strip_naming_typedef (type);
21575 if (TREE_CODE (member) == FUNCTION_DECL)
21576 gen_subprogram_die (member, type_die);
21577 else if (TREE_CODE (member) == FIELD_DECL)
21578 {
21579 /* Ignore the nameless fields that are used to skip bits but handle
21580 C++ anonymous unions and structs. */
21581 if (DECL_NAME (member) != NULL_TREE
21582 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21583 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21584 {
21585 struct vlr_context vlr_ctx = {
21586 DECL_CONTEXT (member), /* struct_type */
21587 NULL_TREE /* variant_part_offset */
21588 };
21589 gen_type_die (member_declared_type (member), type_die);
21590 gen_field_die (member, &vlr_ctx, type_die);
21591 }
21592 }
21593 else
21594 gen_variable_die (member, NULL_TREE, type_die);
21595
21596 pop_decl_scope ();
21597 }
21598 }
21599 \f
21600 /* Forward declare these functions, because they are mutually recursive
21601 with their set_block_* pairing functions. */
21602 static void set_decl_origin_self (tree);
21603
21604 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21605 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21606 that it points to the node itself, thus indicating that the node is its
21607 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21608 the given node is NULL, recursively descend the decl/block tree which
21609 it is the root of, and for each other ..._DECL or BLOCK node contained
21610 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21611 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21612 values to point to themselves. */
21613
21614 static void
21615 set_block_origin_self (tree stmt)
21616 {
21617 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21618 {
21619 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21620
21621 {
21622 tree local_decl;
21623
21624 for (local_decl = BLOCK_VARS (stmt);
21625 local_decl != NULL_TREE;
21626 local_decl = DECL_CHAIN (local_decl))
21627 /* Do not recurse on nested functions since the inlining status
21628 of parent and child can be different as per the DWARF spec. */
21629 if (TREE_CODE (local_decl) != FUNCTION_DECL
21630 && !DECL_EXTERNAL (local_decl))
21631 set_decl_origin_self (local_decl);
21632 }
21633
21634 {
21635 tree subblock;
21636
21637 for (subblock = BLOCK_SUBBLOCKS (stmt);
21638 subblock != NULL_TREE;
21639 subblock = BLOCK_CHAIN (subblock))
21640 set_block_origin_self (subblock); /* Recurse. */
21641 }
21642 }
21643 }
21644
21645 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21646 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21647 node to so that it points to the node itself, thus indicating that the
21648 node represents its own (abstract) origin. Additionally, if the
21649 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21650 the decl/block tree of which the given node is the root of, and for
21651 each other ..._DECL or BLOCK node contained therein whose
21652 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21653 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21654 point to themselves. */
21655
21656 static void
21657 set_decl_origin_self (tree decl)
21658 {
21659 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21660 {
21661 DECL_ABSTRACT_ORIGIN (decl) = decl;
21662 if (TREE_CODE (decl) == FUNCTION_DECL)
21663 {
21664 tree arg;
21665
21666 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21667 DECL_ABSTRACT_ORIGIN (arg) = arg;
21668 if (DECL_INITIAL (decl) != NULL_TREE
21669 && DECL_INITIAL (decl) != error_mark_node)
21670 set_block_origin_self (DECL_INITIAL (decl));
21671 }
21672 }
21673 }
21674 \f
21675 /* Mark the early DIE for DECL as the abstract instance. */
21676
21677 static void
21678 dwarf2out_abstract_function (tree decl)
21679 {
21680 dw_die_ref old_die;
21681
21682 /* Make sure we have the actual abstract inline, not a clone. */
21683 decl = DECL_ORIGIN (decl);
21684
21685 if (DECL_IGNORED_P (decl))
21686 return;
21687
21688 old_die = lookup_decl_die (decl);
21689 /* With early debug we always have an old DIE unless we are in LTO
21690 and the user did not compile but only link with debug. */
21691 if (in_lto_p && ! old_die)
21692 return;
21693 gcc_assert (old_die != NULL);
21694 if (get_AT (old_die, DW_AT_inline)
21695 || get_AT (old_die, DW_AT_abstract_origin))
21696 /* We've already generated the abstract instance. */
21697 return;
21698
21699 /* Go ahead and put DW_AT_inline on the DIE. */
21700 if (DECL_DECLARED_INLINE_P (decl))
21701 {
21702 if (cgraph_function_possibly_inlined_p (decl))
21703 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21704 else
21705 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21706 }
21707 else
21708 {
21709 if (cgraph_function_possibly_inlined_p (decl))
21710 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21711 else
21712 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21713 }
21714
21715 if (DECL_DECLARED_INLINE_P (decl)
21716 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21717 add_AT_flag (old_die, DW_AT_artificial, 1);
21718
21719 set_decl_origin_self (decl);
21720 }
21721
21722 /* Helper function of premark_used_types() which gets called through
21723 htab_traverse.
21724
21725 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21726 marked as unused by prune_unused_types. */
21727
21728 bool
21729 premark_used_types_helper (tree const &type, void *)
21730 {
21731 dw_die_ref die;
21732
21733 die = lookup_type_die (type);
21734 if (die != NULL)
21735 die->die_perennial_p = 1;
21736 return true;
21737 }
21738
21739 /* Helper function of premark_types_used_by_global_vars which gets called
21740 through htab_traverse.
21741
21742 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21743 marked as unused by prune_unused_types. The DIE of the type is marked
21744 only if the global variable using the type will actually be emitted. */
21745
21746 int
21747 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21748 void *)
21749 {
21750 struct types_used_by_vars_entry *entry;
21751 dw_die_ref die;
21752
21753 entry = (struct types_used_by_vars_entry *) *slot;
21754 gcc_assert (entry->type != NULL
21755 && entry->var_decl != NULL);
21756 die = lookup_type_die (entry->type);
21757 if (die)
21758 {
21759 /* Ask cgraph if the global variable really is to be emitted.
21760 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21761 varpool_node *node = varpool_node::get (entry->var_decl);
21762 if (node && node->definition)
21763 {
21764 die->die_perennial_p = 1;
21765 /* Keep the parent DIEs as well. */
21766 while ((die = die->die_parent) && die->die_perennial_p == 0)
21767 die->die_perennial_p = 1;
21768 }
21769 }
21770 return 1;
21771 }
21772
21773 /* Mark all members of used_types_hash as perennial. */
21774
21775 static void
21776 premark_used_types (struct function *fun)
21777 {
21778 if (fun && fun->used_types_hash)
21779 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21780 }
21781
21782 /* Mark all members of types_used_by_vars_entry as perennial. */
21783
21784 static void
21785 premark_types_used_by_global_vars (void)
21786 {
21787 if (types_used_by_vars_hash)
21788 types_used_by_vars_hash
21789 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21790 }
21791
21792 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21793 for CA_LOC call arg loc node. */
21794
21795 static dw_die_ref
21796 gen_call_site_die (tree decl, dw_die_ref subr_die,
21797 struct call_arg_loc_node *ca_loc)
21798 {
21799 dw_die_ref stmt_die = NULL, die;
21800 tree block = ca_loc->block;
21801
21802 while (block
21803 && block != DECL_INITIAL (decl)
21804 && TREE_CODE (block) == BLOCK)
21805 {
21806 stmt_die = BLOCK_DIE (block);
21807 if (stmt_die)
21808 break;
21809 block = BLOCK_SUPERCONTEXT (block);
21810 }
21811 if (stmt_die == NULL)
21812 stmt_die = subr_die;
21813 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21814 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21815 if (ca_loc->tail_call_p)
21816 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21817 if (ca_loc->symbol_ref)
21818 {
21819 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21820 if (tdie)
21821 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21822 else
21823 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21824 false);
21825 }
21826 return die;
21827 }
21828
21829 /* Generate a DIE to represent a declared function (either file-scope or
21830 block-local). */
21831
21832 static void
21833 gen_subprogram_die (tree decl, dw_die_ref context_die)
21834 {
21835 tree origin = decl_ultimate_origin (decl);
21836 dw_die_ref subr_die;
21837 dw_die_ref old_die = lookup_decl_die (decl);
21838
21839 /* This function gets called multiple times for different stages of
21840 the debug process. For example, for func() in this code:
21841
21842 namespace S
21843 {
21844 void func() { ... }
21845 }
21846
21847 ...we get called 4 times. Twice in early debug and twice in
21848 late debug:
21849
21850 Early debug
21851 -----------
21852
21853 1. Once while generating func() within the namespace. This is
21854 the declaration. The declaration bit below is set, as the
21855 context is the namespace.
21856
21857 A new DIE will be generated with DW_AT_declaration set.
21858
21859 2. Once for func() itself. This is the specification. The
21860 declaration bit below is clear as the context is the CU.
21861
21862 We will use the cached DIE from (1) to create a new DIE with
21863 DW_AT_specification pointing to the declaration in (1).
21864
21865 Late debug via rest_of_handle_final()
21866 -------------------------------------
21867
21868 3. Once generating func() within the namespace. This is also the
21869 declaration, as in (1), but this time we will early exit below
21870 as we have a cached DIE and a declaration needs no additional
21871 annotations (no locations), as the source declaration line
21872 info is enough.
21873
21874 4. Once for func() itself. As in (2), this is the specification,
21875 but this time we will re-use the cached DIE, and just annotate
21876 it with the location information that should now be available.
21877
21878 For something without namespaces, but with abstract instances, we
21879 are also called a multiple times:
21880
21881 class Base
21882 {
21883 public:
21884 Base (); // constructor declaration (1)
21885 };
21886
21887 Base::Base () { } // constructor specification (2)
21888
21889 Early debug
21890 -----------
21891
21892 1. Once for the Base() constructor by virtue of it being a
21893 member of the Base class. This is done via
21894 rest_of_type_compilation.
21895
21896 This is a declaration, so a new DIE will be created with
21897 DW_AT_declaration.
21898
21899 2. Once for the Base() constructor definition, but this time
21900 while generating the abstract instance of the base
21901 constructor (__base_ctor) which is being generated via early
21902 debug of reachable functions.
21903
21904 Even though we have a cached version of the declaration (1),
21905 we will create a DW_AT_specification of the declaration DIE
21906 in (1).
21907
21908 3. Once for the __base_ctor itself, but this time, we generate
21909 an DW_AT_abstract_origin version of the DW_AT_specification in
21910 (2).
21911
21912 Late debug via rest_of_handle_final
21913 -----------------------------------
21914
21915 4. One final time for the __base_ctor (which will have a cached
21916 DIE with DW_AT_abstract_origin created in (3). This time,
21917 we will just annotate the location information now
21918 available.
21919 */
21920 int declaration = (current_function_decl != decl
21921 || class_or_namespace_scope_p (context_die));
21922
21923 /* Now that the C++ front end lazily declares artificial member fns, we
21924 might need to retrofit the declaration into its class. */
21925 if (!declaration && !origin && !old_die
21926 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21927 && !class_or_namespace_scope_p (context_die)
21928 && debug_info_level > DINFO_LEVEL_TERSE)
21929 old_die = force_decl_die (decl);
21930
21931 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21932 if (origin != NULL)
21933 {
21934 gcc_assert (!declaration || local_scope_p (context_die));
21935
21936 /* Fixup die_parent for the abstract instance of a nested
21937 inline function. */
21938 if (old_die && old_die->die_parent == NULL)
21939 add_child_die (context_die, old_die);
21940
21941 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21942 {
21943 /* If we have a DW_AT_abstract_origin we have a working
21944 cached version. */
21945 subr_die = old_die;
21946 }
21947 else
21948 {
21949 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21950 add_abstract_origin_attribute (subr_die, origin);
21951 /* This is where the actual code for a cloned function is.
21952 Let's emit linkage name attribute for it. This helps
21953 debuggers to e.g, set breakpoints into
21954 constructors/destructors when the user asks "break
21955 K::K". */
21956 add_linkage_name (subr_die, decl);
21957 }
21958 }
21959 /* A cached copy, possibly from early dwarf generation. Reuse as
21960 much as possible. */
21961 else if (old_die)
21962 {
21963 /* A declaration that has been previously dumped needs no
21964 additional information. */
21965 if (declaration)
21966 return;
21967
21968 if (!get_AT_flag (old_die, DW_AT_declaration)
21969 /* We can have a normal definition following an inline one in the
21970 case of redefinition of GNU C extern inlines.
21971 It seems reasonable to use AT_specification in this case. */
21972 && !get_AT (old_die, DW_AT_inline))
21973 {
21974 /* Detect and ignore this case, where we are trying to output
21975 something we have already output. */
21976 if (get_AT (old_die, DW_AT_low_pc)
21977 || get_AT (old_die, DW_AT_ranges))
21978 return;
21979
21980 /* If we have no location information, this must be a
21981 partially generated DIE from early dwarf generation.
21982 Fall through and generate it. */
21983 }
21984
21985 /* If the definition comes from the same place as the declaration,
21986 maybe use the old DIE. We always want the DIE for this function
21987 that has the *_pc attributes to be under comp_unit_die so the
21988 debugger can find it. We also need to do this for abstract
21989 instances of inlines, since the spec requires the out-of-line copy
21990 to have the same parent. For local class methods, this doesn't
21991 apply; we just use the old DIE. */
21992 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21993 struct dwarf_file_data * file_index = lookup_filename (s.file);
21994 if ((is_cu_die (old_die->die_parent)
21995 /* This condition fixes the inconsistency/ICE with the
21996 following Fortran test (or some derivative thereof) while
21997 building libgfortran:
21998
21999 module some_m
22000 contains
22001 logical function funky (FLAG)
22002 funky = .true.
22003 end function
22004 end module
22005 */
22006 || (old_die->die_parent
22007 && old_die->die_parent->die_tag == DW_TAG_module)
22008 || context_die == NULL)
22009 && (DECL_ARTIFICIAL (decl)
22010 /* The location attributes may be in the abstract origin
22011 which in the case of LTO might be not available to
22012 look at. */
22013 || get_AT (old_die, DW_AT_abstract_origin)
22014 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22015 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22016 == (unsigned) s.line)
22017 && (!debug_column_info
22018 || s.column == 0
22019 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22020 == (unsigned) s.column)))))
22021 {
22022 subr_die = old_die;
22023
22024 /* Clear out the declaration attribute, but leave the
22025 parameters so they can be augmented with location
22026 information later. Unless this was a declaration, in
22027 which case, wipe out the nameless parameters and recreate
22028 them further down. */
22029 if (remove_AT (subr_die, DW_AT_declaration))
22030 {
22031
22032 remove_AT (subr_die, DW_AT_object_pointer);
22033 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22034 }
22035 }
22036 /* Make a specification pointing to the previously built
22037 declaration. */
22038 else
22039 {
22040 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22041 add_AT_specification (subr_die, old_die);
22042 add_pubname (decl, subr_die);
22043 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22044 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22045 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22046 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22047 if (debug_column_info
22048 && s.column
22049 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22050 != (unsigned) s.column))
22051 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22052
22053 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22054 emit the real type on the definition die. */
22055 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22056 {
22057 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22058 if (die == auto_die || die == decltype_auto_die)
22059 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22060 TYPE_UNQUALIFIED, false, context_die);
22061 }
22062
22063 /* When we process the method declaration, we haven't seen
22064 the out-of-class defaulted definition yet, so we have to
22065 recheck now. */
22066 if ((dwarf_version >= 5 || ! dwarf_strict)
22067 && !get_AT (subr_die, DW_AT_defaulted))
22068 {
22069 int defaulted
22070 = lang_hooks.decls.decl_dwarf_attribute (decl,
22071 DW_AT_defaulted);
22072 if (defaulted != -1)
22073 {
22074 /* Other values must have been handled before. */
22075 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22076 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22077 }
22078 }
22079 }
22080 }
22081 /* Create a fresh DIE for anything else. */
22082 else
22083 {
22084 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22085
22086 if (TREE_PUBLIC (decl))
22087 add_AT_flag (subr_die, DW_AT_external, 1);
22088
22089 add_name_and_src_coords_attributes (subr_die, decl);
22090 add_pubname (decl, subr_die);
22091 if (debug_info_level > DINFO_LEVEL_TERSE)
22092 {
22093 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22094 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22095 TYPE_UNQUALIFIED, false, context_die);
22096 }
22097
22098 add_pure_or_virtual_attribute (subr_die, decl);
22099 if (DECL_ARTIFICIAL (decl))
22100 add_AT_flag (subr_die, DW_AT_artificial, 1);
22101
22102 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22103 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22104
22105 add_alignment_attribute (subr_die, decl);
22106
22107 add_accessibility_attribute (subr_die, decl);
22108 }
22109
22110 /* Unless we have an existing non-declaration DIE, equate the new
22111 DIE. */
22112 if (!old_die || is_declaration_die (old_die))
22113 equate_decl_number_to_die (decl, subr_die);
22114
22115 if (declaration)
22116 {
22117 if (!old_die || !get_AT (old_die, DW_AT_inline))
22118 {
22119 add_AT_flag (subr_die, DW_AT_declaration, 1);
22120
22121 /* If this is an explicit function declaration then generate
22122 a DW_AT_explicit attribute. */
22123 if ((dwarf_version >= 3 || !dwarf_strict)
22124 && lang_hooks.decls.decl_dwarf_attribute (decl,
22125 DW_AT_explicit) == 1)
22126 add_AT_flag (subr_die, DW_AT_explicit, 1);
22127
22128 /* If this is a C++11 deleted special function member then generate
22129 a DW_AT_deleted attribute. */
22130 if ((dwarf_version >= 5 || !dwarf_strict)
22131 && lang_hooks.decls.decl_dwarf_attribute (decl,
22132 DW_AT_deleted) == 1)
22133 add_AT_flag (subr_die, DW_AT_deleted, 1);
22134
22135 /* If this is a C++11 defaulted special function member then
22136 generate a DW_AT_defaulted attribute. */
22137 if (dwarf_version >= 5 || !dwarf_strict)
22138 {
22139 int defaulted
22140 = lang_hooks.decls.decl_dwarf_attribute (decl,
22141 DW_AT_defaulted);
22142 if (defaulted != -1)
22143 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22144 }
22145
22146 /* If this is a C++11 non-static member function with & ref-qualifier
22147 then generate a DW_AT_reference attribute. */
22148 if ((dwarf_version >= 5 || !dwarf_strict)
22149 && lang_hooks.decls.decl_dwarf_attribute (decl,
22150 DW_AT_reference) == 1)
22151 add_AT_flag (subr_die, DW_AT_reference, 1);
22152
22153 /* If this is a C++11 non-static member function with &&
22154 ref-qualifier then generate a DW_AT_reference attribute. */
22155 if ((dwarf_version >= 5 || !dwarf_strict)
22156 && lang_hooks.decls.decl_dwarf_attribute (decl,
22157 DW_AT_rvalue_reference)
22158 == 1)
22159 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22160 }
22161 }
22162 /* For non DECL_EXTERNALs, if range information is available, fill
22163 the DIE with it. */
22164 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22165 {
22166 HOST_WIDE_INT cfa_fb_offset;
22167
22168 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22169
22170 if (!crtl->has_bb_partition)
22171 {
22172 dw_fde_ref fde = fun->fde;
22173 if (fde->dw_fde_begin)
22174 {
22175 /* We have already generated the labels. */
22176 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22177 fde->dw_fde_end, false);
22178 }
22179 else
22180 {
22181 /* Create start/end labels and add the range. */
22182 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22183 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22184 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22185 current_function_funcdef_no);
22186 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22187 current_function_funcdef_no);
22188 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22189 false);
22190 }
22191
22192 #if VMS_DEBUGGING_INFO
22193 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22194 Section 2.3 Prologue and Epilogue Attributes:
22195 When a breakpoint is set on entry to a function, it is generally
22196 desirable for execution to be suspended, not on the very first
22197 instruction of the function, but rather at a point after the
22198 function's frame has been set up, after any language defined local
22199 declaration processing has been completed, and before execution of
22200 the first statement of the function begins. Debuggers generally
22201 cannot properly determine where this point is. Similarly for a
22202 breakpoint set on exit from a function. The prologue and epilogue
22203 attributes allow a compiler to communicate the location(s) to use. */
22204
22205 {
22206 if (fde->dw_fde_vms_end_prologue)
22207 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22208 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22209
22210 if (fde->dw_fde_vms_begin_epilogue)
22211 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22212 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22213 }
22214 #endif
22215
22216 }
22217 else
22218 {
22219 /* Generate pubnames entries for the split function code ranges. */
22220 dw_fde_ref fde = fun->fde;
22221
22222 if (fde->dw_fde_second_begin)
22223 {
22224 if (dwarf_version >= 3 || !dwarf_strict)
22225 {
22226 /* We should use ranges for non-contiguous code section
22227 addresses. Use the actual code range for the initial
22228 section, since the HOT/COLD labels might precede an
22229 alignment offset. */
22230 bool range_list_added = false;
22231 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22232 fde->dw_fde_end, &range_list_added,
22233 false);
22234 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22235 fde->dw_fde_second_end,
22236 &range_list_added, false);
22237 if (range_list_added)
22238 add_ranges (NULL);
22239 }
22240 else
22241 {
22242 /* There is no real support in DW2 for this .. so we make
22243 a work-around. First, emit the pub name for the segment
22244 containing the function label. Then make and emit a
22245 simplified subprogram DIE for the second segment with the
22246 name pre-fixed by __hot/cold_sect_of_. We use the same
22247 linkage name for the second die so that gdb will find both
22248 sections when given "b foo". */
22249 const char *name = NULL;
22250 tree decl_name = DECL_NAME (decl);
22251 dw_die_ref seg_die;
22252
22253 /* Do the 'primary' section. */
22254 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22255 fde->dw_fde_end, false);
22256
22257 /* Build a minimal DIE for the secondary section. */
22258 seg_die = new_die (DW_TAG_subprogram,
22259 subr_die->die_parent, decl);
22260
22261 if (TREE_PUBLIC (decl))
22262 add_AT_flag (seg_die, DW_AT_external, 1);
22263
22264 if (decl_name != NULL
22265 && IDENTIFIER_POINTER (decl_name) != NULL)
22266 {
22267 name = dwarf2_name (decl, 1);
22268 if (! DECL_ARTIFICIAL (decl))
22269 add_src_coords_attributes (seg_die, decl);
22270
22271 add_linkage_name (seg_die, decl);
22272 }
22273 gcc_assert (name != NULL);
22274 add_pure_or_virtual_attribute (seg_die, decl);
22275 if (DECL_ARTIFICIAL (decl))
22276 add_AT_flag (seg_die, DW_AT_artificial, 1);
22277
22278 name = concat ("__second_sect_of_", name, NULL);
22279 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22280 fde->dw_fde_second_end, false);
22281 add_name_attribute (seg_die, name);
22282 if (want_pubnames ())
22283 add_pubname_string (name, seg_die);
22284 }
22285 }
22286 else
22287 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22288 false);
22289 }
22290
22291 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22292
22293 /* We define the "frame base" as the function's CFA. This is more
22294 convenient for several reasons: (1) It's stable across the prologue
22295 and epilogue, which makes it better than just a frame pointer,
22296 (2) With dwarf3, there exists a one-byte encoding that allows us
22297 to reference the .debug_frame data by proxy, but failing that,
22298 (3) We can at least reuse the code inspection and interpretation
22299 code that determines the CFA position at various points in the
22300 function. */
22301 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22302 {
22303 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22304 add_AT_loc (subr_die, DW_AT_frame_base, op);
22305 }
22306 else
22307 {
22308 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22309 if (list->dw_loc_next)
22310 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22311 else
22312 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22313 }
22314
22315 /* Compute a displacement from the "steady-state frame pointer" to
22316 the CFA. The former is what all stack slots and argument slots
22317 will reference in the rtl; the latter is what we've told the
22318 debugger about. We'll need to adjust all frame_base references
22319 by this displacement. */
22320 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22321
22322 if (fun->static_chain_decl)
22323 {
22324 /* DWARF requires here a location expression that computes the
22325 address of the enclosing subprogram's frame base. The machinery
22326 in tree-nested.c is supposed to store this specific address in the
22327 last field of the FRAME record. */
22328 const tree frame_type
22329 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22330 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22331
22332 tree fb_expr
22333 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22334 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22335 fb_expr, fb_decl, NULL_TREE);
22336
22337 add_AT_location_description (subr_die, DW_AT_static_link,
22338 loc_list_from_tree (fb_expr, 0, NULL));
22339 }
22340
22341 resolve_variable_values ();
22342 }
22343
22344 /* Generate child dies for template paramaters. */
22345 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22346 gen_generic_params_dies (decl);
22347
22348 /* Now output descriptions of the arguments for this function. This gets
22349 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22350 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22351 `...' at the end of the formal parameter list. In order to find out if
22352 there was a trailing ellipsis or not, we must instead look at the type
22353 associated with the FUNCTION_DECL. This will be a node of type
22354 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22355 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22356 an ellipsis at the end. */
22357
22358 /* In the case where we are describing a mere function declaration, all we
22359 need to do here (and all we *can* do here) is to describe the *types* of
22360 its formal parameters. */
22361 if (debug_info_level <= DINFO_LEVEL_TERSE)
22362 ;
22363 else if (declaration)
22364 gen_formal_types_die (decl, subr_die);
22365 else
22366 {
22367 /* Generate DIEs to represent all known formal parameters. */
22368 tree parm = DECL_ARGUMENTS (decl);
22369 tree generic_decl = early_dwarf
22370 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22371 tree generic_decl_parm = generic_decl
22372 ? DECL_ARGUMENTS (generic_decl)
22373 : NULL;
22374
22375 /* Now we want to walk the list of parameters of the function and
22376 emit their relevant DIEs.
22377
22378 We consider the case of DECL being an instance of a generic function
22379 as well as it being a normal function.
22380
22381 If DECL is an instance of a generic function we walk the
22382 parameters of the generic function declaration _and_ the parameters of
22383 DECL itself. This is useful because we want to emit specific DIEs for
22384 function parameter packs and those are declared as part of the
22385 generic function declaration. In that particular case,
22386 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22387 That DIE has children DIEs representing the set of arguments
22388 of the pack. Note that the set of pack arguments can be empty.
22389 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22390 children DIE.
22391
22392 Otherwise, we just consider the parameters of DECL. */
22393 while (generic_decl_parm || parm)
22394 {
22395 if (generic_decl_parm
22396 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22397 gen_formal_parameter_pack_die (generic_decl_parm,
22398 parm, subr_die,
22399 &parm);
22400 else if (parm && !POINTER_BOUNDS_P (parm))
22401 {
22402 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22403
22404 if (early_dwarf
22405 && parm == DECL_ARGUMENTS (decl)
22406 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22407 && parm_die
22408 && (dwarf_version >= 3 || !dwarf_strict))
22409 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22410
22411 parm = DECL_CHAIN (parm);
22412 }
22413 else if (parm)
22414 parm = DECL_CHAIN (parm);
22415
22416 if (generic_decl_parm)
22417 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22418 }
22419
22420 /* Decide whether we need an unspecified_parameters DIE at the end.
22421 There are 2 more cases to do this for: 1) the ansi ... declaration -
22422 this is detectable when the end of the arg list is not a
22423 void_type_node 2) an unprototyped function declaration (not a
22424 definition). This just means that we have no info about the
22425 parameters at all. */
22426 if (early_dwarf)
22427 {
22428 if (prototype_p (TREE_TYPE (decl)))
22429 {
22430 /* This is the prototyped case, check for.... */
22431 if (stdarg_p (TREE_TYPE (decl)))
22432 gen_unspecified_parameters_die (decl, subr_die);
22433 }
22434 else if (DECL_INITIAL (decl) == NULL_TREE)
22435 gen_unspecified_parameters_die (decl, subr_die);
22436 }
22437 }
22438
22439 if (subr_die != old_die)
22440 /* Add the calling convention attribute if requested. */
22441 add_calling_convention_attribute (subr_die, decl);
22442
22443 /* Output Dwarf info for all of the stuff within the body of the function
22444 (if it has one - it may be just a declaration).
22445
22446 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22447 a function. This BLOCK actually represents the outermost binding contour
22448 for the function, i.e. the contour in which the function's formal
22449 parameters and labels get declared. Curiously, it appears that the front
22450 end doesn't actually put the PARM_DECL nodes for the current function onto
22451 the BLOCK_VARS list for this outer scope, but are strung off of the
22452 DECL_ARGUMENTS list for the function instead.
22453
22454 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22455 the LABEL_DECL nodes for the function however, and we output DWARF info
22456 for those in decls_for_scope. Just within the `outer_scope' there will be
22457 a BLOCK node representing the function's outermost pair of curly braces,
22458 and any blocks used for the base and member initializers of a C++
22459 constructor function. */
22460 tree outer_scope = DECL_INITIAL (decl);
22461 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22462 {
22463 int call_site_note_count = 0;
22464 int tail_call_site_note_count = 0;
22465
22466 /* Emit a DW_TAG_variable DIE for a named return value. */
22467 if (DECL_NAME (DECL_RESULT (decl)))
22468 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22469
22470 /* The first time through decls_for_scope we will generate the
22471 DIEs for the locals. The second time, we fill in the
22472 location info. */
22473 decls_for_scope (outer_scope, subr_die);
22474
22475 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22476 {
22477 struct call_arg_loc_node *ca_loc;
22478 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22479 {
22480 dw_die_ref die = NULL;
22481 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22482 rtx arg, next_arg;
22483
22484 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22485 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22486 : NULL_RTX);
22487 arg; arg = next_arg)
22488 {
22489 dw_loc_descr_ref reg, val;
22490 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22491 dw_die_ref cdie, tdie = NULL;
22492
22493 next_arg = XEXP (arg, 1);
22494 if (REG_P (XEXP (XEXP (arg, 0), 0))
22495 && next_arg
22496 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22497 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22498 && REGNO (XEXP (XEXP (arg, 0), 0))
22499 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22500 next_arg = XEXP (next_arg, 1);
22501 if (mode == VOIDmode)
22502 {
22503 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22504 if (mode == VOIDmode)
22505 mode = GET_MODE (XEXP (arg, 0));
22506 }
22507 if (mode == VOIDmode || mode == BLKmode)
22508 continue;
22509 /* Get dynamic information about call target only if we
22510 have no static information: we cannot generate both
22511 DW_AT_call_origin and DW_AT_call_target
22512 attributes. */
22513 if (ca_loc->symbol_ref == NULL_RTX)
22514 {
22515 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22516 {
22517 tloc = XEXP (XEXP (arg, 0), 1);
22518 continue;
22519 }
22520 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22521 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22522 {
22523 tlocc = XEXP (XEXP (arg, 0), 1);
22524 continue;
22525 }
22526 }
22527 reg = NULL;
22528 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22529 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22530 VAR_INIT_STATUS_INITIALIZED);
22531 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22532 {
22533 rtx mem = XEXP (XEXP (arg, 0), 0);
22534 reg = mem_loc_descriptor (XEXP (mem, 0),
22535 get_address_mode (mem),
22536 GET_MODE (mem),
22537 VAR_INIT_STATUS_INITIALIZED);
22538 }
22539 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22540 == DEBUG_PARAMETER_REF)
22541 {
22542 tree tdecl
22543 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22544 tdie = lookup_decl_die (tdecl);
22545 if (tdie == NULL)
22546 continue;
22547 }
22548 else
22549 continue;
22550 if (reg == NULL
22551 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22552 != DEBUG_PARAMETER_REF)
22553 continue;
22554 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22555 VOIDmode,
22556 VAR_INIT_STATUS_INITIALIZED);
22557 if (val == NULL)
22558 continue;
22559 if (die == NULL)
22560 die = gen_call_site_die (decl, subr_die, ca_loc);
22561 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22562 NULL_TREE);
22563 if (reg != NULL)
22564 add_AT_loc (cdie, DW_AT_location, reg);
22565 else if (tdie != NULL)
22566 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22567 tdie);
22568 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22569 if (next_arg != XEXP (arg, 1))
22570 {
22571 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22572 if (mode == VOIDmode)
22573 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22574 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22575 0), 1),
22576 mode, VOIDmode,
22577 VAR_INIT_STATUS_INITIALIZED);
22578 if (val != NULL)
22579 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22580 val);
22581 }
22582 }
22583 if (die == NULL
22584 && (ca_loc->symbol_ref || tloc))
22585 die = gen_call_site_die (decl, subr_die, ca_loc);
22586 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22587 {
22588 dw_loc_descr_ref tval = NULL;
22589
22590 if (tloc != NULL_RTX)
22591 tval = mem_loc_descriptor (tloc,
22592 GET_MODE (tloc) == VOIDmode
22593 ? Pmode : GET_MODE (tloc),
22594 VOIDmode,
22595 VAR_INIT_STATUS_INITIALIZED);
22596 if (tval)
22597 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22598 else if (tlocc != NULL_RTX)
22599 {
22600 tval = mem_loc_descriptor (tlocc,
22601 GET_MODE (tlocc) == VOIDmode
22602 ? Pmode : GET_MODE (tlocc),
22603 VOIDmode,
22604 VAR_INIT_STATUS_INITIALIZED);
22605 if (tval)
22606 add_AT_loc (die,
22607 dwarf_AT (DW_AT_call_target_clobbered),
22608 tval);
22609 }
22610 }
22611 if (die != NULL)
22612 {
22613 call_site_note_count++;
22614 if (ca_loc->tail_call_p)
22615 tail_call_site_note_count++;
22616 }
22617 }
22618 }
22619 call_arg_locations = NULL;
22620 call_arg_loc_last = NULL;
22621 if (tail_call_site_count >= 0
22622 && tail_call_site_count == tail_call_site_note_count
22623 && (!dwarf_strict || dwarf_version >= 5))
22624 {
22625 if (call_site_count >= 0
22626 && call_site_count == call_site_note_count)
22627 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22628 else
22629 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22630 }
22631 call_site_count = -1;
22632 tail_call_site_count = -1;
22633 }
22634
22635 /* Mark used types after we have created DIEs for the functions scopes. */
22636 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22637 }
22638
22639 /* Returns a hash value for X (which really is a die_struct). */
22640
22641 hashval_t
22642 block_die_hasher::hash (die_struct *d)
22643 {
22644 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22645 }
22646
22647 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22648 as decl_id and die_parent of die_struct Y. */
22649
22650 bool
22651 block_die_hasher::equal (die_struct *x, die_struct *y)
22652 {
22653 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22654 }
22655
22656 /* Return TRUE if DECL, which may have been previously generated as
22657 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22658 true if decl (or its origin) is either an extern declaration or a
22659 class/namespace scoped declaration.
22660
22661 The declare_in_namespace support causes us to get two DIEs for one
22662 variable, both of which are declarations. We want to avoid
22663 considering one to be a specification, so we must test for
22664 DECLARATION and DW_AT_declaration. */
22665 static inline bool
22666 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22667 {
22668 return (old_die && TREE_STATIC (decl) && !declaration
22669 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22670 }
22671
22672 /* Return true if DECL is a local static. */
22673
22674 static inline bool
22675 local_function_static (tree decl)
22676 {
22677 gcc_assert (VAR_P (decl));
22678 return TREE_STATIC (decl)
22679 && DECL_CONTEXT (decl)
22680 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22681 }
22682
22683 /* Generate a DIE to represent a declared data object.
22684 Either DECL or ORIGIN must be non-null. */
22685
22686 static void
22687 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22688 {
22689 HOST_WIDE_INT off = 0;
22690 tree com_decl;
22691 tree decl_or_origin = decl ? decl : origin;
22692 tree ultimate_origin;
22693 dw_die_ref var_die;
22694 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22695 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22696 || class_or_namespace_scope_p (context_die));
22697 bool specialization_p = false;
22698 bool no_linkage_name = false;
22699
22700 /* While C++ inline static data members have definitions inside of the
22701 class, force the first DIE to be a declaration, then let gen_member_die
22702 reparent it to the class context and call gen_variable_die again
22703 to create the outside of the class DIE for the definition. */
22704 if (!declaration
22705 && old_die == NULL
22706 && decl
22707 && DECL_CONTEXT (decl)
22708 && TYPE_P (DECL_CONTEXT (decl))
22709 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22710 {
22711 declaration = true;
22712 if (dwarf_version < 5)
22713 no_linkage_name = true;
22714 }
22715
22716 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22717 if (decl || ultimate_origin)
22718 origin = ultimate_origin;
22719 com_decl = fortran_common (decl_or_origin, &off);
22720
22721 /* Symbol in common gets emitted as a child of the common block, in the form
22722 of a data member. */
22723 if (com_decl)
22724 {
22725 dw_die_ref com_die;
22726 dw_loc_list_ref loc = NULL;
22727 die_node com_die_arg;
22728
22729 var_die = lookup_decl_die (decl_or_origin);
22730 if (var_die)
22731 {
22732 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22733 {
22734 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22735 if (loc)
22736 {
22737 if (off)
22738 {
22739 /* Optimize the common case. */
22740 if (single_element_loc_list_p (loc)
22741 && loc->expr->dw_loc_opc == DW_OP_addr
22742 && loc->expr->dw_loc_next == NULL
22743 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22744 == SYMBOL_REF)
22745 {
22746 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22747 loc->expr->dw_loc_oprnd1.v.val_addr
22748 = plus_constant (GET_MODE (x), x , off);
22749 }
22750 else
22751 loc_list_plus_const (loc, off);
22752 }
22753 add_AT_location_description (var_die, DW_AT_location, loc);
22754 remove_AT (var_die, DW_AT_declaration);
22755 }
22756 }
22757 return;
22758 }
22759
22760 if (common_block_die_table == NULL)
22761 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22762
22763 com_die_arg.decl_id = DECL_UID (com_decl);
22764 com_die_arg.die_parent = context_die;
22765 com_die = common_block_die_table->find (&com_die_arg);
22766 if (! early_dwarf)
22767 loc = loc_list_from_tree (com_decl, 2, NULL);
22768 if (com_die == NULL)
22769 {
22770 const char *cnam
22771 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22772 die_node **slot;
22773
22774 com_die = new_die (DW_TAG_common_block, context_die, decl);
22775 add_name_and_src_coords_attributes (com_die, com_decl);
22776 if (loc)
22777 {
22778 add_AT_location_description (com_die, DW_AT_location, loc);
22779 /* Avoid sharing the same loc descriptor between
22780 DW_TAG_common_block and DW_TAG_variable. */
22781 loc = loc_list_from_tree (com_decl, 2, NULL);
22782 }
22783 else if (DECL_EXTERNAL (decl_or_origin))
22784 add_AT_flag (com_die, DW_AT_declaration, 1);
22785 if (want_pubnames ())
22786 add_pubname_string (cnam, com_die); /* ??? needed? */
22787 com_die->decl_id = DECL_UID (com_decl);
22788 slot = common_block_die_table->find_slot (com_die, INSERT);
22789 *slot = com_die;
22790 }
22791 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22792 {
22793 add_AT_location_description (com_die, DW_AT_location, loc);
22794 loc = loc_list_from_tree (com_decl, 2, NULL);
22795 remove_AT (com_die, DW_AT_declaration);
22796 }
22797 var_die = new_die (DW_TAG_variable, com_die, decl);
22798 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22799 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22800 decl_quals (decl_or_origin), false,
22801 context_die);
22802 add_alignment_attribute (var_die, decl);
22803 add_AT_flag (var_die, DW_AT_external, 1);
22804 if (loc)
22805 {
22806 if (off)
22807 {
22808 /* Optimize the common case. */
22809 if (single_element_loc_list_p (loc)
22810 && loc->expr->dw_loc_opc == DW_OP_addr
22811 && loc->expr->dw_loc_next == NULL
22812 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22813 {
22814 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22815 loc->expr->dw_loc_oprnd1.v.val_addr
22816 = plus_constant (GET_MODE (x), x, off);
22817 }
22818 else
22819 loc_list_plus_const (loc, off);
22820 }
22821 add_AT_location_description (var_die, DW_AT_location, loc);
22822 }
22823 else if (DECL_EXTERNAL (decl_or_origin))
22824 add_AT_flag (var_die, DW_AT_declaration, 1);
22825 if (decl)
22826 equate_decl_number_to_die (decl, var_die);
22827 return;
22828 }
22829
22830 if (old_die)
22831 {
22832 if (declaration)
22833 {
22834 /* A declaration that has been previously dumped, needs no
22835 further annotations, since it doesn't need location on
22836 the second pass. */
22837 return;
22838 }
22839 else if (decl_will_get_specification_p (old_die, decl, declaration)
22840 && !get_AT (old_die, DW_AT_specification))
22841 {
22842 /* Fall-thru so we can make a new variable die along with a
22843 DW_AT_specification. */
22844 }
22845 else if (origin && old_die->die_parent != context_die)
22846 {
22847 /* If we will be creating an inlined instance, we need a
22848 new DIE that will get annotated with
22849 DW_AT_abstract_origin. Clear things so we can get a
22850 new DIE. */
22851 gcc_assert (!DECL_ABSTRACT_P (decl));
22852 old_die = NULL;
22853 }
22854 else
22855 {
22856 /* If a DIE was dumped early, it still needs location info.
22857 Skip to where we fill the location bits. */
22858 var_die = old_die;
22859
22860 /* ??? In LTRANS we cannot annotate early created variably
22861 modified type DIEs without copying them and adjusting all
22862 references to them. Thus we dumped them again, also add a
22863 reference to them. */
22864 tree type = TREE_TYPE (decl_or_origin);
22865 if (in_lto_p
22866 && variably_modified_type_p
22867 (type, decl_function_context (decl_or_origin)))
22868 {
22869 if (decl_by_reference_p (decl_or_origin))
22870 add_type_attribute (var_die, TREE_TYPE (type),
22871 TYPE_UNQUALIFIED, false, context_die);
22872 else
22873 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22874 false, context_die);
22875 }
22876
22877 goto gen_variable_die_location;
22878 }
22879 }
22880
22881 /* For static data members, the declaration in the class is supposed
22882 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22883 also in DWARF2; the specification should still be DW_TAG_variable
22884 referencing the DW_TAG_member DIE. */
22885 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22886 var_die = new_die (DW_TAG_member, context_die, decl);
22887 else
22888 var_die = new_die (DW_TAG_variable, context_die, decl);
22889
22890 if (origin != NULL)
22891 add_abstract_origin_attribute (var_die, origin);
22892
22893 /* Loop unrolling can create multiple blocks that refer to the same
22894 static variable, so we must test for the DW_AT_declaration flag.
22895
22896 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22897 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22898 sharing them.
22899
22900 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22901 else if (decl_will_get_specification_p (old_die, decl, declaration))
22902 {
22903 /* This is a definition of a C++ class level static. */
22904 add_AT_specification (var_die, old_die);
22905 specialization_p = true;
22906 if (DECL_NAME (decl))
22907 {
22908 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22909 struct dwarf_file_data * file_index = lookup_filename (s.file);
22910
22911 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22912 add_AT_file (var_die, DW_AT_decl_file, file_index);
22913
22914 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22915 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22916
22917 if (debug_column_info
22918 && s.column
22919 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22920 != (unsigned) s.column))
22921 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22922
22923 if (old_die->die_tag == DW_TAG_member)
22924 add_linkage_name (var_die, decl);
22925 }
22926 }
22927 else
22928 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22929
22930 if ((origin == NULL && !specialization_p)
22931 || (origin != NULL
22932 && !DECL_ABSTRACT_P (decl_or_origin)
22933 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22934 decl_function_context
22935 (decl_or_origin))))
22936 {
22937 tree type = TREE_TYPE (decl_or_origin);
22938
22939 if (decl_by_reference_p (decl_or_origin))
22940 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22941 context_die);
22942 else
22943 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22944 context_die);
22945 }
22946
22947 if (origin == NULL && !specialization_p)
22948 {
22949 if (TREE_PUBLIC (decl))
22950 add_AT_flag (var_die, DW_AT_external, 1);
22951
22952 if (DECL_ARTIFICIAL (decl))
22953 add_AT_flag (var_die, DW_AT_artificial, 1);
22954
22955 add_alignment_attribute (var_die, decl);
22956
22957 add_accessibility_attribute (var_die, decl);
22958 }
22959
22960 if (declaration)
22961 add_AT_flag (var_die, DW_AT_declaration, 1);
22962
22963 if (decl && (DECL_ABSTRACT_P (decl)
22964 || !old_die || is_declaration_die (old_die)))
22965 equate_decl_number_to_die (decl, var_die);
22966
22967 gen_variable_die_location:
22968 if (! declaration
22969 && (! DECL_ABSTRACT_P (decl_or_origin)
22970 /* Local static vars are shared between all clones/inlines,
22971 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22972 already set. */
22973 || (VAR_P (decl_or_origin)
22974 && TREE_STATIC (decl_or_origin)
22975 && DECL_RTL_SET_P (decl_or_origin))))
22976 {
22977 if (early_dwarf)
22978 add_pubname (decl_or_origin, var_die);
22979 else
22980 add_location_or_const_value_attribute (var_die, decl_or_origin,
22981 decl == NULL);
22982 }
22983 else
22984 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22985
22986 if ((dwarf_version >= 4 || !dwarf_strict)
22987 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22988 DW_AT_const_expr) == 1
22989 && !get_AT (var_die, DW_AT_const_expr)
22990 && !specialization_p)
22991 add_AT_flag (var_die, DW_AT_const_expr, 1);
22992
22993 if (!dwarf_strict)
22994 {
22995 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22996 DW_AT_inline);
22997 if (inl != -1
22998 && !get_AT (var_die, DW_AT_inline)
22999 && !specialization_p)
23000 add_AT_unsigned (var_die, DW_AT_inline, inl);
23001 }
23002 }
23003
23004 /* Generate a DIE to represent a named constant. */
23005
23006 static void
23007 gen_const_die (tree decl, dw_die_ref context_die)
23008 {
23009 dw_die_ref const_die;
23010 tree type = TREE_TYPE (decl);
23011
23012 const_die = lookup_decl_die (decl);
23013 if (const_die)
23014 return;
23015
23016 const_die = new_die (DW_TAG_constant, context_die, decl);
23017 equate_decl_number_to_die (decl, const_die);
23018 add_name_and_src_coords_attributes (const_die, decl);
23019 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23020 if (TREE_PUBLIC (decl))
23021 add_AT_flag (const_die, DW_AT_external, 1);
23022 if (DECL_ARTIFICIAL (decl))
23023 add_AT_flag (const_die, DW_AT_artificial, 1);
23024 tree_add_const_value_attribute_for_decl (const_die, decl);
23025 }
23026
23027 /* Generate a DIE to represent a label identifier. */
23028
23029 static void
23030 gen_label_die (tree decl, dw_die_ref context_die)
23031 {
23032 tree origin = decl_ultimate_origin (decl);
23033 dw_die_ref lbl_die = lookup_decl_die (decl);
23034 rtx insn;
23035 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23036
23037 if (!lbl_die)
23038 {
23039 lbl_die = new_die (DW_TAG_label, context_die, decl);
23040 equate_decl_number_to_die (decl, lbl_die);
23041
23042 if (origin != NULL)
23043 add_abstract_origin_attribute (lbl_die, origin);
23044 else
23045 add_name_and_src_coords_attributes (lbl_die, decl);
23046 }
23047
23048 if (DECL_ABSTRACT_P (decl))
23049 equate_decl_number_to_die (decl, lbl_die);
23050 else if (! early_dwarf)
23051 {
23052 insn = DECL_RTL_IF_SET (decl);
23053
23054 /* Deleted labels are programmer specified labels which have been
23055 eliminated because of various optimizations. We still emit them
23056 here so that it is possible to put breakpoints on them. */
23057 if (insn
23058 && (LABEL_P (insn)
23059 || ((NOTE_P (insn)
23060 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23061 {
23062 /* When optimization is enabled (via -O) some parts of the compiler
23063 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23064 represent source-level labels which were explicitly declared by
23065 the user. This really shouldn't be happening though, so catch
23066 it if it ever does happen. */
23067 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23068
23069 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23070 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23071 }
23072 else if (insn
23073 && NOTE_P (insn)
23074 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23075 && CODE_LABEL_NUMBER (insn) != -1)
23076 {
23077 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23078 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23079 }
23080 }
23081 }
23082
23083 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23084 attributes to the DIE for a block STMT, to describe where the inlined
23085 function was called from. This is similar to add_src_coords_attributes. */
23086
23087 static inline void
23088 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23089 {
23090 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23091
23092 if (dwarf_version >= 3 || !dwarf_strict)
23093 {
23094 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23095 add_AT_unsigned (die, DW_AT_call_line, s.line);
23096 if (debug_column_info && s.column)
23097 add_AT_unsigned (die, DW_AT_call_column, s.column);
23098 }
23099 }
23100
23101
23102 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23103 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23104
23105 static inline void
23106 add_high_low_attributes (tree stmt, dw_die_ref die)
23107 {
23108 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23109
23110 if (BLOCK_FRAGMENT_CHAIN (stmt)
23111 && (dwarf_version >= 3 || !dwarf_strict))
23112 {
23113 tree chain, superblock = NULL_TREE;
23114 dw_die_ref pdie;
23115 dw_attr_node *attr = NULL;
23116
23117 if (inlined_function_outer_scope_p (stmt))
23118 {
23119 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23120 BLOCK_NUMBER (stmt));
23121 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23122 }
23123
23124 /* Optimize duplicate .debug_ranges lists or even tails of
23125 lists. If this BLOCK has same ranges as its supercontext,
23126 lookup DW_AT_ranges attribute in the supercontext (and
23127 recursively so), verify that the ranges_table contains the
23128 right values and use it instead of adding a new .debug_range. */
23129 for (chain = stmt, pdie = die;
23130 BLOCK_SAME_RANGE (chain);
23131 chain = BLOCK_SUPERCONTEXT (chain))
23132 {
23133 dw_attr_node *new_attr;
23134
23135 pdie = pdie->die_parent;
23136 if (pdie == NULL)
23137 break;
23138 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23139 break;
23140 new_attr = get_AT (pdie, DW_AT_ranges);
23141 if (new_attr == NULL
23142 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23143 break;
23144 attr = new_attr;
23145 superblock = BLOCK_SUPERCONTEXT (chain);
23146 }
23147 if (attr != NULL
23148 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23149 == BLOCK_NUMBER (superblock))
23150 && BLOCK_FRAGMENT_CHAIN (superblock))
23151 {
23152 unsigned long off = attr->dw_attr_val.v.val_offset;
23153 unsigned long supercnt = 0, thiscnt = 0;
23154 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23155 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23156 {
23157 ++supercnt;
23158 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23159 == BLOCK_NUMBER (chain));
23160 }
23161 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23162 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23163 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23164 ++thiscnt;
23165 gcc_assert (supercnt >= thiscnt);
23166 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23167 false);
23168 note_rnglist_head (off + supercnt - thiscnt);
23169 return;
23170 }
23171
23172 unsigned int offset = add_ranges (stmt, true);
23173 add_AT_range_list (die, DW_AT_ranges, offset, false);
23174 note_rnglist_head (offset);
23175
23176 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23177 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23178 do
23179 {
23180 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23181 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23182 chain = BLOCK_FRAGMENT_CHAIN (chain);
23183 }
23184 while (chain);
23185 add_ranges (NULL);
23186 }
23187 else
23188 {
23189 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23190 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23191 BLOCK_NUMBER (stmt));
23192 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23193 BLOCK_NUMBER (stmt));
23194 add_AT_low_high_pc (die, label, label_high, false);
23195 }
23196 }
23197
23198 /* Generate a DIE for a lexical block. */
23199
23200 static void
23201 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23202 {
23203 dw_die_ref old_die = BLOCK_DIE (stmt);
23204 dw_die_ref stmt_die = NULL;
23205 if (!old_die)
23206 {
23207 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23208 BLOCK_DIE (stmt) = stmt_die;
23209 }
23210
23211 if (BLOCK_ABSTRACT (stmt))
23212 {
23213 if (old_die)
23214 {
23215 /* This must have been generated early and it won't even
23216 need location information since it's a DW_AT_inline
23217 function. */
23218 if (flag_checking)
23219 for (dw_die_ref c = context_die; c; c = c->die_parent)
23220 if (c->die_tag == DW_TAG_inlined_subroutine
23221 || c->die_tag == DW_TAG_subprogram)
23222 {
23223 gcc_assert (get_AT (c, DW_AT_inline));
23224 break;
23225 }
23226 return;
23227 }
23228 }
23229 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23230 {
23231 /* If this is an inlined instance, create a new lexical die for
23232 anything below to attach DW_AT_abstract_origin to. */
23233 if (old_die)
23234 {
23235 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23236 BLOCK_DIE (stmt) = stmt_die;
23237 old_die = NULL;
23238 }
23239
23240 tree origin = block_ultimate_origin (stmt);
23241 if (origin != NULL_TREE && origin != stmt)
23242 add_abstract_origin_attribute (stmt_die, origin);
23243 }
23244
23245 if (old_die)
23246 stmt_die = old_die;
23247
23248 /* A non abstract block whose blocks have already been reordered
23249 should have the instruction range for this block. If so, set the
23250 high/low attributes. */
23251 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23252 {
23253 gcc_assert (stmt_die);
23254 add_high_low_attributes (stmt, stmt_die);
23255 }
23256
23257 decls_for_scope (stmt, stmt_die);
23258 }
23259
23260 /* Generate a DIE for an inlined subprogram. */
23261
23262 static void
23263 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23264 {
23265 tree decl;
23266
23267 /* The instance of function that is effectively being inlined shall not
23268 be abstract. */
23269 gcc_assert (! BLOCK_ABSTRACT (stmt));
23270
23271 decl = block_ultimate_origin (stmt);
23272
23273 /* Make sure any inlined functions are known to be inlineable. */
23274 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23275 || cgraph_function_possibly_inlined_p (decl));
23276
23277 if (! BLOCK_ABSTRACT (stmt))
23278 {
23279 dw_die_ref subr_die
23280 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23281
23282 if (call_arg_locations)
23283 BLOCK_DIE (stmt) = subr_die;
23284 add_abstract_origin_attribute (subr_die, decl);
23285 if (TREE_ASM_WRITTEN (stmt))
23286 add_high_low_attributes (stmt, subr_die);
23287 add_call_src_coords_attributes (stmt, subr_die);
23288
23289 decls_for_scope (stmt, subr_die);
23290 }
23291 }
23292
23293 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23294 the comment for VLR_CONTEXT. */
23295
23296 static void
23297 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23298 {
23299 dw_die_ref decl_die;
23300
23301 if (TREE_TYPE (decl) == error_mark_node)
23302 return;
23303
23304 decl_die = new_die (DW_TAG_member, context_die, decl);
23305 add_name_and_src_coords_attributes (decl_die, decl);
23306 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23307 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23308 context_die);
23309
23310 if (DECL_BIT_FIELD_TYPE (decl))
23311 {
23312 add_byte_size_attribute (decl_die, decl);
23313 add_bit_size_attribute (decl_die, decl);
23314 add_bit_offset_attribute (decl_die, decl, ctx);
23315 }
23316
23317 add_alignment_attribute (decl_die, decl);
23318
23319 /* If we have a variant part offset, then we are supposed to process a member
23320 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23321 trees. */
23322 gcc_assert (ctx->variant_part_offset == NULL_TREE
23323 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23324 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23325 add_data_member_location_attribute (decl_die, decl, ctx);
23326
23327 if (DECL_ARTIFICIAL (decl))
23328 add_AT_flag (decl_die, DW_AT_artificial, 1);
23329
23330 add_accessibility_attribute (decl_die, decl);
23331
23332 /* Equate decl number to die, so that we can look up this decl later on. */
23333 equate_decl_number_to_die (decl, decl_die);
23334 }
23335
23336 /* Generate a DIE for a pointer to a member type. TYPE can be an
23337 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23338 pointer to member function. */
23339
23340 static void
23341 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23342 {
23343 if (lookup_type_die (type))
23344 return;
23345
23346 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23347 scope_die_for (type, context_die), type);
23348
23349 equate_type_number_to_die (type, ptr_die);
23350 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23351 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23352 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23353 context_die);
23354 add_alignment_attribute (ptr_die, type);
23355
23356 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23357 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23358 {
23359 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23360 add_AT_loc (ptr_die, DW_AT_use_location, op);
23361 }
23362 }
23363
23364 static char *producer_string;
23365
23366 /* Return a heap allocated producer string including command line options
23367 if -grecord-gcc-switches. */
23368
23369 static char *
23370 gen_producer_string (void)
23371 {
23372 size_t j;
23373 auto_vec<const char *> switches;
23374 const char *language_string = lang_hooks.name;
23375 char *producer, *tail;
23376 const char *p;
23377 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23378 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23379
23380 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23381 switch (save_decoded_options[j].opt_index)
23382 {
23383 case OPT_o:
23384 case OPT_d:
23385 case OPT_dumpbase:
23386 case OPT_dumpdir:
23387 case OPT_auxbase:
23388 case OPT_auxbase_strip:
23389 case OPT_quiet:
23390 case OPT_version:
23391 case OPT_v:
23392 case OPT_w:
23393 case OPT_L:
23394 case OPT_D:
23395 case OPT_I:
23396 case OPT_U:
23397 case OPT_SPECIAL_unknown:
23398 case OPT_SPECIAL_ignore:
23399 case OPT_SPECIAL_program_name:
23400 case OPT_SPECIAL_input_file:
23401 case OPT_grecord_gcc_switches:
23402 case OPT__output_pch_:
23403 case OPT_fdiagnostics_show_location_:
23404 case OPT_fdiagnostics_show_option:
23405 case OPT_fdiagnostics_show_caret:
23406 case OPT_fdiagnostics_color_:
23407 case OPT_fverbose_asm:
23408 case OPT____:
23409 case OPT__sysroot_:
23410 case OPT_nostdinc:
23411 case OPT_nostdinc__:
23412 case OPT_fpreprocessed:
23413 case OPT_fltrans_output_list_:
23414 case OPT_fresolution_:
23415 case OPT_fdebug_prefix_map_:
23416 case OPT_fcompare_debug:
23417 /* Ignore these. */
23418 continue;
23419 default:
23420 if (cl_options[save_decoded_options[j].opt_index].flags
23421 & CL_NO_DWARF_RECORD)
23422 continue;
23423 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23424 == '-');
23425 switch (save_decoded_options[j].canonical_option[0][1])
23426 {
23427 case 'M':
23428 case 'i':
23429 case 'W':
23430 continue;
23431 case 'f':
23432 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23433 "dump", 4) == 0)
23434 continue;
23435 break;
23436 default:
23437 break;
23438 }
23439 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23440 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23441 break;
23442 }
23443
23444 producer = XNEWVEC (char, plen + 1 + len + 1);
23445 tail = producer;
23446 sprintf (tail, "%s %s", language_string, version_string);
23447 tail += plen;
23448
23449 FOR_EACH_VEC_ELT (switches, j, p)
23450 {
23451 len = strlen (p);
23452 *tail = ' ';
23453 memcpy (tail + 1, p, len);
23454 tail += len + 1;
23455 }
23456
23457 *tail = '\0';
23458 return producer;
23459 }
23460
23461 /* Given a C and/or C++ language/version string return the "highest".
23462 C++ is assumed to be "higher" than C in this case. Used for merging
23463 LTO translation unit languages. */
23464 static const char *
23465 highest_c_language (const char *lang1, const char *lang2)
23466 {
23467 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23468 return "GNU C++17";
23469 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23470 return "GNU C++14";
23471 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23472 return "GNU C++11";
23473 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23474 return "GNU C++98";
23475
23476 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23477 return "GNU C17";
23478 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23479 return "GNU C11";
23480 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23481 return "GNU C99";
23482 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23483 return "GNU C89";
23484
23485 gcc_unreachable ();
23486 }
23487
23488
23489 /* Generate the DIE for the compilation unit. */
23490
23491 static dw_die_ref
23492 gen_compile_unit_die (const char *filename)
23493 {
23494 dw_die_ref die;
23495 const char *language_string = lang_hooks.name;
23496 int language;
23497
23498 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23499
23500 if (filename)
23501 {
23502 add_name_attribute (die, filename);
23503 /* Don't add cwd for <built-in>. */
23504 if (filename[0] != '<')
23505 add_comp_dir_attribute (die);
23506 }
23507
23508 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23509
23510 /* If our producer is LTO try to figure out a common language to use
23511 from the global list of translation units. */
23512 if (strcmp (language_string, "GNU GIMPLE") == 0)
23513 {
23514 unsigned i;
23515 tree t;
23516 const char *common_lang = NULL;
23517
23518 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23519 {
23520 if (!TRANSLATION_UNIT_LANGUAGE (t))
23521 continue;
23522 if (!common_lang)
23523 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23524 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23525 ;
23526 else if (strncmp (common_lang, "GNU C", 5) == 0
23527 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23528 /* Mixing C and C++ is ok, use C++ in that case. */
23529 common_lang = highest_c_language (common_lang,
23530 TRANSLATION_UNIT_LANGUAGE (t));
23531 else
23532 {
23533 /* Fall back to C. */
23534 common_lang = NULL;
23535 break;
23536 }
23537 }
23538
23539 if (common_lang)
23540 language_string = common_lang;
23541 }
23542
23543 language = DW_LANG_C;
23544 if (strncmp (language_string, "GNU C", 5) == 0
23545 && ISDIGIT (language_string[5]))
23546 {
23547 language = DW_LANG_C89;
23548 if (dwarf_version >= 3 || !dwarf_strict)
23549 {
23550 if (strcmp (language_string, "GNU C89") != 0)
23551 language = DW_LANG_C99;
23552
23553 if (dwarf_version >= 5 /* || !dwarf_strict */)
23554 if (strcmp (language_string, "GNU C11") == 0
23555 || strcmp (language_string, "GNU C17") == 0)
23556 language = DW_LANG_C11;
23557 }
23558 }
23559 else if (strncmp (language_string, "GNU C++", 7) == 0)
23560 {
23561 language = DW_LANG_C_plus_plus;
23562 if (dwarf_version >= 5 /* || !dwarf_strict */)
23563 {
23564 if (strcmp (language_string, "GNU C++11") == 0)
23565 language = DW_LANG_C_plus_plus_11;
23566 else if (strcmp (language_string, "GNU C++14") == 0)
23567 language = DW_LANG_C_plus_plus_14;
23568 else if (strcmp (language_string, "GNU C++17") == 0)
23569 /* For now. */
23570 language = DW_LANG_C_plus_plus_14;
23571 }
23572 }
23573 else if (strcmp (language_string, "GNU F77") == 0)
23574 language = DW_LANG_Fortran77;
23575 else if (dwarf_version >= 3 || !dwarf_strict)
23576 {
23577 if (strcmp (language_string, "GNU Ada") == 0)
23578 language = DW_LANG_Ada95;
23579 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23580 {
23581 language = DW_LANG_Fortran95;
23582 if (dwarf_version >= 5 /* || !dwarf_strict */)
23583 {
23584 if (strcmp (language_string, "GNU Fortran2003") == 0)
23585 language = DW_LANG_Fortran03;
23586 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23587 language = DW_LANG_Fortran08;
23588 }
23589 }
23590 else if (strcmp (language_string, "GNU Objective-C") == 0)
23591 language = DW_LANG_ObjC;
23592 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23593 language = DW_LANG_ObjC_plus_plus;
23594 else if (dwarf_version >= 5 || !dwarf_strict)
23595 {
23596 if (strcmp (language_string, "GNU Go") == 0)
23597 language = DW_LANG_Go;
23598 }
23599 }
23600 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23601 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23602 language = DW_LANG_Fortran90;
23603
23604 add_AT_unsigned (die, DW_AT_language, language);
23605
23606 switch (language)
23607 {
23608 case DW_LANG_Fortran77:
23609 case DW_LANG_Fortran90:
23610 case DW_LANG_Fortran95:
23611 case DW_LANG_Fortran03:
23612 case DW_LANG_Fortran08:
23613 /* Fortran has case insensitive identifiers and the front-end
23614 lowercases everything. */
23615 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23616 break;
23617 default:
23618 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23619 break;
23620 }
23621 return die;
23622 }
23623
23624 /* Generate the DIE for a base class. */
23625
23626 static void
23627 gen_inheritance_die (tree binfo, tree access, tree type,
23628 dw_die_ref context_die)
23629 {
23630 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23631 struct vlr_context ctx = { type, NULL };
23632
23633 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23634 context_die);
23635 add_data_member_location_attribute (die, binfo, &ctx);
23636
23637 if (BINFO_VIRTUAL_P (binfo))
23638 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23639
23640 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23641 children, otherwise the default is DW_ACCESS_public. In DWARF2
23642 the default has always been DW_ACCESS_private. */
23643 if (access == access_public_node)
23644 {
23645 if (dwarf_version == 2
23646 || context_die->die_tag == DW_TAG_class_type)
23647 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23648 }
23649 else if (access == access_protected_node)
23650 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23651 else if (dwarf_version > 2
23652 && context_die->die_tag != DW_TAG_class_type)
23653 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23654 }
23655
23656 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23657 structure. */
23658 static bool
23659 is_variant_part (tree decl)
23660 {
23661 return (TREE_CODE (decl) == FIELD_DECL
23662 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23663 }
23664
23665 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23666 return the FIELD_DECL. Return NULL_TREE otherwise. */
23667
23668 static tree
23669 analyze_discr_in_predicate (tree operand, tree struct_type)
23670 {
23671 bool continue_stripping = true;
23672 while (continue_stripping)
23673 switch (TREE_CODE (operand))
23674 {
23675 CASE_CONVERT:
23676 operand = TREE_OPERAND (operand, 0);
23677 break;
23678 default:
23679 continue_stripping = false;
23680 break;
23681 }
23682
23683 /* Match field access to members of struct_type only. */
23684 if (TREE_CODE (operand) == COMPONENT_REF
23685 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23686 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23687 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23688 return TREE_OPERAND (operand, 1);
23689 else
23690 return NULL_TREE;
23691 }
23692
23693 /* Check that SRC is a constant integer that can be represented as a native
23694 integer constant (either signed or unsigned). If so, store it into DEST and
23695 return true. Return false otherwise. */
23696
23697 static bool
23698 get_discr_value (tree src, dw_discr_value *dest)
23699 {
23700 tree discr_type = TREE_TYPE (src);
23701
23702 if (lang_hooks.types.get_debug_type)
23703 {
23704 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23705 if (debug_type != NULL)
23706 discr_type = debug_type;
23707 }
23708
23709 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23710 return false;
23711
23712 /* Signedness can vary between the original type and the debug type. This
23713 can happen for character types in Ada for instance: the character type
23714 used for code generation can be signed, to be compatible with the C one,
23715 but from a debugger point of view, it must be unsigned. */
23716 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23717 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23718
23719 if (is_orig_unsigned != is_debug_unsigned)
23720 src = fold_convert (discr_type, src);
23721
23722 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23723 return false;
23724
23725 dest->pos = is_debug_unsigned;
23726 if (is_debug_unsigned)
23727 dest->v.uval = tree_to_uhwi (src);
23728 else
23729 dest->v.sval = tree_to_shwi (src);
23730
23731 return true;
23732 }
23733
23734 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23735 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23736 store NULL_TREE in DISCR_DECL. Otherwise:
23737
23738 - store the discriminant field in STRUCT_TYPE that controls the variant
23739 part to *DISCR_DECL
23740
23741 - put in *DISCR_LISTS_P an array where for each variant, the item
23742 represents the corresponding matching list of discriminant values.
23743
23744 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23745 the above array.
23746
23747 Note that when the array is allocated (i.e. when the analysis is
23748 successful), it is up to the caller to free the array. */
23749
23750 static void
23751 analyze_variants_discr (tree variant_part_decl,
23752 tree struct_type,
23753 tree *discr_decl,
23754 dw_discr_list_ref **discr_lists_p,
23755 unsigned *discr_lists_length)
23756 {
23757 tree variant_part_type = TREE_TYPE (variant_part_decl);
23758 tree variant;
23759 dw_discr_list_ref *discr_lists;
23760 unsigned i;
23761
23762 /* Compute how many variants there are in this variant part. */
23763 *discr_lists_length = 0;
23764 for (variant = TYPE_FIELDS (variant_part_type);
23765 variant != NULL_TREE;
23766 variant = DECL_CHAIN (variant))
23767 ++*discr_lists_length;
23768
23769 *discr_decl = NULL_TREE;
23770 *discr_lists_p
23771 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23772 sizeof (**discr_lists_p));
23773 discr_lists = *discr_lists_p;
23774
23775 /* And then analyze all variants to extract discriminant information for all
23776 of them. This analysis is conservative: as soon as we detect something we
23777 do not support, abort everything and pretend we found nothing. */
23778 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23779 variant != NULL_TREE;
23780 variant = DECL_CHAIN (variant), ++i)
23781 {
23782 tree match_expr = DECL_QUALIFIER (variant);
23783
23784 /* Now, try to analyze the predicate and deduce a discriminant for
23785 it. */
23786 if (match_expr == boolean_true_node)
23787 /* Typically happens for the default variant: it matches all cases that
23788 previous variants rejected. Don't output any matching value for
23789 this one. */
23790 continue;
23791
23792 /* The following loop tries to iterate over each discriminant
23793 possibility: single values or ranges. */
23794 while (match_expr != NULL_TREE)
23795 {
23796 tree next_round_match_expr;
23797 tree candidate_discr = NULL_TREE;
23798 dw_discr_list_ref new_node = NULL;
23799
23800 /* Possibilities are matched one after the other by nested
23801 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23802 continue with the rest at next iteration. */
23803 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23804 {
23805 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23806 match_expr = TREE_OPERAND (match_expr, 1);
23807 }
23808 else
23809 next_round_match_expr = NULL_TREE;
23810
23811 if (match_expr == boolean_false_node)
23812 /* This sub-expression matches nothing: just wait for the next
23813 one. */
23814 ;
23815
23816 else if (TREE_CODE (match_expr) == EQ_EXPR)
23817 {
23818 /* We are matching: <discr_field> == <integer_cst>
23819 This sub-expression matches a single value. */
23820 tree integer_cst = TREE_OPERAND (match_expr, 1);
23821
23822 candidate_discr
23823 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23824 struct_type);
23825
23826 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23827 if (!get_discr_value (integer_cst,
23828 &new_node->dw_discr_lower_bound))
23829 goto abort;
23830 new_node->dw_discr_range = false;
23831 }
23832
23833 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23834 {
23835 /* We are matching:
23836 <discr_field> > <integer_cst>
23837 && <discr_field> < <integer_cst>.
23838 This sub-expression matches the range of values between the
23839 two matched integer constants. Note that comparisons can be
23840 inclusive or exclusive. */
23841 tree candidate_discr_1, candidate_discr_2;
23842 tree lower_cst, upper_cst;
23843 bool lower_cst_included, upper_cst_included;
23844 tree lower_op = TREE_OPERAND (match_expr, 0);
23845 tree upper_op = TREE_OPERAND (match_expr, 1);
23846
23847 /* When the comparison is exclusive, the integer constant is not
23848 the discriminant range bound we are looking for: we will have
23849 to increment or decrement it. */
23850 if (TREE_CODE (lower_op) == GE_EXPR)
23851 lower_cst_included = true;
23852 else if (TREE_CODE (lower_op) == GT_EXPR)
23853 lower_cst_included = false;
23854 else
23855 goto abort;
23856
23857 if (TREE_CODE (upper_op) == LE_EXPR)
23858 upper_cst_included = true;
23859 else if (TREE_CODE (upper_op) == LT_EXPR)
23860 upper_cst_included = false;
23861 else
23862 goto abort;
23863
23864 /* Extract the discriminant from the first operand and check it
23865 is consistant with the same analysis in the second
23866 operand. */
23867 candidate_discr_1
23868 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23869 struct_type);
23870 candidate_discr_2
23871 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23872 struct_type);
23873 if (candidate_discr_1 == candidate_discr_2)
23874 candidate_discr = candidate_discr_1;
23875 else
23876 goto abort;
23877
23878 /* Extract bounds from both. */
23879 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23880 lower_cst = TREE_OPERAND (lower_op, 1);
23881 upper_cst = TREE_OPERAND (upper_op, 1);
23882
23883 if (!lower_cst_included)
23884 lower_cst
23885 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23886 build_int_cst (TREE_TYPE (lower_cst), 1));
23887 if (!upper_cst_included)
23888 upper_cst
23889 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23890 build_int_cst (TREE_TYPE (upper_cst), 1));
23891
23892 if (!get_discr_value (lower_cst,
23893 &new_node->dw_discr_lower_bound)
23894 || !get_discr_value (upper_cst,
23895 &new_node->dw_discr_upper_bound))
23896 goto abort;
23897
23898 new_node->dw_discr_range = true;
23899 }
23900
23901 else
23902 /* Unsupported sub-expression: we cannot determine the set of
23903 matching discriminant values. Abort everything. */
23904 goto abort;
23905
23906 /* If the discriminant info is not consistant with what we saw so
23907 far, consider the analysis failed and abort everything. */
23908 if (candidate_discr == NULL_TREE
23909 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23910 goto abort;
23911 else
23912 *discr_decl = candidate_discr;
23913
23914 if (new_node != NULL)
23915 {
23916 new_node->dw_discr_next = discr_lists[i];
23917 discr_lists[i] = new_node;
23918 }
23919 match_expr = next_round_match_expr;
23920 }
23921 }
23922
23923 /* If we reach this point, we could match everything we were interested
23924 in. */
23925 return;
23926
23927 abort:
23928 /* Clean all data structure and return no result. */
23929 free (*discr_lists_p);
23930 *discr_lists_p = NULL;
23931 *discr_decl = NULL_TREE;
23932 }
23933
23934 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23935 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23936 under CONTEXT_DIE.
23937
23938 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23939 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23940 this type, which are record types, represent the available variants and each
23941 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23942 values are inferred from these attributes.
23943
23944 In trees, the offsets for the fields inside these sub-records are relative
23945 to the variant part itself, whereas the corresponding DIEs should have
23946 offset attributes that are relative to the embedding record base address.
23947 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23948 must be an expression that computes the offset of the variant part to
23949 describe in DWARF. */
23950
23951 static void
23952 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23953 dw_die_ref context_die)
23954 {
23955 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23956 tree variant_part_offset = vlr_ctx->variant_part_offset;
23957 struct loc_descr_context ctx = {
23958 vlr_ctx->struct_type, /* context_type */
23959 NULL_TREE, /* base_decl */
23960 NULL, /* dpi */
23961 false, /* placeholder_arg */
23962 false /* placeholder_seen */
23963 };
23964
23965 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23966 NULL_TREE if there is no such field. */
23967 tree discr_decl = NULL_TREE;
23968 dw_discr_list_ref *discr_lists;
23969 unsigned discr_lists_length = 0;
23970 unsigned i;
23971
23972 dw_die_ref dwarf_proc_die = NULL;
23973 dw_die_ref variant_part_die
23974 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23975
23976 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23977
23978 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23979 &discr_decl, &discr_lists, &discr_lists_length);
23980
23981 if (discr_decl != NULL_TREE)
23982 {
23983 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23984
23985 if (discr_die)
23986 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23987 else
23988 /* We have no DIE for the discriminant, so just discard all
23989 discrimimant information in the output. */
23990 discr_decl = NULL_TREE;
23991 }
23992
23993 /* If the offset for this variant part is more complex than a constant,
23994 create a DWARF procedure for it so that we will not have to generate DWARF
23995 expressions for it for each member. */
23996 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23997 && (dwarf_version >= 3 || !dwarf_strict))
23998 {
23999 const tree dwarf_proc_fndecl
24000 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24001 build_function_type (TREE_TYPE (variant_part_offset),
24002 NULL_TREE));
24003 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24004 const dw_loc_descr_ref dwarf_proc_body
24005 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24006
24007 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24008 dwarf_proc_fndecl, context_die);
24009 if (dwarf_proc_die != NULL)
24010 variant_part_offset = dwarf_proc_call;
24011 }
24012
24013 /* Output DIEs for all variants. */
24014 i = 0;
24015 for (tree variant = TYPE_FIELDS (variant_part_type);
24016 variant != NULL_TREE;
24017 variant = DECL_CHAIN (variant), ++i)
24018 {
24019 tree variant_type = TREE_TYPE (variant);
24020 dw_die_ref variant_die;
24021
24022 /* All variants (i.e. members of a variant part) are supposed to be
24023 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24024 under these records. */
24025 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24026
24027 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24028 equate_decl_number_to_die (variant, variant_die);
24029
24030 /* Output discriminant values this variant matches, if any. */
24031 if (discr_decl == NULL || discr_lists[i] == NULL)
24032 /* In the case we have discriminant information at all, this is
24033 probably the default variant: as the standard says, don't
24034 output any discriminant value/list attribute. */
24035 ;
24036 else if (discr_lists[i]->dw_discr_next == NULL
24037 && !discr_lists[i]->dw_discr_range)
24038 /* If there is only one accepted value, don't bother outputting a
24039 list. */
24040 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24041 else
24042 add_discr_list (variant_die, discr_lists[i]);
24043
24044 for (tree member = TYPE_FIELDS (variant_type);
24045 member != NULL_TREE;
24046 member = DECL_CHAIN (member))
24047 {
24048 struct vlr_context vlr_sub_ctx = {
24049 vlr_ctx->struct_type, /* struct_type */
24050 NULL /* variant_part_offset */
24051 };
24052 if (is_variant_part (member))
24053 {
24054 /* All offsets for fields inside variant parts are relative to
24055 the top-level embedding RECORD_TYPE's base address. On the
24056 other hand, offsets in GCC's types are relative to the
24057 nested-most variant part. So we have to sum offsets each time
24058 we recurse. */
24059
24060 vlr_sub_ctx.variant_part_offset
24061 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24062 variant_part_offset, byte_position (member));
24063 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24064 }
24065 else
24066 {
24067 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24068 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24069 }
24070 }
24071 }
24072
24073 free (discr_lists);
24074 }
24075
24076 /* Generate a DIE for a class member. */
24077
24078 static void
24079 gen_member_die (tree type, dw_die_ref context_die)
24080 {
24081 tree member;
24082 tree binfo = TYPE_BINFO (type);
24083
24084 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24085
24086 /* If this is not an incomplete type, output descriptions of each of its
24087 members. Note that as we output the DIEs necessary to represent the
24088 members of this record or union type, we will also be trying to output
24089 DIEs to represent the *types* of those members. However the `type'
24090 function (above) will specifically avoid generating type DIEs for member
24091 types *within* the list of member DIEs for this (containing) type except
24092 for those types (of members) which are explicitly marked as also being
24093 members of this (containing) type themselves. The g++ front- end can
24094 force any given type to be treated as a member of some other (containing)
24095 type by setting the TYPE_CONTEXT of the given (member) type to point to
24096 the TREE node representing the appropriate (containing) type. */
24097
24098 /* First output info about the base classes. */
24099 if (binfo)
24100 {
24101 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24102 int i;
24103 tree base;
24104
24105 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24106 gen_inheritance_die (base,
24107 (accesses ? (*accesses)[i] : access_public_node),
24108 type,
24109 context_die);
24110 }
24111
24112 /* Now output info about the data members and type members. */
24113 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24114 {
24115 struct vlr_context vlr_ctx = { type, NULL_TREE };
24116 bool static_inline_p
24117 = (TREE_STATIC (member)
24118 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24119 != -1));
24120
24121 /* Ignore clones. */
24122 if (DECL_ABSTRACT_ORIGIN (member))
24123 continue;
24124
24125 /* If we thought we were generating minimal debug info for TYPE
24126 and then changed our minds, some of the member declarations
24127 may have already been defined. Don't define them again, but
24128 do put them in the right order. */
24129
24130 if (dw_die_ref child = lookup_decl_die (member))
24131 {
24132 /* Handle inline static data members, which only have in-class
24133 declarations. */
24134 dw_die_ref ref = NULL;
24135 if (child->die_tag == DW_TAG_variable
24136 && child->die_parent == comp_unit_die ())
24137 {
24138 ref = get_AT_ref (child, DW_AT_specification);
24139 /* For C++17 inline static data members followed by redundant
24140 out of class redeclaration, we might get here with
24141 child being the DIE created for the out of class
24142 redeclaration and with its DW_AT_specification being
24143 the DIE created for in-class definition. We want to
24144 reparent the latter, and don't want to create another
24145 DIE with DW_AT_specification in that case, because
24146 we already have one. */
24147 if (ref
24148 && static_inline_p
24149 && ref->die_tag == DW_TAG_variable
24150 && ref->die_parent == comp_unit_die ()
24151 && get_AT (ref, DW_AT_specification) == NULL)
24152 {
24153 child = ref;
24154 ref = NULL;
24155 static_inline_p = false;
24156 }
24157 }
24158
24159 if (child->die_tag == DW_TAG_variable
24160 && child->die_parent == comp_unit_die ()
24161 && ref == NULL)
24162 {
24163 reparent_child (child, context_die);
24164 if (dwarf_version < 5)
24165 child->die_tag = DW_TAG_member;
24166 }
24167 else
24168 splice_child_die (context_die, child);
24169 }
24170
24171 /* Do not generate standard DWARF for variant parts if we are generating
24172 the corresponding GNAT encodings: DIEs generated for both would
24173 conflict in our mappings. */
24174 else if (is_variant_part (member)
24175 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24176 {
24177 vlr_ctx.variant_part_offset = byte_position (member);
24178 gen_variant_part (member, &vlr_ctx, context_die);
24179 }
24180 else
24181 {
24182 vlr_ctx.variant_part_offset = NULL_TREE;
24183 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24184 }
24185
24186 /* For C++ inline static data members emit immediately a DW_TAG_variable
24187 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24188 DW_AT_specification. */
24189 if (static_inline_p)
24190 {
24191 int old_extern = DECL_EXTERNAL (member);
24192 DECL_EXTERNAL (member) = 0;
24193 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24194 DECL_EXTERNAL (member) = old_extern;
24195 }
24196 }
24197 }
24198
24199 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24200 is set, we pretend that the type was never defined, so we only get the
24201 member DIEs needed by later specification DIEs. */
24202
24203 static void
24204 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24205 enum debug_info_usage usage)
24206 {
24207 if (TREE_ASM_WRITTEN (type))
24208 {
24209 /* Fill in the bound of variable-length fields in late dwarf if
24210 still incomplete. */
24211 if (!early_dwarf && variably_modified_type_p (type, NULL))
24212 for (tree member = TYPE_FIELDS (type);
24213 member;
24214 member = DECL_CHAIN (member))
24215 fill_variable_array_bounds (TREE_TYPE (member));
24216 return;
24217 }
24218
24219 dw_die_ref type_die = lookup_type_die (type);
24220 dw_die_ref scope_die = 0;
24221 int nested = 0;
24222 int complete = (TYPE_SIZE (type)
24223 && (! TYPE_STUB_DECL (type)
24224 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24225 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24226 complete = complete && should_emit_struct_debug (type, usage);
24227
24228 if (type_die && ! complete)
24229 return;
24230
24231 if (TYPE_CONTEXT (type) != NULL_TREE
24232 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24233 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24234 nested = 1;
24235
24236 scope_die = scope_die_for (type, context_die);
24237
24238 /* Generate child dies for template paramaters. */
24239 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24240 schedule_generic_params_dies_gen (type);
24241
24242 if (! type_die || (nested && is_cu_die (scope_die)))
24243 /* First occurrence of type or toplevel definition of nested class. */
24244 {
24245 dw_die_ref old_die = type_die;
24246
24247 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24248 ? record_type_tag (type) : DW_TAG_union_type,
24249 scope_die, type);
24250 equate_type_number_to_die (type, type_die);
24251 if (old_die)
24252 add_AT_specification (type_die, old_die);
24253 else
24254 add_name_attribute (type_die, type_tag (type));
24255 }
24256 else
24257 remove_AT (type_die, DW_AT_declaration);
24258
24259 /* If this type has been completed, then give it a byte_size attribute and
24260 then give a list of members. */
24261 if (complete && !ns_decl)
24262 {
24263 /* Prevent infinite recursion in cases where the type of some member of
24264 this type is expressed in terms of this type itself. */
24265 TREE_ASM_WRITTEN (type) = 1;
24266 add_byte_size_attribute (type_die, type);
24267 add_alignment_attribute (type_die, type);
24268 if (TYPE_STUB_DECL (type) != NULL_TREE)
24269 {
24270 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24271 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24272 }
24273
24274 /* If the first reference to this type was as the return type of an
24275 inline function, then it may not have a parent. Fix this now. */
24276 if (type_die->die_parent == NULL)
24277 add_child_die (scope_die, type_die);
24278
24279 push_decl_scope (type);
24280 gen_member_die (type, type_die);
24281 pop_decl_scope ();
24282
24283 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24284 if (TYPE_ARTIFICIAL (type))
24285 add_AT_flag (type_die, DW_AT_artificial, 1);
24286
24287 /* GNU extension: Record what type our vtable lives in. */
24288 if (TYPE_VFIELD (type))
24289 {
24290 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24291
24292 gen_type_die (vtype, context_die);
24293 add_AT_die_ref (type_die, DW_AT_containing_type,
24294 lookup_type_die (vtype));
24295 }
24296 }
24297 else
24298 {
24299 add_AT_flag (type_die, DW_AT_declaration, 1);
24300
24301 /* We don't need to do this for function-local types. */
24302 if (TYPE_STUB_DECL (type)
24303 && ! decl_function_context (TYPE_STUB_DECL (type)))
24304 vec_safe_push (incomplete_types, type);
24305 }
24306
24307 if (get_AT (type_die, DW_AT_name))
24308 add_pubtype (type, type_die);
24309 }
24310
24311 /* Generate a DIE for a subroutine _type_. */
24312
24313 static void
24314 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24315 {
24316 tree return_type = TREE_TYPE (type);
24317 dw_die_ref subr_die
24318 = new_die (DW_TAG_subroutine_type,
24319 scope_die_for (type, context_die), type);
24320
24321 equate_type_number_to_die (type, subr_die);
24322 add_prototyped_attribute (subr_die, type);
24323 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24324 context_die);
24325 add_alignment_attribute (subr_die, type);
24326 gen_formal_types_die (type, subr_die);
24327
24328 if (get_AT (subr_die, DW_AT_name))
24329 add_pubtype (type, subr_die);
24330 if ((dwarf_version >= 5 || !dwarf_strict)
24331 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24332 add_AT_flag (subr_die, DW_AT_reference, 1);
24333 if ((dwarf_version >= 5 || !dwarf_strict)
24334 && lang_hooks.types.type_dwarf_attribute (type,
24335 DW_AT_rvalue_reference) != -1)
24336 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24337 }
24338
24339 /* Generate a DIE for a type definition. */
24340
24341 static void
24342 gen_typedef_die (tree decl, dw_die_ref context_die)
24343 {
24344 dw_die_ref type_die;
24345 tree type;
24346
24347 if (TREE_ASM_WRITTEN (decl))
24348 {
24349 if (DECL_ORIGINAL_TYPE (decl))
24350 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24351 return;
24352 }
24353
24354 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24355 checks in process_scope_var and modified_type_die), this should be called
24356 only for original types. */
24357 gcc_assert (decl_ultimate_origin (decl) == NULL
24358 || decl_ultimate_origin (decl) == decl);
24359
24360 TREE_ASM_WRITTEN (decl) = 1;
24361 type_die = new_die (DW_TAG_typedef, context_die, decl);
24362
24363 add_name_and_src_coords_attributes (type_die, decl);
24364 if (DECL_ORIGINAL_TYPE (decl))
24365 {
24366 type = DECL_ORIGINAL_TYPE (decl);
24367 if (type == error_mark_node)
24368 return;
24369
24370 gcc_assert (type != TREE_TYPE (decl));
24371 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24372 }
24373 else
24374 {
24375 type = TREE_TYPE (decl);
24376 if (type == error_mark_node)
24377 return;
24378
24379 if (is_naming_typedef_decl (TYPE_NAME (type)))
24380 {
24381 /* Here, we are in the case of decl being a typedef naming
24382 an anonymous type, e.g:
24383 typedef struct {...} foo;
24384 In that case TREE_TYPE (decl) is not a typedef variant
24385 type and TYPE_NAME of the anonymous type is set to the
24386 TYPE_DECL of the typedef. This construct is emitted by
24387 the C++ FE.
24388
24389 TYPE is the anonymous struct named by the typedef
24390 DECL. As we need the DW_AT_type attribute of the
24391 DW_TAG_typedef to point to the DIE of TYPE, let's
24392 generate that DIE right away. add_type_attribute
24393 called below will then pick (via lookup_type_die) that
24394 anonymous struct DIE. */
24395 if (!TREE_ASM_WRITTEN (type))
24396 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24397
24398 /* This is a GNU Extension. We are adding a
24399 DW_AT_linkage_name attribute to the DIE of the
24400 anonymous struct TYPE. The value of that attribute
24401 is the name of the typedef decl naming the anonymous
24402 struct. This greatly eases the work of consumers of
24403 this debug info. */
24404 add_linkage_name_raw (lookup_type_die (type), decl);
24405 }
24406 }
24407
24408 add_type_attribute (type_die, type, decl_quals (decl), false,
24409 context_die);
24410
24411 if (is_naming_typedef_decl (decl))
24412 /* We want that all subsequent calls to lookup_type_die with
24413 TYPE in argument yield the DW_TAG_typedef we have just
24414 created. */
24415 equate_type_number_to_die (type, type_die);
24416
24417 add_alignment_attribute (type_die, TREE_TYPE (decl));
24418
24419 add_accessibility_attribute (type_die, decl);
24420
24421 if (DECL_ABSTRACT_P (decl))
24422 equate_decl_number_to_die (decl, type_die);
24423
24424 if (get_AT (type_die, DW_AT_name))
24425 add_pubtype (decl, type_die);
24426 }
24427
24428 /* Generate a DIE for a struct, class, enum or union type. */
24429
24430 static void
24431 gen_tagged_type_die (tree type,
24432 dw_die_ref context_die,
24433 enum debug_info_usage usage)
24434 {
24435 int need_pop;
24436
24437 if (type == NULL_TREE
24438 || !is_tagged_type (type))
24439 return;
24440
24441 if (TREE_ASM_WRITTEN (type))
24442 need_pop = 0;
24443 /* If this is a nested type whose containing class hasn't been written
24444 out yet, writing it out will cover this one, too. This does not apply
24445 to instantiations of member class templates; they need to be added to
24446 the containing class as they are generated. FIXME: This hurts the
24447 idea of combining type decls from multiple TUs, since we can't predict
24448 what set of template instantiations we'll get. */
24449 else if (TYPE_CONTEXT (type)
24450 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24451 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24452 {
24453 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24454
24455 if (TREE_ASM_WRITTEN (type))
24456 return;
24457
24458 /* If that failed, attach ourselves to the stub. */
24459 push_decl_scope (TYPE_CONTEXT (type));
24460 context_die = lookup_type_die (TYPE_CONTEXT (type));
24461 need_pop = 1;
24462 }
24463 else if (TYPE_CONTEXT (type) != NULL_TREE
24464 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24465 {
24466 /* If this type is local to a function that hasn't been written
24467 out yet, use a NULL context for now; it will be fixed up in
24468 decls_for_scope. */
24469 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24470 /* A declaration DIE doesn't count; nested types need to go in the
24471 specification. */
24472 if (context_die && is_declaration_die (context_die))
24473 context_die = NULL;
24474 need_pop = 0;
24475 }
24476 else
24477 {
24478 context_die = declare_in_namespace (type, context_die);
24479 need_pop = 0;
24480 }
24481
24482 if (TREE_CODE (type) == ENUMERAL_TYPE)
24483 {
24484 /* This might have been written out by the call to
24485 declare_in_namespace. */
24486 if (!TREE_ASM_WRITTEN (type))
24487 gen_enumeration_type_die (type, context_die);
24488 }
24489 else
24490 gen_struct_or_union_type_die (type, context_die, usage);
24491
24492 if (need_pop)
24493 pop_decl_scope ();
24494
24495 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24496 it up if it is ever completed. gen_*_type_die will set it for us
24497 when appropriate. */
24498 }
24499
24500 /* Generate a type description DIE. */
24501
24502 static void
24503 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24504 enum debug_info_usage usage)
24505 {
24506 struct array_descr_info info;
24507
24508 if (type == NULL_TREE || type == error_mark_node)
24509 return;
24510
24511 if (flag_checking && type)
24512 verify_type (type);
24513
24514 if (TYPE_NAME (type) != NULL_TREE
24515 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24516 && is_redundant_typedef (TYPE_NAME (type))
24517 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24518 /* The DECL of this type is a typedef we don't want to emit debug
24519 info for but we want debug info for its underlying typedef.
24520 This can happen for e.g, the injected-class-name of a C++
24521 type. */
24522 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24523
24524 /* If TYPE is a typedef type variant, let's generate debug info
24525 for the parent typedef which TYPE is a type of. */
24526 if (typedef_variant_p (type))
24527 {
24528 if (TREE_ASM_WRITTEN (type))
24529 return;
24530
24531 tree name = TYPE_NAME (type);
24532 tree origin = decl_ultimate_origin (name);
24533 if (origin != NULL && origin != name)
24534 {
24535 gen_decl_die (origin, NULL, NULL, context_die);
24536 return;
24537 }
24538
24539 /* Prevent broken recursion; we can't hand off to the same type. */
24540 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24541
24542 /* Give typedefs the right scope. */
24543 context_die = scope_die_for (type, context_die);
24544
24545 TREE_ASM_WRITTEN (type) = 1;
24546
24547 gen_decl_die (name, NULL, NULL, context_die);
24548 return;
24549 }
24550
24551 /* If type is an anonymous tagged type named by a typedef, let's
24552 generate debug info for the typedef. */
24553 if (is_naming_typedef_decl (TYPE_NAME (type)))
24554 {
24555 /* Use the DIE of the containing namespace as the parent DIE of
24556 the type description DIE we want to generate. */
24557 if (DECL_CONTEXT (TYPE_NAME (type))
24558 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24559 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24560
24561 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24562 return;
24563 }
24564
24565 if (lang_hooks.types.get_debug_type)
24566 {
24567 tree debug_type = lang_hooks.types.get_debug_type (type);
24568
24569 if (debug_type != NULL_TREE && debug_type != type)
24570 {
24571 gen_type_die_with_usage (debug_type, context_die, usage);
24572 return;
24573 }
24574 }
24575
24576 /* We are going to output a DIE to represent the unqualified version
24577 of this type (i.e. without any const or volatile qualifiers) so
24578 get the main variant (i.e. the unqualified version) of this type
24579 now. (Vectors and arrays are special because the debugging info is in the
24580 cloned type itself. Similarly function/method types can contain extra
24581 ref-qualification). */
24582 if (TREE_CODE (type) == FUNCTION_TYPE
24583 || TREE_CODE (type) == METHOD_TYPE)
24584 {
24585 /* For function/method types, can't use type_main_variant here,
24586 because that can have different ref-qualifiers for C++,
24587 but try to canonicalize. */
24588 tree main = TYPE_MAIN_VARIANT (type);
24589 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24590 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24591 && check_base_type (t, main)
24592 && check_lang_type (t, type))
24593 {
24594 type = t;
24595 break;
24596 }
24597 }
24598 else if (TREE_CODE (type) != VECTOR_TYPE
24599 && TREE_CODE (type) != ARRAY_TYPE)
24600 type = type_main_variant (type);
24601
24602 /* If this is an array type with hidden descriptor, handle it first. */
24603 if (!TREE_ASM_WRITTEN (type)
24604 && lang_hooks.types.get_array_descr_info)
24605 {
24606 memset (&info, 0, sizeof (info));
24607 if (lang_hooks.types.get_array_descr_info (type, &info))
24608 {
24609 /* Fortran sometimes emits array types with no dimension. */
24610 gcc_assert (info.ndimensions >= 0
24611 && (info.ndimensions
24612 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24613 gen_descr_array_type_die (type, &info, context_die);
24614 TREE_ASM_WRITTEN (type) = 1;
24615 return;
24616 }
24617 }
24618
24619 if (TREE_ASM_WRITTEN (type))
24620 {
24621 /* Variable-length types may be incomplete even if
24622 TREE_ASM_WRITTEN. For such types, fall through to
24623 gen_array_type_die() and possibly fill in
24624 DW_AT_{upper,lower}_bound attributes. */
24625 if ((TREE_CODE (type) != ARRAY_TYPE
24626 && TREE_CODE (type) != RECORD_TYPE
24627 && TREE_CODE (type) != UNION_TYPE
24628 && TREE_CODE (type) != QUAL_UNION_TYPE)
24629 || !variably_modified_type_p (type, NULL))
24630 return;
24631 }
24632
24633 switch (TREE_CODE (type))
24634 {
24635 case ERROR_MARK:
24636 break;
24637
24638 case POINTER_TYPE:
24639 case REFERENCE_TYPE:
24640 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24641 ensures that the gen_type_die recursion will terminate even if the
24642 type is recursive. Recursive types are possible in Ada. */
24643 /* ??? We could perhaps do this for all types before the switch
24644 statement. */
24645 TREE_ASM_WRITTEN (type) = 1;
24646
24647 /* For these types, all that is required is that we output a DIE (or a
24648 set of DIEs) to represent the "basis" type. */
24649 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24650 DINFO_USAGE_IND_USE);
24651 break;
24652
24653 case OFFSET_TYPE:
24654 /* This code is used for C++ pointer-to-data-member types.
24655 Output a description of the relevant class type. */
24656 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24657 DINFO_USAGE_IND_USE);
24658
24659 /* Output a description of the type of the object pointed to. */
24660 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24661 DINFO_USAGE_IND_USE);
24662
24663 /* Now output a DIE to represent this pointer-to-data-member type
24664 itself. */
24665 gen_ptr_to_mbr_type_die (type, context_die);
24666 break;
24667
24668 case FUNCTION_TYPE:
24669 /* Force out return type (in case it wasn't forced out already). */
24670 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24671 DINFO_USAGE_DIR_USE);
24672 gen_subroutine_type_die (type, context_die);
24673 break;
24674
24675 case METHOD_TYPE:
24676 /* Force out return type (in case it wasn't forced out already). */
24677 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24678 DINFO_USAGE_DIR_USE);
24679 gen_subroutine_type_die (type, context_die);
24680 break;
24681
24682 case ARRAY_TYPE:
24683 case VECTOR_TYPE:
24684 gen_array_type_die (type, context_die);
24685 break;
24686
24687 case ENUMERAL_TYPE:
24688 case RECORD_TYPE:
24689 case UNION_TYPE:
24690 case QUAL_UNION_TYPE:
24691 gen_tagged_type_die (type, context_die, usage);
24692 return;
24693
24694 case VOID_TYPE:
24695 case INTEGER_TYPE:
24696 case REAL_TYPE:
24697 case FIXED_POINT_TYPE:
24698 case COMPLEX_TYPE:
24699 case BOOLEAN_TYPE:
24700 case POINTER_BOUNDS_TYPE:
24701 /* No DIEs needed for fundamental types. */
24702 break;
24703
24704 case NULLPTR_TYPE:
24705 case LANG_TYPE:
24706 /* Just use DW_TAG_unspecified_type. */
24707 {
24708 dw_die_ref type_die = lookup_type_die (type);
24709 if (type_die == NULL)
24710 {
24711 tree name = TYPE_IDENTIFIER (type);
24712 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24713 type);
24714 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24715 equate_type_number_to_die (type, type_die);
24716 }
24717 }
24718 break;
24719
24720 default:
24721 if (is_cxx_auto (type))
24722 {
24723 tree name = TYPE_IDENTIFIER (type);
24724 dw_die_ref *die = (name == get_identifier ("auto")
24725 ? &auto_die : &decltype_auto_die);
24726 if (!*die)
24727 {
24728 *die = new_die (DW_TAG_unspecified_type,
24729 comp_unit_die (), NULL_TREE);
24730 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24731 }
24732 equate_type_number_to_die (type, *die);
24733 break;
24734 }
24735 gcc_unreachable ();
24736 }
24737
24738 TREE_ASM_WRITTEN (type) = 1;
24739 }
24740
24741 static void
24742 gen_type_die (tree type, dw_die_ref context_die)
24743 {
24744 if (type != error_mark_node)
24745 {
24746 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24747 if (flag_checking)
24748 {
24749 dw_die_ref die = lookup_type_die (type);
24750 if (die)
24751 check_die (die);
24752 }
24753 }
24754 }
24755
24756 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24757 things which are local to the given block. */
24758
24759 static void
24760 gen_block_die (tree stmt, dw_die_ref context_die)
24761 {
24762 int must_output_die = 0;
24763 bool inlined_func;
24764
24765 /* Ignore blocks that are NULL. */
24766 if (stmt == NULL_TREE)
24767 return;
24768
24769 inlined_func = inlined_function_outer_scope_p (stmt);
24770
24771 /* If the block is one fragment of a non-contiguous block, do not
24772 process the variables, since they will have been done by the
24773 origin block. Do process subblocks. */
24774 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24775 {
24776 tree sub;
24777
24778 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24779 gen_block_die (sub, context_die);
24780
24781 return;
24782 }
24783
24784 /* Determine if we need to output any Dwarf DIEs at all to represent this
24785 block. */
24786 if (inlined_func)
24787 /* The outer scopes for inlinings *must* always be represented. We
24788 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24789 must_output_die = 1;
24790 else
24791 {
24792 /* Determine if this block directly contains any "significant"
24793 local declarations which we will need to output DIEs for. */
24794 if (debug_info_level > DINFO_LEVEL_TERSE)
24795 /* We are not in terse mode so *any* local declaration counts
24796 as being a "significant" one. */
24797 must_output_die = ((BLOCK_VARS (stmt) != NULL
24798 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24799 && (TREE_USED (stmt)
24800 || TREE_ASM_WRITTEN (stmt)
24801 || BLOCK_ABSTRACT (stmt)));
24802 else if ((TREE_USED (stmt)
24803 || TREE_ASM_WRITTEN (stmt)
24804 || BLOCK_ABSTRACT (stmt))
24805 && !dwarf2out_ignore_block (stmt))
24806 must_output_die = 1;
24807 }
24808
24809 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24810 DIE for any block which contains no significant local declarations at
24811 all. Rather, in such cases we just call `decls_for_scope' so that any
24812 needed Dwarf info for any sub-blocks will get properly generated. Note
24813 that in terse mode, our definition of what constitutes a "significant"
24814 local declaration gets restricted to include only inlined function
24815 instances and local (nested) function definitions. */
24816 if (must_output_die)
24817 {
24818 if (inlined_func)
24819 {
24820 /* If STMT block is abstract, that means we have been called
24821 indirectly from dwarf2out_abstract_function.
24822 That function rightfully marks the descendent blocks (of
24823 the abstract function it is dealing with) as being abstract,
24824 precisely to prevent us from emitting any
24825 DW_TAG_inlined_subroutine DIE as a descendent
24826 of an abstract function instance. So in that case, we should
24827 not call gen_inlined_subroutine_die.
24828
24829 Later though, when cgraph asks dwarf2out to emit info
24830 for the concrete instance of the function decl into which
24831 the concrete instance of STMT got inlined, the later will lead
24832 to the generation of a DW_TAG_inlined_subroutine DIE. */
24833 if (! BLOCK_ABSTRACT (stmt))
24834 gen_inlined_subroutine_die (stmt, context_die);
24835 }
24836 else
24837 gen_lexical_block_die (stmt, context_die);
24838 }
24839 else
24840 decls_for_scope (stmt, context_die);
24841 }
24842
24843 /* Process variable DECL (or variable with origin ORIGIN) within
24844 block STMT and add it to CONTEXT_DIE. */
24845 static void
24846 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24847 {
24848 dw_die_ref die;
24849 tree decl_or_origin = decl ? decl : origin;
24850
24851 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24852 die = lookup_decl_die (decl_or_origin);
24853 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24854 {
24855 if (TYPE_DECL_IS_STUB (decl_or_origin))
24856 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24857 else
24858 die = lookup_decl_die (decl_or_origin);
24859 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24860 if (! die && ! early_dwarf)
24861 return;
24862 }
24863 else
24864 die = NULL;
24865
24866 /* Avoid creating DIEs for local typedefs and concrete static variables that
24867 will only be pruned later. */
24868 if ((origin || decl_ultimate_origin (decl))
24869 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24870 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24871 {
24872 origin = decl_ultimate_origin (decl_or_origin);
24873 if (decl && VAR_P (decl) && die != NULL)
24874 {
24875 die = lookup_decl_die (origin);
24876 if (die != NULL)
24877 equate_decl_number_to_die (decl, die);
24878 }
24879 return;
24880 }
24881
24882 if (die != NULL && die->die_parent == NULL)
24883 add_child_die (context_die, die);
24884 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24885 {
24886 if (early_dwarf)
24887 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24888 stmt, context_die);
24889 }
24890 else
24891 {
24892 if (decl && DECL_P (decl))
24893 {
24894 die = lookup_decl_die (decl);
24895
24896 /* Early created DIEs do not have a parent as the decls refer
24897 to the function as DECL_CONTEXT rather than the BLOCK. */
24898 if (die && die->die_parent == NULL)
24899 {
24900 gcc_assert (in_lto_p);
24901 add_child_die (context_die, die);
24902 }
24903 }
24904
24905 gen_decl_die (decl, origin, NULL, context_die);
24906 }
24907 }
24908
24909 /* Generate all of the decls declared within a given scope and (recursively)
24910 all of its sub-blocks. */
24911
24912 static void
24913 decls_for_scope (tree stmt, dw_die_ref context_die)
24914 {
24915 tree decl;
24916 unsigned int i;
24917 tree subblocks;
24918
24919 /* Ignore NULL blocks. */
24920 if (stmt == NULL_TREE)
24921 return;
24922
24923 /* Output the DIEs to represent all of the data objects and typedefs
24924 declared directly within this block but not within any nested
24925 sub-blocks. Also, nested function and tag DIEs have been
24926 generated with a parent of NULL; fix that up now. We don't
24927 have to do this if we're at -g1. */
24928 if (debug_info_level > DINFO_LEVEL_TERSE)
24929 {
24930 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24931 process_scope_var (stmt, decl, NULL_TREE, context_die);
24932 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24933 origin - avoid doing this twice as we have no good way to see
24934 if we've done it once already. */
24935 if (! early_dwarf)
24936 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24937 {
24938 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24939 if (decl == current_function_decl)
24940 /* Ignore declarations of the current function, while they
24941 are declarations, gen_subprogram_die would treat them
24942 as definitions again, because they are equal to
24943 current_function_decl and endlessly recurse. */;
24944 else if (TREE_CODE (decl) == FUNCTION_DECL)
24945 process_scope_var (stmt, decl, NULL_TREE, context_die);
24946 else
24947 process_scope_var (stmt, NULL_TREE, decl, context_die);
24948 }
24949 }
24950
24951 /* Even if we're at -g1, we need to process the subblocks in order to get
24952 inlined call information. */
24953
24954 /* Output the DIEs to represent all sub-blocks (and the items declared
24955 therein) of this block. */
24956 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24957 subblocks != NULL;
24958 subblocks = BLOCK_CHAIN (subblocks))
24959 gen_block_die (subblocks, context_die);
24960 }
24961
24962 /* Is this a typedef we can avoid emitting? */
24963
24964 bool
24965 is_redundant_typedef (const_tree decl)
24966 {
24967 if (TYPE_DECL_IS_STUB (decl))
24968 return true;
24969
24970 if (DECL_ARTIFICIAL (decl)
24971 && DECL_CONTEXT (decl)
24972 && is_tagged_type (DECL_CONTEXT (decl))
24973 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24974 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24975 /* Also ignore the artificial member typedef for the class name. */
24976 return true;
24977
24978 return false;
24979 }
24980
24981 /* Return TRUE if TYPE is a typedef that names a type for linkage
24982 purposes. This kind of typedefs is produced by the C++ FE for
24983 constructs like:
24984
24985 typedef struct {...} foo;
24986
24987 In that case, there is no typedef variant type produced for foo.
24988 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24989 struct type. */
24990
24991 static bool
24992 is_naming_typedef_decl (const_tree decl)
24993 {
24994 if (decl == NULL_TREE
24995 || TREE_CODE (decl) != TYPE_DECL
24996 || DECL_NAMELESS (decl)
24997 || !is_tagged_type (TREE_TYPE (decl))
24998 || DECL_IS_BUILTIN (decl)
24999 || is_redundant_typedef (decl)
25000 /* It looks like Ada produces TYPE_DECLs that are very similar
25001 to C++ naming typedefs but that have different
25002 semantics. Let's be specific to c++ for now. */
25003 || !is_cxx (decl))
25004 return FALSE;
25005
25006 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25007 && TYPE_NAME (TREE_TYPE (decl)) == decl
25008 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25009 != TYPE_NAME (TREE_TYPE (decl))));
25010 }
25011
25012 /* Looks up the DIE for a context. */
25013
25014 static inline dw_die_ref
25015 lookup_context_die (tree context)
25016 {
25017 if (context)
25018 {
25019 /* Find die that represents this context. */
25020 if (TYPE_P (context))
25021 {
25022 context = TYPE_MAIN_VARIANT (context);
25023 dw_die_ref ctx = lookup_type_die (context);
25024 if (!ctx)
25025 return NULL;
25026 return strip_naming_typedef (context, ctx);
25027 }
25028 else
25029 return lookup_decl_die (context);
25030 }
25031 return comp_unit_die ();
25032 }
25033
25034 /* Returns the DIE for a context. */
25035
25036 static inline dw_die_ref
25037 get_context_die (tree context)
25038 {
25039 if (context)
25040 {
25041 /* Find die that represents this context. */
25042 if (TYPE_P (context))
25043 {
25044 context = TYPE_MAIN_VARIANT (context);
25045 return strip_naming_typedef (context, force_type_die (context));
25046 }
25047 else
25048 return force_decl_die (context);
25049 }
25050 return comp_unit_die ();
25051 }
25052
25053 /* Returns the DIE for decl. A DIE will always be returned. */
25054
25055 static dw_die_ref
25056 force_decl_die (tree decl)
25057 {
25058 dw_die_ref decl_die;
25059 unsigned saved_external_flag;
25060 tree save_fn = NULL_TREE;
25061 decl_die = lookup_decl_die (decl);
25062 if (!decl_die)
25063 {
25064 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25065
25066 decl_die = lookup_decl_die (decl);
25067 if (decl_die)
25068 return decl_die;
25069
25070 switch (TREE_CODE (decl))
25071 {
25072 case FUNCTION_DECL:
25073 /* Clear current_function_decl, so that gen_subprogram_die thinks
25074 that this is a declaration. At this point, we just want to force
25075 declaration die. */
25076 save_fn = current_function_decl;
25077 current_function_decl = NULL_TREE;
25078 gen_subprogram_die (decl, context_die);
25079 current_function_decl = save_fn;
25080 break;
25081
25082 case VAR_DECL:
25083 /* Set external flag to force declaration die. Restore it after
25084 gen_decl_die() call. */
25085 saved_external_flag = DECL_EXTERNAL (decl);
25086 DECL_EXTERNAL (decl) = 1;
25087 gen_decl_die (decl, NULL, NULL, context_die);
25088 DECL_EXTERNAL (decl) = saved_external_flag;
25089 break;
25090
25091 case NAMESPACE_DECL:
25092 if (dwarf_version >= 3 || !dwarf_strict)
25093 dwarf2out_decl (decl);
25094 else
25095 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25096 decl_die = comp_unit_die ();
25097 break;
25098
25099 case TRANSLATION_UNIT_DECL:
25100 decl_die = comp_unit_die ();
25101 break;
25102
25103 default:
25104 gcc_unreachable ();
25105 }
25106
25107 /* We should be able to find the DIE now. */
25108 if (!decl_die)
25109 decl_die = lookup_decl_die (decl);
25110 gcc_assert (decl_die);
25111 }
25112
25113 return decl_die;
25114 }
25115
25116 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25117 always returned. */
25118
25119 static dw_die_ref
25120 force_type_die (tree type)
25121 {
25122 dw_die_ref type_die;
25123
25124 type_die = lookup_type_die (type);
25125 if (!type_die)
25126 {
25127 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25128
25129 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25130 false, context_die);
25131 gcc_assert (type_die);
25132 }
25133 return type_die;
25134 }
25135
25136 /* Force out any required namespaces to be able to output DECL,
25137 and return the new context_die for it, if it's changed. */
25138
25139 static dw_die_ref
25140 setup_namespace_context (tree thing, dw_die_ref context_die)
25141 {
25142 tree context = (DECL_P (thing)
25143 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25144 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25145 /* Force out the namespace. */
25146 context_die = force_decl_die (context);
25147
25148 return context_die;
25149 }
25150
25151 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25152 type) within its namespace, if appropriate.
25153
25154 For compatibility with older debuggers, namespace DIEs only contain
25155 declarations; all definitions are emitted at CU scope, with
25156 DW_AT_specification pointing to the declaration (like with class
25157 members). */
25158
25159 static dw_die_ref
25160 declare_in_namespace (tree thing, dw_die_ref context_die)
25161 {
25162 dw_die_ref ns_context;
25163
25164 if (debug_info_level <= DINFO_LEVEL_TERSE)
25165 return context_die;
25166
25167 /* External declarations in the local scope only need to be emitted
25168 once, not once in the namespace and once in the scope.
25169
25170 This avoids declaring the `extern' below in the
25171 namespace DIE as well as in the innermost scope:
25172
25173 namespace S
25174 {
25175 int i=5;
25176 int foo()
25177 {
25178 int i=8;
25179 extern int i;
25180 return i;
25181 }
25182 }
25183 */
25184 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25185 return context_die;
25186
25187 /* If this decl is from an inlined function, then don't try to emit it in its
25188 namespace, as we will get confused. It would have already been emitted
25189 when the abstract instance of the inline function was emitted anyways. */
25190 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25191 return context_die;
25192
25193 ns_context = setup_namespace_context (thing, context_die);
25194
25195 if (ns_context != context_die)
25196 {
25197 if (is_fortran ())
25198 return ns_context;
25199 if (DECL_P (thing))
25200 gen_decl_die (thing, NULL, NULL, ns_context);
25201 else
25202 gen_type_die (thing, ns_context);
25203 }
25204 return context_die;
25205 }
25206
25207 /* Generate a DIE for a namespace or namespace alias. */
25208
25209 static void
25210 gen_namespace_die (tree decl, dw_die_ref context_die)
25211 {
25212 dw_die_ref namespace_die;
25213
25214 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25215 they are an alias of. */
25216 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25217 {
25218 /* Output a real namespace or module. */
25219 context_die = setup_namespace_context (decl, comp_unit_die ());
25220 namespace_die = new_die (is_fortran ()
25221 ? DW_TAG_module : DW_TAG_namespace,
25222 context_die, decl);
25223 /* For Fortran modules defined in different CU don't add src coords. */
25224 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25225 {
25226 const char *name = dwarf2_name (decl, 0);
25227 if (name)
25228 add_name_attribute (namespace_die, name);
25229 }
25230 else
25231 add_name_and_src_coords_attributes (namespace_die, decl);
25232 if (DECL_EXTERNAL (decl))
25233 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25234 equate_decl_number_to_die (decl, namespace_die);
25235 }
25236 else
25237 {
25238 /* Output a namespace alias. */
25239
25240 /* Force out the namespace we are an alias of, if necessary. */
25241 dw_die_ref origin_die
25242 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25243
25244 if (DECL_FILE_SCOPE_P (decl)
25245 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25246 context_die = setup_namespace_context (decl, comp_unit_die ());
25247 /* Now create the namespace alias DIE. */
25248 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25249 add_name_and_src_coords_attributes (namespace_die, decl);
25250 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25251 equate_decl_number_to_die (decl, namespace_die);
25252 }
25253 if ((dwarf_version >= 5 || !dwarf_strict)
25254 && lang_hooks.decls.decl_dwarf_attribute (decl,
25255 DW_AT_export_symbols) == 1)
25256 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25257
25258 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25259 if (want_pubnames ())
25260 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25261 }
25262
25263 /* Generate Dwarf debug information for a decl described by DECL.
25264 The return value is currently only meaningful for PARM_DECLs,
25265 for all other decls it returns NULL.
25266
25267 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25268 It can be NULL otherwise. */
25269
25270 static dw_die_ref
25271 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25272 dw_die_ref context_die)
25273 {
25274 tree decl_or_origin = decl ? decl : origin;
25275 tree class_origin = NULL, ultimate_origin;
25276
25277 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25278 return NULL;
25279
25280 /* Ignore pointer bounds decls. */
25281 if (DECL_P (decl_or_origin)
25282 && TREE_TYPE (decl_or_origin)
25283 && POINTER_BOUNDS_P (decl_or_origin))
25284 return NULL;
25285
25286 switch (TREE_CODE (decl_or_origin))
25287 {
25288 case ERROR_MARK:
25289 break;
25290
25291 case CONST_DECL:
25292 if (!is_fortran () && !is_ada ())
25293 {
25294 /* The individual enumerators of an enum type get output when we output
25295 the Dwarf representation of the relevant enum type itself. */
25296 break;
25297 }
25298
25299 /* Emit its type. */
25300 gen_type_die (TREE_TYPE (decl), context_die);
25301
25302 /* And its containing namespace. */
25303 context_die = declare_in_namespace (decl, context_die);
25304
25305 gen_const_die (decl, context_die);
25306 break;
25307
25308 case FUNCTION_DECL:
25309 #if 0
25310 /* FIXME */
25311 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25312 on local redeclarations of global functions. That seems broken. */
25313 if (current_function_decl != decl)
25314 /* This is only a declaration. */;
25315 #endif
25316
25317 /* We should have abstract copies already and should not generate
25318 stray type DIEs in late LTO dumping. */
25319 if (! early_dwarf)
25320 ;
25321
25322 /* If we're emitting a clone, emit info for the abstract instance. */
25323 else if (origin || DECL_ORIGIN (decl) != decl)
25324 dwarf2out_abstract_function (origin
25325 ? DECL_ORIGIN (origin)
25326 : DECL_ABSTRACT_ORIGIN (decl));
25327
25328 /* If we're emitting a possibly inlined function emit it as
25329 abstract instance. */
25330 else if (cgraph_function_possibly_inlined_p (decl)
25331 && ! DECL_ABSTRACT_P (decl)
25332 && ! class_or_namespace_scope_p (context_die)
25333 /* dwarf2out_abstract_function won't emit a die if this is just
25334 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25335 that case, because that works only if we have a die. */
25336 && DECL_INITIAL (decl) != NULL_TREE)
25337 dwarf2out_abstract_function (decl);
25338
25339 /* Otherwise we're emitting the primary DIE for this decl. */
25340 else if (debug_info_level > DINFO_LEVEL_TERSE)
25341 {
25342 /* Before we describe the FUNCTION_DECL itself, make sure that we
25343 have its containing type. */
25344 if (!origin)
25345 origin = decl_class_context (decl);
25346 if (origin != NULL_TREE)
25347 gen_type_die (origin, context_die);
25348
25349 /* And its return type. */
25350 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25351
25352 /* And its virtual context. */
25353 if (DECL_VINDEX (decl) != NULL_TREE)
25354 gen_type_die (DECL_CONTEXT (decl), context_die);
25355
25356 /* Make sure we have a member DIE for decl. */
25357 if (origin != NULL_TREE)
25358 gen_type_die_for_member (origin, decl, context_die);
25359
25360 /* And its containing namespace. */
25361 context_die = declare_in_namespace (decl, context_die);
25362 }
25363
25364 /* Now output a DIE to represent the function itself. */
25365 if (decl)
25366 gen_subprogram_die (decl, context_die);
25367 break;
25368
25369 case TYPE_DECL:
25370 /* If we are in terse mode, don't generate any DIEs to represent any
25371 actual typedefs. */
25372 if (debug_info_level <= DINFO_LEVEL_TERSE)
25373 break;
25374
25375 /* In the special case of a TYPE_DECL node representing the declaration
25376 of some type tag, if the given TYPE_DECL is marked as having been
25377 instantiated from some other (original) TYPE_DECL node (e.g. one which
25378 was generated within the original definition of an inline function) we
25379 used to generate a special (abbreviated) DW_TAG_structure_type,
25380 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25381 should be actually referencing those DIEs, as variable DIEs with that
25382 type would be emitted already in the abstract origin, so it was always
25383 removed during unused type prunning. Don't add anything in this
25384 case. */
25385 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25386 break;
25387
25388 if (is_redundant_typedef (decl))
25389 gen_type_die (TREE_TYPE (decl), context_die);
25390 else
25391 /* Output a DIE to represent the typedef itself. */
25392 gen_typedef_die (decl, context_die);
25393 break;
25394
25395 case LABEL_DECL:
25396 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25397 gen_label_die (decl, context_die);
25398 break;
25399
25400 case VAR_DECL:
25401 case RESULT_DECL:
25402 /* If we are in terse mode, don't generate any DIEs to represent any
25403 variable declarations or definitions. */
25404 if (debug_info_level <= DINFO_LEVEL_TERSE)
25405 break;
25406
25407 /* Avoid generating stray type DIEs during late dwarf dumping.
25408 All types have been dumped early. */
25409 if (early_dwarf
25410 /* ??? But in LTRANS we cannot annotate early created variably
25411 modified type DIEs without copying them and adjusting all
25412 references to them. Dump them again as happens for inlining
25413 which copies both the decl and the types. */
25414 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25415 in VLA bound information for example. */
25416 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25417 current_function_decl)))
25418 {
25419 /* Output any DIEs that are needed to specify the type of this data
25420 object. */
25421 if (decl_by_reference_p (decl_or_origin))
25422 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25423 else
25424 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25425 }
25426
25427 if (early_dwarf)
25428 {
25429 /* And its containing type. */
25430 class_origin = decl_class_context (decl_or_origin);
25431 if (class_origin != NULL_TREE)
25432 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25433
25434 /* And its containing namespace. */
25435 context_die = declare_in_namespace (decl_or_origin, context_die);
25436 }
25437
25438 /* Now output the DIE to represent the data object itself. This gets
25439 complicated because of the possibility that the VAR_DECL really
25440 represents an inlined instance of a formal parameter for an inline
25441 function. */
25442 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25443 if (ultimate_origin != NULL_TREE
25444 && TREE_CODE (ultimate_origin) == PARM_DECL)
25445 gen_formal_parameter_die (decl, origin,
25446 true /* Emit name attribute. */,
25447 context_die);
25448 else
25449 gen_variable_die (decl, origin, context_die);
25450 break;
25451
25452 case FIELD_DECL:
25453 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25454 /* Ignore the nameless fields that are used to skip bits but handle C++
25455 anonymous unions and structs. */
25456 if (DECL_NAME (decl) != NULL_TREE
25457 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25458 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25459 {
25460 gen_type_die (member_declared_type (decl), context_die);
25461 gen_field_die (decl, ctx, context_die);
25462 }
25463 break;
25464
25465 case PARM_DECL:
25466 /* Avoid generating stray type DIEs during late dwarf dumping.
25467 All types have been dumped early. */
25468 if (early_dwarf
25469 /* ??? But in LTRANS we cannot annotate early created variably
25470 modified type DIEs without copying them and adjusting all
25471 references to them. Dump them again as happens for inlining
25472 which copies both the decl and the types. */
25473 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25474 in VLA bound information for example. */
25475 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25476 current_function_decl)))
25477 {
25478 if (DECL_BY_REFERENCE (decl_or_origin))
25479 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25480 else
25481 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25482 }
25483 return gen_formal_parameter_die (decl, origin,
25484 true /* Emit name attribute. */,
25485 context_die);
25486
25487 case NAMESPACE_DECL:
25488 if (dwarf_version >= 3 || !dwarf_strict)
25489 gen_namespace_die (decl, context_die);
25490 break;
25491
25492 case IMPORTED_DECL:
25493 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25494 DECL_CONTEXT (decl), context_die);
25495 break;
25496
25497 case NAMELIST_DECL:
25498 gen_namelist_decl (DECL_NAME (decl), context_die,
25499 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25500 break;
25501
25502 default:
25503 /* Probably some frontend-internal decl. Assume we don't care. */
25504 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25505 break;
25506 }
25507
25508 return NULL;
25509 }
25510 \f
25511 /* Output initial debug information for global DECL. Called at the
25512 end of the parsing process.
25513
25514 This is the initial debug generation process. As such, the DIEs
25515 generated may be incomplete. A later debug generation pass
25516 (dwarf2out_late_global_decl) will augment the information generated
25517 in this pass (e.g., with complete location info). */
25518
25519 static void
25520 dwarf2out_early_global_decl (tree decl)
25521 {
25522 set_early_dwarf s;
25523
25524 /* gen_decl_die() will set DECL_ABSTRACT because
25525 cgraph_function_possibly_inlined_p() returns true. This is in
25526 turn will cause DW_AT_inline attributes to be set.
25527
25528 This happens because at early dwarf generation, there is no
25529 cgraph information, causing cgraph_function_possibly_inlined_p()
25530 to return true. Trick cgraph_function_possibly_inlined_p()
25531 while we generate dwarf early. */
25532 bool save = symtab->global_info_ready;
25533 symtab->global_info_ready = true;
25534
25535 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25536 other DECLs and they can point to template types or other things
25537 that dwarf2out can't handle when done via dwarf2out_decl. */
25538 if (TREE_CODE (decl) != TYPE_DECL
25539 && TREE_CODE (decl) != PARM_DECL)
25540 {
25541 if (TREE_CODE (decl) == FUNCTION_DECL)
25542 {
25543 tree save_fndecl = current_function_decl;
25544
25545 /* For nested functions, make sure we have DIEs for the parents first
25546 so that all nested DIEs are generated at the proper scope in the
25547 first shot. */
25548 tree context = decl_function_context (decl);
25549 if (context != NULL)
25550 {
25551 dw_die_ref context_die = lookup_decl_die (context);
25552 current_function_decl = context;
25553
25554 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25555 enough so that it lands in its own context. This avoids type
25556 pruning issues later on. */
25557 if (context_die == NULL || is_declaration_die (context_die))
25558 dwarf2out_decl (context);
25559 }
25560
25561 /* Emit an abstract origin of a function first. This happens
25562 with C++ constructor clones for example and makes
25563 dwarf2out_abstract_function happy which requires the early
25564 DIE of the abstract instance to be present. */
25565 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25566 dw_die_ref origin_die;
25567 if (origin != NULL
25568 /* Do not emit the DIE multiple times but make sure to
25569 process it fully here in case we just saw a declaration. */
25570 && ((origin_die = lookup_decl_die (origin)) == NULL
25571 || is_declaration_die (origin_die)))
25572 {
25573 current_function_decl = origin;
25574 dwarf2out_decl (origin);
25575 }
25576
25577 /* Emit the DIE for decl but avoid doing that multiple times. */
25578 dw_die_ref old_die;
25579 if ((old_die = lookup_decl_die (decl)) == NULL
25580 || is_declaration_die (old_die))
25581 {
25582 current_function_decl = decl;
25583 dwarf2out_decl (decl);
25584 }
25585
25586 current_function_decl = save_fndecl;
25587 }
25588 else
25589 dwarf2out_decl (decl);
25590 }
25591 symtab->global_info_ready = save;
25592 }
25593
25594 /* Output debug information for global decl DECL. Called from
25595 toplev.c after compilation proper has finished. */
25596
25597 static void
25598 dwarf2out_late_global_decl (tree decl)
25599 {
25600 /* Fill-in any location information we were unable to determine
25601 on the first pass. */
25602 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25603 {
25604 dw_die_ref die = lookup_decl_die (decl);
25605
25606 /* We may have to generate early debug late for LTO in case debug
25607 was not enabled at compile-time or the target doesn't support
25608 the LTO early debug scheme. */
25609 if (! die && in_lto_p)
25610 {
25611 dwarf2out_decl (decl);
25612 die = lookup_decl_die (decl);
25613 }
25614
25615 if (die)
25616 {
25617 /* We get called via the symtab code invoking late_global_decl
25618 for symbols that are optimized out. Do not add locations
25619 for those, except if they have a DECL_VALUE_EXPR, in which case
25620 they are relevant for debuggers. */
25621 varpool_node *node = varpool_node::get (decl);
25622 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25623 tree_add_const_value_attribute_for_decl (die, decl);
25624 else
25625 add_location_or_const_value_attribute (die, decl, false);
25626 }
25627 }
25628 }
25629
25630 /* Output debug information for type decl DECL. Called from toplev.c
25631 and from language front ends (to record built-in types). */
25632 static void
25633 dwarf2out_type_decl (tree decl, int local)
25634 {
25635 if (!local)
25636 {
25637 set_early_dwarf s;
25638 dwarf2out_decl (decl);
25639 }
25640 }
25641
25642 /* Output debug information for imported module or decl DECL.
25643 NAME is non-NULL name in the lexical block if the decl has been renamed.
25644 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25645 that DECL belongs to.
25646 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25647 static void
25648 dwarf2out_imported_module_or_decl_1 (tree decl,
25649 tree name,
25650 tree lexical_block,
25651 dw_die_ref lexical_block_die)
25652 {
25653 expanded_location xloc;
25654 dw_die_ref imported_die = NULL;
25655 dw_die_ref at_import_die;
25656
25657 if (TREE_CODE (decl) == IMPORTED_DECL)
25658 {
25659 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25660 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25661 gcc_assert (decl);
25662 }
25663 else
25664 xloc = expand_location (input_location);
25665
25666 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25667 {
25668 at_import_die = force_type_die (TREE_TYPE (decl));
25669 /* For namespace N { typedef void T; } using N::T; base_type_die
25670 returns NULL, but DW_TAG_imported_declaration requires
25671 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25672 if (!at_import_die)
25673 {
25674 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25675 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25676 at_import_die = lookup_type_die (TREE_TYPE (decl));
25677 gcc_assert (at_import_die);
25678 }
25679 }
25680 else
25681 {
25682 at_import_die = lookup_decl_die (decl);
25683 if (!at_import_die)
25684 {
25685 /* If we're trying to avoid duplicate debug info, we may not have
25686 emitted the member decl for this field. Emit it now. */
25687 if (TREE_CODE (decl) == FIELD_DECL)
25688 {
25689 tree type = DECL_CONTEXT (decl);
25690
25691 if (TYPE_CONTEXT (type)
25692 && TYPE_P (TYPE_CONTEXT (type))
25693 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25694 DINFO_USAGE_DIR_USE))
25695 return;
25696 gen_type_die_for_member (type, decl,
25697 get_context_die (TYPE_CONTEXT (type)));
25698 }
25699 if (TREE_CODE (decl) == NAMELIST_DECL)
25700 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25701 get_context_die (DECL_CONTEXT (decl)),
25702 NULL_TREE);
25703 else
25704 at_import_die = force_decl_die (decl);
25705 }
25706 }
25707
25708 if (TREE_CODE (decl) == NAMESPACE_DECL)
25709 {
25710 if (dwarf_version >= 3 || !dwarf_strict)
25711 imported_die = new_die (DW_TAG_imported_module,
25712 lexical_block_die,
25713 lexical_block);
25714 else
25715 return;
25716 }
25717 else
25718 imported_die = new_die (DW_TAG_imported_declaration,
25719 lexical_block_die,
25720 lexical_block);
25721
25722 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25723 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25724 if (debug_column_info && xloc.column)
25725 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25726 if (name)
25727 add_AT_string (imported_die, DW_AT_name,
25728 IDENTIFIER_POINTER (name));
25729 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25730 }
25731
25732 /* Output debug information for imported module or decl DECL.
25733 NAME is non-NULL name in context if the decl has been renamed.
25734 CHILD is true if decl is one of the renamed decls as part of
25735 importing whole module.
25736 IMPLICIT is set if this hook is called for an implicit import
25737 such as inline namespace. */
25738
25739 static void
25740 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25741 bool child, bool implicit)
25742 {
25743 /* dw_die_ref at_import_die; */
25744 dw_die_ref scope_die;
25745
25746 if (debug_info_level <= DINFO_LEVEL_TERSE)
25747 return;
25748
25749 gcc_assert (decl);
25750
25751 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25752 should be enough, for DWARF4 and older even if we emit as extension
25753 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25754 for the benefit of consumers unaware of DW_AT_export_symbols. */
25755 if (implicit
25756 && dwarf_version >= 5
25757 && lang_hooks.decls.decl_dwarf_attribute (decl,
25758 DW_AT_export_symbols) == 1)
25759 return;
25760
25761 set_early_dwarf s;
25762
25763 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25764 We need decl DIE for reference and scope die. First, get DIE for the decl
25765 itself. */
25766
25767 /* Get the scope die for decl context. Use comp_unit_die for global module
25768 or decl. If die is not found for non globals, force new die. */
25769 if (context
25770 && TYPE_P (context)
25771 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25772 return;
25773
25774 scope_die = get_context_die (context);
25775
25776 if (child)
25777 {
25778 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25779 there is nothing we can do, here. */
25780 if (dwarf_version < 3 && dwarf_strict)
25781 return;
25782
25783 gcc_assert (scope_die->die_child);
25784 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25785 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25786 scope_die = scope_die->die_child;
25787 }
25788
25789 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25790 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25791 }
25792
25793 /* Output debug information for namelists. */
25794
25795 static dw_die_ref
25796 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25797 {
25798 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25799 tree value;
25800 unsigned i;
25801
25802 if (debug_info_level <= DINFO_LEVEL_TERSE)
25803 return NULL;
25804
25805 gcc_assert (scope_die != NULL);
25806 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25807 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25808
25809 /* If there are no item_decls, we have a nondefining namelist, e.g.
25810 with USE association; hence, set DW_AT_declaration. */
25811 if (item_decls == NULL_TREE)
25812 {
25813 add_AT_flag (nml_die, DW_AT_declaration, 1);
25814 return nml_die;
25815 }
25816
25817 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25818 {
25819 nml_item_ref_die = lookup_decl_die (value);
25820 if (!nml_item_ref_die)
25821 nml_item_ref_die = force_decl_die (value);
25822
25823 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25824 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25825 }
25826 return nml_die;
25827 }
25828
25829
25830 /* Write the debugging output for DECL and return the DIE. */
25831
25832 static void
25833 dwarf2out_decl (tree decl)
25834 {
25835 dw_die_ref context_die = comp_unit_die ();
25836
25837 switch (TREE_CODE (decl))
25838 {
25839 case ERROR_MARK:
25840 return;
25841
25842 case FUNCTION_DECL:
25843 /* If we're a nested function, initially use a parent of NULL; if we're
25844 a plain function, this will be fixed up in decls_for_scope. If
25845 we're a method, it will be ignored, since we already have a DIE. */
25846 if (decl_function_context (decl)
25847 /* But if we're in terse mode, we don't care about scope. */
25848 && debug_info_level > DINFO_LEVEL_TERSE)
25849 context_die = NULL;
25850 break;
25851
25852 case VAR_DECL:
25853 /* For local statics lookup proper context die. */
25854 if (local_function_static (decl))
25855 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25856
25857 /* If we are in terse mode, don't generate any DIEs to represent any
25858 variable declarations or definitions. */
25859 if (debug_info_level <= DINFO_LEVEL_TERSE)
25860 return;
25861 break;
25862
25863 case CONST_DECL:
25864 if (debug_info_level <= DINFO_LEVEL_TERSE)
25865 return;
25866 if (!is_fortran () && !is_ada ())
25867 return;
25868 if (TREE_STATIC (decl) && decl_function_context (decl))
25869 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25870 break;
25871
25872 case NAMESPACE_DECL:
25873 case IMPORTED_DECL:
25874 if (debug_info_level <= DINFO_LEVEL_TERSE)
25875 return;
25876 if (lookup_decl_die (decl) != NULL)
25877 return;
25878 break;
25879
25880 case TYPE_DECL:
25881 /* Don't emit stubs for types unless they are needed by other DIEs. */
25882 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25883 return;
25884
25885 /* Don't bother trying to generate any DIEs to represent any of the
25886 normal built-in types for the language we are compiling. */
25887 if (DECL_IS_BUILTIN (decl))
25888 return;
25889
25890 /* If we are in terse mode, don't generate any DIEs for types. */
25891 if (debug_info_level <= DINFO_LEVEL_TERSE)
25892 return;
25893
25894 /* If we're a function-scope tag, initially use a parent of NULL;
25895 this will be fixed up in decls_for_scope. */
25896 if (decl_function_context (decl))
25897 context_die = NULL;
25898
25899 break;
25900
25901 case NAMELIST_DECL:
25902 break;
25903
25904 default:
25905 return;
25906 }
25907
25908 gen_decl_die (decl, NULL, NULL, context_die);
25909
25910 if (flag_checking)
25911 {
25912 dw_die_ref die = lookup_decl_die (decl);
25913 if (die)
25914 check_die (die);
25915 }
25916 }
25917
25918 /* Write the debugging output for DECL. */
25919
25920 static void
25921 dwarf2out_function_decl (tree decl)
25922 {
25923 dwarf2out_decl (decl);
25924 call_arg_locations = NULL;
25925 call_arg_loc_last = NULL;
25926 call_site_count = -1;
25927 tail_call_site_count = -1;
25928 decl_loc_table->empty ();
25929 cached_dw_loc_list_table->empty ();
25930 }
25931
25932 /* Output a marker (i.e. a label) for the beginning of the generated code for
25933 a lexical block. */
25934
25935 static void
25936 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25937 unsigned int blocknum)
25938 {
25939 switch_to_section (current_function_section ());
25940 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25941 }
25942
25943 /* Output a marker (i.e. a label) for the end of the generated code for a
25944 lexical block. */
25945
25946 static void
25947 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25948 {
25949 switch_to_section (current_function_section ());
25950 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25951 }
25952
25953 /* Returns nonzero if it is appropriate not to emit any debugging
25954 information for BLOCK, because it doesn't contain any instructions.
25955
25956 Don't allow this for blocks with nested functions or local classes
25957 as we would end up with orphans, and in the presence of scheduling
25958 we may end up calling them anyway. */
25959
25960 static bool
25961 dwarf2out_ignore_block (const_tree block)
25962 {
25963 tree decl;
25964 unsigned int i;
25965
25966 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25967 if (TREE_CODE (decl) == FUNCTION_DECL
25968 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25969 return 0;
25970 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25971 {
25972 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25973 if (TREE_CODE (decl) == FUNCTION_DECL
25974 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25975 return 0;
25976 }
25977
25978 return 1;
25979 }
25980
25981 /* Hash table routines for file_hash. */
25982
25983 bool
25984 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25985 {
25986 return filename_cmp (p1->filename, p2) == 0;
25987 }
25988
25989 hashval_t
25990 dwarf_file_hasher::hash (dwarf_file_data *p)
25991 {
25992 return htab_hash_string (p->filename);
25993 }
25994
25995 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25996 dwarf2out.c) and return its "index". The index of each (known) filename is
25997 just a unique number which is associated with only that one filename. We
25998 need such numbers for the sake of generating labels (in the .debug_sfnames
25999 section) and references to those files numbers (in the .debug_srcinfo
26000 and .debug_macinfo sections). If the filename given as an argument is not
26001 found in our current list, add it to the list and assign it the next
26002 available unique index number. */
26003
26004 static struct dwarf_file_data *
26005 lookup_filename (const char *file_name)
26006 {
26007 struct dwarf_file_data * created;
26008
26009 if (!file_name)
26010 return NULL;
26011
26012 dwarf_file_data **slot
26013 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26014 INSERT);
26015 if (*slot)
26016 return *slot;
26017
26018 created = ggc_alloc<dwarf_file_data> ();
26019 created->filename = file_name;
26020 created->emitted_number = 0;
26021 *slot = created;
26022 return created;
26023 }
26024
26025 /* If the assembler will construct the file table, then translate the compiler
26026 internal file table number into the assembler file table number, and emit
26027 a .file directive if we haven't already emitted one yet. The file table
26028 numbers are different because we prune debug info for unused variables and
26029 types, which may include filenames. */
26030
26031 static int
26032 maybe_emit_file (struct dwarf_file_data * fd)
26033 {
26034 if (! fd->emitted_number)
26035 {
26036 if (last_emitted_file)
26037 fd->emitted_number = last_emitted_file->emitted_number + 1;
26038 else
26039 fd->emitted_number = 1;
26040 last_emitted_file = fd;
26041
26042 if (DWARF2_ASM_LINE_DEBUG_INFO)
26043 {
26044 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26045 output_quoted_string (asm_out_file,
26046 remap_debug_filename (fd->filename));
26047 fputc ('\n', asm_out_file);
26048 }
26049 }
26050
26051 return fd->emitted_number;
26052 }
26053
26054 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26055 That generation should happen after function debug info has been
26056 generated. The value of the attribute is the constant value of ARG. */
26057
26058 static void
26059 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26060 {
26061 die_arg_entry entry;
26062
26063 if (!die || !arg)
26064 return;
26065
26066 gcc_assert (early_dwarf);
26067
26068 if (!tmpl_value_parm_die_table)
26069 vec_alloc (tmpl_value_parm_die_table, 32);
26070
26071 entry.die = die;
26072 entry.arg = arg;
26073 vec_safe_push (tmpl_value_parm_die_table, entry);
26074 }
26075
26076 /* Return TRUE if T is an instance of generic type, FALSE
26077 otherwise. */
26078
26079 static bool
26080 generic_type_p (tree t)
26081 {
26082 if (t == NULL_TREE || !TYPE_P (t))
26083 return false;
26084 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26085 }
26086
26087 /* Schedule the generation of the generic parameter dies for the
26088 instance of generic type T. The proper generation itself is later
26089 done by gen_scheduled_generic_parms_dies. */
26090
26091 static void
26092 schedule_generic_params_dies_gen (tree t)
26093 {
26094 if (!generic_type_p (t))
26095 return;
26096
26097 gcc_assert (early_dwarf);
26098
26099 if (!generic_type_instances)
26100 vec_alloc (generic_type_instances, 256);
26101
26102 vec_safe_push (generic_type_instances, t);
26103 }
26104
26105 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26106 by append_entry_to_tmpl_value_parm_die_table. This function must
26107 be called after function DIEs have been generated. */
26108
26109 static void
26110 gen_remaining_tmpl_value_param_die_attribute (void)
26111 {
26112 if (tmpl_value_parm_die_table)
26113 {
26114 unsigned i, j;
26115 die_arg_entry *e;
26116
26117 /* We do this in two phases - first get the cases we can
26118 handle during early-finish, preserving those we cannot
26119 (containing symbolic constants where we don't yet know
26120 whether we are going to output the referenced symbols).
26121 For those we try again at late-finish. */
26122 j = 0;
26123 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26124 {
26125 if (!e->die->removed
26126 && !tree_add_const_value_attribute (e->die, e->arg))
26127 {
26128 dw_loc_descr_ref loc = NULL;
26129 if (! early_dwarf
26130 && (dwarf_version >= 5 || !dwarf_strict))
26131 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26132 if (loc)
26133 add_AT_loc (e->die, DW_AT_location, loc);
26134 else
26135 (*tmpl_value_parm_die_table)[j++] = *e;
26136 }
26137 }
26138 tmpl_value_parm_die_table->truncate (j);
26139 }
26140 }
26141
26142 /* Generate generic parameters DIEs for instances of generic types
26143 that have been previously scheduled by
26144 schedule_generic_params_dies_gen. This function must be called
26145 after all the types of the CU have been laid out. */
26146
26147 static void
26148 gen_scheduled_generic_parms_dies (void)
26149 {
26150 unsigned i;
26151 tree t;
26152
26153 if (!generic_type_instances)
26154 return;
26155
26156 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26157 if (COMPLETE_TYPE_P (t))
26158 gen_generic_params_dies (t);
26159
26160 generic_type_instances = NULL;
26161 }
26162
26163
26164 /* Replace DW_AT_name for the decl with name. */
26165
26166 static void
26167 dwarf2out_set_name (tree decl, tree name)
26168 {
26169 dw_die_ref die;
26170 dw_attr_node *attr;
26171 const char *dname;
26172
26173 die = TYPE_SYMTAB_DIE (decl);
26174 if (!die)
26175 return;
26176
26177 dname = dwarf2_name (name, 0);
26178 if (!dname)
26179 return;
26180
26181 attr = get_AT (die, DW_AT_name);
26182 if (attr)
26183 {
26184 struct indirect_string_node *node;
26185
26186 node = find_AT_string (dname);
26187 /* replace the string. */
26188 attr->dw_attr_val.v.val_str = node;
26189 }
26190
26191 else
26192 add_name_attribute (die, dname);
26193 }
26194
26195 /* True if before or during processing of the first function being emitted. */
26196 static bool in_first_function_p = true;
26197 /* True if loc_note during dwarf2out_var_location call might still be
26198 before first real instruction at address equal to .Ltext0. */
26199 static bool maybe_at_text_label_p = true;
26200 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26201 static unsigned int first_loclabel_num_not_at_text_label;
26202
26203 /* Called by the final INSN scan whenever we see a var location. We
26204 use it to drop labels in the right places, and throw the location in
26205 our lookup table. */
26206
26207 static void
26208 dwarf2out_var_location (rtx_insn *loc_note)
26209 {
26210 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26211 struct var_loc_node *newloc;
26212 rtx_insn *next_real, *next_note;
26213 rtx_insn *call_insn = NULL;
26214 static const char *last_label;
26215 static const char *last_postcall_label;
26216 static bool last_in_cold_section_p;
26217 static rtx_insn *expected_next_loc_note;
26218 tree decl;
26219 bool var_loc_p;
26220
26221 if (!NOTE_P (loc_note))
26222 {
26223 if (CALL_P (loc_note))
26224 {
26225 call_site_count++;
26226 if (SIBLING_CALL_P (loc_note))
26227 tail_call_site_count++;
26228 if (optimize == 0 && !flag_var_tracking)
26229 {
26230 /* When the var-tracking pass is not running, there is no note
26231 for indirect calls whose target is compile-time known. In this
26232 case, process such calls specifically so that we generate call
26233 sites for them anyway. */
26234 rtx x = PATTERN (loc_note);
26235 if (GET_CODE (x) == PARALLEL)
26236 x = XVECEXP (x, 0, 0);
26237 if (GET_CODE (x) == SET)
26238 x = SET_SRC (x);
26239 if (GET_CODE (x) == CALL)
26240 x = XEXP (x, 0);
26241 if (!MEM_P (x)
26242 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26243 || !SYMBOL_REF_DECL (XEXP (x, 0))
26244 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26245 != FUNCTION_DECL))
26246 {
26247 call_insn = loc_note;
26248 loc_note = NULL;
26249 var_loc_p = false;
26250
26251 next_real = next_real_insn (call_insn);
26252 next_note = NULL;
26253 cached_next_real_insn = NULL;
26254 goto create_label;
26255 }
26256 }
26257 }
26258 return;
26259 }
26260
26261 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26262 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26263 return;
26264
26265 /* Optimize processing a large consecutive sequence of location
26266 notes so we don't spend too much time in next_real_insn. If the
26267 next insn is another location note, remember the next_real_insn
26268 calculation for next time. */
26269 next_real = cached_next_real_insn;
26270 if (next_real)
26271 {
26272 if (expected_next_loc_note != loc_note)
26273 next_real = NULL;
26274 }
26275
26276 next_note = NEXT_INSN (loc_note);
26277 if (! next_note
26278 || next_note->deleted ()
26279 || ! NOTE_P (next_note)
26280 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26281 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26282 next_note = NULL;
26283
26284 if (! next_real)
26285 next_real = next_real_insn (loc_note);
26286
26287 if (next_note)
26288 {
26289 expected_next_loc_note = next_note;
26290 cached_next_real_insn = next_real;
26291 }
26292 else
26293 cached_next_real_insn = NULL;
26294
26295 /* If there are no instructions which would be affected by this note,
26296 don't do anything. */
26297 if (var_loc_p
26298 && next_real == NULL_RTX
26299 && !NOTE_DURING_CALL_P (loc_note))
26300 return;
26301
26302 create_label:
26303
26304 if (next_real == NULL_RTX)
26305 next_real = get_last_insn ();
26306
26307 /* If there were any real insns between note we processed last time
26308 and this note (or if it is the first note), clear
26309 last_{,postcall_}label so that they are not reused this time. */
26310 if (last_var_location_insn == NULL_RTX
26311 || last_var_location_insn != next_real
26312 || last_in_cold_section_p != in_cold_section_p)
26313 {
26314 last_label = NULL;
26315 last_postcall_label = NULL;
26316 }
26317
26318 if (var_loc_p)
26319 {
26320 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26321 newloc = add_var_loc_to_decl (decl, loc_note,
26322 NOTE_DURING_CALL_P (loc_note)
26323 ? last_postcall_label : last_label);
26324 if (newloc == NULL)
26325 return;
26326 }
26327 else
26328 {
26329 decl = NULL_TREE;
26330 newloc = NULL;
26331 }
26332
26333 /* If there were no real insns between note we processed last time
26334 and this note, use the label we emitted last time. Otherwise
26335 create a new label and emit it. */
26336 if (last_label == NULL)
26337 {
26338 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26339 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26340 loclabel_num++;
26341 last_label = ggc_strdup (loclabel);
26342 /* See if loclabel might be equal to .Ltext0. If yes,
26343 bump first_loclabel_num_not_at_text_label. */
26344 if (!have_multiple_function_sections
26345 && in_first_function_p
26346 && maybe_at_text_label_p)
26347 {
26348 static rtx_insn *last_start;
26349 rtx_insn *insn;
26350 for (insn = loc_note; insn; insn = previous_insn (insn))
26351 if (insn == last_start)
26352 break;
26353 else if (!NONDEBUG_INSN_P (insn))
26354 continue;
26355 else
26356 {
26357 rtx body = PATTERN (insn);
26358 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26359 continue;
26360 /* Inline asm could occupy zero bytes. */
26361 else if (GET_CODE (body) == ASM_INPUT
26362 || asm_noperands (body) >= 0)
26363 continue;
26364 #ifdef HAVE_attr_length
26365 else if (get_attr_min_length (insn) == 0)
26366 continue;
26367 #endif
26368 else
26369 {
26370 /* Assume insn has non-zero length. */
26371 maybe_at_text_label_p = false;
26372 break;
26373 }
26374 }
26375 if (maybe_at_text_label_p)
26376 {
26377 last_start = loc_note;
26378 first_loclabel_num_not_at_text_label = loclabel_num;
26379 }
26380 }
26381 }
26382
26383 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26384 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26385
26386 if (!var_loc_p)
26387 {
26388 struct call_arg_loc_node *ca_loc
26389 = ggc_cleared_alloc<call_arg_loc_node> ();
26390 rtx_insn *prev
26391 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26392
26393 ca_loc->call_arg_loc_note = loc_note;
26394 ca_loc->next = NULL;
26395 ca_loc->label = last_label;
26396 gcc_assert (prev
26397 && (CALL_P (prev)
26398 || (NONJUMP_INSN_P (prev)
26399 && GET_CODE (PATTERN (prev)) == SEQUENCE
26400 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26401 if (!CALL_P (prev))
26402 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26403 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26404
26405 /* Look for a SYMBOL_REF in the "prev" instruction. */
26406 rtx x = get_call_rtx_from (PATTERN (prev));
26407 if (x)
26408 {
26409 /* Try to get the call symbol, if any. */
26410 if (MEM_P (XEXP (x, 0)))
26411 x = XEXP (x, 0);
26412 /* First, look for a memory access to a symbol_ref. */
26413 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26414 && SYMBOL_REF_DECL (XEXP (x, 0))
26415 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26416 ca_loc->symbol_ref = XEXP (x, 0);
26417 /* Otherwise, look at a compile-time known user-level function
26418 declaration. */
26419 else if (MEM_P (x)
26420 && MEM_EXPR (x)
26421 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26422 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26423 }
26424
26425 ca_loc->block = insn_scope (prev);
26426 if (call_arg_locations)
26427 call_arg_loc_last->next = ca_loc;
26428 else
26429 call_arg_locations = ca_loc;
26430 call_arg_loc_last = ca_loc;
26431 }
26432 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26433 newloc->label = last_label;
26434 else
26435 {
26436 if (!last_postcall_label)
26437 {
26438 sprintf (loclabel, "%s-1", last_label);
26439 last_postcall_label = ggc_strdup (loclabel);
26440 }
26441 newloc->label = last_postcall_label;
26442 }
26443
26444 last_var_location_insn = next_real;
26445 last_in_cold_section_p = in_cold_section_p;
26446 }
26447
26448 /* Called from finalize_size_functions for size functions so that their body
26449 can be encoded in the debug info to describe the layout of variable-length
26450 structures. */
26451
26452 static void
26453 dwarf2out_size_function (tree decl)
26454 {
26455 function_to_dwarf_procedure (decl);
26456 }
26457
26458 /* Note in one location list that text section has changed. */
26459
26460 int
26461 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26462 {
26463 var_loc_list *list = *slot;
26464 if (list->first)
26465 list->last_before_switch
26466 = list->last->next ? list->last->next : list->last;
26467 return 1;
26468 }
26469
26470 /* Note in all location lists that text section has changed. */
26471
26472 static void
26473 var_location_switch_text_section (void)
26474 {
26475 if (decl_loc_table == NULL)
26476 return;
26477
26478 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26479 }
26480
26481 /* Create a new line number table. */
26482
26483 static dw_line_info_table *
26484 new_line_info_table (void)
26485 {
26486 dw_line_info_table *table;
26487
26488 table = ggc_cleared_alloc<dw_line_info_table> ();
26489 table->file_num = 1;
26490 table->line_num = 1;
26491 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26492
26493 return table;
26494 }
26495
26496 /* Lookup the "current" table into which we emit line info, so
26497 that we don't have to do it for every source line. */
26498
26499 static void
26500 set_cur_line_info_table (section *sec)
26501 {
26502 dw_line_info_table *table;
26503
26504 if (sec == text_section)
26505 table = text_section_line_info;
26506 else if (sec == cold_text_section)
26507 {
26508 table = cold_text_section_line_info;
26509 if (!table)
26510 {
26511 cold_text_section_line_info = table = new_line_info_table ();
26512 table->end_label = cold_end_label;
26513 }
26514 }
26515 else
26516 {
26517 const char *end_label;
26518
26519 if (crtl->has_bb_partition)
26520 {
26521 if (in_cold_section_p)
26522 end_label = crtl->subsections.cold_section_end_label;
26523 else
26524 end_label = crtl->subsections.hot_section_end_label;
26525 }
26526 else
26527 {
26528 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26529 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26530 current_function_funcdef_no);
26531 end_label = ggc_strdup (label);
26532 }
26533
26534 table = new_line_info_table ();
26535 table->end_label = end_label;
26536
26537 vec_safe_push (separate_line_info, table);
26538 }
26539
26540 if (DWARF2_ASM_LINE_DEBUG_INFO)
26541 table->is_stmt = (cur_line_info_table
26542 ? cur_line_info_table->is_stmt
26543 : DWARF_LINE_DEFAULT_IS_STMT_START);
26544 cur_line_info_table = table;
26545 }
26546
26547
26548 /* We need to reset the locations at the beginning of each
26549 function. We can't do this in the end_function hook, because the
26550 declarations that use the locations won't have been output when
26551 that hook is called. Also compute have_multiple_function_sections here. */
26552
26553 static void
26554 dwarf2out_begin_function (tree fun)
26555 {
26556 section *sec = function_section (fun);
26557
26558 if (sec != text_section)
26559 have_multiple_function_sections = true;
26560
26561 if (crtl->has_bb_partition && !cold_text_section)
26562 {
26563 gcc_assert (current_function_decl == fun);
26564 cold_text_section = unlikely_text_section ();
26565 switch_to_section (cold_text_section);
26566 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26567 switch_to_section (sec);
26568 }
26569
26570 dwarf2out_note_section_used ();
26571 call_site_count = 0;
26572 tail_call_site_count = 0;
26573
26574 set_cur_line_info_table (sec);
26575 }
26576
26577 /* Helper function of dwarf2out_end_function, called only after emitting
26578 the very first function into assembly. Check if some .debug_loc range
26579 might end with a .LVL* label that could be equal to .Ltext0.
26580 In that case we must force using absolute addresses in .debug_loc ranges,
26581 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26582 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26583 list terminator.
26584 Set have_multiple_function_sections to true in that case and
26585 terminate htab traversal. */
26586
26587 int
26588 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26589 {
26590 var_loc_list *entry = *slot;
26591 struct var_loc_node *node;
26592
26593 node = entry->first;
26594 if (node && node->next && node->next->label)
26595 {
26596 unsigned int i;
26597 const char *label = node->next->label;
26598 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26599
26600 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26601 {
26602 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26603 if (strcmp (label, loclabel) == 0)
26604 {
26605 have_multiple_function_sections = true;
26606 return 0;
26607 }
26608 }
26609 }
26610 return 1;
26611 }
26612
26613 /* Hook called after emitting a function into assembly.
26614 This does something only for the very first function emitted. */
26615
26616 static void
26617 dwarf2out_end_function (unsigned int)
26618 {
26619 if (in_first_function_p
26620 && !have_multiple_function_sections
26621 && first_loclabel_num_not_at_text_label
26622 && decl_loc_table)
26623 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26624 in_first_function_p = false;
26625 maybe_at_text_label_p = false;
26626 }
26627
26628 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26629 front-ends register a translation unit even before dwarf2out_init is
26630 called. */
26631 static tree main_translation_unit = NULL_TREE;
26632
26633 /* Hook called by front-ends after they built their main translation unit.
26634 Associate comp_unit_die to UNIT. */
26635
26636 static void
26637 dwarf2out_register_main_translation_unit (tree unit)
26638 {
26639 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26640 && main_translation_unit == NULL_TREE);
26641 main_translation_unit = unit;
26642 /* If dwarf2out_init has not been called yet, it will perform the association
26643 itself looking at main_translation_unit. */
26644 if (decl_die_table != NULL)
26645 equate_decl_number_to_die (unit, comp_unit_die ());
26646 }
26647
26648 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26649
26650 static void
26651 push_dw_line_info_entry (dw_line_info_table *table,
26652 enum dw_line_info_opcode opcode, unsigned int val)
26653 {
26654 dw_line_info_entry e;
26655 e.opcode = opcode;
26656 e.val = val;
26657 vec_safe_push (table->entries, e);
26658 }
26659
26660 /* Output a label to mark the beginning of a source code line entry
26661 and record information relating to this source line, in
26662 'line_info_table' for later output of the .debug_line section. */
26663 /* ??? The discriminator parameter ought to be unsigned. */
26664
26665 static void
26666 dwarf2out_source_line (unsigned int line, unsigned int column,
26667 const char *filename,
26668 int discriminator, bool is_stmt)
26669 {
26670 unsigned int file_num;
26671 dw_line_info_table *table;
26672
26673 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26674 return;
26675
26676 /* The discriminator column was added in dwarf4. Simplify the below
26677 by simply removing it if we're not supposed to output it. */
26678 if (dwarf_version < 4 && dwarf_strict)
26679 discriminator = 0;
26680
26681 if (!debug_column_info)
26682 column = 0;
26683
26684 table = cur_line_info_table;
26685 file_num = maybe_emit_file (lookup_filename (filename));
26686
26687 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26688 the debugger has used the second (possibly duplicate) line number
26689 at the beginning of the function to mark the end of the prologue.
26690 We could eliminate any other duplicates within the function. For
26691 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26692 that second line number entry. */
26693 /* Recall that this end-of-prologue indication is *not* the same thing
26694 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26695 to which the hook corresponds, follows the last insn that was
26696 emitted by gen_prologue. What we need is to precede the first insn
26697 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26698 insn that corresponds to something the user wrote. These may be
26699 very different locations once scheduling is enabled. */
26700
26701 if (0 && file_num == table->file_num
26702 && line == table->line_num
26703 && column == table->column_num
26704 && discriminator == table->discrim_num
26705 && is_stmt == table->is_stmt)
26706 return;
26707
26708 switch_to_section (current_function_section ());
26709
26710 /* If requested, emit something human-readable. */
26711 if (flag_debug_asm)
26712 {
26713 if (debug_column_info)
26714 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26715 filename, line, column);
26716 else
26717 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26718 filename, line);
26719 }
26720
26721 if (DWARF2_ASM_LINE_DEBUG_INFO)
26722 {
26723 /* Emit the .loc directive understood by GNU as. */
26724 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26725 file_num, line, is_stmt, discriminator */
26726 fputs ("\t.loc ", asm_out_file);
26727 fprint_ul (asm_out_file, file_num);
26728 putc (' ', asm_out_file);
26729 fprint_ul (asm_out_file, line);
26730 putc (' ', asm_out_file);
26731 fprint_ul (asm_out_file, column);
26732
26733 if (is_stmt != table->is_stmt)
26734 {
26735 fputs (" is_stmt ", asm_out_file);
26736 putc (is_stmt ? '1' : '0', asm_out_file);
26737 }
26738 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26739 {
26740 gcc_assert (discriminator > 0);
26741 fputs (" discriminator ", asm_out_file);
26742 fprint_ul (asm_out_file, (unsigned long) discriminator);
26743 }
26744 putc ('\n', asm_out_file);
26745 }
26746 else
26747 {
26748 unsigned int label_num = ++line_info_label_num;
26749
26750 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26751
26752 push_dw_line_info_entry (table, LI_set_address, label_num);
26753 if (file_num != table->file_num)
26754 push_dw_line_info_entry (table, LI_set_file, file_num);
26755 if (discriminator != table->discrim_num)
26756 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26757 if (is_stmt != table->is_stmt)
26758 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26759 push_dw_line_info_entry (table, LI_set_line, line);
26760 if (debug_column_info)
26761 push_dw_line_info_entry (table, LI_set_column, column);
26762 }
26763
26764 table->file_num = file_num;
26765 table->line_num = line;
26766 table->column_num = column;
26767 table->discrim_num = discriminator;
26768 table->is_stmt = is_stmt;
26769 table->in_use = true;
26770 }
26771
26772 /* Record the beginning of a new source file. */
26773
26774 static void
26775 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26776 {
26777 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26778 {
26779 macinfo_entry e;
26780 e.code = DW_MACINFO_start_file;
26781 e.lineno = lineno;
26782 e.info = ggc_strdup (filename);
26783 vec_safe_push (macinfo_table, e);
26784 }
26785 }
26786
26787 /* Record the end of a source file. */
26788
26789 static void
26790 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26791 {
26792 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26793 {
26794 macinfo_entry e;
26795 e.code = DW_MACINFO_end_file;
26796 e.lineno = lineno;
26797 e.info = NULL;
26798 vec_safe_push (macinfo_table, e);
26799 }
26800 }
26801
26802 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26803 the tail part of the directive line, i.e. the part which is past the
26804 initial whitespace, #, whitespace, directive-name, whitespace part. */
26805
26806 static void
26807 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26808 const char *buffer ATTRIBUTE_UNUSED)
26809 {
26810 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26811 {
26812 macinfo_entry e;
26813 /* Insert a dummy first entry to be able to optimize the whole
26814 predefined macro block using DW_MACRO_import. */
26815 if (macinfo_table->is_empty () && lineno <= 1)
26816 {
26817 e.code = 0;
26818 e.lineno = 0;
26819 e.info = NULL;
26820 vec_safe_push (macinfo_table, e);
26821 }
26822 e.code = DW_MACINFO_define;
26823 e.lineno = lineno;
26824 e.info = ggc_strdup (buffer);
26825 vec_safe_push (macinfo_table, e);
26826 }
26827 }
26828
26829 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26830 the tail part of the directive line, i.e. the part which is past the
26831 initial whitespace, #, whitespace, directive-name, whitespace part. */
26832
26833 static void
26834 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26835 const char *buffer ATTRIBUTE_UNUSED)
26836 {
26837 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26838 {
26839 macinfo_entry e;
26840 /* Insert a dummy first entry to be able to optimize the whole
26841 predefined macro block using DW_MACRO_import. */
26842 if (macinfo_table->is_empty () && lineno <= 1)
26843 {
26844 e.code = 0;
26845 e.lineno = 0;
26846 e.info = NULL;
26847 vec_safe_push (macinfo_table, e);
26848 }
26849 e.code = DW_MACINFO_undef;
26850 e.lineno = lineno;
26851 e.info = ggc_strdup (buffer);
26852 vec_safe_push (macinfo_table, e);
26853 }
26854 }
26855
26856 /* Helpers to manipulate hash table of CUs. */
26857
26858 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26859 {
26860 static inline hashval_t hash (const macinfo_entry *);
26861 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26862 };
26863
26864 inline hashval_t
26865 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26866 {
26867 return htab_hash_string (entry->info);
26868 }
26869
26870 inline bool
26871 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26872 const macinfo_entry *entry2)
26873 {
26874 return !strcmp (entry1->info, entry2->info);
26875 }
26876
26877 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26878
26879 /* Output a single .debug_macinfo entry. */
26880
26881 static void
26882 output_macinfo_op (macinfo_entry *ref)
26883 {
26884 int file_num;
26885 size_t len;
26886 struct indirect_string_node *node;
26887 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26888 struct dwarf_file_data *fd;
26889
26890 switch (ref->code)
26891 {
26892 case DW_MACINFO_start_file:
26893 fd = lookup_filename (ref->info);
26894 file_num = maybe_emit_file (fd);
26895 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26896 dw2_asm_output_data_uleb128 (ref->lineno,
26897 "Included from line number %lu",
26898 (unsigned long) ref->lineno);
26899 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26900 break;
26901 case DW_MACINFO_end_file:
26902 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26903 break;
26904 case DW_MACINFO_define:
26905 case DW_MACINFO_undef:
26906 len = strlen (ref->info) + 1;
26907 if (!dwarf_strict
26908 && len > DWARF_OFFSET_SIZE
26909 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26910 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26911 {
26912 ref->code = ref->code == DW_MACINFO_define
26913 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26914 output_macinfo_op (ref);
26915 return;
26916 }
26917 dw2_asm_output_data (1, ref->code,
26918 ref->code == DW_MACINFO_define
26919 ? "Define macro" : "Undefine macro");
26920 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26921 (unsigned long) ref->lineno);
26922 dw2_asm_output_nstring (ref->info, -1, "The macro");
26923 break;
26924 case DW_MACRO_define_strp:
26925 case DW_MACRO_undef_strp:
26926 node = find_AT_string (ref->info);
26927 gcc_assert (node
26928 && (node->form == DW_FORM_strp
26929 || node->form == DW_FORM_GNU_str_index));
26930 dw2_asm_output_data (1, ref->code,
26931 ref->code == DW_MACRO_define_strp
26932 ? "Define macro strp"
26933 : "Undefine macro strp");
26934 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26935 (unsigned long) ref->lineno);
26936 if (node->form == DW_FORM_strp)
26937 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26938 debug_str_section, "The macro: \"%s\"",
26939 ref->info);
26940 else
26941 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26942 ref->info);
26943 break;
26944 case DW_MACRO_import:
26945 dw2_asm_output_data (1, ref->code, "Import");
26946 ASM_GENERATE_INTERNAL_LABEL (label,
26947 DEBUG_MACRO_SECTION_LABEL,
26948 ref->lineno + macinfo_label_base);
26949 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26950 break;
26951 default:
26952 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26953 ASM_COMMENT_START, (unsigned long) ref->code);
26954 break;
26955 }
26956 }
26957
26958 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26959 other compilation unit .debug_macinfo sections. IDX is the first
26960 index of a define/undef, return the number of ops that should be
26961 emitted in a comdat .debug_macinfo section and emit
26962 a DW_MACRO_import entry referencing it.
26963 If the define/undef entry should be emitted normally, return 0. */
26964
26965 static unsigned
26966 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26967 macinfo_hash_type **macinfo_htab)
26968 {
26969 macinfo_entry *first, *second, *cur, *inc;
26970 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26971 unsigned char checksum[16];
26972 struct md5_ctx ctx;
26973 char *grp_name, *tail;
26974 const char *base;
26975 unsigned int i, count, encoded_filename_len, linebuf_len;
26976 macinfo_entry **slot;
26977
26978 first = &(*macinfo_table)[idx];
26979 second = &(*macinfo_table)[idx + 1];
26980
26981 /* Optimize only if there are at least two consecutive define/undef ops,
26982 and either all of them are before first DW_MACINFO_start_file
26983 with lineno {0,1} (i.e. predefined macro block), or all of them are
26984 in some included header file. */
26985 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26986 return 0;
26987 if (vec_safe_is_empty (files))
26988 {
26989 if (first->lineno > 1 || second->lineno > 1)
26990 return 0;
26991 }
26992 else if (first->lineno == 0)
26993 return 0;
26994
26995 /* Find the last define/undef entry that can be grouped together
26996 with first and at the same time compute md5 checksum of their
26997 codes, linenumbers and strings. */
26998 md5_init_ctx (&ctx);
26999 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27000 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27001 break;
27002 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27003 break;
27004 else
27005 {
27006 unsigned char code = cur->code;
27007 md5_process_bytes (&code, 1, &ctx);
27008 checksum_uleb128 (cur->lineno, &ctx);
27009 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27010 }
27011 md5_finish_ctx (&ctx, checksum);
27012 count = i - idx;
27013
27014 /* From the containing include filename (if any) pick up just
27015 usable characters from its basename. */
27016 if (vec_safe_is_empty (files))
27017 base = "";
27018 else
27019 base = lbasename (files->last ().info);
27020 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27021 if (ISIDNUM (base[i]) || base[i] == '.')
27022 encoded_filename_len++;
27023 /* Count . at the end. */
27024 if (encoded_filename_len)
27025 encoded_filename_len++;
27026
27027 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27028 linebuf_len = strlen (linebuf);
27029
27030 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27031 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27032 + 16 * 2 + 1);
27033 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27034 tail = grp_name + 4;
27035 if (encoded_filename_len)
27036 {
27037 for (i = 0; base[i]; i++)
27038 if (ISIDNUM (base[i]) || base[i] == '.')
27039 *tail++ = base[i];
27040 *tail++ = '.';
27041 }
27042 memcpy (tail, linebuf, linebuf_len);
27043 tail += linebuf_len;
27044 *tail++ = '.';
27045 for (i = 0; i < 16; i++)
27046 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27047
27048 /* Construct a macinfo_entry for DW_MACRO_import
27049 in the empty vector entry before the first define/undef. */
27050 inc = &(*macinfo_table)[idx - 1];
27051 inc->code = DW_MACRO_import;
27052 inc->lineno = 0;
27053 inc->info = ggc_strdup (grp_name);
27054 if (!*macinfo_htab)
27055 *macinfo_htab = new macinfo_hash_type (10);
27056 /* Avoid emitting duplicates. */
27057 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27058 if (*slot != NULL)
27059 {
27060 inc->code = 0;
27061 inc->info = NULL;
27062 /* If such an entry has been used before, just emit
27063 a DW_MACRO_import op. */
27064 inc = *slot;
27065 output_macinfo_op (inc);
27066 /* And clear all macinfo_entry in the range to avoid emitting them
27067 in the second pass. */
27068 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27069 {
27070 cur->code = 0;
27071 cur->info = NULL;
27072 }
27073 }
27074 else
27075 {
27076 *slot = inc;
27077 inc->lineno = (*macinfo_htab)->elements ();
27078 output_macinfo_op (inc);
27079 }
27080 return count;
27081 }
27082
27083 /* Save any strings needed by the macinfo table in the debug str
27084 table. All strings must be collected into the table by the time
27085 index_string is called. */
27086
27087 static void
27088 save_macinfo_strings (void)
27089 {
27090 unsigned len;
27091 unsigned i;
27092 macinfo_entry *ref;
27093
27094 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27095 {
27096 switch (ref->code)
27097 {
27098 /* Match the logic in output_macinfo_op to decide on
27099 indirect strings. */
27100 case DW_MACINFO_define:
27101 case DW_MACINFO_undef:
27102 len = strlen (ref->info) + 1;
27103 if (!dwarf_strict
27104 && len > DWARF_OFFSET_SIZE
27105 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27106 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27107 set_indirect_string (find_AT_string (ref->info));
27108 break;
27109 case DW_MACRO_define_strp:
27110 case DW_MACRO_undef_strp:
27111 set_indirect_string (find_AT_string (ref->info));
27112 break;
27113 default:
27114 break;
27115 }
27116 }
27117 }
27118
27119 /* Output macinfo section(s). */
27120
27121 static void
27122 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27123 {
27124 unsigned i;
27125 unsigned long length = vec_safe_length (macinfo_table);
27126 macinfo_entry *ref;
27127 vec<macinfo_entry, va_gc> *files = NULL;
27128 macinfo_hash_type *macinfo_htab = NULL;
27129 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27130
27131 if (! length)
27132 return;
27133
27134 /* output_macinfo* uses these interchangeably. */
27135 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27136 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27137 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27138 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27139
27140 /* AIX Assembler inserts the length, so adjust the reference to match the
27141 offset expected by debuggers. */
27142 strcpy (dl_section_ref, debug_line_label);
27143 if (XCOFF_DEBUGGING_INFO)
27144 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27145
27146 /* For .debug_macro emit the section header. */
27147 if (!dwarf_strict || dwarf_version >= 5)
27148 {
27149 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27150 "DWARF macro version number");
27151 if (DWARF_OFFSET_SIZE == 8)
27152 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27153 else
27154 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27155 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27156 debug_line_section, NULL);
27157 }
27158
27159 /* In the first loop, it emits the primary .debug_macinfo section
27160 and after each emitted op the macinfo_entry is cleared.
27161 If a longer range of define/undef ops can be optimized using
27162 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27163 the vector before the first define/undef in the range and the
27164 whole range of define/undef ops is not emitted and kept. */
27165 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27166 {
27167 switch (ref->code)
27168 {
27169 case DW_MACINFO_start_file:
27170 vec_safe_push (files, *ref);
27171 break;
27172 case DW_MACINFO_end_file:
27173 if (!vec_safe_is_empty (files))
27174 files->pop ();
27175 break;
27176 case DW_MACINFO_define:
27177 case DW_MACINFO_undef:
27178 if ((!dwarf_strict || dwarf_version >= 5)
27179 && HAVE_COMDAT_GROUP
27180 && vec_safe_length (files) != 1
27181 && i > 0
27182 && i + 1 < length
27183 && (*macinfo_table)[i - 1].code == 0)
27184 {
27185 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27186 if (count)
27187 {
27188 i += count - 1;
27189 continue;
27190 }
27191 }
27192 break;
27193 case 0:
27194 /* A dummy entry may be inserted at the beginning to be able
27195 to optimize the whole block of predefined macros. */
27196 if (i == 0)
27197 continue;
27198 default:
27199 break;
27200 }
27201 output_macinfo_op (ref);
27202 ref->info = NULL;
27203 ref->code = 0;
27204 }
27205
27206 if (!macinfo_htab)
27207 return;
27208
27209 /* Save the number of transparent includes so we can adjust the
27210 label number for the fat LTO object DWARF. */
27211 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27212
27213 delete macinfo_htab;
27214 macinfo_htab = NULL;
27215
27216 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27217 terminate the current chain and switch to a new comdat .debug_macinfo
27218 section and emit the define/undef entries within it. */
27219 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27220 switch (ref->code)
27221 {
27222 case 0:
27223 continue;
27224 case DW_MACRO_import:
27225 {
27226 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27227 tree comdat_key = get_identifier (ref->info);
27228 /* Terminate the previous .debug_macinfo section. */
27229 dw2_asm_output_data (1, 0, "End compilation unit");
27230 targetm.asm_out.named_section (debug_macinfo_section_name,
27231 SECTION_DEBUG
27232 | SECTION_LINKONCE
27233 | (early_lto_debug
27234 ? SECTION_EXCLUDE : 0),
27235 comdat_key);
27236 ASM_GENERATE_INTERNAL_LABEL (label,
27237 DEBUG_MACRO_SECTION_LABEL,
27238 ref->lineno + macinfo_label_base);
27239 ASM_OUTPUT_LABEL (asm_out_file, label);
27240 ref->code = 0;
27241 ref->info = NULL;
27242 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27243 "DWARF macro version number");
27244 if (DWARF_OFFSET_SIZE == 8)
27245 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27246 else
27247 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27248 }
27249 break;
27250 case DW_MACINFO_define:
27251 case DW_MACINFO_undef:
27252 output_macinfo_op (ref);
27253 ref->code = 0;
27254 ref->info = NULL;
27255 break;
27256 default:
27257 gcc_unreachable ();
27258 }
27259
27260 macinfo_label_base += macinfo_label_base_adj;
27261 }
27262
27263 /* Initialize the various sections and labels for dwarf output and prefix
27264 them with PREFIX if non-NULL. */
27265
27266 static void
27267 init_sections_and_labels (bool early_lto_debug)
27268 {
27269 /* As we may get called multiple times have a generation count for
27270 labels. */
27271 static unsigned generation = 0;
27272
27273 if (early_lto_debug)
27274 {
27275 if (!dwarf_split_debug_info)
27276 {
27277 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27278 SECTION_DEBUG | SECTION_EXCLUDE,
27279 NULL);
27280 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27281 SECTION_DEBUG | SECTION_EXCLUDE,
27282 NULL);
27283 debug_macinfo_section_name
27284 = ((dwarf_strict && dwarf_version < 5)
27285 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27286 debug_macinfo_section = get_section (debug_macinfo_section_name,
27287 SECTION_DEBUG
27288 | SECTION_EXCLUDE, NULL);
27289 /* For macro info we have to refer to a debug_line section, so
27290 similar to split-dwarf emit a skeleton one for early debug. */
27291 debug_skeleton_line_section
27292 = get_section (DEBUG_LTO_LINE_SECTION,
27293 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27294 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27295 DEBUG_SKELETON_LINE_SECTION_LABEL,
27296 generation);
27297 }
27298 else
27299 {
27300 /* ??? Which of the following do we need early? */
27301 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27302 SECTION_DEBUG | SECTION_EXCLUDE,
27303 NULL);
27304 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27305 SECTION_DEBUG | SECTION_EXCLUDE,
27306 NULL);
27307 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27308 SECTION_DEBUG
27309 | SECTION_EXCLUDE, NULL);
27310 debug_skeleton_abbrev_section
27311 = get_section (DEBUG_LTO_ABBREV_SECTION,
27312 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27313 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27314 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27315 generation);
27316
27317 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27318 stay in the main .o, but the skeleton_line goes into the split
27319 off dwo. */
27320 debug_skeleton_line_section
27321 = get_section (DEBUG_LTO_LINE_SECTION,
27322 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27323 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27324 DEBUG_SKELETON_LINE_SECTION_LABEL,
27325 generation);
27326 debug_str_offsets_section
27327 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27328 SECTION_DEBUG | SECTION_EXCLUDE,
27329 NULL);
27330 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27331 DEBUG_SKELETON_INFO_SECTION_LABEL,
27332 generation);
27333 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27334 DEBUG_STR_DWO_SECTION_FLAGS,
27335 NULL);
27336 debug_macinfo_section_name
27337 = ((dwarf_strict && dwarf_version < 5)
27338 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27339 debug_macinfo_section = get_section (debug_macinfo_section_name,
27340 SECTION_DEBUG | SECTION_EXCLUDE,
27341 NULL);
27342 }
27343 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27344 DEBUG_STR_SECTION_FLAGS
27345 | SECTION_EXCLUDE, NULL);
27346 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27347 debug_line_str_section
27348 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27349 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27350 }
27351 else
27352 {
27353 if (!dwarf_split_debug_info)
27354 {
27355 debug_info_section = get_section (DEBUG_INFO_SECTION,
27356 SECTION_DEBUG, NULL);
27357 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27358 SECTION_DEBUG, NULL);
27359 debug_loc_section = get_section (dwarf_version >= 5
27360 ? DEBUG_LOCLISTS_SECTION
27361 : DEBUG_LOC_SECTION,
27362 SECTION_DEBUG, NULL);
27363 debug_macinfo_section_name
27364 = ((dwarf_strict && dwarf_version < 5)
27365 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27366 debug_macinfo_section = get_section (debug_macinfo_section_name,
27367 SECTION_DEBUG, NULL);
27368 }
27369 else
27370 {
27371 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27372 SECTION_DEBUG | SECTION_EXCLUDE,
27373 NULL);
27374 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27375 SECTION_DEBUG | SECTION_EXCLUDE,
27376 NULL);
27377 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27378 SECTION_DEBUG, NULL);
27379 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27380 SECTION_DEBUG, NULL);
27381 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27382 SECTION_DEBUG, NULL);
27383 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27384 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27385 generation);
27386
27387 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27388 stay in the main .o, but the skeleton_line goes into the
27389 split off dwo. */
27390 debug_skeleton_line_section
27391 = get_section (DEBUG_DWO_LINE_SECTION,
27392 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27393 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27394 DEBUG_SKELETON_LINE_SECTION_LABEL,
27395 generation);
27396 debug_str_offsets_section
27397 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27398 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27399 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27400 DEBUG_SKELETON_INFO_SECTION_LABEL,
27401 generation);
27402 debug_loc_section = get_section (dwarf_version >= 5
27403 ? DEBUG_DWO_LOCLISTS_SECTION
27404 : DEBUG_DWO_LOC_SECTION,
27405 SECTION_DEBUG | SECTION_EXCLUDE,
27406 NULL);
27407 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27408 DEBUG_STR_DWO_SECTION_FLAGS,
27409 NULL);
27410 debug_macinfo_section_name
27411 = ((dwarf_strict && dwarf_version < 5)
27412 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27413 debug_macinfo_section = get_section (debug_macinfo_section_name,
27414 SECTION_DEBUG | SECTION_EXCLUDE,
27415 NULL);
27416 }
27417 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27418 SECTION_DEBUG, NULL);
27419 debug_line_section = get_section (DEBUG_LINE_SECTION,
27420 SECTION_DEBUG, NULL);
27421 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27422 SECTION_DEBUG, NULL);
27423 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27424 SECTION_DEBUG, NULL);
27425 debug_str_section = get_section (DEBUG_STR_SECTION,
27426 DEBUG_STR_SECTION_FLAGS, NULL);
27427 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27428 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27429 DEBUG_STR_SECTION_FLAGS, NULL);
27430 debug_ranges_section = get_section (dwarf_version >= 5
27431 ? DEBUG_RNGLISTS_SECTION
27432 : DEBUG_RANGES_SECTION,
27433 SECTION_DEBUG, NULL);
27434 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27435 SECTION_DEBUG, NULL);
27436 }
27437
27438 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27439 DEBUG_ABBREV_SECTION_LABEL, generation);
27440 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27441 DEBUG_INFO_SECTION_LABEL, generation);
27442 info_section_emitted = false;
27443 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27444 DEBUG_LINE_SECTION_LABEL, generation);
27445 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27446 DEBUG_RANGES_SECTION_LABEL, generation);
27447 if (dwarf_version >= 5 && dwarf_split_debug_info)
27448 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27449 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27450 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27451 DEBUG_ADDR_SECTION_LABEL, generation);
27452 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27453 (dwarf_strict && dwarf_version < 5)
27454 ? DEBUG_MACINFO_SECTION_LABEL
27455 : DEBUG_MACRO_SECTION_LABEL, generation);
27456 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27457 generation);
27458
27459 ++generation;
27460 }
27461
27462 /* Set up for Dwarf output at the start of compilation. */
27463
27464 static void
27465 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27466 {
27467 /* Allocate the file_table. */
27468 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27469
27470 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27471 /* Allocate the decl_die_table. */
27472 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27473
27474 /* Allocate the decl_loc_table. */
27475 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27476
27477 /* Allocate the cached_dw_loc_list_table. */
27478 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27479
27480 /* Allocate the initial hunk of the decl_scope_table. */
27481 vec_alloc (decl_scope_table, 256);
27482
27483 /* Allocate the initial hunk of the abbrev_die_table. */
27484 vec_alloc (abbrev_die_table, 256);
27485 /* Zero-th entry is allocated, but unused. */
27486 abbrev_die_table->quick_push (NULL);
27487
27488 /* Allocate the dwarf_proc_stack_usage_map. */
27489 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27490
27491 /* Allocate the pubtypes and pubnames vectors. */
27492 vec_alloc (pubname_table, 32);
27493 vec_alloc (pubtype_table, 32);
27494
27495 vec_alloc (incomplete_types, 64);
27496
27497 vec_alloc (used_rtx_array, 32);
27498
27499 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27500 vec_alloc (macinfo_table, 64);
27501 #endif
27502
27503 /* If front-ends already registered a main translation unit but we were not
27504 ready to perform the association, do this now. */
27505 if (main_translation_unit != NULL_TREE)
27506 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27507 }
27508
27509 /* Called before compile () starts outputtting functions, variables
27510 and toplevel asms into assembly. */
27511
27512 static void
27513 dwarf2out_assembly_start (void)
27514 {
27515 if (text_section_line_info)
27516 return;
27517
27518 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27519 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27520 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27521 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27522 COLD_TEXT_SECTION_LABEL, 0);
27523 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27524
27525 switch_to_section (text_section);
27526 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27527 #endif
27528
27529 /* Make sure the line number table for .text always exists. */
27530 text_section_line_info = new_line_info_table ();
27531 text_section_line_info->end_label = text_end_label;
27532
27533 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27534 cur_line_info_table = text_section_line_info;
27535 #endif
27536
27537 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27538 && dwarf2out_do_cfi_asm ()
27539 && !dwarf2out_do_eh_frame ())
27540 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27541 }
27542
27543 /* A helper function for dwarf2out_finish called through
27544 htab_traverse. Assign a string its index. All strings must be
27545 collected into the table by the time index_string is called,
27546 because the indexing code relies on htab_traverse to traverse nodes
27547 in the same order for each run. */
27548
27549 int
27550 index_string (indirect_string_node **h, unsigned int *index)
27551 {
27552 indirect_string_node *node = *h;
27553
27554 find_string_form (node);
27555 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27556 {
27557 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27558 node->index = *index;
27559 *index += 1;
27560 }
27561 return 1;
27562 }
27563
27564 /* A helper function for output_indirect_strings called through
27565 htab_traverse. Output the offset to a string and update the
27566 current offset. */
27567
27568 int
27569 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27570 {
27571 indirect_string_node *node = *h;
27572
27573 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27574 {
27575 /* Assert that this node has been assigned an index. */
27576 gcc_assert (node->index != NO_INDEX_ASSIGNED
27577 && node->index != NOT_INDEXED);
27578 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27579 "indexed string 0x%x: %s", node->index, node->str);
27580 *offset += strlen (node->str) + 1;
27581 }
27582 return 1;
27583 }
27584
27585 /* A helper function for dwarf2out_finish called through
27586 htab_traverse. Output the indexed string. */
27587
27588 int
27589 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27590 {
27591 struct indirect_string_node *node = *h;
27592
27593 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27594 {
27595 /* Assert that the strings are output in the same order as their
27596 indexes were assigned. */
27597 gcc_assert (*cur_idx == node->index);
27598 assemble_string (node->str, strlen (node->str) + 1);
27599 *cur_idx += 1;
27600 }
27601 return 1;
27602 }
27603
27604 /* A helper function for dwarf2out_finish called through
27605 htab_traverse. Emit one queued .debug_str string. */
27606
27607 int
27608 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27609 {
27610 struct indirect_string_node *node = *h;
27611
27612 node->form = find_string_form (node);
27613 if (node->form == form && node->refcount > 0)
27614 {
27615 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27616 assemble_string (node->str, strlen (node->str) + 1);
27617 }
27618
27619 return 1;
27620 }
27621
27622 /* Output the indexed string table. */
27623
27624 static void
27625 output_indirect_strings (void)
27626 {
27627 switch_to_section (debug_str_section);
27628 if (!dwarf_split_debug_info)
27629 debug_str_hash->traverse<enum dwarf_form,
27630 output_indirect_string> (DW_FORM_strp);
27631 else
27632 {
27633 unsigned int offset = 0;
27634 unsigned int cur_idx = 0;
27635
27636 skeleton_debug_str_hash->traverse<enum dwarf_form,
27637 output_indirect_string> (DW_FORM_strp);
27638
27639 switch_to_section (debug_str_offsets_section);
27640 debug_str_hash->traverse_noresize
27641 <unsigned int *, output_index_string_offset> (&offset);
27642 switch_to_section (debug_str_dwo_section);
27643 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27644 (&cur_idx);
27645 }
27646 }
27647
27648 /* Callback for htab_traverse to assign an index to an entry in the
27649 table, and to write that entry to the .debug_addr section. */
27650
27651 int
27652 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27653 {
27654 addr_table_entry *entry = *slot;
27655
27656 if (entry->refcount == 0)
27657 {
27658 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27659 || entry->index == NOT_INDEXED);
27660 return 1;
27661 }
27662
27663 gcc_assert (entry->index == *cur_index);
27664 (*cur_index)++;
27665
27666 switch (entry->kind)
27667 {
27668 case ate_kind_rtx:
27669 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27670 "0x%x", entry->index);
27671 break;
27672 case ate_kind_rtx_dtprel:
27673 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27674 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27675 DWARF2_ADDR_SIZE,
27676 entry->addr.rtl);
27677 fputc ('\n', asm_out_file);
27678 break;
27679 case ate_kind_label:
27680 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27681 "0x%x", entry->index);
27682 break;
27683 default:
27684 gcc_unreachable ();
27685 }
27686 return 1;
27687 }
27688
27689 /* Produce the .debug_addr section. */
27690
27691 static void
27692 output_addr_table (void)
27693 {
27694 unsigned int index = 0;
27695 if (addr_index_table == NULL || addr_index_table->size () == 0)
27696 return;
27697
27698 switch_to_section (debug_addr_section);
27699 addr_index_table
27700 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27701 }
27702
27703 #if ENABLE_ASSERT_CHECKING
27704 /* Verify that all marks are clear. */
27705
27706 static void
27707 verify_marks_clear (dw_die_ref die)
27708 {
27709 dw_die_ref c;
27710
27711 gcc_assert (! die->die_mark);
27712 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27713 }
27714 #endif /* ENABLE_ASSERT_CHECKING */
27715
27716 /* Clear the marks for a die and its children.
27717 Be cool if the mark isn't set. */
27718
27719 static void
27720 prune_unmark_dies (dw_die_ref die)
27721 {
27722 dw_die_ref c;
27723
27724 if (die->die_mark)
27725 die->die_mark = 0;
27726 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27727 }
27728
27729 /* Given LOC that is referenced by a DIE we're marking as used, find all
27730 referenced DWARF procedures it references and mark them as used. */
27731
27732 static void
27733 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27734 {
27735 for (; loc != NULL; loc = loc->dw_loc_next)
27736 switch (loc->dw_loc_opc)
27737 {
27738 case DW_OP_implicit_pointer:
27739 case DW_OP_convert:
27740 case DW_OP_reinterpret:
27741 case DW_OP_GNU_implicit_pointer:
27742 case DW_OP_GNU_convert:
27743 case DW_OP_GNU_reinterpret:
27744 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27745 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27746 break;
27747 case DW_OP_GNU_variable_value:
27748 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27749 {
27750 dw_die_ref ref
27751 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27752 if (ref == NULL)
27753 break;
27754 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27755 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27756 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27757 }
27758 /* FALLTHRU */
27759 case DW_OP_call2:
27760 case DW_OP_call4:
27761 case DW_OP_call_ref:
27762 case DW_OP_const_type:
27763 case DW_OP_GNU_const_type:
27764 case DW_OP_GNU_parameter_ref:
27765 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27766 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27767 break;
27768 case DW_OP_regval_type:
27769 case DW_OP_deref_type:
27770 case DW_OP_GNU_regval_type:
27771 case DW_OP_GNU_deref_type:
27772 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27773 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27774 break;
27775 case DW_OP_entry_value:
27776 case DW_OP_GNU_entry_value:
27777 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27778 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27779 break;
27780 default:
27781 break;
27782 }
27783 }
27784
27785 /* Given DIE that we're marking as used, find any other dies
27786 it references as attributes and mark them as used. */
27787
27788 static void
27789 prune_unused_types_walk_attribs (dw_die_ref die)
27790 {
27791 dw_attr_node *a;
27792 unsigned ix;
27793
27794 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27795 {
27796 switch (AT_class (a))
27797 {
27798 /* Make sure DWARF procedures referenced by location descriptions will
27799 get emitted. */
27800 case dw_val_class_loc:
27801 prune_unused_types_walk_loc_descr (AT_loc (a));
27802 break;
27803 case dw_val_class_loc_list:
27804 for (dw_loc_list_ref list = AT_loc_list (a);
27805 list != NULL;
27806 list = list->dw_loc_next)
27807 prune_unused_types_walk_loc_descr (list->expr);
27808 break;
27809
27810 case dw_val_class_die_ref:
27811 /* A reference to another DIE.
27812 Make sure that it will get emitted.
27813 If it was broken out into a comdat group, don't follow it. */
27814 if (! AT_ref (a)->comdat_type_p
27815 || a->dw_attr == DW_AT_specification)
27816 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27817 break;
27818
27819 case dw_val_class_str:
27820 /* Set the string's refcount to 0 so that prune_unused_types_mark
27821 accounts properly for it. */
27822 a->dw_attr_val.v.val_str->refcount = 0;
27823 break;
27824
27825 default:
27826 break;
27827 }
27828 }
27829 }
27830
27831 /* Mark the generic parameters and arguments children DIEs of DIE. */
27832
27833 static void
27834 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27835 {
27836 dw_die_ref c;
27837
27838 if (die == NULL || die->die_child == NULL)
27839 return;
27840 c = die->die_child;
27841 do
27842 {
27843 if (is_template_parameter (c))
27844 prune_unused_types_mark (c, 1);
27845 c = c->die_sib;
27846 } while (c && c != die->die_child);
27847 }
27848
27849 /* Mark DIE as being used. If DOKIDS is true, then walk down
27850 to DIE's children. */
27851
27852 static void
27853 prune_unused_types_mark (dw_die_ref die, int dokids)
27854 {
27855 dw_die_ref c;
27856
27857 if (die->die_mark == 0)
27858 {
27859 /* We haven't done this node yet. Mark it as used. */
27860 die->die_mark = 1;
27861 /* If this is the DIE of a generic type instantiation,
27862 mark the children DIEs that describe its generic parms and
27863 args. */
27864 prune_unused_types_mark_generic_parms_dies (die);
27865
27866 /* We also have to mark its parents as used.
27867 (But we don't want to mark our parent's kids due to this,
27868 unless it is a class.) */
27869 if (die->die_parent)
27870 prune_unused_types_mark (die->die_parent,
27871 class_scope_p (die->die_parent));
27872
27873 /* Mark any referenced nodes. */
27874 prune_unused_types_walk_attribs (die);
27875
27876 /* If this node is a specification,
27877 also mark the definition, if it exists. */
27878 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27879 prune_unused_types_mark (die->die_definition, 1);
27880 }
27881
27882 if (dokids && die->die_mark != 2)
27883 {
27884 /* We need to walk the children, but haven't done so yet.
27885 Remember that we've walked the kids. */
27886 die->die_mark = 2;
27887
27888 /* If this is an array type, we need to make sure our
27889 kids get marked, even if they're types. If we're
27890 breaking out types into comdat sections, do this
27891 for all type definitions. */
27892 if (die->die_tag == DW_TAG_array_type
27893 || (use_debug_types
27894 && is_type_die (die) && ! is_declaration_die (die)))
27895 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27896 else
27897 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27898 }
27899 }
27900
27901 /* For local classes, look if any static member functions were emitted
27902 and if so, mark them. */
27903
27904 static void
27905 prune_unused_types_walk_local_classes (dw_die_ref die)
27906 {
27907 dw_die_ref c;
27908
27909 if (die->die_mark == 2)
27910 return;
27911
27912 switch (die->die_tag)
27913 {
27914 case DW_TAG_structure_type:
27915 case DW_TAG_union_type:
27916 case DW_TAG_class_type:
27917 break;
27918
27919 case DW_TAG_subprogram:
27920 if (!get_AT_flag (die, DW_AT_declaration)
27921 || die->die_definition != NULL)
27922 prune_unused_types_mark (die, 1);
27923 return;
27924
27925 default:
27926 return;
27927 }
27928
27929 /* Mark children. */
27930 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27931 }
27932
27933 /* Walk the tree DIE and mark types that we actually use. */
27934
27935 static void
27936 prune_unused_types_walk (dw_die_ref die)
27937 {
27938 dw_die_ref c;
27939
27940 /* Don't do anything if this node is already marked and
27941 children have been marked as well. */
27942 if (die->die_mark == 2)
27943 return;
27944
27945 switch (die->die_tag)
27946 {
27947 case DW_TAG_structure_type:
27948 case DW_TAG_union_type:
27949 case DW_TAG_class_type:
27950 if (die->die_perennial_p)
27951 break;
27952
27953 for (c = die->die_parent; c; c = c->die_parent)
27954 if (c->die_tag == DW_TAG_subprogram)
27955 break;
27956
27957 /* Finding used static member functions inside of classes
27958 is needed just for local classes, because for other classes
27959 static member function DIEs with DW_AT_specification
27960 are emitted outside of the DW_TAG_*_type. If we ever change
27961 it, we'd need to call this even for non-local classes. */
27962 if (c)
27963 prune_unused_types_walk_local_classes (die);
27964
27965 /* It's a type node --- don't mark it. */
27966 return;
27967
27968 case DW_TAG_const_type:
27969 case DW_TAG_packed_type:
27970 case DW_TAG_pointer_type:
27971 case DW_TAG_reference_type:
27972 case DW_TAG_rvalue_reference_type:
27973 case DW_TAG_volatile_type:
27974 case DW_TAG_typedef:
27975 case DW_TAG_array_type:
27976 case DW_TAG_interface_type:
27977 case DW_TAG_friend:
27978 case DW_TAG_enumeration_type:
27979 case DW_TAG_subroutine_type:
27980 case DW_TAG_string_type:
27981 case DW_TAG_set_type:
27982 case DW_TAG_subrange_type:
27983 case DW_TAG_ptr_to_member_type:
27984 case DW_TAG_file_type:
27985 /* Type nodes are useful only when other DIEs reference them --- don't
27986 mark them. */
27987 /* FALLTHROUGH */
27988
27989 case DW_TAG_dwarf_procedure:
27990 /* Likewise for DWARF procedures. */
27991
27992 if (die->die_perennial_p)
27993 break;
27994
27995 return;
27996
27997 default:
27998 /* Mark everything else. */
27999 break;
28000 }
28001
28002 if (die->die_mark == 0)
28003 {
28004 die->die_mark = 1;
28005
28006 /* Now, mark any dies referenced from here. */
28007 prune_unused_types_walk_attribs (die);
28008 }
28009
28010 die->die_mark = 2;
28011
28012 /* Mark children. */
28013 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28014 }
28015
28016 /* Increment the string counts on strings referred to from DIE's
28017 attributes. */
28018
28019 static void
28020 prune_unused_types_update_strings (dw_die_ref die)
28021 {
28022 dw_attr_node *a;
28023 unsigned ix;
28024
28025 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28026 if (AT_class (a) == dw_val_class_str)
28027 {
28028 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28029 s->refcount++;
28030 /* Avoid unnecessarily putting strings that are used less than
28031 twice in the hash table. */
28032 if (s->refcount
28033 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28034 {
28035 indirect_string_node **slot
28036 = debug_str_hash->find_slot_with_hash (s->str,
28037 htab_hash_string (s->str),
28038 INSERT);
28039 gcc_assert (*slot == NULL);
28040 *slot = s;
28041 }
28042 }
28043 }
28044
28045 /* Mark DIE and its children as removed. */
28046
28047 static void
28048 mark_removed (dw_die_ref die)
28049 {
28050 dw_die_ref c;
28051 die->removed = true;
28052 FOR_EACH_CHILD (die, c, mark_removed (c));
28053 }
28054
28055 /* Remove from the tree DIE any dies that aren't marked. */
28056
28057 static void
28058 prune_unused_types_prune (dw_die_ref die)
28059 {
28060 dw_die_ref c;
28061
28062 gcc_assert (die->die_mark);
28063 prune_unused_types_update_strings (die);
28064
28065 if (! die->die_child)
28066 return;
28067
28068 c = die->die_child;
28069 do {
28070 dw_die_ref prev = c, next;
28071 for (c = c->die_sib; ! c->die_mark; c = next)
28072 if (c == die->die_child)
28073 {
28074 /* No marked children between 'prev' and the end of the list. */
28075 if (prev == c)
28076 /* No marked children at all. */
28077 die->die_child = NULL;
28078 else
28079 {
28080 prev->die_sib = c->die_sib;
28081 die->die_child = prev;
28082 }
28083 c->die_sib = NULL;
28084 mark_removed (c);
28085 return;
28086 }
28087 else
28088 {
28089 next = c->die_sib;
28090 c->die_sib = NULL;
28091 mark_removed (c);
28092 }
28093
28094 if (c != prev->die_sib)
28095 prev->die_sib = c;
28096 prune_unused_types_prune (c);
28097 } while (c != die->die_child);
28098 }
28099
28100 /* Remove dies representing declarations that we never use. */
28101
28102 static void
28103 prune_unused_types (void)
28104 {
28105 unsigned int i;
28106 limbo_die_node *node;
28107 comdat_type_node *ctnode;
28108 pubname_entry *pub;
28109 dw_die_ref base_type;
28110
28111 #if ENABLE_ASSERT_CHECKING
28112 /* All the marks should already be clear. */
28113 verify_marks_clear (comp_unit_die ());
28114 for (node = limbo_die_list; node; node = node->next)
28115 verify_marks_clear (node->die);
28116 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28117 verify_marks_clear (ctnode->root_die);
28118 #endif /* ENABLE_ASSERT_CHECKING */
28119
28120 /* Mark types that are used in global variables. */
28121 premark_types_used_by_global_vars ();
28122
28123 /* Set the mark on nodes that are actually used. */
28124 prune_unused_types_walk (comp_unit_die ());
28125 for (node = limbo_die_list; node; node = node->next)
28126 prune_unused_types_walk (node->die);
28127 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28128 {
28129 prune_unused_types_walk (ctnode->root_die);
28130 prune_unused_types_mark (ctnode->type_die, 1);
28131 }
28132
28133 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28134 are unusual in that they are pubnames that are the children of pubtypes.
28135 They should only be marked via their parent DW_TAG_enumeration_type die,
28136 not as roots in themselves. */
28137 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28138 if (pub->die->die_tag != DW_TAG_enumerator)
28139 prune_unused_types_mark (pub->die, 1);
28140 for (i = 0; base_types.iterate (i, &base_type); i++)
28141 prune_unused_types_mark (base_type, 1);
28142
28143 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28144 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28145 callees). */
28146 cgraph_node *cnode;
28147 FOR_EACH_FUNCTION (cnode)
28148 if (cnode->referred_to_p (false))
28149 {
28150 dw_die_ref die = lookup_decl_die (cnode->decl);
28151 if (die == NULL || die->die_mark)
28152 continue;
28153 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28154 if (e->caller != cnode
28155 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28156 {
28157 prune_unused_types_mark (die, 1);
28158 break;
28159 }
28160 }
28161
28162 if (debug_str_hash)
28163 debug_str_hash->empty ();
28164 if (skeleton_debug_str_hash)
28165 skeleton_debug_str_hash->empty ();
28166 prune_unused_types_prune (comp_unit_die ());
28167 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28168 {
28169 node = *pnode;
28170 if (!node->die->die_mark)
28171 *pnode = node->next;
28172 else
28173 {
28174 prune_unused_types_prune (node->die);
28175 pnode = &node->next;
28176 }
28177 }
28178 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28179 prune_unused_types_prune (ctnode->root_die);
28180
28181 /* Leave the marks clear. */
28182 prune_unmark_dies (comp_unit_die ());
28183 for (node = limbo_die_list; node; node = node->next)
28184 prune_unmark_dies (node->die);
28185 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28186 prune_unmark_dies (ctnode->root_die);
28187 }
28188
28189 /* Helpers to manipulate hash table of comdat type units. */
28190
28191 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28192 {
28193 static inline hashval_t hash (const comdat_type_node *);
28194 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28195 };
28196
28197 inline hashval_t
28198 comdat_type_hasher::hash (const comdat_type_node *type_node)
28199 {
28200 hashval_t h;
28201 memcpy (&h, type_node->signature, sizeof (h));
28202 return h;
28203 }
28204
28205 inline bool
28206 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28207 const comdat_type_node *type_node_2)
28208 {
28209 return (! memcmp (type_node_1->signature, type_node_2->signature,
28210 DWARF_TYPE_SIGNATURE_SIZE));
28211 }
28212
28213 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28214 to the location it would have been added, should we know its
28215 DECL_ASSEMBLER_NAME when we added other attributes. This will
28216 probably improve compactness of debug info, removing equivalent
28217 abbrevs, and hide any differences caused by deferring the
28218 computation of the assembler name, triggered by e.g. PCH. */
28219
28220 static inline void
28221 move_linkage_attr (dw_die_ref die)
28222 {
28223 unsigned ix = vec_safe_length (die->die_attr);
28224 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28225
28226 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28227 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28228
28229 while (--ix > 0)
28230 {
28231 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28232
28233 if (prev->dw_attr == DW_AT_decl_line
28234 || prev->dw_attr == DW_AT_decl_column
28235 || prev->dw_attr == DW_AT_name)
28236 break;
28237 }
28238
28239 if (ix != vec_safe_length (die->die_attr) - 1)
28240 {
28241 die->die_attr->pop ();
28242 die->die_attr->quick_insert (ix, linkage);
28243 }
28244 }
28245
28246 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28247 referenced from typed stack ops and count how often they are used. */
28248
28249 static void
28250 mark_base_types (dw_loc_descr_ref loc)
28251 {
28252 dw_die_ref base_type = NULL;
28253
28254 for (; loc; loc = loc->dw_loc_next)
28255 {
28256 switch (loc->dw_loc_opc)
28257 {
28258 case DW_OP_regval_type:
28259 case DW_OP_deref_type:
28260 case DW_OP_GNU_regval_type:
28261 case DW_OP_GNU_deref_type:
28262 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28263 break;
28264 case DW_OP_convert:
28265 case DW_OP_reinterpret:
28266 case DW_OP_GNU_convert:
28267 case DW_OP_GNU_reinterpret:
28268 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28269 continue;
28270 /* FALLTHRU */
28271 case DW_OP_const_type:
28272 case DW_OP_GNU_const_type:
28273 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28274 break;
28275 case DW_OP_entry_value:
28276 case DW_OP_GNU_entry_value:
28277 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28278 continue;
28279 default:
28280 continue;
28281 }
28282 gcc_assert (base_type->die_parent == comp_unit_die ());
28283 if (base_type->die_mark)
28284 base_type->die_mark++;
28285 else
28286 {
28287 base_types.safe_push (base_type);
28288 base_type->die_mark = 1;
28289 }
28290 }
28291 }
28292
28293 /* Comparison function for sorting marked base types. */
28294
28295 static int
28296 base_type_cmp (const void *x, const void *y)
28297 {
28298 dw_die_ref dx = *(const dw_die_ref *) x;
28299 dw_die_ref dy = *(const dw_die_ref *) y;
28300 unsigned int byte_size1, byte_size2;
28301 unsigned int encoding1, encoding2;
28302 unsigned int align1, align2;
28303 if (dx->die_mark > dy->die_mark)
28304 return -1;
28305 if (dx->die_mark < dy->die_mark)
28306 return 1;
28307 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28308 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28309 if (byte_size1 < byte_size2)
28310 return 1;
28311 if (byte_size1 > byte_size2)
28312 return -1;
28313 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28314 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28315 if (encoding1 < encoding2)
28316 return 1;
28317 if (encoding1 > encoding2)
28318 return -1;
28319 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28320 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28321 if (align1 < align2)
28322 return 1;
28323 if (align1 > align2)
28324 return -1;
28325 return 0;
28326 }
28327
28328 /* Move base types marked by mark_base_types as early as possible
28329 in the CU, sorted by decreasing usage count both to make the
28330 uleb128 references as small as possible and to make sure they
28331 will have die_offset already computed by calc_die_sizes when
28332 sizes of typed stack loc ops is computed. */
28333
28334 static void
28335 move_marked_base_types (void)
28336 {
28337 unsigned int i;
28338 dw_die_ref base_type, die, c;
28339
28340 if (base_types.is_empty ())
28341 return;
28342
28343 /* Sort by decreasing usage count, they will be added again in that
28344 order later on. */
28345 base_types.qsort (base_type_cmp);
28346 die = comp_unit_die ();
28347 c = die->die_child;
28348 do
28349 {
28350 dw_die_ref prev = c;
28351 c = c->die_sib;
28352 while (c->die_mark)
28353 {
28354 remove_child_with_prev (c, prev);
28355 /* As base types got marked, there must be at least
28356 one node other than DW_TAG_base_type. */
28357 gcc_assert (die->die_child != NULL);
28358 c = prev->die_sib;
28359 }
28360 }
28361 while (c != die->die_child);
28362 gcc_assert (die->die_child);
28363 c = die->die_child;
28364 for (i = 0; base_types.iterate (i, &base_type); i++)
28365 {
28366 base_type->die_mark = 0;
28367 base_type->die_sib = c->die_sib;
28368 c->die_sib = base_type;
28369 c = base_type;
28370 }
28371 }
28372
28373 /* Helper function for resolve_addr, attempt to resolve
28374 one CONST_STRING, return true if successful. Similarly verify that
28375 SYMBOL_REFs refer to variables emitted in the current CU. */
28376
28377 static bool
28378 resolve_one_addr (rtx *addr)
28379 {
28380 rtx rtl = *addr;
28381
28382 if (GET_CODE (rtl) == CONST_STRING)
28383 {
28384 size_t len = strlen (XSTR (rtl, 0)) + 1;
28385 tree t = build_string (len, XSTR (rtl, 0));
28386 tree tlen = size_int (len - 1);
28387 TREE_TYPE (t)
28388 = build_array_type (char_type_node, build_index_type (tlen));
28389 rtl = lookup_constant_def (t);
28390 if (!rtl || !MEM_P (rtl))
28391 return false;
28392 rtl = XEXP (rtl, 0);
28393 if (GET_CODE (rtl) == SYMBOL_REF
28394 && SYMBOL_REF_DECL (rtl)
28395 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28396 return false;
28397 vec_safe_push (used_rtx_array, rtl);
28398 *addr = rtl;
28399 return true;
28400 }
28401
28402 if (GET_CODE (rtl) == SYMBOL_REF
28403 && SYMBOL_REF_DECL (rtl))
28404 {
28405 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28406 {
28407 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28408 return false;
28409 }
28410 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28411 return false;
28412 }
28413
28414 if (GET_CODE (rtl) == CONST)
28415 {
28416 subrtx_ptr_iterator::array_type array;
28417 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28418 if (!resolve_one_addr (*iter))
28419 return false;
28420 }
28421
28422 return true;
28423 }
28424
28425 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28426 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28427 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28428
28429 static rtx
28430 string_cst_pool_decl (tree t)
28431 {
28432 rtx rtl = output_constant_def (t, 1);
28433 unsigned char *array;
28434 dw_loc_descr_ref l;
28435 tree decl;
28436 size_t len;
28437 dw_die_ref ref;
28438
28439 if (!rtl || !MEM_P (rtl))
28440 return NULL_RTX;
28441 rtl = XEXP (rtl, 0);
28442 if (GET_CODE (rtl) != SYMBOL_REF
28443 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28444 return NULL_RTX;
28445
28446 decl = SYMBOL_REF_DECL (rtl);
28447 if (!lookup_decl_die (decl))
28448 {
28449 len = TREE_STRING_LENGTH (t);
28450 vec_safe_push (used_rtx_array, rtl);
28451 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28452 array = ggc_vec_alloc<unsigned char> (len);
28453 memcpy (array, TREE_STRING_POINTER (t), len);
28454 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28455 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28456 l->dw_loc_oprnd2.v.val_vec.length = len;
28457 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28458 l->dw_loc_oprnd2.v.val_vec.array = array;
28459 add_AT_loc (ref, DW_AT_location, l);
28460 equate_decl_number_to_die (decl, ref);
28461 }
28462 return rtl;
28463 }
28464
28465 /* Helper function of resolve_addr_in_expr. LOC is
28466 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28467 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28468 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28469 with DW_OP_implicit_pointer if possible
28470 and return true, if unsuccessful, return false. */
28471
28472 static bool
28473 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28474 {
28475 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28476 HOST_WIDE_INT offset = 0;
28477 dw_die_ref ref = NULL;
28478 tree decl;
28479
28480 if (GET_CODE (rtl) == CONST
28481 && GET_CODE (XEXP (rtl, 0)) == PLUS
28482 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28483 {
28484 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28485 rtl = XEXP (XEXP (rtl, 0), 0);
28486 }
28487 if (GET_CODE (rtl) == CONST_STRING)
28488 {
28489 size_t len = strlen (XSTR (rtl, 0)) + 1;
28490 tree t = build_string (len, XSTR (rtl, 0));
28491 tree tlen = size_int (len - 1);
28492
28493 TREE_TYPE (t)
28494 = build_array_type (char_type_node, build_index_type (tlen));
28495 rtl = string_cst_pool_decl (t);
28496 if (!rtl)
28497 return false;
28498 }
28499 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28500 {
28501 decl = SYMBOL_REF_DECL (rtl);
28502 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28503 {
28504 ref = lookup_decl_die (decl);
28505 if (ref && (get_AT (ref, DW_AT_location)
28506 || get_AT (ref, DW_AT_const_value)))
28507 {
28508 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28509 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28510 loc->dw_loc_oprnd1.val_entry = NULL;
28511 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28512 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28513 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28514 loc->dw_loc_oprnd2.v.val_int = offset;
28515 return true;
28516 }
28517 }
28518 }
28519 return false;
28520 }
28521
28522 /* Helper function for resolve_addr, handle one location
28523 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28524 the location list couldn't be resolved. */
28525
28526 static bool
28527 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28528 {
28529 dw_loc_descr_ref keep = NULL;
28530 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28531 switch (loc->dw_loc_opc)
28532 {
28533 case DW_OP_addr:
28534 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28535 {
28536 if ((prev == NULL
28537 || prev->dw_loc_opc == DW_OP_piece
28538 || prev->dw_loc_opc == DW_OP_bit_piece)
28539 && loc->dw_loc_next
28540 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28541 && (!dwarf_strict || dwarf_version >= 5)
28542 && optimize_one_addr_into_implicit_ptr (loc))
28543 break;
28544 return false;
28545 }
28546 break;
28547 case DW_OP_GNU_addr_index:
28548 case DW_OP_GNU_const_index:
28549 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28550 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28551 {
28552 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28553 if (!resolve_one_addr (&rtl))
28554 return false;
28555 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28556 loc->dw_loc_oprnd1.val_entry
28557 = add_addr_table_entry (rtl, ate_kind_rtx);
28558 }
28559 break;
28560 case DW_OP_const4u:
28561 case DW_OP_const8u:
28562 if (loc->dtprel
28563 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28564 return false;
28565 break;
28566 case DW_OP_plus_uconst:
28567 if (size_of_loc_descr (loc)
28568 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28569 + 1
28570 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28571 {
28572 dw_loc_descr_ref repl
28573 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28574 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28575 add_loc_descr (&repl, loc->dw_loc_next);
28576 *loc = *repl;
28577 }
28578 break;
28579 case DW_OP_implicit_value:
28580 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28581 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28582 return false;
28583 break;
28584 case DW_OP_implicit_pointer:
28585 case DW_OP_GNU_implicit_pointer:
28586 case DW_OP_GNU_parameter_ref:
28587 case DW_OP_GNU_variable_value:
28588 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28589 {
28590 dw_die_ref ref
28591 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28592 if (ref == NULL)
28593 return false;
28594 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28595 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28596 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28597 }
28598 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28599 {
28600 if (prev == NULL
28601 && loc->dw_loc_next == NULL
28602 && AT_class (a) == dw_val_class_loc)
28603 switch (a->dw_attr)
28604 {
28605 /* Following attributes allow both exprloc and reference,
28606 so if the whole expression is DW_OP_GNU_variable_value
28607 alone we could transform it into reference. */
28608 case DW_AT_byte_size:
28609 case DW_AT_bit_size:
28610 case DW_AT_lower_bound:
28611 case DW_AT_upper_bound:
28612 case DW_AT_bit_stride:
28613 case DW_AT_count:
28614 case DW_AT_allocated:
28615 case DW_AT_associated:
28616 case DW_AT_byte_stride:
28617 a->dw_attr_val.val_class = dw_val_class_die_ref;
28618 a->dw_attr_val.val_entry = NULL;
28619 a->dw_attr_val.v.val_die_ref.die
28620 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28621 a->dw_attr_val.v.val_die_ref.external = 0;
28622 return true;
28623 default:
28624 break;
28625 }
28626 if (dwarf_strict)
28627 return false;
28628 }
28629 break;
28630 case DW_OP_const_type:
28631 case DW_OP_regval_type:
28632 case DW_OP_deref_type:
28633 case DW_OP_convert:
28634 case DW_OP_reinterpret:
28635 case DW_OP_GNU_const_type:
28636 case DW_OP_GNU_regval_type:
28637 case DW_OP_GNU_deref_type:
28638 case DW_OP_GNU_convert:
28639 case DW_OP_GNU_reinterpret:
28640 while (loc->dw_loc_next
28641 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28642 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28643 {
28644 dw_die_ref base1, base2;
28645 unsigned enc1, enc2, size1, size2;
28646 if (loc->dw_loc_opc == DW_OP_regval_type
28647 || loc->dw_loc_opc == DW_OP_deref_type
28648 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28649 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28650 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28651 else if (loc->dw_loc_oprnd1.val_class
28652 == dw_val_class_unsigned_const)
28653 break;
28654 else
28655 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28656 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28657 == dw_val_class_unsigned_const)
28658 break;
28659 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28660 gcc_assert (base1->die_tag == DW_TAG_base_type
28661 && base2->die_tag == DW_TAG_base_type);
28662 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28663 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28664 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28665 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28666 if (size1 == size2
28667 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28668 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28669 && loc != keep)
28670 || enc1 == enc2))
28671 {
28672 /* Optimize away next DW_OP_convert after
28673 adjusting LOC's base type die reference. */
28674 if (loc->dw_loc_opc == DW_OP_regval_type
28675 || loc->dw_loc_opc == DW_OP_deref_type
28676 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28677 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28678 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28679 else
28680 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28681 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28682 continue;
28683 }
28684 /* Don't change integer DW_OP_convert after e.g. floating
28685 point typed stack entry. */
28686 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28687 keep = loc->dw_loc_next;
28688 break;
28689 }
28690 break;
28691 default:
28692 break;
28693 }
28694 return true;
28695 }
28696
28697 /* Helper function of resolve_addr. DIE had DW_AT_location of
28698 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28699 and DW_OP_addr couldn't be resolved. resolve_addr has already
28700 removed the DW_AT_location attribute. This function attempts to
28701 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28702 to it or DW_AT_const_value attribute, if possible. */
28703
28704 static void
28705 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28706 {
28707 if (!VAR_P (decl)
28708 || lookup_decl_die (decl) != die
28709 || DECL_EXTERNAL (decl)
28710 || !TREE_STATIC (decl)
28711 || DECL_INITIAL (decl) == NULL_TREE
28712 || DECL_P (DECL_INITIAL (decl))
28713 || get_AT (die, DW_AT_const_value))
28714 return;
28715
28716 tree init = DECL_INITIAL (decl);
28717 HOST_WIDE_INT offset = 0;
28718 /* For variables that have been optimized away and thus
28719 don't have a memory location, see if we can emit
28720 DW_AT_const_value instead. */
28721 if (tree_add_const_value_attribute (die, init))
28722 return;
28723 if (dwarf_strict && dwarf_version < 5)
28724 return;
28725 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28726 and ADDR_EXPR refers to a decl that has DW_AT_location or
28727 DW_AT_const_value (but isn't addressable, otherwise
28728 resolving the original DW_OP_addr wouldn't fail), see if
28729 we can add DW_OP_implicit_pointer. */
28730 STRIP_NOPS (init);
28731 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28732 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28733 {
28734 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28735 init = TREE_OPERAND (init, 0);
28736 STRIP_NOPS (init);
28737 }
28738 if (TREE_CODE (init) != ADDR_EXPR)
28739 return;
28740 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28741 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28742 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28743 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28744 && TREE_OPERAND (init, 0) != decl))
28745 {
28746 dw_die_ref ref;
28747 dw_loc_descr_ref l;
28748
28749 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28750 {
28751 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28752 if (!rtl)
28753 return;
28754 decl = SYMBOL_REF_DECL (rtl);
28755 }
28756 else
28757 decl = TREE_OPERAND (init, 0);
28758 ref = lookup_decl_die (decl);
28759 if (ref == NULL
28760 || (!get_AT (ref, DW_AT_location)
28761 && !get_AT (ref, DW_AT_const_value)))
28762 return;
28763 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28764 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28765 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28766 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28767 add_AT_loc (die, DW_AT_location, l);
28768 }
28769 }
28770
28771 /* Return NULL if l is a DWARF expression, or first op that is not
28772 valid DWARF expression. */
28773
28774 static dw_loc_descr_ref
28775 non_dwarf_expression (dw_loc_descr_ref l)
28776 {
28777 while (l)
28778 {
28779 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28780 return l;
28781 switch (l->dw_loc_opc)
28782 {
28783 case DW_OP_regx:
28784 case DW_OP_implicit_value:
28785 case DW_OP_stack_value:
28786 case DW_OP_implicit_pointer:
28787 case DW_OP_GNU_implicit_pointer:
28788 case DW_OP_GNU_parameter_ref:
28789 case DW_OP_piece:
28790 case DW_OP_bit_piece:
28791 return l;
28792 default:
28793 break;
28794 }
28795 l = l->dw_loc_next;
28796 }
28797 return NULL;
28798 }
28799
28800 /* Return adjusted copy of EXPR:
28801 If it is empty DWARF expression, return it.
28802 If it is valid non-empty DWARF expression,
28803 return copy of EXPR with DW_OP_deref appended to it.
28804 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28805 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28806 If it is DWARF expression followed by DW_OP_stack_value, return
28807 copy of the DWARF expression without anything appended.
28808 Otherwise, return NULL. */
28809
28810 static dw_loc_descr_ref
28811 copy_deref_exprloc (dw_loc_descr_ref expr)
28812 {
28813 dw_loc_descr_ref tail = NULL;
28814
28815 if (expr == NULL)
28816 return NULL;
28817
28818 dw_loc_descr_ref l = non_dwarf_expression (expr);
28819 if (l && l->dw_loc_next)
28820 return NULL;
28821
28822 if (l)
28823 {
28824 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28825 tail = new_loc_descr ((enum dwarf_location_atom)
28826 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28827 0, 0);
28828 else
28829 switch (l->dw_loc_opc)
28830 {
28831 case DW_OP_regx:
28832 tail = new_loc_descr (DW_OP_bregx,
28833 l->dw_loc_oprnd1.v.val_unsigned, 0);
28834 break;
28835 case DW_OP_stack_value:
28836 break;
28837 default:
28838 return NULL;
28839 }
28840 }
28841 else
28842 tail = new_loc_descr (DW_OP_deref, 0, 0);
28843
28844 dw_loc_descr_ref ret = NULL, *p = &ret;
28845 while (expr != l)
28846 {
28847 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28848 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28849 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28850 p = &(*p)->dw_loc_next;
28851 expr = expr->dw_loc_next;
28852 }
28853 *p = tail;
28854 return ret;
28855 }
28856
28857 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28858 reference to a variable or argument, adjust it if needed and return:
28859 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28860 attribute if present should be removed
28861 0 keep the attribute perhaps with minor modifications, no need to rescan
28862 1 if the attribute has been successfully adjusted. */
28863
28864 static int
28865 optimize_string_length (dw_attr_node *a)
28866 {
28867 dw_loc_descr_ref l = AT_loc (a), lv;
28868 dw_die_ref die;
28869 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28870 {
28871 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28872 die = lookup_decl_die (decl);
28873 if (die)
28874 {
28875 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28876 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28877 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28878 }
28879 else
28880 return -1;
28881 }
28882 else
28883 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28884
28885 /* DWARF5 allows reference class, so we can then reference the DIE.
28886 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28887 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28888 {
28889 a->dw_attr_val.val_class = dw_val_class_die_ref;
28890 a->dw_attr_val.val_entry = NULL;
28891 a->dw_attr_val.v.val_die_ref.die = die;
28892 a->dw_attr_val.v.val_die_ref.external = 0;
28893 return 0;
28894 }
28895
28896 dw_attr_node *av = get_AT (die, DW_AT_location);
28897 dw_loc_list_ref d;
28898 bool non_dwarf_expr = false;
28899
28900 if (av == NULL)
28901 return dwarf_strict ? -1 : 0;
28902 switch (AT_class (av))
28903 {
28904 case dw_val_class_loc_list:
28905 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28906 if (d->expr && non_dwarf_expression (d->expr))
28907 non_dwarf_expr = true;
28908 break;
28909 case dw_val_class_loc:
28910 lv = AT_loc (av);
28911 if (lv == NULL)
28912 return dwarf_strict ? -1 : 0;
28913 if (non_dwarf_expression (lv))
28914 non_dwarf_expr = true;
28915 break;
28916 default:
28917 return dwarf_strict ? -1 : 0;
28918 }
28919
28920 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28921 into DW_OP_call4 or DW_OP_GNU_variable_value into
28922 DW_OP_call4 DW_OP_deref, do so. */
28923 if (!non_dwarf_expr
28924 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28925 {
28926 l->dw_loc_opc = DW_OP_call4;
28927 if (l->dw_loc_next)
28928 l->dw_loc_next = NULL;
28929 else
28930 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28931 return 0;
28932 }
28933
28934 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28935 copy over the DW_AT_location attribute from die to a. */
28936 if (l->dw_loc_next != NULL)
28937 {
28938 a->dw_attr_val = av->dw_attr_val;
28939 return 1;
28940 }
28941
28942 dw_loc_list_ref list, *p;
28943 switch (AT_class (av))
28944 {
28945 case dw_val_class_loc_list:
28946 p = &list;
28947 list = NULL;
28948 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28949 {
28950 lv = copy_deref_exprloc (d->expr);
28951 if (lv)
28952 {
28953 *p = new_loc_list (lv, d->begin, d->end, d->section);
28954 p = &(*p)->dw_loc_next;
28955 }
28956 else if (!dwarf_strict && d->expr)
28957 return 0;
28958 }
28959 if (list == NULL)
28960 return dwarf_strict ? -1 : 0;
28961 a->dw_attr_val.val_class = dw_val_class_loc_list;
28962 gen_llsym (list);
28963 *AT_loc_list_ptr (a) = list;
28964 return 1;
28965 case dw_val_class_loc:
28966 lv = copy_deref_exprloc (AT_loc (av));
28967 if (lv == NULL)
28968 return dwarf_strict ? -1 : 0;
28969 a->dw_attr_val.v.val_loc = lv;
28970 return 1;
28971 default:
28972 gcc_unreachable ();
28973 }
28974 }
28975
28976 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28977 an address in .rodata section if the string literal is emitted there,
28978 or remove the containing location list or replace DW_AT_const_value
28979 with DW_AT_location and empty location expression, if it isn't found
28980 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28981 to something that has been emitted in the current CU. */
28982
28983 static void
28984 resolve_addr (dw_die_ref die)
28985 {
28986 dw_die_ref c;
28987 dw_attr_node *a;
28988 dw_loc_list_ref *curr, *start, loc;
28989 unsigned ix;
28990 bool remove_AT_byte_size = false;
28991
28992 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28993 switch (AT_class (a))
28994 {
28995 case dw_val_class_loc_list:
28996 start = curr = AT_loc_list_ptr (a);
28997 loc = *curr;
28998 gcc_assert (loc);
28999 /* The same list can be referenced more than once. See if we have
29000 already recorded the result from a previous pass. */
29001 if (loc->replaced)
29002 *curr = loc->dw_loc_next;
29003 else if (!loc->resolved_addr)
29004 {
29005 /* As things stand, we do not expect or allow one die to
29006 reference a suffix of another die's location list chain.
29007 References must be identical or completely separate.
29008 There is therefore no need to cache the result of this
29009 pass on any list other than the first; doing so
29010 would lead to unnecessary writes. */
29011 while (*curr)
29012 {
29013 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29014 if (!resolve_addr_in_expr (a, (*curr)->expr))
29015 {
29016 dw_loc_list_ref next = (*curr)->dw_loc_next;
29017 dw_loc_descr_ref l = (*curr)->expr;
29018
29019 if (next && (*curr)->ll_symbol)
29020 {
29021 gcc_assert (!next->ll_symbol);
29022 next->ll_symbol = (*curr)->ll_symbol;
29023 }
29024 if (dwarf_split_debug_info)
29025 remove_loc_list_addr_table_entries (l);
29026 *curr = next;
29027 }
29028 else
29029 {
29030 mark_base_types ((*curr)->expr);
29031 curr = &(*curr)->dw_loc_next;
29032 }
29033 }
29034 if (loc == *start)
29035 loc->resolved_addr = 1;
29036 else
29037 {
29038 loc->replaced = 1;
29039 loc->dw_loc_next = *start;
29040 }
29041 }
29042 if (!*start)
29043 {
29044 remove_AT (die, a->dw_attr);
29045 ix--;
29046 }
29047 break;
29048 case dw_val_class_loc:
29049 {
29050 dw_loc_descr_ref l = AT_loc (a);
29051 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29052 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29053 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29054 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29055 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29056 with DW_FORM_ref referencing the same DIE as
29057 DW_OP_GNU_variable_value used to reference. */
29058 if (a->dw_attr == DW_AT_string_length
29059 && l
29060 && l->dw_loc_opc == DW_OP_GNU_variable_value
29061 && (l->dw_loc_next == NULL
29062 || (l->dw_loc_next->dw_loc_next == NULL
29063 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29064 {
29065 switch (optimize_string_length (a))
29066 {
29067 case -1:
29068 remove_AT (die, a->dw_attr);
29069 ix--;
29070 /* If we drop DW_AT_string_length, we need to drop also
29071 DW_AT_{string_length_,}byte_size. */
29072 remove_AT_byte_size = true;
29073 continue;
29074 default:
29075 break;
29076 case 1:
29077 /* Even if we keep the optimized DW_AT_string_length,
29078 it might have changed AT_class, so process it again. */
29079 ix--;
29080 continue;
29081 }
29082 }
29083 /* For -gdwarf-2 don't attempt to optimize
29084 DW_AT_data_member_location containing
29085 DW_OP_plus_uconst - older consumers might
29086 rely on it being that op instead of a more complex,
29087 but shorter, location description. */
29088 if ((dwarf_version > 2
29089 || a->dw_attr != DW_AT_data_member_location
29090 || l == NULL
29091 || l->dw_loc_opc != DW_OP_plus_uconst
29092 || l->dw_loc_next != NULL)
29093 && !resolve_addr_in_expr (a, l))
29094 {
29095 if (dwarf_split_debug_info)
29096 remove_loc_list_addr_table_entries (l);
29097 if (l != NULL
29098 && l->dw_loc_next == NULL
29099 && l->dw_loc_opc == DW_OP_addr
29100 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29101 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29102 && a->dw_attr == DW_AT_location)
29103 {
29104 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29105 remove_AT (die, a->dw_attr);
29106 ix--;
29107 optimize_location_into_implicit_ptr (die, decl);
29108 break;
29109 }
29110 if (a->dw_attr == DW_AT_string_length)
29111 /* If we drop DW_AT_string_length, we need to drop also
29112 DW_AT_{string_length_,}byte_size. */
29113 remove_AT_byte_size = true;
29114 remove_AT (die, a->dw_attr);
29115 ix--;
29116 }
29117 else
29118 mark_base_types (l);
29119 }
29120 break;
29121 case dw_val_class_addr:
29122 if (a->dw_attr == DW_AT_const_value
29123 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29124 {
29125 if (AT_index (a) != NOT_INDEXED)
29126 remove_addr_table_entry (a->dw_attr_val.val_entry);
29127 remove_AT (die, a->dw_attr);
29128 ix--;
29129 }
29130 if ((die->die_tag == DW_TAG_call_site
29131 && a->dw_attr == DW_AT_call_origin)
29132 || (die->die_tag == DW_TAG_GNU_call_site
29133 && a->dw_attr == DW_AT_abstract_origin))
29134 {
29135 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29136 dw_die_ref tdie = lookup_decl_die (tdecl);
29137 dw_die_ref cdie;
29138 if (tdie == NULL
29139 && DECL_EXTERNAL (tdecl)
29140 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29141 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29142 {
29143 dw_die_ref pdie = cdie;
29144 /* Make sure we don't add these DIEs into type units.
29145 We could emit skeleton DIEs for context (namespaces,
29146 outer structs/classes) and a skeleton DIE for the
29147 innermost context with DW_AT_signature pointing to the
29148 type unit. See PR78835. */
29149 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29150 pdie = pdie->die_parent;
29151 if (pdie == NULL)
29152 {
29153 /* Creating a full DIE for tdecl is overly expensive and
29154 at this point even wrong when in the LTO phase
29155 as it can end up generating new type DIEs we didn't
29156 output and thus optimize_external_refs will crash. */
29157 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29158 add_AT_flag (tdie, DW_AT_external, 1);
29159 add_AT_flag (tdie, DW_AT_declaration, 1);
29160 add_linkage_attr (tdie, tdecl);
29161 add_name_and_src_coords_attributes (tdie, tdecl, true);
29162 equate_decl_number_to_die (tdecl, tdie);
29163 }
29164 }
29165 if (tdie)
29166 {
29167 a->dw_attr_val.val_class = dw_val_class_die_ref;
29168 a->dw_attr_val.v.val_die_ref.die = tdie;
29169 a->dw_attr_val.v.val_die_ref.external = 0;
29170 }
29171 else
29172 {
29173 if (AT_index (a) != NOT_INDEXED)
29174 remove_addr_table_entry (a->dw_attr_val.val_entry);
29175 remove_AT (die, a->dw_attr);
29176 ix--;
29177 }
29178 }
29179 break;
29180 default:
29181 break;
29182 }
29183
29184 if (remove_AT_byte_size)
29185 remove_AT (die, dwarf_version >= 5
29186 ? DW_AT_string_length_byte_size
29187 : DW_AT_byte_size);
29188
29189 FOR_EACH_CHILD (die, c, resolve_addr (c));
29190 }
29191 \f
29192 /* Helper routines for optimize_location_lists.
29193 This pass tries to share identical local lists in .debug_loc
29194 section. */
29195
29196 /* Iteratively hash operands of LOC opcode into HSTATE. */
29197
29198 static void
29199 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29200 {
29201 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29202 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29203
29204 switch (loc->dw_loc_opc)
29205 {
29206 case DW_OP_const4u:
29207 case DW_OP_const8u:
29208 if (loc->dtprel)
29209 goto hash_addr;
29210 /* FALLTHRU */
29211 case DW_OP_const1u:
29212 case DW_OP_const1s:
29213 case DW_OP_const2u:
29214 case DW_OP_const2s:
29215 case DW_OP_const4s:
29216 case DW_OP_const8s:
29217 case DW_OP_constu:
29218 case DW_OP_consts:
29219 case DW_OP_pick:
29220 case DW_OP_plus_uconst:
29221 case DW_OP_breg0:
29222 case DW_OP_breg1:
29223 case DW_OP_breg2:
29224 case DW_OP_breg3:
29225 case DW_OP_breg4:
29226 case DW_OP_breg5:
29227 case DW_OP_breg6:
29228 case DW_OP_breg7:
29229 case DW_OP_breg8:
29230 case DW_OP_breg9:
29231 case DW_OP_breg10:
29232 case DW_OP_breg11:
29233 case DW_OP_breg12:
29234 case DW_OP_breg13:
29235 case DW_OP_breg14:
29236 case DW_OP_breg15:
29237 case DW_OP_breg16:
29238 case DW_OP_breg17:
29239 case DW_OP_breg18:
29240 case DW_OP_breg19:
29241 case DW_OP_breg20:
29242 case DW_OP_breg21:
29243 case DW_OP_breg22:
29244 case DW_OP_breg23:
29245 case DW_OP_breg24:
29246 case DW_OP_breg25:
29247 case DW_OP_breg26:
29248 case DW_OP_breg27:
29249 case DW_OP_breg28:
29250 case DW_OP_breg29:
29251 case DW_OP_breg30:
29252 case DW_OP_breg31:
29253 case DW_OP_regx:
29254 case DW_OP_fbreg:
29255 case DW_OP_piece:
29256 case DW_OP_deref_size:
29257 case DW_OP_xderef_size:
29258 hstate.add_object (val1->v.val_int);
29259 break;
29260 case DW_OP_skip:
29261 case DW_OP_bra:
29262 {
29263 int offset;
29264
29265 gcc_assert (val1->val_class == dw_val_class_loc);
29266 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29267 hstate.add_object (offset);
29268 }
29269 break;
29270 case DW_OP_implicit_value:
29271 hstate.add_object (val1->v.val_unsigned);
29272 switch (val2->val_class)
29273 {
29274 case dw_val_class_const:
29275 hstate.add_object (val2->v.val_int);
29276 break;
29277 case dw_val_class_vec:
29278 {
29279 unsigned int elt_size = val2->v.val_vec.elt_size;
29280 unsigned int len = val2->v.val_vec.length;
29281
29282 hstate.add_int (elt_size);
29283 hstate.add_int (len);
29284 hstate.add (val2->v.val_vec.array, len * elt_size);
29285 }
29286 break;
29287 case dw_val_class_const_double:
29288 hstate.add_object (val2->v.val_double.low);
29289 hstate.add_object (val2->v.val_double.high);
29290 break;
29291 case dw_val_class_wide_int:
29292 hstate.add (val2->v.val_wide->get_val (),
29293 get_full_len (*val2->v.val_wide)
29294 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29295 break;
29296 case dw_val_class_addr:
29297 inchash::add_rtx (val2->v.val_addr, hstate);
29298 break;
29299 default:
29300 gcc_unreachable ();
29301 }
29302 break;
29303 case DW_OP_bregx:
29304 case DW_OP_bit_piece:
29305 hstate.add_object (val1->v.val_int);
29306 hstate.add_object (val2->v.val_int);
29307 break;
29308 case DW_OP_addr:
29309 hash_addr:
29310 if (loc->dtprel)
29311 {
29312 unsigned char dtprel = 0xd1;
29313 hstate.add_object (dtprel);
29314 }
29315 inchash::add_rtx (val1->v.val_addr, hstate);
29316 break;
29317 case DW_OP_GNU_addr_index:
29318 case DW_OP_GNU_const_index:
29319 {
29320 if (loc->dtprel)
29321 {
29322 unsigned char dtprel = 0xd1;
29323 hstate.add_object (dtprel);
29324 }
29325 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29326 }
29327 break;
29328 case DW_OP_implicit_pointer:
29329 case DW_OP_GNU_implicit_pointer:
29330 hstate.add_int (val2->v.val_int);
29331 break;
29332 case DW_OP_entry_value:
29333 case DW_OP_GNU_entry_value:
29334 hstate.add_object (val1->v.val_loc);
29335 break;
29336 case DW_OP_regval_type:
29337 case DW_OP_deref_type:
29338 case DW_OP_GNU_regval_type:
29339 case DW_OP_GNU_deref_type:
29340 {
29341 unsigned int byte_size
29342 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29343 unsigned int encoding
29344 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29345 hstate.add_object (val1->v.val_int);
29346 hstate.add_object (byte_size);
29347 hstate.add_object (encoding);
29348 }
29349 break;
29350 case DW_OP_convert:
29351 case DW_OP_reinterpret:
29352 case DW_OP_GNU_convert:
29353 case DW_OP_GNU_reinterpret:
29354 if (val1->val_class == dw_val_class_unsigned_const)
29355 {
29356 hstate.add_object (val1->v.val_unsigned);
29357 break;
29358 }
29359 /* FALLTHRU */
29360 case DW_OP_const_type:
29361 case DW_OP_GNU_const_type:
29362 {
29363 unsigned int byte_size
29364 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29365 unsigned int encoding
29366 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29367 hstate.add_object (byte_size);
29368 hstate.add_object (encoding);
29369 if (loc->dw_loc_opc != DW_OP_const_type
29370 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29371 break;
29372 hstate.add_object (val2->val_class);
29373 switch (val2->val_class)
29374 {
29375 case dw_val_class_const:
29376 hstate.add_object (val2->v.val_int);
29377 break;
29378 case dw_val_class_vec:
29379 {
29380 unsigned int elt_size = val2->v.val_vec.elt_size;
29381 unsigned int len = val2->v.val_vec.length;
29382
29383 hstate.add_object (elt_size);
29384 hstate.add_object (len);
29385 hstate.add (val2->v.val_vec.array, len * elt_size);
29386 }
29387 break;
29388 case dw_val_class_const_double:
29389 hstate.add_object (val2->v.val_double.low);
29390 hstate.add_object (val2->v.val_double.high);
29391 break;
29392 case dw_val_class_wide_int:
29393 hstate.add (val2->v.val_wide->get_val (),
29394 get_full_len (*val2->v.val_wide)
29395 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29396 break;
29397 default:
29398 gcc_unreachable ();
29399 }
29400 }
29401 break;
29402
29403 default:
29404 /* Other codes have no operands. */
29405 break;
29406 }
29407 }
29408
29409 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29410
29411 static inline void
29412 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29413 {
29414 dw_loc_descr_ref l;
29415 bool sizes_computed = false;
29416 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29417 size_of_locs (loc);
29418
29419 for (l = loc; l != NULL; l = l->dw_loc_next)
29420 {
29421 enum dwarf_location_atom opc = l->dw_loc_opc;
29422 hstate.add_object (opc);
29423 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29424 {
29425 size_of_locs (loc);
29426 sizes_computed = true;
29427 }
29428 hash_loc_operands (l, hstate);
29429 }
29430 }
29431
29432 /* Compute hash of the whole location list LIST_HEAD. */
29433
29434 static inline void
29435 hash_loc_list (dw_loc_list_ref list_head)
29436 {
29437 dw_loc_list_ref curr = list_head;
29438 inchash::hash hstate;
29439
29440 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29441 {
29442 hstate.add (curr->begin, strlen (curr->begin) + 1);
29443 hstate.add (curr->end, strlen (curr->end) + 1);
29444 if (curr->section)
29445 hstate.add (curr->section, strlen (curr->section) + 1);
29446 hash_locs (curr->expr, hstate);
29447 }
29448 list_head->hash = hstate.end ();
29449 }
29450
29451 /* Return true if X and Y opcodes have the same operands. */
29452
29453 static inline bool
29454 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29455 {
29456 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29457 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29458 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29459 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29460
29461 switch (x->dw_loc_opc)
29462 {
29463 case DW_OP_const4u:
29464 case DW_OP_const8u:
29465 if (x->dtprel)
29466 goto hash_addr;
29467 /* FALLTHRU */
29468 case DW_OP_const1u:
29469 case DW_OP_const1s:
29470 case DW_OP_const2u:
29471 case DW_OP_const2s:
29472 case DW_OP_const4s:
29473 case DW_OP_const8s:
29474 case DW_OP_constu:
29475 case DW_OP_consts:
29476 case DW_OP_pick:
29477 case DW_OP_plus_uconst:
29478 case DW_OP_breg0:
29479 case DW_OP_breg1:
29480 case DW_OP_breg2:
29481 case DW_OP_breg3:
29482 case DW_OP_breg4:
29483 case DW_OP_breg5:
29484 case DW_OP_breg6:
29485 case DW_OP_breg7:
29486 case DW_OP_breg8:
29487 case DW_OP_breg9:
29488 case DW_OP_breg10:
29489 case DW_OP_breg11:
29490 case DW_OP_breg12:
29491 case DW_OP_breg13:
29492 case DW_OP_breg14:
29493 case DW_OP_breg15:
29494 case DW_OP_breg16:
29495 case DW_OP_breg17:
29496 case DW_OP_breg18:
29497 case DW_OP_breg19:
29498 case DW_OP_breg20:
29499 case DW_OP_breg21:
29500 case DW_OP_breg22:
29501 case DW_OP_breg23:
29502 case DW_OP_breg24:
29503 case DW_OP_breg25:
29504 case DW_OP_breg26:
29505 case DW_OP_breg27:
29506 case DW_OP_breg28:
29507 case DW_OP_breg29:
29508 case DW_OP_breg30:
29509 case DW_OP_breg31:
29510 case DW_OP_regx:
29511 case DW_OP_fbreg:
29512 case DW_OP_piece:
29513 case DW_OP_deref_size:
29514 case DW_OP_xderef_size:
29515 return valx1->v.val_int == valy1->v.val_int;
29516 case DW_OP_skip:
29517 case DW_OP_bra:
29518 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29519 can cause irrelevant differences in dw_loc_addr. */
29520 gcc_assert (valx1->val_class == dw_val_class_loc
29521 && valy1->val_class == dw_val_class_loc
29522 && (dwarf_split_debug_info
29523 || x->dw_loc_addr == y->dw_loc_addr));
29524 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29525 case DW_OP_implicit_value:
29526 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29527 || valx2->val_class != valy2->val_class)
29528 return false;
29529 switch (valx2->val_class)
29530 {
29531 case dw_val_class_const:
29532 return valx2->v.val_int == valy2->v.val_int;
29533 case dw_val_class_vec:
29534 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29535 && valx2->v.val_vec.length == valy2->v.val_vec.length
29536 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29537 valx2->v.val_vec.elt_size
29538 * valx2->v.val_vec.length) == 0;
29539 case dw_val_class_const_double:
29540 return valx2->v.val_double.low == valy2->v.val_double.low
29541 && valx2->v.val_double.high == valy2->v.val_double.high;
29542 case dw_val_class_wide_int:
29543 return *valx2->v.val_wide == *valy2->v.val_wide;
29544 case dw_val_class_addr:
29545 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29546 default:
29547 gcc_unreachable ();
29548 }
29549 case DW_OP_bregx:
29550 case DW_OP_bit_piece:
29551 return valx1->v.val_int == valy1->v.val_int
29552 && valx2->v.val_int == valy2->v.val_int;
29553 case DW_OP_addr:
29554 hash_addr:
29555 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29556 case DW_OP_GNU_addr_index:
29557 case DW_OP_GNU_const_index:
29558 {
29559 rtx ax1 = valx1->val_entry->addr.rtl;
29560 rtx ay1 = valy1->val_entry->addr.rtl;
29561 return rtx_equal_p (ax1, ay1);
29562 }
29563 case DW_OP_implicit_pointer:
29564 case DW_OP_GNU_implicit_pointer:
29565 return valx1->val_class == dw_val_class_die_ref
29566 && valx1->val_class == valy1->val_class
29567 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29568 && valx2->v.val_int == valy2->v.val_int;
29569 case DW_OP_entry_value:
29570 case DW_OP_GNU_entry_value:
29571 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29572 case DW_OP_const_type:
29573 case DW_OP_GNU_const_type:
29574 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29575 || valx2->val_class != valy2->val_class)
29576 return false;
29577 switch (valx2->val_class)
29578 {
29579 case dw_val_class_const:
29580 return valx2->v.val_int == valy2->v.val_int;
29581 case dw_val_class_vec:
29582 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29583 && valx2->v.val_vec.length == valy2->v.val_vec.length
29584 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29585 valx2->v.val_vec.elt_size
29586 * valx2->v.val_vec.length) == 0;
29587 case dw_val_class_const_double:
29588 return valx2->v.val_double.low == valy2->v.val_double.low
29589 && valx2->v.val_double.high == valy2->v.val_double.high;
29590 case dw_val_class_wide_int:
29591 return *valx2->v.val_wide == *valy2->v.val_wide;
29592 default:
29593 gcc_unreachable ();
29594 }
29595 case DW_OP_regval_type:
29596 case DW_OP_deref_type:
29597 case DW_OP_GNU_regval_type:
29598 case DW_OP_GNU_deref_type:
29599 return valx1->v.val_int == valy1->v.val_int
29600 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29601 case DW_OP_convert:
29602 case DW_OP_reinterpret:
29603 case DW_OP_GNU_convert:
29604 case DW_OP_GNU_reinterpret:
29605 if (valx1->val_class != valy1->val_class)
29606 return false;
29607 if (valx1->val_class == dw_val_class_unsigned_const)
29608 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29609 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29610 case DW_OP_GNU_parameter_ref:
29611 return valx1->val_class == dw_val_class_die_ref
29612 && valx1->val_class == valy1->val_class
29613 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29614 default:
29615 /* Other codes have no operands. */
29616 return true;
29617 }
29618 }
29619
29620 /* Return true if DWARF location expressions X and Y are the same. */
29621
29622 static inline bool
29623 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29624 {
29625 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29626 if (x->dw_loc_opc != y->dw_loc_opc
29627 || x->dtprel != y->dtprel
29628 || !compare_loc_operands (x, y))
29629 break;
29630 return x == NULL && y == NULL;
29631 }
29632
29633 /* Hashtable helpers. */
29634
29635 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29636 {
29637 static inline hashval_t hash (const dw_loc_list_struct *);
29638 static inline bool equal (const dw_loc_list_struct *,
29639 const dw_loc_list_struct *);
29640 };
29641
29642 /* Return precomputed hash of location list X. */
29643
29644 inline hashval_t
29645 loc_list_hasher::hash (const dw_loc_list_struct *x)
29646 {
29647 return x->hash;
29648 }
29649
29650 /* Return true if location lists A and B are the same. */
29651
29652 inline bool
29653 loc_list_hasher::equal (const dw_loc_list_struct *a,
29654 const dw_loc_list_struct *b)
29655 {
29656 if (a == b)
29657 return 1;
29658 if (a->hash != b->hash)
29659 return 0;
29660 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29661 if (strcmp (a->begin, b->begin) != 0
29662 || strcmp (a->end, b->end) != 0
29663 || (a->section == NULL) != (b->section == NULL)
29664 || (a->section && strcmp (a->section, b->section) != 0)
29665 || !compare_locs (a->expr, b->expr))
29666 break;
29667 return a == NULL && b == NULL;
29668 }
29669
29670 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29671
29672
29673 /* Recursively optimize location lists referenced from DIE
29674 children and share them whenever possible. */
29675
29676 static void
29677 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29678 {
29679 dw_die_ref c;
29680 dw_attr_node *a;
29681 unsigned ix;
29682 dw_loc_list_struct **slot;
29683
29684 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29685 if (AT_class (a) == dw_val_class_loc_list)
29686 {
29687 dw_loc_list_ref list = AT_loc_list (a);
29688 /* TODO: perform some optimizations here, before hashing
29689 it and storing into the hash table. */
29690 hash_loc_list (list);
29691 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29692 if (*slot == NULL)
29693 *slot = list;
29694 else
29695 a->dw_attr_val.v.val_loc_list = *slot;
29696 }
29697
29698 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29699 }
29700
29701
29702 /* Recursively assign each location list a unique index into the debug_addr
29703 section. */
29704
29705 static void
29706 index_location_lists (dw_die_ref die)
29707 {
29708 dw_die_ref c;
29709 dw_attr_node *a;
29710 unsigned ix;
29711
29712 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29713 if (AT_class (a) == dw_val_class_loc_list)
29714 {
29715 dw_loc_list_ref list = AT_loc_list (a);
29716 dw_loc_list_ref curr;
29717 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29718 {
29719 /* Don't index an entry that has already been indexed
29720 or won't be output. */
29721 if (curr->begin_entry != NULL
29722 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29723 continue;
29724
29725 curr->begin_entry
29726 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29727 }
29728 }
29729
29730 FOR_EACH_CHILD (die, c, index_location_lists (c));
29731 }
29732
29733 /* Optimize location lists referenced from DIE
29734 children and share them whenever possible. */
29735
29736 static void
29737 optimize_location_lists (dw_die_ref die)
29738 {
29739 loc_list_hash_type htab (500);
29740 optimize_location_lists_1 (die, &htab);
29741 }
29742 \f
29743 /* Traverse the limbo die list, and add parent/child links. The only
29744 dies without parents that should be here are concrete instances of
29745 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29746 For concrete instances, we can get the parent die from the abstract
29747 instance. */
29748
29749 static void
29750 flush_limbo_die_list (void)
29751 {
29752 limbo_die_node *node;
29753
29754 /* get_context_die calls force_decl_die, which can put new DIEs on the
29755 limbo list in LTO mode when nested functions are put in a different
29756 partition than that of their parent function. */
29757 while ((node = limbo_die_list))
29758 {
29759 dw_die_ref die = node->die;
29760 limbo_die_list = node->next;
29761
29762 if (die->die_parent == NULL)
29763 {
29764 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29765
29766 if (origin && origin->die_parent)
29767 add_child_die (origin->die_parent, die);
29768 else if (is_cu_die (die))
29769 ;
29770 else if (seen_error ())
29771 /* It's OK to be confused by errors in the input. */
29772 add_child_die (comp_unit_die (), die);
29773 else
29774 {
29775 /* In certain situations, the lexical block containing a
29776 nested function can be optimized away, which results
29777 in the nested function die being orphaned. Likewise
29778 with the return type of that nested function. Force
29779 this to be a child of the containing function.
29780
29781 It may happen that even the containing function got fully
29782 inlined and optimized out. In that case we are lost and
29783 assign the empty child. This should not be big issue as
29784 the function is likely unreachable too. */
29785 gcc_assert (node->created_for);
29786
29787 if (DECL_P (node->created_for))
29788 origin = get_context_die (DECL_CONTEXT (node->created_for));
29789 else if (TYPE_P (node->created_for))
29790 origin = scope_die_for (node->created_for, comp_unit_die ());
29791 else
29792 origin = comp_unit_die ();
29793
29794 add_child_die (origin, die);
29795 }
29796 }
29797 }
29798 }
29799
29800 /* Reset DIEs so we can output them again. */
29801
29802 static void
29803 reset_dies (dw_die_ref die)
29804 {
29805 dw_die_ref c;
29806
29807 /* Remove stuff we re-generate. */
29808 die->die_mark = 0;
29809 die->die_offset = 0;
29810 die->die_abbrev = 0;
29811 remove_AT (die, DW_AT_sibling);
29812
29813 FOR_EACH_CHILD (die, c, reset_dies (c));
29814 }
29815
29816 /* Output stuff that dwarf requires at the end of every file,
29817 and generate the DWARF-2 debugging info. */
29818
29819 static void
29820 dwarf2out_finish (const char *)
29821 {
29822 comdat_type_node *ctnode;
29823 dw_die_ref main_comp_unit_die;
29824 unsigned char checksum[16];
29825 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29826
29827 /* Flush out any latecomers to the limbo party. */
29828 flush_limbo_die_list ();
29829
29830 if (flag_checking)
29831 {
29832 verify_die (comp_unit_die ());
29833 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29834 verify_die (node->die);
29835 }
29836
29837 /* We shouldn't have any symbols with delayed asm names for
29838 DIEs generated after early finish. */
29839 gcc_assert (deferred_asm_name == NULL);
29840
29841 gen_remaining_tmpl_value_param_die_attribute ();
29842
29843 if (flag_generate_lto || flag_generate_offload)
29844 {
29845 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29846
29847 /* Prune stuff so that dwarf2out_finish runs successfully
29848 for the fat part of the object. */
29849 reset_dies (comp_unit_die ());
29850 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29851 reset_dies (node->die);
29852
29853 hash_table<comdat_type_hasher> comdat_type_table (100);
29854 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29855 {
29856 comdat_type_node **slot
29857 = comdat_type_table.find_slot (ctnode, INSERT);
29858
29859 /* Don't reset types twice. */
29860 if (*slot != HTAB_EMPTY_ENTRY)
29861 continue;
29862
29863 /* Add a pointer to the line table for the main compilation unit
29864 so that the debugger can make sense of DW_AT_decl_file
29865 attributes. */
29866 if (debug_info_level >= DINFO_LEVEL_TERSE)
29867 reset_dies (ctnode->root_die);
29868
29869 *slot = ctnode;
29870 }
29871
29872 /* Reset die CU symbol so we don't output it twice. */
29873 comp_unit_die ()->die_id.die_symbol = NULL;
29874
29875 /* Remove DW_AT_macro from the early output. */
29876 if (have_macinfo)
29877 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29878
29879 /* Remove indirect string decisions. */
29880 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29881 }
29882
29883 #if ENABLE_ASSERT_CHECKING
29884 {
29885 dw_die_ref die = comp_unit_die (), c;
29886 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29887 }
29888 #endif
29889 resolve_addr (comp_unit_die ());
29890 move_marked_base_types ();
29891
29892 /* Initialize sections and labels used for actual assembler output. */
29893 init_sections_and_labels (false);
29894
29895 /* Traverse the DIE's and add sibling attributes to those DIE's that
29896 have children. */
29897 add_sibling_attributes (comp_unit_die ());
29898 limbo_die_node *node;
29899 for (node = cu_die_list; node; node = node->next)
29900 add_sibling_attributes (node->die);
29901 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29902 add_sibling_attributes (ctnode->root_die);
29903
29904 /* When splitting DWARF info, we put some attributes in the
29905 skeleton compile_unit DIE that remains in the .o, while
29906 most attributes go in the DWO compile_unit_die. */
29907 if (dwarf_split_debug_info)
29908 {
29909 limbo_die_node *cu;
29910 main_comp_unit_die = gen_compile_unit_die (NULL);
29911 if (dwarf_version >= 5)
29912 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29913 cu = limbo_die_list;
29914 gcc_assert (cu->die == main_comp_unit_die);
29915 limbo_die_list = limbo_die_list->next;
29916 cu->next = cu_die_list;
29917 cu_die_list = cu;
29918 }
29919 else
29920 main_comp_unit_die = comp_unit_die ();
29921
29922 /* Output a terminator label for the .text section. */
29923 switch_to_section (text_section);
29924 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29925 if (cold_text_section)
29926 {
29927 switch_to_section (cold_text_section);
29928 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29929 }
29930
29931 /* We can only use the low/high_pc attributes if all of the code was
29932 in .text. */
29933 if (!have_multiple_function_sections
29934 || (dwarf_version < 3 && dwarf_strict))
29935 {
29936 /* Don't add if the CU has no associated code. */
29937 if (text_section_used)
29938 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29939 text_end_label, true);
29940 }
29941 else
29942 {
29943 unsigned fde_idx;
29944 dw_fde_ref fde;
29945 bool range_list_added = false;
29946
29947 if (text_section_used)
29948 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29949 text_end_label, &range_list_added, true);
29950 if (cold_text_section_used)
29951 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29952 cold_end_label, &range_list_added, true);
29953
29954 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29955 {
29956 if (DECL_IGNORED_P (fde->decl))
29957 continue;
29958 if (!fde->in_std_section)
29959 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29960 fde->dw_fde_end, &range_list_added,
29961 true);
29962 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29963 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29964 fde->dw_fde_second_end, &range_list_added,
29965 true);
29966 }
29967
29968 if (range_list_added)
29969 {
29970 /* We need to give .debug_loc and .debug_ranges an appropriate
29971 "base address". Use zero so that these addresses become
29972 absolute. Historically, we've emitted the unexpected
29973 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29974 Emit both to give time for other tools to adapt. */
29975 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29976 if (! dwarf_strict && dwarf_version < 4)
29977 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29978
29979 add_ranges (NULL);
29980 }
29981 }
29982
29983 /* AIX Assembler inserts the length, so adjust the reference to match the
29984 offset expected by debuggers. */
29985 strcpy (dl_section_ref, debug_line_section_label);
29986 if (XCOFF_DEBUGGING_INFO)
29987 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29988
29989 if (debug_info_level >= DINFO_LEVEL_TERSE)
29990 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29991 dl_section_ref);
29992
29993 if (have_macinfo)
29994 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
29995 macinfo_section_label);
29996
29997 if (dwarf_split_debug_info)
29998 {
29999 if (have_location_lists)
30000 {
30001 if (dwarf_version >= 5)
30002 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30003 loc_section_label);
30004 /* optimize_location_lists calculates the size of the lists,
30005 so index them first, and assign indices to the entries.
30006 Although optimize_location_lists will remove entries from
30007 the table, it only does so for duplicates, and therefore
30008 only reduces ref_counts to 1. */
30009 index_location_lists (comp_unit_die ());
30010 }
30011
30012 if (addr_index_table != NULL)
30013 {
30014 unsigned int index = 0;
30015 addr_index_table
30016 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30017 (&index);
30018 }
30019 }
30020
30021 loc_list_idx = 0;
30022 if (have_location_lists)
30023 {
30024 optimize_location_lists (comp_unit_die ());
30025 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30026 if (dwarf_version >= 5 && dwarf_split_debug_info)
30027 assign_location_list_indexes (comp_unit_die ());
30028 }
30029
30030 save_macinfo_strings ();
30031
30032 if (dwarf_split_debug_info)
30033 {
30034 unsigned int index = 0;
30035
30036 /* Add attributes common to skeleton compile_units and
30037 type_units. Because these attributes include strings, it
30038 must be done before freezing the string table. Top-level
30039 skeleton die attrs are added when the skeleton type unit is
30040 created, so ensure it is created by this point. */
30041 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30042 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30043 }
30044
30045 /* Output all of the compilation units. We put the main one last so that
30046 the offsets are available to output_pubnames. */
30047 for (node = cu_die_list; node; node = node->next)
30048 output_comp_unit (node->die, 0, NULL);
30049
30050 hash_table<comdat_type_hasher> comdat_type_table (100);
30051 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30052 {
30053 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30054
30055 /* Don't output duplicate types. */
30056 if (*slot != HTAB_EMPTY_ENTRY)
30057 continue;
30058
30059 /* Add a pointer to the line table for the main compilation unit
30060 so that the debugger can make sense of DW_AT_decl_file
30061 attributes. */
30062 if (debug_info_level >= DINFO_LEVEL_TERSE)
30063 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30064 (!dwarf_split_debug_info
30065 ? dl_section_ref
30066 : debug_skeleton_line_section_label));
30067
30068 output_comdat_type_unit (ctnode);
30069 *slot = ctnode;
30070 }
30071
30072 if (dwarf_split_debug_info)
30073 {
30074 int mark;
30075 struct md5_ctx ctx;
30076
30077 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30078 index_rnglists ();
30079
30080 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30081 md5_init_ctx (&ctx);
30082 mark = 0;
30083 die_checksum (comp_unit_die (), &ctx, &mark);
30084 unmark_all_dies (comp_unit_die ());
30085 md5_finish_ctx (&ctx, checksum);
30086
30087 if (dwarf_version < 5)
30088 {
30089 /* Use the first 8 bytes of the checksum as the dwo_id,
30090 and add it to both comp-unit DIEs. */
30091 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30092 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30093 }
30094
30095 /* Add the base offset of the ranges table to the skeleton
30096 comp-unit DIE. */
30097 if (!vec_safe_is_empty (ranges_table))
30098 {
30099 if (dwarf_version >= 5)
30100 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30101 ranges_base_label);
30102 else
30103 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30104 ranges_section_label);
30105 }
30106
30107 switch_to_section (debug_addr_section);
30108 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30109 output_addr_table ();
30110 }
30111
30112 /* Output the main compilation unit if non-empty or if .debug_macinfo
30113 or .debug_macro will be emitted. */
30114 output_comp_unit (comp_unit_die (), have_macinfo,
30115 dwarf_split_debug_info ? checksum : NULL);
30116
30117 if (dwarf_split_debug_info && info_section_emitted)
30118 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30119
30120 /* Output the abbreviation table. */
30121 if (vec_safe_length (abbrev_die_table) != 1)
30122 {
30123 switch_to_section (debug_abbrev_section);
30124 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30125 output_abbrev_section ();
30126 }
30127
30128 /* Output location list section if necessary. */
30129 if (have_location_lists)
30130 {
30131 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30132 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30133 /* Output the location lists info. */
30134 switch_to_section (debug_loc_section);
30135 if (dwarf_version >= 5)
30136 {
30137 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30138 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30139 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30140 dw2_asm_output_data (4, 0xffffffff,
30141 "Initial length escape value indicating "
30142 "64-bit DWARF extension");
30143 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30144 "Length of Location Lists");
30145 ASM_OUTPUT_LABEL (asm_out_file, l1);
30146 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30147 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30148 dw2_asm_output_data (1, 0, "Segment Size");
30149 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30150 "Offset Entry Count");
30151 }
30152 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30153 if (dwarf_version >= 5 && dwarf_split_debug_info)
30154 {
30155 unsigned int save_loc_list_idx = loc_list_idx;
30156 loc_list_idx = 0;
30157 output_loclists_offsets (comp_unit_die ());
30158 gcc_assert (save_loc_list_idx == loc_list_idx);
30159 }
30160 output_location_lists (comp_unit_die ());
30161 if (dwarf_version >= 5)
30162 ASM_OUTPUT_LABEL (asm_out_file, l2);
30163 }
30164
30165 output_pubtables ();
30166
30167 /* Output the address range information if a CU (.debug_info section)
30168 was emitted. We output an empty table even if we had no functions
30169 to put in it. This because the consumer has no way to tell the
30170 difference between an empty table that we omitted and failure to
30171 generate a table that would have contained data. */
30172 if (info_section_emitted)
30173 {
30174 switch_to_section (debug_aranges_section);
30175 output_aranges ();
30176 }
30177
30178 /* Output ranges section if necessary. */
30179 if (!vec_safe_is_empty (ranges_table))
30180 {
30181 if (dwarf_version >= 5)
30182 output_rnglists ();
30183 else
30184 output_ranges ();
30185 }
30186
30187 /* Have to end the macro section. */
30188 if (have_macinfo)
30189 {
30190 switch_to_section (debug_macinfo_section);
30191 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30192 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30193 : debug_skeleton_line_section_label, false);
30194 dw2_asm_output_data (1, 0, "End compilation unit");
30195 }
30196
30197 /* Output the source line correspondence table. We must do this
30198 even if there is no line information. Otherwise, on an empty
30199 translation unit, we will generate a present, but empty,
30200 .debug_info section. IRIX 6.5 `nm' will then complain when
30201 examining the file. This is done late so that any filenames
30202 used by the debug_info section are marked as 'used'. */
30203 switch_to_section (debug_line_section);
30204 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30205 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30206 output_line_info (false);
30207
30208 if (dwarf_split_debug_info && info_section_emitted)
30209 {
30210 switch_to_section (debug_skeleton_line_section);
30211 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30212 output_line_info (true);
30213 }
30214
30215 /* If we emitted any indirect strings, output the string table too. */
30216 if (debug_str_hash || skeleton_debug_str_hash)
30217 output_indirect_strings ();
30218 if (debug_line_str_hash)
30219 {
30220 switch_to_section (debug_line_str_section);
30221 const enum dwarf_form form = DW_FORM_line_strp;
30222 debug_line_str_hash->traverse<enum dwarf_form,
30223 output_indirect_string> (form);
30224 }
30225 }
30226
30227 /* Returns a hash value for X (which really is a variable_value_struct). */
30228
30229 inline hashval_t
30230 variable_value_hasher::hash (variable_value_struct *x)
30231 {
30232 return (hashval_t) x->decl_id;
30233 }
30234
30235 /* Return nonzero if decl_id of variable_value_struct X is the same as
30236 UID of decl Y. */
30237
30238 inline bool
30239 variable_value_hasher::equal (variable_value_struct *x, tree y)
30240 {
30241 return x->decl_id == DECL_UID (y);
30242 }
30243
30244 /* Helper function for resolve_variable_value, handle
30245 DW_OP_GNU_variable_value in one location expression.
30246 Return true if exprloc has been changed into loclist. */
30247
30248 static bool
30249 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30250 {
30251 dw_loc_descr_ref next;
30252 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30253 {
30254 next = loc->dw_loc_next;
30255 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30256 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30257 continue;
30258
30259 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30260 if (DECL_CONTEXT (decl) != current_function_decl)
30261 continue;
30262
30263 dw_die_ref ref = lookup_decl_die (decl);
30264 if (ref)
30265 {
30266 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30267 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30268 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30269 continue;
30270 }
30271 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30272 if (l == NULL)
30273 continue;
30274 if (l->dw_loc_next)
30275 {
30276 if (AT_class (a) != dw_val_class_loc)
30277 continue;
30278 switch (a->dw_attr)
30279 {
30280 /* Following attributes allow both exprloc and loclist
30281 classes, so we can change them into a loclist. */
30282 case DW_AT_location:
30283 case DW_AT_string_length:
30284 case DW_AT_return_addr:
30285 case DW_AT_data_member_location:
30286 case DW_AT_frame_base:
30287 case DW_AT_segment:
30288 case DW_AT_static_link:
30289 case DW_AT_use_location:
30290 case DW_AT_vtable_elem_location:
30291 if (prev)
30292 {
30293 prev->dw_loc_next = NULL;
30294 prepend_loc_descr_to_each (l, AT_loc (a));
30295 }
30296 if (next)
30297 add_loc_descr_to_each (l, next);
30298 a->dw_attr_val.val_class = dw_val_class_loc_list;
30299 a->dw_attr_val.val_entry = NULL;
30300 a->dw_attr_val.v.val_loc_list = l;
30301 have_location_lists = true;
30302 return true;
30303 /* Following attributes allow both exprloc and reference,
30304 so if the whole expression is DW_OP_GNU_variable_value alone
30305 we could transform it into reference. */
30306 case DW_AT_byte_size:
30307 case DW_AT_bit_size:
30308 case DW_AT_lower_bound:
30309 case DW_AT_upper_bound:
30310 case DW_AT_bit_stride:
30311 case DW_AT_count:
30312 case DW_AT_allocated:
30313 case DW_AT_associated:
30314 case DW_AT_byte_stride:
30315 if (prev == NULL && next == NULL)
30316 break;
30317 /* FALLTHRU */
30318 default:
30319 if (dwarf_strict)
30320 continue;
30321 break;
30322 }
30323 /* Create DW_TAG_variable that we can refer to. */
30324 gen_decl_die (decl, NULL_TREE, NULL,
30325 lookup_decl_die (current_function_decl));
30326 ref = lookup_decl_die (decl);
30327 if (ref)
30328 {
30329 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30330 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30331 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30332 }
30333 continue;
30334 }
30335 if (prev)
30336 {
30337 prev->dw_loc_next = l->expr;
30338 add_loc_descr (&prev->dw_loc_next, next);
30339 free_loc_descr (loc, NULL);
30340 next = prev->dw_loc_next;
30341 }
30342 else
30343 {
30344 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30345 add_loc_descr (&loc, next);
30346 next = loc;
30347 }
30348 loc = prev;
30349 }
30350 return false;
30351 }
30352
30353 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30354
30355 static void
30356 resolve_variable_value (dw_die_ref die)
30357 {
30358 dw_attr_node *a;
30359 dw_loc_list_ref loc;
30360 unsigned ix;
30361
30362 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30363 switch (AT_class (a))
30364 {
30365 case dw_val_class_loc:
30366 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30367 break;
30368 /* FALLTHRU */
30369 case dw_val_class_loc_list:
30370 loc = AT_loc_list (a);
30371 gcc_assert (loc);
30372 for (; loc; loc = loc->dw_loc_next)
30373 resolve_variable_value_in_expr (a, loc->expr);
30374 break;
30375 default:
30376 break;
30377 }
30378 }
30379
30380 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30381 temporaries in the current function. */
30382
30383 static void
30384 resolve_variable_values (void)
30385 {
30386 if (!variable_value_hash || !current_function_decl)
30387 return;
30388
30389 struct variable_value_struct *node
30390 = variable_value_hash->find_with_hash (current_function_decl,
30391 DECL_UID (current_function_decl));
30392
30393 if (node == NULL)
30394 return;
30395
30396 unsigned int i;
30397 dw_die_ref die;
30398 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30399 resolve_variable_value (die);
30400 }
30401
30402 /* Helper function for note_variable_value, handle one location
30403 expression. */
30404
30405 static void
30406 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30407 {
30408 for (; loc; loc = loc->dw_loc_next)
30409 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30410 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30411 {
30412 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30413 dw_die_ref ref = lookup_decl_die (decl);
30414 if (! ref && (flag_generate_lto || flag_generate_offload))
30415 {
30416 /* ??? This is somewhat a hack because we do not create DIEs
30417 for variables not in BLOCK trees early but when generating
30418 early LTO output we need the dw_val_class_decl_ref to be
30419 fully resolved. For fat LTO objects we'd also like to
30420 undo this after LTO dwarf output. */
30421 gcc_assert (DECL_CONTEXT (decl));
30422 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30423 gcc_assert (ctx != NULL);
30424 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30425 ref = lookup_decl_die (decl);
30426 gcc_assert (ref != NULL);
30427 }
30428 if (ref)
30429 {
30430 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30431 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30432 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30433 continue;
30434 }
30435 if (VAR_P (decl)
30436 && DECL_CONTEXT (decl)
30437 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30438 && lookup_decl_die (DECL_CONTEXT (decl)))
30439 {
30440 if (!variable_value_hash)
30441 variable_value_hash
30442 = hash_table<variable_value_hasher>::create_ggc (10);
30443
30444 tree fndecl = DECL_CONTEXT (decl);
30445 struct variable_value_struct *node;
30446 struct variable_value_struct **slot
30447 = variable_value_hash->find_slot_with_hash (fndecl,
30448 DECL_UID (fndecl),
30449 INSERT);
30450 if (*slot == NULL)
30451 {
30452 node = ggc_cleared_alloc<variable_value_struct> ();
30453 node->decl_id = DECL_UID (fndecl);
30454 *slot = node;
30455 }
30456 else
30457 node = *slot;
30458
30459 vec_safe_push (node->dies, die);
30460 }
30461 }
30462 }
30463
30464 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30465 with dw_val_class_decl_ref operand. */
30466
30467 static void
30468 note_variable_value (dw_die_ref die)
30469 {
30470 dw_die_ref c;
30471 dw_attr_node *a;
30472 dw_loc_list_ref loc;
30473 unsigned ix;
30474
30475 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30476 switch (AT_class (a))
30477 {
30478 case dw_val_class_loc_list:
30479 loc = AT_loc_list (a);
30480 gcc_assert (loc);
30481 if (!loc->noted_variable_value)
30482 {
30483 loc->noted_variable_value = 1;
30484 for (; loc; loc = loc->dw_loc_next)
30485 note_variable_value_in_expr (die, loc->expr);
30486 }
30487 break;
30488 case dw_val_class_loc:
30489 note_variable_value_in_expr (die, AT_loc (a));
30490 break;
30491 default:
30492 break;
30493 }
30494
30495 /* Mark children. */
30496 FOR_EACH_CHILD (die, c, note_variable_value (c));
30497 }
30498
30499 /* Perform any cleanups needed after the early debug generation pass
30500 has run. */
30501
30502 static void
30503 dwarf2out_early_finish (const char *filename)
30504 {
30505 set_early_dwarf s;
30506
30507 /* PCH might result in DW_AT_producer string being restored from the
30508 header compilation, so always fill it with empty string initially
30509 and overwrite only here. */
30510 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30511 producer_string = gen_producer_string ();
30512 producer->dw_attr_val.v.val_str->refcount--;
30513 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30514
30515 /* Add the name for the main input file now. We delayed this from
30516 dwarf2out_init to avoid complications with PCH. */
30517 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30518 add_comp_dir_attribute (comp_unit_die ());
30519
30520 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30521 DW_AT_comp_dir into .debug_line_str section. */
30522 if (!DWARF2_ASM_LINE_DEBUG_INFO
30523 && dwarf_version >= 5
30524 && DWARF5_USE_DEBUG_LINE_STR)
30525 {
30526 for (int i = 0; i < 2; i++)
30527 {
30528 dw_attr_node *a = get_AT (comp_unit_die (),
30529 i ? DW_AT_comp_dir : DW_AT_name);
30530 if (a == NULL
30531 || AT_class (a) != dw_val_class_str
30532 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30533 continue;
30534
30535 if (! debug_line_str_hash)
30536 debug_line_str_hash
30537 = hash_table<indirect_string_hasher>::create_ggc (10);
30538
30539 struct indirect_string_node *node
30540 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30541 set_indirect_string (node);
30542 node->form = DW_FORM_line_strp;
30543 a->dw_attr_val.v.val_str->refcount--;
30544 a->dw_attr_val.v.val_str = node;
30545 }
30546 }
30547
30548 /* With LTO early dwarf was really finished at compile-time, so make
30549 sure to adjust the phase after annotating the LTRANS CU DIE. */
30550 if (in_lto_p)
30551 {
30552 early_dwarf_finished = true;
30553 return;
30554 }
30555
30556 /* Walk through the list of incomplete types again, trying once more to
30557 emit full debugging info for them. */
30558 retry_incomplete_types ();
30559
30560 /* The point here is to flush out the limbo list so that it is empty
30561 and we don't need to stream it for LTO. */
30562 flush_limbo_die_list ();
30563
30564 gen_scheduled_generic_parms_dies ();
30565 gen_remaining_tmpl_value_param_die_attribute ();
30566
30567 /* Add DW_AT_linkage_name for all deferred DIEs. */
30568 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30569 {
30570 tree decl = node->created_for;
30571 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30572 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30573 ended up in deferred_asm_name before we knew it was
30574 constant and never written to disk. */
30575 && DECL_ASSEMBLER_NAME (decl))
30576 {
30577 add_linkage_attr (node->die, decl);
30578 move_linkage_attr (node->die);
30579 }
30580 }
30581 deferred_asm_name = NULL;
30582
30583 if (flag_eliminate_unused_debug_types)
30584 prune_unused_types ();
30585
30586 /* Generate separate COMDAT sections for type DIEs. */
30587 if (use_debug_types)
30588 {
30589 break_out_comdat_types (comp_unit_die ());
30590
30591 /* Each new type_unit DIE was added to the limbo die list when created.
30592 Since these have all been added to comdat_type_list, clear the
30593 limbo die list. */
30594 limbo_die_list = NULL;
30595
30596 /* For each new comdat type unit, copy declarations for incomplete
30597 types to make the new unit self-contained (i.e., no direct
30598 references to the main compile unit). */
30599 for (comdat_type_node *ctnode = comdat_type_list;
30600 ctnode != NULL; ctnode = ctnode->next)
30601 copy_decls_for_unworthy_types (ctnode->root_die);
30602 copy_decls_for_unworthy_types (comp_unit_die ());
30603
30604 /* In the process of copying declarations from one unit to another,
30605 we may have left some declarations behind that are no longer
30606 referenced. Prune them. */
30607 prune_unused_types ();
30608 }
30609
30610 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30611 with dw_val_class_decl_ref operand. */
30612 note_variable_value (comp_unit_die ());
30613 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30614 note_variable_value (node->die);
30615 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30616 ctnode = ctnode->next)
30617 note_variable_value (ctnode->root_die);
30618 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30619 note_variable_value (node->die);
30620
30621 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30622 both the main_cu and all skeleton TUs. Making this call unconditional
30623 would end up either adding a second copy of the AT_pubnames attribute, or
30624 requiring a special case in add_top_level_skeleton_die_attrs. */
30625 if (!dwarf_split_debug_info)
30626 add_AT_pubnames (comp_unit_die ());
30627
30628 /* The early debug phase is now finished. */
30629 early_dwarf_finished = true;
30630
30631 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30632 if (!flag_generate_lto && !flag_generate_offload)
30633 return;
30634
30635 /* Now as we are going to output for LTO initialize sections and labels
30636 to the LTO variants. We don't need a random-seed postfix as other
30637 LTO sections as linking the LTO debug sections into one in a partial
30638 link is fine. */
30639 init_sections_and_labels (true);
30640
30641 /* The output below is modeled after dwarf2out_finish with all
30642 location related output removed and some LTO specific changes.
30643 Some refactoring might make both smaller and easier to match up. */
30644
30645 /* Traverse the DIE's and add add sibling attributes to those DIE's
30646 that have children. */
30647 add_sibling_attributes (comp_unit_die ());
30648 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30649 add_sibling_attributes (node->die);
30650 for (comdat_type_node *ctnode = comdat_type_list;
30651 ctnode != NULL; ctnode = ctnode->next)
30652 add_sibling_attributes (ctnode->root_die);
30653
30654 if (have_macinfo)
30655 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30656 macinfo_section_label);
30657
30658 save_macinfo_strings ();
30659
30660 /* Output all of the compilation units. We put the main one last so that
30661 the offsets are available to output_pubnames. */
30662 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30663 output_comp_unit (node->die, 0, NULL);
30664
30665 hash_table<comdat_type_hasher> comdat_type_table (100);
30666 for (comdat_type_node *ctnode = comdat_type_list;
30667 ctnode != NULL; ctnode = ctnode->next)
30668 {
30669 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30670
30671 /* Don't output duplicate types. */
30672 if (*slot != HTAB_EMPTY_ENTRY)
30673 continue;
30674
30675 /* Add a pointer to the line table for the main compilation unit
30676 so that the debugger can make sense of DW_AT_decl_file
30677 attributes. */
30678 if (debug_info_level >= DINFO_LEVEL_TERSE)
30679 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30680 (!dwarf_split_debug_info
30681 ? debug_line_section_label
30682 : debug_skeleton_line_section_label));
30683
30684 output_comdat_type_unit (ctnode);
30685 *slot = ctnode;
30686 }
30687
30688 /* Stick a unique symbol to the main debuginfo section. */
30689 compute_comp_unit_symbol (comp_unit_die ());
30690
30691 /* Output the main compilation unit. We always need it if only for
30692 the CU symbol. */
30693 output_comp_unit (comp_unit_die (), true, NULL);
30694
30695 /* Output the abbreviation table. */
30696 if (vec_safe_length (abbrev_die_table) != 1)
30697 {
30698 switch_to_section (debug_abbrev_section);
30699 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30700 output_abbrev_section ();
30701 }
30702
30703 /* Have to end the macro section. */
30704 if (have_macinfo)
30705 {
30706 /* We have to save macinfo state if we need to output it again
30707 for the FAT part of the object. */
30708 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30709 if (flag_fat_lto_objects)
30710 macinfo_table = macinfo_table->copy ();
30711
30712 switch_to_section (debug_macinfo_section);
30713 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30714 output_macinfo (debug_skeleton_line_section_label, true);
30715 dw2_asm_output_data (1, 0, "End compilation unit");
30716
30717 /* Emit a skeleton debug_line section. */
30718 switch_to_section (debug_skeleton_line_section);
30719 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30720 output_line_info (true);
30721
30722 if (flag_fat_lto_objects)
30723 {
30724 vec_free (macinfo_table);
30725 macinfo_table = saved_macinfo_table;
30726 }
30727 }
30728
30729
30730 /* If we emitted any indirect strings, output the string table too. */
30731 if (debug_str_hash || skeleton_debug_str_hash)
30732 output_indirect_strings ();
30733
30734 /* Switch back to the text section. */
30735 switch_to_section (text_section);
30736 }
30737
30738 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30739 within the same process. For use by toplev::finalize. */
30740
30741 void
30742 dwarf2out_c_finalize (void)
30743 {
30744 last_var_location_insn = NULL;
30745 cached_next_real_insn = NULL;
30746 used_rtx_array = NULL;
30747 incomplete_types = NULL;
30748 decl_scope_table = NULL;
30749 debug_info_section = NULL;
30750 debug_skeleton_info_section = NULL;
30751 debug_abbrev_section = NULL;
30752 debug_skeleton_abbrev_section = NULL;
30753 debug_aranges_section = NULL;
30754 debug_addr_section = NULL;
30755 debug_macinfo_section = NULL;
30756 debug_line_section = NULL;
30757 debug_skeleton_line_section = NULL;
30758 debug_loc_section = NULL;
30759 debug_pubnames_section = NULL;
30760 debug_pubtypes_section = NULL;
30761 debug_str_section = NULL;
30762 debug_line_str_section = NULL;
30763 debug_str_dwo_section = NULL;
30764 debug_str_offsets_section = NULL;
30765 debug_ranges_section = NULL;
30766 debug_frame_section = NULL;
30767 fde_vec = NULL;
30768 debug_str_hash = NULL;
30769 debug_line_str_hash = NULL;
30770 skeleton_debug_str_hash = NULL;
30771 dw2_string_counter = 0;
30772 have_multiple_function_sections = false;
30773 text_section_used = false;
30774 cold_text_section_used = false;
30775 cold_text_section = NULL;
30776 current_unit_personality = NULL;
30777
30778 early_dwarf = false;
30779 early_dwarf_finished = false;
30780
30781 next_die_offset = 0;
30782 single_comp_unit_die = NULL;
30783 comdat_type_list = NULL;
30784 limbo_die_list = NULL;
30785 file_table = NULL;
30786 decl_die_table = NULL;
30787 common_block_die_table = NULL;
30788 decl_loc_table = NULL;
30789 call_arg_locations = NULL;
30790 call_arg_loc_last = NULL;
30791 call_site_count = -1;
30792 tail_call_site_count = -1;
30793 cached_dw_loc_list_table = NULL;
30794 abbrev_die_table = NULL;
30795 delete dwarf_proc_stack_usage_map;
30796 dwarf_proc_stack_usage_map = NULL;
30797 line_info_label_num = 0;
30798 cur_line_info_table = NULL;
30799 text_section_line_info = NULL;
30800 cold_text_section_line_info = NULL;
30801 separate_line_info = NULL;
30802 info_section_emitted = false;
30803 pubname_table = NULL;
30804 pubtype_table = NULL;
30805 macinfo_table = NULL;
30806 ranges_table = NULL;
30807 ranges_by_label = NULL;
30808 rnglist_idx = 0;
30809 have_location_lists = false;
30810 loclabel_num = 0;
30811 poc_label_num = 0;
30812 last_emitted_file = NULL;
30813 label_num = 0;
30814 tmpl_value_parm_die_table = NULL;
30815 generic_type_instances = NULL;
30816 frame_pointer_fb_offset = 0;
30817 frame_pointer_fb_offset_valid = false;
30818 base_types.release ();
30819 XDELETEVEC (producer_string);
30820 producer_string = NULL;
30821 }
30822
30823 #include "gt-dwarf2out.h"