alias.c: Reorder #include statements and remove duplicates.
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "expmed.h"
68 #include "insn-config.h"
69 #include "regs.h"
70 #include "ira.h"
71 #include "cgraph.h"
72 #include "diagnostic.h"
73 #include "alias.h"
74 #include "fold-const.h"
75 #include "stor-layout.h"
76 #include "varasm.h"
77 #include "version.h"
78 #include "flags.h"
79 #include "rtlhash.h"
80 #include "reload.h"
81 #include "output.h"
82 #include "dojump.h"
83 #include "explow.h"
84 #include "calls.h"
85 #include "stmt.h"
86 #include "expr.h"
87 #include "except.h"
88 #include "dwarf2.h"
89 #include "dwarf2out.h"
90 #include "dwarf2asm.h"
91 #include "toplev.h"
92 #include "md5.h"
93 #include "tree-pretty-print.h"
94 #include "debug.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "lra.h"
98 #include "dumpfile.h"
99 #include "opts.h"
100 #include "tree-dfa.h"
101 #include "gdb/gdb-index.h"
102 #include "rtl-iter.h"
103
104 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
105 static rtx_insn *last_var_location_insn;
106 static rtx_insn *cached_next_real_insn;
107 static void dwarf2out_decl (tree);
108
109 #ifndef XCOFF_DEBUGGING_INFO
110 #define XCOFF_DEBUGGING_INFO 0
111 #endif
112
113 #ifndef HAVE_XCOFF_DWARF_EXTRAS
114 #define HAVE_XCOFF_DWARF_EXTRAS 0
115 #endif
116
117 #ifdef VMS_DEBUGGING_INFO
118 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119
120 /* Define this macro to be a nonzero value if the directory specifications
121 which are output in the debug info should end with a separator. */
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
123 /* Define this macro to evaluate to a nonzero value if GCC should refrain
124 from generating indirect strings in DWARF2 debug information, for instance
125 if your target is stuck with an old version of GDB that is unable to
126 process them properly or uses VMS Debug. */
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
128 #else
129 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
130 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
131 #endif
132
133 /* ??? Poison these here until it can be done generically. They've been
134 totally replaced in this file; make sure it stays that way. */
135 #undef DWARF2_UNWIND_INFO
136 #undef DWARF2_FRAME_INFO
137 #if (GCC_VERSION >= 3000)
138 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
139 #endif
140
141 /* The size of the target's pointer type. */
142 #ifndef PTR_SIZE
143 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
144 #endif
145
146 /* Array of RTXes referenced by the debugging information, which therefore
147 must be kept around forever. */
148 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149
150 /* A pointer to the base of a list of incomplete types which might be
151 completed at some later time. incomplete_types_list needs to be a
152 vec<tree, va_gc> *because we want to tell the garbage collector about
153 it. */
154 static GTY(()) vec<tree, va_gc> *incomplete_types;
155
156 /* A pointer to the base of a table of references to declaration
157 scopes. This table is a display which tracks the nesting
158 of declaration scopes at the current scope and containing
159 scopes. This table is used to find the proper place to
160 define type declaration DIE's. */
161 static GTY(()) vec<tree, va_gc> *decl_scope_table;
162
163 /* Pointers to various DWARF2 sections. */
164 static GTY(()) section *debug_info_section;
165 static GTY(()) section *debug_skeleton_info_section;
166 static GTY(()) section *debug_abbrev_section;
167 static GTY(()) section *debug_skeleton_abbrev_section;
168 static GTY(()) section *debug_aranges_section;
169 static GTY(()) section *debug_addr_section;
170 static GTY(()) section *debug_macinfo_section;
171 static GTY(()) section *debug_line_section;
172 static GTY(()) section *debug_skeleton_line_section;
173 static GTY(()) section *debug_loc_section;
174 static GTY(()) section *debug_pubnames_section;
175 static GTY(()) section *debug_pubtypes_section;
176 static GTY(()) section *debug_str_section;
177 static GTY(()) section *debug_str_dwo_section;
178 static GTY(()) section *debug_str_offsets_section;
179 static GTY(()) section *debug_ranges_section;
180 static GTY(()) section *debug_frame_section;
181
182 /* Maximum size (in bytes) of an artificially generated label. */
183 #define MAX_ARTIFICIAL_LABEL_BYTES 30
184
185 /* According to the (draft) DWARF 3 specification, the initial length
186 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
187 bytes are 0xffffffff, followed by the length stored in the next 8
188 bytes.
189
190 However, the SGI/MIPS ABI uses an initial length which is equal to
191 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
192
193 #ifndef DWARF_INITIAL_LENGTH_SIZE
194 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
195 #endif
196
197 /* Round SIZE up to the nearest BOUNDARY. */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
200
201 /* CIE identifier. */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
208
209
210 /* A vector for a table that contains frame description
211 information for each routine. */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
214
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
216
217 struct GTY((for_user)) indirect_string_node {
218 const char *str;
219 unsigned int refcount;
220 enum dwarf_form form;
221 char *label;
222 unsigned int index;
223 };
224
225 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
226 {
227 typedef const char *compare_type;
228
229 static hashval_t hash (indirect_string_node *);
230 static bool equal (indirect_string_node *, const char *);
231 };
232
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
234
235 /* With split_debug_info, both the comp_dir and dwo_name go in the
236 main object file, rather than the dwo, similar to the force_direct
237 parameter elsewhere but with additional complications:
238
239 1) The string is needed in both the main object file and the dwo.
240 That is, the comp_dir and dwo_name will appear in both places.
241
242 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
243 DW_FORM_GNU_str_index.
244
245 3) GCC chooses the form to use late, depending on the size and
246 reference count.
247
248 Rather than forcing the all debug string handling functions and
249 callers to deal with these complications, simply use a separate,
250 special-cased string table for any attribute that should go in the
251 main object file. This limits the complexity to just the places
252 that need it. */
253
254 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
255
256 static GTY(()) int dw2_string_counter;
257
258 /* True if the compilation unit places functions in more than one section. */
259 static GTY(()) bool have_multiple_function_sections = false;
260
261 /* Whether the default text and cold text sections have been used at all. */
262
263 static GTY(()) bool text_section_used = false;
264 static GTY(()) bool cold_text_section_used = false;
265
266 /* The default cold text section. */
267 static GTY(()) section *cold_text_section;
268
269 /* The DIE for C++14 'auto' in a function return type. */
270 static GTY(()) dw_die_ref auto_die;
271
272 /* The DIE for C++14 'decltype(auto)' in a function return type. */
273 static GTY(()) dw_die_ref decltype_auto_die;
274
275 /* Forward declarations for functions defined in this file. */
276
277 static char *stripattributes (const char *);
278 static void output_call_frame_info (int);
279 static void dwarf2out_note_section_used (void);
280
281 /* Personality decl of current unit. Used only when assembler does not support
282 personality CFI. */
283 static GTY(()) rtx current_unit_personality;
284
285 /* Data and reference forms for relocatable data. */
286 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
287 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
288
289 #ifndef DEBUG_FRAME_SECTION
290 #define DEBUG_FRAME_SECTION ".debug_frame"
291 #endif
292
293 #ifndef FUNC_BEGIN_LABEL
294 #define FUNC_BEGIN_LABEL "LFB"
295 #endif
296
297 #ifndef FUNC_END_LABEL
298 #define FUNC_END_LABEL "LFE"
299 #endif
300
301 #ifndef PROLOGUE_END_LABEL
302 #define PROLOGUE_END_LABEL "LPE"
303 #endif
304
305 #ifndef EPILOGUE_BEGIN_LABEL
306 #define EPILOGUE_BEGIN_LABEL "LEB"
307 #endif
308
309 #ifndef FRAME_BEGIN_LABEL
310 #define FRAME_BEGIN_LABEL "Lframe"
311 #endif
312 #define CIE_AFTER_SIZE_LABEL "LSCIE"
313 #define CIE_END_LABEL "LECIE"
314 #define FDE_LABEL "LSFDE"
315 #define FDE_AFTER_SIZE_LABEL "LASFDE"
316 #define FDE_END_LABEL "LEFDE"
317 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
318 #define LINE_NUMBER_END_LABEL "LELT"
319 #define LN_PROLOG_AS_LABEL "LASLTP"
320 #define LN_PROLOG_END_LABEL "LELTP"
321 #define DIE_LABEL_PREFIX "DW"
322 \f
323 /* Match the base name of a file to the base name of a compilation unit. */
324
325 static int
326 matches_main_base (const char *path)
327 {
328 /* Cache the last query. */
329 static const char *last_path = NULL;
330 static int last_match = 0;
331 if (path != last_path)
332 {
333 const char *base;
334 int length = base_of_path (path, &base);
335 last_path = path;
336 last_match = (length == main_input_baselength
337 && memcmp (base, main_input_basename, length) == 0);
338 }
339 return last_match;
340 }
341
342 #ifdef DEBUG_DEBUG_STRUCT
343
344 static int
345 dump_struct_debug (tree type, enum debug_info_usage usage,
346 enum debug_struct_file criterion, int generic,
347 int matches, int result)
348 {
349 /* Find the type name. */
350 tree type_decl = TYPE_STUB_DECL (type);
351 tree t = type_decl;
352 const char *name = 0;
353 if (TREE_CODE (t) == TYPE_DECL)
354 t = DECL_NAME (t);
355 if (t)
356 name = IDENTIFIER_POINTER (t);
357
358 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
359 criterion,
360 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
361 matches ? "bas" : "hdr",
362 generic ? "gen" : "ord",
363 usage == DINFO_USAGE_DFN ? ";" :
364 usage == DINFO_USAGE_DIR_USE ? "." : "*",
365 result,
366 (void*) type_decl, name);
367 return result;
368 }
369 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
370 dump_struct_debug (type, usage, criterion, generic, matches, result)
371
372 #else
373
374 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
375 (result)
376
377 #endif
378
379 /* Get the number of HOST_WIDE_INTs needed to represent the precision
380 of the number. */
381
382 static unsigned int
383 get_full_len (const wide_int &op)
384 {
385 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
386 / HOST_BITS_PER_WIDE_INT);
387 }
388
389 static bool
390 should_emit_struct_debug (tree type, enum debug_info_usage usage)
391 {
392 enum debug_struct_file criterion;
393 tree type_decl;
394 bool generic = lang_hooks.types.generic_p (type);
395
396 if (generic)
397 criterion = debug_struct_generic[usage];
398 else
399 criterion = debug_struct_ordinary[usage];
400
401 if (criterion == DINFO_STRUCT_FILE_NONE)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
403 if (criterion == DINFO_STRUCT_FILE_ANY)
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
405
406 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
407
408 if (type_decl != NULL)
409 {
410 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
412
413 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
414 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
415 }
416
417 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
418 }
419 \f
420 /* Return a pointer to a copy of the section string name S with all
421 attributes stripped off, and an asterisk prepended (for assemble_name). */
422
423 static inline char *
424 stripattributes (const char *s)
425 {
426 char *stripped = XNEWVEC (char, strlen (s) + 2);
427 char *p = stripped;
428
429 *p++ = '*';
430
431 while (*s && *s != ',')
432 *p++ = *s++;
433
434 *p = '\0';
435 return stripped;
436 }
437
438 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
439 switch to the data section instead, and write out a synthetic start label
440 for collect2 the first time around. */
441
442 static void
443 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
444 {
445 if (eh_frame_section == 0)
446 {
447 int flags;
448
449 if (EH_TABLES_CAN_BE_READ_ONLY)
450 {
451 int fde_encoding;
452 int per_encoding;
453 int lsda_encoding;
454
455 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
456 /*global=*/0);
457 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
458 /*global=*/1);
459 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
460 /*global=*/0);
461 flags = ((! flag_pic
462 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
463 && (fde_encoding & 0x70) != DW_EH_PE_aligned
464 && (per_encoding & 0x70) != DW_EH_PE_absptr
465 && (per_encoding & 0x70) != DW_EH_PE_aligned
466 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
467 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
468 ? 0 : SECTION_WRITE);
469 }
470 else
471 flags = SECTION_WRITE;
472
473 #ifdef EH_FRAME_SECTION_NAME
474 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
475 #else
476 eh_frame_section = ((flags == SECTION_WRITE)
477 ? data_section : readonly_data_section);
478 #endif /* EH_FRAME_SECTION_NAME */
479 }
480
481 switch_to_section (eh_frame_section);
482
483 #ifdef EH_FRAME_THROUGH_COLLECT2
484 /* We have no special eh_frame section. Emit special labels to guide
485 collect2. */
486 if (!back)
487 {
488 tree label = get_file_function_name ("F");
489 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
490 targetm.asm_out.globalize_label (asm_out_file,
491 IDENTIFIER_POINTER (label));
492 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
493 }
494 #endif
495 }
496
497 /* Switch [BACK] to the eh or debug frame table section, depending on
498 FOR_EH. */
499
500 static void
501 switch_to_frame_table_section (int for_eh, bool back)
502 {
503 if (for_eh)
504 switch_to_eh_frame_section (back);
505 else
506 {
507 if (!debug_frame_section)
508 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
509 SECTION_DEBUG, NULL);
510 switch_to_section (debug_frame_section);
511 }
512 }
513
514 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
515
516 enum dw_cfi_oprnd_type
517 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
518 {
519 switch (cfi)
520 {
521 case DW_CFA_nop:
522 case DW_CFA_GNU_window_save:
523 case DW_CFA_remember_state:
524 case DW_CFA_restore_state:
525 return dw_cfi_oprnd_unused;
526
527 case DW_CFA_set_loc:
528 case DW_CFA_advance_loc1:
529 case DW_CFA_advance_loc2:
530 case DW_CFA_advance_loc4:
531 case DW_CFA_MIPS_advance_loc8:
532 return dw_cfi_oprnd_addr;
533
534 case DW_CFA_offset:
535 case DW_CFA_offset_extended:
536 case DW_CFA_def_cfa:
537 case DW_CFA_offset_extended_sf:
538 case DW_CFA_def_cfa_sf:
539 case DW_CFA_restore:
540 case DW_CFA_restore_extended:
541 case DW_CFA_undefined:
542 case DW_CFA_same_value:
543 case DW_CFA_def_cfa_register:
544 case DW_CFA_register:
545 case DW_CFA_expression:
546 return dw_cfi_oprnd_reg_num;
547
548 case DW_CFA_def_cfa_offset:
549 case DW_CFA_GNU_args_size:
550 case DW_CFA_def_cfa_offset_sf:
551 return dw_cfi_oprnd_offset;
552
553 case DW_CFA_def_cfa_expression:
554 return dw_cfi_oprnd_loc;
555
556 default:
557 gcc_unreachable ();
558 }
559 }
560
561 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
562
563 enum dw_cfi_oprnd_type
564 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
565 {
566 switch (cfi)
567 {
568 case DW_CFA_def_cfa:
569 case DW_CFA_def_cfa_sf:
570 case DW_CFA_offset:
571 case DW_CFA_offset_extended_sf:
572 case DW_CFA_offset_extended:
573 return dw_cfi_oprnd_offset;
574
575 case DW_CFA_register:
576 return dw_cfi_oprnd_reg_num;
577
578 case DW_CFA_expression:
579 return dw_cfi_oprnd_loc;
580
581 default:
582 return dw_cfi_oprnd_unused;
583 }
584 }
585
586 /* Output one FDE. */
587
588 static void
589 output_fde (dw_fde_ref fde, bool for_eh, bool second,
590 char *section_start_label, int fde_encoding, char *augmentation,
591 bool any_lsda_needed, int lsda_encoding)
592 {
593 const char *begin, *end;
594 static unsigned int j;
595 char l1[20], l2[20];
596
597 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
598 /* empty */ 0);
599 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
600 for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
602 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
603 if (!XCOFF_DEBUGGING_INFO || for_eh)
604 {
605 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
606 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
607 " indicating 64-bit DWARF extension");
608 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
609 "FDE Length");
610 }
611 ASM_OUTPUT_LABEL (asm_out_file, l1);
612
613 if (for_eh)
614 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
615 else
616 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
617 debug_frame_section, "FDE CIE offset");
618
619 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
620 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
621
622 if (for_eh)
623 {
624 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
625 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
626 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
627 "FDE initial location");
628 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
629 end, begin, "FDE address range");
630 }
631 else
632 {
633 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
634 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
635 }
636
637 if (augmentation[0])
638 {
639 if (any_lsda_needed)
640 {
641 int size = size_of_encoded_value (lsda_encoding);
642
643 if (lsda_encoding == DW_EH_PE_aligned)
644 {
645 int offset = ( 4 /* Length */
646 + 4 /* CIE offset */
647 + 2 * size_of_encoded_value (fde_encoding)
648 + 1 /* Augmentation size */ );
649 int pad = -offset & (PTR_SIZE - 1);
650
651 size += pad;
652 gcc_assert (size_of_uleb128 (size) == 1);
653 }
654
655 dw2_asm_output_data_uleb128 (size, "Augmentation size");
656
657 if (fde->uses_eh_lsda)
658 {
659 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
660 fde->funcdef_number);
661 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
662 gen_rtx_SYMBOL_REF (Pmode, l1),
663 false,
664 "Language Specific Data Area");
665 }
666 else
667 {
668 if (lsda_encoding == DW_EH_PE_aligned)
669 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
670 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
671 "Language Specific Data Area (none)");
672 }
673 }
674 else
675 dw2_asm_output_data_uleb128 (0, "Augmentation size");
676 }
677
678 /* Loop through the Call Frame Instructions associated with this FDE. */
679 fde->dw_fde_current_label = begin;
680 {
681 size_t from, until, i;
682
683 from = 0;
684 until = vec_safe_length (fde->dw_fde_cfi);
685
686 if (fde->dw_fde_second_begin == NULL)
687 ;
688 else if (!second)
689 until = fde->dw_fde_switch_cfi_index;
690 else
691 from = fde->dw_fde_switch_cfi_index;
692
693 for (i = from; i < until; i++)
694 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
695 }
696
697 /* If we are to emit a ref/link from function bodies to their frame tables,
698 do it now. This is typically performed to make sure that tables
699 associated with functions are dragged with them and not discarded in
700 garbage collecting links. We need to do this on a per function basis to
701 cope with -ffunction-sections. */
702
703 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
704 /* Switch to the function section, emit the ref to the tables, and
705 switch *back* into the table section. */
706 switch_to_section (function_section (fde->decl));
707 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
708 switch_to_frame_table_section (for_eh, true);
709 #endif
710
711 /* Pad the FDE out to an address sized boundary. */
712 ASM_OUTPUT_ALIGN (asm_out_file,
713 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
714 ASM_OUTPUT_LABEL (asm_out_file, l2);
715
716 j += 2;
717 }
718
719 /* Return true if frame description entry FDE is needed for EH. */
720
721 static bool
722 fde_needed_for_eh_p (dw_fde_ref fde)
723 {
724 if (flag_asynchronous_unwind_tables)
725 return true;
726
727 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
728 return true;
729
730 if (fde->uses_eh_lsda)
731 return true;
732
733 /* If exceptions are enabled, we have collected nothrow info. */
734 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
735 return false;
736
737 return true;
738 }
739
740 /* Output the call frame information used to record information
741 that relates to calculating the frame pointer, and records the
742 location of saved registers. */
743
744 static void
745 output_call_frame_info (int for_eh)
746 {
747 unsigned int i;
748 dw_fde_ref fde;
749 dw_cfi_ref cfi;
750 char l1[20], l2[20], section_start_label[20];
751 bool any_lsda_needed = false;
752 char augmentation[6];
753 int augmentation_size;
754 int fde_encoding = DW_EH_PE_absptr;
755 int per_encoding = DW_EH_PE_absptr;
756 int lsda_encoding = DW_EH_PE_absptr;
757 int return_reg;
758 rtx personality = NULL;
759 int dw_cie_version;
760
761 /* Don't emit a CIE if there won't be any FDEs. */
762 if (!fde_vec)
763 return;
764
765 /* Nothing to do if the assembler's doing it all. */
766 if (dwarf2out_do_cfi_asm ())
767 return;
768
769 /* If we don't have any functions we'll want to unwind out of, don't emit
770 any EH unwind information. If we make FDEs linkonce, we may have to
771 emit an empty label for an FDE that wouldn't otherwise be emitted. We
772 want to avoid having an FDE kept around when the function it refers to
773 is discarded. Example where this matters: a primary function template
774 in C++ requires EH information, an explicit specialization doesn't. */
775 if (for_eh)
776 {
777 bool any_eh_needed = false;
778
779 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
780 {
781 if (fde->uses_eh_lsda)
782 any_eh_needed = any_lsda_needed = true;
783 else if (fde_needed_for_eh_p (fde))
784 any_eh_needed = true;
785 else if (TARGET_USES_WEAK_UNWIND_INFO)
786 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
787 }
788
789 if (!any_eh_needed)
790 return;
791 }
792
793 /* We're going to be generating comments, so turn on app. */
794 if (flag_debug_asm)
795 app_enable ();
796
797 /* Switch to the proper frame section, first time. */
798 switch_to_frame_table_section (for_eh, false);
799
800 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
801 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
802
803 /* Output the CIE. */
804 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
805 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
806 if (!XCOFF_DEBUGGING_INFO || for_eh)
807 {
808 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
809 dw2_asm_output_data (4, 0xffffffff,
810 "Initial length escape value indicating 64-bit DWARF extension");
811 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
812 "Length of Common Information Entry");
813 }
814 ASM_OUTPUT_LABEL (asm_out_file, l1);
815
816 /* Now that the CIE pointer is PC-relative for EH,
817 use 0 to identify the CIE. */
818 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
819 (for_eh ? 0 : DWARF_CIE_ID),
820 "CIE Identifier Tag");
821
822 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
823 use CIE version 1, unless that would produce incorrect results
824 due to overflowing the return register column. */
825 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
826 dw_cie_version = 1;
827 if (return_reg >= 256 || dwarf_version > 2)
828 dw_cie_version = 3;
829 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
830
831 augmentation[0] = 0;
832 augmentation_size = 0;
833
834 personality = current_unit_personality;
835 if (for_eh)
836 {
837 char *p;
838
839 /* Augmentation:
840 z Indicates that a uleb128 is present to size the
841 augmentation section.
842 L Indicates the encoding (and thus presence) of
843 an LSDA pointer in the FDE augmentation.
844 R Indicates a non-default pointer encoding for
845 FDE code pointers.
846 P Indicates the presence of an encoding + language
847 personality routine in the CIE augmentation. */
848
849 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
850 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
851 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
852
853 p = augmentation + 1;
854 if (personality)
855 {
856 *p++ = 'P';
857 augmentation_size += 1 + size_of_encoded_value (per_encoding);
858 assemble_external_libcall (personality);
859 }
860 if (any_lsda_needed)
861 {
862 *p++ = 'L';
863 augmentation_size += 1;
864 }
865 if (fde_encoding != DW_EH_PE_absptr)
866 {
867 *p++ = 'R';
868 augmentation_size += 1;
869 }
870 if (p > augmentation + 1)
871 {
872 augmentation[0] = 'z';
873 *p = '\0';
874 }
875
876 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
877 if (personality && per_encoding == DW_EH_PE_aligned)
878 {
879 int offset = ( 4 /* Length */
880 + 4 /* CIE Id */
881 + 1 /* CIE version */
882 + strlen (augmentation) + 1 /* Augmentation */
883 + size_of_uleb128 (1) /* Code alignment */
884 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
885 + 1 /* RA column */
886 + 1 /* Augmentation size */
887 + 1 /* Personality encoding */ );
888 int pad = -offset & (PTR_SIZE - 1);
889
890 augmentation_size += pad;
891
892 /* Augmentations should be small, so there's scarce need to
893 iterate for a solution. Die if we exceed one uleb128 byte. */
894 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
895 }
896 }
897
898 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
899 if (dw_cie_version >= 4)
900 {
901 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
902 dw2_asm_output_data (1, 0, "CIE Segment Size");
903 }
904 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
905 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
906 "CIE Data Alignment Factor");
907
908 if (dw_cie_version == 1)
909 dw2_asm_output_data (1, return_reg, "CIE RA Column");
910 else
911 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
912
913 if (augmentation[0])
914 {
915 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
916 if (personality)
917 {
918 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
919 eh_data_format_name (per_encoding));
920 dw2_asm_output_encoded_addr_rtx (per_encoding,
921 personality,
922 true, NULL);
923 }
924
925 if (any_lsda_needed)
926 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
927 eh_data_format_name (lsda_encoding));
928
929 if (fde_encoding != DW_EH_PE_absptr)
930 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
931 eh_data_format_name (fde_encoding));
932 }
933
934 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
935 output_cfi (cfi, NULL, for_eh);
936
937 /* Pad the CIE out to an address sized boundary. */
938 ASM_OUTPUT_ALIGN (asm_out_file,
939 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
940 ASM_OUTPUT_LABEL (asm_out_file, l2);
941
942 /* Loop through all of the FDE's. */
943 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
944 {
945 unsigned int k;
946
947 /* Don't emit EH unwind info for leaf functions that don't need it. */
948 if (for_eh && !fde_needed_for_eh_p (fde))
949 continue;
950
951 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
952 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
953 augmentation, any_lsda_needed, lsda_encoding);
954 }
955
956 if (for_eh && targetm.terminate_dw2_eh_frame_info)
957 dw2_asm_output_data (4, 0, "End of Table");
958
959 /* Turn off app to make assembly quicker. */
960 if (flag_debug_asm)
961 app_disable ();
962 }
963
964 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
965
966 static void
967 dwarf2out_do_cfi_startproc (bool second)
968 {
969 int enc;
970 rtx ref;
971 rtx personality = get_personality_function (current_function_decl);
972
973 fprintf (asm_out_file, "\t.cfi_startproc\n");
974
975 if (personality)
976 {
977 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
978 ref = personality;
979
980 /* ??? The GAS support isn't entirely consistent. We have to
981 handle indirect support ourselves, but PC-relative is done
982 in the assembler. Further, the assembler can't handle any
983 of the weirder relocation types. */
984 if (enc & DW_EH_PE_indirect)
985 ref = dw2_force_const_mem (ref, true);
986
987 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
988 output_addr_const (asm_out_file, ref);
989 fputc ('\n', asm_out_file);
990 }
991
992 if (crtl->uses_eh_lsda)
993 {
994 char lab[20];
995
996 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
997 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
998 current_function_funcdef_no);
999 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1000 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1001
1002 if (enc & DW_EH_PE_indirect)
1003 ref = dw2_force_const_mem (ref, true);
1004
1005 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1006 output_addr_const (asm_out_file, ref);
1007 fputc ('\n', asm_out_file);
1008 }
1009 }
1010
1011 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1012 this allocation may be done before pass_final. */
1013
1014 dw_fde_ref
1015 dwarf2out_alloc_current_fde (void)
1016 {
1017 dw_fde_ref fde;
1018
1019 fde = ggc_cleared_alloc<dw_fde_node> ();
1020 fde->decl = current_function_decl;
1021 fde->funcdef_number = current_function_funcdef_no;
1022 fde->fde_index = vec_safe_length (fde_vec);
1023 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1024 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1025 fde->nothrow = crtl->nothrow;
1026 fde->drap_reg = INVALID_REGNUM;
1027 fde->vdrap_reg = INVALID_REGNUM;
1028
1029 /* Record the FDE associated with this function. */
1030 cfun->fde = fde;
1031 vec_safe_push (fde_vec, fde);
1032
1033 return fde;
1034 }
1035
1036 /* Output a marker (i.e. a label) for the beginning of a function, before
1037 the prologue. */
1038
1039 void
1040 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1041 const char *file ATTRIBUTE_UNUSED)
1042 {
1043 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1044 char * dup_label;
1045 dw_fde_ref fde;
1046 section *fnsec;
1047 bool do_frame;
1048
1049 current_function_func_begin_label = NULL;
1050
1051 do_frame = dwarf2out_do_frame ();
1052
1053 /* ??? current_function_func_begin_label is also used by except.c for
1054 call-site information. We must emit this label if it might be used. */
1055 if (!do_frame
1056 && (!flag_exceptions
1057 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1058 return;
1059
1060 fnsec = function_section (current_function_decl);
1061 switch_to_section (fnsec);
1062 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1063 current_function_funcdef_no);
1064 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1065 current_function_funcdef_no);
1066 dup_label = xstrdup (label);
1067 current_function_func_begin_label = dup_label;
1068
1069 /* We can elide the fde allocation if we're not emitting debug info. */
1070 if (!do_frame)
1071 return;
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, 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 ((flag_unwind_tables || flag_exceptions)
1190 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1191 output_call_frame_info (1);
1192 }
1193
1194 /* Note that the current function section is being used for code. */
1195
1196 static void
1197 dwarf2out_note_section_used (void)
1198 {
1199 section *sec = current_function_section ();
1200 if (sec == text_section)
1201 text_section_used = true;
1202 else if (sec == cold_text_section)
1203 cold_text_section_used = true;
1204 }
1205
1206 static void var_location_switch_text_section (void);
1207 static void set_cur_line_info_table (section *);
1208
1209 void
1210 dwarf2out_switch_text_section (void)
1211 {
1212 section *sect;
1213 dw_fde_ref fde = cfun->fde;
1214
1215 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1216
1217 if (!in_cold_section_p)
1218 {
1219 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1220 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1221 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1222 }
1223 else
1224 {
1225 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1226 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1227 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1228 }
1229 have_multiple_function_sections = true;
1230
1231 /* There is no need to mark used sections when not debugging. */
1232 if (cold_text_section != NULL)
1233 dwarf2out_note_section_used ();
1234
1235 if (dwarf2out_do_cfi_asm ())
1236 fprintf (asm_out_file, "\t.cfi_endproc\n");
1237
1238 /* Now do the real section switch. */
1239 sect = current_function_section ();
1240 switch_to_section (sect);
1241
1242 fde->second_in_std_section
1243 = (sect == text_section
1244 || (cold_text_section && sect == cold_text_section));
1245
1246 if (dwarf2out_do_cfi_asm ())
1247 dwarf2out_do_cfi_startproc (true);
1248
1249 var_location_switch_text_section ();
1250
1251 if (cold_text_section != NULL)
1252 set_cur_line_info_table (sect);
1253 }
1254 \f
1255 /* And now, the subset of the debugging information support code necessary
1256 for emitting location expressions. */
1257
1258 /* Data about a single source file. */
1259 struct GTY((for_user)) dwarf_file_data {
1260 const char * filename;
1261 int emitted_number;
1262 };
1263
1264 /* Describe an entry into the .debug_addr section. */
1265
1266 enum ate_kind {
1267 ate_kind_rtx,
1268 ate_kind_rtx_dtprel,
1269 ate_kind_label
1270 };
1271
1272 struct GTY((for_user)) addr_table_entry {
1273 enum ate_kind kind;
1274 unsigned int refcount;
1275 unsigned int index;
1276 union addr_table_entry_struct_union
1277 {
1278 rtx GTY ((tag ("0"))) rtl;
1279 char * GTY ((tag ("1"))) label;
1280 }
1281 GTY ((desc ("%1.kind"))) addr;
1282 };
1283
1284 /* Location lists are ranges + location descriptions for that range,
1285 so you can track variables that are in different places over
1286 their entire life. */
1287 typedef struct GTY(()) dw_loc_list_struct {
1288 dw_loc_list_ref dw_loc_next;
1289 const char *begin; /* Label and addr_entry for start of range */
1290 addr_table_entry *begin_entry;
1291 const char *end; /* Label for end of range */
1292 char *ll_symbol; /* Label for beginning of location list.
1293 Only on head of list */
1294 const char *section; /* Section this loclist is relative to */
1295 dw_loc_descr_ref expr;
1296 hashval_t hash;
1297 /* True if all addresses in this and subsequent lists are known to be
1298 resolved. */
1299 bool resolved_addr;
1300 /* True if this list has been replaced by dw_loc_next. */
1301 bool replaced;
1302 bool emitted;
1303 /* True if the range should be emitted even if begin and end
1304 are the same. */
1305 bool force;
1306 } dw_loc_list_node;
1307
1308 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1309
1310 /* Convert a DWARF stack opcode into its string name. */
1311
1312 static const char *
1313 dwarf_stack_op_name (unsigned int op)
1314 {
1315 const char *name = get_DW_OP_name (op);
1316
1317 if (name != NULL)
1318 return name;
1319
1320 return "OP_<unknown>";
1321 }
1322
1323 /* Return a pointer to a newly allocated location description. Location
1324 descriptions are simple expression terms that can be strung
1325 together to form more complicated location (address) descriptions. */
1326
1327 static inline dw_loc_descr_ref
1328 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1329 unsigned HOST_WIDE_INT oprnd2)
1330 {
1331 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1332
1333 descr->dw_loc_opc = op;
1334 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1335 descr->dw_loc_oprnd1.val_entry = NULL;
1336 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1337 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd2.val_entry = NULL;
1339 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1340
1341 return descr;
1342 }
1343
1344 /* Return a pointer to a newly allocated location description for
1345 REG and OFFSET. */
1346
1347 static inline dw_loc_descr_ref
1348 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1349 {
1350 if (reg <= 31)
1351 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1352 offset, 0);
1353 else
1354 return new_loc_descr (DW_OP_bregx, reg, offset);
1355 }
1356
1357 /* Add a location description term to a location description expression. */
1358
1359 static inline void
1360 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1361 {
1362 dw_loc_descr_ref *d;
1363
1364 /* Find the end of the chain. */
1365 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1366 ;
1367
1368 *d = descr;
1369 }
1370
1371 /* Compare two location operands for exact equality. */
1372
1373 static bool
1374 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1375 {
1376 if (a->val_class != b->val_class)
1377 return false;
1378 switch (a->val_class)
1379 {
1380 case dw_val_class_none:
1381 return true;
1382 case dw_val_class_addr:
1383 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1384
1385 case dw_val_class_offset:
1386 case dw_val_class_unsigned_const:
1387 case dw_val_class_const:
1388 case dw_val_class_range_list:
1389 case dw_val_class_lineptr:
1390 case dw_val_class_macptr:
1391 /* These are all HOST_WIDE_INT, signed or unsigned. */
1392 return a->v.val_unsigned == b->v.val_unsigned;
1393
1394 case dw_val_class_loc:
1395 return a->v.val_loc == b->v.val_loc;
1396 case dw_val_class_loc_list:
1397 return a->v.val_loc_list == b->v.val_loc_list;
1398 case dw_val_class_die_ref:
1399 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1400 case dw_val_class_fde_ref:
1401 return a->v.val_fde_index == b->v.val_fde_index;
1402 case dw_val_class_lbl_id:
1403 case dw_val_class_high_pc:
1404 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1405 case dw_val_class_str:
1406 return a->v.val_str == b->v.val_str;
1407 case dw_val_class_flag:
1408 return a->v.val_flag == b->v.val_flag;
1409 case dw_val_class_file:
1410 return a->v.val_file == b->v.val_file;
1411 case dw_val_class_decl_ref:
1412 return a->v.val_decl_ref == b->v.val_decl_ref;
1413
1414 case dw_val_class_const_double:
1415 return (a->v.val_double.high == b->v.val_double.high
1416 && a->v.val_double.low == b->v.val_double.low);
1417
1418 case dw_val_class_wide_int:
1419 return *a->v.val_wide == *b->v.val_wide;
1420
1421 case dw_val_class_vec:
1422 {
1423 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1424 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1425
1426 return (a_len == b_len
1427 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1428 }
1429
1430 case dw_val_class_data8:
1431 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1432
1433 case dw_val_class_vms_delta:
1434 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1435 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1436 }
1437 gcc_unreachable ();
1438 }
1439
1440 /* Compare two location atoms for exact equality. */
1441
1442 static bool
1443 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1444 {
1445 if (a->dw_loc_opc != b->dw_loc_opc)
1446 return false;
1447
1448 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1449 address size, but since we always allocate cleared storage it
1450 should be zero for other types of locations. */
1451 if (a->dtprel != b->dtprel)
1452 return false;
1453
1454 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1455 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1456 }
1457
1458 /* Compare two complete location expressions for exact equality. */
1459
1460 bool
1461 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1462 {
1463 while (1)
1464 {
1465 if (a == b)
1466 return true;
1467 if (a == NULL || b == NULL)
1468 return false;
1469 if (!loc_descr_equal_p_1 (a, b))
1470 return false;
1471
1472 a = a->dw_loc_next;
1473 b = b->dw_loc_next;
1474 }
1475 }
1476
1477
1478 /* Add a constant OFFSET to a location expression. */
1479
1480 static void
1481 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1482 {
1483 dw_loc_descr_ref loc;
1484 HOST_WIDE_INT *p;
1485
1486 gcc_assert (*list_head != NULL);
1487
1488 if (!offset)
1489 return;
1490
1491 /* Find the end of the chain. */
1492 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1493 ;
1494
1495 p = NULL;
1496 if (loc->dw_loc_opc == DW_OP_fbreg
1497 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1498 p = &loc->dw_loc_oprnd1.v.val_int;
1499 else if (loc->dw_loc_opc == DW_OP_bregx)
1500 p = &loc->dw_loc_oprnd2.v.val_int;
1501
1502 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1503 offset. Don't optimize if an signed integer overflow would happen. */
1504 if (p != NULL
1505 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1506 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1507 *p += offset;
1508
1509 else if (offset > 0)
1510 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1511
1512 else
1513 {
1514 loc->dw_loc_next = int_loc_descriptor (-offset);
1515 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1516 }
1517 }
1518
1519 /* Add a constant OFFSET to a location list. */
1520
1521 static void
1522 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1523 {
1524 dw_loc_list_ref d;
1525 for (d = list_head; d != NULL; d = d->dw_loc_next)
1526 loc_descr_plus_const (&d->expr, offset);
1527 }
1528
1529 #define DWARF_REF_SIZE \
1530 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1531
1532 static unsigned long int get_base_type_offset (dw_die_ref);
1533
1534 /* Return the size of a location descriptor. */
1535
1536 static unsigned long
1537 size_of_loc_descr (dw_loc_descr_ref loc)
1538 {
1539 unsigned long size = 1;
1540
1541 switch (loc->dw_loc_opc)
1542 {
1543 case DW_OP_addr:
1544 size += DWARF2_ADDR_SIZE;
1545 break;
1546 case DW_OP_GNU_addr_index:
1547 case DW_OP_GNU_const_index:
1548 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1549 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1550 break;
1551 case DW_OP_const1u:
1552 case DW_OP_const1s:
1553 size += 1;
1554 break;
1555 case DW_OP_const2u:
1556 case DW_OP_const2s:
1557 size += 2;
1558 break;
1559 case DW_OP_const4u:
1560 case DW_OP_const4s:
1561 size += 4;
1562 break;
1563 case DW_OP_const8u:
1564 case DW_OP_const8s:
1565 size += 8;
1566 break;
1567 case DW_OP_constu:
1568 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1569 break;
1570 case DW_OP_consts:
1571 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1572 break;
1573 case DW_OP_pick:
1574 size += 1;
1575 break;
1576 case DW_OP_plus_uconst:
1577 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1578 break;
1579 case DW_OP_skip:
1580 case DW_OP_bra:
1581 size += 2;
1582 break;
1583 case DW_OP_breg0:
1584 case DW_OP_breg1:
1585 case DW_OP_breg2:
1586 case DW_OP_breg3:
1587 case DW_OP_breg4:
1588 case DW_OP_breg5:
1589 case DW_OP_breg6:
1590 case DW_OP_breg7:
1591 case DW_OP_breg8:
1592 case DW_OP_breg9:
1593 case DW_OP_breg10:
1594 case DW_OP_breg11:
1595 case DW_OP_breg12:
1596 case DW_OP_breg13:
1597 case DW_OP_breg14:
1598 case DW_OP_breg15:
1599 case DW_OP_breg16:
1600 case DW_OP_breg17:
1601 case DW_OP_breg18:
1602 case DW_OP_breg19:
1603 case DW_OP_breg20:
1604 case DW_OP_breg21:
1605 case DW_OP_breg22:
1606 case DW_OP_breg23:
1607 case DW_OP_breg24:
1608 case DW_OP_breg25:
1609 case DW_OP_breg26:
1610 case DW_OP_breg27:
1611 case DW_OP_breg28:
1612 case DW_OP_breg29:
1613 case DW_OP_breg30:
1614 case DW_OP_breg31:
1615 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1616 break;
1617 case DW_OP_regx:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 break;
1620 case DW_OP_fbreg:
1621 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1622 break;
1623 case DW_OP_bregx:
1624 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1625 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1626 break;
1627 case DW_OP_piece:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 break;
1630 case DW_OP_bit_piece:
1631 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1632 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1633 break;
1634 case DW_OP_deref_size:
1635 case DW_OP_xderef_size:
1636 size += 1;
1637 break;
1638 case DW_OP_call2:
1639 size += 2;
1640 break;
1641 case DW_OP_call4:
1642 size += 4;
1643 break;
1644 case DW_OP_call_ref:
1645 size += DWARF_REF_SIZE;
1646 break;
1647 case DW_OP_implicit_value:
1648 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1649 + loc->dw_loc_oprnd1.v.val_unsigned;
1650 break;
1651 case DW_OP_GNU_implicit_pointer:
1652 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1653 break;
1654 case DW_OP_GNU_entry_value:
1655 {
1656 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1657 size += size_of_uleb128 (op_size) + op_size;
1658 break;
1659 }
1660 case DW_OP_GNU_const_type:
1661 {
1662 unsigned long o
1663 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1664 size += size_of_uleb128 (o) + 1;
1665 switch (loc->dw_loc_oprnd2.val_class)
1666 {
1667 case dw_val_class_vec:
1668 size += loc->dw_loc_oprnd2.v.val_vec.length
1669 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1670 break;
1671 case dw_val_class_const:
1672 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1673 break;
1674 case dw_val_class_const_double:
1675 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1676 break;
1677 case dw_val_class_wide_int:
1678 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1679 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1680 break;
1681 default:
1682 gcc_unreachable ();
1683 }
1684 break;
1685 }
1686 case DW_OP_GNU_regval_type:
1687 {
1688 unsigned long o
1689 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1690 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1691 + size_of_uleb128 (o);
1692 }
1693 break;
1694 case DW_OP_GNU_deref_type:
1695 {
1696 unsigned long o
1697 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1698 size += 1 + size_of_uleb128 (o);
1699 }
1700 break;
1701 case DW_OP_GNU_convert:
1702 case DW_OP_GNU_reinterpret:
1703 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1704 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1705 else
1706 {
1707 unsigned long o
1708 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1709 size += size_of_uleb128 (o);
1710 }
1711 break;
1712 case DW_OP_GNU_parameter_ref:
1713 size += 4;
1714 break;
1715 default:
1716 break;
1717 }
1718
1719 return size;
1720 }
1721
1722 /* Return the size of a series of location descriptors. */
1723
1724 unsigned long
1725 size_of_locs (dw_loc_descr_ref loc)
1726 {
1727 dw_loc_descr_ref l;
1728 unsigned long size;
1729
1730 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1731 field, to avoid writing to a PCH file. */
1732 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1733 {
1734 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1735 break;
1736 size += size_of_loc_descr (l);
1737 }
1738 if (! l)
1739 return size;
1740
1741 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1742 {
1743 l->dw_loc_addr = size;
1744 size += size_of_loc_descr (l);
1745 }
1746
1747 return size;
1748 }
1749
1750 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1751 static void get_ref_die_offset_label (char *, dw_die_ref);
1752 static unsigned long int get_ref_die_offset (dw_die_ref);
1753
1754 /* Output location description stack opcode's operands (if any).
1755 The for_eh_or_skip parameter controls whether register numbers are
1756 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1757 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1758 info). This should be suppressed for the cases that have not been converted
1759 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1760
1761 static void
1762 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1763 {
1764 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1765 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1766
1767 switch (loc->dw_loc_opc)
1768 {
1769 #ifdef DWARF2_DEBUGGING_INFO
1770 case DW_OP_const2u:
1771 case DW_OP_const2s:
1772 dw2_asm_output_data (2, val1->v.val_int, NULL);
1773 break;
1774 case DW_OP_const4u:
1775 if (loc->dtprel)
1776 {
1777 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1778 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1779 val1->v.val_addr);
1780 fputc ('\n', asm_out_file);
1781 break;
1782 }
1783 /* FALLTHRU */
1784 case DW_OP_const4s:
1785 dw2_asm_output_data (4, val1->v.val_int, NULL);
1786 break;
1787 case DW_OP_const8u:
1788 if (loc->dtprel)
1789 {
1790 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1791 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1792 val1->v.val_addr);
1793 fputc ('\n', asm_out_file);
1794 break;
1795 }
1796 /* FALLTHRU */
1797 case DW_OP_const8s:
1798 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1799 dw2_asm_output_data (8, val1->v.val_int, NULL);
1800 break;
1801 case DW_OP_skip:
1802 case DW_OP_bra:
1803 {
1804 int offset;
1805
1806 gcc_assert (val1->val_class == dw_val_class_loc);
1807 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1808
1809 dw2_asm_output_data (2, offset, NULL);
1810 }
1811 break;
1812 case DW_OP_implicit_value:
1813 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1814 switch (val2->val_class)
1815 {
1816 case dw_val_class_const:
1817 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1818 break;
1819 case dw_val_class_vec:
1820 {
1821 unsigned int elt_size = val2->v.val_vec.elt_size;
1822 unsigned int len = val2->v.val_vec.length;
1823 unsigned int i;
1824 unsigned char *p;
1825
1826 if (elt_size > sizeof (HOST_WIDE_INT))
1827 {
1828 elt_size /= 2;
1829 len *= 2;
1830 }
1831 for (i = 0, p = val2->v.val_vec.array;
1832 i < len;
1833 i++, p += elt_size)
1834 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1835 "fp or vector constant word %u", i);
1836 }
1837 break;
1838 case dw_val_class_const_double:
1839 {
1840 unsigned HOST_WIDE_INT first, second;
1841
1842 if (WORDS_BIG_ENDIAN)
1843 {
1844 first = val2->v.val_double.high;
1845 second = val2->v.val_double.low;
1846 }
1847 else
1848 {
1849 first = val2->v.val_double.low;
1850 second = val2->v.val_double.high;
1851 }
1852 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1853 first, NULL);
1854 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1855 second, NULL);
1856 }
1857 break;
1858 case dw_val_class_wide_int:
1859 {
1860 int i;
1861 int len = get_full_len (*val2->v.val_wide);
1862 if (WORDS_BIG_ENDIAN)
1863 for (i = len - 1; i >= 0; --i)
1864 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1865 val2->v.val_wide->elt (i), NULL);
1866 else
1867 for (i = 0; i < len; ++i)
1868 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1869 val2->v.val_wide->elt (i), NULL);
1870 }
1871 break;
1872 case dw_val_class_addr:
1873 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1874 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1875 break;
1876 default:
1877 gcc_unreachable ();
1878 }
1879 break;
1880 #else
1881 case DW_OP_const2u:
1882 case DW_OP_const2s:
1883 case DW_OP_const4u:
1884 case DW_OP_const4s:
1885 case DW_OP_const8u:
1886 case DW_OP_const8s:
1887 case DW_OP_skip:
1888 case DW_OP_bra:
1889 case DW_OP_implicit_value:
1890 /* We currently don't make any attempt to make sure these are
1891 aligned properly like we do for the main unwind info, so
1892 don't support emitting things larger than a byte if we're
1893 only doing unwinding. */
1894 gcc_unreachable ();
1895 #endif
1896 case DW_OP_const1u:
1897 case DW_OP_const1s:
1898 dw2_asm_output_data (1, val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_constu:
1901 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1902 break;
1903 case DW_OP_consts:
1904 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1905 break;
1906 case DW_OP_pick:
1907 dw2_asm_output_data (1, val1->v.val_int, NULL);
1908 break;
1909 case DW_OP_plus_uconst:
1910 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1911 break;
1912 case DW_OP_breg0:
1913 case DW_OP_breg1:
1914 case DW_OP_breg2:
1915 case DW_OP_breg3:
1916 case DW_OP_breg4:
1917 case DW_OP_breg5:
1918 case DW_OP_breg6:
1919 case DW_OP_breg7:
1920 case DW_OP_breg8:
1921 case DW_OP_breg9:
1922 case DW_OP_breg10:
1923 case DW_OP_breg11:
1924 case DW_OP_breg12:
1925 case DW_OP_breg13:
1926 case DW_OP_breg14:
1927 case DW_OP_breg15:
1928 case DW_OP_breg16:
1929 case DW_OP_breg17:
1930 case DW_OP_breg18:
1931 case DW_OP_breg19:
1932 case DW_OP_breg20:
1933 case DW_OP_breg21:
1934 case DW_OP_breg22:
1935 case DW_OP_breg23:
1936 case DW_OP_breg24:
1937 case DW_OP_breg25:
1938 case DW_OP_breg26:
1939 case DW_OP_breg27:
1940 case DW_OP_breg28:
1941 case DW_OP_breg29:
1942 case DW_OP_breg30:
1943 case DW_OP_breg31:
1944 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1945 break;
1946 case DW_OP_regx:
1947 {
1948 unsigned r = val1->v.val_unsigned;
1949 if (for_eh_or_skip >= 0)
1950 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1951 gcc_assert (size_of_uleb128 (r)
1952 == size_of_uleb128 (val1->v.val_unsigned));
1953 dw2_asm_output_data_uleb128 (r, NULL);
1954 }
1955 break;
1956 case DW_OP_fbreg:
1957 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1958 break;
1959 case DW_OP_bregx:
1960 {
1961 unsigned r = val1->v.val_unsigned;
1962 if (for_eh_or_skip >= 0)
1963 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1964 gcc_assert (size_of_uleb128 (r)
1965 == size_of_uleb128 (val1->v.val_unsigned));
1966 dw2_asm_output_data_uleb128 (r, NULL);
1967 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1968 }
1969 break;
1970 case DW_OP_piece:
1971 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1972 break;
1973 case DW_OP_bit_piece:
1974 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1975 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1976 break;
1977 case DW_OP_deref_size:
1978 case DW_OP_xderef_size:
1979 dw2_asm_output_data (1, val1->v.val_int, NULL);
1980 break;
1981
1982 case DW_OP_addr:
1983 if (loc->dtprel)
1984 {
1985 if (targetm.asm_out.output_dwarf_dtprel)
1986 {
1987 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1988 DWARF2_ADDR_SIZE,
1989 val1->v.val_addr);
1990 fputc ('\n', asm_out_file);
1991 }
1992 else
1993 gcc_unreachable ();
1994 }
1995 else
1996 {
1997 #ifdef DWARF2_DEBUGGING_INFO
1998 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1999 #else
2000 gcc_unreachable ();
2001 #endif
2002 }
2003 break;
2004
2005 case DW_OP_GNU_addr_index:
2006 case DW_OP_GNU_const_index:
2007 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2008 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2009 "(index into .debug_addr)");
2010 break;
2011
2012 case DW_OP_GNU_implicit_pointer:
2013 {
2014 char label[MAX_ARTIFICIAL_LABEL_BYTES
2015 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2016 gcc_assert (val1->val_class == dw_val_class_die_ref);
2017 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2018 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2019 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2020 }
2021 break;
2022
2023 case DW_OP_GNU_entry_value:
2024 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2025 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2026 break;
2027
2028 case DW_OP_GNU_const_type:
2029 {
2030 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2031 gcc_assert (o);
2032 dw2_asm_output_data_uleb128 (o, NULL);
2033 switch (val2->val_class)
2034 {
2035 case dw_val_class_const:
2036 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2037 dw2_asm_output_data (1, l, NULL);
2038 dw2_asm_output_data (l, val2->v.val_int, NULL);
2039 break;
2040 case dw_val_class_vec:
2041 {
2042 unsigned int elt_size = val2->v.val_vec.elt_size;
2043 unsigned int len = val2->v.val_vec.length;
2044 unsigned int i;
2045 unsigned char *p;
2046
2047 l = len * elt_size;
2048 dw2_asm_output_data (1, l, NULL);
2049 if (elt_size > sizeof (HOST_WIDE_INT))
2050 {
2051 elt_size /= 2;
2052 len *= 2;
2053 }
2054 for (i = 0, p = val2->v.val_vec.array;
2055 i < len;
2056 i++, p += elt_size)
2057 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2058 "fp or vector constant word %u", i);
2059 }
2060 break;
2061 case dw_val_class_const_double:
2062 {
2063 unsigned HOST_WIDE_INT first, second;
2064 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2065
2066 dw2_asm_output_data (1, 2 * l, NULL);
2067 if (WORDS_BIG_ENDIAN)
2068 {
2069 first = val2->v.val_double.high;
2070 second = val2->v.val_double.low;
2071 }
2072 else
2073 {
2074 first = val2->v.val_double.low;
2075 second = val2->v.val_double.high;
2076 }
2077 dw2_asm_output_data (l, first, NULL);
2078 dw2_asm_output_data (l, second, NULL);
2079 }
2080 break;
2081 case dw_val_class_wide_int:
2082 {
2083 int i;
2084 int len = get_full_len (*val2->v.val_wide);
2085 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2086
2087 dw2_asm_output_data (1, len * l, NULL);
2088 if (WORDS_BIG_ENDIAN)
2089 for (i = len - 1; i >= 0; --i)
2090 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2091 else
2092 for (i = 0; i < len; ++i)
2093 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2094 }
2095 break;
2096 default:
2097 gcc_unreachable ();
2098 }
2099 }
2100 break;
2101 case DW_OP_GNU_regval_type:
2102 {
2103 unsigned r = val1->v.val_unsigned;
2104 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2105 gcc_assert (o);
2106 if (for_eh_or_skip >= 0)
2107 {
2108 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2109 gcc_assert (size_of_uleb128 (r)
2110 == size_of_uleb128 (val1->v.val_unsigned));
2111 }
2112 dw2_asm_output_data_uleb128 (r, NULL);
2113 dw2_asm_output_data_uleb128 (o, NULL);
2114 }
2115 break;
2116 case DW_OP_GNU_deref_type:
2117 {
2118 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2119 gcc_assert (o);
2120 dw2_asm_output_data (1, val1->v.val_int, NULL);
2121 dw2_asm_output_data_uleb128 (o, NULL);
2122 }
2123 break;
2124 case DW_OP_GNU_convert:
2125 case DW_OP_GNU_reinterpret:
2126 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2127 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2128 else
2129 {
2130 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2131 gcc_assert (o);
2132 dw2_asm_output_data_uleb128 (o, NULL);
2133 }
2134 break;
2135
2136 case DW_OP_GNU_parameter_ref:
2137 {
2138 unsigned long o;
2139 gcc_assert (val1->val_class == dw_val_class_die_ref);
2140 o = get_ref_die_offset (val1->v.val_die_ref.die);
2141 dw2_asm_output_data (4, o, NULL);
2142 }
2143 break;
2144
2145 default:
2146 /* Other codes have no operands. */
2147 break;
2148 }
2149 }
2150
2151 /* Output a sequence of location operations.
2152 The for_eh_or_skip parameter controls whether register numbers are
2153 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2154 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2155 info). This should be suppressed for the cases that have not been converted
2156 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2157
2158 void
2159 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2160 {
2161 for (; loc != NULL; loc = loc->dw_loc_next)
2162 {
2163 enum dwarf_location_atom opc = loc->dw_loc_opc;
2164 /* Output the opcode. */
2165 if (for_eh_or_skip >= 0
2166 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2167 {
2168 unsigned r = (opc - DW_OP_breg0);
2169 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2170 gcc_assert (r <= 31);
2171 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2172 }
2173 else if (for_eh_or_skip >= 0
2174 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2175 {
2176 unsigned r = (opc - DW_OP_reg0);
2177 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2178 gcc_assert (r <= 31);
2179 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2180 }
2181
2182 dw2_asm_output_data (1, opc,
2183 "%s", dwarf_stack_op_name (opc));
2184
2185 /* Output the operand(s) (if any). */
2186 output_loc_operands (loc, for_eh_or_skip);
2187 }
2188 }
2189
2190 /* Output location description stack opcode's operands (if any).
2191 The output is single bytes on a line, suitable for .cfi_escape. */
2192
2193 static void
2194 output_loc_operands_raw (dw_loc_descr_ref loc)
2195 {
2196 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2197 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2198
2199 switch (loc->dw_loc_opc)
2200 {
2201 case DW_OP_addr:
2202 case DW_OP_GNU_addr_index:
2203 case DW_OP_GNU_const_index:
2204 case DW_OP_implicit_value:
2205 /* We cannot output addresses in .cfi_escape, only bytes. */
2206 gcc_unreachable ();
2207
2208 case DW_OP_const1u:
2209 case DW_OP_const1s:
2210 case DW_OP_pick:
2211 case DW_OP_deref_size:
2212 case DW_OP_xderef_size:
2213 fputc (',', asm_out_file);
2214 dw2_asm_output_data_raw (1, val1->v.val_int);
2215 break;
2216
2217 case DW_OP_const2u:
2218 case DW_OP_const2s:
2219 fputc (',', asm_out_file);
2220 dw2_asm_output_data_raw (2, val1->v.val_int);
2221 break;
2222
2223 case DW_OP_const4u:
2224 case DW_OP_const4s:
2225 fputc (',', asm_out_file);
2226 dw2_asm_output_data_raw (4, val1->v.val_int);
2227 break;
2228
2229 case DW_OP_const8u:
2230 case DW_OP_const8s:
2231 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2232 fputc (',', asm_out_file);
2233 dw2_asm_output_data_raw (8, val1->v.val_int);
2234 break;
2235
2236 case DW_OP_skip:
2237 case DW_OP_bra:
2238 {
2239 int offset;
2240
2241 gcc_assert (val1->val_class == dw_val_class_loc);
2242 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2243
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_raw (2, offset);
2246 }
2247 break;
2248
2249 case DW_OP_regx:
2250 {
2251 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2252 gcc_assert (size_of_uleb128 (r)
2253 == size_of_uleb128 (val1->v.val_unsigned));
2254 fputc (',', asm_out_file);
2255 dw2_asm_output_data_uleb128_raw (r);
2256 }
2257 break;
2258
2259 case DW_OP_constu:
2260 case DW_OP_plus_uconst:
2261 case DW_OP_piece:
2262 fputc (',', asm_out_file);
2263 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2264 break;
2265
2266 case DW_OP_bit_piece:
2267 fputc (',', asm_out_file);
2268 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2269 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2270 break;
2271
2272 case DW_OP_consts:
2273 case DW_OP_breg0:
2274 case DW_OP_breg1:
2275 case DW_OP_breg2:
2276 case DW_OP_breg3:
2277 case DW_OP_breg4:
2278 case DW_OP_breg5:
2279 case DW_OP_breg6:
2280 case DW_OP_breg7:
2281 case DW_OP_breg8:
2282 case DW_OP_breg9:
2283 case DW_OP_breg10:
2284 case DW_OP_breg11:
2285 case DW_OP_breg12:
2286 case DW_OP_breg13:
2287 case DW_OP_breg14:
2288 case DW_OP_breg15:
2289 case DW_OP_breg16:
2290 case DW_OP_breg17:
2291 case DW_OP_breg18:
2292 case DW_OP_breg19:
2293 case DW_OP_breg20:
2294 case DW_OP_breg21:
2295 case DW_OP_breg22:
2296 case DW_OP_breg23:
2297 case DW_OP_breg24:
2298 case DW_OP_breg25:
2299 case DW_OP_breg26:
2300 case DW_OP_breg27:
2301 case DW_OP_breg28:
2302 case DW_OP_breg29:
2303 case DW_OP_breg30:
2304 case DW_OP_breg31:
2305 case DW_OP_fbreg:
2306 fputc (',', asm_out_file);
2307 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2308 break;
2309
2310 case DW_OP_bregx:
2311 {
2312 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2313 gcc_assert (size_of_uleb128 (r)
2314 == size_of_uleb128 (val1->v.val_unsigned));
2315 fputc (',', asm_out_file);
2316 dw2_asm_output_data_uleb128_raw (r);
2317 fputc (',', asm_out_file);
2318 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2319 }
2320 break;
2321
2322 case DW_OP_GNU_implicit_pointer:
2323 case DW_OP_GNU_entry_value:
2324 case DW_OP_GNU_const_type:
2325 case DW_OP_GNU_regval_type:
2326 case DW_OP_GNU_deref_type:
2327 case DW_OP_GNU_convert:
2328 case DW_OP_GNU_reinterpret:
2329 case DW_OP_GNU_parameter_ref:
2330 gcc_unreachable ();
2331 break;
2332
2333 default:
2334 /* Other codes have no operands. */
2335 break;
2336 }
2337 }
2338
2339 void
2340 output_loc_sequence_raw (dw_loc_descr_ref loc)
2341 {
2342 while (1)
2343 {
2344 enum dwarf_location_atom opc = loc->dw_loc_opc;
2345 /* Output the opcode. */
2346 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2347 {
2348 unsigned r = (opc - DW_OP_breg0);
2349 r = DWARF2_FRAME_REG_OUT (r, 1);
2350 gcc_assert (r <= 31);
2351 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2352 }
2353 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2354 {
2355 unsigned r = (opc - DW_OP_reg0);
2356 r = DWARF2_FRAME_REG_OUT (r, 1);
2357 gcc_assert (r <= 31);
2358 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2359 }
2360 /* Output the opcode. */
2361 fprintf (asm_out_file, "%#x", opc);
2362 output_loc_operands_raw (loc);
2363
2364 if (!loc->dw_loc_next)
2365 break;
2366 loc = loc->dw_loc_next;
2367
2368 fputc (',', asm_out_file);
2369 }
2370 }
2371
2372 /* This function builds a dwarf location descriptor sequence from a
2373 dw_cfa_location, adding the given OFFSET to the result of the
2374 expression. */
2375
2376 struct dw_loc_descr_node *
2377 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2378 {
2379 struct dw_loc_descr_node *head, *tmp;
2380
2381 offset += cfa->offset;
2382
2383 if (cfa->indirect)
2384 {
2385 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2386 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2387 head->dw_loc_oprnd1.val_entry = NULL;
2388 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2389 add_loc_descr (&head, tmp);
2390 if (offset != 0)
2391 {
2392 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2393 add_loc_descr (&head, tmp);
2394 }
2395 }
2396 else
2397 head = new_reg_loc_descr (cfa->reg, offset);
2398
2399 return head;
2400 }
2401
2402 /* This function builds a dwarf location descriptor sequence for
2403 the address at OFFSET from the CFA when stack is aligned to
2404 ALIGNMENT byte. */
2405
2406 struct dw_loc_descr_node *
2407 build_cfa_aligned_loc (dw_cfa_location *cfa,
2408 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2409 {
2410 struct dw_loc_descr_node *head;
2411 unsigned int dwarf_fp
2412 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2413
2414 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2415 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2416 {
2417 head = new_reg_loc_descr (dwarf_fp, 0);
2418 add_loc_descr (&head, int_loc_descriptor (alignment));
2419 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2420 loc_descr_plus_const (&head, offset);
2421 }
2422 else
2423 head = new_reg_loc_descr (dwarf_fp, offset);
2424 return head;
2425 }
2426 \f
2427 /* And now, the support for symbolic debugging information. */
2428
2429 /* .debug_str support. */
2430
2431 static void dwarf2out_init (const char *);
2432 static void dwarf2out_finish (const char *);
2433 static void dwarf2out_early_finish (void);
2434 static void dwarf2out_assembly_start (void);
2435 static void dwarf2out_define (unsigned int, const char *);
2436 static void dwarf2out_undef (unsigned int, const char *);
2437 static void dwarf2out_start_source_file (unsigned, const char *);
2438 static void dwarf2out_end_source_file (unsigned);
2439 static void dwarf2out_function_decl (tree);
2440 static void dwarf2out_begin_block (unsigned, unsigned);
2441 static void dwarf2out_end_block (unsigned, unsigned);
2442 static bool dwarf2out_ignore_block (const_tree);
2443 static void dwarf2out_early_global_decl (tree);
2444 static void dwarf2out_late_global_decl (tree);
2445 static void dwarf2out_type_decl (tree, int);
2446 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2447 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2448 dw_die_ref);
2449 static void dwarf2out_abstract_function (tree);
2450 static void dwarf2out_var_location (rtx_insn *);
2451 static void dwarf2out_begin_function (tree);
2452 static void dwarf2out_end_function (unsigned int);
2453 static void dwarf2out_register_main_translation_unit (tree unit);
2454 static void dwarf2out_set_name (tree, tree);
2455
2456 /* The debug hooks structure. */
2457
2458 const struct gcc_debug_hooks dwarf2_debug_hooks =
2459 {
2460 dwarf2out_init,
2461 dwarf2out_finish,
2462 dwarf2out_early_finish,
2463 dwarf2out_assembly_start,
2464 dwarf2out_define,
2465 dwarf2out_undef,
2466 dwarf2out_start_source_file,
2467 dwarf2out_end_source_file,
2468 dwarf2out_begin_block,
2469 dwarf2out_end_block,
2470 dwarf2out_ignore_block,
2471 dwarf2out_source_line,
2472 dwarf2out_begin_prologue,
2473 #if VMS_DEBUGGING_INFO
2474 dwarf2out_vms_end_prologue,
2475 dwarf2out_vms_begin_epilogue,
2476 #else
2477 debug_nothing_int_charstar,
2478 debug_nothing_int_charstar,
2479 #endif
2480 dwarf2out_end_epilogue,
2481 dwarf2out_begin_function,
2482 dwarf2out_end_function, /* end_function */
2483 dwarf2out_register_main_translation_unit,
2484 dwarf2out_function_decl, /* function_decl */
2485 dwarf2out_early_global_decl,
2486 dwarf2out_late_global_decl,
2487 dwarf2out_type_decl, /* type_decl */
2488 dwarf2out_imported_module_or_decl,
2489 debug_nothing_tree, /* deferred_inline_function */
2490 /* The DWARF 2 backend tries to reduce debugging bloat by not
2491 emitting the abstract description of inline functions until
2492 something tries to reference them. */
2493 dwarf2out_abstract_function, /* outlining_inline_function */
2494 debug_nothing_rtx_code_label, /* label */
2495 debug_nothing_int, /* handle_pch */
2496 dwarf2out_var_location,
2497 dwarf2out_switch_text_section,
2498 dwarf2out_set_name,
2499 1, /* start_end_main_source_file */
2500 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2501 };
2502
2503 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2504 {
2505 dwarf2out_init,
2506 debug_nothing_charstar,
2507 debug_nothing_void,
2508 debug_nothing_void,
2509 debug_nothing_int_charstar,
2510 debug_nothing_int_charstar,
2511 debug_nothing_int_charstar,
2512 debug_nothing_int,
2513 debug_nothing_int_int, /* begin_block */
2514 debug_nothing_int_int, /* end_block */
2515 debug_true_const_tree, /* ignore_block */
2516 dwarf2out_source_line, /* source_line */
2517 debug_nothing_int_charstar, /* begin_prologue */
2518 debug_nothing_int_charstar, /* end_prologue */
2519 debug_nothing_int_charstar, /* begin_epilogue */
2520 debug_nothing_int_charstar, /* end_epilogue */
2521 debug_nothing_tree, /* begin_function */
2522 debug_nothing_int, /* end_function */
2523 debug_nothing_tree, /* register_main_translation_unit */
2524 debug_nothing_tree, /* function_decl */
2525 debug_nothing_tree, /* early_global_decl */
2526 debug_nothing_tree, /* late_global_decl */
2527 debug_nothing_tree_int, /* type_decl */
2528 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2529 debug_nothing_tree, /* deferred_inline_function */
2530 debug_nothing_tree, /* outlining_inline_function */
2531 debug_nothing_rtx_code_label, /* label */
2532 debug_nothing_int, /* handle_pch */
2533 debug_nothing_rtx_insn, /* var_location */
2534 debug_nothing_void, /* switch_text_section */
2535 debug_nothing_tree_tree, /* set_name */
2536 0, /* start_end_main_source_file */
2537 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2538 };
2539 \f
2540 /* NOTE: In the comments in this file, many references are made to
2541 "Debugging Information Entries". This term is abbreviated as `DIE'
2542 throughout the remainder of this file. */
2543
2544 /* An internal representation of the DWARF output is built, and then
2545 walked to generate the DWARF debugging info. The walk of the internal
2546 representation is done after the entire program has been compiled.
2547 The types below are used to describe the internal representation. */
2548
2549 /* Whether to put type DIEs into their own section .debug_types instead
2550 of making them part of the .debug_info section. Only supported for
2551 Dwarf V4 or higher and the user didn't disable them through
2552 -fno-debug-types-section. It is more efficient to put them in a
2553 separate comdat sections since the linker will then be able to
2554 remove duplicates. But not all tools support .debug_types sections
2555 yet. */
2556
2557 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2558
2559 /* Various DIE's use offsets relative to the beginning of the
2560 .debug_info section to refer to each other. */
2561
2562 typedef long int dw_offset;
2563
2564 struct comdat_type_node;
2565
2566 /* The entries in the line_info table more-or-less mirror the opcodes
2567 that are used in the real dwarf line table. Arrays of these entries
2568 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2569 supported. */
2570
2571 enum dw_line_info_opcode {
2572 /* Emit DW_LNE_set_address; the operand is the label index. */
2573 LI_set_address,
2574
2575 /* Emit a row to the matrix with the given line. This may be done
2576 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2577 special opcodes. */
2578 LI_set_line,
2579
2580 /* Emit a DW_LNS_set_file. */
2581 LI_set_file,
2582
2583 /* Emit a DW_LNS_set_column. */
2584 LI_set_column,
2585
2586 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2587 LI_negate_stmt,
2588
2589 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2590 LI_set_prologue_end,
2591 LI_set_epilogue_begin,
2592
2593 /* Emit a DW_LNE_set_discriminator. */
2594 LI_set_discriminator
2595 };
2596
2597 typedef struct GTY(()) dw_line_info_struct {
2598 enum dw_line_info_opcode opcode;
2599 unsigned int val;
2600 } dw_line_info_entry;
2601
2602
2603 struct GTY(()) dw_line_info_table {
2604 /* The label that marks the end of this section. */
2605 const char *end_label;
2606
2607 /* The values for the last row of the matrix, as collected in the table.
2608 These are used to minimize the changes to the next row. */
2609 unsigned int file_num;
2610 unsigned int line_num;
2611 unsigned int column_num;
2612 int discrim_num;
2613 bool is_stmt;
2614 bool in_use;
2615
2616 vec<dw_line_info_entry, va_gc> *entries;
2617 };
2618
2619
2620 /* Each DIE attribute has a field specifying the attribute kind,
2621 a link to the next attribute in the chain, and an attribute value.
2622 Attributes are typically linked below the DIE they modify. */
2623
2624 typedef struct GTY(()) dw_attr_struct {
2625 enum dwarf_attribute dw_attr;
2626 dw_val_node dw_attr_val;
2627 }
2628 dw_attr_node;
2629
2630
2631 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2632 The children of each node form a circular list linked by
2633 die_sib. die_child points to the node *before* the "first" child node. */
2634
2635 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2636 union die_symbol_or_type_node
2637 {
2638 const char * GTY ((tag ("0"))) die_symbol;
2639 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2640 }
2641 GTY ((desc ("%0.comdat_type_p"))) die_id;
2642 vec<dw_attr_node, va_gc> *die_attr;
2643 dw_die_ref die_parent;
2644 dw_die_ref die_child;
2645 dw_die_ref die_sib;
2646 dw_die_ref die_definition; /* ref from a specification to its definition */
2647 dw_offset die_offset;
2648 unsigned long die_abbrev;
2649 int die_mark;
2650 unsigned int decl_id;
2651 enum dwarf_tag die_tag;
2652 /* Die is used and must not be pruned as unused. */
2653 BOOL_BITFIELD die_perennial_p : 1;
2654 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2655 /* Lots of spare bits. */
2656 }
2657 die_node;
2658
2659 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2660 static bool early_dwarf;
2661 struct set_early_dwarf {
2662 bool saved;
2663 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2664 ~set_early_dwarf () { early_dwarf = saved; }
2665 };
2666
2667 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2668 #define FOR_EACH_CHILD(die, c, expr) do { \
2669 c = die->die_child; \
2670 if (c) do { \
2671 c = c->die_sib; \
2672 expr; \
2673 } while (c != die->die_child); \
2674 } while (0)
2675
2676 /* The pubname structure */
2677
2678 typedef struct GTY(()) pubname_struct {
2679 dw_die_ref die;
2680 const char *name;
2681 }
2682 pubname_entry;
2683
2684
2685 struct GTY(()) dw_ranges {
2686 /* If this is positive, it's a block number, otherwise it's a
2687 bitwise-negated index into dw_ranges_by_label. */
2688 int num;
2689 };
2690
2691 /* A structure to hold a macinfo entry. */
2692
2693 typedef struct GTY(()) macinfo_struct {
2694 unsigned char code;
2695 unsigned HOST_WIDE_INT lineno;
2696 const char *info;
2697 }
2698 macinfo_entry;
2699
2700
2701 struct GTY(()) dw_ranges_by_label {
2702 const char *begin;
2703 const char *end;
2704 };
2705
2706 /* The comdat type node structure. */
2707 struct GTY(()) comdat_type_node
2708 {
2709 dw_die_ref root_die;
2710 dw_die_ref type_die;
2711 dw_die_ref skeleton_die;
2712 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2713 comdat_type_node *next;
2714 };
2715
2716 /* A list of DIEs for which we can't determine ancestry (parent_die
2717 field) just yet. Later in dwarf2out_finish we will fill in the
2718 missing bits. */
2719 typedef struct GTY(()) limbo_die_struct {
2720 dw_die_ref die;
2721 /* The tree for which this DIE was created. We use this to
2722 determine ancestry later. */
2723 tree created_for;
2724 struct limbo_die_struct *next;
2725 }
2726 limbo_die_node;
2727
2728 typedef struct skeleton_chain_struct
2729 {
2730 dw_die_ref old_die;
2731 dw_die_ref new_die;
2732 struct skeleton_chain_struct *parent;
2733 }
2734 skeleton_chain_node;
2735
2736 /* Define a macro which returns nonzero for a TYPE_DECL which was
2737 implicitly generated for a type.
2738
2739 Note that, unlike the C front-end (which generates a NULL named
2740 TYPE_DECL node for each complete tagged type, each array type,
2741 and each function type node created) the C++ front-end generates
2742 a _named_ TYPE_DECL node for each tagged type node created.
2743 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2744 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2745 front-end, but for each type, tagged or not. */
2746
2747 #define TYPE_DECL_IS_STUB(decl) \
2748 (DECL_NAME (decl) == NULL_TREE \
2749 || (DECL_ARTIFICIAL (decl) \
2750 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2751 /* This is necessary for stub decls that \
2752 appear in nested inline functions. */ \
2753 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2754 && (decl_ultimate_origin (decl) \
2755 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2756
2757 /* Information concerning the compilation unit's programming
2758 language, and compiler version. */
2759
2760 /* Fixed size portion of the DWARF compilation unit header. */
2761 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2762 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2763
2764 /* Fixed size portion of the DWARF comdat type unit header. */
2765 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2766 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2767 + DWARF_OFFSET_SIZE)
2768
2769 /* Fixed size portion of public names info. */
2770 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2771
2772 /* Fixed size portion of the address range info. */
2773 #define DWARF_ARANGES_HEADER_SIZE \
2774 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2775 DWARF2_ADDR_SIZE * 2) \
2776 - DWARF_INITIAL_LENGTH_SIZE)
2777
2778 /* Size of padding portion in the address range info. It must be
2779 aligned to twice the pointer size. */
2780 #define DWARF_ARANGES_PAD_SIZE \
2781 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2782 DWARF2_ADDR_SIZE * 2) \
2783 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2784
2785 /* Use assembler line directives if available. */
2786 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2787 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2788 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2789 #else
2790 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2791 #endif
2792 #endif
2793
2794 /* Minimum line offset in a special line info. opcode.
2795 This value was chosen to give a reasonable range of values. */
2796 #define DWARF_LINE_BASE -10
2797
2798 /* First special line opcode - leave room for the standard opcodes. */
2799 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2800
2801 /* Range of line offsets in a special line info. opcode. */
2802 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2803
2804 /* Flag that indicates the initial value of the is_stmt_start flag.
2805 In the present implementation, we do not mark any lines as
2806 the beginning of a source statement, because that information
2807 is not made available by the GCC front-end. */
2808 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2809
2810 /* Maximum number of operations per instruction bundle. */
2811 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2812 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2813 #endif
2814
2815 /* This location is used by calc_die_sizes() to keep track
2816 the offset of each DIE within the .debug_info section. */
2817 static unsigned long next_die_offset;
2818
2819 /* Record the root of the DIE's built for the current compilation unit. */
2820 static GTY(()) dw_die_ref single_comp_unit_die;
2821
2822 /* A list of type DIEs that have been separated into comdat sections. */
2823 static GTY(()) comdat_type_node *comdat_type_list;
2824
2825 /* A list of DIEs with a NULL parent waiting to be relocated. */
2826 static GTY(()) limbo_die_node *limbo_die_list;
2827
2828 /* A list of DIEs for which we may have to generate
2829 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2830 static GTY(()) limbo_die_node *deferred_asm_name;
2831
2832 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2833 {
2834 typedef const char *compare_type;
2835
2836 static hashval_t hash (dwarf_file_data *);
2837 static bool equal (dwarf_file_data *, const char *);
2838 };
2839
2840 /* Filenames referenced by this compilation unit. */
2841 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2842
2843 struct decl_die_hasher : ggc_ptr_hash<die_node>
2844 {
2845 typedef tree compare_type;
2846
2847 static hashval_t hash (die_node *);
2848 static bool equal (die_node *, tree);
2849 };
2850 /* A hash table of references to DIE's that describe declarations.
2851 The key is a DECL_UID() which is a unique number identifying each decl. */
2852 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2853
2854 struct block_die_hasher : ggc_ptr_hash<die_struct>
2855 {
2856 static hashval_t hash (die_struct *);
2857 static bool equal (die_struct *, die_struct *);
2858 };
2859
2860 /* A hash table of references to DIE's that describe COMMON blocks.
2861 The key is DECL_UID() ^ die_parent. */
2862 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2863
2864 typedef struct GTY(()) die_arg_entry_struct {
2865 dw_die_ref die;
2866 tree arg;
2867 } die_arg_entry;
2868
2869
2870 /* Node of the variable location list. */
2871 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2872 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2873 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2874 in mode of the EXPR_LIST node and first EXPR_LIST operand
2875 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2876 location or NULL for padding. For larger bitsizes,
2877 mode is 0 and first operand is a CONCAT with bitsize
2878 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2879 NULL as second operand. */
2880 rtx GTY (()) loc;
2881 const char * GTY (()) label;
2882 struct var_loc_node * GTY (()) next;
2883 };
2884
2885 /* Variable location list. */
2886 struct GTY ((for_user)) var_loc_list_def {
2887 struct var_loc_node * GTY (()) first;
2888
2889 /* Pointer to the last but one or last element of the
2890 chained list. If the list is empty, both first and
2891 last are NULL, if the list contains just one node
2892 or the last node certainly is not redundant, it points
2893 to the last node, otherwise points to the last but one.
2894 Do not mark it for GC because it is marked through the chain. */
2895 struct var_loc_node * GTY ((skip ("%h"))) last;
2896
2897 /* Pointer to the last element before section switch,
2898 if NULL, either sections weren't switched or first
2899 is after section switch. */
2900 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2901
2902 /* DECL_UID of the variable decl. */
2903 unsigned int decl_id;
2904 };
2905 typedef struct var_loc_list_def var_loc_list;
2906
2907 /* Call argument location list. */
2908 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2909 rtx GTY (()) call_arg_loc_note;
2910 const char * GTY (()) label;
2911 tree GTY (()) block;
2912 bool tail_call_p;
2913 rtx GTY (()) symbol_ref;
2914 struct call_arg_loc_node * GTY (()) next;
2915 };
2916
2917
2918 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2919 {
2920 typedef const_tree compare_type;
2921
2922 static hashval_t hash (var_loc_list *);
2923 static bool equal (var_loc_list *, const_tree);
2924 };
2925
2926 /* Table of decl location linked lists. */
2927 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2928
2929 /* Head and tail of call_arg_loc chain. */
2930 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2931 static struct call_arg_loc_node *call_arg_loc_last;
2932
2933 /* Number of call sites in the current function. */
2934 static int call_site_count = -1;
2935 /* Number of tail call sites in the current function. */
2936 static int tail_call_site_count = -1;
2937
2938 /* A cached location list. */
2939 struct GTY ((for_user)) cached_dw_loc_list_def {
2940 /* The DECL_UID of the decl that this entry describes. */
2941 unsigned int decl_id;
2942
2943 /* The cached location list. */
2944 dw_loc_list_ref loc_list;
2945 };
2946 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2947
2948 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2949 {
2950
2951 typedef const_tree compare_type;
2952
2953 static hashval_t hash (cached_dw_loc_list *);
2954 static bool equal (cached_dw_loc_list *, const_tree);
2955 };
2956
2957 /* Table of cached location lists. */
2958 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2959
2960 /* A pointer to the base of a list of references to DIE's that
2961 are uniquely identified by their tag, presence/absence of
2962 children DIE's, and list of attribute/value pairs. */
2963 static GTY((length ("abbrev_die_table_allocated")))
2964 dw_die_ref *abbrev_die_table;
2965
2966 /* Number of elements currently allocated for abbrev_die_table. */
2967 static GTY(()) unsigned abbrev_die_table_allocated;
2968
2969 /* Number of elements in abbrev_die_table currently in use. */
2970 static GTY(()) unsigned abbrev_die_table_in_use;
2971
2972 /* Size (in elements) of increments by which we may expand the
2973 abbrev_die_table. */
2974 #define ABBREV_DIE_TABLE_INCREMENT 256
2975
2976 /* A global counter for generating labels for line number data. */
2977 static unsigned int line_info_label_num;
2978
2979 /* The current table to which we should emit line number information
2980 for the current function. This will be set up at the beginning of
2981 assembly for the function. */
2982 static dw_line_info_table *cur_line_info_table;
2983
2984 /* The two default tables of line number info. */
2985 static GTY(()) dw_line_info_table *text_section_line_info;
2986 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2987
2988 /* The set of all non-default tables of line number info. */
2989 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
2990
2991 /* A flag to tell pubnames/types export if there is an info section to
2992 refer to. */
2993 static bool info_section_emitted;
2994
2995 /* A pointer to the base of a table that contains a list of publicly
2996 accessible names. */
2997 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2998
2999 /* A pointer to the base of a table that contains a list of publicly
3000 accessible types. */
3001 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3002
3003 /* A pointer to the base of a table that contains a list of macro
3004 defines/undefines (and file start/end markers). */
3005 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3006
3007 /* True if .debug_macinfo or .debug_macros section is going to be
3008 emitted. */
3009 #define have_macinfo \
3010 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3011 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3012 && !macinfo_table->is_empty ())
3013
3014 /* Array of dies for which we should generate .debug_ranges info. */
3015 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3016
3017 /* Number of elements currently allocated for ranges_table. */
3018 static GTY(()) unsigned ranges_table_allocated;
3019
3020 /* Number of elements in ranges_table currently in use. */
3021 static GTY(()) unsigned ranges_table_in_use;
3022
3023 /* Array of pairs of labels referenced in ranges_table. */
3024 static GTY ((length ("ranges_by_label_allocated")))
3025 dw_ranges_by_label *ranges_by_label;
3026
3027 /* Number of elements currently allocated for ranges_by_label. */
3028 static GTY(()) unsigned ranges_by_label_allocated;
3029
3030 /* Number of elements in ranges_by_label currently in use. */
3031 static GTY(()) unsigned ranges_by_label_in_use;
3032
3033 /* Size (in elements) of increments by which we may expand the
3034 ranges_table. */
3035 #define RANGES_TABLE_INCREMENT 64
3036
3037 /* Whether we have location lists that need outputting */
3038 static GTY(()) bool have_location_lists;
3039
3040 /* Unique label counter. */
3041 static GTY(()) unsigned int loclabel_num;
3042
3043 /* Unique label counter for point-of-call tables. */
3044 static GTY(()) unsigned int poc_label_num;
3045
3046 /* The last file entry emitted by maybe_emit_file(). */
3047 static GTY(()) struct dwarf_file_data * last_emitted_file;
3048
3049 /* Number of internal labels generated by gen_internal_sym(). */
3050 static GTY(()) int label_num;
3051
3052 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3053
3054 /* Instances of generic types for which we need to generate debug
3055 info that describe their generic parameters and arguments. That
3056 generation needs to happen once all types are properly laid out so
3057 we do it at the end of compilation. */
3058 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3059
3060 /* Offset from the "steady-state frame pointer" to the frame base,
3061 within the current function. */
3062 static HOST_WIDE_INT frame_pointer_fb_offset;
3063 static bool frame_pointer_fb_offset_valid;
3064
3065 static vec<dw_die_ref> base_types;
3066
3067 /* Flags to represent a set of attribute classes for attributes that represent
3068 a scalar value (bounds, pointers, ...). */
3069 enum dw_scalar_form
3070 {
3071 dw_scalar_form_constant = 0x01,
3072 dw_scalar_form_exprloc = 0x02,
3073 dw_scalar_form_reference = 0x04
3074 };
3075
3076 /* Forward declarations for functions defined in this file. */
3077
3078 static int is_pseudo_reg (const_rtx);
3079 static tree type_main_variant (tree);
3080 static int is_tagged_type (const_tree);
3081 static const char *dwarf_tag_name (unsigned);
3082 static const char *dwarf_attr_name (unsigned);
3083 static const char *dwarf_form_name (unsigned);
3084 static tree decl_ultimate_origin (const_tree);
3085 static tree decl_class_context (tree);
3086 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3087 static inline enum dw_val_class AT_class (dw_attr_node *);
3088 static inline unsigned int AT_index (dw_attr_node *);
3089 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3090 static inline unsigned AT_flag (dw_attr_node *);
3091 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3092 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3093 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3094 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3095 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3096 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3097 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3098 unsigned int, unsigned char *);
3099 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3100 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3101 static inline const char *AT_string (dw_attr_node *);
3102 static enum dwarf_form AT_string_form (dw_attr_node *);
3103 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3104 static void add_AT_specification (dw_die_ref, dw_die_ref);
3105 static inline dw_die_ref AT_ref (dw_attr_node *);
3106 static inline int AT_ref_external (dw_attr_node *);
3107 static inline void set_AT_ref_external (dw_attr_node *, int);
3108 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3109 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3110 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3111 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3112 dw_loc_list_ref);
3113 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3114 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3115 static void remove_addr_table_entry (addr_table_entry *);
3116 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3117 static inline rtx AT_addr (dw_attr_node *);
3118 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3119 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3120 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3121 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3122 unsigned HOST_WIDE_INT);
3123 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3124 unsigned long, bool);
3125 static inline const char *AT_lbl (dw_attr_node *);
3126 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3127 static const char *get_AT_low_pc (dw_die_ref);
3128 static const char *get_AT_hi_pc (dw_die_ref);
3129 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3130 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3131 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3132 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3133 static bool is_cxx (void);
3134 static bool is_fortran (void);
3135 static bool is_ada (void);
3136 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3137 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3138 static void add_child_die (dw_die_ref, dw_die_ref);
3139 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3140 static dw_die_ref lookup_type_die (tree);
3141 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3142 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3143 static void equate_type_number_to_die (tree, dw_die_ref);
3144 static dw_die_ref lookup_decl_die (tree);
3145 static var_loc_list *lookup_decl_loc (const_tree);
3146 static void equate_decl_number_to_die (tree, dw_die_ref);
3147 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3148 static void print_spaces (FILE *);
3149 static void print_die (dw_die_ref, FILE *);
3150 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3151 static dw_die_ref pop_compile_unit (dw_die_ref);
3152 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3153 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3154 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3155 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3156 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3157 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3158 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3159 struct md5_ctx *, int *);
3160 struct checksum_attributes;
3161 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3162 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3163 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3164 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3165 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3166 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3167 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3168 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3169 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3170 static void compute_section_prefix (dw_die_ref);
3171 static int is_type_die (dw_die_ref);
3172 static int is_comdat_die (dw_die_ref);
3173 static int is_symbol_die (dw_die_ref);
3174 static inline bool is_template_instantiation (dw_die_ref);
3175 static void assign_symbol_names (dw_die_ref);
3176 static void break_out_includes (dw_die_ref);
3177 static int is_declaration_die (dw_die_ref);
3178 static int should_move_die_to_comdat (dw_die_ref);
3179 static dw_die_ref clone_as_declaration (dw_die_ref);
3180 static dw_die_ref clone_die (dw_die_ref);
3181 static dw_die_ref clone_tree (dw_die_ref);
3182 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3183 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3184 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3185 static dw_die_ref generate_skeleton (dw_die_ref);
3186 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3187 dw_die_ref,
3188 dw_die_ref);
3189 static void break_out_comdat_types (dw_die_ref);
3190 static void copy_decls_for_unworthy_types (dw_die_ref);
3191
3192 static void add_sibling_attributes (dw_die_ref);
3193 static void output_location_lists (dw_die_ref);
3194 static int constant_size (unsigned HOST_WIDE_INT);
3195 static unsigned long size_of_die (dw_die_ref);
3196 static void calc_die_sizes (dw_die_ref);
3197 static void calc_base_type_die_sizes (void);
3198 static void mark_dies (dw_die_ref);
3199 static void unmark_dies (dw_die_ref);
3200 static void unmark_all_dies (dw_die_ref);
3201 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3202 static unsigned long size_of_aranges (void);
3203 static enum dwarf_form value_format (dw_attr_node *);
3204 static void output_value_format (dw_attr_node *);
3205 static void output_abbrev_section (void);
3206 static void output_die_abbrevs (unsigned long, dw_die_ref);
3207 static void output_die_symbol (dw_die_ref);
3208 static void output_die (dw_die_ref);
3209 static void output_compilation_unit_header (void);
3210 static void output_comp_unit (dw_die_ref, int);
3211 static void output_comdat_type_unit (comdat_type_node *);
3212 static const char *dwarf2_name (tree, int);
3213 static void add_pubname (tree, dw_die_ref);
3214 static void add_enumerator_pubname (const char *, dw_die_ref);
3215 static void add_pubname_string (const char *, dw_die_ref);
3216 static void add_pubtype (tree, dw_die_ref);
3217 static void output_pubnames (vec<pubname_entry, va_gc> *);
3218 static void output_aranges (void);
3219 static unsigned int add_ranges_num (int);
3220 static unsigned int add_ranges (const_tree);
3221 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3222 bool *, bool);
3223 static void output_ranges (void);
3224 static dw_line_info_table *new_line_info_table (void);
3225 static void output_line_info (bool);
3226 static void output_file_names (void);
3227 static dw_die_ref base_type_die (tree);
3228 static int is_base_type (tree);
3229 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3230 static int decl_quals (const_tree);
3231 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3232 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3233 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3234 static int type_is_enum (const_tree);
3235 static unsigned int dbx_reg_number (const_rtx);
3236 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3237 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3238 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3239 enum var_init_status);
3240 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3241 enum var_init_status);
3242 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3243 enum var_init_status);
3244 static int is_based_loc (const_rtx);
3245 static bool resolve_one_addr (rtx *);
3246 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3247 enum var_init_status);
3248 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3249 enum var_init_status);
3250 struct loc_descr_context;
3251 static dw_loc_list_ref loc_list_from_tree (tree, int,
3252 const struct loc_descr_context *);
3253 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3254 const struct loc_descr_context *);
3255 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3256 static tree field_type (const_tree);
3257 static unsigned int simple_type_align_in_bits (const_tree);
3258 static unsigned int simple_decl_align_in_bits (const_tree);
3259 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3260 static HOST_WIDE_INT field_byte_offset (const_tree);
3261 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3262 dw_loc_list_ref);
3263 static void add_data_member_location_attribute (dw_die_ref, tree);
3264 static bool add_const_value_attribute (dw_die_ref, rtx);
3265 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3266 static void insert_wide_int (const wide_int &, unsigned char *, int);
3267 static void insert_float (const_rtx, unsigned char *);
3268 static rtx rtl_for_decl_location (tree);
3269 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3270 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3271 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3272 static void add_name_attribute (dw_die_ref, const char *);
3273 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3274 static void add_comp_dir_attribute (dw_die_ref);
3275 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3276 const struct loc_descr_context *);
3277 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3278 const struct loc_descr_context *);
3279 static void add_subscript_info (dw_die_ref, tree, bool);
3280 static void add_byte_size_attribute (dw_die_ref, tree);
3281 static void add_bit_offset_attribute (dw_die_ref, tree);
3282 static void add_bit_size_attribute (dw_die_ref, tree);
3283 static void add_prototyped_attribute (dw_die_ref, tree);
3284 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3285 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3286 static void add_src_coords_attributes (dw_die_ref, tree);
3287 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3288 static void push_decl_scope (tree);
3289 static void pop_decl_scope (void);
3290 static dw_die_ref scope_die_for (tree, dw_die_ref);
3291 static inline int local_scope_p (dw_die_ref);
3292 static inline int class_scope_p (dw_die_ref);
3293 static inline int class_or_namespace_scope_p (dw_die_ref);
3294 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3295 static void add_calling_convention_attribute (dw_die_ref, tree);
3296 static const char *type_tag (const_tree);
3297 static tree member_declared_type (const_tree);
3298 #if 0
3299 static const char *decl_start_label (tree);
3300 #endif
3301 static void gen_array_type_die (tree, dw_die_ref);
3302 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3303 #if 0
3304 static void gen_entry_point_die (tree, dw_die_ref);
3305 #endif
3306 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3307 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3308 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3309 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3310 static void gen_formal_types_die (tree, dw_die_ref);
3311 static void gen_subprogram_die (tree, dw_die_ref);
3312 static void gen_variable_die (tree, tree, dw_die_ref);
3313 static void gen_const_die (tree, dw_die_ref);
3314 static void gen_label_die (tree, dw_die_ref);
3315 static void gen_lexical_block_die (tree, dw_die_ref);
3316 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3317 static void gen_field_die (tree, dw_die_ref);
3318 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3319 static dw_die_ref gen_compile_unit_die (const char *);
3320 static void gen_inheritance_die (tree, tree, dw_die_ref);
3321 static void gen_member_die (tree, dw_die_ref);
3322 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3323 enum debug_info_usage);
3324 static void gen_subroutine_type_die (tree, dw_die_ref);
3325 static void gen_typedef_die (tree, dw_die_ref);
3326 static void gen_type_die (tree, dw_die_ref);
3327 static void gen_block_die (tree, dw_die_ref);
3328 static void decls_for_scope (tree, dw_die_ref);
3329 static inline int is_redundant_typedef (const_tree);
3330 static bool is_naming_typedef_decl (const_tree);
3331 static inline dw_die_ref get_context_die (tree);
3332 static void gen_namespace_die (tree, dw_die_ref);
3333 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3334 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3335 static dw_die_ref force_decl_die (tree);
3336 static dw_die_ref force_type_die (tree);
3337 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3338 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3339 static struct dwarf_file_data * lookup_filename (const char *);
3340 static void retry_incomplete_types (void);
3341 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3342 static void gen_generic_params_dies (tree);
3343 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3344 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3345 static void splice_child_die (dw_die_ref, dw_die_ref);
3346 static int file_info_cmp (const void *, const void *);
3347 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3348 const char *, const char *);
3349 static void output_loc_list (dw_loc_list_ref);
3350 static char *gen_internal_sym (const char *);
3351 static bool want_pubnames (void);
3352
3353 static void prune_unmark_dies (dw_die_ref);
3354 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3355 static void prune_unused_types_mark (dw_die_ref, int);
3356 static void prune_unused_types_walk (dw_die_ref);
3357 static void prune_unused_types_walk_attribs (dw_die_ref);
3358 static void prune_unused_types_prune (dw_die_ref);
3359 static void prune_unused_types (void);
3360 static int maybe_emit_file (struct dwarf_file_data *fd);
3361 static inline const char *AT_vms_delta1 (dw_attr_node *);
3362 static inline const char *AT_vms_delta2 (dw_attr_node *);
3363 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3364 const char *, const char *);
3365 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3366 static void gen_remaining_tmpl_value_param_die_attribute (void);
3367 static bool generic_type_p (tree);
3368 static void schedule_generic_params_dies_gen (tree t);
3369 static void gen_scheduled_generic_parms_dies (void);
3370
3371 static const char *comp_dir_string (void);
3372
3373 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3374
3375 /* enum for tracking thread-local variables whose address is really an offset
3376 relative to the TLS pointer, which will need link-time relocation, but will
3377 not need relocation by the DWARF consumer. */
3378
3379 enum dtprel_bool
3380 {
3381 dtprel_false = 0,
3382 dtprel_true = 1
3383 };
3384
3385 /* Return the operator to use for an address of a variable. For dtprel_true, we
3386 use DW_OP_const*. For regular variables, which need both link-time
3387 relocation and consumer-level relocation (e.g., to account for shared objects
3388 loaded at a random address), we use DW_OP_addr*. */
3389
3390 static inline enum dwarf_location_atom
3391 dw_addr_op (enum dtprel_bool dtprel)
3392 {
3393 if (dtprel == dtprel_true)
3394 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3395 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3396 else
3397 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3398 }
3399
3400 /* Return a pointer to a newly allocated address location description. If
3401 dwarf_split_debug_info is true, then record the address with the appropriate
3402 relocation. */
3403 static inline dw_loc_descr_ref
3404 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3405 {
3406 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3407
3408 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3409 ref->dw_loc_oprnd1.v.val_addr = addr;
3410 ref->dtprel = dtprel;
3411 if (dwarf_split_debug_info)
3412 ref->dw_loc_oprnd1.val_entry
3413 = add_addr_table_entry (addr,
3414 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3415 else
3416 ref->dw_loc_oprnd1.val_entry = NULL;
3417
3418 return ref;
3419 }
3420
3421 /* Section names used to hold DWARF debugging information. */
3422
3423 #ifndef DEBUG_INFO_SECTION
3424 #define DEBUG_INFO_SECTION ".debug_info"
3425 #endif
3426 #ifndef DEBUG_DWO_INFO_SECTION
3427 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3428 #endif
3429 #ifndef DEBUG_ABBREV_SECTION
3430 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3431 #endif
3432 #ifndef DEBUG_DWO_ABBREV_SECTION
3433 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3434 #endif
3435 #ifndef DEBUG_ARANGES_SECTION
3436 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3437 #endif
3438 #ifndef DEBUG_ADDR_SECTION
3439 #define DEBUG_ADDR_SECTION ".debug_addr"
3440 #endif
3441 #ifndef DEBUG_NORM_MACINFO_SECTION
3442 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3443 #endif
3444 #ifndef DEBUG_DWO_MACINFO_SECTION
3445 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3446 #endif
3447 #ifndef DEBUG_MACINFO_SECTION
3448 #define DEBUG_MACINFO_SECTION \
3449 (!dwarf_split_debug_info \
3450 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3451 #endif
3452 #ifndef DEBUG_NORM_MACRO_SECTION
3453 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3454 #endif
3455 #ifndef DEBUG_DWO_MACRO_SECTION
3456 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3457 #endif
3458 #ifndef DEBUG_MACRO_SECTION
3459 #define DEBUG_MACRO_SECTION \
3460 (!dwarf_split_debug_info \
3461 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3462 #endif
3463 #ifndef DEBUG_LINE_SECTION
3464 #define DEBUG_LINE_SECTION ".debug_line"
3465 #endif
3466 #ifndef DEBUG_DWO_LINE_SECTION
3467 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3468 #endif
3469 #ifndef DEBUG_LOC_SECTION
3470 #define DEBUG_LOC_SECTION ".debug_loc"
3471 #endif
3472 #ifndef DEBUG_DWO_LOC_SECTION
3473 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3474 #endif
3475 #ifndef DEBUG_PUBNAMES_SECTION
3476 #define DEBUG_PUBNAMES_SECTION \
3477 ((debug_generate_pub_sections == 2) \
3478 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3479 #endif
3480 #ifndef DEBUG_PUBTYPES_SECTION
3481 #define DEBUG_PUBTYPES_SECTION \
3482 ((debug_generate_pub_sections == 2) \
3483 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3484 #endif
3485 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3486 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3487 #ifndef DEBUG_STR_OFFSETS_SECTION
3488 #define DEBUG_STR_OFFSETS_SECTION \
3489 (!dwarf_split_debug_info \
3490 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3491 #endif
3492 #ifndef DEBUG_STR_DWO_SECTION
3493 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3494 #endif
3495 #ifndef DEBUG_STR_SECTION
3496 #define DEBUG_STR_SECTION ".debug_str"
3497 #endif
3498 #ifndef DEBUG_RANGES_SECTION
3499 #define DEBUG_RANGES_SECTION ".debug_ranges"
3500 #endif
3501
3502 /* Standard ELF section names for compiled code and data. */
3503 #ifndef TEXT_SECTION_NAME
3504 #define TEXT_SECTION_NAME ".text"
3505 #endif
3506
3507 /* Section flags for .debug_macinfo/.debug_macro section. */
3508 #define DEBUG_MACRO_SECTION_FLAGS \
3509 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3510
3511 /* Section flags for .debug_str section. */
3512 #define DEBUG_STR_SECTION_FLAGS \
3513 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3514 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3515 : SECTION_DEBUG)
3516
3517 /* Section flags for .debug_str.dwo section. */
3518 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3519
3520 /* Labels we insert at beginning sections we can reference instead of
3521 the section names themselves. */
3522
3523 #ifndef TEXT_SECTION_LABEL
3524 #define TEXT_SECTION_LABEL "Ltext"
3525 #endif
3526 #ifndef COLD_TEXT_SECTION_LABEL
3527 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3528 #endif
3529 #ifndef DEBUG_LINE_SECTION_LABEL
3530 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3531 #endif
3532 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3533 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3534 #endif
3535 #ifndef DEBUG_INFO_SECTION_LABEL
3536 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3537 #endif
3538 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3539 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3540 #endif
3541 #ifndef DEBUG_ABBREV_SECTION_LABEL
3542 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3543 #endif
3544 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3545 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3546 #endif
3547 #ifndef DEBUG_ADDR_SECTION_LABEL
3548 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3549 #endif
3550 #ifndef DEBUG_LOC_SECTION_LABEL
3551 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3552 #endif
3553 #ifndef DEBUG_RANGES_SECTION_LABEL
3554 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3555 #endif
3556 #ifndef DEBUG_MACINFO_SECTION_LABEL
3557 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3558 #endif
3559 #ifndef DEBUG_MACRO_SECTION_LABEL
3560 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3561 #endif
3562 #define SKELETON_COMP_DIE_ABBREV 1
3563 #define SKELETON_TYPE_DIE_ABBREV 2
3564
3565 /* Definitions of defaults for formats and names of various special
3566 (artificial) labels which may be generated within this file (when the -g
3567 options is used and DWARF2_DEBUGGING_INFO is in effect.
3568 If necessary, these may be overridden from within the tm.h file, but
3569 typically, overriding these defaults is unnecessary. */
3570
3571 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3572 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3573 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3574 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3575 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3583 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3584 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3585
3586 #ifndef TEXT_END_LABEL
3587 #define TEXT_END_LABEL "Letext"
3588 #endif
3589 #ifndef COLD_END_LABEL
3590 #define COLD_END_LABEL "Letext_cold"
3591 #endif
3592 #ifndef BLOCK_BEGIN_LABEL
3593 #define BLOCK_BEGIN_LABEL "LBB"
3594 #endif
3595 #ifndef BLOCK_END_LABEL
3596 #define BLOCK_END_LABEL "LBE"
3597 #endif
3598 #ifndef LINE_CODE_LABEL
3599 #define LINE_CODE_LABEL "LM"
3600 #endif
3601
3602 \f
3603 /* Return the root of the DIE's built for the current compilation unit. */
3604 static dw_die_ref
3605 comp_unit_die (void)
3606 {
3607 if (!single_comp_unit_die)
3608 single_comp_unit_die = gen_compile_unit_die (NULL);
3609 return single_comp_unit_die;
3610 }
3611
3612 /* We allow a language front-end to designate a function that is to be
3613 called to "demangle" any name before it is put into a DIE. */
3614
3615 static const char *(*demangle_name_func) (const char *);
3616
3617 void
3618 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3619 {
3620 demangle_name_func = func;
3621 }
3622
3623 /* Test if rtl node points to a pseudo register. */
3624
3625 static inline int
3626 is_pseudo_reg (const_rtx rtl)
3627 {
3628 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3629 || (GET_CODE (rtl) == SUBREG
3630 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3631 }
3632
3633 /* Return a reference to a type, with its const and volatile qualifiers
3634 removed. */
3635
3636 static inline tree
3637 type_main_variant (tree type)
3638 {
3639 type = TYPE_MAIN_VARIANT (type);
3640
3641 /* ??? There really should be only one main variant among any group of
3642 variants of a given type (and all of the MAIN_VARIANT values for all
3643 members of the group should point to that one type) but sometimes the C
3644 front-end messes this up for array types, so we work around that bug
3645 here. */
3646 if (TREE_CODE (type) == ARRAY_TYPE)
3647 while (type != TYPE_MAIN_VARIANT (type))
3648 type = TYPE_MAIN_VARIANT (type);
3649
3650 return type;
3651 }
3652
3653 /* Return nonzero if the given type node represents a tagged type. */
3654
3655 static inline int
3656 is_tagged_type (const_tree type)
3657 {
3658 enum tree_code code = TREE_CODE (type);
3659
3660 return (code == RECORD_TYPE || code == UNION_TYPE
3661 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3662 }
3663
3664 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3665
3666 static void
3667 get_ref_die_offset_label (char *label, dw_die_ref ref)
3668 {
3669 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3670 }
3671
3672 /* Return die_offset of a DIE reference to a base type. */
3673
3674 static unsigned long int
3675 get_base_type_offset (dw_die_ref ref)
3676 {
3677 if (ref->die_offset)
3678 return ref->die_offset;
3679 if (comp_unit_die ()->die_abbrev)
3680 {
3681 calc_base_type_die_sizes ();
3682 gcc_assert (ref->die_offset);
3683 }
3684 return ref->die_offset;
3685 }
3686
3687 /* Return die_offset of a DIE reference other than base type. */
3688
3689 static unsigned long int
3690 get_ref_die_offset (dw_die_ref ref)
3691 {
3692 gcc_assert (ref->die_offset);
3693 return ref->die_offset;
3694 }
3695
3696 /* Convert a DIE tag into its string name. */
3697
3698 static const char *
3699 dwarf_tag_name (unsigned int tag)
3700 {
3701 const char *name = get_DW_TAG_name (tag);
3702
3703 if (name != NULL)
3704 return name;
3705
3706 return "DW_TAG_<unknown>";
3707 }
3708
3709 /* Convert a DWARF attribute code into its string name. */
3710
3711 static const char *
3712 dwarf_attr_name (unsigned int attr)
3713 {
3714 const char *name;
3715
3716 switch (attr)
3717 {
3718 #if VMS_DEBUGGING_INFO
3719 case DW_AT_HP_prologue:
3720 return "DW_AT_HP_prologue";
3721 #else
3722 case DW_AT_MIPS_loop_unroll_factor:
3723 return "DW_AT_MIPS_loop_unroll_factor";
3724 #endif
3725
3726 #if VMS_DEBUGGING_INFO
3727 case DW_AT_HP_epilogue:
3728 return "DW_AT_HP_epilogue";
3729 #else
3730 case DW_AT_MIPS_stride:
3731 return "DW_AT_MIPS_stride";
3732 #endif
3733 }
3734
3735 name = get_DW_AT_name (attr);
3736
3737 if (name != NULL)
3738 return name;
3739
3740 return "DW_AT_<unknown>";
3741 }
3742
3743 /* Convert a DWARF value form code into its string name. */
3744
3745 static const char *
3746 dwarf_form_name (unsigned int form)
3747 {
3748 const char *name = get_DW_FORM_name (form);
3749
3750 if (name != NULL)
3751 return name;
3752
3753 return "DW_FORM_<unknown>";
3754 }
3755 \f
3756 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3757 instance of an inlined instance of a decl which is local to an inline
3758 function, so we have to trace all of the way back through the origin chain
3759 to find out what sort of node actually served as the original seed for the
3760 given block. */
3761
3762 static tree
3763 decl_ultimate_origin (const_tree decl)
3764 {
3765 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3766 return NULL_TREE;
3767
3768 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3769 we're trying to output the abstract instance of this function. */
3770 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3771 return NULL_TREE;
3772
3773 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3774 most distant ancestor, this should never happen. */
3775 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3776
3777 return DECL_ABSTRACT_ORIGIN (decl);
3778 }
3779
3780 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3781 of a virtual function may refer to a base class, so we check the 'this'
3782 parameter. */
3783
3784 static tree
3785 decl_class_context (tree decl)
3786 {
3787 tree context = NULL_TREE;
3788
3789 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3790 context = DECL_CONTEXT (decl);
3791 else
3792 context = TYPE_MAIN_VARIANT
3793 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3794
3795 if (context && !TYPE_P (context))
3796 context = NULL_TREE;
3797
3798 return context;
3799 }
3800 \f
3801 /* Add an attribute/value pair to a DIE. */
3802
3803 static inline void
3804 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3805 {
3806 /* Maybe this should be an assert? */
3807 if (die == NULL)
3808 return;
3809
3810 vec_safe_reserve (die->die_attr, 1);
3811 vec_safe_push (die->die_attr, *attr);
3812 }
3813
3814 static inline enum dw_val_class
3815 AT_class (dw_attr_node *a)
3816 {
3817 return a->dw_attr_val.val_class;
3818 }
3819
3820 /* Return the index for any attribute that will be referenced with a
3821 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3822 are stored in dw_attr_val.v.val_str for reference counting
3823 pruning. */
3824
3825 static inline unsigned int
3826 AT_index (dw_attr_node *a)
3827 {
3828 if (AT_class (a) == dw_val_class_str)
3829 return a->dw_attr_val.v.val_str->index;
3830 else if (a->dw_attr_val.val_entry != NULL)
3831 return a->dw_attr_val.val_entry->index;
3832 return NOT_INDEXED;
3833 }
3834
3835 /* Add a flag value attribute to a DIE. */
3836
3837 static inline void
3838 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3839 {
3840 dw_attr_node attr;
3841
3842 attr.dw_attr = attr_kind;
3843 attr.dw_attr_val.val_class = dw_val_class_flag;
3844 attr.dw_attr_val.val_entry = NULL;
3845 attr.dw_attr_val.v.val_flag = flag;
3846 add_dwarf_attr (die, &attr);
3847 }
3848
3849 static inline unsigned
3850 AT_flag (dw_attr_node *a)
3851 {
3852 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3853 return a->dw_attr_val.v.val_flag;
3854 }
3855
3856 /* Add a signed integer attribute value to a DIE. */
3857
3858 static inline void
3859 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3860 {
3861 dw_attr_node attr;
3862
3863 attr.dw_attr = attr_kind;
3864 attr.dw_attr_val.val_class = dw_val_class_const;
3865 attr.dw_attr_val.val_entry = NULL;
3866 attr.dw_attr_val.v.val_int = int_val;
3867 add_dwarf_attr (die, &attr);
3868 }
3869
3870 static inline HOST_WIDE_INT
3871 AT_int (dw_attr_node *a)
3872 {
3873 gcc_assert (a && AT_class (a) == dw_val_class_const);
3874 return a->dw_attr_val.v.val_int;
3875 }
3876
3877 /* Add an unsigned integer attribute value to a DIE. */
3878
3879 static inline void
3880 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3881 unsigned HOST_WIDE_INT unsigned_val)
3882 {
3883 dw_attr_node attr;
3884
3885 attr.dw_attr = attr_kind;
3886 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3887 attr.dw_attr_val.val_entry = NULL;
3888 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3889 add_dwarf_attr (die, &attr);
3890 }
3891
3892 static inline unsigned HOST_WIDE_INT
3893 AT_unsigned (dw_attr_node *a)
3894 {
3895 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3896 return a->dw_attr_val.v.val_unsigned;
3897 }
3898
3899 /* Add an unsigned wide integer attribute value to a DIE. */
3900
3901 static inline void
3902 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3903 const wide_int& w)
3904 {
3905 dw_attr_node attr;
3906
3907 attr.dw_attr = attr_kind;
3908 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3909 attr.dw_attr_val.val_entry = NULL;
3910 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3911 *attr.dw_attr_val.v.val_wide = w;
3912 add_dwarf_attr (die, &attr);
3913 }
3914
3915 /* Add an unsigned double integer attribute value to a DIE. */
3916
3917 static inline void
3918 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3919 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3920 {
3921 dw_attr_node attr;
3922
3923 attr.dw_attr = attr_kind;
3924 attr.dw_attr_val.val_class = dw_val_class_const_double;
3925 attr.dw_attr_val.val_entry = NULL;
3926 attr.dw_attr_val.v.val_double.high = high;
3927 attr.dw_attr_val.v.val_double.low = low;
3928 add_dwarf_attr (die, &attr);
3929 }
3930
3931 /* Add a floating point attribute value to a DIE and return it. */
3932
3933 static inline void
3934 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3935 unsigned int length, unsigned int elt_size, unsigned char *array)
3936 {
3937 dw_attr_node attr;
3938
3939 attr.dw_attr = attr_kind;
3940 attr.dw_attr_val.val_class = dw_val_class_vec;
3941 attr.dw_attr_val.val_entry = NULL;
3942 attr.dw_attr_val.v.val_vec.length = length;
3943 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3944 attr.dw_attr_val.v.val_vec.array = array;
3945 add_dwarf_attr (die, &attr);
3946 }
3947
3948 /* Add an 8-byte data attribute value to a DIE. */
3949
3950 static inline void
3951 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3952 unsigned char data8[8])
3953 {
3954 dw_attr_node attr;
3955
3956 attr.dw_attr = attr_kind;
3957 attr.dw_attr_val.val_class = dw_val_class_data8;
3958 attr.dw_attr_val.val_entry = NULL;
3959 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3960 add_dwarf_attr (die, &attr);
3961 }
3962
3963 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3964 dwarf_split_debug_info, address attributes in dies destined for the
3965 final executable have force_direct set to avoid using indexed
3966 references. */
3967
3968 static inline void
3969 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3970 bool force_direct)
3971 {
3972 dw_attr_node attr;
3973 char * lbl_id;
3974
3975 lbl_id = xstrdup (lbl_low);
3976 attr.dw_attr = DW_AT_low_pc;
3977 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3978 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3979 if (dwarf_split_debug_info && !force_direct)
3980 attr.dw_attr_val.val_entry
3981 = add_addr_table_entry (lbl_id, ate_kind_label);
3982 else
3983 attr.dw_attr_val.val_entry = NULL;
3984 add_dwarf_attr (die, &attr);
3985
3986 attr.dw_attr = DW_AT_high_pc;
3987 if (dwarf_version < 4)
3988 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3989 else
3990 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3991 lbl_id = xstrdup (lbl_high);
3992 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3993 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3994 && dwarf_split_debug_info && !force_direct)
3995 attr.dw_attr_val.val_entry
3996 = add_addr_table_entry (lbl_id, ate_kind_label);
3997 else
3998 attr.dw_attr_val.val_entry = NULL;
3999 add_dwarf_attr (die, &attr);
4000 }
4001
4002 /* Hash and equality functions for debug_str_hash. */
4003
4004 hashval_t
4005 indirect_string_hasher::hash (indirect_string_node *x)
4006 {
4007 return htab_hash_string (x->str);
4008 }
4009
4010 bool
4011 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4012 {
4013 return strcmp (x1->str, x2) == 0;
4014 }
4015
4016 /* Add STR to the given string hash table. */
4017
4018 static struct indirect_string_node *
4019 find_AT_string_in_table (const char *str,
4020 hash_table<indirect_string_hasher> *table)
4021 {
4022 struct indirect_string_node *node;
4023
4024 indirect_string_node **slot
4025 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4026 if (*slot == NULL)
4027 {
4028 node = ggc_cleared_alloc<indirect_string_node> ();
4029 node->str = ggc_strdup (str);
4030 *slot = node;
4031 }
4032 else
4033 node = *slot;
4034
4035 node->refcount++;
4036 return node;
4037 }
4038
4039 /* Add STR to the indirect string hash table. */
4040
4041 static struct indirect_string_node *
4042 find_AT_string (const char *str)
4043 {
4044 if (! debug_str_hash)
4045 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4046
4047 return find_AT_string_in_table (str, debug_str_hash);
4048 }
4049
4050 /* Add a string attribute value to a DIE. */
4051
4052 static inline void
4053 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4054 {
4055 dw_attr_node attr;
4056 struct indirect_string_node *node;
4057
4058 node = find_AT_string (str);
4059
4060 attr.dw_attr = attr_kind;
4061 attr.dw_attr_val.val_class = dw_val_class_str;
4062 attr.dw_attr_val.val_entry = NULL;
4063 attr.dw_attr_val.v.val_str = node;
4064 add_dwarf_attr (die, &attr);
4065 }
4066
4067 static inline const char *
4068 AT_string (dw_attr_node *a)
4069 {
4070 gcc_assert (a && AT_class (a) == dw_val_class_str);
4071 return a->dw_attr_val.v.val_str->str;
4072 }
4073
4074 /* Call this function directly to bypass AT_string_form's logic to put
4075 the string inline in the die. */
4076
4077 static void
4078 set_indirect_string (struct indirect_string_node *node)
4079 {
4080 char label[32];
4081 /* Already indirect is a no op. */
4082 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4083 {
4084 gcc_assert (node->label);
4085 return;
4086 }
4087 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4088 ++dw2_string_counter;
4089 node->label = xstrdup (label);
4090
4091 if (!dwarf_split_debug_info)
4092 {
4093 node->form = DW_FORM_strp;
4094 node->index = NOT_INDEXED;
4095 }
4096 else
4097 {
4098 node->form = DW_FORM_GNU_str_index;
4099 node->index = NO_INDEX_ASSIGNED;
4100 }
4101 }
4102
4103 /* Find out whether a string should be output inline in DIE
4104 or out-of-line in .debug_str section. */
4105
4106 static enum dwarf_form
4107 find_string_form (struct indirect_string_node *node)
4108 {
4109 unsigned int len;
4110
4111 if (node->form)
4112 return node->form;
4113
4114 len = strlen (node->str) + 1;
4115
4116 /* If the string is shorter or equal to the size of the reference, it is
4117 always better to put it inline. */
4118 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4119 return node->form = DW_FORM_string;
4120
4121 /* If we cannot expect the linker to merge strings in .debug_str
4122 section, only put it into .debug_str if it is worth even in this
4123 single module. */
4124 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4125 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4126 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4127 return node->form = DW_FORM_string;
4128
4129 set_indirect_string (node);
4130
4131 return node->form;
4132 }
4133
4134 /* Find out whether the string referenced from the attribute should be
4135 output inline in DIE or out-of-line in .debug_str section. */
4136
4137 static enum dwarf_form
4138 AT_string_form (dw_attr_node *a)
4139 {
4140 gcc_assert (a && AT_class (a) == dw_val_class_str);
4141 return find_string_form (a->dw_attr_val.v.val_str);
4142 }
4143
4144 /* Add a DIE reference attribute value to a DIE. */
4145
4146 static inline void
4147 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4148 {
4149 dw_attr_node attr;
4150 gcc_checking_assert (targ_die != NULL);
4151
4152 /* With LTO we can end up trying to reference something we didn't create
4153 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4154 if (targ_die == NULL)
4155 return;
4156
4157 attr.dw_attr = attr_kind;
4158 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4159 attr.dw_attr_val.val_entry = NULL;
4160 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4161 attr.dw_attr_val.v.val_die_ref.external = 0;
4162 add_dwarf_attr (die, &attr);
4163 }
4164
4165 /* Change DIE reference REF to point to NEW_DIE instead. */
4166
4167 static inline void
4168 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4169 {
4170 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4171 ref->dw_attr_val.v.val_die_ref.die = new_die;
4172 ref->dw_attr_val.v.val_die_ref.external = 0;
4173 }
4174
4175 /* Add an AT_specification attribute to a DIE, and also make the back
4176 pointer from the specification to the definition. */
4177
4178 static inline void
4179 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4180 {
4181 add_AT_die_ref (die, DW_AT_specification, targ_die);
4182 gcc_assert (!targ_die->die_definition);
4183 targ_die->die_definition = die;
4184 }
4185
4186 static inline dw_die_ref
4187 AT_ref (dw_attr_node *a)
4188 {
4189 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4190 return a->dw_attr_val.v.val_die_ref.die;
4191 }
4192
4193 static inline int
4194 AT_ref_external (dw_attr_node *a)
4195 {
4196 if (a && AT_class (a) == dw_val_class_die_ref)
4197 return a->dw_attr_val.v.val_die_ref.external;
4198
4199 return 0;
4200 }
4201
4202 static inline void
4203 set_AT_ref_external (dw_attr_node *a, int i)
4204 {
4205 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4206 a->dw_attr_val.v.val_die_ref.external = i;
4207 }
4208
4209 /* Add an FDE reference attribute value to a DIE. */
4210
4211 static inline void
4212 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4213 {
4214 dw_attr_node attr;
4215
4216 attr.dw_attr = attr_kind;
4217 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4218 attr.dw_attr_val.val_entry = NULL;
4219 attr.dw_attr_val.v.val_fde_index = targ_fde;
4220 add_dwarf_attr (die, &attr);
4221 }
4222
4223 /* Add a location description attribute value to a DIE. */
4224
4225 static inline void
4226 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4227 {
4228 dw_attr_node attr;
4229
4230 attr.dw_attr = attr_kind;
4231 attr.dw_attr_val.val_class = dw_val_class_loc;
4232 attr.dw_attr_val.val_entry = NULL;
4233 attr.dw_attr_val.v.val_loc = loc;
4234 add_dwarf_attr (die, &attr);
4235 }
4236
4237 static inline dw_loc_descr_ref
4238 AT_loc (dw_attr_node *a)
4239 {
4240 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4241 return a->dw_attr_val.v.val_loc;
4242 }
4243
4244 static inline void
4245 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4246 {
4247 dw_attr_node attr;
4248
4249 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4250 return;
4251
4252 attr.dw_attr = attr_kind;
4253 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4254 attr.dw_attr_val.val_entry = NULL;
4255 attr.dw_attr_val.v.val_loc_list = loc_list;
4256 add_dwarf_attr (die, &attr);
4257 have_location_lists = true;
4258 }
4259
4260 static inline dw_loc_list_ref
4261 AT_loc_list (dw_attr_node *a)
4262 {
4263 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4264 return a->dw_attr_val.v.val_loc_list;
4265 }
4266
4267 static inline dw_loc_list_ref *
4268 AT_loc_list_ptr (dw_attr_node *a)
4269 {
4270 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4271 return &a->dw_attr_val.v.val_loc_list;
4272 }
4273
4274 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4275 {
4276 static hashval_t hash (addr_table_entry *);
4277 static bool equal (addr_table_entry *, addr_table_entry *);
4278 };
4279
4280 /* Table of entries into the .debug_addr section. */
4281
4282 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4283
4284 /* Hash an address_table_entry. */
4285
4286 hashval_t
4287 addr_hasher::hash (addr_table_entry *a)
4288 {
4289 inchash::hash hstate;
4290 switch (a->kind)
4291 {
4292 case ate_kind_rtx:
4293 hstate.add_int (0);
4294 break;
4295 case ate_kind_rtx_dtprel:
4296 hstate.add_int (1);
4297 break;
4298 case ate_kind_label:
4299 return htab_hash_string (a->addr.label);
4300 default:
4301 gcc_unreachable ();
4302 }
4303 inchash::add_rtx (a->addr.rtl, hstate);
4304 return hstate.end ();
4305 }
4306
4307 /* Determine equality for two address_table_entries. */
4308
4309 bool
4310 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4311 {
4312 if (a1->kind != a2->kind)
4313 return 0;
4314 switch (a1->kind)
4315 {
4316 case ate_kind_rtx:
4317 case ate_kind_rtx_dtprel:
4318 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4319 case ate_kind_label:
4320 return strcmp (a1->addr.label, a2->addr.label) == 0;
4321 default:
4322 gcc_unreachable ();
4323 }
4324 }
4325
4326 /* Initialize an addr_table_entry. */
4327
4328 void
4329 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4330 {
4331 e->kind = kind;
4332 switch (kind)
4333 {
4334 case ate_kind_rtx:
4335 case ate_kind_rtx_dtprel:
4336 e->addr.rtl = (rtx) addr;
4337 break;
4338 case ate_kind_label:
4339 e->addr.label = (char *) addr;
4340 break;
4341 }
4342 e->refcount = 0;
4343 e->index = NO_INDEX_ASSIGNED;
4344 }
4345
4346 /* Add attr to the address table entry to the table. Defer setting an
4347 index until output time. */
4348
4349 static addr_table_entry *
4350 add_addr_table_entry (void *addr, enum ate_kind kind)
4351 {
4352 addr_table_entry *node;
4353 addr_table_entry finder;
4354
4355 gcc_assert (dwarf_split_debug_info);
4356 if (! addr_index_table)
4357 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4358 init_addr_table_entry (&finder, kind, addr);
4359 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4360
4361 if (*slot == HTAB_EMPTY_ENTRY)
4362 {
4363 node = ggc_cleared_alloc<addr_table_entry> ();
4364 init_addr_table_entry (node, kind, addr);
4365 *slot = node;
4366 }
4367 else
4368 node = *slot;
4369
4370 node->refcount++;
4371 return node;
4372 }
4373
4374 /* Remove an entry from the addr table by decrementing its refcount.
4375 Strictly, decrementing the refcount would be enough, but the
4376 assertion that the entry is actually in the table has found
4377 bugs. */
4378
4379 static void
4380 remove_addr_table_entry (addr_table_entry *entry)
4381 {
4382 gcc_assert (dwarf_split_debug_info && addr_index_table);
4383 /* After an index is assigned, the table is frozen. */
4384 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4385 entry->refcount--;
4386 }
4387
4388 /* Given a location list, remove all addresses it refers to from the
4389 address_table. */
4390
4391 static void
4392 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4393 {
4394 for (; descr; descr = descr->dw_loc_next)
4395 if (descr->dw_loc_oprnd1.val_entry != NULL)
4396 {
4397 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4398 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4399 }
4400 }
4401
4402 /* A helper function for dwarf2out_finish called through
4403 htab_traverse. Assign an addr_table_entry its index. All entries
4404 must be collected into the table when this function is called,
4405 because the indexing code relies on htab_traverse to traverse nodes
4406 in the same order for each run. */
4407
4408 int
4409 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4410 {
4411 addr_table_entry *node = *h;
4412
4413 /* Don't index unreferenced nodes. */
4414 if (node->refcount == 0)
4415 return 1;
4416
4417 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4418 node->index = *index;
4419 *index += 1;
4420
4421 return 1;
4422 }
4423
4424 /* Add an address constant attribute value to a DIE. When using
4425 dwarf_split_debug_info, address attributes in dies destined for the
4426 final executable should be direct references--setting the parameter
4427 force_direct ensures this behavior. */
4428
4429 static inline void
4430 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4431 bool force_direct)
4432 {
4433 dw_attr_node attr;
4434
4435 attr.dw_attr = attr_kind;
4436 attr.dw_attr_val.val_class = dw_val_class_addr;
4437 attr.dw_attr_val.v.val_addr = addr;
4438 if (dwarf_split_debug_info && !force_direct)
4439 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4440 else
4441 attr.dw_attr_val.val_entry = NULL;
4442 add_dwarf_attr (die, &attr);
4443 }
4444
4445 /* Get the RTX from to an address DIE attribute. */
4446
4447 static inline rtx
4448 AT_addr (dw_attr_node *a)
4449 {
4450 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4451 return a->dw_attr_val.v.val_addr;
4452 }
4453
4454 /* Add a file attribute value to a DIE. */
4455
4456 static inline void
4457 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4458 struct dwarf_file_data *fd)
4459 {
4460 dw_attr_node attr;
4461
4462 attr.dw_attr = attr_kind;
4463 attr.dw_attr_val.val_class = dw_val_class_file;
4464 attr.dw_attr_val.val_entry = NULL;
4465 attr.dw_attr_val.v.val_file = fd;
4466 add_dwarf_attr (die, &attr);
4467 }
4468
4469 /* Get the dwarf_file_data from a file DIE attribute. */
4470
4471 static inline struct dwarf_file_data *
4472 AT_file (dw_attr_node *a)
4473 {
4474 gcc_assert (a && AT_class (a) == dw_val_class_file);
4475 return a->dw_attr_val.v.val_file;
4476 }
4477
4478 /* Add a vms delta attribute value to a DIE. */
4479
4480 static inline void
4481 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4482 const char *lbl1, const char *lbl2)
4483 {
4484 dw_attr_node attr;
4485
4486 attr.dw_attr = attr_kind;
4487 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4488 attr.dw_attr_val.val_entry = NULL;
4489 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4490 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4491 add_dwarf_attr (die, &attr);
4492 }
4493
4494 /* Add a label identifier attribute value to a DIE. */
4495
4496 static inline void
4497 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4498 const char *lbl_id)
4499 {
4500 dw_attr_node attr;
4501
4502 attr.dw_attr = attr_kind;
4503 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4504 attr.dw_attr_val.val_entry = NULL;
4505 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4506 if (dwarf_split_debug_info)
4507 attr.dw_attr_val.val_entry
4508 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4509 ate_kind_label);
4510 add_dwarf_attr (die, &attr);
4511 }
4512
4513 /* Add a section offset attribute value to a DIE, an offset into the
4514 debug_line section. */
4515
4516 static inline void
4517 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4518 const char *label)
4519 {
4520 dw_attr_node attr;
4521
4522 attr.dw_attr = attr_kind;
4523 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4524 attr.dw_attr_val.val_entry = NULL;
4525 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4526 add_dwarf_attr (die, &attr);
4527 }
4528
4529 /* Add a section offset attribute value to a DIE, an offset into the
4530 debug_macinfo section. */
4531
4532 static inline void
4533 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4534 const char *label)
4535 {
4536 dw_attr_node attr;
4537
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_macptr;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4542 add_dwarf_attr (die, &attr);
4543 }
4544
4545 /* Add an offset attribute value to a DIE. */
4546
4547 static inline void
4548 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4549 unsigned HOST_WIDE_INT offset)
4550 {
4551 dw_attr_node attr;
4552
4553 attr.dw_attr = attr_kind;
4554 attr.dw_attr_val.val_class = dw_val_class_offset;
4555 attr.dw_attr_val.val_entry = NULL;
4556 attr.dw_attr_val.v.val_offset = offset;
4557 add_dwarf_attr (die, &attr);
4558 }
4559
4560 /* Add a range_list attribute value to a DIE. When using
4561 dwarf_split_debug_info, address attributes in dies destined for the
4562 final executable should be direct references--setting the parameter
4563 force_direct ensures this behavior. */
4564
4565 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4566 #define RELOCATED_OFFSET (NULL)
4567
4568 static void
4569 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4570 long unsigned int offset, bool force_direct)
4571 {
4572 dw_attr_node attr;
4573
4574 attr.dw_attr = attr_kind;
4575 attr.dw_attr_val.val_class = dw_val_class_range_list;
4576 /* For the range_list attribute, use val_entry to store whether the
4577 offset should follow split-debug-info or normal semantics. This
4578 value is read in output_range_list_offset. */
4579 if (dwarf_split_debug_info && !force_direct)
4580 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4581 else
4582 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4583 attr.dw_attr_val.v.val_offset = offset;
4584 add_dwarf_attr (die, &attr);
4585 }
4586
4587 /* Return the start label of a delta attribute. */
4588
4589 static inline const char *
4590 AT_vms_delta1 (dw_attr_node *a)
4591 {
4592 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4593 return a->dw_attr_val.v.val_vms_delta.lbl1;
4594 }
4595
4596 /* Return the end label of a delta attribute. */
4597
4598 static inline const char *
4599 AT_vms_delta2 (dw_attr_node *a)
4600 {
4601 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4602 return a->dw_attr_val.v.val_vms_delta.lbl2;
4603 }
4604
4605 static inline const char *
4606 AT_lbl (dw_attr_node *a)
4607 {
4608 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4609 || AT_class (a) == dw_val_class_lineptr
4610 || AT_class (a) == dw_val_class_macptr
4611 || AT_class (a) == dw_val_class_high_pc));
4612 return a->dw_attr_val.v.val_lbl_id;
4613 }
4614
4615 /* Get the attribute of type attr_kind. */
4616
4617 static dw_attr_node *
4618 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4619 {
4620 dw_attr_node *a;
4621 unsigned ix;
4622 dw_die_ref spec = NULL;
4623
4624 if (! die)
4625 return NULL;
4626
4627 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4628 if (a->dw_attr == attr_kind)
4629 return a;
4630 else if (a->dw_attr == DW_AT_specification
4631 || a->dw_attr == DW_AT_abstract_origin)
4632 spec = AT_ref (a);
4633
4634 if (spec)
4635 return get_AT (spec, attr_kind);
4636
4637 return NULL;
4638 }
4639
4640 /* Returns the parent of the declaration of DIE. */
4641
4642 static dw_die_ref
4643 get_die_parent (dw_die_ref die)
4644 {
4645 dw_die_ref t;
4646
4647 if (!die)
4648 return NULL;
4649
4650 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4651 || (t = get_AT_ref (die, DW_AT_specification)))
4652 die = t;
4653
4654 return die->die_parent;
4655 }
4656
4657 /* Return the "low pc" attribute value, typically associated with a subprogram
4658 DIE. Return null if the "low pc" attribute is either not present, or if it
4659 cannot be represented as an assembler label identifier. */
4660
4661 static inline const char *
4662 get_AT_low_pc (dw_die_ref die)
4663 {
4664 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4665
4666 return a ? AT_lbl (a) : NULL;
4667 }
4668
4669 /* Return the "high pc" attribute value, typically associated with a subprogram
4670 DIE. Return null if the "high pc" attribute is either not present, or if it
4671 cannot be represented as an assembler label identifier. */
4672
4673 static inline const char *
4674 get_AT_hi_pc (dw_die_ref die)
4675 {
4676 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4677
4678 return a ? AT_lbl (a) : NULL;
4679 }
4680
4681 /* Return the value of the string attribute designated by ATTR_KIND, or
4682 NULL if it is not present. */
4683
4684 static inline const char *
4685 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4686 {
4687 dw_attr_node *a = get_AT (die, attr_kind);
4688
4689 return a ? AT_string (a) : NULL;
4690 }
4691
4692 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4693 if it is not present. */
4694
4695 static inline int
4696 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4697 {
4698 dw_attr_node *a = get_AT (die, attr_kind);
4699
4700 return a ? AT_flag (a) : 0;
4701 }
4702
4703 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4704 if it is not present. */
4705
4706 static inline unsigned
4707 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4708 {
4709 dw_attr_node *a = get_AT (die, attr_kind);
4710
4711 return a ? AT_unsigned (a) : 0;
4712 }
4713
4714 static inline dw_die_ref
4715 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4716 {
4717 dw_attr_node *a = get_AT (die, attr_kind);
4718
4719 return a ? AT_ref (a) : NULL;
4720 }
4721
4722 static inline struct dwarf_file_data *
4723 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4724 {
4725 dw_attr_node *a = get_AT (die, attr_kind);
4726
4727 return a ? AT_file (a) : NULL;
4728 }
4729
4730 /* Return TRUE if the language is C++. */
4731
4732 static inline bool
4733 is_cxx (void)
4734 {
4735 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4736
4737 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4738 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4739 }
4740
4741 /* Return TRUE if the language is Java. */
4742
4743 static inline bool
4744 is_java (void)
4745 {
4746 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4747
4748 return lang == DW_LANG_Java;
4749 }
4750
4751 /* Return TRUE if the language is Fortran. */
4752
4753 static inline bool
4754 is_fortran (void)
4755 {
4756 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4757
4758 return (lang == DW_LANG_Fortran77
4759 || lang == DW_LANG_Fortran90
4760 || lang == DW_LANG_Fortran95
4761 || lang == DW_LANG_Fortran03
4762 || lang == DW_LANG_Fortran08);
4763 }
4764
4765 /* Return TRUE if the language is Ada. */
4766
4767 static inline bool
4768 is_ada (void)
4769 {
4770 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4771
4772 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4773 }
4774
4775 /* Remove the specified attribute if present. Return TRUE if removal
4776 was successful. */
4777
4778 static bool
4779 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4780 {
4781 dw_attr_node *a;
4782 unsigned ix;
4783
4784 if (! die)
4785 return false;
4786
4787 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4788 if (a->dw_attr == attr_kind)
4789 {
4790 if (AT_class (a) == dw_val_class_str)
4791 if (a->dw_attr_val.v.val_str->refcount)
4792 a->dw_attr_val.v.val_str->refcount--;
4793
4794 /* vec::ordered_remove should help reduce the number of abbrevs
4795 that are needed. */
4796 die->die_attr->ordered_remove (ix);
4797 return true;
4798 }
4799 return false;
4800 }
4801
4802 /* Remove CHILD from its parent. PREV must have the property that
4803 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4804
4805 static void
4806 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4807 {
4808 gcc_assert (child->die_parent == prev->die_parent);
4809 gcc_assert (prev->die_sib == child);
4810 if (prev == child)
4811 {
4812 gcc_assert (child->die_parent->die_child == child);
4813 prev = NULL;
4814 }
4815 else
4816 prev->die_sib = child->die_sib;
4817 if (child->die_parent->die_child == child)
4818 child->die_parent->die_child = prev;
4819 }
4820
4821 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4822 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4823
4824 static void
4825 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4826 {
4827 dw_die_ref parent = old_child->die_parent;
4828
4829 gcc_assert (parent == prev->die_parent);
4830 gcc_assert (prev->die_sib == old_child);
4831
4832 new_child->die_parent = parent;
4833 if (prev == old_child)
4834 {
4835 gcc_assert (parent->die_child == old_child);
4836 new_child->die_sib = new_child;
4837 }
4838 else
4839 {
4840 prev->die_sib = new_child;
4841 new_child->die_sib = old_child->die_sib;
4842 }
4843 if (old_child->die_parent->die_child == old_child)
4844 old_child->die_parent->die_child = new_child;
4845 }
4846
4847 /* Move all children from OLD_PARENT to NEW_PARENT. */
4848
4849 static void
4850 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4851 {
4852 dw_die_ref c;
4853 new_parent->die_child = old_parent->die_child;
4854 old_parent->die_child = NULL;
4855 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4856 }
4857
4858 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4859 matches TAG. */
4860
4861 static void
4862 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4863 {
4864 dw_die_ref c;
4865
4866 c = die->die_child;
4867 if (c) do {
4868 dw_die_ref prev = c;
4869 c = c->die_sib;
4870 while (c->die_tag == tag)
4871 {
4872 remove_child_with_prev (c, prev);
4873 c->die_parent = NULL;
4874 /* Might have removed every child. */
4875 if (c == c->die_sib)
4876 return;
4877 c = c->die_sib;
4878 }
4879 } while (c != die->die_child);
4880 }
4881
4882 /* Add a CHILD_DIE as the last child of DIE. */
4883
4884 static void
4885 add_child_die (dw_die_ref die, dw_die_ref child_die)
4886 {
4887 /* FIXME this should probably be an assert. */
4888 if (! die || ! child_die)
4889 return;
4890 gcc_assert (die != child_die);
4891
4892 child_die->die_parent = die;
4893 if (die->die_child)
4894 {
4895 child_die->die_sib = die->die_child->die_sib;
4896 die->die_child->die_sib = child_die;
4897 }
4898 else
4899 child_die->die_sib = child_die;
4900 die->die_child = child_die;
4901 }
4902
4903 /* Unassociate CHILD from its parent, and make its parent be
4904 NEW_PARENT. */
4905
4906 static void
4907 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4908 {
4909 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4910 if (p->die_sib == child)
4911 {
4912 remove_child_with_prev (child, p);
4913 break;
4914 }
4915 add_child_die (new_parent, child);
4916 }
4917
4918 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4919 is the specification, to the end of PARENT's list of children.
4920 This is done by removing and re-adding it. */
4921
4922 static void
4923 splice_child_die (dw_die_ref parent, dw_die_ref child)
4924 {
4925 /* We want the declaration DIE from inside the class, not the
4926 specification DIE at toplevel. */
4927 if (child->die_parent != parent)
4928 {
4929 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4930
4931 if (tmp)
4932 child = tmp;
4933 }
4934
4935 gcc_assert (child->die_parent == parent
4936 || (child->die_parent
4937 == get_AT_ref (parent, DW_AT_specification)));
4938
4939 reparent_child (child, parent);
4940 }
4941
4942 /* Create and return a new die with a parent of PARENT_DIE. If
4943 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4944 associated tree T must be supplied to determine parenthood
4945 later. */
4946
4947 static inline dw_die_ref
4948 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4949 {
4950 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4951
4952 die->die_tag = tag_value;
4953
4954 if (parent_die != NULL)
4955 add_child_die (parent_die, die);
4956 else
4957 {
4958 limbo_die_node *limbo_node;
4959
4960 /* No DIEs created after early dwarf should end up in limbo,
4961 because the limbo list should not persist past LTO
4962 streaming. */
4963 if (tag_value != DW_TAG_compile_unit
4964 /* These are allowed because they're generated while
4965 breaking out COMDAT units late. */
4966 && tag_value != DW_TAG_type_unit
4967 && !early_dwarf
4968 /* Allow nested functions to live in limbo because they will
4969 only temporarily live there, as decls_for_scope will fix
4970 them up. */
4971 && (TREE_CODE (t) != FUNCTION_DECL
4972 || !decl_function_context (t))
4973 /* Same as nested functions above but for types. Types that
4974 are local to a function will be fixed in
4975 decls_for_scope. */
4976 && (!RECORD_OR_UNION_TYPE_P (t)
4977 || !TYPE_CONTEXT (t)
4978 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4979 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4980 especially in the ltrans stage, but once we implement LTO
4981 dwarf streaming, we should remove this exception. */
4982 && !in_lto_p)
4983 {
4984 fprintf (stderr, "symbol ended up in limbo too late:");
4985 debug_generic_stmt (t);
4986 gcc_unreachable ();
4987 }
4988
4989 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4990 limbo_node->die = die;
4991 limbo_node->created_for = t;
4992 limbo_node->next = limbo_die_list;
4993 limbo_die_list = limbo_node;
4994 }
4995
4996 return die;
4997 }
4998
4999 /* Return the DIE associated with the given type specifier. */
5000
5001 static inline dw_die_ref
5002 lookup_type_die (tree type)
5003 {
5004 return TYPE_SYMTAB_DIE (type);
5005 }
5006
5007 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5008 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5009 anonymous type instead the one of the naming typedef. */
5010
5011 static inline dw_die_ref
5012 strip_naming_typedef (tree type, dw_die_ref type_die)
5013 {
5014 if (type
5015 && TREE_CODE (type) == RECORD_TYPE
5016 && type_die
5017 && type_die->die_tag == DW_TAG_typedef
5018 && is_naming_typedef_decl (TYPE_NAME (type)))
5019 type_die = get_AT_ref (type_die, DW_AT_type);
5020 return type_die;
5021 }
5022
5023 /* Like lookup_type_die, but if type is an anonymous type named by a
5024 typedef[1], return the DIE of the anonymous type instead the one of
5025 the naming typedef. This is because in gen_typedef_die, we did
5026 equate the anonymous struct named by the typedef with the DIE of
5027 the naming typedef. So by default, lookup_type_die on an anonymous
5028 struct yields the DIE of the naming typedef.
5029
5030 [1]: Read the comment of is_naming_typedef_decl to learn about what
5031 a naming typedef is. */
5032
5033 static inline dw_die_ref
5034 lookup_type_die_strip_naming_typedef (tree type)
5035 {
5036 dw_die_ref die = lookup_type_die (type);
5037 return strip_naming_typedef (type, die);
5038 }
5039
5040 /* Equate a DIE to a given type specifier. */
5041
5042 static inline void
5043 equate_type_number_to_die (tree type, dw_die_ref type_die)
5044 {
5045 TYPE_SYMTAB_DIE (type) = type_die;
5046 }
5047
5048 /* Returns a hash value for X (which really is a die_struct). */
5049
5050 inline hashval_t
5051 decl_die_hasher::hash (die_node *x)
5052 {
5053 return (hashval_t) x->decl_id;
5054 }
5055
5056 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5057
5058 inline bool
5059 decl_die_hasher::equal (die_node *x, tree y)
5060 {
5061 return (x->decl_id == DECL_UID (y));
5062 }
5063
5064 /* Return the DIE associated with a given declaration. */
5065
5066 static inline dw_die_ref
5067 lookup_decl_die (tree decl)
5068 {
5069 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5070 }
5071
5072 /* Returns a hash value for X (which really is a var_loc_list). */
5073
5074 inline hashval_t
5075 decl_loc_hasher::hash (var_loc_list *x)
5076 {
5077 return (hashval_t) x->decl_id;
5078 }
5079
5080 /* Return nonzero if decl_id of var_loc_list X is the same as
5081 UID of decl *Y. */
5082
5083 inline bool
5084 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5085 {
5086 return (x->decl_id == DECL_UID (y));
5087 }
5088
5089 /* Return the var_loc list associated with a given declaration. */
5090
5091 static inline var_loc_list *
5092 lookup_decl_loc (const_tree decl)
5093 {
5094 if (!decl_loc_table)
5095 return NULL;
5096 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5097 }
5098
5099 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5100
5101 inline hashval_t
5102 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5103 {
5104 return (hashval_t) x->decl_id;
5105 }
5106
5107 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5108 UID of decl *Y. */
5109
5110 inline bool
5111 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5112 {
5113 return (x->decl_id == DECL_UID (y));
5114 }
5115
5116 /* Equate a DIE to a particular declaration. */
5117
5118 static void
5119 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5120 {
5121 unsigned int decl_id = DECL_UID (decl);
5122
5123 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5124 decl_die->decl_id = decl_id;
5125 }
5126
5127 /* Return how many bits covers PIECE EXPR_LIST. */
5128
5129 static HOST_WIDE_INT
5130 decl_piece_bitsize (rtx piece)
5131 {
5132 int ret = (int) GET_MODE (piece);
5133 if (ret)
5134 return ret;
5135 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5136 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5137 return INTVAL (XEXP (XEXP (piece, 0), 0));
5138 }
5139
5140 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5141
5142 static rtx *
5143 decl_piece_varloc_ptr (rtx piece)
5144 {
5145 if ((int) GET_MODE (piece))
5146 return &XEXP (piece, 0);
5147 else
5148 return &XEXP (XEXP (piece, 0), 1);
5149 }
5150
5151 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5152 Next is the chain of following piece nodes. */
5153
5154 static rtx_expr_list *
5155 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5156 {
5157 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5158 return alloc_EXPR_LIST (bitsize, loc_note, next);
5159 else
5160 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5161 GEN_INT (bitsize),
5162 loc_note), next);
5163 }
5164
5165 /* Return rtx that should be stored into loc field for
5166 LOC_NOTE and BITPOS/BITSIZE. */
5167
5168 static rtx
5169 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5170 HOST_WIDE_INT bitsize)
5171 {
5172 if (bitsize != -1)
5173 {
5174 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5175 if (bitpos != 0)
5176 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5177 }
5178 return loc_note;
5179 }
5180
5181 /* This function either modifies location piece list *DEST in
5182 place (if SRC and INNER is NULL), or copies location piece list
5183 *SRC to *DEST while modifying it. Location BITPOS is modified
5184 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5185 not copied and if needed some padding around it is added.
5186 When modifying in place, DEST should point to EXPR_LIST where
5187 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5188 to the start of the whole list and INNER points to the EXPR_LIST
5189 where earlier pieces cover PIECE_BITPOS bits. */
5190
5191 static void
5192 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5193 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5194 HOST_WIDE_INT bitsize, rtx loc_note)
5195 {
5196 HOST_WIDE_INT diff;
5197 bool copy = inner != NULL;
5198
5199 if (copy)
5200 {
5201 /* First copy all nodes preceding the current bitpos. */
5202 while (src != inner)
5203 {
5204 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5205 decl_piece_bitsize (*src), NULL_RTX);
5206 dest = &XEXP (*dest, 1);
5207 src = &XEXP (*src, 1);
5208 }
5209 }
5210 /* Add padding if needed. */
5211 if (bitpos != piece_bitpos)
5212 {
5213 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5214 copy ? NULL_RTX : *dest);
5215 dest = &XEXP (*dest, 1);
5216 }
5217 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5218 {
5219 gcc_assert (!copy);
5220 /* A piece with correct bitpos and bitsize already exist,
5221 just update the location for it and return. */
5222 *decl_piece_varloc_ptr (*dest) = loc_note;
5223 return;
5224 }
5225 /* Add the piece that changed. */
5226 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5227 dest = &XEXP (*dest, 1);
5228 /* Skip over pieces that overlap it. */
5229 diff = bitpos - piece_bitpos + bitsize;
5230 if (!copy)
5231 src = dest;
5232 while (diff > 0 && *src)
5233 {
5234 rtx piece = *src;
5235 diff -= decl_piece_bitsize (piece);
5236 if (copy)
5237 src = &XEXP (piece, 1);
5238 else
5239 {
5240 *src = XEXP (piece, 1);
5241 free_EXPR_LIST_node (piece);
5242 }
5243 }
5244 /* Add padding if needed. */
5245 if (diff < 0 && *src)
5246 {
5247 if (!copy)
5248 dest = src;
5249 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5250 dest = &XEXP (*dest, 1);
5251 }
5252 if (!copy)
5253 return;
5254 /* Finally copy all nodes following it. */
5255 while (*src)
5256 {
5257 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5258 decl_piece_bitsize (*src), NULL_RTX);
5259 dest = &XEXP (*dest, 1);
5260 src = &XEXP (*src, 1);
5261 }
5262 }
5263
5264 /* Add a variable location node to the linked list for DECL. */
5265
5266 static struct var_loc_node *
5267 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5268 {
5269 unsigned int decl_id;
5270 var_loc_list *temp;
5271 struct var_loc_node *loc = NULL;
5272 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5273
5274 if (TREE_CODE (decl) == VAR_DECL
5275 && DECL_HAS_DEBUG_EXPR_P (decl))
5276 {
5277 tree realdecl = DECL_DEBUG_EXPR (decl);
5278 if (handled_component_p (realdecl)
5279 || (TREE_CODE (realdecl) == MEM_REF
5280 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5281 {
5282 HOST_WIDE_INT maxsize;
5283 tree innerdecl;
5284 innerdecl
5285 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5286 if (!DECL_P (innerdecl)
5287 || DECL_IGNORED_P (innerdecl)
5288 || TREE_STATIC (innerdecl)
5289 || bitsize <= 0
5290 || bitpos + bitsize > 256
5291 || bitsize != maxsize)
5292 return NULL;
5293 decl = innerdecl;
5294 }
5295 }
5296
5297 decl_id = DECL_UID (decl);
5298 var_loc_list **slot
5299 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5300 if (*slot == NULL)
5301 {
5302 temp = ggc_cleared_alloc<var_loc_list> ();
5303 temp->decl_id = decl_id;
5304 *slot = temp;
5305 }
5306 else
5307 temp = *slot;
5308
5309 /* For PARM_DECLs try to keep around the original incoming value,
5310 even if that means we'll emit a zero-range .debug_loc entry. */
5311 if (temp->last
5312 && temp->first == temp->last
5313 && TREE_CODE (decl) == PARM_DECL
5314 && NOTE_P (temp->first->loc)
5315 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5316 && DECL_INCOMING_RTL (decl)
5317 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5318 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5319 == GET_CODE (DECL_INCOMING_RTL (decl))
5320 && prev_real_insn (temp->first->loc) == NULL_RTX
5321 && (bitsize != -1
5322 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5323 NOTE_VAR_LOCATION_LOC (loc_note))
5324 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5325 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5326 {
5327 loc = ggc_cleared_alloc<var_loc_node> ();
5328 temp->first->next = loc;
5329 temp->last = loc;
5330 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5331 }
5332 else if (temp->last)
5333 {
5334 struct var_loc_node *last = temp->last, *unused = NULL;
5335 rtx *piece_loc = NULL, last_loc_note;
5336 HOST_WIDE_INT piece_bitpos = 0;
5337 if (last->next)
5338 {
5339 last = last->next;
5340 gcc_assert (last->next == NULL);
5341 }
5342 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5343 {
5344 piece_loc = &last->loc;
5345 do
5346 {
5347 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5348 if (piece_bitpos + cur_bitsize > bitpos)
5349 break;
5350 piece_bitpos += cur_bitsize;
5351 piece_loc = &XEXP (*piece_loc, 1);
5352 }
5353 while (*piece_loc);
5354 }
5355 /* TEMP->LAST here is either pointer to the last but one or
5356 last element in the chained list, LAST is pointer to the
5357 last element. */
5358 if (label && strcmp (last->label, label) == 0)
5359 {
5360 /* For SRA optimized variables if there weren't any real
5361 insns since last note, just modify the last node. */
5362 if (piece_loc != NULL)
5363 {
5364 adjust_piece_list (piece_loc, NULL, NULL,
5365 bitpos, piece_bitpos, bitsize, loc_note);
5366 return NULL;
5367 }
5368 /* If the last note doesn't cover any instructions, remove it. */
5369 if (temp->last != last)
5370 {
5371 temp->last->next = NULL;
5372 unused = last;
5373 last = temp->last;
5374 gcc_assert (strcmp (last->label, label) != 0);
5375 }
5376 else
5377 {
5378 gcc_assert (temp->first == temp->last
5379 || (temp->first->next == temp->last
5380 && TREE_CODE (decl) == PARM_DECL));
5381 memset (temp->last, '\0', sizeof (*temp->last));
5382 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5383 return temp->last;
5384 }
5385 }
5386 if (bitsize == -1 && NOTE_P (last->loc))
5387 last_loc_note = last->loc;
5388 else if (piece_loc != NULL
5389 && *piece_loc != NULL_RTX
5390 && piece_bitpos == bitpos
5391 && decl_piece_bitsize (*piece_loc) == bitsize)
5392 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5393 else
5394 last_loc_note = NULL_RTX;
5395 /* If the current location is the same as the end of the list,
5396 and either both or neither of the locations is uninitialized,
5397 we have nothing to do. */
5398 if (last_loc_note == NULL_RTX
5399 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5400 NOTE_VAR_LOCATION_LOC (loc_note)))
5401 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5402 != NOTE_VAR_LOCATION_STATUS (loc_note))
5403 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5404 == VAR_INIT_STATUS_UNINITIALIZED)
5405 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5406 == VAR_INIT_STATUS_UNINITIALIZED))))
5407 {
5408 /* Add LOC to the end of list and update LAST. If the last
5409 element of the list has been removed above, reuse its
5410 memory for the new node, otherwise allocate a new one. */
5411 if (unused)
5412 {
5413 loc = unused;
5414 memset (loc, '\0', sizeof (*loc));
5415 }
5416 else
5417 loc = ggc_cleared_alloc<var_loc_node> ();
5418 if (bitsize == -1 || piece_loc == NULL)
5419 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5420 else
5421 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5422 bitpos, piece_bitpos, bitsize, loc_note);
5423 last->next = loc;
5424 /* Ensure TEMP->LAST will point either to the new last but one
5425 element of the chain, or to the last element in it. */
5426 if (last != temp->last)
5427 temp->last = last;
5428 }
5429 else if (unused)
5430 ggc_free (unused);
5431 }
5432 else
5433 {
5434 loc = ggc_cleared_alloc<var_loc_node> ();
5435 temp->first = loc;
5436 temp->last = loc;
5437 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5438 }
5439 return loc;
5440 }
5441 \f
5442 /* Keep track of the number of spaces used to indent the
5443 output of the debugging routines that print the structure of
5444 the DIE internal representation. */
5445 static int print_indent;
5446
5447 /* Indent the line the number of spaces given by print_indent. */
5448
5449 static inline void
5450 print_spaces (FILE *outfile)
5451 {
5452 fprintf (outfile, "%*s", print_indent, "");
5453 }
5454
5455 /* Print a type signature in hex. */
5456
5457 static inline void
5458 print_signature (FILE *outfile, char *sig)
5459 {
5460 int i;
5461
5462 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5463 fprintf (outfile, "%02x", sig[i] & 0xff);
5464 }
5465
5466 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5467
5468 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5469 RECURSE, output location descriptor operations. */
5470
5471 static void
5472 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5473 {
5474 switch (val->val_class)
5475 {
5476 case dw_val_class_addr:
5477 fprintf (outfile, "address");
5478 break;
5479 case dw_val_class_offset:
5480 fprintf (outfile, "offset");
5481 break;
5482 case dw_val_class_loc:
5483 fprintf (outfile, "location descriptor");
5484 if (val->v.val_loc == NULL)
5485 fprintf (outfile, " -> <null>\n");
5486 else if (recurse)
5487 {
5488 fprintf (outfile, ":\n");
5489 print_indent += 4;
5490 print_loc_descr (val->v.val_loc, outfile);
5491 print_indent -= 4;
5492 }
5493 else
5494 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5495 break;
5496 case dw_val_class_loc_list:
5497 fprintf (outfile, "location list -> label:%s",
5498 val->v.val_loc_list->ll_symbol);
5499 break;
5500 case dw_val_class_range_list:
5501 fprintf (outfile, "range list");
5502 break;
5503 case dw_val_class_const:
5504 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5505 break;
5506 case dw_val_class_unsigned_const:
5507 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5508 break;
5509 case dw_val_class_const_double:
5510 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5511 HOST_WIDE_INT_PRINT_UNSIGNED")",
5512 val->v.val_double.high,
5513 val->v.val_double.low);
5514 break;
5515 case dw_val_class_wide_int:
5516 {
5517 int i = val->v.val_wide->get_len ();
5518 fprintf (outfile, "constant (");
5519 gcc_assert (i > 0);
5520 if (val->v.val_wide->elt (i - 1) == 0)
5521 fprintf (outfile, "0x");
5522 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5523 val->v.val_wide->elt (--i));
5524 while (--i >= 0)
5525 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5526 val->v.val_wide->elt (i));
5527 fprintf (outfile, ")");
5528 break;
5529 }
5530 case dw_val_class_vec:
5531 fprintf (outfile, "floating-point or vector constant");
5532 break;
5533 case dw_val_class_flag:
5534 fprintf (outfile, "%u", val->v.val_flag);
5535 break;
5536 case dw_val_class_die_ref:
5537 if (val->v.val_die_ref.die != NULL)
5538 {
5539 dw_die_ref die = val->v.val_die_ref.die;
5540
5541 if (die->comdat_type_p)
5542 {
5543 fprintf (outfile, "die -> signature: ");
5544 print_signature (outfile,
5545 die->die_id.die_type_node->signature);
5546 }
5547 else if (die->die_id.die_symbol)
5548 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5549 else
5550 fprintf (outfile, "die -> %ld", die->die_offset);
5551 fprintf (outfile, " (%p)", (void *) die);
5552 }
5553 else
5554 fprintf (outfile, "die -> <null>");
5555 break;
5556 case dw_val_class_vms_delta:
5557 fprintf (outfile, "delta: @slotcount(%s-%s)",
5558 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5559 break;
5560 case dw_val_class_lbl_id:
5561 case dw_val_class_lineptr:
5562 case dw_val_class_macptr:
5563 case dw_val_class_high_pc:
5564 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5565 break;
5566 case dw_val_class_str:
5567 if (val->v.val_str->str != NULL)
5568 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5569 else
5570 fprintf (outfile, "<null>");
5571 break;
5572 case dw_val_class_file:
5573 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5574 val->v.val_file->emitted_number);
5575 break;
5576 case dw_val_class_data8:
5577 {
5578 int i;
5579
5580 for (i = 0; i < 8; i++)
5581 fprintf (outfile, "%02x", val->v.val_data8[i]);
5582 break;
5583 }
5584 default:
5585 break;
5586 }
5587 }
5588
5589 /* Likewise, for a DIE attribute. */
5590
5591 static void
5592 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5593 {
5594 print_dw_val (&a->dw_attr_val, recurse, outfile);
5595 }
5596
5597
5598 /* Print the list of operands in the LOC location description to OUTFILE. This
5599 routine is a debugging aid only. */
5600
5601 static void
5602 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5603 {
5604 dw_loc_descr_ref l = loc;
5605
5606 if (loc == NULL)
5607 {
5608 print_spaces (outfile);
5609 fprintf (outfile, "<null>\n");
5610 return;
5611 }
5612
5613 for (l = loc; l != NULL; l = l->dw_loc_next)
5614 {
5615 print_spaces (outfile);
5616 fprintf (outfile, "(%p) %s",
5617 (void *) l,
5618 dwarf_stack_op_name (l->dw_loc_opc));
5619 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5620 {
5621 fprintf (outfile, " ");
5622 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5623 }
5624 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5625 {
5626 fprintf (outfile, ", ");
5627 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5628 }
5629 fprintf (outfile, "\n");
5630 }
5631 }
5632
5633 /* Print the information associated with a given DIE, and its children.
5634 This routine is a debugging aid only. */
5635
5636 static void
5637 print_die (dw_die_ref die, FILE *outfile)
5638 {
5639 dw_attr_node *a;
5640 dw_die_ref c;
5641 unsigned ix;
5642
5643 print_spaces (outfile);
5644 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5645 die->die_offset, dwarf_tag_name (die->die_tag),
5646 (void*) die);
5647 print_spaces (outfile);
5648 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5649 fprintf (outfile, " offset: %ld", die->die_offset);
5650 fprintf (outfile, " mark: %d\n", die->die_mark);
5651
5652 if (die->comdat_type_p)
5653 {
5654 print_spaces (outfile);
5655 fprintf (outfile, " signature: ");
5656 print_signature (outfile, die->die_id.die_type_node->signature);
5657 fprintf (outfile, "\n");
5658 }
5659
5660 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5661 {
5662 print_spaces (outfile);
5663 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5664
5665 print_attribute (a, true, outfile);
5666 fprintf (outfile, "\n");
5667 }
5668
5669 if (die->die_child != NULL)
5670 {
5671 print_indent += 4;
5672 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5673 print_indent -= 4;
5674 }
5675 if (print_indent == 0)
5676 fprintf (outfile, "\n");
5677 }
5678
5679 /* Print the list of operations in the LOC location description. */
5680
5681 DEBUG_FUNCTION void
5682 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5683 {
5684 print_loc_descr (loc, stderr);
5685 }
5686
5687 /* Print the information collected for a given DIE. */
5688
5689 DEBUG_FUNCTION void
5690 debug_dwarf_die (dw_die_ref die)
5691 {
5692 print_die (die, stderr);
5693 }
5694
5695 DEBUG_FUNCTION void
5696 debug (die_struct &ref)
5697 {
5698 print_die (&ref, stderr);
5699 }
5700
5701 DEBUG_FUNCTION void
5702 debug (die_struct *ptr)
5703 {
5704 if (ptr)
5705 debug (*ptr);
5706 else
5707 fprintf (stderr, "<nil>\n");
5708 }
5709
5710
5711 /* Print all DWARF information collected for the compilation unit.
5712 This routine is a debugging aid only. */
5713
5714 DEBUG_FUNCTION void
5715 debug_dwarf (void)
5716 {
5717 print_indent = 0;
5718 print_die (comp_unit_die (), stderr);
5719 }
5720
5721 /* Sanity checks on DIEs. */
5722
5723 static void
5724 check_die (dw_die_ref die)
5725 {
5726 unsigned ix;
5727 dw_attr_node *a;
5728 bool inline_found = false;
5729 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5730 int n_decl_line = 0, n_decl_file = 0;
5731 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5732 {
5733 switch (a->dw_attr)
5734 {
5735 case DW_AT_inline:
5736 if (a->dw_attr_val.v.val_unsigned)
5737 inline_found = true;
5738 break;
5739 case DW_AT_location:
5740 ++n_location;
5741 break;
5742 case DW_AT_low_pc:
5743 ++n_low_pc;
5744 break;
5745 case DW_AT_high_pc:
5746 ++n_high_pc;
5747 break;
5748 case DW_AT_artificial:
5749 ++n_artificial;
5750 break;
5751 case DW_AT_decl_line:
5752 ++n_decl_line;
5753 break;
5754 case DW_AT_decl_file:
5755 ++n_decl_file;
5756 break;
5757 default:
5758 break;
5759 }
5760 }
5761 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5762 || n_decl_line > 1 || n_decl_file > 1)
5763 {
5764 fprintf (stderr, "Duplicate attributes in DIE:\n");
5765 debug_dwarf_die (die);
5766 gcc_unreachable ();
5767 }
5768 if (inline_found)
5769 {
5770 /* A debugging information entry that is a member of an abstract
5771 instance tree [that has DW_AT_inline] should not contain any
5772 attributes which describe aspects of the subroutine which vary
5773 between distinct inlined expansions or distinct out-of-line
5774 expansions. */
5775 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5776 gcc_assert (a->dw_attr != DW_AT_low_pc
5777 && a->dw_attr != DW_AT_high_pc
5778 && a->dw_attr != DW_AT_location
5779 && a->dw_attr != DW_AT_frame_base
5780 && a->dw_attr != DW_AT_GNU_all_call_sites);
5781 }
5782 }
5783 \f
5784 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5785 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5786 DIE that marks the start of the DIEs for this include file. */
5787
5788 static dw_die_ref
5789 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5790 {
5791 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5792 dw_die_ref new_unit = gen_compile_unit_die (filename);
5793
5794 new_unit->die_sib = old_unit;
5795 return new_unit;
5796 }
5797
5798 /* Close an include-file CU and reopen the enclosing one. */
5799
5800 static dw_die_ref
5801 pop_compile_unit (dw_die_ref old_unit)
5802 {
5803 dw_die_ref new_unit = old_unit->die_sib;
5804
5805 old_unit->die_sib = NULL;
5806 return new_unit;
5807 }
5808
5809 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5810 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5811 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5812
5813 /* Calculate the checksum of a location expression. */
5814
5815 static inline void
5816 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5817 {
5818 int tem;
5819 inchash::hash hstate;
5820 hashval_t hash;
5821
5822 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5823 CHECKSUM (tem);
5824 hash_loc_operands (loc, hstate);
5825 hash = hstate.end();
5826 CHECKSUM (hash);
5827 }
5828
5829 /* Calculate the checksum of an attribute. */
5830
5831 static void
5832 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5833 {
5834 dw_loc_descr_ref loc;
5835 rtx r;
5836
5837 CHECKSUM (at->dw_attr);
5838
5839 /* We don't care that this was compiled with a different compiler
5840 snapshot; if the output is the same, that's what matters. */
5841 if (at->dw_attr == DW_AT_producer)
5842 return;
5843
5844 switch (AT_class (at))
5845 {
5846 case dw_val_class_const:
5847 CHECKSUM (at->dw_attr_val.v.val_int);
5848 break;
5849 case dw_val_class_unsigned_const:
5850 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5851 break;
5852 case dw_val_class_const_double:
5853 CHECKSUM (at->dw_attr_val.v.val_double);
5854 break;
5855 case dw_val_class_wide_int:
5856 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5857 get_full_len (*at->dw_attr_val.v.val_wide)
5858 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5859 break;
5860 case dw_val_class_vec:
5861 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5862 (at->dw_attr_val.v.val_vec.length
5863 * at->dw_attr_val.v.val_vec.elt_size));
5864 break;
5865 case dw_val_class_flag:
5866 CHECKSUM (at->dw_attr_val.v.val_flag);
5867 break;
5868 case dw_val_class_str:
5869 CHECKSUM_STRING (AT_string (at));
5870 break;
5871
5872 case dw_val_class_addr:
5873 r = AT_addr (at);
5874 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5875 CHECKSUM_STRING (XSTR (r, 0));
5876 break;
5877
5878 case dw_val_class_offset:
5879 CHECKSUM (at->dw_attr_val.v.val_offset);
5880 break;
5881
5882 case dw_val_class_loc:
5883 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5884 loc_checksum (loc, ctx);
5885 break;
5886
5887 case dw_val_class_die_ref:
5888 die_checksum (AT_ref (at), ctx, mark);
5889 break;
5890
5891 case dw_val_class_fde_ref:
5892 case dw_val_class_vms_delta:
5893 case dw_val_class_lbl_id:
5894 case dw_val_class_lineptr:
5895 case dw_val_class_macptr:
5896 case dw_val_class_high_pc:
5897 break;
5898
5899 case dw_val_class_file:
5900 CHECKSUM_STRING (AT_file (at)->filename);
5901 break;
5902
5903 case dw_val_class_data8:
5904 CHECKSUM (at->dw_attr_val.v.val_data8);
5905 break;
5906
5907 default:
5908 break;
5909 }
5910 }
5911
5912 /* Calculate the checksum of a DIE. */
5913
5914 static void
5915 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5916 {
5917 dw_die_ref c;
5918 dw_attr_node *a;
5919 unsigned ix;
5920
5921 /* To avoid infinite recursion. */
5922 if (die->die_mark)
5923 {
5924 CHECKSUM (die->die_mark);
5925 return;
5926 }
5927 die->die_mark = ++(*mark);
5928
5929 CHECKSUM (die->die_tag);
5930
5931 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5932 attr_checksum (a, ctx, mark);
5933
5934 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5935 }
5936
5937 #undef CHECKSUM
5938 #undef CHECKSUM_BLOCK
5939 #undef CHECKSUM_STRING
5940
5941 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5942 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5943 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5944 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5945 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5946 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5947 #define CHECKSUM_ATTR(FOO) \
5948 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5949
5950 /* Calculate the checksum of a number in signed LEB128 format. */
5951
5952 static void
5953 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5954 {
5955 unsigned char byte;
5956 bool more;
5957
5958 while (1)
5959 {
5960 byte = (value & 0x7f);
5961 value >>= 7;
5962 more = !((value == 0 && (byte & 0x40) == 0)
5963 || (value == -1 && (byte & 0x40) != 0));
5964 if (more)
5965 byte |= 0x80;
5966 CHECKSUM (byte);
5967 if (!more)
5968 break;
5969 }
5970 }
5971
5972 /* Calculate the checksum of a number in unsigned LEB128 format. */
5973
5974 static void
5975 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5976 {
5977 while (1)
5978 {
5979 unsigned char byte = (value & 0x7f);
5980 value >>= 7;
5981 if (value != 0)
5982 /* More bytes to follow. */
5983 byte |= 0x80;
5984 CHECKSUM (byte);
5985 if (value == 0)
5986 break;
5987 }
5988 }
5989
5990 /* Checksum the context of the DIE. This adds the names of any
5991 surrounding namespaces or structures to the checksum. */
5992
5993 static void
5994 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5995 {
5996 const char *name;
5997 dw_die_ref spec;
5998 int tag = die->die_tag;
5999
6000 if (tag != DW_TAG_namespace
6001 && tag != DW_TAG_structure_type
6002 && tag != DW_TAG_class_type)
6003 return;
6004
6005 name = get_AT_string (die, DW_AT_name);
6006
6007 spec = get_AT_ref (die, DW_AT_specification);
6008 if (spec != NULL)
6009 die = spec;
6010
6011 if (die->die_parent != NULL)
6012 checksum_die_context (die->die_parent, ctx);
6013
6014 CHECKSUM_ULEB128 ('C');
6015 CHECKSUM_ULEB128 (tag);
6016 if (name != NULL)
6017 CHECKSUM_STRING (name);
6018 }
6019
6020 /* Calculate the checksum of a location expression. */
6021
6022 static inline void
6023 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6024 {
6025 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6026 were emitted as a DW_FORM_sdata instead of a location expression. */
6027 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6028 {
6029 CHECKSUM_ULEB128 (DW_FORM_sdata);
6030 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6031 return;
6032 }
6033
6034 /* Otherwise, just checksum the raw location expression. */
6035 while (loc != NULL)
6036 {
6037 inchash::hash hstate;
6038 hashval_t hash;
6039
6040 CHECKSUM_ULEB128 (loc->dtprel);
6041 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6042 hash_loc_operands (loc, hstate);
6043 hash = hstate.end ();
6044 CHECKSUM (hash);
6045 loc = loc->dw_loc_next;
6046 }
6047 }
6048
6049 /* Calculate the checksum of an attribute. */
6050
6051 static void
6052 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6053 struct md5_ctx *ctx, int *mark)
6054 {
6055 dw_loc_descr_ref loc;
6056 rtx r;
6057
6058 if (AT_class (at) == dw_val_class_die_ref)
6059 {
6060 dw_die_ref target_die = AT_ref (at);
6061
6062 /* For pointer and reference types, we checksum only the (qualified)
6063 name of the target type (if there is a name). For friend entries,
6064 we checksum only the (qualified) name of the target type or function.
6065 This allows the checksum to remain the same whether the target type
6066 is complete or not. */
6067 if ((at->dw_attr == DW_AT_type
6068 && (tag == DW_TAG_pointer_type
6069 || tag == DW_TAG_reference_type
6070 || tag == DW_TAG_rvalue_reference_type
6071 || tag == DW_TAG_ptr_to_member_type))
6072 || (at->dw_attr == DW_AT_friend
6073 && tag == DW_TAG_friend))
6074 {
6075 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6076
6077 if (name_attr != NULL)
6078 {
6079 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6080
6081 if (decl == NULL)
6082 decl = target_die;
6083 CHECKSUM_ULEB128 ('N');
6084 CHECKSUM_ULEB128 (at->dw_attr);
6085 if (decl->die_parent != NULL)
6086 checksum_die_context (decl->die_parent, ctx);
6087 CHECKSUM_ULEB128 ('E');
6088 CHECKSUM_STRING (AT_string (name_attr));
6089 return;
6090 }
6091 }
6092
6093 /* For all other references to another DIE, we check to see if the
6094 target DIE has already been visited. If it has, we emit a
6095 backward reference; if not, we descend recursively. */
6096 if (target_die->die_mark > 0)
6097 {
6098 CHECKSUM_ULEB128 ('R');
6099 CHECKSUM_ULEB128 (at->dw_attr);
6100 CHECKSUM_ULEB128 (target_die->die_mark);
6101 }
6102 else
6103 {
6104 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6105
6106 if (decl == NULL)
6107 decl = target_die;
6108 target_die->die_mark = ++(*mark);
6109 CHECKSUM_ULEB128 ('T');
6110 CHECKSUM_ULEB128 (at->dw_attr);
6111 if (decl->die_parent != NULL)
6112 checksum_die_context (decl->die_parent, ctx);
6113 die_checksum_ordered (target_die, ctx, mark);
6114 }
6115 return;
6116 }
6117
6118 CHECKSUM_ULEB128 ('A');
6119 CHECKSUM_ULEB128 (at->dw_attr);
6120
6121 switch (AT_class (at))
6122 {
6123 case dw_val_class_const:
6124 CHECKSUM_ULEB128 (DW_FORM_sdata);
6125 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6126 break;
6127
6128 case dw_val_class_unsigned_const:
6129 CHECKSUM_ULEB128 (DW_FORM_sdata);
6130 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6131 break;
6132
6133 case dw_val_class_const_double:
6134 CHECKSUM_ULEB128 (DW_FORM_block);
6135 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6136 CHECKSUM (at->dw_attr_val.v.val_double);
6137 break;
6138
6139 case dw_val_class_wide_int:
6140 CHECKSUM_ULEB128 (DW_FORM_block);
6141 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6142 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6143 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6144 get_full_len (*at->dw_attr_val.v.val_wide)
6145 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6146 break;
6147
6148 case dw_val_class_vec:
6149 CHECKSUM_ULEB128 (DW_FORM_block);
6150 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6151 * at->dw_attr_val.v.val_vec.elt_size);
6152 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6153 (at->dw_attr_val.v.val_vec.length
6154 * at->dw_attr_val.v.val_vec.elt_size));
6155 break;
6156
6157 case dw_val_class_flag:
6158 CHECKSUM_ULEB128 (DW_FORM_flag);
6159 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6160 break;
6161
6162 case dw_val_class_str:
6163 CHECKSUM_ULEB128 (DW_FORM_string);
6164 CHECKSUM_STRING (AT_string (at));
6165 break;
6166
6167 case dw_val_class_addr:
6168 r = AT_addr (at);
6169 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6170 CHECKSUM_ULEB128 (DW_FORM_string);
6171 CHECKSUM_STRING (XSTR (r, 0));
6172 break;
6173
6174 case dw_val_class_offset:
6175 CHECKSUM_ULEB128 (DW_FORM_sdata);
6176 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6177 break;
6178
6179 case dw_val_class_loc:
6180 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6181 loc_checksum_ordered (loc, ctx);
6182 break;
6183
6184 case dw_val_class_fde_ref:
6185 case dw_val_class_lbl_id:
6186 case dw_val_class_lineptr:
6187 case dw_val_class_macptr:
6188 case dw_val_class_high_pc:
6189 break;
6190
6191 case dw_val_class_file:
6192 CHECKSUM_ULEB128 (DW_FORM_string);
6193 CHECKSUM_STRING (AT_file (at)->filename);
6194 break;
6195
6196 case dw_val_class_data8:
6197 CHECKSUM (at->dw_attr_val.v.val_data8);
6198 break;
6199
6200 default:
6201 break;
6202 }
6203 }
6204
6205 struct checksum_attributes
6206 {
6207 dw_attr_node *at_name;
6208 dw_attr_node *at_type;
6209 dw_attr_node *at_friend;
6210 dw_attr_node *at_accessibility;
6211 dw_attr_node *at_address_class;
6212 dw_attr_node *at_allocated;
6213 dw_attr_node *at_artificial;
6214 dw_attr_node *at_associated;
6215 dw_attr_node *at_binary_scale;
6216 dw_attr_node *at_bit_offset;
6217 dw_attr_node *at_bit_size;
6218 dw_attr_node *at_bit_stride;
6219 dw_attr_node *at_byte_size;
6220 dw_attr_node *at_byte_stride;
6221 dw_attr_node *at_const_value;
6222 dw_attr_node *at_containing_type;
6223 dw_attr_node *at_count;
6224 dw_attr_node *at_data_location;
6225 dw_attr_node *at_data_member_location;
6226 dw_attr_node *at_decimal_scale;
6227 dw_attr_node *at_decimal_sign;
6228 dw_attr_node *at_default_value;
6229 dw_attr_node *at_digit_count;
6230 dw_attr_node *at_discr;
6231 dw_attr_node *at_discr_list;
6232 dw_attr_node *at_discr_value;
6233 dw_attr_node *at_encoding;
6234 dw_attr_node *at_endianity;
6235 dw_attr_node *at_explicit;
6236 dw_attr_node *at_is_optional;
6237 dw_attr_node *at_location;
6238 dw_attr_node *at_lower_bound;
6239 dw_attr_node *at_mutable;
6240 dw_attr_node *at_ordering;
6241 dw_attr_node *at_picture_string;
6242 dw_attr_node *at_prototyped;
6243 dw_attr_node *at_small;
6244 dw_attr_node *at_segment;
6245 dw_attr_node *at_string_length;
6246 dw_attr_node *at_threads_scaled;
6247 dw_attr_node *at_upper_bound;
6248 dw_attr_node *at_use_location;
6249 dw_attr_node *at_use_UTF8;
6250 dw_attr_node *at_variable_parameter;
6251 dw_attr_node *at_virtuality;
6252 dw_attr_node *at_visibility;
6253 dw_attr_node *at_vtable_elem_location;
6254 };
6255
6256 /* Collect the attributes that we will want to use for the checksum. */
6257
6258 static void
6259 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6260 {
6261 dw_attr_node *a;
6262 unsigned ix;
6263
6264 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6265 {
6266 switch (a->dw_attr)
6267 {
6268 case DW_AT_name:
6269 attrs->at_name = a;
6270 break;
6271 case DW_AT_type:
6272 attrs->at_type = a;
6273 break;
6274 case DW_AT_friend:
6275 attrs->at_friend = a;
6276 break;
6277 case DW_AT_accessibility:
6278 attrs->at_accessibility = a;
6279 break;
6280 case DW_AT_address_class:
6281 attrs->at_address_class = a;
6282 break;
6283 case DW_AT_allocated:
6284 attrs->at_allocated = a;
6285 break;
6286 case DW_AT_artificial:
6287 attrs->at_artificial = a;
6288 break;
6289 case DW_AT_associated:
6290 attrs->at_associated = a;
6291 break;
6292 case DW_AT_binary_scale:
6293 attrs->at_binary_scale = a;
6294 break;
6295 case DW_AT_bit_offset:
6296 attrs->at_bit_offset = a;
6297 break;
6298 case DW_AT_bit_size:
6299 attrs->at_bit_size = a;
6300 break;
6301 case DW_AT_bit_stride:
6302 attrs->at_bit_stride = a;
6303 break;
6304 case DW_AT_byte_size:
6305 attrs->at_byte_size = a;
6306 break;
6307 case DW_AT_byte_stride:
6308 attrs->at_byte_stride = a;
6309 break;
6310 case DW_AT_const_value:
6311 attrs->at_const_value = a;
6312 break;
6313 case DW_AT_containing_type:
6314 attrs->at_containing_type = a;
6315 break;
6316 case DW_AT_count:
6317 attrs->at_count = a;
6318 break;
6319 case DW_AT_data_location:
6320 attrs->at_data_location = a;
6321 break;
6322 case DW_AT_data_member_location:
6323 attrs->at_data_member_location = a;
6324 break;
6325 case DW_AT_decimal_scale:
6326 attrs->at_decimal_scale = a;
6327 break;
6328 case DW_AT_decimal_sign:
6329 attrs->at_decimal_sign = a;
6330 break;
6331 case DW_AT_default_value:
6332 attrs->at_default_value = a;
6333 break;
6334 case DW_AT_digit_count:
6335 attrs->at_digit_count = a;
6336 break;
6337 case DW_AT_discr:
6338 attrs->at_discr = a;
6339 break;
6340 case DW_AT_discr_list:
6341 attrs->at_discr_list = a;
6342 break;
6343 case DW_AT_discr_value:
6344 attrs->at_discr_value = a;
6345 break;
6346 case DW_AT_encoding:
6347 attrs->at_encoding = a;
6348 break;
6349 case DW_AT_endianity:
6350 attrs->at_endianity = a;
6351 break;
6352 case DW_AT_explicit:
6353 attrs->at_explicit = a;
6354 break;
6355 case DW_AT_is_optional:
6356 attrs->at_is_optional = a;
6357 break;
6358 case DW_AT_location:
6359 attrs->at_location = a;
6360 break;
6361 case DW_AT_lower_bound:
6362 attrs->at_lower_bound = a;
6363 break;
6364 case DW_AT_mutable:
6365 attrs->at_mutable = a;
6366 break;
6367 case DW_AT_ordering:
6368 attrs->at_ordering = a;
6369 break;
6370 case DW_AT_picture_string:
6371 attrs->at_picture_string = a;
6372 break;
6373 case DW_AT_prototyped:
6374 attrs->at_prototyped = a;
6375 break;
6376 case DW_AT_small:
6377 attrs->at_small = a;
6378 break;
6379 case DW_AT_segment:
6380 attrs->at_segment = a;
6381 break;
6382 case DW_AT_string_length:
6383 attrs->at_string_length = a;
6384 break;
6385 case DW_AT_threads_scaled:
6386 attrs->at_threads_scaled = a;
6387 break;
6388 case DW_AT_upper_bound:
6389 attrs->at_upper_bound = a;
6390 break;
6391 case DW_AT_use_location:
6392 attrs->at_use_location = a;
6393 break;
6394 case DW_AT_use_UTF8:
6395 attrs->at_use_UTF8 = a;
6396 break;
6397 case DW_AT_variable_parameter:
6398 attrs->at_variable_parameter = a;
6399 break;
6400 case DW_AT_virtuality:
6401 attrs->at_virtuality = a;
6402 break;
6403 case DW_AT_visibility:
6404 attrs->at_visibility = a;
6405 break;
6406 case DW_AT_vtable_elem_location:
6407 attrs->at_vtable_elem_location = a;
6408 break;
6409 default:
6410 break;
6411 }
6412 }
6413 }
6414
6415 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6416
6417 static void
6418 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6419 {
6420 dw_die_ref c;
6421 dw_die_ref decl;
6422 struct checksum_attributes attrs;
6423
6424 CHECKSUM_ULEB128 ('D');
6425 CHECKSUM_ULEB128 (die->die_tag);
6426
6427 memset (&attrs, 0, sizeof (attrs));
6428
6429 decl = get_AT_ref (die, DW_AT_specification);
6430 if (decl != NULL)
6431 collect_checksum_attributes (&attrs, decl);
6432 collect_checksum_attributes (&attrs, die);
6433
6434 CHECKSUM_ATTR (attrs.at_name);
6435 CHECKSUM_ATTR (attrs.at_accessibility);
6436 CHECKSUM_ATTR (attrs.at_address_class);
6437 CHECKSUM_ATTR (attrs.at_allocated);
6438 CHECKSUM_ATTR (attrs.at_artificial);
6439 CHECKSUM_ATTR (attrs.at_associated);
6440 CHECKSUM_ATTR (attrs.at_binary_scale);
6441 CHECKSUM_ATTR (attrs.at_bit_offset);
6442 CHECKSUM_ATTR (attrs.at_bit_size);
6443 CHECKSUM_ATTR (attrs.at_bit_stride);
6444 CHECKSUM_ATTR (attrs.at_byte_size);
6445 CHECKSUM_ATTR (attrs.at_byte_stride);
6446 CHECKSUM_ATTR (attrs.at_const_value);
6447 CHECKSUM_ATTR (attrs.at_containing_type);
6448 CHECKSUM_ATTR (attrs.at_count);
6449 CHECKSUM_ATTR (attrs.at_data_location);
6450 CHECKSUM_ATTR (attrs.at_data_member_location);
6451 CHECKSUM_ATTR (attrs.at_decimal_scale);
6452 CHECKSUM_ATTR (attrs.at_decimal_sign);
6453 CHECKSUM_ATTR (attrs.at_default_value);
6454 CHECKSUM_ATTR (attrs.at_digit_count);
6455 CHECKSUM_ATTR (attrs.at_discr);
6456 CHECKSUM_ATTR (attrs.at_discr_list);
6457 CHECKSUM_ATTR (attrs.at_discr_value);
6458 CHECKSUM_ATTR (attrs.at_encoding);
6459 CHECKSUM_ATTR (attrs.at_endianity);
6460 CHECKSUM_ATTR (attrs.at_explicit);
6461 CHECKSUM_ATTR (attrs.at_is_optional);
6462 CHECKSUM_ATTR (attrs.at_location);
6463 CHECKSUM_ATTR (attrs.at_lower_bound);
6464 CHECKSUM_ATTR (attrs.at_mutable);
6465 CHECKSUM_ATTR (attrs.at_ordering);
6466 CHECKSUM_ATTR (attrs.at_picture_string);
6467 CHECKSUM_ATTR (attrs.at_prototyped);
6468 CHECKSUM_ATTR (attrs.at_small);
6469 CHECKSUM_ATTR (attrs.at_segment);
6470 CHECKSUM_ATTR (attrs.at_string_length);
6471 CHECKSUM_ATTR (attrs.at_threads_scaled);
6472 CHECKSUM_ATTR (attrs.at_upper_bound);
6473 CHECKSUM_ATTR (attrs.at_use_location);
6474 CHECKSUM_ATTR (attrs.at_use_UTF8);
6475 CHECKSUM_ATTR (attrs.at_variable_parameter);
6476 CHECKSUM_ATTR (attrs.at_virtuality);
6477 CHECKSUM_ATTR (attrs.at_visibility);
6478 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6479 CHECKSUM_ATTR (attrs.at_type);
6480 CHECKSUM_ATTR (attrs.at_friend);
6481
6482 /* Checksum the child DIEs. */
6483 c = die->die_child;
6484 if (c) do {
6485 dw_attr_node *name_attr;
6486
6487 c = c->die_sib;
6488 name_attr = get_AT (c, DW_AT_name);
6489 if (is_template_instantiation (c))
6490 {
6491 /* Ignore instantiations of member type and function templates. */
6492 }
6493 else if (name_attr != NULL
6494 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6495 {
6496 /* Use a shallow checksum for named nested types and member
6497 functions. */
6498 CHECKSUM_ULEB128 ('S');
6499 CHECKSUM_ULEB128 (c->die_tag);
6500 CHECKSUM_STRING (AT_string (name_attr));
6501 }
6502 else
6503 {
6504 /* Use a deep checksum for other children. */
6505 /* Mark this DIE so it gets processed when unmarking. */
6506 if (c->die_mark == 0)
6507 c->die_mark = -1;
6508 die_checksum_ordered (c, ctx, mark);
6509 }
6510 } while (c != die->die_child);
6511
6512 CHECKSUM_ULEB128 (0);
6513 }
6514
6515 /* Add a type name and tag to a hash. */
6516 static void
6517 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6518 {
6519 CHECKSUM_ULEB128 (tag);
6520 CHECKSUM_STRING (name);
6521 }
6522
6523 #undef CHECKSUM
6524 #undef CHECKSUM_STRING
6525 #undef CHECKSUM_ATTR
6526 #undef CHECKSUM_LEB128
6527 #undef CHECKSUM_ULEB128
6528
6529 /* Generate the type signature for DIE. This is computed by generating an
6530 MD5 checksum over the DIE's tag, its relevant attributes, and its
6531 children. Attributes that are references to other DIEs are processed
6532 by recursion, using the MARK field to prevent infinite recursion.
6533 If the DIE is nested inside a namespace or another type, we also
6534 need to include that context in the signature. The lower 64 bits
6535 of the resulting MD5 checksum comprise the signature. */
6536
6537 static void
6538 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6539 {
6540 int mark;
6541 const char *name;
6542 unsigned char checksum[16];
6543 struct md5_ctx ctx;
6544 dw_die_ref decl;
6545 dw_die_ref parent;
6546
6547 name = get_AT_string (die, DW_AT_name);
6548 decl = get_AT_ref (die, DW_AT_specification);
6549 parent = get_die_parent (die);
6550
6551 /* First, compute a signature for just the type name (and its surrounding
6552 context, if any. This is stored in the type unit DIE for link-time
6553 ODR (one-definition rule) checking. */
6554
6555 if (is_cxx () && name != NULL)
6556 {
6557 md5_init_ctx (&ctx);
6558
6559 /* Checksum the names of surrounding namespaces and structures. */
6560 if (parent != NULL)
6561 checksum_die_context (parent, &ctx);
6562
6563 /* Checksum the current DIE. */
6564 die_odr_checksum (die->die_tag, name, &ctx);
6565 md5_finish_ctx (&ctx, checksum);
6566
6567 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6568 }
6569
6570 /* Next, compute the complete type signature. */
6571
6572 md5_init_ctx (&ctx);
6573 mark = 1;
6574 die->die_mark = mark;
6575
6576 /* Checksum the names of surrounding namespaces and structures. */
6577 if (parent != NULL)
6578 checksum_die_context (parent, &ctx);
6579
6580 /* Checksum the DIE and its children. */
6581 die_checksum_ordered (die, &ctx, &mark);
6582 unmark_all_dies (die);
6583 md5_finish_ctx (&ctx, checksum);
6584
6585 /* Store the signature in the type node and link the type DIE and the
6586 type node together. */
6587 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6588 DWARF_TYPE_SIGNATURE_SIZE);
6589 die->comdat_type_p = true;
6590 die->die_id.die_type_node = type_node;
6591 type_node->type_die = die;
6592
6593 /* If the DIE is a specification, link its declaration to the type node
6594 as well. */
6595 if (decl != NULL)
6596 {
6597 decl->comdat_type_p = true;
6598 decl->die_id.die_type_node = type_node;
6599 }
6600 }
6601
6602 /* Do the location expressions look same? */
6603 static inline int
6604 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6605 {
6606 return loc1->dw_loc_opc == loc2->dw_loc_opc
6607 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6608 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6609 }
6610
6611 /* Do the values look the same? */
6612 static int
6613 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6614 {
6615 dw_loc_descr_ref loc1, loc2;
6616 rtx r1, r2;
6617
6618 if (v1->val_class != v2->val_class)
6619 return 0;
6620
6621 switch (v1->val_class)
6622 {
6623 case dw_val_class_const:
6624 return v1->v.val_int == v2->v.val_int;
6625 case dw_val_class_unsigned_const:
6626 return v1->v.val_unsigned == v2->v.val_unsigned;
6627 case dw_val_class_const_double:
6628 return v1->v.val_double.high == v2->v.val_double.high
6629 && v1->v.val_double.low == v2->v.val_double.low;
6630 case dw_val_class_wide_int:
6631 return *v1->v.val_wide == *v2->v.val_wide;
6632 case dw_val_class_vec:
6633 if (v1->v.val_vec.length != v2->v.val_vec.length
6634 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6635 return 0;
6636 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6637 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6638 return 0;
6639 return 1;
6640 case dw_val_class_flag:
6641 return v1->v.val_flag == v2->v.val_flag;
6642 case dw_val_class_str:
6643 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6644
6645 case dw_val_class_addr:
6646 r1 = v1->v.val_addr;
6647 r2 = v2->v.val_addr;
6648 if (GET_CODE (r1) != GET_CODE (r2))
6649 return 0;
6650 return !rtx_equal_p (r1, r2);
6651
6652 case dw_val_class_offset:
6653 return v1->v.val_offset == v2->v.val_offset;
6654
6655 case dw_val_class_loc:
6656 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6657 loc1 && loc2;
6658 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6659 if (!same_loc_p (loc1, loc2, mark))
6660 return 0;
6661 return !loc1 && !loc2;
6662
6663 case dw_val_class_die_ref:
6664 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6665
6666 case dw_val_class_fde_ref:
6667 case dw_val_class_vms_delta:
6668 case dw_val_class_lbl_id:
6669 case dw_val_class_lineptr:
6670 case dw_val_class_macptr:
6671 case dw_val_class_high_pc:
6672 return 1;
6673
6674 case dw_val_class_file:
6675 return v1->v.val_file == v2->v.val_file;
6676
6677 case dw_val_class_data8:
6678 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6679
6680 default:
6681 return 1;
6682 }
6683 }
6684
6685 /* Do the attributes look the same? */
6686
6687 static int
6688 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6689 {
6690 if (at1->dw_attr != at2->dw_attr)
6691 return 0;
6692
6693 /* We don't care that this was compiled with a different compiler
6694 snapshot; if the output is the same, that's what matters. */
6695 if (at1->dw_attr == DW_AT_producer)
6696 return 1;
6697
6698 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6699 }
6700
6701 /* Do the dies look the same? */
6702
6703 static int
6704 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6705 {
6706 dw_die_ref c1, c2;
6707 dw_attr_node *a1;
6708 unsigned ix;
6709
6710 /* To avoid infinite recursion. */
6711 if (die1->die_mark)
6712 return die1->die_mark == die2->die_mark;
6713 die1->die_mark = die2->die_mark = ++(*mark);
6714
6715 if (die1->die_tag != die2->die_tag)
6716 return 0;
6717
6718 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6719 return 0;
6720
6721 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6722 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6723 return 0;
6724
6725 c1 = die1->die_child;
6726 c2 = die2->die_child;
6727 if (! c1)
6728 {
6729 if (c2)
6730 return 0;
6731 }
6732 else
6733 for (;;)
6734 {
6735 if (!same_die_p (c1, c2, mark))
6736 return 0;
6737 c1 = c1->die_sib;
6738 c2 = c2->die_sib;
6739 if (c1 == die1->die_child)
6740 {
6741 if (c2 == die2->die_child)
6742 break;
6743 else
6744 return 0;
6745 }
6746 }
6747
6748 return 1;
6749 }
6750
6751 /* Do the dies look the same? Wrapper around same_die_p. */
6752
6753 static int
6754 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6755 {
6756 int mark = 0;
6757 int ret = same_die_p (die1, die2, &mark);
6758
6759 unmark_all_dies (die1);
6760 unmark_all_dies (die2);
6761
6762 return ret;
6763 }
6764
6765 /* The prefix to attach to symbols on DIEs in the current comdat debug
6766 info section. */
6767 static const char *comdat_symbol_id;
6768
6769 /* The index of the current symbol within the current comdat CU. */
6770 static unsigned int comdat_symbol_number;
6771
6772 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6773 children, and set comdat_symbol_id accordingly. */
6774
6775 static void
6776 compute_section_prefix (dw_die_ref unit_die)
6777 {
6778 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6779 const char *base = die_name ? lbasename (die_name) : "anonymous";
6780 char *name = XALLOCAVEC (char, strlen (base) + 64);
6781 char *p;
6782 int i, mark;
6783 unsigned char checksum[16];
6784 struct md5_ctx ctx;
6785
6786 /* Compute the checksum of the DIE, then append part of it as hex digits to
6787 the name filename of the unit. */
6788
6789 md5_init_ctx (&ctx);
6790 mark = 0;
6791 die_checksum (unit_die, &ctx, &mark);
6792 unmark_all_dies (unit_die);
6793 md5_finish_ctx (&ctx, checksum);
6794
6795 sprintf (name, "%s.", base);
6796 clean_symbol_name (name);
6797
6798 p = name + strlen (name);
6799 for (i = 0; i < 4; i++)
6800 {
6801 sprintf (p, "%.2x", checksum[i]);
6802 p += 2;
6803 }
6804
6805 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6806 comdat_symbol_number = 0;
6807 }
6808
6809 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6810
6811 static int
6812 is_type_die (dw_die_ref die)
6813 {
6814 switch (die->die_tag)
6815 {
6816 case DW_TAG_array_type:
6817 case DW_TAG_class_type:
6818 case DW_TAG_interface_type:
6819 case DW_TAG_enumeration_type:
6820 case DW_TAG_pointer_type:
6821 case DW_TAG_reference_type:
6822 case DW_TAG_rvalue_reference_type:
6823 case DW_TAG_string_type:
6824 case DW_TAG_structure_type:
6825 case DW_TAG_subroutine_type:
6826 case DW_TAG_union_type:
6827 case DW_TAG_ptr_to_member_type:
6828 case DW_TAG_set_type:
6829 case DW_TAG_subrange_type:
6830 case DW_TAG_base_type:
6831 case DW_TAG_const_type:
6832 case DW_TAG_file_type:
6833 case DW_TAG_packed_type:
6834 case DW_TAG_volatile_type:
6835 case DW_TAG_typedef:
6836 return 1;
6837 default:
6838 return 0;
6839 }
6840 }
6841
6842 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6843 Basically, we want to choose the bits that are likely to be shared between
6844 compilations (types) and leave out the bits that are specific to individual
6845 compilations (functions). */
6846
6847 static int
6848 is_comdat_die (dw_die_ref c)
6849 {
6850 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6851 we do for stabs. The advantage is a greater likelihood of sharing between
6852 objects that don't include headers in the same order (and therefore would
6853 put the base types in a different comdat). jason 8/28/00 */
6854
6855 if (c->die_tag == DW_TAG_base_type)
6856 return 0;
6857
6858 if (c->die_tag == DW_TAG_pointer_type
6859 || c->die_tag == DW_TAG_reference_type
6860 || c->die_tag == DW_TAG_rvalue_reference_type
6861 || c->die_tag == DW_TAG_const_type
6862 || c->die_tag == DW_TAG_volatile_type)
6863 {
6864 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6865
6866 return t ? is_comdat_die (t) : 0;
6867 }
6868
6869 return is_type_die (c);
6870 }
6871
6872 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6873 compilation unit. */
6874
6875 static int
6876 is_symbol_die (dw_die_ref c)
6877 {
6878 return (is_type_die (c)
6879 || is_declaration_die (c)
6880 || c->die_tag == DW_TAG_namespace
6881 || c->die_tag == DW_TAG_module);
6882 }
6883
6884 /* Returns true iff C is a compile-unit DIE. */
6885
6886 static inline bool
6887 is_cu_die (dw_die_ref c)
6888 {
6889 return c && c->die_tag == DW_TAG_compile_unit;
6890 }
6891
6892 /* Returns true iff C is a unit DIE of some sort. */
6893
6894 static inline bool
6895 is_unit_die (dw_die_ref c)
6896 {
6897 return c && (c->die_tag == DW_TAG_compile_unit
6898 || c->die_tag == DW_TAG_partial_unit
6899 || c->die_tag == DW_TAG_type_unit);
6900 }
6901
6902 /* Returns true iff C is a namespace DIE. */
6903
6904 static inline bool
6905 is_namespace_die (dw_die_ref c)
6906 {
6907 return c && c->die_tag == DW_TAG_namespace;
6908 }
6909
6910 /* Returns true iff C is a class or structure DIE. */
6911
6912 static inline bool
6913 is_class_die (dw_die_ref c)
6914 {
6915 return c && (c->die_tag == DW_TAG_class_type
6916 || c->die_tag == DW_TAG_structure_type);
6917 }
6918
6919 /* Return non-zero if this DIE is a template parameter. */
6920
6921 static inline bool
6922 is_template_parameter (dw_die_ref die)
6923 {
6924 switch (die->die_tag)
6925 {
6926 case DW_TAG_template_type_param:
6927 case DW_TAG_template_value_param:
6928 case DW_TAG_GNU_template_template_param:
6929 case DW_TAG_GNU_template_parameter_pack:
6930 return true;
6931 default:
6932 return false;
6933 }
6934 }
6935
6936 /* Return non-zero if this DIE represents a template instantiation. */
6937
6938 static inline bool
6939 is_template_instantiation (dw_die_ref die)
6940 {
6941 dw_die_ref c;
6942
6943 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6944 return false;
6945 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6946 return false;
6947 }
6948
6949 static char *
6950 gen_internal_sym (const char *prefix)
6951 {
6952 char buf[256];
6953
6954 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6955 return xstrdup (buf);
6956 }
6957
6958 /* Assign symbols to all worthy DIEs under DIE. */
6959
6960 static void
6961 assign_symbol_names (dw_die_ref die)
6962 {
6963 dw_die_ref c;
6964
6965 if (is_symbol_die (die) && !die->comdat_type_p)
6966 {
6967 if (comdat_symbol_id)
6968 {
6969 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6970
6971 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6972 comdat_symbol_id, comdat_symbol_number++);
6973 die->die_id.die_symbol = xstrdup (p);
6974 }
6975 else
6976 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6977 }
6978
6979 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6980 }
6981
6982 struct cu_hash_table_entry
6983 {
6984 dw_die_ref cu;
6985 unsigned min_comdat_num, max_comdat_num;
6986 struct cu_hash_table_entry *next;
6987 };
6988
6989 /* Helpers to manipulate hash table of CUs. */
6990
6991 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6992 {
6993 typedef die_struct *compare_type;
6994 static inline hashval_t hash (const cu_hash_table_entry *);
6995 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6996 static inline void remove (cu_hash_table_entry *);
6997 };
6998
6999 inline hashval_t
7000 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7001 {
7002 return htab_hash_string (entry->cu->die_id.die_symbol);
7003 }
7004
7005 inline bool
7006 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7007 const die_struct *entry2)
7008 {
7009 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7010 }
7011
7012 inline void
7013 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7014 {
7015 struct cu_hash_table_entry *next;
7016
7017 while (entry)
7018 {
7019 next = entry->next;
7020 free (entry);
7021 entry = next;
7022 }
7023 }
7024
7025 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7026
7027 /* Check whether we have already seen this CU and set up SYM_NUM
7028 accordingly. */
7029 static int
7030 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7031 {
7032 struct cu_hash_table_entry dummy;
7033 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7034
7035 dummy.max_comdat_num = 0;
7036
7037 slot = htable->find_slot_with_hash (cu,
7038 htab_hash_string (cu->die_id.die_symbol),
7039 INSERT);
7040 entry = *slot;
7041
7042 for (; entry; last = entry, entry = entry->next)
7043 {
7044 if (same_die_p_wrap (cu, entry->cu))
7045 break;
7046 }
7047
7048 if (entry)
7049 {
7050 *sym_num = entry->min_comdat_num;
7051 return 1;
7052 }
7053
7054 entry = XCNEW (struct cu_hash_table_entry);
7055 entry->cu = cu;
7056 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7057 entry->next = *slot;
7058 *slot = entry;
7059
7060 return 0;
7061 }
7062
7063 /* Record SYM_NUM to record of CU in HTABLE. */
7064 static void
7065 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7066 unsigned int sym_num)
7067 {
7068 struct cu_hash_table_entry **slot, *entry;
7069
7070 slot = htable->find_slot_with_hash (cu,
7071 htab_hash_string (cu->die_id.die_symbol),
7072 NO_INSERT);
7073 entry = *slot;
7074
7075 entry->max_comdat_num = sym_num;
7076 }
7077
7078 /* Traverse the DIE (which is always comp_unit_die), and set up
7079 additional compilation units for each of the include files we see
7080 bracketed by BINCL/EINCL. */
7081
7082 static void
7083 break_out_includes (dw_die_ref die)
7084 {
7085 dw_die_ref c;
7086 dw_die_ref unit = NULL;
7087 limbo_die_node *node, **pnode;
7088
7089 c = die->die_child;
7090 if (c) do {
7091 dw_die_ref prev = c;
7092 c = c->die_sib;
7093 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7094 || (unit && is_comdat_die (c)))
7095 {
7096 dw_die_ref next = c->die_sib;
7097
7098 /* This DIE is for a secondary CU; remove it from the main one. */
7099 remove_child_with_prev (c, prev);
7100
7101 if (c->die_tag == DW_TAG_GNU_BINCL)
7102 unit = push_new_compile_unit (unit, c);
7103 else if (c->die_tag == DW_TAG_GNU_EINCL)
7104 unit = pop_compile_unit (unit);
7105 else
7106 add_child_die (unit, c);
7107 c = next;
7108 if (c == die->die_child)
7109 break;
7110 }
7111 } while (c != die->die_child);
7112
7113 #if 0
7114 /* We can only use this in debugging, since the frontend doesn't check
7115 to make sure that we leave every include file we enter. */
7116 gcc_assert (!unit);
7117 #endif
7118
7119 assign_symbol_names (die);
7120 cu_hash_type cu_hash_table (10);
7121 for (node = limbo_die_list, pnode = &limbo_die_list;
7122 node;
7123 node = node->next)
7124 {
7125 int is_dupl;
7126
7127 compute_section_prefix (node->die);
7128 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7129 &comdat_symbol_number);
7130 assign_symbol_names (node->die);
7131 if (is_dupl)
7132 *pnode = node->next;
7133 else
7134 {
7135 pnode = &node->next;
7136 record_comdat_symbol_number (node->die, &cu_hash_table,
7137 comdat_symbol_number);
7138 }
7139 }
7140 }
7141
7142 /* Return non-zero if this DIE is a declaration. */
7143
7144 static int
7145 is_declaration_die (dw_die_ref die)
7146 {
7147 dw_attr_node *a;
7148 unsigned ix;
7149
7150 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7151 if (a->dw_attr == DW_AT_declaration)
7152 return 1;
7153
7154 return 0;
7155 }
7156
7157 /* Return non-zero if this DIE is nested inside a subprogram. */
7158
7159 static int
7160 is_nested_in_subprogram (dw_die_ref die)
7161 {
7162 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7163
7164 if (decl == NULL)
7165 decl = die;
7166 return local_scope_p (decl);
7167 }
7168
7169 /* Return non-zero if this DIE contains a defining declaration of a
7170 subprogram. */
7171
7172 static int
7173 contains_subprogram_definition (dw_die_ref die)
7174 {
7175 dw_die_ref c;
7176
7177 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7178 return 1;
7179 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7180 return 0;
7181 }
7182
7183 /* Return non-zero if this is a type DIE that should be moved to a
7184 COMDAT .debug_types section. */
7185
7186 static int
7187 should_move_die_to_comdat (dw_die_ref die)
7188 {
7189 switch (die->die_tag)
7190 {
7191 case DW_TAG_class_type:
7192 case DW_TAG_structure_type:
7193 case DW_TAG_enumeration_type:
7194 case DW_TAG_union_type:
7195 /* Don't move declarations, inlined instances, types nested in a
7196 subprogram, or types that contain subprogram definitions. */
7197 if (is_declaration_die (die)
7198 || get_AT (die, DW_AT_abstract_origin)
7199 || is_nested_in_subprogram (die)
7200 || contains_subprogram_definition (die))
7201 return 0;
7202 return 1;
7203 case DW_TAG_array_type:
7204 case DW_TAG_interface_type:
7205 case DW_TAG_pointer_type:
7206 case DW_TAG_reference_type:
7207 case DW_TAG_rvalue_reference_type:
7208 case DW_TAG_string_type:
7209 case DW_TAG_subroutine_type:
7210 case DW_TAG_ptr_to_member_type:
7211 case DW_TAG_set_type:
7212 case DW_TAG_subrange_type:
7213 case DW_TAG_base_type:
7214 case DW_TAG_const_type:
7215 case DW_TAG_file_type:
7216 case DW_TAG_packed_type:
7217 case DW_TAG_volatile_type:
7218 case DW_TAG_typedef:
7219 default:
7220 return 0;
7221 }
7222 }
7223
7224 /* Make a clone of DIE. */
7225
7226 static dw_die_ref
7227 clone_die (dw_die_ref die)
7228 {
7229 dw_die_ref clone;
7230 dw_attr_node *a;
7231 unsigned ix;
7232
7233 clone = ggc_cleared_alloc<die_node> ();
7234 clone->die_tag = die->die_tag;
7235
7236 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7237 add_dwarf_attr (clone, a);
7238
7239 return clone;
7240 }
7241
7242 /* Make a clone of the tree rooted at DIE. */
7243
7244 static dw_die_ref
7245 clone_tree (dw_die_ref die)
7246 {
7247 dw_die_ref c;
7248 dw_die_ref clone = clone_die (die);
7249
7250 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7251
7252 return clone;
7253 }
7254
7255 /* Make a clone of DIE as a declaration. */
7256
7257 static dw_die_ref
7258 clone_as_declaration (dw_die_ref die)
7259 {
7260 dw_die_ref clone;
7261 dw_die_ref decl;
7262 dw_attr_node *a;
7263 unsigned ix;
7264
7265 /* If the DIE is already a declaration, just clone it. */
7266 if (is_declaration_die (die))
7267 return clone_die (die);
7268
7269 /* If the DIE is a specification, just clone its declaration DIE. */
7270 decl = get_AT_ref (die, DW_AT_specification);
7271 if (decl != NULL)
7272 {
7273 clone = clone_die (decl);
7274 if (die->comdat_type_p)
7275 add_AT_die_ref (clone, DW_AT_signature, die);
7276 return clone;
7277 }
7278
7279 clone = ggc_cleared_alloc<die_node> ();
7280 clone->die_tag = die->die_tag;
7281
7282 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7283 {
7284 /* We don't want to copy over all attributes.
7285 For example we don't want DW_AT_byte_size because otherwise we will no
7286 longer have a declaration and GDB will treat it as a definition. */
7287
7288 switch (a->dw_attr)
7289 {
7290 case DW_AT_abstract_origin:
7291 case DW_AT_artificial:
7292 case DW_AT_containing_type:
7293 case DW_AT_external:
7294 case DW_AT_name:
7295 case DW_AT_type:
7296 case DW_AT_virtuality:
7297 case DW_AT_linkage_name:
7298 case DW_AT_MIPS_linkage_name:
7299 add_dwarf_attr (clone, a);
7300 break;
7301 case DW_AT_byte_size:
7302 default:
7303 break;
7304 }
7305 }
7306
7307 if (die->comdat_type_p)
7308 add_AT_die_ref (clone, DW_AT_signature, die);
7309
7310 add_AT_flag (clone, DW_AT_declaration, 1);
7311 return clone;
7312 }
7313
7314
7315 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7316
7317 struct decl_table_entry
7318 {
7319 dw_die_ref orig;
7320 dw_die_ref copy;
7321 };
7322
7323 /* Helpers to manipulate hash table of copied declarations. */
7324
7325 /* Hashtable helpers. */
7326
7327 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7328 {
7329 typedef die_struct *compare_type;
7330 static inline hashval_t hash (const decl_table_entry *);
7331 static inline bool equal (const decl_table_entry *, const die_struct *);
7332 };
7333
7334 inline hashval_t
7335 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7336 {
7337 return htab_hash_pointer (entry->orig);
7338 }
7339
7340 inline bool
7341 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7342 const die_struct *entry2)
7343 {
7344 return entry1->orig == entry2;
7345 }
7346
7347 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7348
7349 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7350 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7351 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7352 to check if the ancestor has already been copied into UNIT. */
7353
7354 static dw_die_ref
7355 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7356 decl_hash_type *decl_table)
7357 {
7358 dw_die_ref parent = die->die_parent;
7359 dw_die_ref new_parent = unit;
7360 dw_die_ref copy;
7361 decl_table_entry **slot = NULL;
7362 struct decl_table_entry *entry = NULL;
7363
7364 if (decl_table)
7365 {
7366 /* Check if the entry has already been copied to UNIT. */
7367 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7368 INSERT);
7369 if (*slot != HTAB_EMPTY_ENTRY)
7370 {
7371 entry = *slot;
7372 return entry->copy;
7373 }
7374
7375 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7376 entry = XCNEW (struct decl_table_entry);
7377 entry->orig = die;
7378 entry->copy = NULL;
7379 *slot = entry;
7380 }
7381
7382 if (parent != NULL)
7383 {
7384 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7385 if (spec != NULL)
7386 parent = spec;
7387 if (!is_unit_die (parent))
7388 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7389 }
7390
7391 copy = clone_as_declaration (die);
7392 add_child_die (new_parent, copy);
7393
7394 if (decl_table)
7395 {
7396 /* Record the pointer to the copy. */
7397 entry->copy = copy;
7398 }
7399
7400 return copy;
7401 }
7402 /* Copy the declaration context to the new type unit DIE. This includes
7403 any surrounding namespace or type declarations. If the DIE has an
7404 AT_specification attribute, it also includes attributes and children
7405 attached to the specification, and returns a pointer to the original
7406 parent of the declaration DIE. Returns NULL otherwise. */
7407
7408 static dw_die_ref
7409 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7410 {
7411 dw_die_ref decl;
7412 dw_die_ref new_decl;
7413 dw_die_ref orig_parent = NULL;
7414
7415 decl = get_AT_ref (die, DW_AT_specification);
7416 if (decl == NULL)
7417 decl = die;
7418 else
7419 {
7420 unsigned ix;
7421 dw_die_ref c;
7422 dw_attr_node *a;
7423
7424 /* The original DIE will be changed to a declaration, and must
7425 be moved to be a child of the original declaration DIE. */
7426 orig_parent = decl->die_parent;
7427
7428 /* Copy the type node pointer from the new DIE to the original
7429 declaration DIE so we can forward references later. */
7430 decl->comdat_type_p = true;
7431 decl->die_id.die_type_node = die->die_id.die_type_node;
7432
7433 remove_AT (die, DW_AT_specification);
7434
7435 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7436 {
7437 if (a->dw_attr != DW_AT_name
7438 && a->dw_attr != DW_AT_declaration
7439 && a->dw_attr != DW_AT_external)
7440 add_dwarf_attr (die, a);
7441 }
7442
7443 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7444 }
7445
7446 if (decl->die_parent != NULL
7447 && !is_unit_die (decl->die_parent))
7448 {
7449 new_decl = copy_ancestor_tree (unit, decl, NULL);
7450 if (new_decl != NULL)
7451 {
7452 remove_AT (new_decl, DW_AT_signature);
7453 add_AT_specification (die, new_decl);
7454 }
7455 }
7456
7457 return orig_parent;
7458 }
7459
7460 /* Generate the skeleton ancestor tree for the given NODE, then clone
7461 the DIE and add the clone into the tree. */
7462
7463 static void
7464 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7465 {
7466 if (node->new_die != NULL)
7467 return;
7468
7469 node->new_die = clone_as_declaration (node->old_die);
7470
7471 if (node->parent != NULL)
7472 {
7473 generate_skeleton_ancestor_tree (node->parent);
7474 add_child_die (node->parent->new_die, node->new_die);
7475 }
7476 }
7477
7478 /* Generate a skeleton tree of DIEs containing any declarations that are
7479 found in the original tree. We traverse the tree looking for declaration
7480 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7481
7482 static void
7483 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7484 {
7485 skeleton_chain_node node;
7486 dw_die_ref c;
7487 dw_die_ref first;
7488 dw_die_ref prev = NULL;
7489 dw_die_ref next = NULL;
7490
7491 node.parent = parent;
7492
7493 first = c = parent->old_die->die_child;
7494 if (c)
7495 next = c->die_sib;
7496 if (c) do {
7497 if (prev == NULL || prev->die_sib == c)
7498 prev = c;
7499 c = next;
7500 next = (c == first ? NULL : c->die_sib);
7501 node.old_die = c;
7502 node.new_die = NULL;
7503 if (is_declaration_die (c))
7504 {
7505 if (is_template_instantiation (c))
7506 {
7507 /* Instantiated templates do not need to be cloned into the
7508 type unit. Just move the DIE and its children back to
7509 the skeleton tree (in the main CU). */
7510 remove_child_with_prev (c, prev);
7511 add_child_die (parent->new_die, c);
7512 c = prev;
7513 }
7514 else
7515 {
7516 /* Clone the existing DIE, move the original to the skeleton
7517 tree (which is in the main CU), and put the clone, with
7518 all the original's children, where the original came from
7519 (which is about to be moved to the type unit). */
7520 dw_die_ref clone = clone_die (c);
7521 move_all_children (c, clone);
7522
7523 /* If the original has a DW_AT_object_pointer attribute,
7524 it would now point to a child DIE just moved to the
7525 cloned tree, so we need to remove that attribute from
7526 the original. */
7527 remove_AT (c, DW_AT_object_pointer);
7528
7529 replace_child (c, clone, prev);
7530 generate_skeleton_ancestor_tree (parent);
7531 add_child_die (parent->new_die, c);
7532 node.new_die = c;
7533 c = clone;
7534 }
7535 }
7536 generate_skeleton_bottom_up (&node);
7537 } while (next != NULL);
7538 }
7539
7540 /* Wrapper function for generate_skeleton_bottom_up. */
7541
7542 static dw_die_ref
7543 generate_skeleton (dw_die_ref die)
7544 {
7545 skeleton_chain_node node;
7546
7547 node.old_die = die;
7548 node.new_die = NULL;
7549 node.parent = NULL;
7550
7551 /* If this type definition is nested inside another type,
7552 and is not an instantiation of a template, always leave
7553 at least a declaration in its place. */
7554 if (die->die_parent != NULL
7555 && is_type_die (die->die_parent)
7556 && !is_template_instantiation (die))
7557 node.new_die = clone_as_declaration (die);
7558
7559 generate_skeleton_bottom_up (&node);
7560 return node.new_die;
7561 }
7562
7563 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7564 declaration. The original DIE is moved to a new compile unit so that
7565 existing references to it follow it to the new location. If any of the
7566 original DIE's descendants is a declaration, we need to replace the
7567 original DIE with a skeleton tree and move the declarations back into the
7568 skeleton tree. */
7569
7570 static dw_die_ref
7571 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7572 dw_die_ref prev)
7573 {
7574 dw_die_ref skeleton, orig_parent;
7575
7576 /* Copy the declaration context to the type unit DIE. If the returned
7577 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7578 that DIE. */
7579 orig_parent = copy_declaration_context (unit, child);
7580
7581 skeleton = generate_skeleton (child);
7582 if (skeleton == NULL)
7583 remove_child_with_prev (child, prev);
7584 else
7585 {
7586 skeleton->comdat_type_p = true;
7587 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7588
7589 /* If the original DIE was a specification, we need to put
7590 the skeleton under the parent DIE of the declaration.
7591 This leaves the original declaration in the tree, but
7592 it will be pruned later since there are no longer any
7593 references to it. */
7594 if (orig_parent != NULL)
7595 {
7596 remove_child_with_prev (child, prev);
7597 add_child_die (orig_parent, skeleton);
7598 }
7599 else
7600 replace_child (child, skeleton, prev);
7601 }
7602
7603 return skeleton;
7604 }
7605
7606 /* Traverse the DIE and set up additional .debug_types sections for each
7607 type worthy of being placed in a COMDAT section. */
7608
7609 static void
7610 break_out_comdat_types (dw_die_ref die)
7611 {
7612 dw_die_ref c;
7613 dw_die_ref first;
7614 dw_die_ref prev = NULL;
7615 dw_die_ref next = NULL;
7616 dw_die_ref unit = NULL;
7617
7618 first = c = die->die_child;
7619 if (c)
7620 next = c->die_sib;
7621 if (c) do {
7622 if (prev == NULL || prev->die_sib == c)
7623 prev = c;
7624 c = next;
7625 next = (c == first ? NULL : c->die_sib);
7626 if (should_move_die_to_comdat (c))
7627 {
7628 dw_die_ref replacement;
7629 comdat_type_node *type_node;
7630
7631 /* Break out nested types into their own type units. */
7632 break_out_comdat_types (c);
7633
7634 /* Create a new type unit DIE as the root for the new tree, and
7635 add it to the list of comdat types. */
7636 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7637 add_AT_unsigned (unit, DW_AT_language,
7638 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7639 type_node = ggc_cleared_alloc<comdat_type_node> ();
7640 type_node->root_die = unit;
7641 type_node->next = comdat_type_list;
7642 comdat_type_list = type_node;
7643
7644 /* Generate the type signature. */
7645 generate_type_signature (c, type_node);
7646
7647 /* Copy the declaration context, attributes, and children of the
7648 declaration into the new type unit DIE, then remove this DIE
7649 from the main CU (or replace it with a skeleton if necessary). */
7650 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7651 type_node->skeleton_die = replacement;
7652
7653 /* Add the DIE to the new compunit. */
7654 add_child_die (unit, c);
7655
7656 if (replacement != NULL)
7657 c = replacement;
7658 }
7659 else if (c->die_tag == DW_TAG_namespace
7660 || c->die_tag == DW_TAG_class_type
7661 || c->die_tag == DW_TAG_structure_type
7662 || c->die_tag == DW_TAG_union_type)
7663 {
7664 /* Look for nested types that can be broken out. */
7665 break_out_comdat_types (c);
7666 }
7667 } while (next != NULL);
7668 }
7669
7670 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7671 Enter all the cloned children into the hash table decl_table. */
7672
7673 static dw_die_ref
7674 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7675 {
7676 dw_die_ref c;
7677 dw_die_ref clone;
7678 struct decl_table_entry *entry;
7679 decl_table_entry **slot;
7680
7681 if (die->die_tag == DW_TAG_subprogram)
7682 clone = clone_as_declaration (die);
7683 else
7684 clone = clone_die (die);
7685
7686 slot = decl_table->find_slot_with_hash (die,
7687 htab_hash_pointer (die), INSERT);
7688
7689 /* Assert that DIE isn't in the hash table yet. If it would be there
7690 before, the ancestors would be necessarily there as well, therefore
7691 clone_tree_partial wouldn't be called. */
7692 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7693
7694 entry = XCNEW (struct decl_table_entry);
7695 entry->orig = die;
7696 entry->copy = clone;
7697 *slot = entry;
7698
7699 if (die->die_tag != DW_TAG_subprogram)
7700 FOR_EACH_CHILD (die, c,
7701 add_child_die (clone, clone_tree_partial (c, decl_table)));
7702
7703 return clone;
7704 }
7705
7706 /* Walk the DIE and its children, looking for references to incomplete
7707 or trivial types that are unmarked (i.e., that are not in the current
7708 type_unit). */
7709
7710 static void
7711 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7712 {
7713 dw_die_ref c;
7714 dw_attr_node *a;
7715 unsigned ix;
7716
7717 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7718 {
7719 if (AT_class (a) == dw_val_class_die_ref)
7720 {
7721 dw_die_ref targ = AT_ref (a);
7722 decl_table_entry **slot;
7723 struct decl_table_entry *entry;
7724
7725 if (targ->die_mark != 0 || targ->comdat_type_p)
7726 continue;
7727
7728 slot = decl_table->find_slot_with_hash (targ,
7729 htab_hash_pointer (targ),
7730 INSERT);
7731
7732 if (*slot != HTAB_EMPTY_ENTRY)
7733 {
7734 /* TARG has already been copied, so we just need to
7735 modify the reference to point to the copy. */
7736 entry = *slot;
7737 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7738 }
7739 else
7740 {
7741 dw_die_ref parent = unit;
7742 dw_die_ref copy = clone_die (targ);
7743
7744 /* Record in DECL_TABLE that TARG has been copied.
7745 Need to do this now, before the recursive call,
7746 because DECL_TABLE may be expanded and SLOT
7747 would no longer be a valid pointer. */
7748 entry = XCNEW (struct decl_table_entry);
7749 entry->orig = targ;
7750 entry->copy = copy;
7751 *slot = entry;
7752
7753 /* If TARG is not a declaration DIE, we need to copy its
7754 children. */
7755 if (!is_declaration_die (targ))
7756 {
7757 FOR_EACH_CHILD (
7758 targ, c,
7759 add_child_die (copy,
7760 clone_tree_partial (c, decl_table)));
7761 }
7762
7763 /* Make sure the cloned tree is marked as part of the
7764 type unit. */
7765 mark_dies (copy);
7766
7767 /* If TARG has surrounding context, copy its ancestor tree
7768 into the new type unit. */
7769 if (targ->die_parent != NULL
7770 && !is_unit_die (targ->die_parent))
7771 parent = copy_ancestor_tree (unit, targ->die_parent,
7772 decl_table);
7773
7774 add_child_die (parent, copy);
7775 a->dw_attr_val.v.val_die_ref.die = copy;
7776
7777 /* Make sure the newly-copied DIE is walked. If it was
7778 installed in a previously-added context, it won't
7779 get visited otherwise. */
7780 if (parent != unit)
7781 {
7782 /* Find the highest point of the newly-added tree,
7783 mark each node along the way, and walk from there. */
7784 parent->die_mark = 1;
7785 while (parent->die_parent
7786 && parent->die_parent->die_mark == 0)
7787 {
7788 parent = parent->die_parent;
7789 parent->die_mark = 1;
7790 }
7791 copy_decls_walk (unit, parent, decl_table);
7792 }
7793 }
7794 }
7795 }
7796
7797 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7798 }
7799
7800 /* Copy declarations for "unworthy" types into the new comdat section.
7801 Incomplete types, modified types, and certain other types aren't broken
7802 out into comdat sections of their own, so they don't have a signature,
7803 and we need to copy the declaration into the same section so that we
7804 don't have an external reference. */
7805
7806 static void
7807 copy_decls_for_unworthy_types (dw_die_ref unit)
7808 {
7809 mark_dies (unit);
7810 decl_hash_type decl_table (10);
7811 copy_decls_walk (unit, unit, &decl_table);
7812 unmark_dies (unit);
7813 }
7814
7815 /* Traverse the DIE and add a sibling attribute if it may have the
7816 effect of speeding up access to siblings. To save some space,
7817 avoid generating sibling attributes for DIE's without children. */
7818
7819 static void
7820 add_sibling_attributes (dw_die_ref die)
7821 {
7822 dw_die_ref c;
7823
7824 if (! die->die_child)
7825 return;
7826
7827 if (die->die_parent && die != die->die_parent->die_child)
7828 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7829
7830 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7831 }
7832
7833 /* Output all location lists for the DIE and its children. */
7834
7835 static void
7836 output_location_lists (dw_die_ref die)
7837 {
7838 dw_die_ref c;
7839 dw_attr_node *a;
7840 unsigned ix;
7841
7842 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7843 if (AT_class (a) == dw_val_class_loc_list)
7844 output_loc_list (AT_loc_list (a));
7845
7846 FOR_EACH_CHILD (die, c, output_location_lists (c));
7847 }
7848
7849 /* We want to limit the number of external references, because they are
7850 larger than local references: a relocation takes multiple words, and
7851 even a sig8 reference is always eight bytes, whereas a local reference
7852 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7853 So if we encounter multiple external references to the same type DIE, we
7854 make a local typedef stub for it and redirect all references there.
7855
7856 This is the element of the hash table for keeping track of these
7857 references. */
7858
7859 struct external_ref
7860 {
7861 dw_die_ref type;
7862 dw_die_ref stub;
7863 unsigned n_refs;
7864 };
7865
7866 /* Hashtable helpers. */
7867
7868 struct external_ref_hasher : free_ptr_hash <external_ref>
7869 {
7870 static inline hashval_t hash (const external_ref *);
7871 static inline bool equal (const external_ref *, const external_ref *);
7872 };
7873
7874 inline hashval_t
7875 external_ref_hasher::hash (const external_ref *r)
7876 {
7877 dw_die_ref die = r->type;
7878 hashval_t h = 0;
7879
7880 /* We can't use the address of the DIE for hashing, because
7881 that will make the order of the stub DIEs non-deterministic. */
7882 if (! die->comdat_type_p)
7883 /* We have a symbol; use it to compute a hash. */
7884 h = htab_hash_string (die->die_id.die_symbol);
7885 else
7886 {
7887 /* We have a type signature; use a subset of the bits as the hash.
7888 The 8-byte signature is at least as large as hashval_t. */
7889 comdat_type_node *type_node = die->die_id.die_type_node;
7890 memcpy (&h, type_node->signature, sizeof (h));
7891 }
7892 return h;
7893 }
7894
7895 inline bool
7896 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7897 {
7898 return r1->type == r2->type;
7899 }
7900
7901 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7902
7903 /* Return a pointer to the external_ref for references to DIE. */
7904
7905 static struct external_ref *
7906 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7907 {
7908 struct external_ref ref, *ref_p;
7909 external_ref **slot;
7910
7911 ref.type = die;
7912 slot = map->find_slot (&ref, INSERT);
7913 if (*slot != HTAB_EMPTY_ENTRY)
7914 return *slot;
7915
7916 ref_p = XCNEW (struct external_ref);
7917 ref_p->type = die;
7918 *slot = ref_p;
7919 return ref_p;
7920 }
7921
7922 /* Subroutine of optimize_external_refs, below.
7923
7924 If we see a type skeleton, record it as our stub. If we see external
7925 references, remember how many we've seen. */
7926
7927 static void
7928 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7929 {
7930 dw_die_ref c;
7931 dw_attr_node *a;
7932 unsigned ix;
7933 struct external_ref *ref_p;
7934
7935 if (is_type_die (die)
7936 && (c = get_AT_ref (die, DW_AT_signature)))
7937 {
7938 /* This is a local skeleton; use it for local references. */
7939 ref_p = lookup_external_ref (map, c);
7940 ref_p->stub = die;
7941 }
7942
7943 /* Scan the DIE references, and remember any that refer to DIEs from
7944 other CUs (i.e. those which are not marked). */
7945 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7946 if (AT_class (a) == dw_val_class_die_ref
7947 && (c = AT_ref (a))->die_mark == 0
7948 && is_type_die (c))
7949 {
7950 ref_p = lookup_external_ref (map, c);
7951 ref_p->n_refs++;
7952 }
7953
7954 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7955 }
7956
7957 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7958 points to an external_ref, DATA is the CU we're processing. If we don't
7959 already have a local stub, and we have multiple refs, build a stub. */
7960
7961 int
7962 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7963 {
7964 struct external_ref *ref_p = *slot;
7965
7966 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7967 {
7968 /* We have multiple references to this type, so build a small stub.
7969 Both of these forms are a bit dodgy from the perspective of the
7970 DWARF standard, since technically they should have names. */
7971 dw_die_ref cu = data;
7972 dw_die_ref type = ref_p->type;
7973 dw_die_ref stub = NULL;
7974
7975 if (type->comdat_type_p)
7976 {
7977 /* If we refer to this type via sig8, use AT_signature. */
7978 stub = new_die (type->die_tag, cu, NULL_TREE);
7979 add_AT_die_ref (stub, DW_AT_signature, type);
7980 }
7981 else
7982 {
7983 /* Otherwise, use a typedef with no name. */
7984 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7985 add_AT_die_ref (stub, DW_AT_type, type);
7986 }
7987
7988 stub->die_mark++;
7989 ref_p->stub = stub;
7990 }
7991 return 1;
7992 }
7993
7994 /* DIE is a unit; look through all the DIE references to see if there are
7995 any external references to types, and if so, create local stubs for
7996 them which will be applied in build_abbrev_table. This is useful because
7997 references to local DIEs are smaller. */
7998
7999 static external_ref_hash_type *
8000 optimize_external_refs (dw_die_ref die)
8001 {
8002 external_ref_hash_type *map = new external_ref_hash_type (10);
8003 optimize_external_refs_1 (die, map);
8004 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8005 return map;
8006 }
8007
8008 /* The format of each DIE (and its attribute value pairs) is encoded in an
8009 abbreviation table. This routine builds the abbreviation table and assigns
8010 a unique abbreviation id for each abbreviation entry. The children of each
8011 die are visited recursively. */
8012
8013 static void
8014 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8015 {
8016 unsigned long abbrev_id;
8017 unsigned int n_alloc;
8018 dw_die_ref c;
8019 dw_attr_node *a;
8020 unsigned ix;
8021
8022 /* Scan the DIE references, and replace any that refer to
8023 DIEs from other CUs (i.e. those which are not marked) with
8024 the local stubs we built in optimize_external_refs. */
8025 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8026 if (AT_class (a) == dw_val_class_die_ref
8027 && (c = AT_ref (a))->die_mark == 0)
8028 {
8029 struct external_ref *ref_p;
8030 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8031
8032 ref_p = lookup_external_ref (extern_map, c);
8033 if (ref_p->stub && ref_p->stub != die)
8034 change_AT_die_ref (a, ref_p->stub);
8035 else
8036 /* We aren't changing this reference, so mark it external. */
8037 set_AT_ref_external (a, 1);
8038 }
8039
8040 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8041 {
8042 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8043 dw_attr_node *die_a, *abbrev_a;
8044 unsigned ix;
8045 bool ok = true;
8046
8047 if (abbrev->die_tag != die->die_tag)
8048 continue;
8049 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8050 continue;
8051
8052 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8053 continue;
8054
8055 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8056 {
8057 abbrev_a = &(*abbrev->die_attr)[ix];
8058 if ((abbrev_a->dw_attr != die_a->dw_attr)
8059 || (value_format (abbrev_a) != value_format (die_a)))
8060 {
8061 ok = false;
8062 break;
8063 }
8064 }
8065 if (ok)
8066 break;
8067 }
8068
8069 if (abbrev_id >= abbrev_die_table_in_use)
8070 {
8071 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8072 {
8073 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8074 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8075 n_alloc);
8076
8077 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8078 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8079 abbrev_die_table_allocated = n_alloc;
8080 }
8081
8082 ++abbrev_die_table_in_use;
8083 abbrev_die_table[abbrev_id] = die;
8084 }
8085
8086 die->die_abbrev = abbrev_id;
8087 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8088 }
8089 \f
8090 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8091
8092 static int
8093 constant_size (unsigned HOST_WIDE_INT value)
8094 {
8095 int log;
8096
8097 if (value == 0)
8098 log = 0;
8099 else
8100 log = floor_log2 (value);
8101
8102 log = log / 8;
8103 log = 1 << (floor_log2 (log) + 1);
8104
8105 return log;
8106 }
8107
8108 /* Return the size of a DIE as it is represented in the
8109 .debug_info section. */
8110
8111 static unsigned long
8112 size_of_die (dw_die_ref die)
8113 {
8114 unsigned long size = 0;
8115 dw_attr_node *a;
8116 unsigned ix;
8117 enum dwarf_form form;
8118
8119 size += size_of_uleb128 (die->die_abbrev);
8120 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8121 {
8122 switch (AT_class (a))
8123 {
8124 case dw_val_class_addr:
8125 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8126 {
8127 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8128 size += size_of_uleb128 (AT_index (a));
8129 }
8130 else
8131 size += DWARF2_ADDR_SIZE;
8132 break;
8133 case dw_val_class_offset:
8134 size += DWARF_OFFSET_SIZE;
8135 break;
8136 case dw_val_class_loc:
8137 {
8138 unsigned long lsize = size_of_locs (AT_loc (a));
8139
8140 /* Block length. */
8141 if (dwarf_version >= 4)
8142 size += size_of_uleb128 (lsize);
8143 else
8144 size += constant_size (lsize);
8145 size += lsize;
8146 }
8147 break;
8148 case dw_val_class_loc_list:
8149 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8150 {
8151 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8152 size += size_of_uleb128 (AT_index (a));
8153 }
8154 else
8155 size += DWARF_OFFSET_SIZE;
8156 break;
8157 case dw_val_class_range_list:
8158 size += DWARF_OFFSET_SIZE;
8159 break;
8160 case dw_val_class_const:
8161 size += size_of_sleb128 (AT_int (a));
8162 break;
8163 case dw_val_class_unsigned_const:
8164 {
8165 int csize = constant_size (AT_unsigned (a));
8166 if (dwarf_version == 3
8167 && a->dw_attr == DW_AT_data_member_location
8168 && csize >= 4)
8169 size += size_of_uleb128 (AT_unsigned (a));
8170 else
8171 size += csize;
8172 }
8173 break;
8174 case dw_val_class_const_double:
8175 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8176 if (HOST_BITS_PER_WIDE_INT >= 64)
8177 size++; /* block */
8178 break;
8179 case dw_val_class_wide_int:
8180 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8181 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8182 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8183 > 64)
8184 size++; /* block */
8185 break;
8186 case dw_val_class_vec:
8187 size += constant_size (a->dw_attr_val.v.val_vec.length
8188 * a->dw_attr_val.v.val_vec.elt_size)
8189 + a->dw_attr_val.v.val_vec.length
8190 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8191 break;
8192 case dw_val_class_flag:
8193 if (dwarf_version >= 4)
8194 /* Currently all add_AT_flag calls pass in 1 as last argument,
8195 so DW_FORM_flag_present can be used. If that ever changes,
8196 we'll need to use DW_FORM_flag and have some optimization
8197 in build_abbrev_table that will change those to
8198 DW_FORM_flag_present if it is set to 1 in all DIEs using
8199 the same abbrev entry. */
8200 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8201 else
8202 size += 1;
8203 break;
8204 case dw_val_class_die_ref:
8205 if (AT_ref_external (a))
8206 {
8207 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8208 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8209 is sized by target address length, whereas in DWARF3
8210 it's always sized as an offset. */
8211 if (use_debug_types)
8212 size += DWARF_TYPE_SIGNATURE_SIZE;
8213 else if (dwarf_version == 2)
8214 size += DWARF2_ADDR_SIZE;
8215 else
8216 size += DWARF_OFFSET_SIZE;
8217 }
8218 else
8219 size += DWARF_OFFSET_SIZE;
8220 break;
8221 case dw_val_class_fde_ref:
8222 size += DWARF_OFFSET_SIZE;
8223 break;
8224 case dw_val_class_lbl_id:
8225 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8226 {
8227 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8228 size += size_of_uleb128 (AT_index (a));
8229 }
8230 else
8231 size += DWARF2_ADDR_SIZE;
8232 break;
8233 case dw_val_class_lineptr:
8234 case dw_val_class_macptr:
8235 size += DWARF_OFFSET_SIZE;
8236 break;
8237 case dw_val_class_str:
8238 form = AT_string_form (a);
8239 if (form == DW_FORM_strp)
8240 size += DWARF_OFFSET_SIZE;
8241 else if (form == DW_FORM_GNU_str_index)
8242 size += size_of_uleb128 (AT_index (a));
8243 else
8244 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8245 break;
8246 case dw_val_class_file:
8247 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8248 break;
8249 case dw_val_class_data8:
8250 size += 8;
8251 break;
8252 case dw_val_class_vms_delta:
8253 size += DWARF_OFFSET_SIZE;
8254 break;
8255 case dw_val_class_high_pc:
8256 size += DWARF2_ADDR_SIZE;
8257 break;
8258 default:
8259 gcc_unreachable ();
8260 }
8261 }
8262
8263 return size;
8264 }
8265
8266 /* Size the debugging information associated with a given DIE. Visits the
8267 DIE's children recursively. Updates the global variable next_die_offset, on
8268 each time through. Uses the current value of next_die_offset to update the
8269 die_offset field in each DIE. */
8270
8271 static void
8272 calc_die_sizes (dw_die_ref die)
8273 {
8274 dw_die_ref c;
8275
8276 gcc_assert (die->die_offset == 0
8277 || (unsigned long int) die->die_offset == next_die_offset);
8278 die->die_offset = next_die_offset;
8279 next_die_offset += size_of_die (die);
8280
8281 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8282
8283 if (die->die_child != NULL)
8284 /* Count the null byte used to terminate sibling lists. */
8285 next_die_offset += 1;
8286 }
8287
8288 /* Size just the base type children at the start of the CU.
8289 This is needed because build_abbrev needs to size locs
8290 and sizing of type based stack ops needs to know die_offset
8291 values for the base types. */
8292
8293 static void
8294 calc_base_type_die_sizes (void)
8295 {
8296 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8297 unsigned int i;
8298 dw_die_ref base_type;
8299 #if ENABLE_ASSERT_CHECKING
8300 dw_die_ref prev = comp_unit_die ()->die_child;
8301 #endif
8302
8303 die_offset += size_of_die (comp_unit_die ());
8304 for (i = 0; base_types.iterate (i, &base_type); i++)
8305 {
8306 #if ENABLE_ASSERT_CHECKING
8307 gcc_assert (base_type->die_offset == 0
8308 && prev->die_sib == base_type
8309 && base_type->die_child == NULL
8310 && base_type->die_abbrev);
8311 prev = base_type;
8312 #endif
8313 base_type->die_offset = die_offset;
8314 die_offset += size_of_die (base_type);
8315 }
8316 }
8317
8318 /* Set the marks for a die and its children. We do this so
8319 that we know whether or not a reference needs to use FORM_ref_addr; only
8320 DIEs in the same CU will be marked. We used to clear out the offset
8321 and use that as the flag, but ran into ordering problems. */
8322
8323 static void
8324 mark_dies (dw_die_ref die)
8325 {
8326 dw_die_ref c;
8327
8328 gcc_assert (!die->die_mark);
8329
8330 die->die_mark = 1;
8331 FOR_EACH_CHILD (die, c, mark_dies (c));
8332 }
8333
8334 /* Clear the marks for a die and its children. */
8335
8336 static void
8337 unmark_dies (dw_die_ref die)
8338 {
8339 dw_die_ref c;
8340
8341 if (! use_debug_types)
8342 gcc_assert (die->die_mark);
8343
8344 die->die_mark = 0;
8345 FOR_EACH_CHILD (die, c, unmark_dies (c));
8346 }
8347
8348 /* Clear the marks for a die, its children and referred dies. */
8349
8350 static void
8351 unmark_all_dies (dw_die_ref die)
8352 {
8353 dw_die_ref c;
8354 dw_attr_node *a;
8355 unsigned ix;
8356
8357 if (!die->die_mark)
8358 return;
8359 die->die_mark = 0;
8360
8361 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8362
8363 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8364 if (AT_class (a) == dw_val_class_die_ref)
8365 unmark_all_dies (AT_ref (a));
8366 }
8367
8368 /* Calculate if the entry should appear in the final output file. It may be
8369 from a pruned a type. */
8370
8371 static bool
8372 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8373 {
8374 /* By limiting gnu pubnames to definitions only, gold can generate a
8375 gdb index without entries for declarations, which don't include
8376 enough information to be useful. */
8377 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8378 return false;
8379
8380 if (table == pubname_table)
8381 {
8382 /* Enumerator names are part of the pubname table, but the
8383 parent DW_TAG_enumeration_type die may have been pruned.
8384 Don't output them if that is the case. */
8385 if (p->die->die_tag == DW_TAG_enumerator &&
8386 (p->die->die_parent == NULL
8387 || !p->die->die_parent->die_perennial_p))
8388 return false;
8389
8390 /* Everything else in the pubname table is included. */
8391 return true;
8392 }
8393
8394 /* The pubtypes table shouldn't include types that have been
8395 pruned. */
8396 return (p->die->die_offset != 0
8397 || !flag_eliminate_unused_debug_types);
8398 }
8399
8400 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8401 generated for the compilation unit. */
8402
8403 static unsigned long
8404 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8405 {
8406 unsigned long size;
8407 unsigned i;
8408 pubname_entry *p;
8409 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8410
8411 size = DWARF_PUBNAMES_HEADER_SIZE;
8412 FOR_EACH_VEC_ELT (*names, i, p)
8413 if (include_pubname_in_output (names, p))
8414 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8415
8416 size += DWARF_OFFSET_SIZE;
8417 return size;
8418 }
8419
8420 /* Return the size of the information in the .debug_aranges section. */
8421
8422 static unsigned long
8423 size_of_aranges (void)
8424 {
8425 unsigned long size;
8426
8427 size = DWARF_ARANGES_HEADER_SIZE;
8428
8429 /* Count the address/length pair for this compilation unit. */
8430 if (text_section_used)
8431 size += 2 * DWARF2_ADDR_SIZE;
8432 if (cold_text_section_used)
8433 size += 2 * DWARF2_ADDR_SIZE;
8434 if (have_multiple_function_sections)
8435 {
8436 unsigned fde_idx;
8437 dw_fde_ref fde;
8438
8439 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8440 {
8441 if (DECL_IGNORED_P (fde->decl))
8442 continue;
8443 if (!fde->in_std_section)
8444 size += 2 * DWARF2_ADDR_SIZE;
8445 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8446 size += 2 * DWARF2_ADDR_SIZE;
8447 }
8448 }
8449
8450 /* Count the two zero words used to terminated the address range table. */
8451 size += 2 * DWARF2_ADDR_SIZE;
8452 return size;
8453 }
8454 \f
8455 /* Select the encoding of an attribute value. */
8456
8457 static enum dwarf_form
8458 value_format (dw_attr_node *a)
8459 {
8460 switch (AT_class (a))
8461 {
8462 case dw_val_class_addr:
8463 /* Only very few attributes allow DW_FORM_addr. */
8464 switch (a->dw_attr)
8465 {
8466 case DW_AT_low_pc:
8467 case DW_AT_high_pc:
8468 case DW_AT_entry_pc:
8469 case DW_AT_trampoline:
8470 return (AT_index (a) == NOT_INDEXED
8471 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8472 default:
8473 break;
8474 }
8475 switch (DWARF2_ADDR_SIZE)
8476 {
8477 case 1:
8478 return DW_FORM_data1;
8479 case 2:
8480 return DW_FORM_data2;
8481 case 4:
8482 return DW_FORM_data4;
8483 case 8:
8484 return DW_FORM_data8;
8485 default:
8486 gcc_unreachable ();
8487 }
8488 case dw_val_class_range_list:
8489 case dw_val_class_loc_list:
8490 if (dwarf_version >= 4)
8491 return DW_FORM_sec_offset;
8492 /* FALLTHRU */
8493 case dw_val_class_vms_delta:
8494 case dw_val_class_offset:
8495 switch (DWARF_OFFSET_SIZE)
8496 {
8497 case 4:
8498 return DW_FORM_data4;
8499 case 8:
8500 return DW_FORM_data8;
8501 default:
8502 gcc_unreachable ();
8503 }
8504 case dw_val_class_loc:
8505 if (dwarf_version >= 4)
8506 return DW_FORM_exprloc;
8507 switch (constant_size (size_of_locs (AT_loc (a))))
8508 {
8509 case 1:
8510 return DW_FORM_block1;
8511 case 2:
8512 return DW_FORM_block2;
8513 case 4:
8514 return DW_FORM_block4;
8515 default:
8516 gcc_unreachable ();
8517 }
8518 case dw_val_class_const:
8519 return DW_FORM_sdata;
8520 case dw_val_class_unsigned_const:
8521 switch (constant_size (AT_unsigned (a)))
8522 {
8523 case 1:
8524 return DW_FORM_data1;
8525 case 2:
8526 return DW_FORM_data2;
8527 case 4:
8528 /* In DWARF3 DW_AT_data_member_location with
8529 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8530 constant, so we need to use DW_FORM_udata if we need
8531 a large constant. */
8532 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8533 return DW_FORM_udata;
8534 return DW_FORM_data4;
8535 case 8:
8536 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8537 return DW_FORM_udata;
8538 return DW_FORM_data8;
8539 default:
8540 gcc_unreachable ();
8541 }
8542 case dw_val_class_const_double:
8543 switch (HOST_BITS_PER_WIDE_INT)
8544 {
8545 case 8:
8546 return DW_FORM_data2;
8547 case 16:
8548 return DW_FORM_data4;
8549 case 32:
8550 return DW_FORM_data8;
8551 case 64:
8552 default:
8553 return DW_FORM_block1;
8554 }
8555 case dw_val_class_wide_int:
8556 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8557 {
8558 case 8:
8559 return DW_FORM_data1;
8560 case 16:
8561 return DW_FORM_data2;
8562 case 32:
8563 return DW_FORM_data4;
8564 case 64:
8565 return DW_FORM_data8;
8566 default:
8567 return DW_FORM_block1;
8568 }
8569 case dw_val_class_vec:
8570 switch (constant_size (a->dw_attr_val.v.val_vec.length
8571 * a->dw_attr_val.v.val_vec.elt_size))
8572 {
8573 case 1:
8574 return DW_FORM_block1;
8575 case 2:
8576 return DW_FORM_block2;
8577 case 4:
8578 return DW_FORM_block4;
8579 default:
8580 gcc_unreachable ();
8581 }
8582 case dw_val_class_flag:
8583 if (dwarf_version >= 4)
8584 {
8585 /* Currently all add_AT_flag calls pass in 1 as last argument,
8586 so DW_FORM_flag_present can be used. If that ever changes,
8587 we'll need to use DW_FORM_flag and have some optimization
8588 in build_abbrev_table that will change those to
8589 DW_FORM_flag_present if it is set to 1 in all DIEs using
8590 the same abbrev entry. */
8591 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8592 return DW_FORM_flag_present;
8593 }
8594 return DW_FORM_flag;
8595 case dw_val_class_die_ref:
8596 if (AT_ref_external (a))
8597 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8598 else
8599 return DW_FORM_ref;
8600 case dw_val_class_fde_ref:
8601 return DW_FORM_data;
8602 case dw_val_class_lbl_id:
8603 return (AT_index (a) == NOT_INDEXED
8604 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8605 case dw_val_class_lineptr:
8606 case dw_val_class_macptr:
8607 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8608 case dw_val_class_str:
8609 return AT_string_form (a);
8610 case dw_val_class_file:
8611 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8612 {
8613 case 1:
8614 return DW_FORM_data1;
8615 case 2:
8616 return DW_FORM_data2;
8617 case 4:
8618 return DW_FORM_data4;
8619 default:
8620 gcc_unreachable ();
8621 }
8622
8623 case dw_val_class_data8:
8624 return DW_FORM_data8;
8625
8626 case dw_val_class_high_pc:
8627 switch (DWARF2_ADDR_SIZE)
8628 {
8629 case 1:
8630 return DW_FORM_data1;
8631 case 2:
8632 return DW_FORM_data2;
8633 case 4:
8634 return DW_FORM_data4;
8635 case 8:
8636 return DW_FORM_data8;
8637 default:
8638 gcc_unreachable ();
8639 }
8640
8641 default:
8642 gcc_unreachable ();
8643 }
8644 }
8645
8646 /* Output the encoding of an attribute value. */
8647
8648 static void
8649 output_value_format (dw_attr_node *a)
8650 {
8651 enum dwarf_form form = value_format (a);
8652
8653 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8654 }
8655
8656 /* Given a die and id, produce the appropriate abbreviations. */
8657
8658 static void
8659 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8660 {
8661 unsigned ix;
8662 dw_attr_node *a_attr;
8663
8664 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8665 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8666 dwarf_tag_name (abbrev->die_tag));
8667
8668 if (abbrev->die_child != NULL)
8669 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8670 else
8671 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8672
8673 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8674 {
8675 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8676 dwarf_attr_name (a_attr->dw_attr));
8677 output_value_format (a_attr);
8678 }
8679
8680 dw2_asm_output_data (1, 0, NULL);
8681 dw2_asm_output_data (1, 0, NULL);
8682 }
8683
8684
8685 /* Output the .debug_abbrev section which defines the DIE abbreviation
8686 table. */
8687
8688 static void
8689 output_abbrev_section (void)
8690 {
8691 unsigned long abbrev_id;
8692
8693 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8694 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8695
8696 /* Terminate the table. */
8697 dw2_asm_output_data (1, 0, NULL);
8698 }
8699
8700 /* Output a symbol we can use to refer to this DIE from another CU. */
8701
8702 static inline void
8703 output_die_symbol (dw_die_ref die)
8704 {
8705 const char *sym = die->die_id.die_symbol;
8706
8707 gcc_assert (!die->comdat_type_p);
8708
8709 if (sym == 0)
8710 return;
8711
8712 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8713 /* We make these global, not weak; if the target doesn't support
8714 .linkonce, it doesn't support combining the sections, so debugging
8715 will break. */
8716 targetm.asm_out.globalize_label (asm_out_file, sym);
8717
8718 ASM_OUTPUT_LABEL (asm_out_file, sym);
8719 }
8720
8721 /* Return a new location list, given the begin and end range, and the
8722 expression. */
8723
8724 static inline dw_loc_list_ref
8725 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8726 const char *section)
8727 {
8728 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8729
8730 retlist->begin = begin;
8731 retlist->begin_entry = NULL;
8732 retlist->end = end;
8733 retlist->expr = expr;
8734 retlist->section = section;
8735
8736 return retlist;
8737 }
8738
8739 /* Generate a new internal symbol for this location list node, if it
8740 hasn't got one yet. */
8741
8742 static inline void
8743 gen_llsym (dw_loc_list_ref list)
8744 {
8745 gcc_assert (!list->ll_symbol);
8746 list->ll_symbol = gen_internal_sym ("LLST");
8747 }
8748
8749 /* Output the location list given to us. */
8750
8751 static void
8752 output_loc_list (dw_loc_list_ref list_head)
8753 {
8754 dw_loc_list_ref curr = list_head;
8755
8756 if (list_head->emitted)
8757 return;
8758 list_head->emitted = true;
8759
8760 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8761
8762 /* Walk the location list, and output each range + expression. */
8763 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8764 {
8765 unsigned long size;
8766 /* Don't output an entry that starts and ends at the same address. */
8767 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8768 continue;
8769 size = size_of_locs (curr->expr);
8770 /* If the expression is too large, drop it on the floor. We could
8771 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8772 in the expression, but >= 64KB expressions for a single value
8773 in a single range are unlikely very useful. */
8774 if (size > 0xffff)
8775 continue;
8776 if (dwarf_split_debug_info)
8777 {
8778 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8779 "Location list start/length entry (%s)",
8780 list_head->ll_symbol);
8781 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8782 "Location list range start index (%s)",
8783 curr->begin);
8784 /* The length field is 4 bytes. If we ever need to support
8785 an 8-byte length, we can add a new DW_LLE code or fall back
8786 to DW_LLE_GNU_start_end_entry. */
8787 dw2_asm_output_delta (4, curr->end, curr->begin,
8788 "Location list range length (%s)",
8789 list_head->ll_symbol);
8790 }
8791 else if (!have_multiple_function_sections)
8792 {
8793 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8794 "Location list begin address (%s)",
8795 list_head->ll_symbol);
8796 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8797 "Location list end address (%s)",
8798 list_head->ll_symbol);
8799 }
8800 else
8801 {
8802 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8803 "Location list begin address (%s)",
8804 list_head->ll_symbol);
8805 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8806 "Location list end address (%s)",
8807 list_head->ll_symbol);
8808 }
8809
8810 /* Output the block length for this list of location operations. */
8811 gcc_assert (size <= 0xffff);
8812 dw2_asm_output_data (2, size, "%s", "Location expression size");
8813
8814 output_loc_sequence (curr->expr, -1);
8815 }
8816
8817 if (dwarf_split_debug_info)
8818 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8819 "Location list terminator (%s)",
8820 list_head->ll_symbol);
8821 else
8822 {
8823 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8824 "Location list terminator begin (%s)",
8825 list_head->ll_symbol);
8826 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8827 "Location list terminator end (%s)",
8828 list_head->ll_symbol);
8829 }
8830 }
8831
8832 /* Output a range_list offset into the debug_range section. Emit a
8833 relocated reference if val_entry is NULL, otherwise, emit an
8834 indirect reference. */
8835
8836 static void
8837 output_range_list_offset (dw_attr_node *a)
8838 {
8839 const char *name = dwarf_attr_name (a->dw_attr);
8840
8841 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8842 {
8843 char *p = strchr (ranges_section_label, '\0');
8844 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8845 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8846 debug_ranges_section, "%s", name);
8847 *p = '\0';
8848 }
8849 else
8850 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8851 "%s (offset from %s)", name, ranges_section_label);
8852 }
8853
8854 /* Output the offset into the debug_loc section. */
8855
8856 static void
8857 output_loc_list_offset (dw_attr_node *a)
8858 {
8859 char *sym = AT_loc_list (a)->ll_symbol;
8860
8861 gcc_assert (sym);
8862 if (dwarf_split_debug_info)
8863 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8864 "%s", dwarf_attr_name (a->dw_attr));
8865 else
8866 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8867 "%s", dwarf_attr_name (a->dw_attr));
8868 }
8869
8870 /* Output an attribute's index or value appropriately. */
8871
8872 static void
8873 output_attr_index_or_value (dw_attr_node *a)
8874 {
8875 const char *name = dwarf_attr_name (a->dw_attr);
8876
8877 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8878 {
8879 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8880 return;
8881 }
8882 switch (AT_class (a))
8883 {
8884 case dw_val_class_addr:
8885 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8886 break;
8887 case dw_val_class_high_pc:
8888 case dw_val_class_lbl_id:
8889 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8890 break;
8891 case dw_val_class_loc_list:
8892 output_loc_list_offset (a);
8893 break;
8894 default:
8895 gcc_unreachable ();
8896 }
8897 }
8898
8899 /* Output a type signature. */
8900
8901 static inline void
8902 output_signature (const char *sig, const char *name)
8903 {
8904 int i;
8905
8906 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8907 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8908 }
8909
8910 /* Output the DIE and its attributes. Called recursively to generate
8911 the definitions of each child DIE. */
8912
8913 static void
8914 output_die (dw_die_ref die)
8915 {
8916 dw_attr_node *a;
8917 dw_die_ref c;
8918 unsigned long size;
8919 unsigned ix;
8920
8921 /* If someone in another CU might refer to us, set up a symbol for
8922 them to point to. */
8923 if (! die->comdat_type_p && die->die_id.die_symbol)
8924 output_die_symbol (die);
8925
8926 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8927 (unsigned long)die->die_offset,
8928 dwarf_tag_name (die->die_tag));
8929
8930 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8931 {
8932 const char *name = dwarf_attr_name (a->dw_attr);
8933
8934 switch (AT_class (a))
8935 {
8936 case dw_val_class_addr:
8937 output_attr_index_or_value (a);
8938 break;
8939
8940 case dw_val_class_offset:
8941 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8942 "%s", name);
8943 break;
8944
8945 case dw_val_class_range_list:
8946 output_range_list_offset (a);
8947 break;
8948
8949 case dw_val_class_loc:
8950 size = size_of_locs (AT_loc (a));
8951
8952 /* Output the block length for this list of location operations. */
8953 if (dwarf_version >= 4)
8954 dw2_asm_output_data_uleb128 (size, "%s", name);
8955 else
8956 dw2_asm_output_data (constant_size (size), size, "%s", name);
8957
8958 output_loc_sequence (AT_loc (a), -1);
8959 break;
8960
8961 case dw_val_class_const:
8962 /* ??? It would be slightly more efficient to use a scheme like is
8963 used for unsigned constants below, but gdb 4.x does not sign
8964 extend. Gdb 5.x does sign extend. */
8965 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8966 break;
8967
8968 case dw_val_class_unsigned_const:
8969 {
8970 int csize = constant_size (AT_unsigned (a));
8971 if (dwarf_version == 3
8972 && a->dw_attr == DW_AT_data_member_location
8973 && csize >= 4)
8974 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8975 else
8976 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8977 }
8978 break;
8979
8980 case dw_val_class_const_double:
8981 {
8982 unsigned HOST_WIDE_INT first, second;
8983
8984 if (HOST_BITS_PER_WIDE_INT >= 64)
8985 dw2_asm_output_data (1,
8986 HOST_BITS_PER_DOUBLE_INT
8987 / HOST_BITS_PER_CHAR,
8988 NULL);
8989
8990 if (WORDS_BIG_ENDIAN)
8991 {
8992 first = a->dw_attr_val.v.val_double.high;
8993 second = a->dw_attr_val.v.val_double.low;
8994 }
8995 else
8996 {
8997 first = a->dw_attr_val.v.val_double.low;
8998 second = a->dw_attr_val.v.val_double.high;
8999 }
9000
9001 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9002 first, "%s", name);
9003 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9004 second, NULL);
9005 }
9006 break;
9007
9008 case dw_val_class_wide_int:
9009 {
9010 int i;
9011 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9012 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9013 if (len * HOST_BITS_PER_WIDE_INT > 64)
9014 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9015 NULL);
9016
9017 if (WORDS_BIG_ENDIAN)
9018 for (i = len - 1; i >= 0; --i)
9019 {
9020 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9021 "%s", name);
9022 name = NULL;
9023 }
9024 else
9025 for (i = 0; i < len; ++i)
9026 {
9027 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9028 "%s", name);
9029 name = NULL;
9030 }
9031 }
9032 break;
9033
9034 case dw_val_class_vec:
9035 {
9036 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9037 unsigned int len = a->dw_attr_val.v.val_vec.length;
9038 unsigned int i;
9039 unsigned char *p;
9040
9041 dw2_asm_output_data (constant_size (len * elt_size),
9042 len * elt_size, "%s", name);
9043 if (elt_size > sizeof (HOST_WIDE_INT))
9044 {
9045 elt_size /= 2;
9046 len *= 2;
9047 }
9048 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9049 i < len;
9050 i++, p += elt_size)
9051 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9052 "fp or vector constant word %u", i);
9053 break;
9054 }
9055
9056 case dw_val_class_flag:
9057 if (dwarf_version >= 4)
9058 {
9059 /* Currently all add_AT_flag calls pass in 1 as last argument,
9060 so DW_FORM_flag_present can be used. If that ever changes,
9061 we'll need to use DW_FORM_flag and have some optimization
9062 in build_abbrev_table that will change those to
9063 DW_FORM_flag_present if it is set to 1 in all DIEs using
9064 the same abbrev entry. */
9065 gcc_assert (AT_flag (a) == 1);
9066 if (flag_debug_asm)
9067 fprintf (asm_out_file, "\t\t\t%s %s\n",
9068 ASM_COMMENT_START, name);
9069 break;
9070 }
9071 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9072 break;
9073
9074 case dw_val_class_loc_list:
9075 output_attr_index_or_value (a);
9076 break;
9077
9078 case dw_val_class_die_ref:
9079 if (AT_ref_external (a))
9080 {
9081 if (AT_ref (a)->comdat_type_p)
9082 {
9083 comdat_type_node *type_node =
9084 AT_ref (a)->die_id.die_type_node;
9085
9086 gcc_assert (type_node);
9087 output_signature (type_node->signature, name);
9088 }
9089 else
9090 {
9091 const char *sym = AT_ref (a)->die_id.die_symbol;
9092 int size;
9093
9094 gcc_assert (sym);
9095 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9096 length, whereas in DWARF3 it's always sized as an
9097 offset. */
9098 if (dwarf_version == 2)
9099 size = DWARF2_ADDR_SIZE;
9100 else
9101 size = DWARF_OFFSET_SIZE;
9102 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9103 name);
9104 }
9105 }
9106 else
9107 {
9108 gcc_assert (AT_ref (a)->die_offset);
9109 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9110 "%s", name);
9111 }
9112 break;
9113
9114 case dw_val_class_fde_ref:
9115 {
9116 char l1[20];
9117
9118 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9119 a->dw_attr_val.v.val_fde_index * 2);
9120 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9121 "%s", name);
9122 }
9123 break;
9124
9125 case dw_val_class_vms_delta:
9126 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9127 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9128 AT_vms_delta2 (a), AT_vms_delta1 (a),
9129 "%s", name);
9130 #else
9131 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9132 AT_vms_delta2 (a), AT_vms_delta1 (a),
9133 "%s", name);
9134 #endif
9135 break;
9136
9137 case dw_val_class_lbl_id:
9138 output_attr_index_or_value (a);
9139 break;
9140
9141 case dw_val_class_lineptr:
9142 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9143 debug_line_section, "%s", name);
9144 break;
9145
9146 case dw_val_class_macptr:
9147 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9148 debug_macinfo_section, "%s", name);
9149 break;
9150
9151 case dw_val_class_str:
9152 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9153 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9154 a->dw_attr_val.v.val_str->label,
9155 debug_str_section,
9156 "%s: \"%s\"", name, AT_string (a));
9157 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9158 dw2_asm_output_data_uleb128 (AT_index (a),
9159 "%s: \"%s\"", name, AT_string (a));
9160 else
9161 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9162 break;
9163
9164 case dw_val_class_file:
9165 {
9166 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9167
9168 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9169 a->dw_attr_val.v.val_file->filename);
9170 break;
9171 }
9172
9173 case dw_val_class_data8:
9174 {
9175 int i;
9176
9177 for (i = 0; i < 8; i++)
9178 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9179 i == 0 ? "%s" : NULL, name);
9180 break;
9181 }
9182
9183 case dw_val_class_high_pc:
9184 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9185 get_AT_low_pc (die), "DW_AT_high_pc");
9186 break;
9187
9188 default:
9189 gcc_unreachable ();
9190 }
9191 }
9192
9193 FOR_EACH_CHILD (die, c, output_die (c));
9194
9195 /* Add null byte to terminate sibling list. */
9196 if (die->die_child != NULL)
9197 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9198 (unsigned long) die->die_offset);
9199 }
9200
9201 /* Output the compilation unit that appears at the beginning of the
9202 .debug_info section, and precedes the DIE descriptions. */
9203
9204 static void
9205 output_compilation_unit_header (void)
9206 {
9207 /* We don't support actual DWARFv5 units yet, we just use some
9208 DWARFv5 draft DIE tags in DWARFv4 format. */
9209 int ver = dwarf_version < 5 ? dwarf_version : 4;
9210
9211 if (!XCOFF_DEBUGGING_INFO)
9212 {
9213 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9214 dw2_asm_output_data (4, 0xffffffff,
9215 "Initial length escape value indicating 64-bit DWARF extension");
9216 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9217 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9218 "Length of Compilation Unit Info");
9219 }
9220
9221 dw2_asm_output_data (2, ver, "DWARF version number");
9222 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9223 debug_abbrev_section,
9224 "Offset Into Abbrev. Section");
9225 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9226 }
9227
9228 /* Output the compilation unit DIE and its children. */
9229
9230 static void
9231 output_comp_unit (dw_die_ref die, int output_if_empty)
9232 {
9233 const char *secname, *oldsym;
9234 char *tmp;
9235
9236 /* Unless we are outputting main CU, we may throw away empty ones. */
9237 if (!output_if_empty && die->die_child == NULL)
9238 return;
9239
9240 /* Even if there are no children of this DIE, we must output the information
9241 about the compilation unit. Otherwise, on an empty translation unit, we
9242 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9243 will then complain when examining the file. First mark all the DIEs in
9244 this CU so we know which get local refs. */
9245 mark_dies (die);
9246
9247 external_ref_hash_type *extern_map = optimize_external_refs (die);
9248
9249 build_abbrev_table (die, extern_map);
9250
9251 delete extern_map;
9252
9253 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9254 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9255 calc_die_sizes (die);
9256
9257 oldsym = die->die_id.die_symbol;
9258 if (oldsym)
9259 {
9260 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9261
9262 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9263 secname = tmp;
9264 die->die_id.die_symbol = NULL;
9265 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9266 }
9267 else
9268 {
9269 switch_to_section (debug_info_section);
9270 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9271 info_section_emitted = true;
9272 }
9273
9274 /* Output debugging information. */
9275 output_compilation_unit_header ();
9276 output_die (die);
9277
9278 /* Leave the marks on the main CU, so we can check them in
9279 output_pubnames. */
9280 if (oldsym)
9281 {
9282 unmark_dies (die);
9283 die->die_id.die_symbol = oldsym;
9284 }
9285 }
9286
9287 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9288 and .debug_pubtypes. This is configured per-target, but can be
9289 overridden by the -gpubnames or -gno-pubnames options. */
9290
9291 static inline bool
9292 want_pubnames (void)
9293 {
9294 if (debug_info_level <= DINFO_LEVEL_TERSE)
9295 return false;
9296 if (debug_generate_pub_sections != -1)
9297 return debug_generate_pub_sections;
9298 return targetm.want_debug_pub_sections;
9299 }
9300
9301 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9302
9303 static void
9304 add_AT_pubnames (dw_die_ref die)
9305 {
9306 if (want_pubnames ())
9307 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9308 }
9309
9310 /* Add a string attribute value to a skeleton DIE. */
9311
9312 static inline void
9313 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9314 const char *str)
9315 {
9316 dw_attr_node attr;
9317 struct indirect_string_node *node;
9318
9319 if (! skeleton_debug_str_hash)
9320 skeleton_debug_str_hash
9321 = hash_table<indirect_string_hasher>::create_ggc (10);
9322
9323 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9324 find_string_form (node);
9325 if (node->form == DW_FORM_GNU_str_index)
9326 node->form = DW_FORM_strp;
9327
9328 attr.dw_attr = attr_kind;
9329 attr.dw_attr_val.val_class = dw_val_class_str;
9330 attr.dw_attr_val.val_entry = NULL;
9331 attr.dw_attr_val.v.val_str = node;
9332 add_dwarf_attr (die, &attr);
9333 }
9334
9335 /* Helper function to generate top-level dies for skeleton debug_info and
9336 debug_types. */
9337
9338 static void
9339 add_top_level_skeleton_die_attrs (dw_die_ref die)
9340 {
9341 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9342 const char *comp_dir = comp_dir_string ();
9343
9344 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9345 if (comp_dir != NULL)
9346 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9347 add_AT_pubnames (die);
9348 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9349 }
9350
9351 /* Output skeleton debug sections that point to the dwo file. */
9352
9353 static void
9354 output_skeleton_debug_sections (dw_die_ref comp_unit)
9355 {
9356 /* We don't support actual DWARFv5 units yet, we just use some
9357 DWARFv5 draft DIE tags in DWARFv4 format. */
9358 int ver = dwarf_version < 5 ? dwarf_version : 4;
9359
9360 /* These attributes will be found in the full debug_info section. */
9361 remove_AT (comp_unit, DW_AT_producer);
9362 remove_AT (comp_unit, DW_AT_language);
9363
9364 switch_to_section (debug_skeleton_info_section);
9365 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9366
9367 /* Produce the skeleton compilation-unit header. This one differs enough from
9368 a normal CU header that it's better not to call output_compilation_unit
9369 header. */
9370 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9371 dw2_asm_output_data (4, 0xffffffff,
9372 "Initial length escape value indicating 64-bit DWARF extension");
9373
9374 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9375 DWARF_COMPILE_UNIT_HEADER_SIZE
9376 - DWARF_INITIAL_LENGTH_SIZE
9377 + size_of_die (comp_unit),
9378 "Length of Compilation Unit Info");
9379 dw2_asm_output_data (2, ver, "DWARF version number");
9380 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9381 debug_abbrev_section,
9382 "Offset Into Abbrev. Section");
9383 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9384
9385 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9386 output_die (comp_unit);
9387
9388 /* Build the skeleton debug_abbrev section. */
9389 switch_to_section (debug_skeleton_abbrev_section);
9390 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9391
9392 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9393
9394 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9395 }
9396
9397 /* Output a comdat type unit DIE and its children. */
9398
9399 static void
9400 output_comdat_type_unit (comdat_type_node *node)
9401 {
9402 const char *secname;
9403 char *tmp;
9404 int i;
9405 #if defined (OBJECT_FORMAT_ELF)
9406 tree comdat_key;
9407 #endif
9408
9409 /* First mark all the DIEs in this CU so we know which get local refs. */
9410 mark_dies (node->root_die);
9411
9412 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9413
9414 build_abbrev_table (node->root_die, extern_map);
9415
9416 delete extern_map;
9417 extern_map = NULL;
9418
9419 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9420 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9421 calc_die_sizes (node->root_die);
9422
9423 #if defined (OBJECT_FORMAT_ELF)
9424 if (!dwarf_split_debug_info)
9425 secname = ".debug_types";
9426 else
9427 secname = ".debug_types.dwo";
9428
9429 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9430 sprintf (tmp, "wt.");
9431 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9432 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9433 comdat_key = get_identifier (tmp);
9434 targetm.asm_out.named_section (secname,
9435 SECTION_DEBUG | SECTION_LINKONCE,
9436 comdat_key);
9437 #else
9438 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9439 sprintf (tmp, ".gnu.linkonce.wt.");
9440 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9441 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9442 secname = tmp;
9443 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9444 #endif
9445
9446 /* Output debugging information. */
9447 output_compilation_unit_header ();
9448 output_signature (node->signature, "Type Signature");
9449 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9450 "Offset to Type DIE");
9451 output_die (node->root_die);
9452
9453 unmark_dies (node->root_die);
9454 }
9455
9456 /* Return the DWARF2/3 pubname associated with a decl. */
9457
9458 static const char *
9459 dwarf2_name (tree decl, int scope)
9460 {
9461 if (DECL_NAMELESS (decl))
9462 return NULL;
9463 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9464 }
9465
9466 /* Add a new entry to .debug_pubnames if appropriate. */
9467
9468 static void
9469 add_pubname_string (const char *str, dw_die_ref die)
9470 {
9471 pubname_entry e;
9472
9473 e.die = die;
9474 e.name = xstrdup (str);
9475 vec_safe_push (pubname_table, e);
9476 }
9477
9478 static void
9479 add_pubname (tree decl, dw_die_ref die)
9480 {
9481 if (!want_pubnames ())
9482 return;
9483
9484 /* Don't add items to the table when we expect that the consumer will have
9485 just read the enclosing die. For example, if the consumer is looking at a
9486 class_member, it will either be inside the class already, or will have just
9487 looked up the class to find the member. Either way, searching the class is
9488 faster than searching the index. */
9489 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9490 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9491 {
9492 const char *name = dwarf2_name (decl, 1);
9493
9494 if (name)
9495 add_pubname_string (name, die);
9496 }
9497 }
9498
9499 /* Add an enumerator to the pubnames section. */
9500
9501 static void
9502 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9503 {
9504 pubname_entry e;
9505
9506 gcc_assert (scope_name);
9507 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9508 e.die = die;
9509 vec_safe_push (pubname_table, e);
9510 }
9511
9512 /* Add a new entry to .debug_pubtypes if appropriate. */
9513
9514 static void
9515 add_pubtype (tree decl, dw_die_ref die)
9516 {
9517 pubname_entry e;
9518
9519 if (!want_pubnames ())
9520 return;
9521
9522 if ((TREE_PUBLIC (decl)
9523 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9524 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9525 {
9526 tree scope = NULL;
9527 const char *scope_name = "";
9528 const char *sep = is_cxx () ? "::" : ".";
9529 const char *name;
9530
9531 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9532 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9533 {
9534 scope_name = lang_hooks.dwarf_name (scope, 1);
9535 if (scope_name != NULL && scope_name[0] != '\0')
9536 scope_name = concat (scope_name, sep, NULL);
9537 else
9538 scope_name = "";
9539 }
9540
9541 if (TYPE_P (decl))
9542 name = type_tag (decl);
9543 else
9544 name = lang_hooks.dwarf_name (decl, 1);
9545
9546 /* If we don't have a name for the type, there's no point in adding
9547 it to the table. */
9548 if (name != NULL && name[0] != '\0')
9549 {
9550 e.die = die;
9551 e.name = concat (scope_name, name, NULL);
9552 vec_safe_push (pubtype_table, e);
9553 }
9554
9555 /* Although it might be more consistent to add the pubinfo for the
9556 enumerators as their dies are created, they should only be added if the
9557 enum type meets the criteria above. So rather than re-check the parent
9558 enum type whenever an enumerator die is created, just output them all
9559 here. This isn't protected by the name conditional because anonymous
9560 enums don't have names. */
9561 if (die->die_tag == DW_TAG_enumeration_type)
9562 {
9563 dw_die_ref c;
9564
9565 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9566 }
9567 }
9568 }
9569
9570 /* Output a single entry in the pubnames table. */
9571
9572 static void
9573 output_pubname (dw_offset die_offset, pubname_entry *entry)
9574 {
9575 dw_die_ref die = entry->die;
9576 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9577
9578 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9579
9580 if (debug_generate_pub_sections == 2)
9581 {
9582 /* This logic follows gdb's method for determining the value of the flag
9583 byte. */
9584 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9585 switch (die->die_tag)
9586 {
9587 case DW_TAG_typedef:
9588 case DW_TAG_base_type:
9589 case DW_TAG_subrange_type:
9590 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9591 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9592 break;
9593 case DW_TAG_enumerator:
9594 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9595 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9596 if (!is_cxx () && !is_java ())
9597 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9598 break;
9599 case DW_TAG_subprogram:
9600 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9601 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9602 if (!is_ada ())
9603 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9604 break;
9605 case DW_TAG_constant:
9606 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9607 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9608 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9609 break;
9610 case DW_TAG_variable:
9611 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9612 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9613 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9614 break;
9615 case DW_TAG_namespace:
9616 case DW_TAG_imported_declaration:
9617 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9618 break;
9619 case DW_TAG_class_type:
9620 case DW_TAG_interface_type:
9621 case DW_TAG_structure_type:
9622 case DW_TAG_union_type:
9623 case DW_TAG_enumeration_type:
9624 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9625 if (!is_cxx () && !is_java ())
9626 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9627 break;
9628 default:
9629 /* An unusual tag. Leave the flag-byte empty. */
9630 break;
9631 }
9632 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9633 "GDB-index flags");
9634 }
9635
9636 dw2_asm_output_nstring (entry->name, -1, "external name");
9637 }
9638
9639
9640 /* Output the public names table used to speed up access to externally
9641 visible names; or the public types table used to find type definitions. */
9642
9643 static void
9644 output_pubnames (vec<pubname_entry, va_gc> *names)
9645 {
9646 unsigned i;
9647 unsigned long pubnames_length = size_of_pubnames (names);
9648 pubname_entry *pub;
9649
9650 if (!XCOFF_DEBUGGING_INFO)
9651 {
9652 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9653 dw2_asm_output_data (4, 0xffffffff,
9654 "Initial length escape value indicating 64-bit DWARF extension");
9655 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9656 "Pub Info Length");
9657 }
9658
9659 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9660 dw2_asm_output_data (2, 2, "DWARF Version");
9661
9662 if (dwarf_split_debug_info)
9663 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9664 debug_skeleton_info_section,
9665 "Offset of Compilation Unit Info");
9666 else
9667 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9668 debug_info_section,
9669 "Offset of Compilation Unit Info");
9670 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9671 "Compilation Unit Length");
9672
9673 FOR_EACH_VEC_ELT (*names, i, pub)
9674 {
9675 if (include_pubname_in_output (names, pub))
9676 {
9677 dw_offset die_offset = pub->die->die_offset;
9678
9679 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9680 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9681 gcc_assert (pub->die->die_mark);
9682
9683 /* If we're putting types in their own .debug_types sections,
9684 the .debug_pubtypes table will still point to the compile
9685 unit (not the type unit), so we want to use the offset of
9686 the skeleton DIE (if there is one). */
9687 if (pub->die->comdat_type_p && names == pubtype_table)
9688 {
9689 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9690
9691 if (type_node != NULL)
9692 die_offset = (type_node->skeleton_die != NULL
9693 ? type_node->skeleton_die->die_offset
9694 : comp_unit_die ()->die_offset);
9695 }
9696
9697 output_pubname (die_offset, pub);
9698 }
9699 }
9700
9701 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9702 }
9703
9704 /* Output public names and types tables if necessary. */
9705
9706 static void
9707 output_pubtables (void)
9708 {
9709 if (!want_pubnames () || !info_section_emitted)
9710 return;
9711
9712 switch_to_section (debug_pubnames_section);
9713 output_pubnames (pubname_table);
9714 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9715 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9716 simply won't look for the section. */
9717 switch_to_section (debug_pubtypes_section);
9718 output_pubnames (pubtype_table);
9719 }
9720
9721
9722 /* Output the information that goes into the .debug_aranges table.
9723 Namely, define the beginning and ending address range of the
9724 text section generated for this compilation unit. */
9725
9726 static void
9727 output_aranges (void)
9728 {
9729 unsigned i;
9730 unsigned long aranges_length = size_of_aranges ();
9731
9732 if (!XCOFF_DEBUGGING_INFO)
9733 {
9734 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9735 dw2_asm_output_data (4, 0xffffffff,
9736 "Initial length escape value indicating 64-bit DWARF extension");
9737 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9738 "Length of Address Ranges Info");
9739 }
9740
9741 /* Version number for aranges is still 2, even up to DWARF5. */
9742 dw2_asm_output_data (2, 2, "DWARF Version");
9743 if (dwarf_split_debug_info)
9744 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9745 debug_skeleton_info_section,
9746 "Offset of Compilation Unit Info");
9747 else
9748 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9749 debug_info_section,
9750 "Offset of Compilation Unit Info");
9751 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9752 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9753
9754 /* We need to align to twice the pointer size here. */
9755 if (DWARF_ARANGES_PAD_SIZE)
9756 {
9757 /* Pad using a 2 byte words so that padding is correct for any
9758 pointer size. */
9759 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9760 2 * DWARF2_ADDR_SIZE);
9761 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9762 dw2_asm_output_data (2, 0, NULL);
9763 }
9764
9765 /* It is necessary not to output these entries if the sections were
9766 not used; if the sections were not used, the length will be 0 and
9767 the address may end up as 0 if the section is discarded by ld
9768 --gc-sections, leaving an invalid (0, 0) entry that can be
9769 confused with the terminator. */
9770 if (text_section_used)
9771 {
9772 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9773 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9774 text_section_label, "Length");
9775 }
9776 if (cold_text_section_used)
9777 {
9778 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9779 "Address");
9780 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9781 cold_text_section_label, "Length");
9782 }
9783
9784 if (have_multiple_function_sections)
9785 {
9786 unsigned fde_idx;
9787 dw_fde_ref fde;
9788
9789 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9790 {
9791 if (DECL_IGNORED_P (fde->decl))
9792 continue;
9793 if (!fde->in_std_section)
9794 {
9795 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9796 "Address");
9797 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9798 fde->dw_fde_begin, "Length");
9799 }
9800 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9801 {
9802 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9803 "Address");
9804 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9805 fde->dw_fde_second_begin, "Length");
9806 }
9807 }
9808 }
9809
9810 /* Output the terminator words. */
9811 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9812 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9813 }
9814
9815 /* Add a new entry to .debug_ranges. Return the offset at which it
9816 was placed. */
9817
9818 static unsigned int
9819 add_ranges_num (int num)
9820 {
9821 unsigned int in_use = ranges_table_in_use;
9822
9823 if (in_use == ranges_table_allocated)
9824 {
9825 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9826 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
9827 ranges_table_allocated);
9828 memset (ranges_table + ranges_table_in_use, 0,
9829 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
9830 }
9831
9832 ranges_table[in_use].num = num;
9833 ranges_table_in_use = in_use + 1;
9834
9835 return in_use * 2 * DWARF2_ADDR_SIZE;
9836 }
9837
9838 /* Add a new entry to .debug_ranges corresponding to a block, or a
9839 range terminator if BLOCK is NULL. */
9840
9841 static unsigned int
9842 add_ranges (const_tree block)
9843 {
9844 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9845 }
9846
9847 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9848 When using dwarf_split_debug_info, address attributes in dies destined
9849 for the final executable should be direct references--setting the
9850 parameter force_direct ensures this behavior. */
9851
9852 static void
9853 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9854 bool *added, bool force_direct)
9855 {
9856 unsigned int in_use = ranges_by_label_in_use;
9857 unsigned int offset;
9858
9859 if (in_use == ranges_by_label_allocated)
9860 {
9861 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9862 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
9863 ranges_by_label_allocated);
9864 memset (ranges_by_label + ranges_by_label_in_use, 0,
9865 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
9866 }
9867
9868 ranges_by_label[in_use].begin = begin;
9869 ranges_by_label[in_use].end = end;
9870 ranges_by_label_in_use = in_use + 1;
9871
9872 offset = add_ranges_num (-(int)in_use - 1);
9873 if (!*added)
9874 {
9875 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9876 *added = true;
9877 }
9878 }
9879
9880 static void
9881 output_ranges (void)
9882 {
9883 unsigned i;
9884 static const char *const start_fmt = "Offset %#x";
9885 const char *fmt = start_fmt;
9886
9887 for (i = 0; i < ranges_table_in_use; i++)
9888 {
9889 int block_num = ranges_table[i].num;
9890
9891 if (block_num > 0)
9892 {
9893 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9894 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9895
9896 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9897 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9898
9899 /* If all code is in the text section, then the compilation
9900 unit base address defaults to DW_AT_low_pc, which is the
9901 base of the text section. */
9902 if (!have_multiple_function_sections)
9903 {
9904 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9905 text_section_label,
9906 fmt, i * 2 * DWARF2_ADDR_SIZE);
9907 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9908 text_section_label, NULL);
9909 }
9910
9911 /* Otherwise, the compilation unit base address is zero,
9912 which allows us to use absolute addresses, and not worry
9913 about whether the target supports cross-section
9914 arithmetic. */
9915 else
9916 {
9917 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9918 fmt, i * 2 * DWARF2_ADDR_SIZE);
9919 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9920 }
9921
9922 fmt = NULL;
9923 }
9924
9925 /* Negative block_num stands for an index into ranges_by_label. */
9926 else if (block_num < 0)
9927 {
9928 int lab_idx = - block_num - 1;
9929
9930 if (!have_multiple_function_sections)
9931 {
9932 gcc_unreachable ();
9933 #if 0
9934 /* If we ever use add_ranges_by_labels () for a single
9935 function section, all we have to do is to take out
9936 the #if 0 above. */
9937 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9938 ranges_by_label[lab_idx].begin,
9939 text_section_label,
9940 fmt, i * 2 * DWARF2_ADDR_SIZE);
9941 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9942 ranges_by_label[lab_idx].end,
9943 text_section_label, NULL);
9944 #endif
9945 }
9946 else
9947 {
9948 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9949 ranges_by_label[lab_idx].begin,
9950 fmt, i * 2 * DWARF2_ADDR_SIZE);
9951 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9952 ranges_by_label[lab_idx].end,
9953 NULL);
9954 }
9955 }
9956 else
9957 {
9958 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9959 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9960 fmt = start_fmt;
9961 }
9962 }
9963 }
9964
9965 /* Data structure containing information about input files. */
9966 struct file_info
9967 {
9968 const char *path; /* Complete file name. */
9969 const char *fname; /* File name part. */
9970 int length; /* Length of entire string. */
9971 struct dwarf_file_data * file_idx; /* Index in input file table. */
9972 int dir_idx; /* Index in directory table. */
9973 };
9974
9975 /* Data structure containing information about directories with source
9976 files. */
9977 struct dir_info
9978 {
9979 const char *path; /* Path including directory name. */
9980 int length; /* Path length. */
9981 int prefix; /* Index of directory entry which is a prefix. */
9982 int count; /* Number of files in this directory. */
9983 int dir_idx; /* Index of directory used as base. */
9984 };
9985
9986 /* Callback function for file_info comparison. We sort by looking at
9987 the directories in the path. */
9988
9989 static int
9990 file_info_cmp (const void *p1, const void *p2)
9991 {
9992 const struct file_info *const s1 = (const struct file_info *) p1;
9993 const struct file_info *const s2 = (const struct file_info *) p2;
9994 const unsigned char *cp1;
9995 const unsigned char *cp2;
9996
9997 /* Take care of file names without directories. We need to make sure that
9998 we return consistent values to qsort since some will get confused if
9999 we return the same value when identical operands are passed in opposite
10000 orders. So if neither has a directory, return 0 and otherwise return
10001 1 or -1 depending on which one has the directory. */
10002 if ((s1->path == s1->fname || s2->path == s2->fname))
10003 return (s2->path == s2->fname) - (s1->path == s1->fname);
10004
10005 cp1 = (const unsigned char *) s1->path;
10006 cp2 = (const unsigned char *) s2->path;
10007
10008 while (1)
10009 {
10010 ++cp1;
10011 ++cp2;
10012 /* Reached the end of the first path? If so, handle like above. */
10013 if ((cp1 == (const unsigned char *) s1->fname)
10014 || (cp2 == (const unsigned char *) s2->fname))
10015 return ((cp2 == (const unsigned char *) s2->fname)
10016 - (cp1 == (const unsigned char *) s1->fname));
10017
10018 /* Character of current path component the same? */
10019 else if (*cp1 != *cp2)
10020 return *cp1 - *cp2;
10021 }
10022 }
10023
10024 struct file_name_acquire_data
10025 {
10026 struct file_info *files;
10027 int used_files;
10028 int max_files;
10029 };
10030
10031 /* Traversal function for the hash table. */
10032
10033 int
10034 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10035 {
10036 struct dwarf_file_data *d = *slot;
10037 struct file_info *fi;
10038 const char *f;
10039
10040 gcc_assert (fnad->max_files >= d->emitted_number);
10041
10042 if (! d->emitted_number)
10043 return 1;
10044
10045 gcc_assert (fnad->max_files != fnad->used_files);
10046
10047 fi = fnad->files + fnad->used_files++;
10048
10049 /* Skip all leading "./". */
10050 f = d->filename;
10051 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10052 f += 2;
10053
10054 /* Create a new array entry. */
10055 fi->path = f;
10056 fi->length = strlen (f);
10057 fi->file_idx = d;
10058
10059 /* Search for the file name part. */
10060 f = strrchr (f, DIR_SEPARATOR);
10061 #if defined (DIR_SEPARATOR_2)
10062 {
10063 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10064
10065 if (g != NULL)
10066 {
10067 if (f == NULL || f < g)
10068 f = g;
10069 }
10070 }
10071 #endif
10072
10073 fi->fname = f == NULL ? fi->path : f + 1;
10074 return 1;
10075 }
10076
10077 /* Output the directory table and the file name table. We try to minimize
10078 the total amount of memory needed. A heuristic is used to avoid large
10079 slowdowns with many input files. */
10080
10081 static void
10082 output_file_names (void)
10083 {
10084 struct file_name_acquire_data fnad;
10085 int numfiles;
10086 struct file_info *files;
10087 struct dir_info *dirs;
10088 int *saved;
10089 int *savehere;
10090 int *backmap;
10091 int ndirs;
10092 int idx_offset;
10093 int i;
10094
10095 if (!last_emitted_file)
10096 {
10097 dw2_asm_output_data (1, 0, "End directory table");
10098 dw2_asm_output_data (1, 0, "End file name table");
10099 return;
10100 }
10101
10102 numfiles = last_emitted_file->emitted_number;
10103
10104 /* Allocate the various arrays we need. */
10105 files = XALLOCAVEC (struct file_info, numfiles);
10106 dirs = XALLOCAVEC (struct dir_info, numfiles);
10107
10108 fnad.files = files;
10109 fnad.used_files = 0;
10110 fnad.max_files = numfiles;
10111 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10112 gcc_assert (fnad.used_files == fnad.max_files);
10113
10114 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10115
10116 /* Find all the different directories used. */
10117 dirs[0].path = files[0].path;
10118 dirs[0].length = files[0].fname - files[0].path;
10119 dirs[0].prefix = -1;
10120 dirs[0].count = 1;
10121 dirs[0].dir_idx = 0;
10122 files[0].dir_idx = 0;
10123 ndirs = 1;
10124
10125 for (i = 1; i < numfiles; i++)
10126 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10127 && memcmp (dirs[ndirs - 1].path, files[i].path,
10128 dirs[ndirs - 1].length) == 0)
10129 {
10130 /* Same directory as last entry. */
10131 files[i].dir_idx = ndirs - 1;
10132 ++dirs[ndirs - 1].count;
10133 }
10134 else
10135 {
10136 int j;
10137
10138 /* This is a new directory. */
10139 dirs[ndirs].path = files[i].path;
10140 dirs[ndirs].length = files[i].fname - files[i].path;
10141 dirs[ndirs].count = 1;
10142 dirs[ndirs].dir_idx = ndirs;
10143 files[i].dir_idx = ndirs;
10144
10145 /* Search for a prefix. */
10146 dirs[ndirs].prefix = -1;
10147 for (j = 0; j < ndirs; j++)
10148 if (dirs[j].length < dirs[ndirs].length
10149 && dirs[j].length > 1
10150 && (dirs[ndirs].prefix == -1
10151 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10152 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10153 dirs[ndirs].prefix = j;
10154
10155 ++ndirs;
10156 }
10157
10158 /* Now to the actual work. We have to find a subset of the directories which
10159 allow expressing the file name using references to the directory table
10160 with the least amount of characters. We do not do an exhaustive search
10161 where we would have to check out every combination of every single
10162 possible prefix. Instead we use a heuristic which provides nearly optimal
10163 results in most cases and never is much off. */
10164 saved = XALLOCAVEC (int, ndirs);
10165 savehere = XALLOCAVEC (int, ndirs);
10166
10167 memset (saved, '\0', ndirs * sizeof (saved[0]));
10168 for (i = 0; i < ndirs; i++)
10169 {
10170 int j;
10171 int total;
10172
10173 /* We can always save some space for the current directory. But this
10174 does not mean it will be enough to justify adding the directory. */
10175 savehere[i] = dirs[i].length;
10176 total = (savehere[i] - saved[i]) * dirs[i].count;
10177
10178 for (j = i + 1; j < ndirs; j++)
10179 {
10180 savehere[j] = 0;
10181 if (saved[j] < dirs[i].length)
10182 {
10183 /* Determine whether the dirs[i] path is a prefix of the
10184 dirs[j] path. */
10185 int k;
10186
10187 k = dirs[j].prefix;
10188 while (k != -1 && k != (int) i)
10189 k = dirs[k].prefix;
10190
10191 if (k == (int) i)
10192 {
10193 /* Yes it is. We can possibly save some memory by
10194 writing the filenames in dirs[j] relative to
10195 dirs[i]. */
10196 savehere[j] = dirs[i].length;
10197 total += (savehere[j] - saved[j]) * dirs[j].count;
10198 }
10199 }
10200 }
10201
10202 /* Check whether we can save enough to justify adding the dirs[i]
10203 directory. */
10204 if (total > dirs[i].length + 1)
10205 {
10206 /* It's worthwhile adding. */
10207 for (j = i; j < ndirs; j++)
10208 if (savehere[j] > 0)
10209 {
10210 /* Remember how much we saved for this directory so far. */
10211 saved[j] = savehere[j];
10212
10213 /* Remember the prefix directory. */
10214 dirs[j].dir_idx = i;
10215 }
10216 }
10217 }
10218
10219 /* Emit the directory name table. */
10220 idx_offset = dirs[0].length > 0 ? 1 : 0;
10221 for (i = 1 - idx_offset; i < ndirs; i++)
10222 dw2_asm_output_nstring (dirs[i].path,
10223 dirs[i].length
10224 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10225 "Directory Entry: %#x", i + idx_offset);
10226
10227 dw2_asm_output_data (1, 0, "End directory table");
10228
10229 /* We have to emit them in the order of emitted_number since that's
10230 used in the debug info generation. To do this efficiently we
10231 generate a back-mapping of the indices first. */
10232 backmap = XALLOCAVEC (int, numfiles);
10233 for (i = 0; i < numfiles; i++)
10234 backmap[files[i].file_idx->emitted_number - 1] = i;
10235
10236 /* Now write all the file names. */
10237 for (i = 0; i < numfiles; i++)
10238 {
10239 int file_idx = backmap[i];
10240 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10241
10242 #ifdef VMS_DEBUGGING_INFO
10243 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10244
10245 /* Setting these fields can lead to debugger miscomparisons,
10246 but VMS Debug requires them to be set correctly. */
10247
10248 int ver;
10249 long long cdt;
10250 long siz;
10251 int maxfilelen = strlen (files[file_idx].path)
10252 + dirs[dir_idx].length
10253 + MAX_VMS_VERSION_LEN + 1;
10254 char *filebuf = XALLOCAVEC (char, maxfilelen);
10255
10256 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10257 snprintf (filebuf, maxfilelen, "%s;%d",
10258 files[file_idx].path + dirs[dir_idx].length, ver);
10259
10260 dw2_asm_output_nstring
10261 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10262
10263 /* Include directory index. */
10264 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10265
10266 /* Modification time. */
10267 dw2_asm_output_data_uleb128
10268 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10269 ? cdt : 0,
10270 NULL);
10271
10272 /* File length in bytes. */
10273 dw2_asm_output_data_uleb128
10274 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10275 ? siz : 0,
10276 NULL);
10277 #else
10278 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10279 "File Entry: %#x", (unsigned) i + 1);
10280
10281 /* Include directory index. */
10282 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10283
10284 /* Modification time. */
10285 dw2_asm_output_data_uleb128 (0, NULL);
10286
10287 /* File length in bytes. */
10288 dw2_asm_output_data_uleb128 (0, NULL);
10289 #endif /* VMS_DEBUGGING_INFO */
10290 }
10291
10292 dw2_asm_output_data (1, 0, "End file name table");
10293 }
10294
10295
10296 /* Output one line number table into the .debug_line section. */
10297
10298 static void
10299 output_one_line_info_table (dw_line_info_table *table)
10300 {
10301 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10302 unsigned int current_line = 1;
10303 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10304 dw_line_info_entry *ent;
10305 size_t i;
10306
10307 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10308 {
10309 switch (ent->opcode)
10310 {
10311 case LI_set_address:
10312 /* ??? Unfortunately, we have little choice here currently, and
10313 must always use the most general form. GCC does not know the
10314 address delta itself, so we can't use DW_LNS_advance_pc. Many
10315 ports do have length attributes which will give an upper bound
10316 on the address range. We could perhaps use length attributes
10317 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10318 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10319
10320 /* This can handle any delta. This takes
10321 4+DWARF2_ADDR_SIZE bytes. */
10322 dw2_asm_output_data (1, 0, "set address %s", line_label);
10323 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10324 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10325 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10326 break;
10327
10328 case LI_set_line:
10329 if (ent->val == current_line)
10330 {
10331 /* We still need to start a new row, so output a copy insn. */
10332 dw2_asm_output_data (1, DW_LNS_copy,
10333 "copy line %u", current_line);
10334 }
10335 else
10336 {
10337 int line_offset = ent->val - current_line;
10338 int line_delta = line_offset - DWARF_LINE_BASE;
10339
10340 current_line = ent->val;
10341 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10342 {
10343 /* This can handle deltas from -10 to 234, using the current
10344 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10345 This takes 1 byte. */
10346 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10347 "line %u", current_line);
10348 }
10349 else
10350 {
10351 /* This can handle any delta. This takes at least 4 bytes,
10352 depending on the value being encoded. */
10353 dw2_asm_output_data (1, DW_LNS_advance_line,
10354 "advance to line %u", current_line);
10355 dw2_asm_output_data_sleb128 (line_offset, NULL);
10356 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10357 }
10358 }
10359 break;
10360
10361 case LI_set_file:
10362 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10363 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10364 break;
10365
10366 case LI_set_column:
10367 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10368 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10369 break;
10370
10371 case LI_negate_stmt:
10372 current_is_stmt = !current_is_stmt;
10373 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10374 "is_stmt %d", current_is_stmt);
10375 break;
10376
10377 case LI_set_prologue_end:
10378 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10379 "set prologue end");
10380 break;
10381
10382 case LI_set_epilogue_begin:
10383 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10384 "set epilogue begin");
10385 break;
10386
10387 case LI_set_discriminator:
10388 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10389 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10390 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10391 dw2_asm_output_data_uleb128 (ent->val, NULL);
10392 break;
10393 }
10394 }
10395
10396 /* Emit debug info for the address of the end of the table. */
10397 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10398 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10399 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10400 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10401
10402 dw2_asm_output_data (1, 0, "end sequence");
10403 dw2_asm_output_data_uleb128 (1, NULL);
10404 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10405 }
10406
10407 /* Output the source line number correspondence information. This
10408 information goes into the .debug_line section. */
10409
10410 static void
10411 output_line_info (bool prologue_only)
10412 {
10413 char l1[20], l2[20], p1[20], p2[20];
10414 /* We don't support DWARFv5 line tables yet. */
10415 int ver = dwarf_version < 5 ? dwarf_version : 4;
10416 bool saw_one = false;
10417 int opc;
10418
10419 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10420 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10421 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10422 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10423
10424 if (!XCOFF_DEBUGGING_INFO)
10425 {
10426 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10427 dw2_asm_output_data (4, 0xffffffff,
10428 "Initial length escape value indicating 64-bit DWARF extension");
10429 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10430 "Length of Source Line Info");
10431 }
10432
10433 ASM_OUTPUT_LABEL (asm_out_file, l1);
10434
10435 dw2_asm_output_data (2, ver, "DWARF Version");
10436 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10437 ASM_OUTPUT_LABEL (asm_out_file, p1);
10438
10439 /* Define the architecture-dependent minimum instruction length (in bytes).
10440 In this implementation of DWARF, this field is used for information
10441 purposes only. Since GCC generates assembly language, we have no
10442 a priori knowledge of how many instruction bytes are generated for each
10443 source line, and therefore can use only the DW_LNE_set_address and
10444 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10445 this as '1', which is "correct enough" for all architectures,
10446 and don't let the target override. */
10447 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10448
10449 if (ver >= 4)
10450 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10451 "Maximum Operations Per Instruction");
10452 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10453 "Default is_stmt_start flag");
10454 dw2_asm_output_data (1, DWARF_LINE_BASE,
10455 "Line Base Value (Special Opcodes)");
10456 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10457 "Line Range Value (Special Opcodes)");
10458 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10459 "Special Opcode Base");
10460
10461 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10462 {
10463 int n_op_args;
10464 switch (opc)
10465 {
10466 case DW_LNS_advance_pc:
10467 case DW_LNS_advance_line:
10468 case DW_LNS_set_file:
10469 case DW_LNS_set_column:
10470 case DW_LNS_fixed_advance_pc:
10471 case DW_LNS_set_isa:
10472 n_op_args = 1;
10473 break;
10474 default:
10475 n_op_args = 0;
10476 break;
10477 }
10478
10479 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10480 opc, n_op_args);
10481 }
10482
10483 /* Write out the information about the files we use. */
10484 output_file_names ();
10485 ASM_OUTPUT_LABEL (asm_out_file, p2);
10486 if (prologue_only)
10487 {
10488 /* Output the marker for the end of the line number info. */
10489 ASM_OUTPUT_LABEL (asm_out_file, l2);
10490 return;
10491 }
10492
10493 if (separate_line_info)
10494 {
10495 dw_line_info_table *table;
10496 size_t i;
10497
10498 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10499 if (table->in_use)
10500 {
10501 output_one_line_info_table (table);
10502 saw_one = true;
10503 }
10504 }
10505 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10506 {
10507 output_one_line_info_table (cold_text_section_line_info);
10508 saw_one = true;
10509 }
10510
10511 /* ??? Some Darwin linkers crash on a .debug_line section with no
10512 sequences. Further, merely a DW_LNE_end_sequence entry is not
10513 sufficient -- the address column must also be initialized.
10514 Make sure to output at least one set_address/end_sequence pair,
10515 choosing .text since that section is always present. */
10516 if (text_section_line_info->in_use || !saw_one)
10517 output_one_line_info_table (text_section_line_info);
10518
10519 /* Output the marker for the end of the line number info. */
10520 ASM_OUTPUT_LABEL (asm_out_file, l2);
10521 }
10522 \f
10523 /* Given a pointer to a tree node for some base type, return a pointer to
10524 a DIE that describes the given type.
10525
10526 This routine must only be called for GCC type nodes that correspond to
10527 Dwarf base (fundamental) types. */
10528
10529 static dw_die_ref
10530 base_type_die (tree type)
10531 {
10532 dw_die_ref base_type_result;
10533 enum dwarf_type encoding;
10534
10535 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10536 return 0;
10537
10538 /* If this is a subtype that should not be emitted as a subrange type,
10539 use the base type. See subrange_type_for_debug_p. */
10540 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10541 type = TREE_TYPE (type);
10542
10543 switch (TREE_CODE (type))
10544 {
10545 case INTEGER_TYPE:
10546 if ((dwarf_version >= 4 || !dwarf_strict)
10547 && TYPE_NAME (type)
10548 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10549 && DECL_IS_BUILTIN (TYPE_NAME (type))
10550 && DECL_NAME (TYPE_NAME (type)))
10551 {
10552 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10553 if (strcmp (name, "char16_t") == 0
10554 || strcmp (name, "char32_t") == 0)
10555 {
10556 encoding = DW_ATE_UTF;
10557 break;
10558 }
10559 }
10560 if (TYPE_STRING_FLAG (type))
10561 {
10562 if (TYPE_UNSIGNED (type))
10563 encoding = DW_ATE_unsigned_char;
10564 else
10565 encoding = DW_ATE_signed_char;
10566 }
10567 else if (TYPE_UNSIGNED (type))
10568 encoding = DW_ATE_unsigned;
10569 else
10570 encoding = DW_ATE_signed;
10571 break;
10572
10573 case REAL_TYPE:
10574 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10575 {
10576 if (dwarf_version >= 3 || !dwarf_strict)
10577 encoding = DW_ATE_decimal_float;
10578 else
10579 encoding = DW_ATE_lo_user;
10580 }
10581 else
10582 encoding = DW_ATE_float;
10583 break;
10584
10585 case FIXED_POINT_TYPE:
10586 if (!(dwarf_version >= 3 || !dwarf_strict))
10587 encoding = DW_ATE_lo_user;
10588 else if (TYPE_UNSIGNED (type))
10589 encoding = DW_ATE_unsigned_fixed;
10590 else
10591 encoding = DW_ATE_signed_fixed;
10592 break;
10593
10594 /* Dwarf2 doesn't know anything about complex ints, so use
10595 a user defined type for it. */
10596 case COMPLEX_TYPE:
10597 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10598 encoding = DW_ATE_complex_float;
10599 else
10600 encoding = DW_ATE_lo_user;
10601 break;
10602
10603 case BOOLEAN_TYPE:
10604 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10605 encoding = DW_ATE_boolean;
10606 break;
10607
10608 default:
10609 /* No other TREE_CODEs are Dwarf fundamental types. */
10610 gcc_unreachable ();
10611 }
10612
10613 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10614
10615 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10616 int_size_in_bytes (type));
10617 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10618 add_pubtype (type, base_type_result);
10619
10620 return base_type_result;
10621 }
10622
10623 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10624 named 'auto' in its type: return true for it, false otherwise. */
10625
10626 static inline bool
10627 is_cxx_auto (tree type)
10628 {
10629 if (is_cxx ())
10630 {
10631 tree name = TYPE_IDENTIFIER (type);
10632 if (name == get_identifier ("auto")
10633 || name == get_identifier ("decltype(auto)"))
10634 return true;
10635 }
10636 return false;
10637 }
10638
10639 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10640 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10641
10642 static inline int
10643 is_base_type (tree type)
10644 {
10645 switch (TREE_CODE (type))
10646 {
10647 case ERROR_MARK:
10648 case VOID_TYPE:
10649 case INTEGER_TYPE:
10650 case REAL_TYPE:
10651 case FIXED_POINT_TYPE:
10652 case COMPLEX_TYPE:
10653 case BOOLEAN_TYPE:
10654 case POINTER_BOUNDS_TYPE:
10655 return 1;
10656
10657 case ARRAY_TYPE:
10658 case RECORD_TYPE:
10659 case UNION_TYPE:
10660 case QUAL_UNION_TYPE:
10661 case ENUMERAL_TYPE:
10662 case FUNCTION_TYPE:
10663 case METHOD_TYPE:
10664 case POINTER_TYPE:
10665 case REFERENCE_TYPE:
10666 case NULLPTR_TYPE:
10667 case OFFSET_TYPE:
10668 case LANG_TYPE:
10669 case VECTOR_TYPE:
10670 return 0;
10671
10672 default:
10673 if (is_cxx_auto (type))
10674 return 0;
10675 gcc_unreachable ();
10676 }
10677
10678 return 0;
10679 }
10680
10681 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10682 node, return the size in bits for the type if it is a constant, or else
10683 return the alignment for the type if the type's size is not constant, or
10684 else return BITS_PER_WORD if the type actually turns out to be an
10685 ERROR_MARK node. */
10686
10687 static inline unsigned HOST_WIDE_INT
10688 simple_type_size_in_bits (const_tree type)
10689 {
10690 if (TREE_CODE (type) == ERROR_MARK)
10691 return BITS_PER_WORD;
10692 else if (TYPE_SIZE (type) == NULL_TREE)
10693 return 0;
10694 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10695 return tree_to_uhwi (TYPE_SIZE (type));
10696 else
10697 return TYPE_ALIGN (type);
10698 }
10699
10700 /* Similarly, but return an offset_int instead of UHWI. */
10701
10702 static inline offset_int
10703 offset_int_type_size_in_bits (const_tree type)
10704 {
10705 if (TREE_CODE (type) == ERROR_MARK)
10706 return BITS_PER_WORD;
10707 else if (TYPE_SIZE (type) == NULL_TREE)
10708 return 0;
10709 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10710 return wi::to_offset (TYPE_SIZE (type));
10711 else
10712 return TYPE_ALIGN (type);
10713 }
10714
10715 /* Given a pointer to a tree node for a subrange type, return a pointer
10716 to a DIE that describes the given type. */
10717
10718 static dw_die_ref
10719 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10720 {
10721 dw_die_ref subrange_die;
10722 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10723
10724 if (context_die == NULL)
10725 context_die = comp_unit_die ();
10726
10727 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10728
10729 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10730 {
10731 /* The size of the subrange type and its base type do not match,
10732 so we need to generate a size attribute for the subrange type. */
10733 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10734 }
10735
10736 if (low)
10737 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10738 if (high)
10739 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10740
10741 return subrange_die;
10742 }
10743
10744 /* Returns the (const and/or volatile) cv_qualifiers associated with
10745 the decl node. This will normally be augmented with the
10746 cv_qualifiers of the underlying type in add_type_attribute. */
10747
10748 static int
10749 decl_quals (const_tree decl)
10750 {
10751 return ((TREE_READONLY (decl)
10752 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10753 | (TREE_THIS_VOLATILE (decl)
10754 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10755 }
10756
10757 /* Determine the TYPE whose qualifiers match the largest strict subset
10758 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10759 qualifiers outside QUAL_MASK. */
10760
10761 static int
10762 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10763 {
10764 tree t;
10765 int best_rank = 0, best_qual = 0, max_rank;
10766
10767 type_quals &= qual_mask;
10768 max_rank = popcount_hwi (type_quals) - 1;
10769
10770 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10771 t = TYPE_NEXT_VARIANT (t))
10772 {
10773 int q = TYPE_QUALS (t) & qual_mask;
10774
10775 if ((q & type_quals) == q && q != type_quals
10776 && check_base_type (t, type))
10777 {
10778 int rank = popcount_hwi (q);
10779
10780 if (rank > best_rank)
10781 {
10782 best_rank = rank;
10783 best_qual = q;
10784 }
10785 }
10786 }
10787
10788 return best_qual;
10789 }
10790
10791 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10792 entry that chains various modifiers in front of the given type. */
10793
10794 static dw_die_ref
10795 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10796 {
10797 enum tree_code code = TREE_CODE (type);
10798 dw_die_ref mod_type_die;
10799 dw_die_ref sub_die = NULL;
10800 tree item_type = NULL;
10801 tree qualified_type;
10802 tree name, low, high;
10803 dw_die_ref mod_scope;
10804 /* Only these cv-qualifiers are currently handled. */
10805 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10806 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10807
10808 if (code == ERROR_MARK)
10809 return NULL;
10810
10811 cv_quals &= cv_qual_mask;
10812
10813 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10814 tag modifier (and not an attribute) old consumers won't be able
10815 to handle it. */
10816 if (dwarf_version < 3)
10817 cv_quals &= ~TYPE_QUAL_RESTRICT;
10818
10819 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10820 if (dwarf_version < 5)
10821 cv_quals &= ~TYPE_QUAL_ATOMIC;
10822
10823 /* See if we already have the appropriately qualified variant of
10824 this type. */
10825 qualified_type = get_qualified_type (type, cv_quals);
10826
10827 if (qualified_type == sizetype
10828 && TYPE_NAME (qualified_type)
10829 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10830 {
10831 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10832
10833 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10834 && TYPE_PRECISION (t)
10835 == TYPE_PRECISION (qualified_type)
10836 && TYPE_UNSIGNED (t)
10837 == TYPE_UNSIGNED (qualified_type));
10838 qualified_type = t;
10839 }
10840
10841 /* If we do, then we can just use its DIE, if it exists. */
10842 if (qualified_type)
10843 {
10844 mod_type_die = lookup_type_die (qualified_type);
10845 if (mod_type_die)
10846 return mod_type_die;
10847 }
10848
10849 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10850
10851 /* Handle C typedef types. */
10852 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10853 && !DECL_ARTIFICIAL (name))
10854 {
10855 tree dtype = TREE_TYPE (name);
10856
10857 if (qualified_type == dtype)
10858 {
10859 /* For a named type, use the typedef. */
10860 gen_type_die (qualified_type, context_die);
10861 return lookup_type_die (qualified_type);
10862 }
10863 else
10864 {
10865 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10866 dquals &= cv_qual_mask;
10867 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10868 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10869 /* cv-unqualified version of named type. Just use
10870 the unnamed type to which it refers. */
10871 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10872 cv_quals, context_die);
10873 /* Else cv-qualified version of named type; fall through. */
10874 }
10875 }
10876
10877 mod_scope = scope_die_for (type, context_die);
10878
10879 if (cv_quals)
10880 {
10881 struct qual_info { int q; enum dwarf_tag t; };
10882 static const struct qual_info qual_info[] =
10883 {
10884 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10885 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10886 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10887 { TYPE_QUAL_CONST, DW_TAG_const_type },
10888 };
10889 int sub_quals;
10890 unsigned i;
10891
10892 /* Determine a lesser qualified type that most closely matches
10893 this one. Then generate DW_TAG_* entries for the remaining
10894 qualifiers. */
10895 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10896 cv_qual_mask);
10897 mod_type_die = modified_type_die (type, sub_quals, context_die);
10898
10899 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10900 if (qual_info[i].q & cv_quals & ~sub_quals)
10901 {
10902 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10903 if (mod_type_die)
10904 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10905 mod_type_die = d;
10906 }
10907 }
10908 else if (code == POINTER_TYPE)
10909 {
10910 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10911 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10912 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10913 item_type = TREE_TYPE (type);
10914 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10915 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10916 TYPE_ADDR_SPACE (item_type));
10917 }
10918 else if (code == REFERENCE_TYPE)
10919 {
10920 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10921 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10922 type);
10923 else
10924 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10925 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10926 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10927 item_type = TREE_TYPE (type);
10928 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10929 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10930 TYPE_ADDR_SPACE (item_type));
10931 }
10932 else if (code == INTEGER_TYPE
10933 && TREE_TYPE (type) != NULL_TREE
10934 && subrange_type_for_debug_p (type, &low, &high))
10935 {
10936 mod_type_die = subrange_type_die (type, low, high, context_die);
10937 item_type = TREE_TYPE (type);
10938 }
10939 else if (is_base_type (type))
10940 mod_type_die = base_type_die (type);
10941 else
10942 {
10943 gen_type_die (type, context_die);
10944
10945 /* We have to get the type_main_variant here (and pass that to the
10946 `lookup_type_die' routine) because the ..._TYPE node we have
10947 might simply be a *copy* of some original type node (where the
10948 copy was created to help us keep track of typedef names) and
10949 that copy might have a different TYPE_UID from the original
10950 ..._TYPE node. */
10951 if (TREE_CODE (type) != VECTOR_TYPE)
10952 return lookup_type_die (type_main_variant (type));
10953 else
10954 /* Vectors have the debugging information in the type,
10955 not the main variant. */
10956 return lookup_type_die (type);
10957 }
10958
10959 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10960 don't output a DW_TAG_typedef, since there isn't one in the
10961 user's program; just attach a DW_AT_name to the type.
10962 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10963 if the base type already has the same name. */
10964 if (name
10965 && ((TREE_CODE (name) != TYPE_DECL
10966 && (qualified_type == TYPE_MAIN_VARIANT (type)
10967 || (cv_quals == TYPE_UNQUALIFIED)))
10968 || (TREE_CODE (name) == TYPE_DECL
10969 && TREE_TYPE (name) == qualified_type
10970 && DECL_NAME (name))))
10971 {
10972 if (TREE_CODE (name) == TYPE_DECL)
10973 /* Could just call add_name_and_src_coords_attributes here,
10974 but since this is a builtin type it doesn't have any
10975 useful source coordinates anyway. */
10976 name = DECL_NAME (name);
10977 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10978 }
10979 /* This probably indicates a bug. */
10980 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10981 {
10982 name = TYPE_IDENTIFIER (type);
10983 add_name_attribute (mod_type_die,
10984 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10985 }
10986
10987 if (qualified_type)
10988 equate_type_number_to_die (qualified_type, mod_type_die);
10989
10990 if (item_type)
10991 /* We must do this after the equate_type_number_to_die call, in case
10992 this is a recursive type. This ensures that the modified_type_die
10993 recursion will terminate even if the type is recursive. Recursive
10994 types are possible in Ada. */
10995 sub_die = modified_type_die (item_type,
10996 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10997 context_die);
10998
10999 if (sub_die != NULL)
11000 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11001
11002 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11003 if (TYPE_ARTIFICIAL (type))
11004 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11005
11006 return mod_type_die;
11007 }
11008
11009 /* Generate DIEs for the generic parameters of T.
11010 T must be either a generic type or a generic function.
11011 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11012
11013 static void
11014 gen_generic_params_dies (tree t)
11015 {
11016 tree parms, args;
11017 int parms_num, i;
11018 dw_die_ref die = NULL;
11019 int non_default;
11020
11021 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11022 return;
11023
11024 if (TYPE_P (t))
11025 die = lookup_type_die (t);
11026 else if (DECL_P (t))
11027 die = lookup_decl_die (t);
11028
11029 gcc_assert (die);
11030
11031 parms = lang_hooks.get_innermost_generic_parms (t);
11032 if (!parms)
11033 /* T has no generic parameter. It means T is neither a generic type
11034 or function. End of story. */
11035 return;
11036
11037 parms_num = TREE_VEC_LENGTH (parms);
11038 args = lang_hooks.get_innermost_generic_args (t);
11039 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11040 non_default = int_cst_value (TREE_CHAIN (args));
11041 else
11042 non_default = TREE_VEC_LENGTH (args);
11043 for (i = 0; i < parms_num; i++)
11044 {
11045 tree parm, arg, arg_pack_elems;
11046 dw_die_ref parm_die;
11047
11048 parm = TREE_VEC_ELT (parms, i);
11049 arg = TREE_VEC_ELT (args, i);
11050 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11051 gcc_assert (parm && TREE_VALUE (parm) && arg);
11052
11053 if (parm && TREE_VALUE (parm) && arg)
11054 {
11055 /* If PARM represents a template parameter pack,
11056 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11057 by DW_TAG_template_*_parameter DIEs for the argument
11058 pack elements of ARG. Note that ARG would then be
11059 an argument pack. */
11060 if (arg_pack_elems)
11061 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11062 arg_pack_elems,
11063 die);
11064 else
11065 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11066 true /* emit name */, die);
11067 if (i >= non_default)
11068 add_AT_flag (parm_die, DW_AT_default_value, 1);
11069 }
11070 }
11071 }
11072
11073 /* Create and return a DIE for PARM which should be
11074 the representation of a generic type parameter.
11075 For instance, in the C++ front end, PARM would be a template parameter.
11076 ARG is the argument to PARM.
11077 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11078 name of the PARM.
11079 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11080 as a child node. */
11081
11082 static dw_die_ref
11083 generic_parameter_die (tree parm, tree arg,
11084 bool emit_name_p,
11085 dw_die_ref parent_die)
11086 {
11087 dw_die_ref tmpl_die = NULL;
11088 const char *name = NULL;
11089
11090 if (!parm || !DECL_NAME (parm) || !arg)
11091 return NULL;
11092
11093 /* We support non-type generic parameters and arguments,
11094 type generic parameters and arguments, as well as
11095 generic generic parameters (a.k.a. template template parameters in C++)
11096 and arguments. */
11097 if (TREE_CODE (parm) == PARM_DECL)
11098 /* PARM is a nontype generic parameter */
11099 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11100 else if (TREE_CODE (parm) == TYPE_DECL)
11101 /* PARM is a type generic parameter. */
11102 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11103 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11104 /* PARM is a generic generic parameter.
11105 Its DIE is a GNU extension. It shall have a
11106 DW_AT_name attribute to represent the name of the template template
11107 parameter, and a DW_AT_GNU_template_name attribute to represent the
11108 name of the template template argument. */
11109 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11110 parent_die, parm);
11111 else
11112 gcc_unreachable ();
11113
11114 if (tmpl_die)
11115 {
11116 tree tmpl_type;
11117
11118 /* If PARM is a generic parameter pack, it means we are
11119 emitting debug info for a template argument pack element.
11120 In other terms, ARG is a template argument pack element.
11121 In that case, we don't emit any DW_AT_name attribute for
11122 the die. */
11123 if (emit_name_p)
11124 {
11125 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11126 gcc_assert (name);
11127 add_AT_string (tmpl_die, DW_AT_name, name);
11128 }
11129
11130 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11131 {
11132 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11133 TMPL_DIE should have a child DW_AT_type attribute that is set
11134 to the type of the argument to PARM, which is ARG.
11135 If PARM is a type generic parameter, TMPL_DIE should have a
11136 child DW_AT_type that is set to ARG. */
11137 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11138 add_type_attribute (tmpl_die, tmpl_type,
11139 (TREE_THIS_VOLATILE (tmpl_type)
11140 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11141 parent_die);
11142 }
11143 else
11144 {
11145 /* So TMPL_DIE is a DIE representing a
11146 a generic generic template parameter, a.k.a template template
11147 parameter in C++ and arg is a template. */
11148
11149 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11150 to the name of the argument. */
11151 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11152 if (name)
11153 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11154 }
11155
11156 if (TREE_CODE (parm) == PARM_DECL)
11157 /* So PARM is a non-type generic parameter.
11158 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11159 attribute of TMPL_DIE which value represents the value
11160 of ARG.
11161 We must be careful here:
11162 The value of ARG might reference some function decls.
11163 We might currently be emitting debug info for a generic
11164 type and types are emitted before function decls, we don't
11165 know if the function decls referenced by ARG will actually be
11166 emitted after cgraph computations.
11167 So must defer the generation of the DW_AT_const_value to
11168 after cgraph is ready. */
11169 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11170 }
11171
11172 return tmpl_die;
11173 }
11174
11175 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11176 PARM_PACK must be a template parameter pack. The returned DIE
11177 will be child DIE of PARENT_DIE. */
11178
11179 static dw_die_ref
11180 template_parameter_pack_die (tree parm_pack,
11181 tree parm_pack_args,
11182 dw_die_ref parent_die)
11183 {
11184 dw_die_ref die;
11185 int j;
11186
11187 gcc_assert (parent_die && parm_pack);
11188
11189 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11190 add_name_and_src_coords_attributes (die, parm_pack);
11191 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11192 generic_parameter_die (parm_pack,
11193 TREE_VEC_ELT (parm_pack_args, j),
11194 false /* Don't emit DW_AT_name */,
11195 die);
11196 return die;
11197 }
11198
11199 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11200 an enumerated type. */
11201
11202 static inline int
11203 type_is_enum (const_tree type)
11204 {
11205 return TREE_CODE (type) == ENUMERAL_TYPE;
11206 }
11207
11208 /* Return the DBX register number described by a given RTL node. */
11209
11210 static unsigned int
11211 dbx_reg_number (const_rtx rtl)
11212 {
11213 unsigned regno = REGNO (rtl);
11214
11215 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11216
11217 #ifdef LEAF_REG_REMAP
11218 if (crtl->uses_only_leaf_regs)
11219 {
11220 int leaf_reg = LEAF_REG_REMAP (regno);
11221 if (leaf_reg != -1)
11222 regno = (unsigned) leaf_reg;
11223 }
11224 #endif
11225
11226 regno = DBX_REGISTER_NUMBER (regno);
11227 gcc_assert (regno != INVALID_REGNUM);
11228 return regno;
11229 }
11230
11231 /* Optionally add a DW_OP_piece term to a location description expression.
11232 DW_OP_piece is only added if the location description expression already
11233 doesn't end with DW_OP_piece. */
11234
11235 static void
11236 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11237 {
11238 dw_loc_descr_ref loc;
11239
11240 if (*list_head != NULL)
11241 {
11242 /* Find the end of the chain. */
11243 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11244 ;
11245
11246 if (loc->dw_loc_opc != DW_OP_piece)
11247 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11248 }
11249 }
11250
11251 /* Return a location descriptor that designates a machine register or
11252 zero if there is none. */
11253
11254 static dw_loc_descr_ref
11255 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11256 {
11257 rtx regs;
11258
11259 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11260 return 0;
11261
11262 /* We only use "frame base" when we're sure we're talking about the
11263 post-prologue local stack frame. We do this by *not* running
11264 register elimination until this point, and recognizing the special
11265 argument pointer and soft frame pointer rtx's.
11266 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11267 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11268 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11269 {
11270 dw_loc_descr_ref result = NULL;
11271
11272 if (dwarf_version >= 4 || !dwarf_strict)
11273 {
11274 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11275 initialized);
11276 if (result)
11277 add_loc_descr (&result,
11278 new_loc_descr (DW_OP_stack_value, 0, 0));
11279 }
11280 return result;
11281 }
11282
11283 regs = targetm.dwarf_register_span (rtl);
11284
11285 if (REG_NREGS (rtl) > 1 || regs)
11286 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11287 else
11288 {
11289 unsigned int dbx_regnum = dbx_reg_number (rtl);
11290 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11291 return 0;
11292 return one_reg_loc_descriptor (dbx_regnum, initialized);
11293 }
11294 }
11295
11296 /* Return a location descriptor that designates a machine register for
11297 a given hard register number. */
11298
11299 static dw_loc_descr_ref
11300 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11301 {
11302 dw_loc_descr_ref reg_loc_descr;
11303
11304 if (regno <= 31)
11305 reg_loc_descr
11306 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11307 else
11308 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11309
11310 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11311 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11312
11313 return reg_loc_descr;
11314 }
11315
11316 /* Given an RTL of a register, return a location descriptor that
11317 designates a value that spans more than one register. */
11318
11319 static dw_loc_descr_ref
11320 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11321 enum var_init_status initialized)
11322 {
11323 int size, i;
11324 dw_loc_descr_ref loc_result = NULL;
11325
11326 /* Simple, contiguous registers. */
11327 if (regs == NULL_RTX)
11328 {
11329 unsigned reg = REGNO (rtl);
11330 int nregs;
11331
11332 #ifdef LEAF_REG_REMAP
11333 if (crtl->uses_only_leaf_regs)
11334 {
11335 int leaf_reg = LEAF_REG_REMAP (reg);
11336 if (leaf_reg != -1)
11337 reg = (unsigned) leaf_reg;
11338 }
11339 #endif
11340
11341 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11342 nregs = REG_NREGS (rtl);
11343
11344 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11345
11346 loc_result = NULL;
11347 while (nregs--)
11348 {
11349 dw_loc_descr_ref t;
11350
11351 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11352 VAR_INIT_STATUS_INITIALIZED);
11353 add_loc_descr (&loc_result, t);
11354 add_loc_descr_op_piece (&loc_result, size);
11355 ++reg;
11356 }
11357 return loc_result;
11358 }
11359
11360 /* Now onto stupid register sets in non contiguous locations. */
11361
11362 gcc_assert (GET_CODE (regs) == PARALLEL);
11363
11364 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11365 loc_result = NULL;
11366
11367 for (i = 0; i < XVECLEN (regs, 0); ++i)
11368 {
11369 dw_loc_descr_ref t;
11370
11371 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11372 VAR_INIT_STATUS_INITIALIZED);
11373 add_loc_descr (&loc_result, t);
11374 add_loc_descr_op_piece (&loc_result, size);
11375 }
11376
11377 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11378 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11379 return loc_result;
11380 }
11381
11382 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11383
11384 /* Return a location descriptor that designates a constant i,
11385 as a compound operation from constant (i >> shift), constant shift
11386 and DW_OP_shl. */
11387
11388 static dw_loc_descr_ref
11389 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11390 {
11391 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11392 add_loc_descr (&ret, int_loc_descriptor (shift));
11393 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11394 return ret;
11395 }
11396
11397 /* Return a location descriptor that designates a constant. */
11398
11399 static dw_loc_descr_ref
11400 int_loc_descriptor (HOST_WIDE_INT i)
11401 {
11402 enum dwarf_location_atom op;
11403
11404 /* Pick the smallest representation of a constant, rather than just
11405 defaulting to the LEB encoding. */
11406 if (i >= 0)
11407 {
11408 int clz = clz_hwi (i);
11409 int ctz = ctz_hwi (i);
11410 if (i <= 31)
11411 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11412 else if (i <= 0xff)
11413 op = DW_OP_const1u;
11414 else if (i <= 0xffff)
11415 op = DW_OP_const2u;
11416 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11417 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11418 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11419 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11420 while DW_OP_const4u is 5 bytes. */
11421 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11422 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11423 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11424 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11425 while DW_OP_const4u is 5 bytes. */
11426 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11427 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11428 op = DW_OP_const4u;
11429 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11430 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11431 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11432 while DW_OP_constu of constant >= 0x100000000 takes at least
11433 6 bytes. */
11434 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11435 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11436 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11437 >= HOST_BITS_PER_WIDE_INT)
11438 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11439 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11440 while DW_OP_constu takes in this case at least 6 bytes. */
11441 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11442 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11443 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11444 && size_of_uleb128 (i) > 6)
11445 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11446 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11447 else
11448 op = DW_OP_constu;
11449 }
11450 else
11451 {
11452 if (i >= -0x80)
11453 op = DW_OP_const1s;
11454 else if (i >= -0x8000)
11455 op = DW_OP_const2s;
11456 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11457 {
11458 if (size_of_int_loc_descriptor (i) < 5)
11459 {
11460 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11461 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11462 return ret;
11463 }
11464 op = DW_OP_const4s;
11465 }
11466 else
11467 {
11468 if (size_of_int_loc_descriptor (i)
11469 < (unsigned long) 1 + size_of_sleb128 (i))
11470 {
11471 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11472 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11473 return ret;
11474 }
11475 op = DW_OP_consts;
11476 }
11477 }
11478
11479 return new_loc_descr (op, i, 0);
11480 }
11481
11482 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11483 without actually allocating it. */
11484
11485 static unsigned long
11486 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11487 {
11488 return size_of_int_loc_descriptor (i >> shift)
11489 + size_of_int_loc_descriptor (shift)
11490 + 1;
11491 }
11492
11493 /* Return size_of_locs (int_loc_descriptor (i)) without
11494 actually allocating it. */
11495
11496 static unsigned long
11497 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11498 {
11499 unsigned long s;
11500
11501 if (i >= 0)
11502 {
11503 int clz, ctz;
11504 if (i <= 31)
11505 return 1;
11506 else if (i <= 0xff)
11507 return 2;
11508 else if (i <= 0xffff)
11509 return 3;
11510 clz = clz_hwi (i);
11511 ctz = ctz_hwi (i);
11512 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11513 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11514 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11515 - clz - 5);
11516 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11517 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11518 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11519 - clz - 8);
11520 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11521 return 5;
11522 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11523 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11524 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11525 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11526 - clz - 8);
11527 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11528 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11529 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11530 - clz - 16);
11531 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11532 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11533 && s > 6)
11534 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11535 - clz - 32);
11536 else
11537 return 1 + s;
11538 }
11539 else
11540 {
11541 if (i >= -0x80)
11542 return 2;
11543 else if (i >= -0x8000)
11544 return 3;
11545 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11546 {
11547 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11548 {
11549 s = size_of_int_loc_descriptor (-i) + 1;
11550 if (s < 5)
11551 return s;
11552 }
11553 return 5;
11554 }
11555 else
11556 {
11557 unsigned long r = 1 + size_of_sleb128 (i);
11558 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11559 {
11560 s = size_of_int_loc_descriptor (-i) + 1;
11561 if (s < r)
11562 return s;
11563 }
11564 return r;
11565 }
11566 }
11567 }
11568
11569 /* Return loc description representing "address" of integer value.
11570 This can appear only as toplevel expression. */
11571
11572 static dw_loc_descr_ref
11573 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11574 {
11575 int litsize;
11576 dw_loc_descr_ref loc_result = NULL;
11577
11578 if (!(dwarf_version >= 4 || !dwarf_strict))
11579 return NULL;
11580
11581 litsize = size_of_int_loc_descriptor (i);
11582 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11583 is more compact. For DW_OP_stack_value we need:
11584 litsize + 1 (DW_OP_stack_value)
11585 and for DW_OP_implicit_value:
11586 1 (DW_OP_implicit_value) + 1 (length) + size. */
11587 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11588 {
11589 loc_result = int_loc_descriptor (i);
11590 add_loc_descr (&loc_result,
11591 new_loc_descr (DW_OP_stack_value, 0, 0));
11592 return loc_result;
11593 }
11594
11595 loc_result = new_loc_descr (DW_OP_implicit_value,
11596 size, 0);
11597 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11598 loc_result->dw_loc_oprnd2.v.val_int = i;
11599 return loc_result;
11600 }
11601
11602 /* Return a location descriptor that designates a base+offset location. */
11603
11604 static dw_loc_descr_ref
11605 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11606 enum var_init_status initialized)
11607 {
11608 unsigned int regno;
11609 dw_loc_descr_ref result;
11610 dw_fde_ref fde = cfun->fde;
11611
11612 /* We only use "frame base" when we're sure we're talking about the
11613 post-prologue local stack frame. We do this by *not* running
11614 register elimination until this point, and recognizing the special
11615 argument pointer and soft frame pointer rtx's. */
11616 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11617 {
11618 rtx elim = (ira_use_lra_p
11619 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11620 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11621
11622 if (elim != reg)
11623 {
11624 if (GET_CODE (elim) == PLUS)
11625 {
11626 offset += INTVAL (XEXP (elim, 1));
11627 elim = XEXP (elim, 0);
11628 }
11629 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11630 && (elim == hard_frame_pointer_rtx
11631 || elim == stack_pointer_rtx))
11632 || elim == (frame_pointer_needed
11633 ? hard_frame_pointer_rtx
11634 : stack_pointer_rtx));
11635
11636 /* If drap register is used to align stack, use frame
11637 pointer + offset to access stack variables. If stack
11638 is aligned without drap, use stack pointer + offset to
11639 access stack variables. */
11640 if (crtl->stack_realign_tried
11641 && reg == frame_pointer_rtx)
11642 {
11643 int base_reg
11644 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11645 ? HARD_FRAME_POINTER_REGNUM
11646 : REGNO (elim));
11647 return new_reg_loc_descr (base_reg, offset);
11648 }
11649
11650 gcc_assert (frame_pointer_fb_offset_valid);
11651 offset += frame_pointer_fb_offset;
11652 return new_loc_descr (DW_OP_fbreg, offset, 0);
11653 }
11654 }
11655
11656 regno = REGNO (reg);
11657 #ifdef LEAF_REG_REMAP
11658 if (crtl->uses_only_leaf_regs)
11659 {
11660 int leaf_reg = LEAF_REG_REMAP (regno);
11661 if (leaf_reg != -1)
11662 regno = (unsigned) leaf_reg;
11663 }
11664 #endif
11665 regno = DWARF_FRAME_REGNUM (regno);
11666
11667 if (!optimize && fde
11668 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11669 {
11670 /* Use cfa+offset to represent the location of arguments passed
11671 on the stack when drap is used to align stack.
11672 Only do this when not optimizing, for optimized code var-tracking
11673 is supposed to track where the arguments live and the register
11674 used as vdrap or drap in some spot might be used for something
11675 else in other part of the routine. */
11676 return new_loc_descr (DW_OP_fbreg, offset, 0);
11677 }
11678
11679 if (regno <= 31)
11680 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11681 offset, 0);
11682 else
11683 result = new_loc_descr (DW_OP_bregx, regno, offset);
11684
11685 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11686 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11687
11688 return result;
11689 }
11690
11691 /* Return true if this RTL expression describes a base+offset calculation. */
11692
11693 static inline int
11694 is_based_loc (const_rtx rtl)
11695 {
11696 return (GET_CODE (rtl) == PLUS
11697 && ((REG_P (XEXP (rtl, 0))
11698 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11699 && CONST_INT_P (XEXP (rtl, 1)))));
11700 }
11701
11702 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11703 failed. */
11704
11705 static dw_loc_descr_ref
11706 tls_mem_loc_descriptor (rtx mem)
11707 {
11708 tree base;
11709 dw_loc_descr_ref loc_result;
11710
11711 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11712 return NULL;
11713
11714 base = get_base_address (MEM_EXPR (mem));
11715 if (base == NULL
11716 || TREE_CODE (base) != VAR_DECL
11717 || !DECL_THREAD_LOCAL_P (base))
11718 return NULL;
11719
11720 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11721 if (loc_result == NULL)
11722 return NULL;
11723
11724 if (MEM_OFFSET (mem))
11725 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11726
11727 return loc_result;
11728 }
11729
11730 /* Output debug info about reason why we failed to expand expression as dwarf
11731 expression. */
11732
11733 static void
11734 expansion_failed (tree expr, rtx rtl, char const *reason)
11735 {
11736 if (dump_file && (dump_flags & TDF_DETAILS))
11737 {
11738 fprintf (dump_file, "Failed to expand as dwarf: ");
11739 if (expr)
11740 print_generic_expr (dump_file, expr, dump_flags);
11741 if (rtl)
11742 {
11743 fprintf (dump_file, "\n");
11744 print_rtl (dump_file, rtl);
11745 }
11746 fprintf (dump_file, "\nReason: %s\n", reason);
11747 }
11748 }
11749
11750 /* Helper function for const_ok_for_output. */
11751
11752 static bool
11753 const_ok_for_output_1 (rtx rtl)
11754 {
11755 if (GET_CODE (rtl) == UNSPEC)
11756 {
11757 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11758 we can't express it in the debug info. */
11759 /* Don't complain about TLS UNSPECs, those are just too hard to
11760 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11761 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11762 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11763 if (flag_checking
11764 && (XVECLEN (rtl, 0) == 0
11765 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11766 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
11767 inform (current_function_decl
11768 ? DECL_SOURCE_LOCATION (current_function_decl)
11769 : UNKNOWN_LOCATION,
11770 #if NUM_UNSPEC_VALUES > 0
11771 "non-delegitimized UNSPEC %s (%d) found in variable location",
11772 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11773 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11774 XINT (rtl, 1));
11775 #else
11776 "non-delegitimized UNSPEC %d found in variable location",
11777 XINT (rtl, 1));
11778 #endif
11779 expansion_failed (NULL_TREE, rtl,
11780 "UNSPEC hasn't been delegitimized.\n");
11781 return false;
11782 }
11783
11784 if (targetm.const_not_ok_for_debug_p (rtl))
11785 {
11786 expansion_failed (NULL_TREE, rtl,
11787 "Expression rejected for debug by the backend.\n");
11788 return false;
11789 }
11790
11791 /* FIXME: Refer to PR60655. It is possible for simplification
11792 of rtl expressions in var tracking to produce such expressions.
11793 We should really identify / validate expressions
11794 enclosed in CONST that can be handled by assemblers on various
11795 targets and only handle legitimate cases here. */
11796 if (GET_CODE (rtl) != SYMBOL_REF)
11797 {
11798 if (GET_CODE (rtl) == NOT)
11799 return false;
11800 return true;
11801 }
11802
11803 if (CONSTANT_POOL_ADDRESS_P (rtl))
11804 {
11805 bool marked;
11806 get_pool_constant_mark (rtl, &marked);
11807 /* If all references to this pool constant were optimized away,
11808 it was not output and thus we can't represent it. */
11809 if (!marked)
11810 {
11811 expansion_failed (NULL_TREE, rtl,
11812 "Constant was removed from constant pool.\n");
11813 return false;
11814 }
11815 }
11816
11817 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11818 return false;
11819
11820 /* Avoid references to external symbols in debug info, on several targets
11821 the linker might even refuse to link when linking a shared library,
11822 and in many other cases the relocations for .debug_info/.debug_loc are
11823 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11824 to be defined within the same shared library or executable are fine. */
11825 if (SYMBOL_REF_EXTERNAL_P (rtl))
11826 {
11827 tree decl = SYMBOL_REF_DECL (rtl);
11828
11829 if (decl == NULL || !targetm.binds_local_p (decl))
11830 {
11831 expansion_failed (NULL_TREE, rtl,
11832 "Symbol not defined in current TU.\n");
11833 return false;
11834 }
11835 }
11836
11837 return true;
11838 }
11839
11840 /* Return true if constant RTL can be emitted in DW_OP_addr or
11841 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11842 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11843
11844 static bool
11845 const_ok_for_output (rtx rtl)
11846 {
11847 if (GET_CODE (rtl) == SYMBOL_REF)
11848 return const_ok_for_output_1 (rtl);
11849
11850 if (GET_CODE (rtl) == CONST)
11851 {
11852 subrtx_var_iterator::array_type array;
11853 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11854 if (!const_ok_for_output_1 (*iter))
11855 return false;
11856 return true;
11857 }
11858
11859 return true;
11860 }
11861
11862 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11863 if possible, NULL otherwise. */
11864
11865 static dw_die_ref
11866 base_type_for_mode (machine_mode mode, bool unsignedp)
11867 {
11868 dw_die_ref type_die;
11869 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11870
11871 if (type == NULL)
11872 return NULL;
11873 switch (TREE_CODE (type))
11874 {
11875 case INTEGER_TYPE:
11876 case REAL_TYPE:
11877 break;
11878 default:
11879 return NULL;
11880 }
11881 type_die = lookup_type_die (type);
11882 if (!type_die)
11883 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11884 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11885 return NULL;
11886 return type_die;
11887 }
11888
11889 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11890 type matching MODE, or, if MODE is narrower than or as wide as
11891 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11892 possible. */
11893
11894 static dw_loc_descr_ref
11895 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11896 {
11897 machine_mode outer_mode = mode;
11898 dw_die_ref type_die;
11899 dw_loc_descr_ref cvt;
11900
11901 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11902 {
11903 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11904 return op;
11905 }
11906 type_die = base_type_for_mode (outer_mode, 1);
11907 if (type_die == NULL)
11908 return NULL;
11909 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11910 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11911 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11912 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11913 add_loc_descr (&op, cvt);
11914 return op;
11915 }
11916
11917 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11918
11919 static dw_loc_descr_ref
11920 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11921 dw_loc_descr_ref op1)
11922 {
11923 dw_loc_descr_ref ret = op0;
11924 add_loc_descr (&ret, op1);
11925 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11926 if (STORE_FLAG_VALUE != 1)
11927 {
11928 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11929 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11930 }
11931 return ret;
11932 }
11933
11934 /* Return location descriptor for signed comparison OP RTL. */
11935
11936 static dw_loc_descr_ref
11937 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11938 machine_mode mem_mode)
11939 {
11940 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11941 dw_loc_descr_ref op0, op1;
11942 int shift;
11943
11944 if (op_mode == VOIDmode)
11945 op_mode = GET_MODE (XEXP (rtl, 1));
11946 if (op_mode == VOIDmode)
11947 return NULL;
11948
11949 if (dwarf_strict
11950 && (GET_MODE_CLASS (op_mode) != MODE_INT
11951 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11952 return NULL;
11953
11954 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11955 VAR_INIT_STATUS_INITIALIZED);
11956 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11957 VAR_INIT_STATUS_INITIALIZED);
11958
11959 if (op0 == NULL || op1 == NULL)
11960 return NULL;
11961
11962 if (GET_MODE_CLASS (op_mode) != MODE_INT
11963 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11964 return compare_loc_descriptor (op, op0, op1);
11965
11966 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11967 {
11968 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11969 dw_loc_descr_ref cvt;
11970
11971 if (type_die == NULL)
11972 return NULL;
11973 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11974 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11975 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11976 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11977 add_loc_descr (&op0, cvt);
11978 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11979 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11980 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11981 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11982 add_loc_descr (&op1, cvt);
11983 return compare_loc_descriptor (op, op0, op1);
11984 }
11985
11986 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11987 /* For eq/ne, if the operands are known to be zero-extended,
11988 there is no need to do the fancy shifting up. */
11989 if (op == DW_OP_eq || op == DW_OP_ne)
11990 {
11991 dw_loc_descr_ref last0, last1;
11992 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11993 ;
11994 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11995 ;
11996 /* deref_size zero extends, and for constants we can check
11997 whether they are zero extended or not. */
11998 if (((last0->dw_loc_opc == DW_OP_deref_size
11999 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12000 || (CONST_INT_P (XEXP (rtl, 0))
12001 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12002 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12003 && ((last1->dw_loc_opc == DW_OP_deref_size
12004 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12005 || (CONST_INT_P (XEXP (rtl, 1))
12006 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12007 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12008 return compare_loc_descriptor (op, op0, op1);
12009
12010 /* EQ/NE comparison against constant in narrower type than
12011 DWARF2_ADDR_SIZE can be performed either as
12012 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12013 DW_OP_{eq,ne}
12014 or
12015 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12016 DW_OP_{eq,ne}. Pick whatever is shorter. */
12017 if (CONST_INT_P (XEXP (rtl, 1))
12018 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12019 && (size_of_int_loc_descriptor (shift) + 1
12020 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12021 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12022 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12023 & GET_MODE_MASK (op_mode))))
12024 {
12025 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12026 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12027 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12028 & GET_MODE_MASK (op_mode));
12029 return compare_loc_descriptor (op, op0, op1);
12030 }
12031 }
12032 add_loc_descr (&op0, int_loc_descriptor (shift));
12033 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12034 if (CONST_INT_P (XEXP (rtl, 1)))
12035 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12036 else
12037 {
12038 add_loc_descr (&op1, int_loc_descriptor (shift));
12039 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12040 }
12041 return compare_loc_descriptor (op, op0, op1);
12042 }
12043
12044 /* Return location descriptor for unsigned comparison OP RTL. */
12045
12046 static dw_loc_descr_ref
12047 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12048 machine_mode mem_mode)
12049 {
12050 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12051 dw_loc_descr_ref op0, op1;
12052
12053 if (op_mode == VOIDmode)
12054 op_mode = GET_MODE (XEXP (rtl, 1));
12055 if (op_mode == VOIDmode)
12056 return NULL;
12057 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12058 return NULL;
12059
12060 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12061 return NULL;
12062
12063 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12064 VAR_INIT_STATUS_INITIALIZED);
12065 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12066 VAR_INIT_STATUS_INITIALIZED);
12067
12068 if (op0 == NULL || op1 == NULL)
12069 return NULL;
12070
12071 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12072 {
12073 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12074 dw_loc_descr_ref last0, last1;
12075 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12076 ;
12077 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12078 ;
12079 if (CONST_INT_P (XEXP (rtl, 0)))
12080 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12081 /* deref_size zero extends, so no need to mask it again. */
12082 else if (last0->dw_loc_opc != DW_OP_deref_size
12083 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12084 {
12085 add_loc_descr (&op0, int_loc_descriptor (mask));
12086 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12087 }
12088 if (CONST_INT_P (XEXP (rtl, 1)))
12089 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12090 /* deref_size zero extends, so no need to mask it again. */
12091 else if (last1->dw_loc_opc != DW_OP_deref_size
12092 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12093 {
12094 add_loc_descr (&op1, int_loc_descriptor (mask));
12095 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12096 }
12097 }
12098 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12099 {
12100 HOST_WIDE_INT bias = 1;
12101 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12102 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12103 if (CONST_INT_P (XEXP (rtl, 1)))
12104 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12105 + INTVAL (XEXP (rtl, 1)));
12106 else
12107 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12108 bias, 0));
12109 }
12110 return compare_loc_descriptor (op, op0, op1);
12111 }
12112
12113 /* Return location descriptor for {U,S}{MIN,MAX}. */
12114
12115 static dw_loc_descr_ref
12116 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12117 machine_mode mem_mode)
12118 {
12119 enum dwarf_location_atom op;
12120 dw_loc_descr_ref op0, op1, ret;
12121 dw_loc_descr_ref bra_node, drop_node;
12122
12123 if (dwarf_strict
12124 && (GET_MODE_CLASS (mode) != MODE_INT
12125 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12126 return NULL;
12127
12128 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12129 VAR_INIT_STATUS_INITIALIZED);
12130 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12131 VAR_INIT_STATUS_INITIALIZED);
12132
12133 if (op0 == NULL || op1 == NULL)
12134 return NULL;
12135
12136 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12137 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12138 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12139 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12140 {
12141 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12142 {
12143 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12144 add_loc_descr (&op0, int_loc_descriptor (mask));
12145 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12146 add_loc_descr (&op1, int_loc_descriptor (mask));
12147 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12148 }
12149 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12150 {
12151 HOST_WIDE_INT bias = 1;
12152 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12153 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12154 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12155 }
12156 }
12157 else if (GET_MODE_CLASS (mode) == MODE_INT
12158 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12159 {
12160 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12161 add_loc_descr (&op0, int_loc_descriptor (shift));
12162 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12163 add_loc_descr (&op1, int_loc_descriptor (shift));
12164 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12165 }
12166 else if (GET_MODE_CLASS (mode) == MODE_INT
12167 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12168 {
12169 dw_die_ref type_die = base_type_for_mode (mode, 0);
12170 dw_loc_descr_ref cvt;
12171 if (type_die == NULL)
12172 return NULL;
12173 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12174 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12175 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12176 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12177 add_loc_descr (&op0, cvt);
12178 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12179 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12180 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12181 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12182 add_loc_descr (&op1, cvt);
12183 }
12184
12185 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12186 op = DW_OP_lt;
12187 else
12188 op = DW_OP_gt;
12189 ret = op0;
12190 add_loc_descr (&ret, op1);
12191 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12192 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12193 add_loc_descr (&ret, bra_node);
12194 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12195 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12196 add_loc_descr (&ret, drop_node);
12197 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12198 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12199 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12200 && GET_MODE_CLASS (mode) == MODE_INT
12201 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12202 ret = convert_descriptor_to_mode (mode, ret);
12203 return ret;
12204 }
12205
12206 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12207 but after converting arguments to type_die, afterwards
12208 convert back to unsigned. */
12209
12210 static dw_loc_descr_ref
12211 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12212 machine_mode mode, machine_mode mem_mode)
12213 {
12214 dw_loc_descr_ref cvt, op0, op1;
12215
12216 if (type_die == NULL)
12217 return NULL;
12218 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12219 VAR_INIT_STATUS_INITIALIZED);
12220 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12221 VAR_INIT_STATUS_INITIALIZED);
12222 if (op0 == NULL || op1 == NULL)
12223 return NULL;
12224 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12225 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12226 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12227 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12228 add_loc_descr (&op0, cvt);
12229 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12230 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12231 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12232 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12233 add_loc_descr (&op1, cvt);
12234 add_loc_descr (&op0, op1);
12235 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12236 return convert_descriptor_to_mode (mode, op0);
12237 }
12238
12239 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12240 const0 is DW_OP_lit0 or corresponding typed constant,
12241 const1 is DW_OP_lit1 or corresponding typed constant
12242 and constMSB is constant with just the MSB bit set
12243 for the mode):
12244 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12245 L1: const0 DW_OP_swap
12246 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12247 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12248 L3: DW_OP_drop
12249 L4: DW_OP_nop
12250
12251 CTZ is similar:
12252 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12253 L1: const0 DW_OP_swap
12254 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12255 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12256 L3: DW_OP_drop
12257 L4: DW_OP_nop
12258
12259 FFS is similar:
12260 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12261 L1: const1 DW_OP_swap
12262 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12263 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12264 L3: DW_OP_drop
12265 L4: DW_OP_nop */
12266
12267 static dw_loc_descr_ref
12268 clz_loc_descriptor (rtx rtl, machine_mode mode,
12269 machine_mode mem_mode)
12270 {
12271 dw_loc_descr_ref op0, ret, tmp;
12272 HOST_WIDE_INT valv;
12273 dw_loc_descr_ref l1jump, l1label;
12274 dw_loc_descr_ref l2jump, l2label;
12275 dw_loc_descr_ref l3jump, l3label;
12276 dw_loc_descr_ref l4jump, l4label;
12277 rtx msb;
12278
12279 if (GET_MODE_CLASS (mode) != MODE_INT
12280 || GET_MODE (XEXP (rtl, 0)) != mode)
12281 return NULL;
12282
12283 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12284 VAR_INIT_STATUS_INITIALIZED);
12285 if (op0 == NULL)
12286 return NULL;
12287 ret = op0;
12288 if (GET_CODE (rtl) == CLZ)
12289 {
12290 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12291 valv = GET_MODE_BITSIZE (mode);
12292 }
12293 else if (GET_CODE (rtl) == FFS)
12294 valv = 0;
12295 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12296 valv = GET_MODE_BITSIZE (mode);
12297 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12298 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12299 add_loc_descr (&ret, l1jump);
12300 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12301 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12302 VAR_INIT_STATUS_INITIALIZED);
12303 if (tmp == NULL)
12304 return NULL;
12305 add_loc_descr (&ret, tmp);
12306 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12307 add_loc_descr (&ret, l4jump);
12308 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12309 ? const1_rtx : const0_rtx,
12310 mode, mem_mode,
12311 VAR_INIT_STATUS_INITIALIZED);
12312 if (l1label == NULL)
12313 return NULL;
12314 add_loc_descr (&ret, l1label);
12315 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12316 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12317 add_loc_descr (&ret, l2label);
12318 if (GET_CODE (rtl) != CLZ)
12319 msb = const1_rtx;
12320 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12321 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12322 << (GET_MODE_BITSIZE (mode) - 1));
12323 else
12324 msb = immed_wide_int_const
12325 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12326 GET_MODE_PRECISION (mode)), mode);
12327 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12328 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12329 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12330 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12331 else
12332 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12333 VAR_INIT_STATUS_INITIALIZED);
12334 if (tmp == NULL)
12335 return NULL;
12336 add_loc_descr (&ret, tmp);
12337 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12338 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12339 add_loc_descr (&ret, l3jump);
12340 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12341 VAR_INIT_STATUS_INITIALIZED);
12342 if (tmp == NULL)
12343 return NULL;
12344 add_loc_descr (&ret, tmp);
12345 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12346 ? DW_OP_shl : DW_OP_shr, 0, 0));
12347 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12348 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12349 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12350 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12351 add_loc_descr (&ret, l2jump);
12352 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12353 add_loc_descr (&ret, l3label);
12354 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12355 add_loc_descr (&ret, l4label);
12356 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12357 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12358 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12359 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12360 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12361 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12362 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12363 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12364 return ret;
12365 }
12366
12367 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12368 const1 is DW_OP_lit1 or corresponding typed constant):
12369 const0 DW_OP_swap
12370 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12371 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12372 L2: DW_OP_drop
12373
12374 PARITY is similar:
12375 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12376 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12377 L2: DW_OP_drop */
12378
12379 static dw_loc_descr_ref
12380 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12381 machine_mode mem_mode)
12382 {
12383 dw_loc_descr_ref op0, ret, tmp;
12384 dw_loc_descr_ref l1jump, l1label;
12385 dw_loc_descr_ref l2jump, l2label;
12386
12387 if (GET_MODE_CLASS (mode) != MODE_INT
12388 || GET_MODE (XEXP (rtl, 0)) != mode)
12389 return NULL;
12390
12391 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12392 VAR_INIT_STATUS_INITIALIZED);
12393 if (op0 == NULL)
12394 return NULL;
12395 ret = op0;
12396 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12397 VAR_INIT_STATUS_INITIALIZED);
12398 if (tmp == NULL)
12399 return NULL;
12400 add_loc_descr (&ret, tmp);
12401 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12402 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12403 add_loc_descr (&ret, l1label);
12404 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12405 add_loc_descr (&ret, l2jump);
12406 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12407 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12408 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12409 VAR_INIT_STATUS_INITIALIZED);
12410 if (tmp == NULL)
12411 return NULL;
12412 add_loc_descr (&ret, tmp);
12413 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12414 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12415 ? DW_OP_plus : DW_OP_xor, 0, 0));
12416 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12417 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12418 VAR_INIT_STATUS_INITIALIZED);
12419 add_loc_descr (&ret, tmp);
12420 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12421 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12422 add_loc_descr (&ret, l1jump);
12423 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12424 add_loc_descr (&ret, l2label);
12425 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12426 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12427 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12428 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12429 return ret;
12430 }
12431
12432 /* BSWAP (constS is initial shift count, either 56 or 24):
12433 constS const0
12434 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12435 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12436 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12437 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12438 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12439
12440 static dw_loc_descr_ref
12441 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12442 machine_mode mem_mode)
12443 {
12444 dw_loc_descr_ref op0, ret, tmp;
12445 dw_loc_descr_ref l1jump, l1label;
12446 dw_loc_descr_ref l2jump, l2label;
12447
12448 if (GET_MODE_CLASS (mode) != MODE_INT
12449 || BITS_PER_UNIT != 8
12450 || (GET_MODE_BITSIZE (mode) != 32
12451 && GET_MODE_BITSIZE (mode) != 64))
12452 return NULL;
12453
12454 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12455 VAR_INIT_STATUS_INITIALIZED);
12456 if (op0 == NULL)
12457 return NULL;
12458
12459 ret = op0;
12460 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12461 mode, mem_mode,
12462 VAR_INIT_STATUS_INITIALIZED);
12463 if (tmp == NULL)
12464 return NULL;
12465 add_loc_descr (&ret, tmp);
12466 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12467 VAR_INIT_STATUS_INITIALIZED);
12468 if (tmp == NULL)
12469 return NULL;
12470 add_loc_descr (&ret, tmp);
12471 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12472 add_loc_descr (&ret, l1label);
12473 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12474 mode, mem_mode,
12475 VAR_INIT_STATUS_INITIALIZED);
12476 add_loc_descr (&ret, tmp);
12477 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12478 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12479 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12480 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12481 VAR_INIT_STATUS_INITIALIZED);
12482 if (tmp == NULL)
12483 return NULL;
12484 add_loc_descr (&ret, tmp);
12485 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12486 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12487 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12488 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12489 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12490 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12491 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12492 VAR_INIT_STATUS_INITIALIZED);
12493 add_loc_descr (&ret, tmp);
12494 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12495 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12496 add_loc_descr (&ret, l2jump);
12497 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12498 VAR_INIT_STATUS_INITIALIZED);
12499 add_loc_descr (&ret, tmp);
12500 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12501 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12502 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12503 add_loc_descr (&ret, l1jump);
12504 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12505 add_loc_descr (&ret, l2label);
12506 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12507 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12508 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12509 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12510 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12511 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12512 return ret;
12513 }
12514
12515 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12516 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12517 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12518 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12519
12520 ROTATERT is similar:
12521 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12522 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12523 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12524
12525 static dw_loc_descr_ref
12526 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12527 machine_mode mem_mode)
12528 {
12529 rtx rtlop1 = XEXP (rtl, 1);
12530 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12531 int i;
12532
12533 if (GET_MODE_CLASS (mode) != MODE_INT)
12534 return NULL;
12535
12536 if (GET_MODE (rtlop1) != VOIDmode
12537 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12538 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12539 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12540 VAR_INIT_STATUS_INITIALIZED);
12541 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12542 VAR_INIT_STATUS_INITIALIZED);
12543 if (op0 == NULL || op1 == NULL)
12544 return NULL;
12545 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12546 for (i = 0; i < 2; i++)
12547 {
12548 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12549 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12550 mode, mem_mode,
12551 VAR_INIT_STATUS_INITIALIZED);
12552 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12553 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12554 ? DW_OP_const4u
12555 : HOST_BITS_PER_WIDE_INT == 64
12556 ? DW_OP_const8u : DW_OP_constu,
12557 GET_MODE_MASK (mode), 0);
12558 else
12559 mask[i] = NULL;
12560 if (mask[i] == NULL)
12561 return NULL;
12562 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12563 }
12564 ret = op0;
12565 add_loc_descr (&ret, op1);
12566 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12567 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12568 if (GET_CODE (rtl) == ROTATERT)
12569 {
12570 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12571 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12572 GET_MODE_BITSIZE (mode), 0));
12573 }
12574 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12575 if (mask[0] != NULL)
12576 add_loc_descr (&ret, mask[0]);
12577 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12578 if (mask[1] != NULL)
12579 {
12580 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12581 add_loc_descr (&ret, mask[1]);
12582 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12583 }
12584 if (GET_CODE (rtl) == ROTATE)
12585 {
12586 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12587 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12588 GET_MODE_BITSIZE (mode), 0));
12589 }
12590 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12591 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12592 return ret;
12593 }
12594
12595 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12596 for DEBUG_PARAMETER_REF RTL. */
12597
12598 static dw_loc_descr_ref
12599 parameter_ref_descriptor (rtx rtl)
12600 {
12601 dw_loc_descr_ref ret;
12602 dw_die_ref ref;
12603
12604 if (dwarf_strict)
12605 return NULL;
12606 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12607 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12608 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12609 if (ref)
12610 {
12611 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12612 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12613 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12614 }
12615 else
12616 {
12617 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12618 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12619 }
12620 return ret;
12621 }
12622
12623 /* The following routine converts the RTL for a variable or parameter
12624 (resident in memory) into an equivalent Dwarf representation of a
12625 mechanism for getting the address of that same variable onto the top of a
12626 hypothetical "address evaluation" stack.
12627
12628 When creating memory location descriptors, we are effectively transforming
12629 the RTL for a memory-resident object into its Dwarf postfix expression
12630 equivalent. This routine recursively descends an RTL tree, turning
12631 it into Dwarf postfix code as it goes.
12632
12633 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12634
12635 MEM_MODE is the mode of the memory reference, needed to handle some
12636 autoincrement addressing modes.
12637
12638 Return 0 if we can't represent the location. */
12639
12640 dw_loc_descr_ref
12641 mem_loc_descriptor (rtx rtl, machine_mode mode,
12642 machine_mode mem_mode,
12643 enum var_init_status initialized)
12644 {
12645 dw_loc_descr_ref mem_loc_result = NULL;
12646 enum dwarf_location_atom op;
12647 dw_loc_descr_ref op0, op1;
12648 rtx inner = NULL_RTX;
12649
12650 if (mode == VOIDmode)
12651 mode = GET_MODE (rtl);
12652
12653 /* Note that for a dynamically sized array, the location we will generate a
12654 description of here will be the lowest numbered location which is
12655 actually within the array. That's *not* necessarily the same as the
12656 zeroth element of the array. */
12657
12658 rtl = targetm.delegitimize_address (rtl);
12659
12660 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12661 return NULL;
12662
12663 switch (GET_CODE (rtl))
12664 {
12665 case POST_INC:
12666 case POST_DEC:
12667 case POST_MODIFY:
12668 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12669
12670 case SUBREG:
12671 /* The case of a subreg may arise when we have a local (register)
12672 variable or a formal (register) parameter which doesn't quite fill
12673 up an entire register. For now, just assume that it is
12674 legitimate to make the Dwarf info refer to the whole register which
12675 contains the given subreg. */
12676 if (!subreg_lowpart_p (rtl))
12677 break;
12678 inner = SUBREG_REG (rtl);
12679 case TRUNCATE:
12680 if (inner == NULL_RTX)
12681 inner = XEXP (rtl, 0);
12682 if (GET_MODE_CLASS (mode) == MODE_INT
12683 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12684 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12685 #ifdef POINTERS_EXTEND_UNSIGNED
12686 || (mode == Pmode && mem_mode != VOIDmode)
12687 #endif
12688 )
12689 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12690 {
12691 mem_loc_result = mem_loc_descriptor (inner,
12692 GET_MODE (inner),
12693 mem_mode, initialized);
12694 break;
12695 }
12696 if (dwarf_strict)
12697 break;
12698 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12699 break;
12700 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12701 && (GET_MODE_CLASS (mode) != MODE_INT
12702 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12703 break;
12704 else
12705 {
12706 dw_die_ref type_die;
12707 dw_loc_descr_ref cvt;
12708
12709 mem_loc_result = mem_loc_descriptor (inner,
12710 GET_MODE (inner),
12711 mem_mode, initialized);
12712 if (mem_loc_result == NULL)
12713 break;
12714 type_die = base_type_for_mode (mode,
12715 GET_MODE_CLASS (mode) == MODE_INT);
12716 if (type_die == NULL)
12717 {
12718 mem_loc_result = NULL;
12719 break;
12720 }
12721 if (GET_MODE_SIZE (mode)
12722 != GET_MODE_SIZE (GET_MODE (inner)))
12723 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12724 else
12725 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12726 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12727 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12728 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12729 add_loc_descr (&mem_loc_result, cvt);
12730 }
12731 break;
12732
12733 case REG:
12734 if (GET_MODE_CLASS (mode) != MODE_INT
12735 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12736 && rtl != arg_pointer_rtx
12737 && rtl != frame_pointer_rtx
12738 #ifdef POINTERS_EXTEND_UNSIGNED
12739 && (mode != Pmode || mem_mode == VOIDmode)
12740 #endif
12741 ))
12742 {
12743 dw_die_ref type_die;
12744 unsigned int dbx_regnum;
12745
12746 if (dwarf_strict)
12747 break;
12748 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12749 break;
12750 type_die = base_type_for_mode (mode,
12751 GET_MODE_CLASS (mode) == MODE_INT);
12752 if (type_die == NULL)
12753 break;
12754
12755 dbx_regnum = dbx_reg_number (rtl);
12756 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12757 break;
12758 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12759 dbx_regnum, 0);
12760 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12761 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12762 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12763 break;
12764 }
12765 /* Whenever a register number forms a part of the description of the
12766 method for calculating the (dynamic) address of a memory resident
12767 object, DWARF rules require the register number be referred to as
12768 a "base register". This distinction is not based in any way upon
12769 what category of register the hardware believes the given register
12770 belongs to. This is strictly DWARF terminology we're dealing with
12771 here. Note that in cases where the location of a memory-resident
12772 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12773 OP_CONST (0)) the actual DWARF location descriptor that we generate
12774 may just be OP_BASEREG (basereg). This may look deceptively like
12775 the object in question was allocated to a register (rather than in
12776 memory) so DWARF consumers need to be aware of the subtle
12777 distinction between OP_REG and OP_BASEREG. */
12778 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12779 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12780 else if (stack_realign_drap
12781 && crtl->drap_reg
12782 && crtl->args.internal_arg_pointer == rtl
12783 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12784 {
12785 /* If RTL is internal_arg_pointer, which has been optimized
12786 out, use DRAP instead. */
12787 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12788 VAR_INIT_STATUS_INITIALIZED);
12789 }
12790 break;
12791
12792 case SIGN_EXTEND:
12793 case ZERO_EXTEND:
12794 if (GET_MODE_CLASS (mode) != MODE_INT)
12795 break;
12796 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12797 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12798 if (op0 == 0)
12799 break;
12800 else if (GET_CODE (rtl) == ZERO_EXTEND
12801 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12802 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12803 < HOST_BITS_PER_WIDE_INT
12804 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12805 to expand zero extend as two shifts instead of
12806 masking. */
12807 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12808 {
12809 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12810 mem_loc_result = op0;
12811 add_loc_descr (&mem_loc_result,
12812 int_loc_descriptor (GET_MODE_MASK (imode)));
12813 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12814 }
12815 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12816 {
12817 int shift = DWARF2_ADDR_SIZE
12818 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12819 shift *= BITS_PER_UNIT;
12820 if (GET_CODE (rtl) == SIGN_EXTEND)
12821 op = DW_OP_shra;
12822 else
12823 op = DW_OP_shr;
12824 mem_loc_result = op0;
12825 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12826 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12827 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12828 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12829 }
12830 else if (!dwarf_strict)
12831 {
12832 dw_die_ref type_die1, type_die2;
12833 dw_loc_descr_ref cvt;
12834
12835 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12836 GET_CODE (rtl) == ZERO_EXTEND);
12837 if (type_die1 == NULL)
12838 break;
12839 type_die2 = base_type_for_mode (mode, 1);
12840 if (type_die2 == NULL)
12841 break;
12842 mem_loc_result = op0;
12843 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12844 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12845 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12846 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12847 add_loc_descr (&mem_loc_result, cvt);
12848 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12849 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12850 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12851 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12852 add_loc_descr (&mem_loc_result, cvt);
12853 }
12854 break;
12855
12856 case MEM:
12857 {
12858 rtx new_rtl = avoid_constant_pool_reference (rtl);
12859 if (new_rtl != rtl)
12860 {
12861 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12862 initialized);
12863 if (mem_loc_result != NULL)
12864 return mem_loc_result;
12865 }
12866 }
12867 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12868 get_address_mode (rtl), mode,
12869 VAR_INIT_STATUS_INITIALIZED);
12870 if (mem_loc_result == NULL)
12871 mem_loc_result = tls_mem_loc_descriptor (rtl);
12872 if (mem_loc_result != NULL)
12873 {
12874 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12875 || GET_MODE_CLASS (mode) != MODE_INT)
12876 {
12877 dw_die_ref type_die;
12878 dw_loc_descr_ref deref;
12879
12880 if (dwarf_strict)
12881 return NULL;
12882 type_die
12883 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12884 if (type_die == NULL)
12885 return NULL;
12886 deref = new_loc_descr (DW_OP_GNU_deref_type,
12887 GET_MODE_SIZE (mode), 0);
12888 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12889 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12890 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12891 add_loc_descr (&mem_loc_result, deref);
12892 }
12893 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12894 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12895 else
12896 add_loc_descr (&mem_loc_result,
12897 new_loc_descr (DW_OP_deref_size,
12898 GET_MODE_SIZE (mode), 0));
12899 }
12900 break;
12901
12902 case LO_SUM:
12903 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12904
12905 case LABEL_REF:
12906 /* Some ports can transform a symbol ref into a label ref, because
12907 the symbol ref is too far away and has to be dumped into a constant
12908 pool. */
12909 case CONST:
12910 case SYMBOL_REF:
12911 if ((GET_MODE_CLASS (mode) != MODE_INT
12912 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12913 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12914 #ifdef POINTERS_EXTEND_UNSIGNED
12915 && (mode != Pmode || mem_mode == VOIDmode)
12916 #endif
12917 ))
12918 break;
12919 if (GET_CODE (rtl) == SYMBOL_REF
12920 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12921 {
12922 dw_loc_descr_ref temp;
12923
12924 /* If this is not defined, we have no way to emit the data. */
12925 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12926 break;
12927
12928 temp = new_addr_loc_descr (rtl, dtprel_true);
12929
12930 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12931 add_loc_descr (&mem_loc_result, temp);
12932
12933 break;
12934 }
12935
12936 if (!const_ok_for_output (rtl))
12937 {
12938 if (GET_CODE (rtl) == CONST)
12939 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12940 initialized);
12941 break;
12942 }
12943
12944 symref:
12945 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12946 vec_safe_push (used_rtx_array, rtl);
12947 break;
12948
12949 case CONCAT:
12950 case CONCATN:
12951 case VAR_LOCATION:
12952 case DEBUG_IMPLICIT_PTR:
12953 expansion_failed (NULL_TREE, rtl,
12954 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12955 return 0;
12956
12957 case ENTRY_VALUE:
12958 if (dwarf_strict)
12959 return NULL;
12960 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12961 {
12962 if (GET_MODE_CLASS (mode) != MODE_INT
12963 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12964 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12965 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12966 else
12967 {
12968 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12969 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12970 return NULL;
12971 op0 = one_reg_loc_descriptor (dbx_regnum,
12972 VAR_INIT_STATUS_INITIALIZED);
12973 }
12974 }
12975 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12976 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12977 {
12978 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12979 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12980 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12981 return NULL;
12982 }
12983 else
12984 gcc_unreachable ();
12985 if (op0 == NULL)
12986 return NULL;
12987 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12988 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12989 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12990 break;
12991
12992 case DEBUG_PARAMETER_REF:
12993 mem_loc_result = parameter_ref_descriptor (rtl);
12994 break;
12995
12996 case PRE_MODIFY:
12997 /* Extract the PLUS expression nested inside and fall into
12998 PLUS code below. */
12999 rtl = XEXP (rtl, 1);
13000 goto plus;
13001
13002 case PRE_INC:
13003 case PRE_DEC:
13004 /* Turn these into a PLUS expression and fall into the PLUS code
13005 below. */
13006 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13007 gen_int_mode (GET_CODE (rtl) == PRE_INC
13008 ? GET_MODE_UNIT_SIZE (mem_mode)
13009 : -GET_MODE_UNIT_SIZE (mem_mode),
13010 mode));
13011
13012 /* ... fall through ... */
13013
13014 case PLUS:
13015 plus:
13016 if (is_based_loc (rtl)
13017 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13018 || XEXP (rtl, 0) == arg_pointer_rtx
13019 || XEXP (rtl, 0) == frame_pointer_rtx)
13020 && GET_MODE_CLASS (mode) == MODE_INT)
13021 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13022 INTVAL (XEXP (rtl, 1)),
13023 VAR_INIT_STATUS_INITIALIZED);
13024 else
13025 {
13026 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13027 VAR_INIT_STATUS_INITIALIZED);
13028 if (mem_loc_result == 0)
13029 break;
13030
13031 if (CONST_INT_P (XEXP (rtl, 1))
13032 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13033 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13034 else
13035 {
13036 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13037 VAR_INIT_STATUS_INITIALIZED);
13038 if (op1 == 0)
13039 return NULL;
13040 add_loc_descr (&mem_loc_result, op1);
13041 add_loc_descr (&mem_loc_result,
13042 new_loc_descr (DW_OP_plus, 0, 0));
13043 }
13044 }
13045 break;
13046
13047 /* If a pseudo-reg is optimized away, it is possible for it to
13048 be replaced with a MEM containing a multiply or shift. */
13049 case MINUS:
13050 op = DW_OP_minus;
13051 goto do_binop;
13052
13053 case MULT:
13054 op = DW_OP_mul;
13055 goto do_binop;
13056
13057 case DIV:
13058 if (!dwarf_strict
13059 && GET_MODE_CLASS (mode) == MODE_INT
13060 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13061 {
13062 mem_loc_result = typed_binop (DW_OP_div, rtl,
13063 base_type_for_mode (mode, 0),
13064 mode, mem_mode);
13065 break;
13066 }
13067 op = DW_OP_div;
13068 goto do_binop;
13069
13070 case UMOD:
13071 op = DW_OP_mod;
13072 goto do_binop;
13073
13074 case ASHIFT:
13075 op = DW_OP_shl;
13076 goto do_shift;
13077
13078 case ASHIFTRT:
13079 op = DW_OP_shra;
13080 goto do_shift;
13081
13082 case LSHIFTRT:
13083 op = DW_OP_shr;
13084 goto do_shift;
13085
13086 do_shift:
13087 if (GET_MODE_CLASS (mode) != MODE_INT)
13088 break;
13089 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13090 VAR_INIT_STATUS_INITIALIZED);
13091 {
13092 rtx rtlop1 = XEXP (rtl, 1);
13093 if (GET_MODE (rtlop1) != VOIDmode
13094 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13095 < GET_MODE_BITSIZE (mode))
13096 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13097 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13098 VAR_INIT_STATUS_INITIALIZED);
13099 }
13100
13101 if (op0 == 0 || op1 == 0)
13102 break;
13103
13104 mem_loc_result = op0;
13105 add_loc_descr (&mem_loc_result, op1);
13106 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13107 break;
13108
13109 case AND:
13110 op = DW_OP_and;
13111 goto do_binop;
13112
13113 case IOR:
13114 op = DW_OP_or;
13115 goto do_binop;
13116
13117 case XOR:
13118 op = DW_OP_xor;
13119 goto do_binop;
13120
13121 do_binop:
13122 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13123 VAR_INIT_STATUS_INITIALIZED);
13124 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13125 VAR_INIT_STATUS_INITIALIZED);
13126
13127 if (op0 == 0 || op1 == 0)
13128 break;
13129
13130 mem_loc_result = op0;
13131 add_loc_descr (&mem_loc_result, op1);
13132 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13133 break;
13134
13135 case MOD:
13136 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13137 {
13138 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13139 base_type_for_mode (mode, 0),
13140 mode, mem_mode);
13141 break;
13142 }
13143
13144 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13145 VAR_INIT_STATUS_INITIALIZED);
13146 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13147 VAR_INIT_STATUS_INITIALIZED);
13148
13149 if (op0 == 0 || op1 == 0)
13150 break;
13151
13152 mem_loc_result = op0;
13153 add_loc_descr (&mem_loc_result, op1);
13154 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13155 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13156 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13157 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13158 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13159 break;
13160
13161 case UDIV:
13162 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13163 {
13164 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13165 {
13166 op = DW_OP_div;
13167 goto do_binop;
13168 }
13169 mem_loc_result = typed_binop (DW_OP_div, rtl,
13170 base_type_for_mode (mode, 1),
13171 mode, mem_mode);
13172 }
13173 break;
13174
13175 case NOT:
13176 op = DW_OP_not;
13177 goto do_unop;
13178
13179 case ABS:
13180 op = DW_OP_abs;
13181 goto do_unop;
13182
13183 case NEG:
13184 op = DW_OP_neg;
13185 goto do_unop;
13186
13187 do_unop:
13188 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13189 VAR_INIT_STATUS_INITIALIZED);
13190
13191 if (op0 == 0)
13192 break;
13193
13194 mem_loc_result = op0;
13195 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13196 break;
13197
13198 case CONST_INT:
13199 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13200 #ifdef POINTERS_EXTEND_UNSIGNED
13201 || (mode == Pmode
13202 && mem_mode != VOIDmode
13203 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13204 #endif
13205 )
13206 {
13207 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13208 break;
13209 }
13210 if (!dwarf_strict
13211 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13212 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13213 {
13214 dw_die_ref type_die = base_type_for_mode (mode, 1);
13215 machine_mode amode;
13216 if (type_die == NULL)
13217 return NULL;
13218 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13219 MODE_INT, 0);
13220 if (INTVAL (rtl) >= 0
13221 && amode != BLKmode
13222 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13223 /* const DW_OP_GNU_convert <XXX> vs.
13224 DW_OP_GNU_const_type <XXX, 1, const>. */
13225 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13226 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13227 {
13228 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13229 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13230 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13231 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13232 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13233 add_loc_descr (&mem_loc_result, op0);
13234 return mem_loc_result;
13235 }
13236 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13237 INTVAL (rtl));
13238 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13239 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13240 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13241 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13242 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13243 else
13244 {
13245 mem_loc_result->dw_loc_oprnd2.val_class
13246 = dw_val_class_const_double;
13247 mem_loc_result->dw_loc_oprnd2.v.val_double
13248 = double_int::from_shwi (INTVAL (rtl));
13249 }
13250 }
13251 break;
13252
13253 case CONST_DOUBLE:
13254 if (!dwarf_strict)
13255 {
13256 dw_die_ref type_die;
13257
13258 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13259 CONST_DOUBLE rtx could represent either a large integer
13260 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13261 the value is always a floating point constant.
13262
13263 When it is an integer, a CONST_DOUBLE is used whenever
13264 the constant requires 2 HWIs to be adequately represented.
13265 We output CONST_DOUBLEs as blocks. */
13266 if (mode == VOIDmode
13267 || (GET_MODE (rtl) == VOIDmode
13268 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13269 break;
13270 type_die = base_type_for_mode (mode,
13271 GET_MODE_CLASS (mode) == MODE_INT);
13272 if (type_die == NULL)
13273 return NULL;
13274 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13275 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13276 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13277 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13278 #if TARGET_SUPPORTS_WIDE_INT == 0
13279 if (!SCALAR_FLOAT_MODE_P (mode))
13280 {
13281 mem_loc_result->dw_loc_oprnd2.val_class
13282 = dw_val_class_const_double;
13283 mem_loc_result->dw_loc_oprnd2.v.val_double
13284 = rtx_to_double_int (rtl);
13285 }
13286 else
13287 #endif
13288 {
13289 unsigned int length = GET_MODE_SIZE (mode);
13290 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13291
13292 insert_float (rtl, array);
13293 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13294 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13295 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13296 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13297 }
13298 }
13299 break;
13300
13301 case CONST_WIDE_INT:
13302 if (!dwarf_strict)
13303 {
13304 dw_die_ref type_die;
13305
13306 type_die = base_type_for_mode (mode,
13307 GET_MODE_CLASS (mode) == MODE_INT);
13308 if (type_die == NULL)
13309 return NULL;
13310 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13311 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13312 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13313 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13314 mem_loc_result->dw_loc_oprnd2.val_class
13315 = dw_val_class_wide_int;
13316 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13317 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13318 }
13319 break;
13320
13321 case EQ:
13322 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13323 break;
13324
13325 case GE:
13326 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13327 break;
13328
13329 case GT:
13330 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13331 break;
13332
13333 case LE:
13334 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13335 break;
13336
13337 case LT:
13338 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13339 break;
13340
13341 case NE:
13342 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13343 break;
13344
13345 case GEU:
13346 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13347 break;
13348
13349 case GTU:
13350 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13351 break;
13352
13353 case LEU:
13354 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13355 break;
13356
13357 case LTU:
13358 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13359 break;
13360
13361 case UMIN:
13362 case UMAX:
13363 if (GET_MODE_CLASS (mode) != MODE_INT)
13364 break;
13365 /* FALLTHRU */
13366 case SMIN:
13367 case SMAX:
13368 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13369 break;
13370
13371 case ZERO_EXTRACT:
13372 case SIGN_EXTRACT:
13373 if (CONST_INT_P (XEXP (rtl, 1))
13374 && CONST_INT_P (XEXP (rtl, 2))
13375 && ((unsigned) INTVAL (XEXP (rtl, 1))
13376 + (unsigned) INTVAL (XEXP (rtl, 2))
13377 <= GET_MODE_BITSIZE (mode))
13378 && GET_MODE_CLASS (mode) == MODE_INT
13379 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13380 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13381 {
13382 int shift, size;
13383 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13384 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13385 if (op0 == 0)
13386 break;
13387 if (GET_CODE (rtl) == SIGN_EXTRACT)
13388 op = DW_OP_shra;
13389 else
13390 op = DW_OP_shr;
13391 mem_loc_result = op0;
13392 size = INTVAL (XEXP (rtl, 1));
13393 shift = INTVAL (XEXP (rtl, 2));
13394 if (BITS_BIG_ENDIAN)
13395 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13396 - shift - size;
13397 if (shift + size != (int) DWARF2_ADDR_SIZE)
13398 {
13399 add_loc_descr (&mem_loc_result,
13400 int_loc_descriptor (DWARF2_ADDR_SIZE
13401 - shift - size));
13402 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13403 }
13404 if (size != (int) DWARF2_ADDR_SIZE)
13405 {
13406 add_loc_descr (&mem_loc_result,
13407 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13408 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13409 }
13410 }
13411 break;
13412
13413 case IF_THEN_ELSE:
13414 {
13415 dw_loc_descr_ref op2, bra_node, drop_node;
13416 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13417 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13418 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13419 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13420 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13421 VAR_INIT_STATUS_INITIALIZED);
13422 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13423 VAR_INIT_STATUS_INITIALIZED);
13424 if (op0 == NULL || op1 == NULL || op2 == NULL)
13425 break;
13426
13427 mem_loc_result = op1;
13428 add_loc_descr (&mem_loc_result, op2);
13429 add_loc_descr (&mem_loc_result, op0);
13430 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13431 add_loc_descr (&mem_loc_result, bra_node);
13432 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13433 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13434 add_loc_descr (&mem_loc_result, drop_node);
13435 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13436 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13437 }
13438 break;
13439
13440 case FLOAT_EXTEND:
13441 case FLOAT_TRUNCATE:
13442 case FLOAT:
13443 case UNSIGNED_FLOAT:
13444 case FIX:
13445 case UNSIGNED_FIX:
13446 if (!dwarf_strict)
13447 {
13448 dw_die_ref type_die;
13449 dw_loc_descr_ref cvt;
13450
13451 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13452 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13453 if (op0 == NULL)
13454 break;
13455 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13456 && (GET_CODE (rtl) == FLOAT
13457 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13458 <= DWARF2_ADDR_SIZE))
13459 {
13460 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13461 GET_CODE (rtl) == UNSIGNED_FLOAT);
13462 if (type_die == NULL)
13463 break;
13464 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13465 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13466 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13467 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13468 add_loc_descr (&op0, cvt);
13469 }
13470 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13471 if (type_die == NULL)
13472 break;
13473 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13474 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13475 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13476 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13477 add_loc_descr (&op0, cvt);
13478 if (GET_MODE_CLASS (mode) == MODE_INT
13479 && (GET_CODE (rtl) == FIX
13480 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13481 {
13482 op0 = convert_descriptor_to_mode (mode, op0);
13483 if (op0 == NULL)
13484 break;
13485 }
13486 mem_loc_result = op0;
13487 }
13488 break;
13489
13490 case CLZ:
13491 case CTZ:
13492 case FFS:
13493 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13494 break;
13495
13496 case POPCOUNT:
13497 case PARITY:
13498 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13499 break;
13500
13501 case BSWAP:
13502 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13503 break;
13504
13505 case ROTATE:
13506 case ROTATERT:
13507 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13508 break;
13509
13510 case COMPARE:
13511 /* In theory, we could implement the above. */
13512 /* DWARF cannot represent the unsigned compare operations
13513 natively. */
13514 case SS_MULT:
13515 case US_MULT:
13516 case SS_DIV:
13517 case US_DIV:
13518 case SS_PLUS:
13519 case US_PLUS:
13520 case SS_MINUS:
13521 case US_MINUS:
13522 case SS_NEG:
13523 case US_NEG:
13524 case SS_ABS:
13525 case SS_ASHIFT:
13526 case US_ASHIFT:
13527 case SS_TRUNCATE:
13528 case US_TRUNCATE:
13529 case UNORDERED:
13530 case ORDERED:
13531 case UNEQ:
13532 case UNGE:
13533 case UNGT:
13534 case UNLE:
13535 case UNLT:
13536 case LTGT:
13537 case FRACT_CONVERT:
13538 case UNSIGNED_FRACT_CONVERT:
13539 case SAT_FRACT:
13540 case UNSIGNED_SAT_FRACT:
13541 case SQRT:
13542 case ASM_OPERANDS:
13543 case VEC_MERGE:
13544 case VEC_SELECT:
13545 case VEC_CONCAT:
13546 case VEC_DUPLICATE:
13547 case UNSPEC:
13548 case HIGH:
13549 case FMA:
13550 case STRICT_LOW_PART:
13551 case CONST_VECTOR:
13552 case CONST_FIXED:
13553 case CLRSB:
13554 case CLOBBER:
13555 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13556 can't express it in the debug info. This can happen e.g. with some
13557 TLS UNSPECs. */
13558 break;
13559
13560 case CONST_STRING:
13561 resolve_one_addr (&rtl);
13562 goto symref;
13563
13564 default:
13565 if (flag_checking)
13566 {
13567 print_rtl (stderr, rtl);
13568 gcc_unreachable ();
13569 }
13570 break;
13571 }
13572
13573 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13574 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13575
13576 return mem_loc_result;
13577 }
13578
13579 /* Return a descriptor that describes the concatenation of two locations.
13580 This is typically a complex variable. */
13581
13582 static dw_loc_descr_ref
13583 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13584 {
13585 dw_loc_descr_ref cc_loc_result = NULL;
13586 dw_loc_descr_ref x0_ref
13587 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13588 dw_loc_descr_ref x1_ref
13589 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13590
13591 if (x0_ref == 0 || x1_ref == 0)
13592 return 0;
13593
13594 cc_loc_result = x0_ref;
13595 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13596
13597 add_loc_descr (&cc_loc_result, x1_ref);
13598 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13599
13600 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13601 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13602
13603 return cc_loc_result;
13604 }
13605
13606 /* Return a descriptor that describes the concatenation of N
13607 locations. */
13608
13609 static dw_loc_descr_ref
13610 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13611 {
13612 unsigned int i;
13613 dw_loc_descr_ref cc_loc_result = NULL;
13614 unsigned int n = XVECLEN (concatn, 0);
13615
13616 for (i = 0; i < n; ++i)
13617 {
13618 dw_loc_descr_ref ref;
13619 rtx x = XVECEXP (concatn, 0, i);
13620
13621 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13622 if (ref == NULL)
13623 return NULL;
13624
13625 add_loc_descr (&cc_loc_result, ref);
13626 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13627 }
13628
13629 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13630 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13631
13632 return cc_loc_result;
13633 }
13634
13635 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13636 for DEBUG_IMPLICIT_PTR RTL. */
13637
13638 static dw_loc_descr_ref
13639 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13640 {
13641 dw_loc_descr_ref ret;
13642 dw_die_ref ref;
13643
13644 if (dwarf_strict)
13645 return NULL;
13646 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13647 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13648 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13649 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13650 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13651 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13652 if (ref)
13653 {
13654 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13655 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13656 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13657 }
13658 else
13659 {
13660 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13661 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13662 }
13663 return ret;
13664 }
13665
13666 /* Output a proper Dwarf location descriptor for a variable or parameter
13667 which is either allocated in a register or in a memory location. For a
13668 register, we just generate an OP_REG and the register number. For a
13669 memory location we provide a Dwarf postfix expression describing how to
13670 generate the (dynamic) address of the object onto the address stack.
13671
13672 MODE is mode of the decl if this loc_descriptor is going to be used in
13673 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13674 allowed, VOIDmode otherwise.
13675
13676 If we don't know how to describe it, return 0. */
13677
13678 static dw_loc_descr_ref
13679 loc_descriptor (rtx rtl, machine_mode mode,
13680 enum var_init_status initialized)
13681 {
13682 dw_loc_descr_ref loc_result = NULL;
13683
13684 switch (GET_CODE (rtl))
13685 {
13686 case SUBREG:
13687 /* The case of a subreg may arise when we have a local (register)
13688 variable or a formal (register) parameter which doesn't quite fill
13689 up an entire register. For now, just assume that it is
13690 legitimate to make the Dwarf info refer to the whole register which
13691 contains the given subreg. */
13692 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13693 loc_result = loc_descriptor (SUBREG_REG (rtl),
13694 GET_MODE (SUBREG_REG (rtl)), initialized);
13695 else
13696 goto do_default;
13697 break;
13698
13699 case REG:
13700 loc_result = reg_loc_descriptor (rtl, initialized);
13701 break;
13702
13703 case MEM:
13704 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13705 GET_MODE (rtl), initialized);
13706 if (loc_result == NULL)
13707 loc_result = tls_mem_loc_descriptor (rtl);
13708 if (loc_result == NULL)
13709 {
13710 rtx new_rtl = avoid_constant_pool_reference (rtl);
13711 if (new_rtl != rtl)
13712 loc_result = loc_descriptor (new_rtl, mode, initialized);
13713 }
13714 break;
13715
13716 case CONCAT:
13717 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13718 initialized);
13719 break;
13720
13721 case CONCATN:
13722 loc_result = concatn_loc_descriptor (rtl, initialized);
13723 break;
13724
13725 case VAR_LOCATION:
13726 /* Single part. */
13727 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13728 {
13729 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13730 if (GET_CODE (loc) == EXPR_LIST)
13731 loc = XEXP (loc, 0);
13732 loc_result = loc_descriptor (loc, mode, initialized);
13733 break;
13734 }
13735
13736 rtl = XEXP (rtl, 1);
13737 /* FALLTHRU */
13738
13739 case PARALLEL:
13740 {
13741 rtvec par_elems = XVEC (rtl, 0);
13742 int num_elem = GET_NUM_ELEM (par_elems);
13743 machine_mode mode;
13744 int i;
13745
13746 /* Create the first one, so we have something to add to. */
13747 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13748 VOIDmode, initialized);
13749 if (loc_result == NULL)
13750 return NULL;
13751 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13752 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13753 for (i = 1; i < num_elem; i++)
13754 {
13755 dw_loc_descr_ref temp;
13756
13757 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13758 VOIDmode, initialized);
13759 if (temp == NULL)
13760 return NULL;
13761 add_loc_descr (&loc_result, temp);
13762 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13763 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13764 }
13765 }
13766 break;
13767
13768 case CONST_INT:
13769 if (mode != VOIDmode && mode != BLKmode)
13770 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13771 INTVAL (rtl));
13772 break;
13773
13774 case CONST_DOUBLE:
13775 if (mode == VOIDmode)
13776 mode = GET_MODE (rtl);
13777
13778 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13779 {
13780 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13781
13782 /* Note that a CONST_DOUBLE rtx could represent either an integer
13783 or a floating-point constant. A CONST_DOUBLE is used whenever
13784 the constant requires more than one word in order to be
13785 adequately represented. We output CONST_DOUBLEs as blocks. */
13786 loc_result = new_loc_descr (DW_OP_implicit_value,
13787 GET_MODE_SIZE (mode), 0);
13788 #if TARGET_SUPPORTS_WIDE_INT == 0
13789 if (!SCALAR_FLOAT_MODE_P (mode))
13790 {
13791 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13792 loc_result->dw_loc_oprnd2.v.val_double
13793 = rtx_to_double_int (rtl);
13794 }
13795 else
13796 #endif
13797 {
13798 unsigned int length = GET_MODE_SIZE (mode);
13799 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13800
13801 insert_float (rtl, array);
13802 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13803 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13804 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13805 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13806 }
13807 }
13808 break;
13809
13810 case CONST_WIDE_INT:
13811 if (mode == VOIDmode)
13812 mode = GET_MODE (rtl);
13813
13814 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13815 {
13816 loc_result = new_loc_descr (DW_OP_implicit_value,
13817 GET_MODE_SIZE (mode), 0);
13818 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13819 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13820 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13821 }
13822 break;
13823
13824 case CONST_VECTOR:
13825 if (mode == VOIDmode)
13826 mode = GET_MODE (rtl);
13827
13828 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13829 {
13830 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13831 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13832 unsigned char *array
13833 = ggc_vec_alloc<unsigned char> (length * elt_size);
13834 unsigned int i;
13835 unsigned char *p;
13836 machine_mode imode = GET_MODE_INNER (mode);
13837
13838 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13839 switch (GET_MODE_CLASS (mode))
13840 {
13841 case MODE_VECTOR_INT:
13842 for (i = 0, p = array; i < length; i++, p += elt_size)
13843 {
13844 rtx elt = CONST_VECTOR_ELT (rtl, i);
13845 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13846 }
13847 break;
13848
13849 case MODE_VECTOR_FLOAT:
13850 for (i = 0, p = array; i < length; i++, p += elt_size)
13851 {
13852 rtx elt = CONST_VECTOR_ELT (rtl, i);
13853 insert_float (elt, p);
13854 }
13855 break;
13856
13857 default:
13858 gcc_unreachable ();
13859 }
13860
13861 loc_result = new_loc_descr (DW_OP_implicit_value,
13862 length * elt_size, 0);
13863 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13864 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13865 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13866 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13867 }
13868 break;
13869
13870 case CONST:
13871 if (mode == VOIDmode
13872 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13873 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13874 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13875 {
13876 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13877 break;
13878 }
13879 /* FALLTHROUGH */
13880 case SYMBOL_REF:
13881 if (!const_ok_for_output (rtl))
13882 break;
13883 case LABEL_REF:
13884 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13885 && (dwarf_version >= 4 || !dwarf_strict))
13886 {
13887 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13888 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13889 vec_safe_push (used_rtx_array, rtl);
13890 }
13891 break;
13892
13893 case DEBUG_IMPLICIT_PTR:
13894 loc_result = implicit_ptr_descriptor (rtl, 0);
13895 break;
13896
13897 case PLUS:
13898 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13899 && CONST_INT_P (XEXP (rtl, 1)))
13900 {
13901 loc_result
13902 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13903 break;
13904 }
13905 /* FALLTHRU */
13906 do_default:
13907 default:
13908 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13909 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13910 && dwarf_version >= 4)
13911 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13912 {
13913 /* Value expression. */
13914 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13915 if (loc_result)
13916 add_loc_descr (&loc_result,
13917 new_loc_descr (DW_OP_stack_value, 0, 0));
13918 }
13919 break;
13920 }
13921
13922 return loc_result;
13923 }
13924
13925 /* We need to figure out what section we should use as the base for the
13926 address ranges where a given location is valid.
13927 1. If this particular DECL has a section associated with it, use that.
13928 2. If this function has a section associated with it, use that.
13929 3. Otherwise, use the text section.
13930 XXX: If you split a variable across multiple sections, we won't notice. */
13931
13932 static const char *
13933 secname_for_decl (const_tree decl)
13934 {
13935 const char *secname;
13936
13937 if (VAR_OR_FUNCTION_DECL_P (decl)
13938 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13939 && DECL_SECTION_NAME (decl))
13940 secname = DECL_SECTION_NAME (decl);
13941 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13942 secname = DECL_SECTION_NAME (current_function_decl);
13943 else if (cfun && in_cold_section_p)
13944 secname = crtl->subsections.cold_section_label;
13945 else
13946 secname = text_section_label;
13947
13948 return secname;
13949 }
13950
13951 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13952
13953 static bool
13954 decl_by_reference_p (tree decl)
13955 {
13956 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13957 || TREE_CODE (decl) == VAR_DECL)
13958 && DECL_BY_REFERENCE (decl));
13959 }
13960
13961 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13962 for VARLOC. */
13963
13964 static dw_loc_descr_ref
13965 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13966 enum var_init_status initialized)
13967 {
13968 int have_address = 0;
13969 dw_loc_descr_ref descr;
13970 machine_mode mode;
13971
13972 if (want_address != 2)
13973 {
13974 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13975 /* Single part. */
13976 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13977 {
13978 varloc = PAT_VAR_LOCATION_LOC (varloc);
13979 if (GET_CODE (varloc) == EXPR_LIST)
13980 varloc = XEXP (varloc, 0);
13981 mode = GET_MODE (varloc);
13982 if (MEM_P (varloc))
13983 {
13984 rtx addr = XEXP (varloc, 0);
13985 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13986 mode, initialized);
13987 if (descr)
13988 have_address = 1;
13989 else
13990 {
13991 rtx x = avoid_constant_pool_reference (varloc);
13992 if (x != varloc)
13993 descr = mem_loc_descriptor (x, mode, VOIDmode,
13994 initialized);
13995 }
13996 }
13997 else
13998 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13999 }
14000 else
14001 return 0;
14002 }
14003 else
14004 {
14005 if (GET_CODE (varloc) == VAR_LOCATION)
14006 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14007 else
14008 mode = DECL_MODE (loc);
14009 descr = loc_descriptor (varloc, mode, initialized);
14010 have_address = 1;
14011 }
14012
14013 if (!descr)
14014 return 0;
14015
14016 if (want_address == 2 && !have_address
14017 && (dwarf_version >= 4 || !dwarf_strict))
14018 {
14019 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14020 {
14021 expansion_failed (loc, NULL_RTX,
14022 "DWARF address size mismatch");
14023 return 0;
14024 }
14025 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14026 have_address = 1;
14027 }
14028 /* Show if we can't fill the request for an address. */
14029 if (want_address && !have_address)
14030 {
14031 expansion_failed (loc, NULL_RTX,
14032 "Want address and only have value");
14033 return 0;
14034 }
14035
14036 /* If we've got an address and don't want one, dereference. */
14037 if (!want_address && have_address)
14038 {
14039 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14040 enum dwarf_location_atom op;
14041
14042 if (size > DWARF2_ADDR_SIZE || size == -1)
14043 {
14044 expansion_failed (loc, NULL_RTX,
14045 "DWARF address size mismatch");
14046 return 0;
14047 }
14048 else if (size == DWARF2_ADDR_SIZE)
14049 op = DW_OP_deref;
14050 else
14051 op = DW_OP_deref_size;
14052
14053 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14054 }
14055
14056 return descr;
14057 }
14058
14059 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14060 if it is not possible. */
14061
14062 static dw_loc_descr_ref
14063 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14064 {
14065 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14066 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14067 else if (dwarf_version >= 3 || !dwarf_strict)
14068 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14069 else
14070 return NULL;
14071 }
14072
14073 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14074 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14075
14076 static dw_loc_descr_ref
14077 dw_sra_loc_expr (tree decl, rtx loc)
14078 {
14079 rtx p;
14080 unsigned HOST_WIDE_INT padsize = 0;
14081 dw_loc_descr_ref descr, *descr_tail;
14082 unsigned HOST_WIDE_INT decl_size;
14083 rtx varloc;
14084 enum var_init_status initialized;
14085
14086 if (DECL_SIZE (decl) == NULL
14087 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14088 return NULL;
14089
14090 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14091 descr = NULL;
14092 descr_tail = &descr;
14093
14094 for (p = loc; p; p = XEXP (p, 1))
14095 {
14096 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14097 rtx loc_note = *decl_piece_varloc_ptr (p);
14098 dw_loc_descr_ref cur_descr;
14099 dw_loc_descr_ref *tail, last = NULL;
14100 unsigned HOST_WIDE_INT opsize = 0;
14101
14102 if (loc_note == NULL_RTX
14103 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14104 {
14105 padsize += bitsize;
14106 continue;
14107 }
14108 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14109 varloc = NOTE_VAR_LOCATION (loc_note);
14110 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14111 if (cur_descr == NULL)
14112 {
14113 padsize += bitsize;
14114 continue;
14115 }
14116
14117 /* Check that cur_descr either doesn't use
14118 DW_OP_*piece operations, or their sum is equal
14119 to bitsize. Otherwise we can't embed it. */
14120 for (tail = &cur_descr; *tail != NULL;
14121 tail = &(*tail)->dw_loc_next)
14122 if ((*tail)->dw_loc_opc == DW_OP_piece)
14123 {
14124 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14125 * BITS_PER_UNIT;
14126 last = *tail;
14127 }
14128 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14129 {
14130 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14131 last = *tail;
14132 }
14133
14134 if (last != NULL && opsize != bitsize)
14135 {
14136 padsize += bitsize;
14137 /* Discard the current piece of the descriptor and release any
14138 addr_table entries it uses. */
14139 remove_loc_list_addr_table_entries (cur_descr);
14140 continue;
14141 }
14142
14143 /* If there is a hole, add DW_OP_*piece after empty DWARF
14144 expression, which means that those bits are optimized out. */
14145 if (padsize)
14146 {
14147 if (padsize > decl_size)
14148 {
14149 remove_loc_list_addr_table_entries (cur_descr);
14150 goto discard_descr;
14151 }
14152 decl_size -= padsize;
14153 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14154 if (*descr_tail == NULL)
14155 {
14156 remove_loc_list_addr_table_entries (cur_descr);
14157 goto discard_descr;
14158 }
14159 descr_tail = &(*descr_tail)->dw_loc_next;
14160 padsize = 0;
14161 }
14162 *descr_tail = cur_descr;
14163 descr_tail = tail;
14164 if (bitsize > decl_size)
14165 goto discard_descr;
14166 decl_size -= bitsize;
14167 if (last == NULL)
14168 {
14169 HOST_WIDE_INT offset = 0;
14170 if (GET_CODE (varloc) == VAR_LOCATION
14171 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14172 {
14173 varloc = PAT_VAR_LOCATION_LOC (varloc);
14174 if (GET_CODE (varloc) == EXPR_LIST)
14175 varloc = XEXP (varloc, 0);
14176 }
14177 do
14178 {
14179 if (GET_CODE (varloc) == CONST
14180 || GET_CODE (varloc) == SIGN_EXTEND
14181 || GET_CODE (varloc) == ZERO_EXTEND)
14182 varloc = XEXP (varloc, 0);
14183 else if (GET_CODE (varloc) == SUBREG)
14184 varloc = SUBREG_REG (varloc);
14185 else
14186 break;
14187 }
14188 while (1);
14189 /* DW_OP_bit_size offset should be zero for register
14190 or implicit location descriptions and empty location
14191 descriptions, but for memory addresses needs big endian
14192 adjustment. */
14193 if (MEM_P (varloc))
14194 {
14195 unsigned HOST_WIDE_INT memsize
14196 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14197 if (memsize != bitsize)
14198 {
14199 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14200 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14201 goto discard_descr;
14202 if (memsize < bitsize)
14203 goto discard_descr;
14204 if (BITS_BIG_ENDIAN)
14205 offset = memsize - bitsize;
14206 }
14207 }
14208
14209 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14210 if (*descr_tail == NULL)
14211 goto discard_descr;
14212 descr_tail = &(*descr_tail)->dw_loc_next;
14213 }
14214 }
14215
14216 /* If there were any non-empty expressions, add padding till the end of
14217 the decl. */
14218 if (descr != NULL && decl_size != 0)
14219 {
14220 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14221 if (*descr_tail == NULL)
14222 goto discard_descr;
14223 }
14224 return descr;
14225
14226 discard_descr:
14227 /* Discard the descriptor and release any addr_table entries it uses. */
14228 remove_loc_list_addr_table_entries (descr);
14229 return NULL;
14230 }
14231
14232 /* Return the dwarf representation of the location list LOC_LIST of
14233 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14234 function. */
14235
14236 static dw_loc_list_ref
14237 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14238 {
14239 const char *endname, *secname;
14240 rtx varloc;
14241 enum var_init_status initialized;
14242 struct var_loc_node *node;
14243 dw_loc_descr_ref descr;
14244 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14245 dw_loc_list_ref list = NULL;
14246 dw_loc_list_ref *listp = &list;
14247
14248 /* Now that we know what section we are using for a base,
14249 actually construct the list of locations.
14250 The first location information is what is passed to the
14251 function that creates the location list, and the remaining
14252 locations just get added on to that list.
14253 Note that we only know the start address for a location
14254 (IE location changes), so to build the range, we use
14255 the range [current location start, next location start].
14256 This means we have to special case the last node, and generate
14257 a range of [last location start, end of function label]. */
14258
14259 secname = secname_for_decl (decl);
14260
14261 for (node = loc_list->first; node; node = node->next)
14262 if (GET_CODE (node->loc) == EXPR_LIST
14263 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14264 {
14265 if (GET_CODE (node->loc) == EXPR_LIST)
14266 {
14267 /* This requires DW_OP_{,bit_}piece, which is not usable
14268 inside DWARF expressions. */
14269 if (want_address != 2)
14270 continue;
14271 descr = dw_sra_loc_expr (decl, node->loc);
14272 if (descr == NULL)
14273 continue;
14274 }
14275 else
14276 {
14277 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14278 varloc = NOTE_VAR_LOCATION (node->loc);
14279 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14280 }
14281 if (descr)
14282 {
14283 bool range_across_switch = false;
14284 /* If section switch happens in between node->label
14285 and node->next->label (or end of function) and
14286 we can't emit it as a single entry list,
14287 emit two ranges, first one ending at the end
14288 of first partition and second one starting at the
14289 beginning of second partition. */
14290 if (node == loc_list->last_before_switch
14291 && (node != loc_list->first || loc_list->first->next)
14292 && current_function_decl)
14293 {
14294 endname = cfun->fde->dw_fde_end;
14295 range_across_switch = true;
14296 }
14297 /* The variable has a location between NODE->LABEL and
14298 NODE->NEXT->LABEL. */
14299 else if (node->next)
14300 endname = node->next->label;
14301 /* If the variable has a location at the last label
14302 it keeps its location until the end of function. */
14303 else if (!current_function_decl)
14304 endname = text_end_label;
14305 else
14306 {
14307 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14308 current_function_funcdef_no);
14309 endname = ggc_strdup (label_id);
14310 }
14311
14312 *listp = new_loc_list (descr, node->label, endname, secname);
14313 if (TREE_CODE (decl) == PARM_DECL
14314 && node == loc_list->first
14315 && NOTE_P (node->loc)
14316 && strcmp (node->label, endname) == 0)
14317 (*listp)->force = true;
14318 listp = &(*listp)->dw_loc_next;
14319
14320 if (range_across_switch)
14321 {
14322 if (GET_CODE (node->loc) == EXPR_LIST)
14323 descr = dw_sra_loc_expr (decl, node->loc);
14324 else
14325 {
14326 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14327 varloc = NOTE_VAR_LOCATION (node->loc);
14328 descr = dw_loc_list_1 (decl, varloc, want_address,
14329 initialized);
14330 }
14331 gcc_assert (descr);
14332 /* The variable has a location between NODE->LABEL and
14333 NODE->NEXT->LABEL. */
14334 if (node->next)
14335 endname = node->next->label;
14336 else
14337 endname = cfun->fde->dw_fde_second_end;
14338 *listp = new_loc_list (descr,
14339 cfun->fde->dw_fde_second_begin,
14340 endname, secname);
14341 listp = &(*listp)->dw_loc_next;
14342 }
14343 }
14344 }
14345
14346 /* Try to avoid the overhead of a location list emitting a location
14347 expression instead, but only if we didn't have more than one
14348 location entry in the first place. If some entries were not
14349 representable, we don't want to pretend a single entry that was
14350 applies to the entire scope in which the variable is
14351 available. */
14352 if (list && loc_list->first->next)
14353 gen_llsym (list);
14354
14355 return list;
14356 }
14357
14358 /* Return if the loc_list has only single element and thus can be represented
14359 as location description. */
14360
14361 static bool
14362 single_element_loc_list_p (dw_loc_list_ref list)
14363 {
14364 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14365 return !list->ll_symbol;
14366 }
14367
14368 /* To each location in list LIST add loc descr REF. */
14369
14370 static void
14371 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14372 {
14373 dw_loc_descr_ref copy;
14374 add_loc_descr (&list->expr, ref);
14375 list = list->dw_loc_next;
14376 while (list)
14377 {
14378 copy = ggc_alloc<dw_loc_descr_node> ();
14379 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14380 add_loc_descr (&list->expr, copy);
14381 while (copy->dw_loc_next)
14382 {
14383 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14384 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14385 copy->dw_loc_next = new_copy;
14386 copy = new_copy;
14387 }
14388 list = list->dw_loc_next;
14389 }
14390 }
14391
14392 /* Given two lists RET and LIST
14393 produce location list that is result of adding expression in LIST
14394 to expression in RET on each position in program.
14395 Might be destructive on both RET and LIST.
14396
14397 TODO: We handle only simple cases of RET or LIST having at most one
14398 element. General case would inolve sorting the lists in program order
14399 and merging them that will need some additional work.
14400 Adding that will improve quality of debug info especially for SRA-ed
14401 structures. */
14402
14403 static void
14404 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14405 {
14406 if (!list)
14407 return;
14408 if (!*ret)
14409 {
14410 *ret = list;
14411 return;
14412 }
14413 if (!list->dw_loc_next)
14414 {
14415 add_loc_descr_to_each (*ret, list->expr);
14416 return;
14417 }
14418 if (!(*ret)->dw_loc_next)
14419 {
14420 add_loc_descr_to_each (list, (*ret)->expr);
14421 *ret = list;
14422 return;
14423 }
14424 expansion_failed (NULL_TREE, NULL_RTX,
14425 "Don't know how to merge two non-trivial"
14426 " location lists.\n");
14427 *ret = NULL;
14428 return;
14429 }
14430
14431 /* LOC is constant expression. Try a luck, look it up in constant
14432 pool and return its loc_descr of its address. */
14433
14434 static dw_loc_descr_ref
14435 cst_pool_loc_descr (tree loc)
14436 {
14437 /* Get an RTL for this, if something has been emitted. */
14438 rtx rtl = lookup_constant_def (loc);
14439
14440 if (!rtl || !MEM_P (rtl))
14441 {
14442 gcc_assert (!rtl);
14443 return 0;
14444 }
14445 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14446
14447 /* TODO: We might get more coverage if we was actually delaying expansion
14448 of all expressions till end of compilation when constant pools are fully
14449 populated. */
14450 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14451 {
14452 expansion_failed (loc, NULL_RTX,
14453 "CST value in contant pool but not marked.");
14454 return 0;
14455 }
14456 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14457 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14458 }
14459
14460 /* Return dw_loc_list representing address of addr_expr LOC
14461 by looking for inner INDIRECT_REF expression and turning
14462 it into simple arithmetics.
14463
14464 See loc_list_from_tree for the meaning of CONTEXT. */
14465
14466 static dw_loc_list_ref
14467 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14468 const loc_descr_context *context)
14469 {
14470 tree obj, offset;
14471 HOST_WIDE_INT bitsize, bitpos, bytepos;
14472 machine_mode mode;
14473 int unsignedp, volatilep = 0;
14474 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14475
14476 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14477 &bitsize, &bitpos, &offset, &mode,
14478 &unsignedp, &volatilep, false);
14479 STRIP_NOPS (obj);
14480 if (bitpos % BITS_PER_UNIT)
14481 {
14482 expansion_failed (loc, NULL_RTX, "bitfield access");
14483 return 0;
14484 }
14485 if (!INDIRECT_REF_P (obj))
14486 {
14487 expansion_failed (obj,
14488 NULL_RTX, "no indirect ref in inner refrence");
14489 return 0;
14490 }
14491 if (!offset && !bitpos)
14492 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14493 context);
14494 else if (toplev
14495 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14496 && (dwarf_version >= 4 || !dwarf_strict))
14497 {
14498 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14499 if (!list_ret)
14500 return 0;
14501 if (offset)
14502 {
14503 /* Variable offset. */
14504 list_ret1 = loc_list_from_tree (offset, 0, context);
14505 if (list_ret1 == 0)
14506 return 0;
14507 add_loc_list (&list_ret, list_ret1);
14508 if (!list_ret)
14509 return 0;
14510 add_loc_descr_to_each (list_ret,
14511 new_loc_descr (DW_OP_plus, 0, 0));
14512 }
14513 bytepos = bitpos / BITS_PER_UNIT;
14514 if (bytepos > 0)
14515 add_loc_descr_to_each (list_ret,
14516 new_loc_descr (DW_OP_plus_uconst,
14517 bytepos, 0));
14518 else if (bytepos < 0)
14519 loc_list_plus_const (list_ret, bytepos);
14520 add_loc_descr_to_each (list_ret,
14521 new_loc_descr (DW_OP_stack_value, 0, 0));
14522 }
14523 return list_ret;
14524 }
14525
14526
14527 /* Helper structure for location descriptions generation. */
14528 struct loc_descr_context
14529 {
14530 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14531 NULL_TREE if DW_OP_push_object_address in invalid for this location
14532 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14533 tree context_type;
14534 /* The ..._DECL node that should be translated as a
14535 DW_OP_push_object_address operation. */
14536 tree base_decl;
14537 };
14538
14539 /* Generate Dwarf location list representing LOC.
14540 If WANT_ADDRESS is false, expression computing LOC will be computed
14541 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14542 if WANT_ADDRESS is 2, expression computing address useable in location
14543 will be returned (i.e. DW_OP_reg can be used
14544 to refer to register values).
14545
14546 CONTEXT provides information to customize the location descriptions
14547 generation. Its context_type field specifies what type is implicitly
14548 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14549 will not be generated.
14550
14551 If CONTEXT is NULL, the behavior is the same as if both context_type and
14552 base_decl fields were NULL_TREE. */
14553
14554 static dw_loc_list_ref
14555 loc_list_from_tree (tree loc, int want_address,
14556 const struct loc_descr_context *context)
14557 {
14558 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14559 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14560 int have_address = 0;
14561 enum dwarf_location_atom op;
14562
14563 /* ??? Most of the time we do not take proper care for sign/zero
14564 extending the values properly. Hopefully this won't be a real
14565 problem... */
14566
14567 if (context != NULL
14568 && context->base_decl == loc
14569 && want_address == 0)
14570 {
14571 if (dwarf_version >= 3 || !dwarf_strict)
14572 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14573 NULL, NULL, NULL);
14574 else
14575 return NULL;
14576 }
14577
14578 switch (TREE_CODE (loc))
14579 {
14580 case ERROR_MARK:
14581 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14582 return 0;
14583
14584 case PLACEHOLDER_EXPR:
14585 /* This case involves extracting fields from an object to determine the
14586 position of other fields. It is supposed to appear only as the first
14587 operand of COMPONENT_REF nodes and to reference precisely the type
14588 that the context allows. */
14589 if (context != NULL
14590 && TREE_TYPE (loc) == context->context_type
14591 && want_address >= 1)
14592 {
14593 if (dwarf_version >= 3 || !dwarf_strict)
14594 {
14595 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14596 have_address = 1;
14597 break;
14598 }
14599 else
14600 return NULL;
14601 }
14602 else
14603 expansion_failed (loc, NULL_RTX,
14604 "PLACEHOLDER_EXPR for an unexpected type");
14605 break;
14606
14607 case CALL_EXPR:
14608 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14609 /* There are no opcodes for these operations. */
14610 return 0;
14611
14612 case PREINCREMENT_EXPR:
14613 case PREDECREMENT_EXPR:
14614 case POSTINCREMENT_EXPR:
14615 case POSTDECREMENT_EXPR:
14616 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14617 /* There are no opcodes for these operations. */
14618 return 0;
14619
14620 case ADDR_EXPR:
14621 /* If we already want an address, see if there is INDIRECT_REF inside
14622 e.g. for &this->field. */
14623 if (want_address)
14624 {
14625 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14626 (loc, want_address == 2, context);
14627 if (list_ret)
14628 have_address = 1;
14629 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14630 && (ret = cst_pool_loc_descr (loc)))
14631 have_address = 1;
14632 }
14633 /* Otherwise, process the argument and look for the address. */
14634 if (!list_ret && !ret)
14635 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14636 else
14637 {
14638 if (want_address)
14639 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14640 return NULL;
14641 }
14642 break;
14643
14644 case VAR_DECL:
14645 if (DECL_THREAD_LOCAL_P (loc))
14646 {
14647 rtx rtl;
14648 enum dwarf_location_atom tls_op;
14649 enum dtprel_bool dtprel = dtprel_false;
14650
14651 if (targetm.have_tls)
14652 {
14653 /* If this is not defined, we have no way to emit the
14654 data. */
14655 if (!targetm.asm_out.output_dwarf_dtprel)
14656 return 0;
14657
14658 /* The way DW_OP_GNU_push_tls_address is specified, we
14659 can only look up addresses of objects in the current
14660 module. We used DW_OP_addr as first op, but that's
14661 wrong, because DW_OP_addr is relocated by the debug
14662 info consumer, while DW_OP_GNU_push_tls_address
14663 operand shouldn't be. */
14664 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14665 return 0;
14666 dtprel = dtprel_true;
14667 tls_op = DW_OP_GNU_push_tls_address;
14668 }
14669 else
14670 {
14671 if (!targetm.emutls.debug_form_tls_address
14672 || !(dwarf_version >= 3 || !dwarf_strict))
14673 return 0;
14674 /* We stuffed the control variable into the DECL_VALUE_EXPR
14675 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14676 no longer appear in gimple code. We used the control
14677 variable in specific so that we could pick it up here. */
14678 loc = DECL_VALUE_EXPR (loc);
14679 tls_op = DW_OP_form_tls_address;
14680 }
14681
14682 rtl = rtl_for_decl_location (loc);
14683 if (rtl == NULL_RTX)
14684 return 0;
14685
14686 if (!MEM_P (rtl))
14687 return 0;
14688 rtl = XEXP (rtl, 0);
14689 if (! CONSTANT_P (rtl))
14690 return 0;
14691
14692 ret = new_addr_loc_descr (rtl, dtprel);
14693 ret1 = new_loc_descr (tls_op, 0, 0);
14694 add_loc_descr (&ret, ret1);
14695
14696 have_address = 1;
14697 break;
14698 }
14699 /* FALLTHRU */
14700
14701 case PARM_DECL:
14702 case RESULT_DECL:
14703 if (DECL_HAS_VALUE_EXPR_P (loc))
14704 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14705 want_address, context);
14706 /* FALLTHRU */
14707
14708 case FUNCTION_DECL:
14709 {
14710 rtx rtl;
14711 var_loc_list *loc_list = lookup_decl_loc (loc);
14712
14713 if (loc_list && loc_list->first)
14714 {
14715 list_ret = dw_loc_list (loc_list, loc, want_address);
14716 have_address = want_address != 0;
14717 break;
14718 }
14719 rtl = rtl_for_decl_location (loc);
14720 if (rtl == NULL_RTX)
14721 {
14722 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14723 return 0;
14724 }
14725 else if (CONST_INT_P (rtl))
14726 {
14727 HOST_WIDE_INT val = INTVAL (rtl);
14728 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14729 val &= GET_MODE_MASK (DECL_MODE (loc));
14730 ret = int_loc_descriptor (val);
14731 }
14732 else if (GET_CODE (rtl) == CONST_STRING)
14733 {
14734 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14735 return 0;
14736 }
14737 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14738 ret = new_addr_loc_descr (rtl, dtprel_false);
14739 else
14740 {
14741 machine_mode mode, mem_mode;
14742
14743 /* Certain constructs can only be represented at top-level. */
14744 if (want_address == 2)
14745 {
14746 ret = loc_descriptor (rtl, VOIDmode,
14747 VAR_INIT_STATUS_INITIALIZED);
14748 have_address = 1;
14749 }
14750 else
14751 {
14752 mode = GET_MODE (rtl);
14753 mem_mode = VOIDmode;
14754 if (MEM_P (rtl))
14755 {
14756 mem_mode = mode;
14757 mode = get_address_mode (rtl);
14758 rtl = XEXP (rtl, 0);
14759 have_address = 1;
14760 }
14761 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14762 VAR_INIT_STATUS_INITIALIZED);
14763 }
14764 if (!ret)
14765 expansion_failed (loc, rtl,
14766 "failed to produce loc descriptor for rtl");
14767 }
14768 }
14769 break;
14770
14771 case MEM_REF:
14772 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14773 {
14774 have_address = 1;
14775 goto do_plus;
14776 }
14777 /* Fallthru. */
14778 case INDIRECT_REF:
14779 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14780 have_address = 1;
14781 break;
14782
14783 case TARGET_MEM_REF:
14784 case SSA_NAME:
14785 case DEBUG_EXPR_DECL:
14786 return NULL;
14787
14788 case COMPOUND_EXPR:
14789 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14790
14791 CASE_CONVERT:
14792 case VIEW_CONVERT_EXPR:
14793 case SAVE_EXPR:
14794 case MODIFY_EXPR:
14795 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14796
14797 case COMPONENT_REF:
14798 case BIT_FIELD_REF:
14799 case ARRAY_REF:
14800 case ARRAY_RANGE_REF:
14801 case REALPART_EXPR:
14802 case IMAGPART_EXPR:
14803 {
14804 tree obj, offset;
14805 HOST_WIDE_INT bitsize, bitpos, bytepos;
14806 machine_mode mode;
14807 int unsignedp, volatilep = 0;
14808
14809 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14810 &unsignedp, &volatilep, false);
14811
14812 gcc_assert (obj != loc);
14813
14814 list_ret = loc_list_from_tree (obj,
14815 want_address == 2
14816 && !bitpos && !offset ? 2 : 1,
14817 context);
14818 /* TODO: We can extract value of the small expression via shifting even
14819 for nonzero bitpos. */
14820 if (list_ret == 0)
14821 return 0;
14822 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14823 {
14824 expansion_failed (loc, NULL_RTX,
14825 "bitfield access");
14826 return 0;
14827 }
14828
14829 if (offset != NULL_TREE)
14830 {
14831 /* Variable offset. */
14832 list_ret1 = loc_list_from_tree (offset, 0, context);
14833 if (list_ret1 == 0)
14834 return 0;
14835 add_loc_list (&list_ret, list_ret1);
14836 if (!list_ret)
14837 return 0;
14838 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14839 }
14840
14841 bytepos = bitpos / BITS_PER_UNIT;
14842 if (bytepos > 0)
14843 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14844 else if (bytepos < 0)
14845 loc_list_plus_const (list_ret, bytepos);
14846
14847 have_address = 1;
14848 break;
14849 }
14850
14851 case INTEGER_CST:
14852 if ((want_address || !tree_fits_shwi_p (loc))
14853 && (ret = cst_pool_loc_descr (loc)))
14854 have_address = 1;
14855 else if (want_address == 2
14856 && tree_fits_shwi_p (loc)
14857 && (ret = address_of_int_loc_descriptor
14858 (int_size_in_bytes (TREE_TYPE (loc)),
14859 tree_to_shwi (loc))))
14860 have_address = 1;
14861 else if (tree_fits_shwi_p (loc))
14862 ret = int_loc_descriptor (tree_to_shwi (loc));
14863 else
14864 {
14865 expansion_failed (loc, NULL_RTX,
14866 "Integer operand is not host integer");
14867 return 0;
14868 }
14869 break;
14870
14871 case CONSTRUCTOR:
14872 case REAL_CST:
14873 case STRING_CST:
14874 case COMPLEX_CST:
14875 if ((ret = cst_pool_loc_descr (loc)))
14876 have_address = 1;
14877 else
14878 /* We can construct small constants here using int_loc_descriptor. */
14879 expansion_failed (loc, NULL_RTX,
14880 "constructor or constant not in constant pool");
14881 break;
14882
14883 case TRUTH_AND_EXPR:
14884 case TRUTH_ANDIF_EXPR:
14885 case BIT_AND_EXPR:
14886 op = DW_OP_and;
14887 goto do_binop;
14888
14889 case TRUTH_XOR_EXPR:
14890 case BIT_XOR_EXPR:
14891 op = DW_OP_xor;
14892 goto do_binop;
14893
14894 case TRUTH_OR_EXPR:
14895 case TRUTH_ORIF_EXPR:
14896 case BIT_IOR_EXPR:
14897 op = DW_OP_or;
14898 goto do_binop;
14899
14900 case FLOOR_DIV_EXPR:
14901 case CEIL_DIV_EXPR:
14902 case ROUND_DIV_EXPR:
14903 case TRUNC_DIV_EXPR:
14904 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14905 return 0;
14906 op = DW_OP_div;
14907 goto do_binop;
14908
14909 case MINUS_EXPR:
14910 op = DW_OP_minus;
14911 goto do_binop;
14912
14913 case FLOOR_MOD_EXPR:
14914 case CEIL_MOD_EXPR:
14915 case ROUND_MOD_EXPR:
14916 case TRUNC_MOD_EXPR:
14917 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14918 {
14919 op = DW_OP_mod;
14920 goto do_binop;
14921 }
14922 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14923 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14924 if (list_ret == 0 || list_ret1 == 0)
14925 return 0;
14926
14927 add_loc_list (&list_ret, list_ret1);
14928 if (list_ret == 0)
14929 return 0;
14930 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14931 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14932 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14933 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14934 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14935 break;
14936
14937 case MULT_EXPR:
14938 op = DW_OP_mul;
14939 goto do_binop;
14940
14941 case LSHIFT_EXPR:
14942 op = DW_OP_shl;
14943 goto do_binop;
14944
14945 case RSHIFT_EXPR:
14946 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14947 goto do_binop;
14948
14949 case POINTER_PLUS_EXPR:
14950 case PLUS_EXPR:
14951 do_plus:
14952 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14953 {
14954 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14955 if (list_ret == 0)
14956 return 0;
14957
14958 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14959 break;
14960 }
14961
14962 op = DW_OP_plus;
14963 goto do_binop;
14964
14965 case LE_EXPR:
14966 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14967 return 0;
14968
14969 op = DW_OP_le;
14970 goto do_binop;
14971
14972 case GE_EXPR:
14973 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14974 return 0;
14975
14976 op = DW_OP_ge;
14977 goto do_binop;
14978
14979 case LT_EXPR:
14980 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14981 return 0;
14982
14983 op = DW_OP_lt;
14984 goto do_binop;
14985
14986 case GT_EXPR:
14987 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14988 return 0;
14989
14990 op = DW_OP_gt;
14991 goto do_binop;
14992
14993 case EQ_EXPR:
14994 op = DW_OP_eq;
14995 goto do_binop;
14996
14997 case NE_EXPR:
14998 op = DW_OP_ne;
14999 goto do_binop;
15000
15001 do_binop:
15002 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15003 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
15004 if (list_ret == 0 || list_ret1 == 0)
15005 return 0;
15006
15007 add_loc_list (&list_ret, list_ret1);
15008 if (list_ret == 0)
15009 return 0;
15010 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15011 break;
15012
15013 case TRUTH_NOT_EXPR:
15014 case BIT_NOT_EXPR:
15015 op = DW_OP_not;
15016 goto do_unop;
15017
15018 case ABS_EXPR:
15019 op = DW_OP_abs;
15020 goto do_unop;
15021
15022 case NEGATE_EXPR:
15023 op = DW_OP_neg;
15024 goto do_unop;
15025
15026 do_unop:
15027 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15028 if (list_ret == 0)
15029 return 0;
15030
15031 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15032 break;
15033
15034 case MIN_EXPR:
15035 case MAX_EXPR:
15036 {
15037 const enum tree_code code =
15038 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15039
15040 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15041 build2 (code, integer_type_node,
15042 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15043 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15044 }
15045
15046 /* ... fall through ... */
15047
15048 case COND_EXPR:
15049 {
15050 dw_loc_descr_ref lhs
15051 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15052 dw_loc_list_ref rhs
15053 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15054 dw_loc_descr_ref bra_node, jump_node, tmp;
15055
15056 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15057 if (list_ret == 0 || lhs == 0 || rhs == 0)
15058 return 0;
15059
15060 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15061 add_loc_descr_to_each (list_ret, bra_node);
15062
15063 add_loc_list (&list_ret, rhs);
15064 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15065 add_loc_descr_to_each (list_ret, jump_node);
15066
15067 add_loc_descr_to_each (list_ret, lhs);
15068 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15069 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15070
15071 /* ??? Need a node to point the skip at. Use a nop. */
15072 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15073 add_loc_descr_to_each (list_ret, tmp);
15074 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15075 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15076 }
15077 break;
15078
15079 case FIX_TRUNC_EXPR:
15080 return 0;
15081
15082 default:
15083 /* Leave front-end specific codes as simply unknown. This comes
15084 up, for instance, with the C STMT_EXPR. */
15085 if ((unsigned int) TREE_CODE (loc)
15086 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15087 {
15088 expansion_failed (loc, NULL_RTX,
15089 "language specific tree node");
15090 return 0;
15091 }
15092
15093 /* Otherwise this is a generic code; we should just lists all of
15094 these explicitly. We forgot one. */
15095 if (flag_checking)
15096 gcc_unreachable ();
15097
15098 /* In a release build, we want to degrade gracefully: better to
15099 generate incomplete debugging information than to crash. */
15100 return NULL;
15101 }
15102
15103 if (!ret && !list_ret)
15104 return 0;
15105
15106 if (want_address == 2 && !have_address
15107 && (dwarf_version >= 4 || !dwarf_strict))
15108 {
15109 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15110 {
15111 expansion_failed (loc, NULL_RTX,
15112 "DWARF address size mismatch");
15113 return 0;
15114 }
15115 if (ret)
15116 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15117 else
15118 add_loc_descr_to_each (list_ret,
15119 new_loc_descr (DW_OP_stack_value, 0, 0));
15120 have_address = 1;
15121 }
15122 /* Show if we can't fill the request for an address. */
15123 if (want_address && !have_address)
15124 {
15125 expansion_failed (loc, NULL_RTX,
15126 "Want address and only have value");
15127 return 0;
15128 }
15129
15130 gcc_assert (!ret || !list_ret);
15131
15132 /* If we've got an address and don't want one, dereference. */
15133 if (!want_address && have_address)
15134 {
15135 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15136
15137 if (size > DWARF2_ADDR_SIZE || size == -1)
15138 {
15139 expansion_failed (loc, NULL_RTX,
15140 "DWARF address size mismatch");
15141 return 0;
15142 }
15143 else if (size == DWARF2_ADDR_SIZE)
15144 op = DW_OP_deref;
15145 else
15146 op = DW_OP_deref_size;
15147
15148 if (ret)
15149 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15150 else
15151 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15152 }
15153 if (ret)
15154 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15155
15156 return list_ret;
15157 }
15158
15159 /* Same as above but return only single location expression. */
15160 static dw_loc_descr_ref
15161 loc_descriptor_from_tree (tree loc, int want_address,
15162 const struct loc_descr_context *context)
15163 {
15164 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15165 if (!ret)
15166 return NULL;
15167 if (ret->dw_loc_next)
15168 {
15169 expansion_failed (loc, NULL_RTX,
15170 "Location list where only loc descriptor needed");
15171 return NULL;
15172 }
15173 return ret->expr;
15174 }
15175
15176 /* Given a value, round it up to the lowest multiple of `boundary'
15177 which is not less than the value itself. */
15178
15179 static inline HOST_WIDE_INT
15180 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15181 {
15182 return (((value + boundary - 1) / boundary) * boundary);
15183 }
15184
15185 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15186 pointer to the declared type for the relevant field variable, or return
15187 `integer_type_node' if the given node turns out to be an
15188 ERROR_MARK node. */
15189
15190 static inline tree
15191 field_type (const_tree decl)
15192 {
15193 tree type;
15194
15195 if (TREE_CODE (decl) == ERROR_MARK)
15196 return integer_type_node;
15197
15198 type = DECL_BIT_FIELD_TYPE (decl);
15199 if (type == NULL_TREE)
15200 type = TREE_TYPE (decl);
15201
15202 return type;
15203 }
15204
15205 /* Given a pointer to a tree node, return the alignment in bits for
15206 it, or else return BITS_PER_WORD if the node actually turns out to
15207 be an ERROR_MARK node. */
15208
15209 static inline unsigned
15210 simple_type_align_in_bits (const_tree type)
15211 {
15212 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15213 }
15214
15215 static inline unsigned
15216 simple_decl_align_in_bits (const_tree decl)
15217 {
15218 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15219 }
15220
15221 /* Return the result of rounding T up to ALIGN. */
15222
15223 static inline offset_int
15224 round_up_to_align (const offset_int &t, unsigned int align)
15225 {
15226 return wi::udiv_trunc (t + align - 1, align) * align;
15227 }
15228
15229 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15230 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15231 or return 0 if we are unable to determine what that offset is, either
15232 because the argument turns out to be a pointer to an ERROR_MARK node, or
15233 because the offset is actually variable. (We can't handle the latter case
15234 just yet). */
15235
15236 static HOST_WIDE_INT
15237 field_byte_offset (const_tree decl)
15238 {
15239 offset_int object_offset_in_bits;
15240 offset_int object_offset_in_bytes;
15241 offset_int bitpos_int;
15242
15243 if (TREE_CODE (decl) == ERROR_MARK)
15244 return 0;
15245
15246 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15247
15248 /* We cannot yet cope with fields whose positions are variable, so
15249 for now, when we see such things, we simply return 0. Someday, we may
15250 be able to handle such cases, but it will be damn difficult. */
15251 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15252 return 0;
15253
15254 bitpos_int = wi::to_offset (bit_position (decl));
15255
15256 if (PCC_BITFIELD_TYPE_MATTERS)
15257 {
15258 tree type;
15259 tree field_size_tree;
15260 offset_int deepest_bitpos;
15261 offset_int field_size_in_bits;
15262 unsigned int type_align_in_bits;
15263 unsigned int decl_align_in_bits;
15264 offset_int type_size_in_bits;
15265
15266 type = field_type (decl);
15267 type_size_in_bits = offset_int_type_size_in_bits (type);
15268 type_align_in_bits = simple_type_align_in_bits (type);
15269
15270 field_size_tree = DECL_SIZE (decl);
15271
15272 /* The size could be unspecified if there was an error, or for
15273 a flexible array member. */
15274 if (!field_size_tree)
15275 field_size_tree = bitsize_zero_node;
15276
15277 /* If the size of the field is not constant, use the type size. */
15278 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15279 field_size_in_bits = wi::to_offset (field_size_tree);
15280 else
15281 field_size_in_bits = type_size_in_bits;
15282
15283 decl_align_in_bits = simple_decl_align_in_bits (decl);
15284
15285 /* The GCC front-end doesn't make any attempt to keep track of the
15286 starting bit offset (relative to the start of the containing
15287 structure type) of the hypothetical "containing object" for a
15288 bit-field. Thus, when computing the byte offset value for the
15289 start of the "containing object" of a bit-field, we must deduce
15290 this information on our own. This can be rather tricky to do in
15291 some cases. For example, handling the following structure type
15292 definition when compiling for an i386/i486 target (which only
15293 aligns long long's to 32-bit boundaries) can be very tricky:
15294
15295 struct S { int field1; long long field2:31; };
15296
15297 Fortunately, there is a simple rule-of-thumb which can be used
15298 in such cases. When compiling for an i386/i486, GCC will
15299 allocate 8 bytes for the structure shown above. It decides to
15300 do this based upon one simple rule for bit-field allocation.
15301 GCC allocates each "containing object" for each bit-field at
15302 the first (i.e. lowest addressed) legitimate alignment boundary
15303 (based upon the required minimum alignment for the declared
15304 type of the field) which it can possibly use, subject to the
15305 condition that there is still enough available space remaining
15306 in the containing object (when allocated at the selected point)
15307 to fully accommodate all of the bits of the bit-field itself.
15308
15309 This simple rule makes it obvious why GCC allocates 8 bytes for
15310 each object of the structure type shown above. When looking
15311 for a place to allocate the "containing object" for `field2',
15312 the compiler simply tries to allocate a 64-bit "containing
15313 object" at each successive 32-bit boundary (starting at zero)
15314 until it finds a place to allocate that 64- bit field such that
15315 at least 31 contiguous (and previously unallocated) bits remain
15316 within that selected 64 bit field. (As it turns out, for the
15317 example above, the compiler finds it is OK to allocate the
15318 "containing object" 64-bit field at bit-offset zero within the
15319 structure type.)
15320
15321 Here we attempt to work backwards from the limited set of facts
15322 we're given, and we try to deduce from those facts, where GCC
15323 must have believed that the containing object started (within
15324 the structure type). The value we deduce is then used (by the
15325 callers of this routine) to generate DW_AT_location and
15326 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15327 the case of DW_AT_location, regular fields as well). */
15328
15329 /* Figure out the bit-distance from the start of the structure to
15330 the "deepest" bit of the bit-field. */
15331 deepest_bitpos = bitpos_int + field_size_in_bits;
15332
15333 /* This is the tricky part. Use some fancy footwork to deduce
15334 where the lowest addressed bit of the containing object must
15335 be. */
15336 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15337
15338 /* Round up to type_align by default. This works best for
15339 bitfields. */
15340 object_offset_in_bits
15341 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15342
15343 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15344 {
15345 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15346
15347 /* Round up to decl_align instead. */
15348 object_offset_in_bits
15349 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15350 }
15351 }
15352 else
15353 object_offset_in_bits = bitpos_int;
15354
15355 object_offset_in_bytes
15356 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15357 return object_offset_in_bytes.to_shwi ();
15358 }
15359 \f
15360 /* The following routines define various Dwarf attributes and any data
15361 associated with them. */
15362
15363 /* Add a location description attribute value to a DIE.
15364
15365 This emits location attributes suitable for whole variables and
15366 whole parameters. Note that the location attributes for struct fields are
15367 generated by the routine `data_member_location_attribute' below. */
15368
15369 static inline void
15370 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15371 dw_loc_list_ref descr)
15372 {
15373 if (descr == 0)
15374 return;
15375 if (single_element_loc_list_p (descr))
15376 add_AT_loc (die, attr_kind, descr->expr);
15377 else
15378 add_AT_loc_list (die, attr_kind, descr);
15379 }
15380
15381 /* Add DW_AT_accessibility attribute to DIE if needed. */
15382
15383 static void
15384 add_accessibility_attribute (dw_die_ref die, tree decl)
15385 {
15386 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15387 children, otherwise the default is DW_ACCESS_public. In DWARF2
15388 the default has always been DW_ACCESS_public. */
15389 if (TREE_PROTECTED (decl))
15390 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15391 else if (TREE_PRIVATE (decl))
15392 {
15393 if (dwarf_version == 2
15394 || die->die_parent == NULL
15395 || die->die_parent->die_tag != DW_TAG_class_type)
15396 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15397 }
15398 else if (dwarf_version > 2
15399 && die->die_parent
15400 && die->die_parent->die_tag == DW_TAG_class_type)
15401 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15402 }
15403
15404 /* Attach the specialized form of location attribute used for data members of
15405 struct and union types. In the special case of a FIELD_DECL node which
15406 represents a bit-field, the "offset" part of this special location
15407 descriptor must indicate the distance in bytes from the lowest-addressed
15408 byte of the containing struct or union type to the lowest-addressed byte of
15409 the "containing object" for the bit-field. (See the `field_byte_offset'
15410 function above).
15411
15412 For any given bit-field, the "containing object" is a hypothetical object
15413 (of some integral or enum type) within which the given bit-field lives. The
15414 type of this hypothetical "containing object" is always the same as the
15415 declared type of the individual bit-field itself (for GCC anyway... the
15416 DWARF spec doesn't actually mandate this). Note that it is the size (in
15417 bytes) of the hypothetical "containing object" which will be given in the
15418 DW_AT_byte_size attribute for this bit-field. (See the
15419 `byte_size_attribute' function below.) It is also used when calculating the
15420 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15421 function below.) */
15422
15423 static void
15424 add_data_member_location_attribute (dw_die_ref die, tree decl)
15425 {
15426 HOST_WIDE_INT offset;
15427 dw_loc_descr_ref loc_descr = 0;
15428
15429 if (TREE_CODE (decl) == TREE_BINFO)
15430 {
15431 /* We're working on the TAG_inheritance for a base class. */
15432 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15433 {
15434 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15435 aren't at a fixed offset from all (sub)objects of the same
15436 type. We need to extract the appropriate offset from our
15437 vtable. The following dwarf expression means
15438
15439 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15440
15441 This is specific to the V3 ABI, of course. */
15442
15443 dw_loc_descr_ref tmp;
15444
15445 /* Make a copy of the object address. */
15446 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15447 add_loc_descr (&loc_descr, tmp);
15448
15449 /* Extract the vtable address. */
15450 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15451 add_loc_descr (&loc_descr, tmp);
15452
15453 /* Calculate the address of the offset. */
15454 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15455 gcc_assert (offset < 0);
15456
15457 tmp = int_loc_descriptor (-offset);
15458 add_loc_descr (&loc_descr, tmp);
15459 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15460 add_loc_descr (&loc_descr, tmp);
15461
15462 /* Extract the offset. */
15463 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15464 add_loc_descr (&loc_descr, tmp);
15465
15466 /* Add it to the object address. */
15467 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15468 add_loc_descr (&loc_descr, tmp);
15469 }
15470 else
15471 offset = tree_to_shwi (BINFO_OFFSET (decl));
15472 }
15473 else
15474 offset = field_byte_offset (decl);
15475
15476 if (! loc_descr)
15477 {
15478 if (dwarf_version > 2)
15479 {
15480 /* Don't need to output a location expression, just the constant. */
15481 if (offset < 0)
15482 add_AT_int (die, DW_AT_data_member_location, offset);
15483 else
15484 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15485 return;
15486 }
15487 else
15488 {
15489 enum dwarf_location_atom op;
15490
15491 /* The DWARF2 standard says that we should assume that the structure
15492 address is already on the stack, so we can specify a structure
15493 field address by using DW_OP_plus_uconst. */
15494 op = DW_OP_plus_uconst;
15495 loc_descr = new_loc_descr (op, offset, 0);
15496 }
15497 }
15498
15499 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15500 }
15501
15502 /* Writes integer values to dw_vec_const array. */
15503
15504 static void
15505 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15506 {
15507 while (size != 0)
15508 {
15509 *dest++ = val & 0xff;
15510 val >>= 8;
15511 --size;
15512 }
15513 }
15514
15515 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15516
15517 static HOST_WIDE_INT
15518 extract_int (const unsigned char *src, unsigned int size)
15519 {
15520 HOST_WIDE_INT val = 0;
15521
15522 src += size;
15523 while (size != 0)
15524 {
15525 val <<= 8;
15526 val |= *--src & 0xff;
15527 --size;
15528 }
15529 return val;
15530 }
15531
15532 /* Writes wide_int values to dw_vec_const array. */
15533
15534 static void
15535 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15536 {
15537 int i;
15538
15539 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15540 {
15541 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15542 return;
15543 }
15544
15545 /* We'd have to extend this code to support odd sizes. */
15546 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15547
15548 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15549
15550 if (WORDS_BIG_ENDIAN)
15551 for (i = n - 1; i >= 0; i--)
15552 {
15553 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15554 dest += sizeof (HOST_WIDE_INT);
15555 }
15556 else
15557 for (i = 0; i < n; i++)
15558 {
15559 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15560 dest += sizeof (HOST_WIDE_INT);
15561 }
15562 }
15563
15564 /* Writes floating point values to dw_vec_const array. */
15565
15566 static void
15567 insert_float (const_rtx rtl, unsigned char *array)
15568 {
15569 long val[4];
15570 int i;
15571
15572 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
15573
15574 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15575 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15576 {
15577 insert_int (val[i], 4, array);
15578 array += 4;
15579 }
15580 }
15581
15582 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15583 does not have a "location" either in memory or in a register. These
15584 things can arise in GNU C when a constant is passed as an actual parameter
15585 to an inlined function. They can also arise in C++ where declared
15586 constants do not necessarily get memory "homes". */
15587
15588 static bool
15589 add_const_value_attribute (dw_die_ref die, rtx rtl)
15590 {
15591 switch (GET_CODE (rtl))
15592 {
15593 case CONST_INT:
15594 {
15595 HOST_WIDE_INT val = INTVAL (rtl);
15596
15597 if (val < 0)
15598 add_AT_int (die, DW_AT_const_value, val);
15599 else
15600 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15601 }
15602 return true;
15603
15604 case CONST_WIDE_INT:
15605 add_AT_wide (die, DW_AT_const_value,
15606 std::make_pair (rtl, GET_MODE (rtl)));
15607 return true;
15608
15609 case CONST_DOUBLE:
15610 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15611 floating-point constant. A CONST_DOUBLE is used whenever the
15612 constant requires more than one word in order to be adequately
15613 represented. */
15614 {
15615 machine_mode mode = GET_MODE (rtl);
15616
15617 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15618 add_AT_double (die, DW_AT_const_value,
15619 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15620 else
15621 {
15622 unsigned int length = GET_MODE_SIZE (mode);
15623 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15624
15625 insert_float (rtl, array);
15626 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15627 }
15628 }
15629 return true;
15630
15631 case CONST_VECTOR:
15632 {
15633 machine_mode mode = GET_MODE (rtl);
15634 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15635 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15636 unsigned char *array
15637 = ggc_vec_alloc<unsigned char> (length * elt_size);
15638 unsigned int i;
15639 unsigned char *p;
15640 machine_mode imode = GET_MODE_INNER (mode);
15641
15642 switch (GET_MODE_CLASS (mode))
15643 {
15644 case MODE_VECTOR_INT:
15645 for (i = 0, p = array; i < length; i++, p += elt_size)
15646 {
15647 rtx elt = CONST_VECTOR_ELT (rtl, i);
15648 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15649 }
15650 break;
15651
15652 case MODE_VECTOR_FLOAT:
15653 for (i = 0, p = array; i < length; i++, p += elt_size)
15654 {
15655 rtx elt = CONST_VECTOR_ELT (rtl, i);
15656 insert_float (elt, p);
15657 }
15658 break;
15659
15660 default:
15661 gcc_unreachable ();
15662 }
15663
15664 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15665 }
15666 return true;
15667
15668 case CONST_STRING:
15669 if (dwarf_version >= 4 || !dwarf_strict)
15670 {
15671 dw_loc_descr_ref loc_result;
15672 resolve_one_addr (&rtl);
15673 rtl_addr:
15674 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15675 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15676 add_AT_loc (die, DW_AT_location, loc_result);
15677 vec_safe_push (used_rtx_array, rtl);
15678 return true;
15679 }
15680 return false;
15681
15682 case CONST:
15683 if (CONSTANT_P (XEXP (rtl, 0)))
15684 return add_const_value_attribute (die, XEXP (rtl, 0));
15685 /* FALLTHROUGH */
15686 case SYMBOL_REF:
15687 if (!const_ok_for_output (rtl))
15688 return false;
15689 case LABEL_REF:
15690 if (dwarf_version >= 4 || !dwarf_strict)
15691 goto rtl_addr;
15692 return false;
15693
15694 case PLUS:
15695 /* In cases where an inlined instance of an inline function is passed
15696 the address of an `auto' variable (which is local to the caller) we
15697 can get a situation where the DECL_RTL of the artificial local
15698 variable (for the inlining) which acts as a stand-in for the
15699 corresponding formal parameter (of the inline function) will look
15700 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15701 exactly a compile-time constant expression, but it isn't the address
15702 of the (artificial) local variable either. Rather, it represents the
15703 *value* which the artificial local variable always has during its
15704 lifetime. We currently have no way to represent such quasi-constant
15705 values in Dwarf, so for now we just punt and generate nothing. */
15706 return false;
15707
15708 case HIGH:
15709 case CONST_FIXED:
15710 return false;
15711
15712 case MEM:
15713 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15714 && MEM_READONLY_P (rtl)
15715 && GET_MODE (rtl) == BLKmode)
15716 {
15717 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15718 return true;
15719 }
15720 return false;
15721
15722 default:
15723 /* No other kinds of rtx should be possible here. */
15724 gcc_unreachable ();
15725 }
15726 return false;
15727 }
15728
15729 /* Determine whether the evaluation of EXPR references any variables
15730 or functions which aren't otherwise used (and therefore may not be
15731 output). */
15732 static tree
15733 reference_to_unused (tree * tp, int * walk_subtrees,
15734 void * data ATTRIBUTE_UNUSED)
15735 {
15736 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15737 *walk_subtrees = 0;
15738
15739 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15740 && ! TREE_ASM_WRITTEN (*tp))
15741 return *tp;
15742 /* ??? The C++ FE emits debug information for using decls, so
15743 putting gcc_unreachable here falls over. See PR31899. For now
15744 be conservative. */
15745 else if (!symtab->global_info_ready
15746 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15747 return *tp;
15748 else if (TREE_CODE (*tp) == VAR_DECL)
15749 {
15750 varpool_node *node = varpool_node::get (*tp);
15751 if (!node || !node->definition)
15752 return *tp;
15753 }
15754 else if (TREE_CODE (*tp) == FUNCTION_DECL
15755 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15756 {
15757 /* The call graph machinery must have finished analyzing,
15758 optimizing and gimplifying the CU by now.
15759 So if *TP has no call graph node associated
15760 to it, it means *TP will not be emitted. */
15761 if (!cgraph_node::get (*tp))
15762 return *tp;
15763 }
15764 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15765 return *tp;
15766
15767 return NULL_TREE;
15768 }
15769
15770 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15771 for use in a later add_const_value_attribute call. */
15772
15773 static rtx
15774 rtl_for_decl_init (tree init, tree type)
15775 {
15776 rtx rtl = NULL_RTX;
15777
15778 STRIP_NOPS (init);
15779
15780 /* If a variable is initialized with a string constant without embedded
15781 zeros, build CONST_STRING. */
15782 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15783 {
15784 tree enttype = TREE_TYPE (type);
15785 tree domain = TYPE_DOMAIN (type);
15786 machine_mode mode = TYPE_MODE (enttype);
15787
15788 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15789 && domain
15790 && integer_zerop (TYPE_MIN_VALUE (domain))
15791 && compare_tree_int (TYPE_MAX_VALUE (domain),
15792 TREE_STRING_LENGTH (init) - 1) == 0
15793 && ((size_t) TREE_STRING_LENGTH (init)
15794 == strlen (TREE_STRING_POINTER (init)) + 1))
15795 {
15796 rtl = gen_rtx_CONST_STRING (VOIDmode,
15797 ggc_strdup (TREE_STRING_POINTER (init)));
15798 rtl = gen_rtx_MEM (BLKmode, rtl);
15799 MEM_READONLY_P (rtl) = 1;
15800 }
15801 }
15802 /* Other aggregates, and complex values, could be represented using
15803 CONCAT: FIXME! */
15804 else if (AGGREGATE_TYPE_P (type)
15805 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15806 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15807 || TREE_CODE (type) == COMPLEX_TYPE)
15808 ;
15809 /* Vectors only work if their mode is supported by the target.
15810 FIXME: generic vectors ought to work too. */
15811 else if (TREE_CODE (type) == VECTOR_TYPE
15812 && !VECTOR_MODE_P (TYPE_MODE (type)))
15813 ;
15814 /* If the initializer is something that we know will expand into an
15815 immediate RTL constant, expand it now. We must be careful not to
15816 reference variables which won't be output. */
15817 else if (initializer_constant_valid_p (init, type)
15818 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15819 {
15820 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15821 possible. */
15822 if (TREE_CODE (type) == VECTOR_TYPE)
15823 switch (TREE_CODE (init))
15824 {
15825 case VECTOR_CST:
15826 break;
15827 case CONSTRUCTOR:
15828 if (TREE_CONSTANT (init))
15829 {
15830 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15831 bool constant_p = true;
15832 tree value;
15833 unsigned HOST_WIDE_INT ix;
15834
15835 /* Even when ctor is constant, it might contain non-*_CST
15836 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15837 belong into VECTOR_CST nodes. */
15838 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15839 if (!CONSTANT_CLASS_P (value))
15840 {
15841 constant_p = false;
15842 break;
15843 }
15844
15845 if (constant_p)
15846 {
15847 init = build_vector_from_ctor (type, elts);
15848 break;
15849 }
15850 }
15851 /* FALLTHRU */
15852
15853 default:
15854 return NULL;
15855 }
15856
15857 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15858
15859 /* If expand_expr returns a MEM, it wasn't immediate. */
15860 gcc_assert (!rtl || !MEM_P (rtl));
15861 }
15862
15863 return rtl;
15864 }
15865
15866 /* Generate RTL for the variable DECL to represent its location. */
15867
15868 static rtx
15869 rtl_for_decl_location (tree decl)
15870 {
15871 rtx rtl;
15872
15873 /* Here we have to decide where we are going to say the parameter "lives"
15874 (as far as the debugger is concerned). We only have a couple of
15875 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15876
15877 DECL_RTL normally indicates where the parameter lives during most of the
15878 activation of the function. If optimization is enabled however, this
15879 could be either NULL or else a pseudo-reg. Both of those cases indicate
15880 that the parameter doesn't really live anywhere (as far as the code
15881 generation parts of GCC are concerned) during most of the function's
15882 activation. That will happen (for example) if the parameter is never
15883 referenced within the function.
15884
15885 We could just generate a location descriptor here for all non-NULL
15886 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15887 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15888 where DECL_RTL is NULL or is a pseudo-reg.
15889
15890 Note however that we can only get away with using DECL_INCOMING_RTL as
15891 a backup substitute for DECL_RTL in certain limited cases. In cases
15892 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15893 we can be sure that the parameter was passed using the same type as it is
15894 declared to have within the function, and that its DECL_INCOMING_RTL
15895 points us to a place where a value of that type is passed.
15896
15897 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15898 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15899 because in these cases DECL_INCOMING_RTL points us to a value of some
15900 type which is *different* from the type of the parameter itself. Thus,
15901 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15902 such cases, the debugger would end up (for example) trying to fetch a
15903 `float' from a place which actually contains the first part of a
15904 `double'. That would lead to really incorrect and confusing
15905 output at debug-time.
15906
15907 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15908 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15909 are a couple of exceptions however. On little-endian machines we can
15910 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15911 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15912 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15913 when (on a little-endian machine) a non-prototyped function has a
15914 parameter declared to be of type `short' or `char'. In such cases,
15915 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15916 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15917 passed `int' value. If the debugger then uses that address to fetch
15918 a `short' or a `char' (on a little-endian machine) the result will be
15919 the correct data, so we allow for such exceptional cases below.
15920
15921 Note that our goal here is to describe the place where the given formal
15922 parameter lives during most of the function's activation (i.e. between the
15923 end of the prologue and the start of the epilogue). We'll do that as best
15924 as we can. Note however that if the given formal parameter is modified
15925 sometime during the execution of the function, then a stack backtrace (at
15926 debug-time) will show the function as having been called with the *new*
15927 value rather than the value which was originally passed in. This happens
15928 rarely enough that it is not a major problem, but it *is* a problem, and
15929 I'd like to fix it.
15930
15931 A future version of dwarf2out.c may generate two additional attributes for
15932 any given DW_TAG_formal_parameter DIE which will describe the "passed
15933 type" and the "passed location" for the given formal parameter in addition
15934 to the attributes we now generate to indicate the "declared type" and the
15935 "active location" for each parameter. This additional set of attributes
15936 could be used by debuggers for stack backtraces. Separately, note that
15937 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15938 This happens (for example) for inlined-instances of inline function formal
15939 parameters which are never referenced. This really shouldn't be
15940 happening. All PARM_DECL nodes should get valid non-NULL
15941 DECL_INCOMING_RTL values. FIXME. */
15942
15943 /* Use DECL_RTL as the "location" unless we find something better. */
15944 rtl = DECL_RTL_IF_SET (decl);
15945
15946 /* When generating abstract instances, ignore everything except
15947 constants, symbols living in memory, and symbols living in
15948 fixed registers. */
15949 if (! reload_completed)
15950 {
15951 if (rtl
15952 && (CONSTANT_P (rtl)
15953 || (MEM_P (rtl)
15954 && CONSTANT_P (XEXP (rtl, 0)))
15955 || (REG_P (rtl)
15956 && TREE_CODE (decl) == VAR_DECL
15957 && TREE_STATIC (decl))))
15958 {
15959 rtl = targetm.delegitimize_address (rtl);
15960 return rtl;
15961 }
15962 rtl = NULL_RTX;
15963 }
15964 else if (TREE_CODE (decl) == PARM_DECL)
15965 {
15966 if (rtl == NULL_RTX
15967 || is_pseudo_reg (rtl)
15968 || (MEM_P (rtl)
15969 && is_pseudo_reg (XEXP (rtl, 0))
15970 && DECL_INCOMING_RTL (decl)
15971 && MEM_P (DECL_INCOMING_RTL (decl))
15972 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15973 {
15974 tree declared_type = TREE_TYPE (decl);
15975 tree passed_type = DECL_ARG_TYPE (decl);
15976 machine_mode dmode = TYPE_MODE (declared_type);
15977 machine_mode pmode = TYPE_MODE (passed_type);
15978
15979 /* This decl represents a formal parameter which was optimized out.
15980 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15981 all cases where (rtl == NULL_RTX) just below. */
15982 if (dmode == pmode)
15983 rtl = DECL_INCOMING_RTL (decl);
15984 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15985 && SCALAR_INT_MODE_P (dmode)
15986 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15987 && DECL_INCOMING_RTL (decl))
15988 {
15989 rtx inc = DECL_INCOMING_RTL (decl);
15990 if (REG_P (inc))
15991 rtl = inc;
15992 else if (MEM_P (inc))
15993 {
15994 if (BYTES_BIG_ENDIAN)
15995 rtl = adjust_address_nv (inc, dmode,
15996 GET_MODE_SIZE (pmode)
15997 - GET_MODE_SIZE (dmode));
15998 else
15999 rtl = inc;
16000 }
16001 }
16002 }
16003
16004 /* If the parm was passed in registers, but lives on the stack, then
16005 make a big endian correction if the mode of the type of the
16006 parameter is not the same as the mode of the rtl. */
16007 /* ??? This is the same series of checks that are made in dbxout.c before
16008 we reach the big endian correction code there. It isn't clear if all
16009 of these checks are necessary here, but keeping them all is the safe
16010 thing to do. */
16011 else if (MEM_P (rtl)
16012 && XEXP (rtl, 0) != const0_rtx
16013 && ! CONSTANT_P (XEXP (rtl, 0))
16014 /* Not passed in memory. */
16015 && !MEM_P (DECL_INCOMING_RTL (decl))
16016 /* Not passed by invisible reference. */
16017 && (!REG_P (XEXP (rtl, 0))
16018 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16019 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16020 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16021 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16022 #endif
16023 )
16024 /* Big endian correction check. */
16025 && BYTES_BIG_ENDIAN
16026 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16027 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16028 < UNITS_PER_WORD))
16029 {
16030 machine_mode addr_mode = get_address_mode (rtl);
16031 int offset = (UNITS_PER_WORD
16032 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16033
16034 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16035 plus_constant (addr_mode, XEXP (rtl, 0), offset));
16036 }
16037 }
16038 else if (TREE_CODE (decl) == VAR_DECL
16039 && rtl
16040 && MEM_P (rtl)
16041 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16042 && BYTES_BIG_ENDIAN)
16043 {
16044 machine_mode addr_mode = get_address_mode (rtl);
16045 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16046 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16047
16048 /* If a variable is declared "register" yet is smaller than
16049 a register, then if we store the variable to memory, it
16050 looks like we're storing a register-sized value, when in
16051 fact we are not. We need to adjust the offset of the
16052 storage location to reflect the actual value's bytes,
16053 else gdb will not be able to display it. */
16054 if (rsize > dsize)
16055 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16056 plus_constant (addr_mode, XEXP (rtl, 0),
16057 rsize - dsize));
16058 }
16059
16060 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16061 and will have been substituted directly into all expressions that use it.
16062 C does not have such a concept, but C++ and other languages do. */
16063 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16064 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16065
16066 if (rtl)
16067 rtl = targetm.delegitimize_address (rtl);
16068
16069 /* If we don't look past the constant pool, we risk emitting a
16070 reference to a constant pool entry that isn't referenced from
16071 code, and thus is not emitted. */
16072 if (rtl)
16073 rtl = avoid_constant_pool_reference (rtl);
16074
16075 /* Try harder to get a rtl. If this symbol ends up not being emitted
16076 in the current CU, resolve_addr will remove the expression referencing
16077 it. */
16078 if (rtl == NULL_RTX
16079 && TREE_CODE (decl) == VAR_DECL
16080 && !DECL_EXTERNAL (decl)
16081 && TREE_STATIC (decl)
16082 && DECL_NAME (decl)
16083 && !DECL_HARD_REGISTER (decl)
16084 && DECL_MODE (decl) != VOIDmode)
16085 {
16086 rtl = make_decl_rtl_for_debug (decl);
16087 if (!MEM_P (rtl)
16088 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16089 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16090 rtl = NULL_RTX;
16091 }
16092
16093 return rtl;
16094 }
16095
16096 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16097 returned. If so, the decl for the COMMON block is returned, and the
16098 value is the offset into the common block for the symbol. */
16099
16100 static tree
16101 fortran_common (tree decl, HOST_WIDE_INT *value)
16102 {
16103 tree val_expr, cvar;
16104 machine_mode mode;
16105 HOST_WIDE_INT bitsize, bitpos;
16106 tree offset;
16107 int unsignedp, volatilep = 0;
16108
16109 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16110 it does not have a value (the offset into the common area), or if it
16111 is thread local (as opposed to global) then it isn't common, and shouldn't
16112 be handled as such. */
16113 if (TREE_CODE (decl) != VAR_DECL
16114 || !TREE_STATIC (decl)
16115 || !DECL_HAS_VALUE_EXPR_P (decl)
16116 || !is_fortran ())
16117 return NULL_TREE;
16118
16119 val_expr = DECL_VALUE_EXPR (decl);
16120 if (TREE_CODE (val_expr) != COMPONENT_REF)
16121 return NULL_TREE;
16122
16123 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16124 &mode, &unsignedp, &volatilep, true);
16125
16126 if (cvar == NULL_TREE
16127 || TREE_CODE (cvar) != VAR_DECL
16128 || DECL_ARTIFICIAL (cvar)
16129 || !TREE_PUBLIC (cvar))
16130 return NULL_TREE;
16131
16132 *value = 0;
16133 if (offset != NULL)
16134 {
16135 if (!tree_fits_shwi_p (offset))
16136 return NULL_TREE;
16137 *value = tree_to_shwi (offset);
16138 }
16139 if (bitpos != 0)
16140 *value += bitpos / BITS_PER_UNIT;
16141
16142 return cvar;
16143 }
16144
16145 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16146 data attribute for a variable or a parameter. We generate the
16147 DW_AT_const_value attribute only in those cases where the given variable
16148 or parameter does not have a true "location" either in memory or in a
16149 register. This can happen (for example) when a constant is passed as an
16150 actual argument in a call to an inline function. (It's possible that
16151 these things can crop up in other ways also.) Note that one type of
16152 constant value which can be passed into an inlined function is a constant
16153 pointer. This can happen for example if an actual argument in an inlined
16154 function call evaluates to a compile-time constant address.
16155
16156 CACHE_P is true if it is worth caching the location list for DECL,
16157 so that future calls can reuse it rather than regenerate it from scratch.
16158 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16159 since we will need to refer to them each time the function is inlined. */
16160
16161 static bool
16162 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
16163 {
16164 rtx rtl;
16165 dw_loc_list_ref list;
16166 var_loc_list *loc_list;
16167 cached_dw_loc_list *cache;
16168
16169 if (early_dwarf)
16170 return false;
16171
16172 if (TREE_CODE (decl) == ERROR_MARK)
16173 return false;
16174
16175 if (get_AT (die, DW_AT_location)
16176 || get_AT (die, DW_AT_const_value))
16177 return true;
16178
16179 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16180 || TREE_CODE (decl) == RESULT_DECL);
16181
16182 /* Try to get some constant RTL for this decl, and use that as the value of
16183 the location. */
16184
16185 rtl = rtl_for_decl_location (decl);
16186 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16187 && add_const_value_attribute (die, rtl))
16188 return true;
16189
16190 /* See if we have single element location list that is equivalent to
16191 a constant value. That way we are better to use add_const_value_attribute
16192 rather than expanding constant value equivalent. */
16193 loc_list = lookup_decl_loc (decl);
16194 if (loc_list
16195 && loc_list->first
16196 && loc_list->first->next == NULL
16197 && NOTE_P (loc_list->first->loc)
16198 && NOTE_VAR_LOCATION (loc_list->first->loc)
16199 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16200 {
16201 struct var_loc_node *node;
16202
16203 node = loc_list->first;
16204 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16205 if (GET_CODE (rtl) == EXPR_LIST)
16206 rtl = XEXP (rtl, 0);
16207 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16208 && add_const_value_attribute (die, rtl))
16209 return true;
16210 }
16211 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16212 list several times. See if we've already cached the contents. */
16213 list = NULL;
16214 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16215 cache_p = false;
16216 if (cache_p)
16217 {
16218 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16219 if (cache)
16220 list = cache->loc_list;
16221 }
16222 if (list == NULL)
16223 {
16224 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16225 NULL);
16226 /* It is usually worth caching this result if the decl is from
16227 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16228 if (cache_p && list && list->dw_loc_next)
16229 {
16230 cached_dw_loc_list **slot
16231 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16232 DECL_UID (decl),
16233 INSERT);
16234 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16235 cache->decl_id = DECL_UID (decl);
16236 cache->loc_list = list;
16237 *slot = cache;
16238 }
16239 }
16240 if (list)
16241 {
16242 add_AT_location_description (die, DW_AT_location, list);
16243 return true;
16244 }
16245 /* None of that worked, so it must not really have a location;
16246 try adding a constant value attribute from the DECL_INITIAL. */
16247 return tree_add_const_value_attribute_for_decl (die, decl);
16248 }
16249
16250 /* Helper function for tree_add_const_value_attribute. Natively encode
16251 initializer INIT into an array. Return true if successful. */
16252
16253 static bool
16254 native_encode_initializer (tree init, unsigned char *array, int size)
16255 {
16256 tree type;
16257
16258 if (init == NULL_TREE)
16259 return false;
16260
16261 STRIP_NOPS (init);
16262 switch (TREE_CODE (init))
16263 {
16264 case STRING_CST:
16265 type = TREE_TYPE (init);
16266 if (TREE_CODE (type) == ARRAY_TYPE)
16267 {
16268 tree enttype = TREE_TYPE (type);
16269 machine_mode mode = TYPE_MODE (enttype);
16270
16271 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16272 return false;
16273 if (int_size_in_bytes (type) != size)
16274 return false;
16275 if (size > TREE_STRING_LENGTH (init))
16276 {
16277 memcpy (array, TREE_STRING_POINTER (init),
16278 TREE_STRING_LENGTH (init));
16279 memset (array + TREE_STRING_LENGTH (init),
16280 '\0', size - TREE_STRING_LENGTH (init));
16281 }
16282 else
16283 memcpy (array, TREE_STRING_POINTER (init), size);
16284 return true;
16285 }
16286 return false;
16287 case CONSTRUCTOR:
16288 type = TREE_TYPE (init);
16289 if (int_size_in_bytes (type) != size)
16290 return false;
16291 if (TREE_CODE (type) == ARRAY_TYPE)
16292 {
16293 HOST_WIDE_INT min_index;
16294 unsigned HOST_WIDE_INT cnt;
16295 int curpos = 0, fieldsize;
16296 constructor_elt *ce;
16297
16298 if (TYPE_DOMAIN (type) == NULL_TREE
16299 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16300 return false;
16301
16302 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16303 if (fieldsize <= 0)
16304 return false;
16305
16306 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16307 memset (array, '\0', size);
16308 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16309 {
16310 tree val = ce->value;
16311 tree index = ce->index;
16312 int pos = curpos;
16313 if (index && TREE_CODE (index) == RANGE_EXPR)
16314 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16315 * fieldsize;
16316 else if (index)
16317 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16318
16319 if (val)
16320 {
16321 STRIP_NOPS (val);
16322 if (!native_encode_initializer (val, array + pos, fieldsize))
16323 return false;
16324 }
16325 curpos = pos + fieldsize;
16326 if (index && TREE_CODE (index) == RANGE_EXPR)
16327 {
16328 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16329 - tree_to_shwi (TREE_OPERAND (index, 0));
16330 while (count-- > 0)
16331 {
16332 if (val)
16333 memcpy (array + curpos, array + pos, fieldsize);
16334 curpos += fieldsize;
16335 }
16336 }
16337 gcc_assert (curpos <= size);
16338 }
16339 return true;
16340 }
16341 else if (TREE_CODE (type) == RECORD_TYPE
16342 || TREE_CODE (type) == UNION_TYPE)
16343 {
16344 tree field = NULL_TREE;
16345 unsigned HOST_WIDE_INT cnt;
16346 constructor_elt *ce;
16347
16348 if (int_size_in_bytes (type) != size)
16349 return false;
16350
16351 if (TREE_CODE (type) == RECORD_TYPE)
16352 field = TYPE_FIELDS (type);
16353
16354 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16355 {
16356 tree val = ce->value;
16357 int pos, fieldsize;
16358
16359 if (ce->index != 0)
16360 field = ce->index;
16361
16362 if (val)
16363 STRIP_NOPS (val);
16364
16365 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16366 return false;
16367
16368 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16369 && TYPE_DOMAIN (TREE_TYPE (field))
16370 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16371 return false;
16372 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16373 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16374 return false;
16375 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16376 pos = int_byte_position (field);
16377 gcc_assert (pos + fieldsize <= size);
16378 if (val
16379 && !native_encode_initializer (val, array + pos, fieldsize))
16380 return false;
16381 }
16382 return true;
16383 }
16384 return false;
16385 case VIEW_CONVERT_EXPR:
16386 case NON_LVALUE_EXPR:
16387 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16388 default:
16389 return native_encode_expr (init, array, size) == size;
16390 }
16391 }
16392
16393 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16394 attribute is the const value T. */
16395
16396 static bool
16397 tree_add_const_value_attribute (dw_die_ref die, tree t)
16398 {
16399 tree init;
16400 tree type = TREE_TYPE (t);
16401 rtx rtl;
16402
16403 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16404 return false;
16405
16406 init = t;
16407 gcc_assert (!DECL_P (init));
16408
16409 rtl = rtl_for_decl_init (init, type);
16410 if (rtl)
16411 return add_const_value_attribute (die, rtl);
16412 /* If the host and target are sane, try harder. */
16413 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16414 && initializer_constant_valid_p (init, type))
16415 {
16416 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16417 if (size > 0 && (int) size == size)
16418 {
16419 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16420
16421 if (native_encode_initializer (init, array, size))
16422 {
16423 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16424 return true;
16425 }
16426 ggc_free (array);
16427 }
16428 }
16429 return false;
16430 }
16431
16432 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16433 attribute is the const value of T, where T is an integral constant
16434 variable with static storage duration
16435 (so it can't be a PARM_DECL or a RESULT_DECL). */
16436
16437 static bool
16438 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16439 {
16440
16441 if (!decl
16442 || (TREE_CODE (decl) != VAR_DECL
16443 && TREE_CODE (decl) != CONST_DECL)
16444 || (TREE_CODE (decl) == VAR_DECL
16445 && !TREE_STATIC (decl)))
16446 return false;
16447
16448 if (TREE_READONLY (decl)
16449 && ! TREE_THIS_VOLATILE (decl)
16450 && DECL_INITIAL (decl))
16451 /* OK */;
16452 else
16453 return false;
16454
16455 /* Don't add DW_AT_const_value if abstract origin already has one. */
16456 if (get_AT (var_die, DW_AT_const_value))
16457 return false;
16458
16459 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16460 }
16461
16462 /* Convert the CFI instructions for the current function into a
16463 location list. This is used for DW_AT_frame_base when we targeting
16464 a dwarf2 consumer that does not support the dwarf3
16465 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16466 expressions. */
16467
16468 static dw_loc_list_ref
16469 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16470 {
16471 int ix;
16472 dw_fde_ref fde;
16473 dw_loc_list_ref list, *list_tail;
16474 dw_cfi_ref cfi;
16475 dw_cfa_location last_cfa, next_cfa;
16476 const char *start_label, *last_label, *section;
16477 dw_cfa_location remember;
16478
16479 fde = cfun->fde;
16480 gcc_assert (fde != NULL);
16481
16482 section = secname_for_decl (current_function_decl);
16483 list_tail = &list;
16484 list = NULL;
16485
16486 memset (&next_cfa, 0, sizeof (next_cfa));
16487 next_cfa.reg = INVALID_REGNUM;
16488 remember = next_cfa;
16489
16490 start_label = fde->dw_fde_begin;
16491
16492 /* ??? Bald assumption that the CIE opcode list does not contain
16493 advance opcodes. */
16494 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16495 lookup_cfa_1 (cfi, &next_cfa, &remember);
16496
16497 last_cfa = next_cfa;
16498 last_label = start_label;
16499
16500 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16501 {
16502 /* If the first partition contained no CFI adjustments, the
16503 CIE opcodes apply to the whole first partition. */
16504 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16505 fde->dw_fde_begin, fde->dw_fde_end, section);
16506 list_tail =&(*list_tail)->dw_loc_next;
16507 start_label = last_label = fde->dw_fde_second_begin;
16508 }
16509
16510 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16511 {
16512 switch (cfi->dw_cfi_opc)
16513 {
16514 case DW_CFA_set_loc:
16515 case DW_CFA_advance_loc1:
16516 case DW_CFA_advance_loc2:
16517 case DW_CFA_advance_loc4:
16518 if (!cfa_equal_p (&last_cfa, &next_cfa))
16519 {
16520 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16521 start_label, last_label, section);
16522
16523 list_tail = &(*list_tail)->dw_loc_next;
16524 last_cfa = next_cfa;
16525 start_label = last_label;
16526 }
16527 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16528 break;
16529
16530 case DW_CFA_advance_loc:
16531 /* The encoding is complex enough that we should never emit this. */
16532 gcc_unreachable ();
16533
16534 default:
16535 lookup_cfa_1 (cfi, &next_cfa, &remember);
16536 break;
16537 }
16538 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16539 {
16540 if (!cfa_equal_p (&last_cfa, &next_cfa))
16541 {
16542 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16543 start_label, last_label, section);
16544
16545 list_tail = &(*list_tail)->dw_loc_next;
16546 last_cfa = next_cfa;
16547 start_label = last_label;
16548 }
16549 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16550 start_label, fde->dw_fde_end, section);
16551 list_tail = &(*list_tail)->dw_loc_next;
16552 start_label = last_label = fde->dw_fde_second_begin;
16553 }
16554 }
16555
16556 if (!cfa_equal_p (&last_cfa, &next_cfa))
16557 {
16558 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16559 start_label, last_label, section);
16560 list_tail = &(*list_tail)->dw_loc_next;
16561 start_label = last_label;
16562 }
16563
16564 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16565 start_label,
16566 fde->dw_fde_second_begin
16567 ? fde->dw_fde_second_end : fde->dw_fde_end,
16568 section);
16569
16570 if (list && list->dw_loc_next)
16571 gen_llsym (list);
16572
16573 return list;
16574 }
16575
16576 /* Compute a displacement from the "steady-state frame pointer" to the
16577 frame base (often the same as the CFA), and store it in
16578 frame_pointer_fb_offset. OFFSET is added to the displacement
16579 before the latter is negated. */
16580
16581 static void
16582 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16583 {
16584 rtx reg, elim;
16585
16586 #ifdef FRAME_POINTER_CFA_OFFSET
16587 reg = frame_pointer_rtx;
16588 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16589 #else
16590 reg = arg_pointer_rtx;
16591 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16592 #endif
16593
16594 elim = (ira_use_lra_p
16595 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16596 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16597 if (GET_CODE (elim) == PLUS)
16598 {
16599 offset += INTVAL (XEXP (elim, 1));
16600 elim = XEXP (elim, 0);
16601 }
16602
16603 frame_pointer_fb_offset = -offset;
16604
16605 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16606 in which to eliminate. This is because it's stack pointer isn't
16607 directly accessible as a register within the ISA. To work around
16608 this, assume that while we cannot provide a proper value for
16609 frame_pointer_fb_offset, we won't need one either. */
16610 frame_pointer_fb_offset_valid
16611 = ((SUPPORTS_STACK_ALIGNMENT
16612 && (elim == hard_frame_pointer_rtx
16613 || elim == stack_pointer_rtx))
16614 || elim == (frame_pointer_needed
16615 ? hard_frame_pointer_rtx
16616 : stack_pointer_rtx));
16617 }
16618
16619 /* Generate a DW_AT_name attribute given some string value to be included as
16620 the value of the attribute. */
16621
16622 static void
16623 add_name_attribute (dw_die_ref die, const char *name_string)
16624 {
16625 if (name_string != NULL && *name_string != 0)
16626 {
16627 if (demangle_name_func)
16628 name_string = (*demangle_name_func) (name_string);
16629
16630 add_AT_string (die, DW_AT_name, name_string);
16631 }
16632 }
16633
16634 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16635 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16636 of TYPE accordingly.
16637
16638 ??? This is a temporary measure until after we're able to generate
16639 regular DWARF for the complex Ada type system. */
16640
16641 static void
16642 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16643 dw_die_ref context_die)
16644 {
16645 tree dtype;
16646 dw_die_ref dtype_die;
16647
16648 if (!lang_hooks.types.descriptive_type)
16649 return;
16650
16651 dtype = lang_hooks.types.descriptive_type (type);
16652 if (!dtype)
16653 return;
16654
16655 dtype_die = lookup_type_die (dtype);
16656 if (!dtype_die)
16657 {
16658 gen_type_die (dtype, context_die);
16659 dtype_die = lookup_type_die (dtype);
16660 gcc_assert (dtype_die);
16661 }
16662
16663 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16664 }
16665
16666 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16667
16668 static const char *
16669 comp_dir_string (void)
16670 {
16671 const char *wd;
16672 char *wd1;
16673 static const char *cached_wd = NULL;
16674
16675 if (cached_wd != NULL)
16676 return cached_wd;
16677
16678 wd = get_src_pwd ();
16679 if (wd == NULL)
16680 return NULL;
16681
16682 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16683 {
16684 int wdlen;
16685
16686 wdlen = strlen (wd);
16687 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16688 strcpy (wd1, wd);
16689 wd1 [wdlen] = DIR_SEPARATOR;
16690 wd1 [wdlen + 1] = 0;
16691 wd = wd1;
16692 }
16693
16694 cached_wd = remap_debug_filename (wd);
16695 return cached_wd;
16696 }
16697
16698 /* Generate a DW_AT_comp_dir attribute for DIE. */
16699
16700 static void
16701 add_comp_dir_attribute (dw_die_ref die)
16702 {
16703 const char * wd = comp_dir_string ();
16704 if (wd != NULL)
16705 add_AT_string (die, DW_AT_comp_dir, wd);
16706 }
16707
16708 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16709 pointer computation, ...), output a representation for that bound according
16710 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16711 loc_list_from_tree for the meaning of CONTEXT. */
16712
16713 static void
16714 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16715 int forms, const struct loc_descr_context *context)
16716 {
16717 dw_die_ref ctx, decl_die;
16718 dw_loc_list_ref list;
16719
16720 bool strip_conversions = true;
16721
16722 while (strip_conversions)
16723 switch (TREE_CODE (value))
16724 {
16725 case ERROR_MARK:
16726 case SAVE_EXPR:
16727 return;
16728
16729 CASE_CONVERT:
16730 case VIEW_CONVERT_EXPR:
16731 value = TREE_OPERAND (value, 0);
16732 break;
16733
16734 default:
16735 strip_conversions = false;
16736 break;
16737 }
16738
16739 /* If possible and permitted, output the attribute as a constant. */
16740 if ((forms & dw_scalar_form_constant) != 0
16741 && TREE_CODE (value) == INTEGER_CST)
16742 {
16743 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16744
16745 /* If HOST_WIDE_INT is big enough then represent the bound as
16746 a constant value. We need to choose a form based on
16747 whether the type is signed or unsigned. We cannot just
16748 call add_AT_unsigned if the value itself is positive
16749 (add_AT_unsigned might add the unsigned value encoded as
16750 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16751 bounds type and then sign extend any unsigned values found
16752 for signed types. This is needed only for
16753 DW_AT_{lower,upper}_bound, since for most other attributes,
16754 consumers will treat DW_FORM_data[1248] as unsigned values,
16755 regardless of the underlying type. */
16756 if (prec <= HOST_BITS_PER_WIDE_INT
16757 || tree_fits_uhwi_p (value))
16758 {
16759 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16760 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16761 else
16762 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16763 }
16764 else
16765 /* Otherwise represent the bound as an unsigned value with
16766 the precision of its type. The precision and signedness
16767 of the type will be necessary to re-interpret it
16768 unambiguously. */
16769 add_AT_wide (die, attr, value);
16770 return;
16771 }
16772
16773 /* Otherwise, if it's possible and permitted too, output a reference to
16774 another DIE. */
16775 if ((forms & dw_scalar_form_reference) != 0)
16776 {
16777 tree decl = NULL_TREE;
16778
16779 /* Some type attributes reference an outer type. For instance, the upper
16780 bound of an array may reference an embedding record (this happens in
16781 Ada). */
16782 if (TREE_CODE (value) == COMPONENT_REF
16783 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16784 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16785 decl = TREE_OPERAND (value, 1);
16786
16787 else if (TREE_CODE (value) == VAR_DECL
16788 || TREE_CODE (value) == PARM_DECL
16789 || TREE_CODE (value) == RESULT_DECL)
16790 decl = value;
16791
16792 if (decl != NULL_TREE)
16793 {
16794 dw_die_ref decl_die = lookup_decl_die (decl);
16795
16796 /* ??? Can this happen, or should the variable have been bound
16797 first? Probably it can, since I imagine that we try to create
16798 the types of parameters in the order in which they exist in
16799 the list, and won't have created a forward reference to a
16800 later parameter. */
16801 if (decl_die != NULL)
16802 {
16803 add_AT_die_ref (die, attr, decl_die);
16804 return;
16805 }
16806 }
16807 }
16808
16809 /* Last chance: try to create a stack operation procedure to evaluate the
16810 value. Do nothing if even that is not possible or permitted. */
16811 if ((forms & dw_scalar_form_exprloc) == 0)
16812 return;
16813
16814 list = loc_list_from_tree (value, 2, context);
16815 if (list == NULL || single_element_loc_list_p (list))
16816 {
16817 /* If this attribute is not a reference nor constant, it is
16818 a DWARF expression rather than location description. For that
16819 loc_list_from_tree (value, 0, &context) is needed. */
16820 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16821 if (list2 && single_element_loc_list_p (list2))
16822 {
16823 add_AT_loc (die, attr, list2->expr);
16824 return;
16825 }
16826 }
16827
16828 /* If that failed to give a single element location list, fall back to
16829 outputting this as a reference... still if permitted. */
16830 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16831 return;
16832
16833 if (current_function_decl == 0)
16834 ctx = comp_unit_die ();
16835 else
16836 ctx = lookup_decl_die (current_function_decl);
16837
16838 decl_die = new_die (DW_TAG_variable, ctx, value);
16839 add_AT_flag (decl_die, DW_AT_artificial, 1);
16840 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16841 add_AT_location_description (decl_die, DW_AT_location, list);
16842 add_AT_die_ref (die, attr, decl_die);
16843 }
16844
16845 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16846 default. */
16847
16848 static int
16849 lower_bound_default (void)
16850 {
16851 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16852 {
16853 case DW_LANG_C:
16854 case DW_LANG_C89:
16855 case DW_LANG_C99:
16856 case DW_LANG_C11:
16857 case DW_LANG_C_plus_plus:
16858 case DW_LANG_C_plus_plus_11:
16859 case DW_LANG_C_plus_plus_14:
16860 case DW_LANG_ObjC:
16861 case DW_LANG_ObjC_plus_plus:
16862 case DW_LANG_Java:
16863 return 0;
16864 case DW_LANG_Fortran77:
16865 case DW_LANG_Fortran90:
16866 case DW_LANG_Fortran95:
16867 case DW_LANG_Fortran03:
16868 case DW_LANG_Fortran08:
16869 return 1;
16870 case DW_LANG_UPC:
16871 case DW_LANG_D:
16872 case DW_LANG_Python:
16873 return dwarf_version >= 4 ? 0 : -1;
16874 case DW_LANG_Ada95:
16875 case DW_LANG_Ada83:
16876 case DW_LANG_Cobol74:
16877 case DW_LANG_Cobol85:
16878 case DW_LANG_Pascal83:
16879 case DW_LANG_Modula2:
16880 case DW_LANG_PLI:
16881 return dwarf_version >= 4 ? 1 : -1;
16882 default:
16883 return -1;
16884 }
16885 }
16886
16887 /* Given a tree node describing an array bound (either lower or upper) output
16888 a representation for that bound. */
16889
16890 static void
16891 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16892 tree bound, const struct loc_descr_context *context)
16893 {
16894 int dflt;
16895
16896 while (1)
16897 switch (TREE_CODE (bound))
16898 {
16899 /* Strip all conversions. */
16900 CASE_CONVERT:
16901 case VIEW_CONVERT_EXPR:
16902 bound = TREE_OPERAND (bound, 0);
16903 break;
16904
16905 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16906 are even omitted when they are the default. */
16907 case INTEGER_CST:
16908 /* If the value for this bound is the default one, we can even omit the
16909 attribute. */
16910 if (bound_attr == DW_AT_lower_bound
16911 && tree_fits_shwi_p (bound)
16912 && (dflt = lower_bound_default ()) != -1
16913 && tree_to_shwi (bound) == dflt)
16914 return;
16915
16916 /* FALLTHRU */
16917
16918 default:
16919 add_scalar_info (subrange_die, bound_attr, bound,
16920 dw_scalar_form_constant
16921 | dw_scalar_form_exprloc
16922 | dw_scalar_form_reference,
16923 context);
16924 return;
16925 }
16926 }
16927
16928 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16929 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16930 Note that the block of subscript information for an array type also
16931 includes information about the element type of the given array type.
16932
16933 This function reuses previously set type and bound information if
16934 available. */
16935
16936 static void
16937 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16938 {
16939 unsigned dimension_number;
16940 tree lower, upper;
16941 dw_die_ref child = type_die->die_child;
16942
16943 for (dimension_number = 0;
16944 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16945 type = TREE_TYPE (type), dimension_number++)
16946 {
16947 tree domain = TYPE_DOMAIN (type);
16948
16949 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16950 break;
16951
16952 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16953 and (in GNU C only) variable bounds. Handle all three forms
16954 here. */
16955
16956 /* Find and reuse a previously generated DW_TAG_subrange_type if
16957 available.
16958
16959 For multi-dimensional arrays, as we iterate through the
16960 various dimensions in the enclosing for loop above, we also
16961 iterate through the DIE children and pick at each
16962 DW_TAG_subrange_type previously generated (if available).
16963 Each child DW_TAG_subrange_type DIE describes the range of
16964 the current dimension. At this point we should have as many
16965 DW_TAG_subrange_type's as we have dimensions in the
16966 array. */
16967 dw_die_ref subrange_die = NULL;
16968 if (child)
16969 while (1)
16970 {
16971 child = child->die_sib;
16972 if (child->die_tag == DW_TAG_subrange_type)
16973 subrange_die = child;
16974 if (child == type_die->die_child)
16975 {
16976 /* If we wrapped around, stop looking next time. */
16977 child = NULL;
16978 break;
16979 }
16980 if (child->die_tag == DW_TAG_subrange_type)
16981 break;
16982 }
16983 if (!subrange_die)
16984 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16985
16986 if (domain)
16987 {
16988 /* We have an array type with specified bounds. */
16989 lower = TYPE_MIN_VALUE (domain);
16990 upper = TYPE_MAX_VALUE (domain);
16991
16992 /* Define the index type. */
16993 if (TREE_TYPE (domain)
16994 && !get_AT (subrange_die, DW_AT_type))
16995 {
16996 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16997 TREE_TYPE field. We can't emit debug info for this
16998 because it is an unnamed integral type. */
16999 if (TREE_CODE (domain) == INTEGER_TYPE
17000 && TYPE_NAME (domain) == NULL_TREE
17001 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17002 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17003 ;
17004 else
17005 add_type_attribute (subrange_die, TREE_TYPE (domain),
17006 TYPE_UNQUALIFIED, type_die);
17007 }
17008
17009 /* ??? If upper is NULL, the array has unspecified length,
17010 but it does have a lower bound. This happens with Fortran
17011 dimension arr(N:*)
17012 Since the debugger is definitely going to need to know N
17013 to produce useful results, go ahead and output the lower
17014 bound solo, and hope the debugger can cope. */
17015
17016 if (!get_AT (subrange_die, DW_AT_lower_bound))
17017 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
17018 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
17019 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
17020 }
17021
17022 /* Otherwise we have an array type with an unspecified length. The
17023 DWARF-2 spec does not say how to handle this; let's just leave out the
17024 bounds. */
17025 }
17026 }
17027
17028 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
17029
17030 static void
17031 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17032 {
17033 dw_die_ref decl_die;
17034 HOST_WIDE_INT size;
17035
17036 switch (TREE_CODE (tree_node))
17037 {
17038 case ERROR_MARK:
17039 size = 0;
17040 break;
17041 case ENUMERAL_TYPE:
17042 case RECORD_TYPE:
17043 case UNION_TYPE:
17044 case QUAL_UNION_TYPE:
17045 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17046 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17047 {
17048 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17049 return;
17050 }
17051 size = int_size_in_bytes (tree_node);
17052 break;
17053 case FIELD_DECL:
17054 /* For a data member of a struct or union, the DW_AT_byte_size is
17055 generally given as the number of bytes normally allocated for an
17056 object of the *declared* type of the member itself. This is true
17057 even for bit-fields. */
17058 size = int_size_in_bytes (field_type (tree_node));
17059 break;
17060 default:
17061 gcc_unreachable ();
17062 }
17063
17064 /* Note that `size' might be -1 when we get to this point. If it is, that
17065 indicates that the byte size of the entity in question is variable. We
17066 have no good way of expressing this fact in Dwarf at the present time,
17067 when location description was not used by the caller code instead. */
17068 if (size >= 0)
17069 add_AT_unsigned (die, DW_AT_byte_size, size);
17070 }
17071
17072 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17073 which specifies the distance in bits from the highest order bit of the
17074 "containing object" for the bit-field to the highest order bit of the
17075 bit-field itself.
17076
17077 For any given bit-field, the "containing object" is a hypothetical object
17078 (of some integral or enum type) within which the given bit-field lives. The
17079 type of this hypothetical "containing object" is always the same as the
17080 declared type of the individual bit-field itself. The determination of the
17081 exact location of the "containing object" for a bit-field is rather
17082 complicated. It's handled by the `field_byte_offset' function (above).
17083
17084 Note that it is the size (in bytes) of the hypothetical "containing object"
17085 which will be given in the DW_AT_byte_size attribute for this bit-field.
17086 (See `byte_size_attribute' above). */
17087
17088 static inline void
17089 add_bit_offset_attribute (dw_die_ref die, tree decl)
17090 {
17091 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17092 tree type = DECL_BIT_FIELD_TYPE (decl);
17093 HOST_WIDE_INT bitpos_int;
17094 HOST_WIDE_INT highest_order_object_bit_offset;
17095 HOST_WIDE_INT highest_order_field_bit_offset;
17096 HOST_WIDE_INT bit_offset;
17097
17098 /* Must be a field and a bit field. */
17099 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17100
17101 /* We can't yet handle bit-fields whose offsets are variable, so if we
17102 encounter such things, just return without generating any attribute
17103 whatsoever. Likewise for variable or too large size. */
17104 if (! tree_fits_shwi_p (bit_position (decl))
17105 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17106 return;
17107
17108 bitpos_int = int_bit_position (decl);
17109
17110 /* Note that the bit offset is always the distance (in bits) from the
17111 highest-order bit of the "containing object" to the highest-order bit of
17112 the bit-field itself. Since the "high-order end" of any object or field
17113 is different on big-endian and little-endian machines, the computation
17114 below must take account of these differences. */
17115 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17116 highest_order_field_bit_offset = bitpos_int;
17117
17118 if (! BYTES_BIG_ENDIAN)
17119 {
17120 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17121 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17122 }
17123
17124 bit_offset
17125 = (! BYTES_BIG_ENDIAN
17126 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17127 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17128
17129 if (bit_offset < 0)
17130 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17131 else
17132 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17133 }
17134
17135 /* For a FIELD_DECL node which represents a bit field, output an attribute
17136 which specifies the length in bits of the given field. */
17137
17138 static inline void
17139 add_bit_size_attribute (dw_die_ref die, tree decl)
17140 {
17141 /* Must be a field and a bit field. */
17142 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17143 && DECL_BIT_FIELD_TYPE (decl));
17144
17145 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17146 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17147 }
17148
17149 /* If the compiled language is ANSI C, then add a 'prototyped'
17150 attribute, if arg types are given for the parameters of a function. */
17151
17152 static inline void
17153 add_prototyped_attribute (dw_die_ref die, tree func_type)
17154 {
17155 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17156 {
17157 case DW_LANG_C:
17158 case DW_LANG_C89:
17159 case DW_LANG_C99:
17160 case DW_LANG_C11:
17161 case DW_LANG_ObjC:
17162 if (prototype_p (func_type))
17163 add_AT_flag (die, DW_AT_prototyped, 1);
17164 break;
17165 default:
17166 break;
17167 }
17168 }
17169
17170 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17171 by looking in either the type declaration or object declaration
17172 equate table. */
17173
17174 static inline dw_die_ref
17175 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17176 {
17177 dw_die_ref origin_die = NULL;
17178
17179 if (TREE_CODE (origin) != FUNCTION_DECL)
17180 {
17181 /* We may have gotten separated from the block for the inlined
17182 function, if we're in an exception handler or some such; make
17183 sure that the abstract function has been written out.
17184
17185 Doing this for nested functions is wrong, however; functions are
17186 distinct units, and our context might not even be inline. */
17187 tree fn = origin;
17188
17189 if (TYPE_P (fn))
17190 fn = TYPE_STUB_DECL (fn);
17191
17192 fn = decl_function_context (fn);
17193 if (fn)
17194 dwarf2out_abstract_function (fn);
17195 }
17196
17197 if (DECL_P (origin))
17198 origin_die = lookup_decl_die (origin);
17199 else if (TYPE_P (origin))
17200 origin_die = lookup_type_die (origin);
17201
17202 /* XXX: Functions that are never lowered don't always have correct block
17203 trees (in the case of java, they simply have no block tree, in some other
17204 languages). For these functions, there is nothing we can really do to
17205 output correct debug info for inlined functions in all cases. Rather
17206 than die, we'll just produce deficient debug info now, in that we will
17207 have variables without a proper abstract origin. In the future, when all
17208 functions are lowered, we should re-add a gcc_assert (origin_die)
17209 here. */
17210
17211 if (origin_die)
17212 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17213 return origin_die;
17214 }
17215
17216 /* We do not currently support the pure_virtual attribute. */
17217
17218 static inline void
17219 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17220 {
17221 if (DECL_VINDEX (func_decl))
17222 {
17223 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17224
17225 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17226 add_AT_loc (die, DW_AT_vtable_elem_location,
17227 new_loc_descr (DW_OP_constu,
17228 tree_to_shwi (DECL_VINDEX (func_decl)),
17229 0));
17230
17231 /* GNU extension: Record what type this method came from originally. */
17232 if (debug_info_level > DINFO_LEVEL_TERSE
17233 && DECL_CONTEXT (func_decl))
17234 add_AT_die_ref (die, DW_AT_containing_type,
17235 lookup_type_die (DECL_CONTEXT (func_decl)));
17236 }
17237 }
17238 \f
17239 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17240 given decl. This used to be a vendor extension until after DWARF 4
17241 standardized it. */
17242
17243 static void
17244 add_linkage_attr (dw_die_ref die, tree decl)
17245 {
17246 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17247
17248 /* Mimic what assemble_name_raw does with a leading '*'. */
17249 if (name[0] == '*')
17250 name = &name[1];
17251
17252 if (dwarf_version >= 4)
17253 add_AT_string (die, DW_AT_linkage_name, name);
17254 else
17255 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17256 }
17257
17258 /* Add source coordinate attributes for the given decl. */
17259
17260 static void
17261 add_src_coords_attributes (dw_die_ref die, tree decl)
17262 {
17263 expanded_location s;
17264
17265 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17266 return;
17267 s = expand_location (DECL_SOURCE_LOCATION (decl));
17268 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17269 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17270 }
17271
17272 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17273
17274 static void
17275 add_linkage_name (dw_die_ref die, tree decl)
17276 {
17277 if (debug_info_level > DINFO_LEVEL_NONE
17278 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17279 && TREE_PUBLIC (decl)
17280 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17281 && die->die_tag != DW_TAG_member)
17282 {
17283 /* Defer until we have an assembler name set. */
17284 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17285 {
17286 limbo_die_node *asm_name;
17287
17288 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17289 asm_name->die = die;
17290 asm_name->created_for = decl;
17291 asm_name->next = deferred_asm_name;
17292 deferred_asm_name = asm_name;
17293 }
17294 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17295 add_linkage_attr (die, decl);
17296 }
17297 }
17298
17299 /* Add a DW_AT_name attribute and source coordinate attribute for the
17300 given decl, but only if it actually has a name. */
17301
17302 static void
17303 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17304 {
17305 tree decl_name;
17306
17307 decl_name = DECL_NAME (decl);
17308 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17309 {
17310 const char *name = dwarf2_name (decl, 0);
17311 if (name)
17312 add_name_attribute (die, name);
17313 if (! DECL_ARTIFICIAL (decl))
17314 add_src_coords_attributes (die, decl);
17315
17316 add_linkage_name (die, decl);
17317 }
17318
17319 #ifdef VMS_DEBUGGING_INFO
17320 /* Get the function's name, as described by its RTL. This may be different
17321 from the DECL_NAME name used in the source file. */
17322 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17323 {
17324 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17325 XEXP (DECL_RTL (decl), 0), false);
17326 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17327 }
17328 #endif /* VMS_DEBUGGING_INFO */
17329 }
17330
17331 #ifdef VMS_DEBUGGING_INFO
17332 /* Output the debug main pointer die for VMS */
17333
17334 void
17335 dwarf2out_vms_debug_main_pointer (void)
17336 {
17337 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17338 dw_die_ref die;
17339
17340 /* Allocate the VMS debug main subprogram die. */
17341 die = ggc_cleared_alloc<die_node> ();
17342 die->die_tag = DW_TAG_subprogram;
17343 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17344 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17345 current_function_funcdef_no);
17346 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17347
17348 /* Make it the first child of comp_unit_die (). */
17349 die->die_parent = comp_unit_die ();
17350 if (comp_unit_die ()->die_child)
17351 {
17352 die->die_sib = comp_unit_die ()->die_child->die_sib;
17353 comp_unit_die ()->die_child->die_sib = die;
17354 }
17355 else
17356 {
17357 die->die_sib = die;
17358 comp_unit_die ()->die_child = die;
17359 }
17360 }
17361 #endif /* VMS_DEBUGGING_INFO */
17362
17363 /* Push a new declaration scope. */
17364
17365 static void
17366 push_decl_scope (tree scope)
17367 {
17368 vec_safe_push (decl_scope_table, scope);
17369 }
17370
17371 /* Pop a declaration scope. */
17372
17373 static inline void
17374 pop_decl_scope (void)
17375 {
17376 decl_scope_table->pop ();
17377 }
17378
17379 /* walk_tree helper function for uses_local_type, below. */
17380
17381 static tree
17382 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17383 {
17384 if (!TYPE_P (*tp))
17385 *walk_subtrees = 0;
17386 else
17387 {
17388 tree name = TYPE_NAME (*tp);
17389 if (name && DECL_P (name) && decl_function_context (name))
17390 return *tp;
17391 }
17392 return NULL_TREE;
17393 }
17394
17395 /* If TYPE involves a function-local type (including a local typedef to a
17396 non-local type), returns that type; otherwise returns NULL_TREE. */
17397
17398 static tree
17399 uses_local_type (tree type)
17400 {
17401 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17402 return used;
17403 }
17404
17405 /* Return the DIE for the scope that immediately contains this type.
17406 Non-named types that do not involve a function-local type get global
17407 scope. Named types nested in namespaces or other types get their
17408 containing scope. All other types (i.e. function-local named types) get
17409 the current active scope. */
17410
17411 static dw_die_ref
17412 scope_die_for (tree t, dw_die_ref context_die)
17413 {
17414 dw_die_ref scope_die = NULL;
17415 tree containing_scope;
17416
17417 /* Non-types always go in the current scope. */
17418 gcc_assert (TYPE_P (t));
17419
17420 /* Use the scope of the typedef, rather than the scope of the type
17421 it refers to. */
17422 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17423 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17424 else
17425 containing_scope = TYPE_CONTEXT (t);
17426
17427 /* Use the containing namespace if there is one. */
17428 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17429 {
17430 if (context_die == lookup_decl_die (containing_scope))
17431 /* OK */;
17432 else if (debug_info_level > DINFO_LEVEL_TERSE)
17433 context_die = get_context_die (containing_scope);
17434 else
17435 containing_scope = NULL_TREE;
17436 }
17437
17438 /* Ignore function type "scopes" from the C frontend. They mean that
17439 a tagged type is local to a parmlist of a function declarator, but
17440 that isn't useful to DWARF. */
17441 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17442 containing_scope = NULL_TREE;
17443
17444 if (SCOPE_FILE_SCOPE_P (containing_scope))
17445 {
17446 /* If T uses a local type keep it local as well, to avoid references
17447 to function-local DIEs from outside the function. */
17448 if (current_function_decl && uses_local_type (t))
17449 scope_die = context_die;
17450 else
17451 scope_die = comp_unit_die ();
17452 }
17453 else if (TYPE_P (containing_scope))
17454 {
17455 /* For types, we can just look up the appropriate DIE. */
17456 if (debug_info_level > DINFO_LEVEL_TERSE)
17457 scope_die = get_context_die (containing_scope);
17458 else
17459 {
17460 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17461 if (scope_die == NULL)
17462 scope_die = comp_unit_die ();
17463 }
17464 }
17465 else
17466 scope_die = context_die;
17467
17468 return scope_die;
17469 }
17470
17471 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17472
17473 static inline int
17474 local_scope_p (dw_die_ref context_die)
17475 {
17476 for (; context_die; context_die = context_die->die_parent)
17477 if (context_die->die_tag == DW_TAG_inlined_subroutine
17478 || context_die->die_tag == DW_TAG_subprogram)
17479 return 1;
17480
17481 return 0;
17482 }
17483
17484 /* Returns nonzero if CONTEXT_DIE is a class. */
17485
17486 static inline int
17487 class_scope_p (dw_die_ref context_die)
17488 {
17489 return (context_die
17490 && (context_die->die_tag == DW_TAG_structure_type
17491 || context_die->die_tag == DW_TAG_class_type
17492 || context_die->die_tag == DW_TAG_interface_type
17493 || context_die->die_tag == DW_TAG_union_type));
17494 }
17495
17496 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17497 whether or not to treat a DIE in this context as a declaration. */
17498
17499 static inline int
17500 class_or_namespace_scope_p (dw_die_ref context_die)
17501 {
17502 return (class_scope_p (context_die)
17503 || (context_die && context_die->die_tag == DW_TAG_namespace));
17504 }
17505
17506 /* Many forms of DIEs require a "type description" attribute. This
17507 routine locates the proper "type descriptor" die for the type given
17508 by 'type' plus any additional qualifiers given by 'cv_quals', and
17509 adds a DW_AT_type attribute below the given die. */
17510
17511 static void
17512 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17513 dw_die_ref context_die)
17514 {
17515 enum tree_code code = TREE_CODE (type);
17516 dw_die_ref type_die = NULL;
17517
17518 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17519 or fixed-point type, use the inner type. This is because we have no
17520 support for unnamed types in base_type_die. This can happen if this is
17521 an Ada subrange type. Correct solution is emit a subrange type die. */
17522 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17523 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17524 type = TREE_TYPE (type), code = TREE_CODE (type);
17525
17526 if (code == ERROR_MARK
17527 /* Handle a special case. For functions whose return type is void, we
17528 generate *no* type attribute. (Note that no object may have type
17529 `void', so this only applies to function return types). */
17530 || code == VOID_TYPE)
17531 return;
17532
17533 type_die = modified_type_die (type,
17534 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17535 context_die);
17536
17537 if (type_die != NULL)
17538 add_AT_die_ref (object_die, DW_AT_type, type_die);
17539 }
17540
17541 /* Given an object die, add the calling convention attribute for the
17542 function call type. */
17543 static void
17544 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17545 {
17546 enum dwarf_calling_convention value = DW_CC_normal;
17547
17548 value = ((enum dwarf_calling_convention)
17549 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17550
17551 if (is_fortran ()
17552 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17553 {
17554 /* DWARF 2 doesn't provide a way to identify a program's source-level
17555 entry point. DW_AT_calling_convention attributes are only meant
17556 to describe functions' calling conventions. However, lacking a
17557 better way to signal the Fortran main program, we used this for
17558 a long time, following existing custom. Now, DWARF 4 has
17559 DW_AT_main_subprogram, which we add below, but some tools still
17560 rely on the old way, which we thus keep. */
17561 value = DW_CC_program;
17562
17563 if (dwarf_version >= 4 || !dwarf_strict)
17564 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17565 }
17566
17567 /* Only add the attribute if the backend requests it, and
17568 is not DW_CC_normal. */
17569 if (value && (value != DW_CC_normal))
17570 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17571 }
17572
17573 /* Given a tree pointer to a struct, class, union, or enum type node, return
17574 a pointer to the (string) tag name for the given type, or zero if the type
17575 was declared without a tag. */
17576
17577 static const char *
17578 type_tag (const_tree type)
17579 {
17580 const char *name = 0;
17581
17582 if (TYPE_NAME (type) != 0)
17583 {
17584 tree t = 0;
17585
17586 /* Find the IDENTIFIER_NODE for the type name. */
17587 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17588 && !TYPE_NAMELESS (type))
17589 t = TYPE_NAME (type);
17590
17591 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17592 a TYPE_DECL node, regardless of whether or not a `typedef' was
17593 involved. */
17594 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17595 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17596 {
17597 /* We want to be extra verbose. Don't call dwarf_name if
17598 DECL_NAME isn't set. The default hook for decl_printable_name
17599 doesn't like that, and in this context it's correct to return
17600 0, instead of "<anonymous>" or the like. */
17601 if (DECL_NAME (TYPE_NAME (type))
17602 && !DECL_NAMELESS (TYPE_NAME (type)))
17603 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17604 }
17605
17606 /* Now get the name as a string, or invent one. */
17607 if (!name && t != 0)
17608 name = IDENTIFIER_POINTER (t);
17609 }
17610
17611 return (name == 0 || *name == '\0') ? 0 : name;
17612 }
17613
17614 /* Return the type associated with a data member, make a special check
17615 for bit field types. */
17616
17617 static inline tree
17618 member_declared_type (const_tree member)
17619 {
17620 return (DECL_BIT_FIELD_TYPE (member)
17621 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17622 }
17623
17624 /* Get the decl's label, as described by its RTL. This may be different
17625 from the DECL_NAME name used in the source file. */
17626
17627 #if 0
17628 static const char *
17629 decl_start_label (tree decl)
17630 {
17631 rtx x;
17632 const char *fnname;
17633
17634 x = DECL_RTL (decl);
17635 gcc_assert (MEM_P (x));
17636
17637 x = XEXP (x, 0);
17638 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17639
17640 fnname = XSTR (x, 0);
17641 return fnname;
17642 }
17643 #endif
17644 \f
17645 /* For variable-length arrays that have been previously generated, but
17646 may be incomplete due to missing subscript info, fill the subscript
17647 info. Return TRUE if this is one of those cases. */
17648 static bool
17649 fill_variable_array_bounds (tree type)
17650 {
17651 if (TREE_ASM_WRITTEN (type)
17652 && TREE_CODE (type) == ARRAY_TYPE
17653 && variably_modified_type_p (type, NULL))
17654 {
17655 dw_die_ref array_die = lookup_type_die (type);
17656 if (!array_die)
17657 return false;
17658 add_subscript_info (array_die, type, !is_ada ());
17659 return true;
17660 }
17661 return false;
17662 }
17663
17664 /* These routines generate the internal representation of the DIE's for
17665 the compilation unit. Debugging information is collected by walking
17666 the declaration trees passed in from dwarf2out_decl(). */
17667
17668 static void
17669 gen_array_type_die (tree type, dw_die_ref context_die)
17670 {
17671 dw_die_ref array_die;
17672
17673 /* GNU compilers represent multidimensional array types as sequences of one
17674 dimensional array types whose element types are themselves array types.
17675 We sometimes squish that down to a single array_type DIE with multiple
17676 subscripts in the Dwarf debugging info. The draft Dwarf specification
17677 say that we are allowed to do this kind of compression in C, because
17678 there is no difference between an array of arrays and a multidimensional
17679 array. We don't do this for Ada to remain as close as possible to the
17680 actual representation, which is especially important against the language
17681 flexibilty wrt arrays of variable size. */
17682
17683 bool collapse_nested_arrays = !is_ada ();
17684
17685 if (fill_variable_array_bounds (type))
17686 return;
17687
17688 dw_die_ref scope_die = scope_die_for (type, context_die);
17689 tree element_type;
17690
17691 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17692 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17693 if (TYPE_STRING_FLAG (type)
17694 && TREE_CODE (type) == ARRAY_TYPE
17695 && is_fortran ()
17696 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17697 {
17698 HOST_WIDE_INT size;
17699
17700 array_die = new_die (DW_TAG_string_type, scope_die, type);
17701 add_name_attribute (array_die, type_tag (type));
17702 equate_type_number_to_die (type, array_die);
17703 size = int_size_in_bytes (type);
17704 if (size >= 0)
17705 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17706 else if (TYPE_DOMAIN (type) != NULL_TREE
17707 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17708 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17709 {
17710 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17711 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17712
17713 size = int_size_in_bytes (TREE_TYPE (szdecl));
17714 if (loc && size > 0)
17715 {
17716 add_AT_location_description (array_die, DW_AT_string_length, loc);
17717 if (size != DWARF2_ADDR_SIZE)
17718 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17719 }
17720 }
17721 return;
17722 }
17723
17724 array_die = new_die (DW_TAG_array_type, scope_die, type);
17725 add_name_attribute (array_die, type_tag (type));
17726 equate_type_number_to_die (type, array_die);
17727
17728 if (TREE_CODE (type) == VECTOR_TYPE)
17729 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17730
17731 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17732 if (is_fortran ()
17733 && TREE_CODE (type) == ARRAY_TYPE
17734 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17735 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17736 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17737
17738 #if 0
17739 /* We default the array ordering. SDB will probably do
17740 the right things even if DW_AT_ordering is not present. It's not even
17741 an issue until we start to get into multidimensional arrays anyway. If
17742 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17743 then we'll have to put the DW_AT_ordering attribute back in. (But if
17744 and when we find out that we need to put these in, we will only do so
17745 for multidimensional arrays. */
17746 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17747 #endif
17748
17749 if (TREE_CODE (type) == VECTOR_TYPE)
17750 {
17751 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17752 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17753 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17754 add_bound_info (subrange_die, DW_AT_upper_bound,
17755 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17756 }
17757 else
17758 add_subscript_info (array_die, type, collapse_nested_arrays);
17759
17760 /* Add representation of the type of the elements of this array type and
17761 emit the corresponding DIE if we haven't done it already. */
17762 element_type = TREE_TYPE (type);
17763 if (collapse_nested_arrays)
17764 while (TREE_CODE (element_type) == ARRAY_TYPE)
17765 {
17766 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17767 break;
17768 element_type = TREE_TYPE (element_type);
17769 }
17770
17771 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17772
17773 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17774 if (TYPE_ARTIFICIAL (type))
17775 add_AT_flag (array_die, DW_AT_artificial, 1);
17776
17777 if (get_AT (array_die, DW_AT_name))
17778 add_pubtype (type, array_die);
17779 }
17780
17781 /* This routine generates DIE for array with hidden descriptor, details
17782 are filled into *info by a langhook. */
17783
17784 static void
17785 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17786 dw_die_ref context_die)
17787 {
17788 const dw_die_ref scope_die = scope_die_for (type, context_die);
17789 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17790 const struct loc_descr_context context = { type, info->base_decl };
17791 int dim;
17792
17793 add_name_attribute (array_die, type_tag (type));
17794 equate_type_number_to_die (type, array_die);
17795
17796 if (info->ndimensions > 1)
17797 switch (info->ordering)
17798 {
17799 case array_descr_ordering_row_major:
17800 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17801 break;
17802 case array_descr_ordering_column_major:
17803 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17804 break;
17805 default:
17806 break;
17807 }
17808
17809 if (dwarf_version >= 3 || !dwarf_strict)
17810 {
17811 if (info->data_location)
17812 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17813 dw_scalar_form_exprloc, &context);
17814 if (info->associated)
17815 add_scalar_info (array_die, DW_AT_associated, info->associated,
17816 dw_scalar_form_constant
17817 | dw_scalar_form_exprloc
17818 | dw_scalar_form_reference, &context);
17819 if (info->allocated)
17820 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17821 dw_scalar_form_constant
17822 | dw_scalar_form_exprloc
17823 | dw_scalar_form_reference, &context);
17824 }
17825
17826 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17827
17828 for (dim = 0; dim < info->ndimensions; dim++)
17829 {
17830 dw_die_ref subrange_die
17831 = new_die (DW_TAG_subrange_type, array_die, NULL);
17832
17833 if (info->dimen[dim].bounds_type)
17834 add_type_attribute (subrange_die,
17835 info->dimen[dim].bounds_type, 0,
17836 context_die);
17837 if (info->dimen[dim].lower_bound)
17838 add_bound_info (subrange_die, DW_AT_lower_bound,
17839 info->dimen[dim].lower_bound, &context);
17840 if (info->dimen[dim].upper_bound)
17841 add_bound_info (subrange_die, DW_AT_upper_bound,
17842 info->dimen[dim].upper_bound, &context);
17843 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17844 add_scalar_info (subrange_die, DW_AT_byte_stride,
17845 info->dimen[dim].stride,
17846 dw_scalar_form_constant
17847 | dw_scalar_form_exprloc
17848 | dw_scalar_form_reference,
17849 &context);
17850 }
17851
17852 gen_type_die (info->element_type, context_die);
17853 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17854 context_die);
17855
17856 if (get_AT (array_die, DW_AT_name))
17857 add_pubtype (type, array_die);
17858 }
17859
17860 #if 0
17861 static void
17862 gen_entry_point_die (tree decl, dw_die_ref context_die)
17863 {
17864 tree origin = decl_ultimate_origin (decl);
17865 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17866
17867 if (origin != NULL)
17868 add_abstract_origin_attribute (decl_die, origin);
17869 else
17870 {
17871 add_name_and_src_coords_attributes (decl_die, decl);
17872 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17873 TYPE_UNQUALIFIED, context_die);
17874 }
17875
17876 if (DECL_ABSTRACT_P (decl))
17877 equate_decl_number_to_die (decl, decl_die);
17878 else
17879 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17880 }
17881 #endif
17882
17883 /* Walk through the list of incomplete types again, trying once more to
17884 emit full debugging info for them. */
17885
17886 static void
17887 retry_incomplete_types (void)
17888 {
17889 int i;
17890
17891 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17892 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17893 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17894 }
17895
17896 /* Determine what tag to use for a record type. */
17897
17898 static enum dwarf_tag
17899 record_type_tag (tree type)
17900 {
17901 if (! lang_hooks.types.classify_record)
17902 return DW_TAG_structure_type;
17903
17904 switch (lang_hooks.types.classify_record (type))
17905 {
17906 case RECORD_IS_STRUCT:
17907 return DW_TAG_structure_type;
17908
17909 case RECORD_IS_CLASS:
17910 return DW_TAG_class_type;
17911
17912 case RECORD_IS_INTERFACE:
17913 if (dwarf_version >= 3 || !dwarf_strict)
17914 return DW_TAG_interface_type;
17915 return DW_TAG_structure_type;
17916
17917 default:
17918 gcc_unreachable ();
17919 }
17920 }
17921
17922 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17923 include all of the information about the enumeration values also. Each
17924 enumerated type name/value is listed as a child of the enumerated type
17925 DIE. */
17926
17927 static dw_die_ref
17928 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17929 {
17930 dw_die_ref type_die = lookup_type_die (type);
17931
17932 if (type_die == NULL)
17933 {
17934 type_die = new_die (DW_TAG_enumeration_type,
17935 scope_die_for (type, context_die), type);
17936 equate_type_number_to_die (type, type_die);
17937 add_name_attribute (type_die, type_tag (type));
17938 if (dwarf_version >= 4 || !dwarf_strict)
17939 {
17940 if (ENUM_IS_SCOPED (type))
17941 add_AT_flag (type_die, DW_AT_enum_class, 1);
17942 if (ENUM_IS_OPAQUE (type))
17943 add_AT_flag (type_die, DW_AT_declaration, 1);
17944 }
17945 }
17946 else if (! TYPE_SIZE (type))
17947 return type_die;
17948 else
17949 remove_AT (type_die, DW_AT_declaration);
17950
17951 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17952 given enum type is incomplete, do not generate the DW_AT_byte_size
17953 attribute or the DW_AT_element_list attribute. */
17954 if (TYPE_SIZE (type))
17955 {
17956 tree link;
17957
17958 TREE_ASM_WRITTEN (type) = 1;
17959 add_byte_size_attribute (type_die, type);
17960 if (dwarf_version >= 3 || !dwarf_strict)
17961 {
17962 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17963 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17964 context_die);
17965 }
17966 if (TYPE_STUB_DECL (type) != NULL_TREE)
17967 {
17968 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17969 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17970 }
17971
17972 /* If the first reference to this type was as the return type of an
17973 inline function, then it may not have a parent. Fix this now. */
17974 if (type_die->die_parent == NULL)
17975 add_child_die (scope_die_for (type, context_die), type_die);
17976
17977 for (link = TYPE_VALUES (type);
17978 link != NULL; link = TREE_CHAIN (link))
17979 {
17980 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17981 tree value = TREE_VALUE (link);
17982
17983 add_name_attribute (enum_die,
17984 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17985
17986 if (TREE_CODE (value) == CONST_DECL)
17987 value = DECL_INITIAL (value);
17988
17989 if (simple_type_size_in_bits (TREE_TYPE (value))
17990 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17991 {
17992 /* For constant forms created by add_AT_unsigned DWARF
17993 consumers (GDB, elfutils, etc.) always zero extend
17994 the value. Only when the actual value is negative
17995 do we need to use add_AT_int to generate a constant
17996 form that can represent negative values. */
17997 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17998 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17999 add_AT_unsigned (enum_die, DW_AT_const_value,
18000 (unsigned HOST_WIDE_INT) val);
18001 else
18002 add_AT_int (enum_die, DW_AT_const_value, val);
18003 }
18004 else
18005 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
18006 that here. TODO: This should be re-worked to use correct
18007 signed/unsigned double tags for all cases. */
18008 add_AT_wide (enum_die, DW_AT_const_value, value);
18009 }
18010
18011 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18012 if (TYPE_ARTIFICIAL (type))
18013 add_AT_flag (type_die, DW_AT_artificial, 1);
18014 }
18015 else
18016 add_AT_flag (type_die, DW_AT_declaration, 1);
18017
18018 add_pubtype (type, type_die);
18019
18020 return type_die;
18021 }
18022
18023 /* Generate a DIE to represent either a real live formal parameter decl or to
18024 represent just the type of some formal parameter position in some function
18025 type.
18026
18027 Note that this routine is a bit unusual because its argument may be a
18028 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18029 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18030 node. If it's the former then this function is being called to output a
18031 DIE to represent a formal parameter object (or some inlining thereof). If
18032 it's the latter, then this function is only being called to output a
18033 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18034 argument type of some subprogram type.
18035 If EMIT_NAME_P is true, name and source coordinate attributes
18036 are emitted. */
18037
18038 static dw_die_ref
18039 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18040 dw_die_ref context_die)
18041 {
18042 tree node_or_origin = node ? node : origin;
18043 tree ultimate_origin;
18044 dw_die_ref parm_die = NULL;
18045
18046 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18047 {
18048 parm_die = lookup_decl_die (node);
18049
18050 /* If the contexts differ, we may not be talking about the same
18051 thing. */
18052 if (parm_die && parm_die->die_parent != context_die)
18053 {
18054 if (!DECL_ABSTRACT_P (node))
18055 {
18056 /* This can happen when creating an inlined instance, in
18057 which case we need to create a new DIE that will get
18058 annotated with DW_AT_abstract_origin. */
18059 parm_die = NULL;
18060 }
18061 else
18062 {
18063 /* FIXME: Reuse DIE even with a differing context.
18064
18065 This can happen when calling
18066 dwarf2out_abstract_function to build debug info for
18067 the abstract instance of a function for which we have
18068 already generated a DIE in
18069 dwarf2out_early_global_decl.
18070
18071 Once we remove dwarf2out_abstract_function, we should
18072 have a call to gcc_unreachable here. */
18073 }
18074 }
18075
18076 if (parm_die && parm_die->die_parent == NULL)
18077 {
18078 /* Check that parm_die already has the right attributes that
18079 we would have added below. If any attributes are
18080 missing, fall through to add them. */
18081 if (! DECL_ABSTRACT_P (node_or_origin)
18082 && !get_AT (parm_die, DW_AT_location)
18083 && !get_AT (parm_die, DW_AT_const_value))
18084 /* We are missing location info, and are about to add it. */
18085 ;
18086 else
18087 {
18088 add_child_die (context_die, parm_die);
18089 return parm_die;
18090 }
18091 }
18092 }
18093
18094 /* If we have a previously generated DIE, use it, unless this is an
18095 concrete instance (origin != NULL), in which case we need a new
18096 DIE with a corresponding DW_AT_abstract_origin. */
18097 bool reusing_die;
18098 if (parm_die && origin == NULL)
18099 reusing_die = true;
18100 else
18101 {
18102 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18103 reusing_die = false;
18104 }
18105
18106 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18107 {
18108 case tcc_declaration:
18109 ultimate_origin = decl_ultimate_origin (node_or_origin);
18110 if (node || ultimate_origin)
18111 origin = ultimate_origin;
18112
18113 if (reusing_die)
18114 goto add_location;
18115
18116 if (origin != NULL)
18117 add_abstract_origin_attribute (parm_die, origin);
18118 else if (emit_name_p)
18119 add_name_and_src_coords_attributes (parm_die, node);
18120 if (origin == NULL
18121 || (! DECL_ABSTRACT_P (node_or_origin)
18122 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18123 decl_function_context
18124 (node_or_origin))))
18125 {
18126 tree type = TREE_TYPE (node_or_origin);
18127 if (decl_by_reference_p (node_or_origin))
18128 add_type_attribute (parm_die, TREE_TYPE (type),
18129 TYPE_UNQUALIFIED, context_die);
18130 else
18131 add_type_attribute (parm_die, type,
18132 decl_quals (node_or_origin),
18133 context_die);
18134 }
18135 if (origin == NULL && DECL_ARTIFICIAL (node))
18136 add_AT_flag (parm_die, DW_AT_artificial, 1);
18137 add_location:
18138 if (node && node != origin)
18139 equate_decl_number_to_die (node, parm_die);
18140 if (! DECL_ABSTRACT_P (node_or_origin))
18141 add_location_or_const_value_attribute (parm_die, node_or_origin,
18142 node == NULL);
18143
18144 break;
18145
18146 case tcc_type:
18147 /* We were called with some kind of a ..._TYPE node. */
18148 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18149 context_die);
18150 break;
18151
18152 default:
18153 gcc_unreachable ();
18154 }
18155
18156 return parm_die;
18157 }
18158
18159 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18160 children DW_TAG_formal_parameter DIEs representing the arguments of the
18161 parameter pack.
18162
18163 PARM_PACK must be a function parameter pack.
18164 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18165 must point to the subsequent arguments of the function PACK_ARG belongs to.
18166 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18167 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18168 following the last one for which a DIE was generated. */
18169
18170 static dw_die_ref
18171 gen_formal_parameter_pack_die (tree parm_pack,
18172 tree pack_arg,
18173 dw_die_ref subr_die,
18174 tree *next_arg)
18175 {
18176 tree arg;
18177 dw_die_ref parm_pack_die;
18178
18179 gcc_assert (parm_pack
18180 && lang_hooks.function_parameter_pack_p (parm_pack)
18181 && subr_die);
18182
18183 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18184 add_src_coords_attributes (parm_pack_die, parm_pack);
18185
18186 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18187 {
18188 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18189 parm_pack))
18190 break;
18191 gen_formal_parameter_die (arg, NULL,
18192 false /* Don't emit name attribute. */,
18193 parm_pack_die);
18194 }
18195 if (next_arg)
18196 *next_arg = arg;
18197 return parm_pack_die;
18198 }
18199
18200 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18201 at the end of an (ANSI prototyped) formal parameters list. */
18202
18203 static void
18204 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18205 {
18206 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18207 }
18208
18209 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18210 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18211 parameters as specified in some function type specification (except for
18212 those which appear as part of a function *definition*). */
18213
18214 static void
18215 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18216 {
18217 tree link;
18218 tree formal_type = NULL;
18219 tree first_parm_type;
18220 tree arg;
18221
18222 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18223 {
18224 arg = DECL_ARGUMENTS (function_or_method_type);
18225 function_or_method_type = TREE_TYPE (function_or_method_type);
18226 }
18227 else
18228 arg = NULL_TREE;
18229
18230 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18231
18232 /* Make our first pass over the list of formal parameter types and output a
18233 DW_TAG_formal_parameter DIE for each one. */
18234 for (link = first_parm_type; link; )
18235 {
18236 dw_die_ref parm_die;
18237
18238 formal_type = TREE_VALUE (link);
18239 if (formal_type == void_type_node)
18240 break;
18241
18242 /* Output a (nameless) DIE to represent the formal parameter itself. */
18243 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18244 {
18245 parm_die = gen_formal_parameter_die (formal_type, NULL,
18246 true /* Emit name attribute. */,
18247 context_die);
18248 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18249 && link == first_parm_type)
18250 {
18251 add_AT_flag (parm_die, DW_AT_artificial, 1);
18252 if (dwarf_version >= 3 || !dwarf_strict)
18253 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18254 }
18255 else if (arg && DECL_ARTIFICIAL (arg))
18256 add_AT_flag (parm_die, DW_AT_artificial, 1);
18257 }
18258
18259 link = TREE_CHAIN (link);
18260 if (arg)
18261 arg = DECL_CHAIN (arg);
18262 }
18263
18264 /* If this function type has an ellipsis, add a
18265 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18266 if (formal_type != void_type_node)
18267 gen_unspecified_parameters_die (function_or_method_type, context_die);
18268
18269 /* Make our second (and final) pass over the list of formal parameter types
18270 and output DIEs to represent those types (as necessary). */
18271 for (link = TYPE_ARG_TYPES (function_or_method_type);
18272 link && TREE_VALUE (link);
18273 link = TREE_CHAIN (link))
18274 gen_type_die (TREE_VALUE (link), context_die);
18275 }
18276
18277 /* We want to generate the DIE for TYPE so that we can generate the
18278 die for MEMBER, which has been defined; we will need to refer back
18279 to the member declaration nested within TYPE. If we're trying to
18280 generate minimal debug info for TYPE, processing TYPE won't do the
18281 trick; we need to attach the member declaration by hand. */
18282
18283 static void
18284 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18285 {
18286 gen_type_die (type, context_die);
18287
18288 /* If we're trying to avoid duplicate debug info, we may not have
18289 emitted the member decl for this function. Emit it now. */
18290 if (TYPE_STUB_DECL (type)
18291 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18292 && ! lookup_decl_die (member))
18293 {
18294 dw_die_ref type_die;
18295 gcc_assert (!decl_ultimate_origin (member));
18296
18297 push_decl_scope (type);
18298 type_die = lookup_type_die_strip_naming_typedef (type);
18299 if (TREE_CODE (member) == FUNCTION_DECL)
18300 gen_subprogram_die (member, type_die);
18301 else if (TREE_CODE (member) == FIELD_DECL)
18302 {
18303 /* Ignore the nameless fields that are used to skip bits but handle
18304 C++ anonymous unions and structs. */
18305 if (DECL_NAME (member) != NULL_TREE
18306 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18307 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18308 {
18309 gen_type_die (member_declared_type (member), type_die);
18310 gen_field_die (member, type_die);
18311 }
18312 }
18313 else
18314 gen_variable_die (member, NULL_TREE, type_die);
18315
18316 pop_decl_scope ();
18317 }
18318 }
18319 \f
18320 /* Forward declare these functions, because they are mutually recursive
18321 with their set_block_* pairing functions. */
18322 static void set_decl_origin_self (tree);
18323 static void set_decl_abstract_flags (tree, vec<tree> &);
18324
18325 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18326 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18327 that it points to the node itself, thus indicating that the node is its
18328 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18329 the given node is NULL, recursively descend the decl/block tree which
18330 it is the root of, and for each other ..._DECL or BLOCK node contained
18331 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18332 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18333 values to point to themselves. */
18334
18335 static void
18336 set_block_origin_self (tree stmt)
18337 {
18338 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18339 {
18340 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18341
18342 {
18343 tree local_decl;
18344
18345 for (local_decl = BLOCK_VARS (stmt);
18346 local_decl != NULL_TREE;
18347 local_decl = DECL_CHAIN (local_decl))
18348 /* Do not recurse on nested functions since the inlining status
18349 of parent and child can be different as per the DWARF spec. */
18350 if (TREE_CODE (local_decl) != FUNCTION_DECL
18351 && !DECL_EXTERNAL (local_decl))
18352 set_decl_origin_self (local_decl);
18353 }
18354
18355 {
18356 tree subblock;
18357
18358 for (subblock = BLOCK_SUBBLOCKS (stmt);
18359 subblock != NULL_TREE;
18360 subblock = BLOCK_CHAIN (subblock))
18361 set_block_origin_self (subblock); /* Recurse. */
18362 }
18363 }
18364 }
18365
18366 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18367 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18368 node to so that it points to the node itself, thus indicating that the
18369 node represents its own (abstract) origin. Additionally, if the
18370 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18371 the decl/block tree of which the given node is the root of, and for
18372 each other ..._DECL or BLOCK node contained therein whose
18373 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18374 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18375 point to themselves. */
18376
18377 static void
18378 set_decl_origin_self (tree decl)
18379 {
18380 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18381 {
18382 DECL_ABSTRACT_ORIGIN (decl) = decl;
18383 if (TREE_CODE (decl) == FUNCTION_DECL)
18384 {
18385 tree arg;
18386
18387 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18388 DECL_ABSTRACT_ORIGIN (arg) = arg;
18389 if (DECL_INITIAL (decl) != NULL_TREE
18390 && DECL_INITIAL (decl) != error_mark_node)
18391 set_block_origin_self (DECL_INITIAL (decl));
18392 }
18393 }
18394 }
18395 \f
18396 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18397 and if it wasn't 1 before, push it to abstract_vec vector.
18398 For all local decls and all local sub-blocks (recursively) do it
18399 too. */
18400
18401 static void
18402 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18403 {
18404 tree local_decl;
18405 tree subblock;
18406 unsigned int i;
18407
18408 if (!BLOCK_ABSTRACT (stmt))
18409 {
18410 abstract_vec.safe_push (stmt);
18411 BLOCK_ABSTRACT (stmt) = 1;
18412 }
18413
18414 for (local_decl = BLOCK_VARS (stmt);
18415 local_decl != NULL_TREE;
18416 local_decl = DECL_CHAIN (local_decl))
18417 if (! DECL_EXTERNAL (local_decl))
18418 set_decl_abstract_flags (local_decl, abstract_vec);
18419
18420 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18421 {
18422 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18423 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18424 || TREE_CODE (local_decl) == PARM_DECL)
18425 set_decl_abstract_flags (local_decl, abstract_vec);
18426 }
18427
18428 for (subblock = BLOCK_SUBBLOCKS (stmt);
18429 subblock != NULL_TREE;
18430 subblock = BLOCK_CHAIN (subblock))
18431 set_block_abstract_flags (subblock, abstract_vec);
18432 }
18433
18434 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18435 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18436 In the case where the decl is a FUNCTION_DECL also set the abstract
18437 flags for all of the parameters, local vars, local
18438 blocks and sub-blocks (recursively). */
18439
18440 static void
18441 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18442 {
18443 if (!DECL_ABSTRACT_P (decl))
18444 {
18445 abstract_vec.safe_push (decl);
18446 DECL_ABSTRACT_P (decl) = 1;
18447 }
18448
18449 if (TREE_CODE (decl) == FUNCTION_DECL)
18450 {
18451 tree arg;
18452
18453 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18454 if (!DECL_ABSTRACT_P (arg))
18455 {
18456 abstract_vec.safe_push (arg);
18457 DECL_ABSTRACT_P (arg) = 1;
18458 }
18459 if (DECL_INITIAL (decl) != NULL_TREE
18460 && DECL_INITIAL (decl) != error_mark_node)
18461 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18462 }
18463 }
18464
18465 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18466 may later generate inlined and/or out-of-line instances of.
18467
18468 FIXME: In the early-dwarf world, this function, and most of the
18469 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18470 the abstract instance. All we would need to do is annotate
18471 the early DIE with the appropriate DW_AT_inline in late
18472 dwarf (perhaps in gen_inlined_subroutine_die).
18473
18474 However, we can't do this yet, because LTO streaming of DIEs
18475 has not been implemented yet. */
18476
18477 static void
18478 dwarf2out_abstract_function (tree decl)
18479 {
18480 dw_die_ref old_die;
18481 tree save_fn;
18482 tree context;
18483 hash_table<decl_loc_hasher> *old_decl_loc_table;
18484 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18485 int old_call_site_count, old_tail_call_site_count;
18486 struct call_arg_loc_node *old_call_arg_locations;
18487
18488 /* Make sure we have the actual abstract inline, not a clone. */
18489 decl = DECL_ORIGIN (decl);
18490
18491 old_die = lookup_decl_die (decl);
18492 if (old_die && get_AT (old_die, DW_AT_inline))
18493 /* We've already generated the abstract instance. */
18494 return;
18495
18496 /* We can be called while recursively when seeing block defining inlined subroutine
18497 DIE. Be sure to not clobber the outer location table nor use it or we would
18498 get locations in abstract instantces. */
18499 old_decl_loc_table = decl_loc_table;
18500 decl_loc_table = NULL;
18501 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18502 cached_dw_loc_list_table = NULL;
18503 old_call_arg_locations = call_arg_locations;
18504 call_arg_locations = NULL;
18505 old_call_site_count = call_site_count;
18506 call_site_count = -1;
18507 old_tail_call_site_count = tail_call_site_count;
18508 tail_call_site_count = -1;
18509
18510 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18511 we don't get confused by DECL_ABSTRACT_P. */
18512 if (debug_info_level > DINFO_LEVEL_TERSE)
18513 {
18514 context = decl_class_context (decl);
18515 if (context)
18516 gen_type_die_for_member
18517 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18518 }
18519
18520 /* Pretend we've just finished compiling this function. */
18521 save_fn = current_function_decl;
18522 current_function_decl = decl;
18523
18524 auto_vec<tree, 64> abstract_vec;
18525 set_decl_abstract_flags (decl, abstract_vec);
18526 dwarf2out_decl (decl);
18527 unsigned int i;
18528 tree t;
18529 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18530 if (TREE_CODE (t) == BLOCK)
18531 BLOCK_ABSTRACT (t) = 0;
18532 else
18533 DECL_ABSTRACT_P (t) = 0;
18534
18535 current_function_decl = save_fn;
18536 decl_loc_table = old_decl_loc_table;
18537 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18538 call_arg_locations = old_call_arg_locations;
18539 call_site_count = old_call_site_count;
18540 tail_call_site_count = old_tail_call_site_count;
18541 }
18542
18543 /* Helper function of premark_used_types() which gets called through
18544 htab_traverse.
18545
18546 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18547 marked as unused by prune_unused_types. */
18548
18549 bool
18550 premark_used_types_helper (tree const &type, void *)
18551 {
18552 dw_die_ref die;
18553
18554 die = lookup_type_die (type);
18555 if (die != NULL)
18556 die->die_perennial_p = 1;
18557 return true;
18558 }
18559
18560 /* Helper function of premark_types_used_by_global_vars which gets called
18561 through htab_traverse.
18562
18563 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18564 marked as unused by prune_unused_types. The DIE of the type is marked
18565 only if the global variable using the type will actually be emitted. */
18566
18567 int
18568 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18569 void *)
18570 {
18571 struct types_used_by_vars_entry *entry;
18572 dw_die_ref die;
18573
18574 entry = (struct types_used_by_vars_entry *) *slot;
18575 gcc_assert (entry->type != NULL
18576 && entry->var_decl != NULL);
18577 die = lookup_type_die (entry->type);
18578 if (die)
18579 {
18580 /* Ask cgraph if the global variable really is to be emitted.
18581 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18582 varpool_node *node = varpool_node::get (entry->var_decl);
18583 if (node && node->definition)
18584 {
18585 die->die_perennial_p = 1;
18586 /* Keep the parent DIEs as well. */
18587 while ((die = die->die_parent) && die->die_perennial_p == 0)
18588 die->die_perennial_p = 1;
18589 }
18590 }
18591 return 1;
18592 }
18593
18594 /* Mark all members of used_types_hash as perennial. */
18595
18596 static void
18597 premark_used_types (struct function *fun)
18598 {
18599 if (fun && fun->used_types_hash)
18600 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18601 }
18602
18603 /* Mark all members of types_used_by_vars_entry as perennial. */
18604
18605 static void
18606 premark_types_used_by_global_vars (void)
18607 {
18608 if (types_used_by_vars_hash)
18609 types_used_by_vars_hash
18610 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18611 }
18612
18613 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18614 for CA_LOC call arg loc node. */
18615
18616 static dw_die_ref
18617 gen_call_site_die (tree decl, dw_die_ref subr_die,
18618 struct call_arg_loc_node *ca_loc)
18619 {
18620 dw_die_ref stmt_die = NULL, die;
18621 tree block = ca_loc->block;
18622
18623 while (block
18624 && block != DECL_INITIAL (decl)
18625 && TREE_CODE (block) == BLOCK)
18626 {
18627 stmt_die = BLOCK_DIE (block);
18628 if (stmt_die)
18629 break;
18630 block = BLOCK_SUPERCONTEXT (block);
18631 }
18632 if (stmt_die == NULL)
18633 stmt_die = subr_die;
18634 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18635 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18636 if (ca_loc->tail_call_p)
18637 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18638 if (ca_loc->symbol_ref)
18639 {
18640 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18641 if (tdie)
18642 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18643 else
18644 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18645 }
18646 return die;
18647 }
18648
18649 /* Generate a DIE to represent a declared function (either file-scope or
18650 block-local). */
18651
18652 static void
18653 gen_subprogram_die (tree decl, dw_die_ref context_die)
18654 {
18655 tree origin = decl_ultimate_origin (decl);
18656 dw_die_ref subr_die;
18657 dw_die_ref old_die = lookup_decl_die (decl);
18658
18659 /* This function gets called multiple times for different stages of
18660 the debug process. For example, for func() in this code:
18661
18662 namespace S
18663 {
18664 void func() { ... }
18665 }
18666
18667 ...we get called 4 times. Twice in early debug and twice in
18668 late debug:
18669
18670 Early debug
18671 -----------
18672
18673 1. Once while generating func() within the namespace. This is
18674 the declaration. The declaration bit below is set, as the
18675 context is the namespace.
18676
18677 A new DIE will be generated with DW_AT_declaration set.
18678
18679 2. Once for func() itself. This is the specification. The
18680 declaration bit below is clear as the context is the CU.
18681
18682 We will use the cached DIE from (1) to create a new DIE with
18683 DW_AT_specification pointing to the declaration in (1).
18684
18685 Late debug via rest_of_handle_final()
18686 -------------------------------------
18687
18688 3. Once generating func() within the namespace. This is also the
18689 declaration, as in (1), but this time we will early exit below
18690 as we have a cached DIE and a declaration needs no additional
18691 annotations (no locations), as the source declaration line
18692 info is enough.
18693
18694 4. Once for func() itself. As in (2), this is the specification,
18695 but this time we will re-use the cached DIE, and just annotate
18696 it with the location information that should now be available.
18697
18698 For something without namespaces, but with abstract instances, we
18699 are also called a multiple times:
18700
18701 class Base
18702 {
18703 public:
18704 Base (); // constructor declaration (1)
18705 };
18706
18707 Base::Base () { } // constructor specification (2)
18708
18709 Early debug
18710 -----------
18711
18712 1. Once for the Base() constructor by virtue of it being a
18713 member of the Base class. This is done via
18714 rest_of_type_compilation.
18715
18716 This is a declaration, so a new DIE will be created with
18717 DW_AT_declaration.
18718
18719 2. Once for the Base() constructor definition, but this time
18720 while generating the abstract instance of the base
18721 constructor (__base_ctor) which is being generated via early
18722 debug of reachable functions.
18723
18724 Even though we have a cached version of the declaration (1),
18725 we will create a DW_AT_specification of the declaration DIE
18726 in (1).
18727
18728 3. Once for the __base_ctor itself, but this time, we generate
18729 an DW_AT_abstract_origin version of the DW_AT_specification in
18730 (2).
18731
18732 Late debug via rest_of_handle_final
18733 -----------------------------------
18734
18735 4. One final time for the __base_ctor (which will have a cached
18736 DIE with DW_AT_abstract_origin created in (3). This time,
18737 we will just annotate the location information now
18738 available.
18739 */
18740 int declaration = (current_function_decl != decl
18741 || class_or_namespace_scope_p (context_die));
18742
18743 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18744
18745 /* Now that the C++ front end lazily declares artificial member fns, we
18746 might need to retrofit the declaration into its class. */
18747 if (!declaration && !origin && !old_die
18748 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18749 && !class_or_namespace_scope_p (context_die)
18750 && debug_info_level > DINFO_LEVEL_TERSE)
18751 old_die = force_decl_die (decl);
18752
18753 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18754 if (origin != NULL)
18755 {
18756 gcc_assert (!declaration || local_scope_p (context_die));
18757
18758 /* Fixup die_parent for the abstract instance of a nested
18759 inline function. */
18760 if (old_die && old_die->die_parent == NULL)
18761 add_child_die (context_die, old_die);
18762
18763 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18764 {
18765 /* If we have a DW_AT_abstract_origin we have a working
18766 cached version. */
18767 subr_die = old_die;
18768 }
18769 else
18770 {
18771 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18772 add_abstract_origin_attribute (subr_die, origin);
18773 /* This is where the actual code for a cloned function is.
18774 Let's emit linkage name attribute for it. This helps
18775 debuggers to e.g, set breakpoints into
18776 constructors/destructors when the user asks "break
18777 K::K". */
18778 add_linkage_name (subr_die, decl);
18779 }
18780 }
18781 /* A cached copy, possibly from early dwarf generation. Reuse as
18782 much as possible. */
18783 else if (old_die)
18784 {
18785 /* A declaration that has been previously dumped needs no
18786 additional information. */
18787 if (declaration)
18788 return;
18789
18790 if (!get_AT_flag (old_die, DW_AT_declaration)
18791 /* We can have a normal definition following an inline one in the
18792 case of redefinition of GNU C extern inlines.
18793 It seems reasonable to use AT_specification in this case. */
18794 && !get_AT (old_die, DW_AT_inline))
18795 {
18796 /* Detect and ignore this case, where we are trying to output
18797 something we have already output. */
18798 if (get_AT (old_die, DW_AT_low_pc)
18799 || get_AT (old_die, DW_AT_ranges))
18800 return;
18801
18802 /* If we have no location information, this must be a
18803 partially generated DIE from early dwarf generation.
18804 Fall through and generate it. */
18805 }
18806
18807 /* If the definition comes from the same place as the declaration,
18808 maybe use the old DIE. We always want the DIE for this function
18809 that has the *_pc attributes to be under comp_unit_die so the
18810 debugger can find it. We also need to do this for abstract
18811 instances of inlines, since the spec requires the out-of-line copy
18812 to have the same parent. For local class methods, this doesn't
18813 apply; we just use the old DIE. */
18814 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18815 struct dwarf_file_data * file_index = lookup_filename (s.file);
18816 if ((is_cu_die (old_die->die_parent)
18817 /* This condition fixes the inconsistency/ICE with the
18818 following Fortran test (or some derivative thereof) while
18819 building libgfortran:
18820
18821 module some_m
18822 contains
18823 logical function funky (FLAG)
18824 funky = .true.
18825 end function
18826 end module
18827 */
18828 || (old_die->die_parent
18829 && old_die->die_parent->die_tag == DW_TAG_module)
18830 || context_die == NULL)
18831 && (DECL_ARTIFICIAL (decl)
18832 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18833 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18834 == (unsigned) s.line))))
18835 {
18836 subr_die = old_die;
18837
18838 /* Clear out the declaration attribute, but leave the
18839 parameters so they can be augmented with location
18840 information later. Unless this was a declaration, in
18841 which case, wipe out the nameless parameters and recreate
18842 them further down. */
18843 if (remove_AT (subr_die, DW_AT_declaration))
18844 {
18845
18846 remove_AT (subr_die, DW_AT_object_pointer);
18847 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18848 }
18849 }
18850 /* Make a specification pointing to the previously built
18851 declaration. */
18852 else
18853 {
18854 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18855 add_AT_specification (subr_die, old_die);
18856 add_pubname (decl, subr_die);
18857 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18858 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18859 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18860 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18861
18862 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18863 emit the real type on the definition die. */
18864 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18865 {
18866 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18867 if (die == auto_die || die == decltype_auto_die)
18868 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18869 TYPE_UNQUALIFIED, context_die);
18870 }
18871 }
18872 }
18873 /* Create a fresh DIE for anything else. */
18874 else
18875 {
18876 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18877
18878 if (TREE_PUBLIC (decl))
18879 add_AT_flag (subr_die, DW_AT_external, 1);
18880
18881 add_name_and_src_coords_attributes (subr_die, decl);
18882 add_pubname (decl, subr_die);
18883 if (debug_info_level > DINFO_LEVEL_TERSE)
18884 {
18885 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18886 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18887 TYPE_UNQUALIFIED, context_die);
18888 }
18889
18890 add_pure_or_virtual_attribute (subr_die, decl);
18891 if (DECL_ARTIFICIAL (decl))
18892 add_AT_flag (subr_die, DW_AT_artificial, 1);
18893
18894 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18895 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18896
18897 add_accessibility_attribute (subr_die, decl);
18898 }
18899
18900 /* Unless we have an existing non-declaration DIE, equate the new
18901 DIE. */
18902 if (!old_die || is_declaration_die (old_die))
18903 equate_decl_number_to_die (decl, subr_die);
18904
18905 if (declaration)
18906 {
18907 if (!old_die || !get_AT (old_die, DW_AT_inline))
18908 {
18909 add_AT_flag (subr_die, DW_AT_declaration, 1);
18910
18911 /* If this is an explicit function declaration then generate
18912 a DW_AT_explicit attribute. */
18913 if (lang_hooks.decls.function_decl_explicit_p (decl)
18914 && (dwarf_version >= 3 || !dwarf_strict))
18915 add_AT_flag (subr_die, DW_AT_explicit, 1);
18916
18917 /* If this is a C++11 deleted special function member then generate
18918 a DW_AT_GNU_deleted attribute. */
18919 if (lang_hooks.decls.function_decl_deleted_p (decl)
18920 && (! dwarf_strict))
18921 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18922 }
18923 }
18924 /* Tag abstract instances with DW_AT_inline. */
18925 else if (DECL_ABSTRACT_P (decl))
18926 {
18927 if (DECL_DECLARED_INLINE_P (decl))
18928 {
18929 if (cgraph_function_possibly_inlined_p (decl))
18930 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18931 else
18932 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18933 }
18934 else
18935 {
18936 if (cgraph_function_possibly_inlined_p (decl))
18937 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18938 else
18939 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18940 }
18941
18942 if (DECL_DECLARED_INLINE_P (decl)
18943 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18944 add_AT_flag (subr_die, DW_AT_artificial, 1);
18945 }
18946 /* For non DECL_EXTERNALs, if range information is available, fill
18947 the DIE with it. */
18948 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18949 {
18950 HOST_WIDE_INT cfa_fb_offset;
18951
18952 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18953
18954 if (!flag_reorder_blocks_and_partition)
18955 {
18956 dw_fde_ref fde = fun->fde;
18957 if (fde->dw_fde_begin)
18958 {
18959 /* We have already generated the labels. */
18960 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18961 fde->dw_fde_end, false);
18962 }
18963 else
18964 {
18965 /* Create start/end labels and add the range. */
18966 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18967 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18968 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18969 current_function_funcdef_no);
18970 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18971 current_function_funcdef_no);
18972 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18973 false);
18974 }
18975
18976 #if VMS_DEBUGGING_INFO
18977 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18978 Section 2.3 Prologue and Epilogue Attributes:
18979 When a breakpoint is set on entry to a function, it is generally
18980 desirable for execution to be suspended, not on the very first
18981 instruction of the function, but rather at a point after the
18982 function's frame has been set up, after any language defined local
18983 declaration processing has been completed, and before execution of
18984 the first statement of the function begins. Debuggers generally
18985 cannot properly determine where this point is. Similarly for a
18986 breakpoint set on exit from a function. The prologue and epilogue
18987 attributes allow a compiler to communicate the location(s) to use. */
18988
18989 {
18990 if (fde->dw_fde_vms_end_prologue)
18991 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18992 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18993
18994 if (fde->dw_fde_vms_begin_epilogue)
18995 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18996 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18997 }
18998 #endif
18999
19000 }
19001 else
19002 {
19003 /* Generate pubnames entries for the split function code ranges. */
19004 dw_fde_ref fde = fun->fde;
19005
19006 if (fde->dw_fde_second_begin)
19007 {
19008 if (dwarf_version >= 3 || !dwarf_strict)
19009 {
19010 /* We should use ranges for non-contiguous code section
19011 addresses. Use the actual code range for the initial
19012 section, since the HOT/COLD labels might precede an
19013 alignment offset. */
19014 bool range_list_added = false;
19015 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19016 fde->dw_fde_end, &range_list_added,
19017 false);
19018 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19019 fde->dw_fde_second_end,
19020 &range_list_added, false);
19021 if (range_list_added)
19022 add_ranges (NULL);
19023 }
19024 else
19025 {
19026 /* There is no real support in DW2 for this .. so we make
19027 a work-around. First, emit the pub name for the segment
19028 containing the function label. Then make and emit a
19029 simplified subprogram DIE for the second segment with the
19030 name pre-fixed by __hot/cold_sect_of_. We use the same
19031 linkage name for the second die so that gdb will find both
19032 sections when given "b foo". */
19033 const char *name = NULL;
19034 tree decl_name = DECL_NAME (decl);
19035 dw_die_ref seg_die;
19036
19037 /* Do the 'primary' section. */
19038 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19039 fde->dw_fde_end, false);
19040
19041 /* Build a minimal DIE for the secondary section. */
19042 seg_die = new_die (DW_TAG_subprogram,
19043 subr_die->die_parent, decl);
19044
19045 if (TREE_PUBLIC (decl))
19046 add_AT_flag (seg_die, DW_AT_external, 1);
19047
19048 if (decl_name != NULL
19049 && IDENTIFIER_POINTER (decl_name) != NULL)
19050 {
19051 name = dwarf2_name (decl, 1);
19052 if (! DECL_ARTIFICIAL (decl))
19053 add_src_coords_attributes (seg_die, decl);
19054
19055 add_linkage_name (seg_die, decl);
19056 }
19057 gcc_assert (name != NULL);
19058 add_pure_or_virtual_attribute (seg_die, decl);
19059 if (DECL_ARTIFICIAL (decl))
19060 add_AT_flag (seg_die, DW_AT_artificial, 1);
19061
19062 name = concat ("__second_sect_of_", name, NULL);
19063 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19064 fde->dw_fde_second_end, false);
19065 add_name_attribute (seg_die, name);
19066 if (want_pubnames ())
19067 add_pubname_string (name, seg_die);
19068 }
19069 }
19070 else
19071 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19072 false);
19073 }
19074
19075 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19076
19077 /* We define the "frame base" as the function's CFA. This is more
19078 convenient for several reasons: (1) It's stable across the prologue
19079 and epilogue, which makes it better than just a frame pointer,
19080 (2) With dwarf3, there exists a one-byte encoding that allows us
19081 to reference the .debug_frame data by proxy, but failing that,
19082 (3) We can at least reuse the code inspection and interpretation
19083 code that determines the CFA position at various points in the
19084 function. */
19085 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19086 {
19087 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19088 add_AT_loc (subr_die, DW_AT_frame_base, op);
19089 }
19090 else
19091 {
19092 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19093 if (list->dw_loc_next)
19094 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19095 else
19096 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19097 }
19098
19099 /* Compute a displacement from the "steady-state frame pointer" to
19100 the CFA. The former is what all stack slots and argument slots
19101 will reference in the rtl; the latter is what we've told the
19102 debugger about. We'll need to adjust all frame_base references
19103 by this displacement. */
19104 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19105
19106 if (fun->static_chain_decl)
19107 add_AT_location_description
19108 (subr_die, DW_AT_static_link,
19109 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19110 }
19111
19112 /* Generate child dies for template paramaters. */
19113 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19114 gen_generic_params_dies (decl);
19115
19116 /* Now output descriptions of the arguments for this function. This gets
19117 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19118 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19119 `...' at the end of the formal parameter list. In order to find out if
19120 there was a trailing ellipsis or not, we must instead look at the type
19121 associated with the FUNCTION_DECL. This will be a node of type
19122 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19123 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19124 an ellipsis at the end. */
19125
19126 /* In the case where we are describing a mere function declaration, all we
19127 need to do here (and all we *can* do here) is to describe the *types* of
19128 its formal parameters. */
19129 if (debug_info_level <= DINFO_LEVEL_TERSE)
19130 ;
19131 else if (declaration)
19132 gen_formal_types_die (decl, subr_die);
19133 else
19134 {
19135 /* Generate DIEs to represent all known formal parameters. */
19136 tree parm = DECL_ARGUMENTS (decl);
19137 tree generic_decl = early_dwarf
19138 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19139 tree generic_decl_parm = generic_decl
19140 ? DECL_ARGUMENTS (generic_decl)
19141 : NULL;
19142
19143 /* Now we want to walk the list of parameters of the function and
19144 emit their relevant DIEs.
19145
19146 We consider the case of DECL being an instance of a generic function
19147 as well as it being a normal function.
19148
19149 If DECL is an instance of a generic function we walk the
19150 parameters of the generic function declaration _and_ the parameters of
19151 DECL itself. This is useful because we want to emit specific DIEs for
19152 function parameter packs and those are declared as part of the
19153 generic function declaration. In that particular case,
19154 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19155 That DIE has children DIEs representing the set of arguments
19156 of the pack. Note that the set of pack arguments can be empty.
19157 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19158 children DIE.
19159
19160 Otherwise, we just consider the parameters of DECL. */
19161 while (generic_decl_parm || parm)
19162 {
19163 if (generic_decl_parm
19164 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19165 gen_formal_parameter_pack_die (generic_decl_parm,
19166 parm, subr_die,
19167 &parm);
19168 else if (parm && !POINTER_BOUNDS_P (parm))
19169 {
19170 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19171
19172 if (parm == DECL_ARGUMENTS (decl)
19173 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19174 && parm_die
19175 && (dwarf_version >= 3 || !dwarf_strict))
19176 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19177
19178 parm = DECL_CHAIN (parm);
19179 }
19180 else if (parm)
19181 parm = DECL_CHAIN (parm);
19182
19183 if (generic_decl_parm)
19184 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19185 }
19186
19187 /* Decide whether we need an unspecified_parameters DIE at the end.
19188 There are 2 more cases to do this for: 1) the ansi ... declaration -
19189 this is detectable when the end of the arg list is not a
19190 void_type_node 2) an unprototyped function declaration (not a
19191 definition). This just means that we have no info about the
19192 parameters at all. */
19193 if (prototype_p (TREE_TYPE (decl)))
19194 {
19195 /* This is the prototyped case, check for.... */
19196 if (stdarg_p (TREE_TYPE (decl)))
19197 gen_unspecified_parameters_die (decl, subr_die);
19198 }
19199 else if (DECL_INITIAL (decl) == NULL_TREE)
19200 gen_unspecified_parameters_die (decl, subr_die);
19201 }
19202
19203 if (subr_die != old_die)
19204 /* Add the calling convention attribute if requested. */
19205 add_calling_convention_attribute (subr_die, decl);
19206
19207 /* Output Dwarf info for all of the stuff within the body of the function
19208 (if it has one - it may be just a declaration).
19209
19210 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19211 a function. This BLOCK actually represents the outermost binding contour
19212 for the function, i.e. the contour in which the function's formal
19213 parameters and labels get declared. Curiously, it appears that the front
19214 end doesn't actually put the PARM_DECL nodes for the current function onto
19215 the BLOCK_VARS list for this outer scope, but are strung off of the
19216 DECL_ARGUMENTS list for the function instead.
19217
19218 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19219 the LABEL_DECL nodes for the function however, and we output DWARF info
19220 for those in decls_for_scope. Just within the `outer_scope' there will be
19221 a BLOCK node representing the function's outermost pair of curly braces,
19222 and any blocks used for the base and member initializers of a C++
19223 constructor function. */
19224 tree outer_scope = DECL_INITIAL (decl);
19225 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19226 {
19227 int call_site_note_count = 0;
19228 int tail_call_site_note_count = 0;
19229
19230 /* Emit a DW_TAG_variable DIE for a named return value. */
19231 if (DECL_NAME (DECL_RESULT (decl)))
19232 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19233
19234 /* The first time through decls_for_scope we will generate the
19235 DIEs for the locals. The second time, we fill in the
19236 location info. */
19237 decls_for_scope (outer_scope, subr_die);
19238
19239 if (call_arg_locations && !dwarf_strict)
19240 {
19241 struct call_arg_loc_node *ca_loc;
19242 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19243 {
19244 dw_die_ref die = NULL;
19245 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19246 rtx arg, next_arg;
19247
19248 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19249 arg; arg = next_arg)
19250 {
19251 dw_loc_descr_ref reg, val;
19252 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19253 dw_die_ref cdie, tdie = NULL;
19254
19255 next_arg = XEXP (arg, 1);
19256 if (REG_P (XEXP (XEXP (arg, 0), 0))
19257 && next_arg
19258 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19259 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19260 && REGNO (XEXP (XEXP (arg, 0), 0))
19261 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19262 next_arg = XEXP (next_arg, 1);
19263 if (mode == VOIDmode)
19264 {
19265 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19266 if (mode == VOIDmode)
19267 mode = GET_MODE (XEXP (arg, 0));
19268 }
19269 if (mode == VOIDmode || mode == BLKmode)
19270 continue;
19271 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19272 {
19273 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19274 tloc = XEXP (XEXP (arg, 0), 1);
19275 continue;
19276 }
19277 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19278 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19279 {
19280 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19281 tlocc = XEXP (XEXP (arg, 0), 1);
19282 continue;
19283 }
19284 reg = NULL;
19285 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19286 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19287 VAR_INIT_STATUS_INITIALIZED);
19288 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19289 {
19290 rtx mem = XEXP (XEXP (arg, 0), 0);
19291 reg = mem_loc_descriptor (XEXP (mem, 0),
19292 get_address_mode (mem),
19293 GET_MODE (mem),
19294 VAR_INIT_STATUS_INITIALIZED);
19295 }
19296 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19297 == DEBUG_PARAMETER_REF)
19298 {
19299 tree tdecl
19300 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19301 tdie = lookup_decl_die (tdecl);
19302 if (tdie == NULL)
19303 continue;
19304 }
19305 else
19306 continue;
19307 if (reg == NULL
19308 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19309 != DEBUG_PARAMETER_REF)
19310 continue;
19311 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19312 VOIDmode,
19313 VAR_INIT_STATUS_INITIALIZED);
19314 if (val == NULL)
19315 continue;
19316 if (die == NULL)
19317 die = gen_call_site_die (decl, subr_die, ca_loc);
19318 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19319 NULL_TREE);
19320 if (reg != NULL)
19321 add_AT_loc (cdie, DW_AT_location, reg);
19322 else if (tdie != NULL)
19323 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19324 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19325 if (next_arg != XEXP (arg, 1))
19326 {
19327 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19328 if (mode == VOIDmode)
19329 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19330 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19331 0), 1),
19332 mode, VOIDmode,
19333 VAR_INIT_STATUS_INITIALIZED);
19334 if (val != NULL)
19335 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19336 }
19337 }
19338 if (die == NULL
19339 && (ca_loc->symbol_ref || tloc))
19340 die = gen_call_site_die (decl, subr_die, ca_loc);
19341 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19342 {
19343 dw_loc_descr_ref tval = NULL;
19344
19345 if (tloc != NULL_RTX)
19346 tval = mem_loc_descriptor (tloc,
19347 GET_MODE (tloc) == VOIDmode
19348 ? Pmode : GET_MODE (tloc),
19349 VOIDmode,
19350 VAR_INIT_STATUS_INITIALIZED);
19351 if (tval)
19352 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19353 else if (tlocc != NULL_RTX)
19354 {
19355 tval = mem_loc_descriptor (tlocc,
19356 GET_MODE (tlocc) == VOIDmode
19357 ? Pmode : GET_MODE (tlocc),
19358 VOIDmode,
19359 VAR_INIT_STATUS_INITIALIZED);
19360 if (tval)
19361 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19362 tval);
19363 }
19364 }
19365 if (die != NULL)
19366 {
19367 call_site_note_count++;
19368 if (ca_loc->tail_call_p)
19369 tail_call_site_note_count++;
19370 }
19371 }
19372 }
19373 call_arg_locations = NULL;
19374 call_arg_loc_last = NULL;
19375 if (tail_call_site_count >= 0
19376 && tail_call_site_count == tail_call_site_note_count
19377 && !dwarf_strict)
19378 {
19379 if (call_site_count >= 0
19380 && call_site_count == call_site_note_count)
19381 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19382 else
19383 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19384 }
19385 call_site_count = -1;
19386 tail_call_site_count = -1;
19387 }
19388 }
19389
19390 /* Returns a hash value for X (which really is a die_struct). */
19391
19392 hashval_t
19393 block_die_hasher::hash (die_struct *d)
19394 {
19395 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19396 }
19397
19398 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19399 as decl_id and die_parent of die_struct Y. */
19400
19401 bool
19402 block_die_hasher::equal (die_struct *x, die_struct *y)
19403 {
19404 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19405 }
19406
19407 /* Return TRUE if DECL, which may have been previously generated as
19408 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19409 true if decl (or its origin) is either an extern declaration or a
19410 class/namespace scoped declaration.
19411
19412 The declare_in_namespace support causes us to get two DIEs for one
19413 variable, both of which are declarations. We want to avoid
19414 considering one to be a specification, so we must test for
19415 DECLARATION and DW_AT_declaration. */
19416 static inline bool
19417 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19418 {
19419 return (old_die && TREE_STATIC (decl) && !declaration
19420 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19421 }
19422
19423 /* Return true if DECL is a local static. */
19424
19425 static inline bool
19426 local_function_static (tree decl)
19427 {
19428 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19429 return TREE_STATIC (decl)
19430 && DECL_CONTEXT (decl)
19431 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19432 }
19433
19434 /* Generate a DIE to represent a declared data object.
19435 Either DECL or ORIGIN must be non-null. */
19436
19437 static void
19438 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19439 {
19440 HOST_WIDE_INT off = 0;
19441 tree com_decl;
19442 tree decl_or_origin = decl ? decl : origin;
19443 tree ultimate_origin;
19444 dw_die_ref var_die;
19445 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19446 dw_die_ref origin_die = NULL;
19447 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19448 || class_or_namespace_scope_p (context_die));
19449 bool specialization_p = false;
19450
19451 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19452 if (decl || ultimate_origin)
19453 origin = ultimate_origin;
19454 com_decl = fortran_common (decl_or_origin, &off);
19455
19456 /* Symbol in common gets emitted as a child of the common block, in the form
19457 of a data member. */
19458 if (com_decl)
19459 {
19460 dw_die_ref com_die;
19461 dw_loc_list_ref loc;
19462 die_node com_die_arg;
19463
19464 var_die = lookup_decl_die (decl_or_origin);
19465 if (var_die)
19466 {
19467 if (get_AT (var_die, DW_AT_location) == NULL)
19468 {
19469 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19470 if (loc)
19471 {
19472 if (off)
19473 {
19474 /* Optimize the common case. */
19475 if (single_element_loc_list_p (loc)
19476 && loc->expr->dw_loc_opc == DW_OP_addr
19477 && loc->expr->dw_loc_next == NULL
19478 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19479 == SYMBOL_REF)
19480 {
19481 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19482 loc->expr->dw_loc_oprnd1.v.val_addr
19483 = plus_constant (GET_MODE (x), x , off);
19484 }
19485 else
19486 loc_list_plus_const (loc, off);
19487 }
19488 add_AT_location_description (var_die, DW_AT_location, loc);
19489 remove_AT (var_die, DW_AT_declaration);
19490 }
19491 }
19492 return;
19493 }
19494
19495 if (common_block_die_table == NULL)
19496 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19497
19498 com_die_arg.decl_id = DECL_UID (com_decl);
19499 com_die_arg.die_parent = context_die;
19500 com_die = common_block_die_table->find (&com_die_arg);
19501 loc = loc_list_from_tree (com_decl, 2, NULL);
19502 if (com_die == NULL)
19503 {
19504 const char *cnam
19505 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19506 die_node **slot;
19507
19508 com_die = new_die (DW_TAG_common_block, context_die, decl);
19509 add_name_and_src_coords_attributes (com_die, com_decl);
19510 if (loc)
19511 {
19512 add_AT_location_description (com_die, DW_AT_location, loc);
19513 /* Avoid sharing the same loc descriptor between
19514 DW_TAG_common_block and DW_TAG_variable. */
19515 loc = loc_list_from_tree (com_decl, 2, NULL);
19516 }
19517 else if (DECL_EXTERNAL (decl))
19518 add_AT_flag (com_die, DW_AT_declaration, 1);
19519 if (want_pubnames ())
19520 add_pubname_string (cnam, com_die); /* ??? needed? */
19521 com_die->decl_id = DECL_UID (com_decl);
19522 slot = common_block_die_table->find_slot (com_die, INSERT);
19523 *slot = com_die;
19524 }
19525 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19526 {
19527 add_AT_location_description (com_die, DW_AT_location, loc);
19528 loc = loc_list_from_tree (com_decl, 2, NULL);
19529 remove_AT (com_die, DW_AT_declaration);
19530 }
19531 var_die = new_die (DW_TAG_variable, com_die, decl);
19532 add_name_and_src_coords_attributes (var_die, decl);
19533 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19534 context_die);
19535 add_AT_flag (var_die, DW_AT_external, 1);
19536 if (loc)
19537 {
19538 if (off)
19539 {
19540 /* Optimize the common case. */
19541 if (single_element_loc_list_p (loc)
19542 && loc->expr->dw_loc_opc == DW_OP_addr
19543 && loc->expr->dw_loc_next == NULL
19544 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19545 {
19546 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19547 loc->expr->dw_loc_oprnd1.v.val_addr
19548 = plus_constant (GET_MODE (x), x, off);
19549 }
19550 else
19551 loc_list_plus_const (loc, off);
19552 }
19553 add_AT_location_description (var_die, DW_AT_location, loc);
19554 }
19555 else if (DECL_EXTERNAL (decl))
19556 add_AT_flag (var_die, DW_AT_declaration, 1);
19557 equate_decl_number_to_die (decl, var_die);
19558 return;
19559 }
19560
19561 if (old_die)
19562 {
19563 if (declaration)
19564 {
19565 /* A declaration that has been previously dumped, needs no
19566 further annotations, since it doesn't need location on
19567 the second pass. */
19568 return;
19569 }
19570 else if (decl_will_get_specification_p (old_die, decl, declaration)
19571 && !get_AT (old_die, DW_AT_specification))
19572 {
19573 /* Fall-thru so we can make a new variable die along with a
19574 DW_AT_specification. */
19575 }
19576 else if (origin && old_die->die_parent != context_die)
19577 {
19578 /* If we will be creating an inlined instance, we need a
19579 new DIE that will get annotated with
19580 DW_AT_abstract_origin. Clear things so we can get a
19581 new DIE. */
19582 gcc_assert (!DECL_ABSTRACT_P (decl));
19583 old_die = NULL;
19584 }
19585 else
19586 {
19587 /* If a DIE was dumped early, it still needs location info.
19588 Skip to where we fill the location bits. */
19589 var_die = old_die;
19590 goto gen_variable_die_location;
19591 }
19592 }
19593
19594 /* For static data members, the declaration in the class is supposed
19595 to have DW_TAG_member tag; the specification should still be
19596 DW_TAG_variable referencing the DW_TAG_member DIE. */
19597 if (declaration && class_scope_p (context_die))
19598 var_die = new_die (DW_TAG_member, context_die, decl);
19599 else
19600 var_die = new_die (DW_TAG_variable, context_die, decl);
19601
19602 if (origin != NULL)
19603 origin_die = add_abstract_origin_attribute (var_die, origin);
19604
19605 /* Loop unrolling can create multiple blocks that refer to the same
19606 static variable, so we must test for the DW_AT_declaration flag.
19607
19608 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19609 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19610 sharing them.
19611
19612 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19613 else if (decl_will_get_specification_p (old_die, decl, declaration))
19614 {
19615 /* This is a definition of a C++ class level static. */
19616 add_AT_specification (var_die, old_die);
19617 specialization_p = true;
19618 if (DECL_NAME (decl))
19619 {
19620 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19621 struct dwarf_file_data * file_index = lookup_filename (s.file);
19622
19623 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19624 add_AT_file (var_die, DW_AT_decl_file, file_index);
19625
19626 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19627 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19628
19629 if (old_die->die_tag == DW_TAG_member)
19630 add_linkage_name (var_die, decl);
19631 }
19632 }
19633 else
19634 add_name_and_src_coords_attributes (var_die, decl);
19635
19636 if ((origin == NULL && !specialization_p)
19637 || (origin != NULL
19638 && !DECL_ABSTRACT_P (decl_or_origin)
19639 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19640 decl_function_context
19641 (decl_or_origin))))
19642 {
19643 tree type = TREE_TYPE (decl_or_origin);
19644
19645 if (decl_by_reference_p (decl_or_origin))
19646 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19647 context_die);
19648 else
19649 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19650 context_die);
19651 }
19652
19653 if (origin == NULL && !specialization_p)
19654 {
19655 if (TREE_PUBLIC (decl))
19656 add_AT_flag (var_die, DW_AT_external, 1);
19657
19658 if (DECL_ARTIFICIAL (decl))
19659 add_AT_flag (var_die, DW_AT_artificial, 1);
19660
19661 add_accessibility_attribute (var_die, decl);
19662 }
19663
19664 if (declaration)
19665 add_AT_flag (var_die, DW_AT_declaration, 1);
19666
19667 if (decl && (DECL_ABSTRACT_P (decl)
19668 || !old_die || is_declaration_die (old_die)))
19669 equate_decl_number_to_die (decl, var_die);
19670
19671 gen_variable_die_location:
19672 if (! declaration
19673 && (! DECL_ABSTRACT_P (decl_or_origin)
19674 /* Local static vars are shared between all clones/inlines,
19675 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19676 already set. */
19677 || (TREE_CODE (decl_or_origin) == VAR_DECL
19678 && TREE_STATIC (decl_or_origin)
19679 && DECL_RTL_SET_P (decl_or_origin)))
19680 /* When abstract origin already has DW_AT_location attribute, no need
19681 to add it again. */
19682 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19683 {
19684 if (early_dwarf)
19685 add_pubname (decl_or_origin, var_die);
19686 else
19687 add_location_or_const_value_attribute (var_die, decl_or_origin,
19688 decl == NULL);
19689 }
19690 else
19691 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19692 }
19693
19694 /* Generate a DIE to represent a named constant. */
19695
19696 static void
19697 gen_const_die (tree decl, dw_die_ref context_die)
19698 {
19699 dw_die_ref const_die;
19700 tree type = TREE_TYPE (decl);
19701
19702 const_die = lookup_decl_die (decl);
19703 if (const_die)
19704 return;
19705
19706 const_die = new_die (DW_TAG_constant, context_die, decl);
19707 equate_decl_number_to_die (decl, const_die);
19708 add_name_and_src_coords_attributes (const_die, decl);
19709 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19710 if (TREE_PUBLIC (decl))
19711 add_AT_flag (const_die, DW_AT_external, 1);
19712 if (DECL_ARTIFICIAL (decl))
19713 add_AT_flag (const_die, DW_AT_artificial, 1);
19714 tree_add_const_value_attribute_for_decl (const_die, decl);
19715 }
19716
19717 /* Generate a DIE to represent a label identifier. */
19718
19719 static void
19720 gen_label_die (tree decl, dw_die_ref context_die)
19721 {
19722 tree origin = decl_ultimate_origin (decl);
19723 dw_die_ref lbl_die = lookup_decl_die (decl);
19724 rtx insn;
19725 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19726
19727 if (!lbl_die)
19728 {
19729 lbl_die = new_die (DW_TAG_label, context_die, decl);
19730 equate_decl_number_to_die (decl, lbl_die);
19731
19732 if (origin != NULL)
19733 add_abstract_origin_attribute (lbl_die, origin);
19734 else
19735 add_name_and_src_coords_attributes (lbl_die, decl);
19736 }
19737
19738 if (DECL_ABSTRACT_P (decl))
19739 equate_decl_number_to_die (decl, lbl_die);
19740 else
19741 {
19742 insn = DECL_RTL_IF_SET (decl);
19743
19744 /* Deleted labels are programmer specified labels which have been
19745 eliminated because of various optimizations. We still emit them
19746 here so that it is possible to put breakpoints on them. */
19747 if (insn
19748 && (LABEL_P (insn)
19749 || ((NOTE_P (insn)
19750 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19751 {
19752 /* When optimization is enabled (via -O) some parts of the compiler
19753 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19754 represent source-level labels which were explicitly declared by
19755 the user. This really shouldn't be happening though, so catch
19756 it if it ever does happen. */
19757 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19758
19759 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19760 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19761 }
19762 else if (insn
19763 && NOTE_P (insn)
19764 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19765 && CODE_LABEL_NUMBER (insn) != -1)
19766 {
19767 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19768 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19769 }
19770 }
19771 }
19772
19773 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19774 attributes to the DIE for a block STMT, to describe where the inlined
19775 function was called from. This is similar to add_src_coords_attributes. */
19776
19777 static inline void
19778 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19779 {
19780 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19781
19782 if (dwarf_version >= 3 || !dwarf_strict)
19783 {
19784 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19785 add_AT_unsigned (die, DW_AT_call_line, s.line);
19786 }
19787 }
19788
19789
19790 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19791 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19792
19793 static inline void
19794 add_high_low_attributes (tree stmt, dw_die_ref die)
19795 {
19796 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19797
19798 if (BLOCK_FRAGMENT_CHAIN (stmt)
19799 && (dwarf_version >= 3 || !dwarf_strict))
19800 {
19801 tree chain, superblock = NULL_TREE;
19802 dw_die_ref pdie;
19803 dw_attr_node *attr = NULL;
19804
19805 if (inlined_function_outer_scope_p (stmt))
19806 {
19807 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19808 BLOCK_NUMBER (stmt));
19809 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19810 }
19811
19812 /* Optimize duplicate .debug_ranges lists or even tails of
19813 lists. If this BLOCK has same ranges as its supercontext,
19814 lookup DW_AT_ranges attribute in the supercontext (and
19815 recursively so), verify that the ranges_table contains the
19816 right values and use it instead of adding a new .debug_range. */
19817 for (chain = stmt, pdie = die;
19818 BLOCK_SAME_RANGE (chain);
19819 chain = BLOCK_SUPERCONTEXT (chain))
19820 {
19821 dw_attr_node *new_attr;
19822
19823 pdie = pdie->die_parent;
19824 if (pdie == NULL)
19825 break;
19826 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19827 break;
19828 new_attr = get_AT (pdie, DW_AT_ranges);
19829 if (new_attr == NULL
19830 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19831 break;
19832 attr = new_attr;
19833 superblock = BLOCK_SUPERCONTEXT (chain);
19834 }
19835 if (attr != NULL
19836 && (ranges_table[attr->dw_attr_val.v.val_offset
19837 / 2 / DWARF2_ADDR_SIZE].num
19838 == BLOCK_NUMBER (superblock))
19839 && BLOCK_FRAGMENT_CHAIN (superblock))
19840 {
19841 unsigned long off = attr->dw_attr_val.v.val_offset
19842 / 2 / DWARF2_ADDR_SIZE;
19843 unsigned long supercnt = 0, thiscnt = 0;
19844 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19845 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19846 {
19847 ++supercnt;
19848 gcc_checking_assert (ranges_table[off + supercnt].num
19849 == BLOCK_NUMBER (chain));
19850 }
19851 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19852 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19853 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19854 ++thiscnt;
19855 gcc_assert (supercnt >= thiscnt);
19856 add_AT_range_list (die, DW_AT_ranges,
19857 ((off + supercnt - thiscnt)
19858 * 2 * DWARF2_ADDR_SIZE),
19859 false);
19860 return;
19861 }
19862
19863 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19864
19865 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19866 do
19867 {
19868 add_ranges (chain);
19869 chain = BLOCK_FRAGMENT_CHAIN (chain);
19870 }
19871 while (chain);
19872 add_ranges (NULL);
19873 }
19874 else
19875 {
19876 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19877 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19878 BLOCK_NUMBER (stmt));
19879 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19880 BLOCK_NUMBER (stmt));
19881 add_AT_low_high_pc (die, label, label_high, false);
19882 }
19883 }
19884
19885 /* Generate a DIE for a lexical block. */
19886
19887 static void
19888 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19889 {
19890 dw_die_ref old_die = BLOCK_DIE (stmt);
19891 dw_die_ref stmt_die = NULL;
19892 if (!old_die)
19893 {
19894 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19895 BLOCK_DIE (stmt) = stmt_die;
19896 }
19897
19898 if (BLOCK_ABSTRACT (stmt))
19899 {
19900 if (old_die)
19901 {
19902 /* This must have been generated early and it won't even
19903 need location information since it's a DW_AT_inline
19904 function. */
19905 if (flag_checking)
19906 for (dw_die_ref c = context_die; c; c = c->die_parent)
19907 if (c->die_tag == DW_TAG_inlined_subroutine
19908 || c->die_tag == DW_TAG_subprogram)
19909 {
19910 gcc_assert (get_AT (c, DW_AT_inline));
19911 break;
19912 }
19913 return;
19914 }
19915 }
19916 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19917 {
19918 /* If this is an inlined instance, create a new lexical die for
19919 anything below to attach DW_AT_abstract_origin to. */
19920 if (old_die)
19921 {
19922 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19923 BLOCK_DIE (stmt) = stmt_die;
19924 old_die = NULL;
19925 }
19926 }
19927
19928 if (old_die)
19929 stmt_die = old_die;
19930
19931 /* A non abstract block whose blocks have already been reordered
19932 should have the instruction range for this block. If so, set the
19933 high/low attributes. */
19934 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19935 {
19936 gcc_assert (stmt_die);
19937 add_high_low_attributes (stmt, stmt_die);
19938 }
19939
19940 decls_for_scope (stmt, stmt_die);
19941 }
19942
19943 /* Generate a DIE for an inlined subprogram. */
19944
19945 static void
19946 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19947 {
19948 tree decl;
19949
19950 /* The instance of function that is effectively being inlined shall not
19951 be abstract. */
19952 gcc_assert (! BLOCK_ABSTRACT (stmt));
19953
19954 decl = block_ultimate_origin (stmt);
19955
19956 /* Make sure any inlined functions are known to be inlineable. */
19957 gcc_checking_assert (DECL_ABSTRACT_P (decl)
19958 || cgraph_function_possibly_inlined_p (decl));
19959
19960 /* Emit info for the abstract instance first, if we haven't yet. We
19961 must emit this even if the block is abstract, otherwise when we
19962 emit the block below (or elsewhere), we may end up trying to emit
19963 a die whose origin die hasn't been emitted, and crashing. */
19964 dwarf2out_abstract_function (decl);
19965
19966 if (! BLOCK_ABSTRACT (stmt))
19967 {
19968 dw_die_ref subr_die
19969 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19970
19971 if (call_arg_locations)
19972 BLOCK_DIE (stmt) = subr_die;
19973 add_abstract_origin_attribute (subr_die, decl);
19974 if (TREE_ASM_WRITTEN (stmt))
19975 add_high_low_attributes (stmt, subr_die);
19976 add_call_src_coords_attributes (stmt, subr_die);
19977
19978 decls_for_scope (stmt, subr_die);
19979 }
19980 }
19981
19982 /* Generate a DIE for a field in a record, or structure. */
19983
19984 static void
19985 gen_field_die (tree decl, dw_die_ref context_die)
19986 {
19987 dw_die_ref decl_die;
19988
19989 if (TREE_TYPE (decl) == error_mark_node)
19990 return;
19991
19992 decl_die = new_die (DW_TAG_member, context_die, decl);
19993 add_name_and_src_coords_attributes (decl_die, decl);
19994 add_type_attribute (decl_die, member_declared_type (decl),
19995 decl_quals (decl), context_die);
19996
19997 if (DECL_BIT_FIELD_TYPE (decl))
19998 {
19999 add_byte_size_attribute (decl_die, decl);
20000 add_bit_size_attribute (decl_die, decl);
20001 add_bit_offset_attribute (decl_die, decl);
20002 }
20003
20004 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20005 add_data_member_location_attribute (decl_die, decl);
20006
20007 if (DECL_ARTIFICIAL (decl))
20008 add_AT_flag (decl_die, DW_AT_artificial, 1);
20009
20010 add_accessibility_attribute (decl_die, decl);
20011
20012 /* Equate decl number to die, so that we can look up this decl later on. */
20013 equate_decl_number_to_die (decl, decl_die);
20014 }
20015
20016 #if 0
20017 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20018 Use modified_type_die instead.
20019 We keep this code here just in case these types of DIEs may be needed to
20020 represent certain things in other languages (e.g. Pascal) someday. */
20021
20022 static void
20023 gen_pointer_type_die (tree type, dw_die_ref context_die)
20024 {
20025 dw_die_ref ptr_die
20026 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20027
20028 equate_type_number_to_die (type, ptr_die);
20029 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20030 context_die);
20031 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20032 }
20033
20034 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20035 Use modified_type_die instead.
20036 We keep this code here just in case these types of DIEs may be needed to
20037 represent certain things in other languages (e.g. Pascal) someday. */
20038
20039 static void
20040 gen_reference_type_die (tree type, dw_die_ref context_die)
20041 {
20042 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20043
20044 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20045 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20046 else
20047 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20048
20049 equate_type_number_to_die (type, ref_die);
20050 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20051 context_die);
20052 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20053 }
20054 #endif
20055
20056 /* Generate a DIE for a pointer to a member type. */
20057
20058 static void
20059 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20060 {
20061 dw_die_ref ptr_die
20062 = new_die (DW_TAG_ptr_to_member_type,
20063 scope_die_for (type, context_die), type);
20064
20065 equate_type_number_to_die (type, ptr_die);
20066 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20067 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20068 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20069 context_die);
20070 }
20071
20072 static char *producer_string;
20073
20074 /* Return a heap allocated producer string including command line options
20075 if -grecord-gcc-switches. */
20076
20077 static char *
20078 gen_producer_string (void)
20079 {
20080 size_t j;
20081 auto_vec<const char *> switches;
20082 const char *language_string = lang_hooks.name;
20083 char *producer, *tail;
20084 const char *p;
20085 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20086 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20087
20088 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20089 switch (save_decoded_options[j].opt_index)
20090 {
20091 case OPT_o:
20092 case OPT_d:
20093 case OPT_dumpbase:
20094 case OPT_dumpdir:
20095 case OPT_auxbase:
20096 case OPT_auxbase_strip:
20097 case OPT_quiet:
20098 case OPT_version:
20099 case OPT_v:
20100 case OPT_w:
20101 case OPT_L:
20102 case OPT_D:
20103 case OPT_I:
20104 case OPT_U:
20105 case OPT_SPECIAL_unknown:
20106 case OPT_SPECIAL_ignore:
20107 case OPT_SPECIAL_program_name:
20108 case OPT_SPECIAL_input_file:
20109 case OPT_grecord_gcc_switches:
20110 case OPT_gno_record_gcc_switches:
20111 case OPT__output_pch_:
20112 case OPT_fdiagnostics_show_location_:
20113 case OPT_fdiagnostics_show_option:
20114 case OPT_fdiagnostics_show_caret:
20115 case OPT_fdiagnostics_color_:
20116 case OPT_fverbose_asm:
20117 case OPT____:
20118 case OPT__sysroot_:
20119 case OPT_nostdinc:
20120 case OPT_nostdinc__:
20121 case OPT_fpreprocessed:
20122 case OPT_fltrans_output_list_:
20123 case OPT_fresolution_:
20124 /* Ignore these. */
20125 continue;
20126 default:
20127 if (cl_options[save_decoded_options[j].opt_index].flags
20128 & CL_NO_DWARF_RECORD)
20129 continue;
20130 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20131 == '-');
20132 switch (save_decoded_options[j].canonical_option[0][1])
20133 {
20134 case 'M':
20135 case 'i':
20136 case 'W':
20137 continue;
20138 case 'f':
20139 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20140 "dump", 4) == 0)
20141 continue;
20142 break;
20143 default:
20144 break;
20145 }
20146 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20147 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20148 break;
20149 }
20150
20151 producer = XNEWVEC (char, plen + 1 + len + 1);
20152 tail = producer;
20153 sprintf (tail, "%s %s", language_string, version_string);
20154 tail += plen;
20155
20156 FOR_EACH_VEC_ELT (switches, j, p)
20157 {
20158 len = strlen (p);
20159 *tail = ' ';
20160 memcpy (tail + 1, p, len);
20161 tail += len + 1;
20162 }
20163
20164 *tail = '\0';
20165 return producer;
20166 }
20167
20168 /* Given a C and/or C++ language/version string return the "highest".
20169 C++ is assumed to be "higher" than C in this case. Used for merging
20170 LTO translation unit languages. */
20171 static const char *
20172 highest_c_language (const char *lang1, const char *lang2)
20173 {
20174 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20175 return "GNU C++14";
20176 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20177 return "GNU C++11";
20178 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20179 return "GNU C++98";
20180
20181 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20182 return "GNU C11";
20183 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20184 return "GNU C99";
20185 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20186 return "GNU C89";
20187
20188 gcc_unreachable ();
20189 }
20190
20191
20192 /* Generate the DIE for the compilation unit. */
20193
20194 static dw_die_ref
20195 gen_compile_unit_die (const char *filename)
20196 {
20197 dw_die_ref die;
20198 const char *language_string = lang_hooks.name;
20199 int language;
20200
20201 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20202
20203 if (filename)
20204 {
20205 add_name_attribute (die, filename);
20206 /* Don't add cwd for <built-in>. */
20207 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20208 add_comp_dir_attribute (die);
20209 }
20210
20211 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20212
20213 /* If our producer is LTO try to figure out a common language to use
20214 from the global list of translation units. */
20215 if (strcmp (language_string, "GNU GIMPLE") == 0)
20216 {
20217 unsigned i;
20218 tree t;
20219 const char *common_lang = NULL;
20220
20221 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20222 {
20223 if (!TRANSLATION_UNIT_LANGUAGE (t))
20224 continue;
20225 if (!common_lang)
20226 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20227 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20228 ;
20229 else if (strncmp (common_lang, "GNU C", 5) == 0
20230 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20231 /* Mixing C and C++ is ok, use C++ in that case. */
20232 common_lang = highest_c_language (common_lang,
20233 TRANSLATION_UNIT_LANGUAGE (t));
20234 else
20235 {
20236 /* Fall back to C. */
20237 common_lang = NULL;
20238 break;
20239 }
20240 }
20241
20242 if (common_lang)
20243 language_string = common_lang;
20244 }
20245
20246 language = DW_LANG_C;
20247 if (strncmp (language_string, "GNU C", 5) == 0
20248 && ISDIGIT (language_string[5]))
20249 {
20250 language = DW_LANG_C89;
20251 if (dwarf_version >= 3 || !dwarf_strict)
20252 {
20253 if (strcmp (language_string, "GNU C89") != 0)
20254 language = DW_LANG_C99;
20255
20256 if (dwarf_version >= 5 /* || !dwarf_strict */)
20257 if (strcmp (language_string, "GNU C11") == 0)
20258 language = DW_LANG_C11;
20259 }
20260 }
20261 else if (strncmp (language_string, "GNU C++", 7) == 0)
20262 {
20263 language = DW_LANG_C_plus_plus;
20264 if (dwarf_version >= 5 /* || !dwarf_strict */)
20265 {
20266 if (strcmp (language_string, "GNU C++11") == 0)
20267 language = DW_LANG_C_plus_plus_11;
20268 else if (strcmp (language_string, "GNU C++14") == 0)
20269 language = DW_LANG_C_plus_plus_14;
20270 }
20271 }
20272 else if (strcmp (language_string, "GNU F77") == 0)
20273 language = DW_LANG_Fortran77;
20274 else if (strcmp (language_string, "GNU Pascal") == 0)
20275 language = DW_LANG_Pascal83;
20276 else if (dwarf_version >= 3 || !dwarf_strict)
20277 {
20278 if (strcmp (language_string, "GNU Ada") == 0)
20279 language = DW_LANG_Ada95;
20280 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20281 {
20282 language = DW_LANG_Fortran95;
20283 if (dwarf_version >= 5 /* || !dwarf_strict */)
20284 {
20285 if (strcmp (language_string, "GNU Fortran2003") == 0)
20286 language = DW_LANG_Fortran03;
20287 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20288 language = DW_LANG_Fortran08;
20289 }
20290 }
20291 else if (strcmp (language_string, "GNU Java") == 0)
20292 language = DW_LANG_Java;
20293 else if (strcmp (language_string, "GNU Objective-C") == 0)
20294 language = DW_LANG_ObjC;
20295 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20296 language = DW_LANG_ObjC_plus_plus;
20297 else if (dwarf_version >= 5 || !dwarf_strict)
20298 {
20299 if (strcmp (language_string, "GNU Go") == 0)
20300 language = DW_LANG_Go;
20301 }
20302 }
20303 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20304 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20305 language = DW_LANG_Fortran90;
20306
20307 add_AT_unsigned (die, DW_AT_language, language);
20308
20309 switch (language)
20310 {
20311 case DW_LANG_Fortran77:
20312 case DW_LANG_Fortran90:
20313 case DW_LANG_Fortran95:
20314 case DW_LANG_Fortran03:
20315 case DW_LANG_Fortran08:
20316 /* Fortran has case insensitive identifiers and the front-end
20317 lowercases everything. */
20318 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20319 break;
20320 default:
20321 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20322 break;
20323 }
20324 return die;
20325 }
20326
20327 /* Generate the DIE for a base class. */
20328
20329 static void
20330 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20331 {
20332 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20333
20334 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20335 add_data_member_location_attribute (die, binfo);
20336
20337 if (BINFO_VIRTUAL_P (binfo))
20338 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20339
20340 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20341 children, otherwise the default is DW_ACCESS_public. In DWARF2
20342 the default has always been DW_ACCESS_private. */
20343 if (access == access_public_node)
20344 {
20345 if (dwarf_version == 2
20346 || context_die->die_tag == DW_TAG_class_type)
20347 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20348 }
20349 else if (access == access_protected_node)
20350 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20351 else if (dwarf_version > 2
20352 && context_die->die_tag != DW_TAG_class_type)
20353 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20354 }
20355
20356 /* Generate a DIE for a class member. */
20357
20358 static void
20359 gen_member_die (tree type, dw_die_ref context_die)
20360 {
20361 tree member;
20362 tree binfo = TYPE_BINFO (type);
20363 dw_die_ref child;
20364
20365 /* If this is not an incomplete type, output descriptions of each of its
20366 members. Note that as we output the DIEs necessary to represent the
20367 members of this record or union type, we will also be trying to output
20368 DIEs to represent the *types* of those members. However the `type'
20369 function (above) will specifically avoid generating type DIEs for member
20370 types *within* the list of member DIEs for this (containing) type except
20371 for those types (of members) which are explicitly marked as also being
20372 members of this (containing) type themselves. The g++ front- end can
20373 force any given type to be treated as a member of some other (containing)
20374 type by setting the TYPE_CONTEXT of the given (member) type to point to
20375 the TREE node representing the appropriate (containing) type. */
20376
20377 /* First output info about the base classes. */
20378 if (binfo)
20379 {
20380 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20381 int i;
20382 tree base;
20383
20384 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20385 gen_inheritance_die (base,
20386 (accesses ? (*accesses)[i] : access_public_node),
20387 context_die);
20388 }
20389
20390 /* Now output info about the data members and type members. */
20391 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20392 {
20393 /* If we thought we were generating minimal debug info for TYPE
20394 and then changed our minds, some of the member declarations
20395 may have already been defined. Don't define them again, but
20396 do put them in the right order. */
20397
20398 child = lookup_decl_die (member);
20399 if (child)
20400 splice_child_die (context_die, child);
20401 else
20402 gen_decl_die (member, NULL, context_die);
20403 }
20404
20405 /* We do not keep type methods in type variants. */
20406 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20407 /* Now output info about the function members (if any). */
20408 if (TYPE_METHODS (type) != error_mark_node)
20409 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20410 {
20411 /* Don't include clones in the member list. */
20412 if (DECL_ABSTRACT_ORIGIN (member))
20413 continue;
20414 /* Nor constructors for anonymous classes. */
20415 if (DECL_ARTIFICIAL (member)
20416 && dwarf2_name (member, 0) == NULL)
20417 continue;
20418
20419 child = lookup_decl_die (member);
20420 if (child)
20421 splice_child_die (context_die, child);
20422 else
20423 gen_decl_die (member, NULL, context_die);
20424 }
20425 }
20426
20427 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20428 is set, we pretend that the type was never defined, so we only get the
20429 member DIEs needed by later specification DIEs. */
20430
20431 static void
20432 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20433 enum debug_info_usage usage)
20434 {
20435 if (TREE_ASM_WRITTEN (type))
20436 {
20437 /* Fill in the bound of variable-length fields in late dwarf if
20438 still incomplete. */
20439 if (!early_dwarf && variably_modified_type_p (type, NULL))
20440 for (tree member = TYPE_FIELDS (type);
20441 member;
20442 member = DECL_CHAIN (member))
20443 fill_variable_array_bounds (TREE_TYPE (member));
20444 return;
20445 }
20446
20447 dw_die_ref type_die = lookup_type_die (type);
20448 dw_die_ref scope_die = 0;
20449 int nested = 0;
20450 int complete = (TYPE_SIZE (type)
20451 && (! TYPE_STUB_DECL (type)
20452 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20453 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20454 complete = complete && should_emit_struct_debug (type, usage);
20455
20456 if (type_die && ! complete)
20457 return;
20458
20459 if (TYPE_CONTEXT (type) != NULL_TREE
20460 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20461 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20462 nested = 1;
20463
20464 scope_die = scope_die_for (type, context_die);
20465
20466 /* Generate child dies for template paramaters. */
20467 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20468 schedule_generic_params_dies_gen (type);
20469
20470 if (! type_die || (nested && is_cu_die (scope_die)))
20471 /* First occurrence of type or toplevel definition of nested class. */
20472 {
20473 dw_die_ref old_die = type_die;
20474
20475 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20476 ? record_type_tag (type) : DW_TAG_union_type,
20477 scope_die, type);
20478 equate_type_number_to_die (type, type_die);
20479 if (old_die)
20480 add_AT_specification (type_die, old_die);
20481 else
20482 add_name_attribute (type_die, type_tag (type));
20483 }
20484 else
20485 remove_AT (type_die, DW_AT_declaration);
20486
20487 /* If this type has been completed, then give it a byte_size attribute and
20488 then give a list of members. */
20489 if (complete && !ns_decl)
20490 {
20491 /* Prevent infinite recursion in cases where the type of some member of
20492 this type is expressed in terms of this type itself. */
20493 TREE_ASM_WRITTEN (type) = 1;
20494 add_byte_size_attribute (type_die, type);
20495 if (TYPE_STUB_DECL (type) != NULL_TREE)
20496 {
20497 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20498 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20499 }
20500
20501 /* If the first reference to this type was as the return type of an
20502 inline function, then it may not have a parent. Fix this now. */
20503 if (type_die->die_parent == NULL)
20504 add_child_die (scope_die, type_die);
20505
20506 push_decl_scope (type);
20507 gen_member_die (type, type_die);
20508 pop_decl_scope ();
20509
20510 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20511 if (TYPE_ARTIFICIAL (type))
20512 add_AT_flag (type_die, DW_AT_artificial, 1);
20513
20514 /* GNU extension: Record what type our vtable lives in. */
20515 if (TYPE_VFIELD (type))
20516 {
20517 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20518
20519 gen_type_die (vtype, context_die);
20520 add_AT_die_ref (type_die, DW_AT_containing_type,
20521 lookup_type_die (vtype));
20522 }
20523 }
20524 else
20525 {
20526 add_AT_flag (type_die, DW_AT_declaration, 1);
20527
20528 /* We don't need to do this for function-local types. */
20529 if (TYPE_STUB_DECL (type)
20530 && ! decl_function_context (TYPE_STUB_DECL (type)))
20531 vec_safe_push (incomplete_types, type);
20532 }
20533
20534 if (get_AT (type_die, DW_AT_name))
20535 add_pubtype (type, type_die);
20536 }
20537
20538 /* Generate a DIE for a subroutine _type_. */
20539
20540 static void
20541 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20542 {
20543 tree return_type = TREE_TYPE (type);
20544 dw_die_ref subr_die
20545 = new_die (DW_TAG_subroutine_type,
20546 scope_die_for (type, context_die), type);
20547
20548 equate_type_number_to_die (type, subr_die);
20549 add_prototyped_attribute (subr_die, type);
20550 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20551 gen_formal_types_die (type, subr_die);
20552
20553 if (get_AT (subr_die, DW_AT_name))
20554 add_pubtype (type, subr_die);
20555 }
20556
20557 /* Generate a DIE for a type definition. */
20558
20559 static void
20560 gen_typedef_die (tree decl, dw_die_ref context_die)
20561 {
20562 dw_die_ref type_die;
20563 tree origin;
20564
20565 if (TREE_ASM_WRITTEN (decl))
20566 {
20567 if (DECL_ORIGINAL_TYPE (decl))
20568 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20569 return;
20570 }
20571
20572 TREE_ASM_WRITTEN (decl) = 1;
20573 type_die = new_die (DW_TAG_typedef, context_die, decl);
20574 origin = decl_ultimate_origin (decl);
20575 if (origin != NULL)
20576 add_abstract_origin_attribute (type_die, origin);
20577 else
20578 {
20579 tree type;
20580
20581 add_name_and_src_coords_attributes (type_die, decl);
20582 if (DECL_ORIGINAL_TYPE (decl))
20583 {
20584 type = DECL_ORIGINAL_TYPE (decl);
20585
20586 if (type == error_mark_node)
20587 return;
20588
20589 gcc_assert (type != TREE_TYPE (decl));
20590 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20591 }
20592 else
20593 {
20594 type = TREE_TYPE (decl);
20595
20596 if (type == error_mark_node)
20597 return;
20598
20599 if (is_naming_typedef_decl (TYPE_NAME (type)))
20600 {
20601 /* Here, we are in the case of decl being a typedef naming
20602 an anonymous type, e.g:
20603 typedef struct {...} foo;
20604 In that case TREE_TYPE (decl) is not a typedef variant
20605 type and TYPE_NAME of the anonymous type is set to the
20606 TYPE_DECL of the typedef. This construct is emitted by
20607 the C++ FE.
20608
20609 TYPE is the anonymous struct named by the typedef
20610 DECL. As we need the DW_AT_type attribute of the
20611 DW_TAG_typedef to point to the DIE of TYPE, let's
20612 generate that DIE right away. add_type_attribute
20613 called below will then pick (via lookup_type_die) that
20614 anonymous struct DIE. */
20615 if (!TREE_ASM_WRITTEN (type))
20616 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20617
20618 /* This is a GNU Extension. We are adding a
20619 DW_AT_linkage_name attribute to the DIE of the
20620 anonymous struct TYPE. The value of that attribute
20621 is the name of the typedef decl naming the anonymous
20622 struct. This greatly eases the work of consumers of
20623 this debug info. */
20624 add_linkage_attr (lookup_type_die (type), decl);
20625 }
20626 }
20627
20628 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20629
20630 if (is_naming_typedef_decl (decl))
20631 /* We want that all subsequent calls to lookup_type_die with
20632 TYPE in argument yield the DW_TAG_typedef we have just
20633 created. */
20634 equate_type_number_to_die (type, type_die);
20635
20636 add_accessibility_attribute (type_die, decl);
20637 }
20638
20639 if (DECL_ABSTRACT_P (decl))
20640 equate_decl_number_to_die (decl, type_die);
20641
20642 if (get_AT (type_die, DW_AT_name))
20643 add_pubtype (decl, type_die);
20644 }
20645
20646 /* Generate a DIE for a struct, class, enum or union type. */
20647
20648 static void
20649 gen_tagged_type_die (tree type,
20650 dw_die_ref context_die,
20651 enum debug_info_usage usage)
20652 {
20653 int need_pop;
20654
20655 if (type == NULL_TREE
20656 || !is_tagged_type (type))
20657 return;
20658
20659 if (TREE_ASM_WRITTEN (type))
20660 need_pop = 0;
20661 /* If this is a nested type whose containing class hasn't been written
20662 out yet, writing it out will cover this one, too. This does not apply
20663 to instantiations of member class templates; they need to be added to
20664 the containing class as they are generated. FIXME: This hurts the
20665 idea of combining type decls from multiple TUs, since we can't predict
20666 what set of template instantiations we'll get. */
20667 else if (TYPE_CONTEXT (type)
20668 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20669 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20670 {
20671 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20672
20673 if (TREE_ASM_WRITTEN (type))
20674 return;
20675
20676 /* If that failed, attach ourselves to the stub. */
20677 push_decl_scope (TYPE_CONTEXT (type));
20678 context_die = lookup_type_die (TYPE_CONTEXT (type));
20679 need_pop = 1;
20680 }
20681 else if (TYPE_CONTEXT (type) != NULL_TREE
20682 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20683 {
20684 /* If this type is local to a function that hasn't been written
20685 out yet, use a NULL context for now; it will be fixed up in
20686 decls_for_scope. */
20687 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20688 /* A declaration DIE doesn't count; nested types need to go in the
20689 specification. */
20690 if (context_die && is_declaration_die (context_die))
20691 context_die = NULL;
20692 need_pop = 0;
20693 }
20694 else
20695 {
20696 context_die = declare_in_namespace (type, context_die);
20697 need_pop = 0;
20698 }
20699
20700 if (TREE_CODE (type) == ENUMERAL_TYPE)
20701 {
20702 /* This might have been written out by the call to
20703 declare_in_namespace. */
20704 if (!TREE_ASM_WRITTEN (type))
20705 gen_enumeration_type_die (type, context_die);
20706 }
20707 else
20708 gen_struct_or_union_type_die (type, context_die, usage);
20709
20710 if (need_pop)
20711 pop_decl_scope ();
20712
20713 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20714 it up if it is ever completed. gen_*_type_die will set it for us
20715 when appropriate. */
20716 }
20717
20718 /* Generate a type description DIE. */
20719
20720 static void
20721 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20722 enum debug_info_usage usage)
20723 {
20724 struct array_descr_info info;
20725
20726 if (type == NULL_TREE || type == error_mark_node)
20727 return;
20728
20729 if (flag_checking && type)
20730 verify_type (type);
20731
20732 if (TYPE_NAME (type) != NULL_TREE
20733 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20734 && is_redundant_typedef (TYPE_NAME (type))
20735 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20736 /* The DECL of this type is a typedef we don't want to emit debug
20737 info for but we want debug info for its underlying typedef.
20738 This can happen for e.g, the injected-class-name of a C++
20739 type. */
20740 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20741
20742 /* If TYPE is a typedef type variant, let's generate debug info
20743 for the parent typedef which TYPE is a type of. */
20744 if (typedef_variant_p (type))
20745 {
20746 if (TREE_ASM_WRITTEN (type))
20747 return;
20748
20749 /* Prevent broken recursion; we can't hand off to the same type. */
20750 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20751
20752 /* Give typedefs the right scope. */
20753 context_die = scope_die_for (type, context_die);
20754
20755 TREE_ASM_WRITTEN (type) = 1;
20756
20757 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20758 return;
20759 }
20760
20761 /* If type is an anonymous tagged type named by a typedef, let's
20762 generate debug info for the typedef. */
20763 if (is_naming_typedef_decl (TYPE_NAME (type)))
20764 {
20765 /* Use the DIE of the containing namespace as the parent DIE of
20766 the type description DIE we want to generate. */
20767 if (DECL_CONTEXT (TYPE_NAME (type))
20768 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20769 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20770
20771 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20772 return;
20773 }
20774
20775 /* We are going to output a DIE to represent the unqualified version
20776 of this type (i.e. without any const or volatile qualifiers) so
20777 get the main variant (i.e. the unqualified version) of this type
20778 now. (Vectors are special because the debugging info is in the
20779 cloned type itself). */
20780 if (TREE_CODE (type) != VECTOR_TYPE)
20781 type = type_main_variant (type);
20782
20783 /* If this is an array type with hidden descriptor, handle it first. */
20784 if (!TREE_ASM_WRITTEN (type)
20785 && lang_hooks.types.get_array_descr_info)
20786 {
20787 memset (&info, 0, sizeof (info));
20788 if (lang_hooks.types.get_array_descr_info (type, &info))
20789 {
20790 gen_descr_array_type_die (type, &info, context_die);
20791 TREE_ASM_WRITTEN (type) = 1;
20792 return;
20793 }
20794 }
20795
20796 if (TREE_ASM_WRITTEN (type))
20797 {
20798 /* Variable-length types may be incomplete even if
20799 TREE_ASM_WRITTEN. For such types, fall through to
20800 gen_array_type_die() and possibly fill in
20801 DW_AT_{upper,lower}_bound attributes. */
20802 if ((TREE_CODE (type) != ARRAY_TYPE
20803 && TREE_CODE (type) != RECORD_TYPE
20804 && TREE_CODE (type) != UNION_TYPE
20805 && TREE_CODE (type) != QUAL_UNION_TYPE)
20806 || !variably_modified_type_p (type, NULL))
20807 return;
20808 }
20809
20810 switch (TREE_CODE (type))
20811 {
20812 case ERROR_MARK:
20813 break;
20814
20815 case POINTER_TYPE:
20816 case REFERENCE_TYPE:
20817 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20818 ensures that the gen_type_die recursion will terminate even if the
20819 type is recursive. Recursive types are possible in Ada. */
20820 /* ??? We could perhaps do this for all types before the switch
20821 statement. */
20822 TREE_ASM_WRITTEN (type) = 1;
20823
20824 /* For these types, all that is required is that we output a DIE (or a
20825 set of DIEs) to represent the "basis" type. */
20826 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20827 DINFO_USAGE_IND_USE);
20828 break;
20829
20830 case OFFSET_TYPE:
20831 /* This code is used for C++ pointer-to-data-member types.
20832 Output a description of the relevant class type. */
20833 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20834 DINFO_USAGE_IND_USE);
20835
20836 /* Output a description of the type of the object pointed to. */
20837 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20838 DINFO_USAGE_IND_USE);
20839
20840 /* Now output a DIE to represent this pointer-to-data-member type
20841 itself. */
20842 gen_ptr_to_mbr_type_die (type, context_die);
20843 break;
20844
20845 case FUNCTION_TYPE:
20846 /* Force out return type (in case it wasn't forced out already). */
20847 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20848 DINFO_USAGE_DIR_USE);
20849 gen_subroutine_type_die (type, context_die);
20850 break;
20851
20852 case METHOD_TYPE:
20853 /* Force out return type (in case it wasn't forced out already). */
20854 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20855 DINFO_USAGE_DIR_USE);
20856 gen_subroutine_type_die (type, context_die);
20857 break;
20858
20859 case ARRAY_TYPE:
20860 case VECTOR_TYPE:
20861 gen_array_type_die (type, context_die);
20862 break;
20863
20864 case ENUMERAL_TYPE:
20865 case RECORD_TYPE:
20866 case UNION_TYPE:
20867 case QUAL_UNION_TYPE:
20868 gen_tagged_type_die (type, context_die, usage);
20869 return;
20870
20871 case VOID_TYPE:
20872 case INTEGER_TYPE:
20873 case REAL_TYPE:
20874 case FIXED_POINT_TYPE:
20875 case COMPLEX_TYPE:
20876 case BOOLEAN_TYPE:
20877 case POINTER_BOUNDS_TYPE:
20878 /* No DIEs needed for fundamental types. */
20879 break;
20880
20881 case NULLPTR_TYPE:
20882 case LANG_TYPE:
20883 /* Just use DW_TAG_unspecified_type. */
20884 {
20885 dw_die_ref type_die = lookup_type_die (type);
20886 if (type_die == NULL)
20887 {
20888 tree name = TYPE_IDENTIFIER (type);
20889 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20890 type);
20891 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20892 equate_type_number_to_die (type, type_die);
20893 }
20894 }
20895 break;
20896
20897 default:
20898 if (is_cxx_auto (type))
20899 {
20900 tree name = TYPE_IDENTIFIER (type);
20901 dw_die_ref *die = (name == get_identifier ("auto")
20902 ? &auto_die : &decltype_auto_die);
20903 if (!*die)
20904 {
20905 *die = new_die (DW_TAG_unspecified_type,
20906 comp_unit_die (), NULL_TREE);
20907 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20908 }
20909 equate_type_number_to_die (type, *die);
20910 break;
20911 }
20912 gcc_unreachable ();
20913 }
20914
20915 TREE_ASM_WRITTEN (type) = 1;
20916 }
20917
20918 static void
20919 gen_type_die (tree type, dw_die_ref context_die)
20920 {
20921 if (type != error_mark_node)
20922 {
20923 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20924 if (flag_checking)
20925 {
20926 dw_die_ref die = lookup_type_die (type);
20927 if (die)
20928 check_die (die);
20929 }
20930 }
20931 }
20932
20933 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20934 things which are local to the given block. */
20935
20936 static void
20937 gen_block_die (tree stmt, dw_die_ref context_die)
20938 {
20939 int must_output_die = 0;
20940 bool inlined_func;
20941
20942 /* Ignore blocks that are NULL. */
20943 if (stmt == NULL_TREE)
20944 return;
20945
20946 inlined_func = inlined_function_outer_scope_p (stmt);
20947
20948 /* If the block is one fragment of a non-contiguous block, do not
20949 process the variables, since they will have been done by the
20950 origin block. Do process subblocks. */
20951 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20952 {
20953 tree sub;
20954
20955 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20956 gen_block_die (sub, context_die);
20957
20958 return;
20959 }
20960
20961 /* Determine if we need to output any Dwarf DIEs at all to represent this
20962 block. */
20963 if (inlined_func)
20964 /* The outer scopes for inlinings *must* always be represented. We
20965 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20966 must_output_die = 1;
20967 else
20968 {
20969 /* Determine if this block directly contains any "significant"
20970 local declarations which we will need to output DIEs for. */
20971 if (debug_info_level > DINFO_LEVEL_TERSE)
20972 /* We are not in terse mode so *any* local declaration counts
20973 as being a "significant" one. */
20974 must_output_die = ((BLOCK_VARS (stmt) != NULL
20975 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20976 && (TREE_USED (stmt)
20977 || TREE_ASM_WRITTEN (stmt)
20978 || BLOCK_ABSTRACT (stmt)));
20979 else if ((TREE_USED (stmt)
20980 || TREE_ASM_WRITTEN (stmt)
20981 || BLOCK_ABSTRACT (stmt))
20982 && !dwarf2out_ignore_block (stmt))
20983 must_output_die = 1;
20984 }
20985
20986 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20987 DIE for any block which contains no significant local declarations at
20988 all. Rather, in such cases we just call `decls_for_scope' so that any
20989 needed Dwarf info for any sub-blocks will get properly generated. Note
20990 that in terse mode, our definition of what constitutes a "significant"
20991 local declaration gets restricted to include only inlined function
20992 instances and local (nested) function definitions. */
20993 if (must_output_die)
20994 {
20995 if (inlined_func)
20996 {
20997 /* If STMT block is abstract, that means we have been called
20998 indirectly from dwarf2out_abstract_function.
20999 That function rightfully marks the descendent blocks (of
21000 the abstract function it is dealing with) as being abstract,
21001 precisely to prevent us from emitting any
21002 DW_TAG_inlined_subroutine DIE as a descendent
21003 of an abstract function instance. So in that case, we should
21004 not call gen_inlined_subroutine_die.
21005
21006 Later though, when cgraph asks dwarf2out to emit info
21007 for the concrete instance of the function decl into which
21008 the concrete instance of STMT got inlined, the later will lead
21009 to the generation of a DW_TAG_inlined_subroutine DIE. */
21010 if (! BLOCK_ABSTRACT (stmt))
21011 gen_inlined_subroutine_die (stmt, context_die);
21012 }
21013 else
21014 gen_lexical_block_die (stmt, context_die);
21015 }
21016 else
21017 decls_for_scope (stmt, context_die);
21018 }
21019
21020 /* Process variable DECL (or variable with origin ORIGIN) within
21021 block STMT and add it to CONTEXT_DIE. */
21022 static void
21023 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21024 {
21025 dw_die_ref die;
21026 tree decl_or_origin = decl ? decl : origin;
21027
21028 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21029 die = lookup_decl_die (decl_or_origin);
21030 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21031 && TYPE_DECL_IS_STUB (decl_or_origin))
21032 die = lookup_type_die (TREE_TYPE (decl_or_origin));
21033 else
21034 die = NULL;
21035
21036 if (die != NULL && die->die_parent == NULL)
21037 add_child_die (context_die, die);
21038 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21039 {
21040 if (early_dwarf)
21041 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21042 stmt, context_die);
21043 }
21044 else
21045 gen_decl_die (decl, origin, context_die);
21046 }
21047
21048 /* Generate all of the decls declared within a given scope and (recursively)
21049 all of its sub-blocks. */
21050
21051 static void
21052 decls_for_scope (tree stmt, dw_die_ref context_die)
21053 {
21054 tree decl;
21055 unsigned int i;
21056 tree subblocks;
21057
21058 /* Ignore NULL blocks. */
21059 if (stmt == NULL_TREE)
21060 return;
21061
21062 /* Output the DIEs to represent all of the data objects and typedefs
21063 declared directly within this block but not within any nested
21064 sub-blocks. Also, nested function and tag DIEs have been
21065 generated with a parent of NULL; fix that up now. We don't
21066 have to do this if we're at -g1. */
21067 if (debug_info_level > DINFO_LEVEL_TERSE)
21068 {
21069 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21070 process_scope_var (stmt, decl, NULL_TREE, context_die);
21071 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21072 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21073 context_die);
21074 }
21075
21076 /* Even if we're at -g1, we need to process the subblocks in order to get
21077 inlined call information. */
21078
21079 /* Output the DIEs to represent all sub-blocks (and the items declared
21080 therein) of this block. */
21081 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21082 subblocks != NULL;
21083 subblocks = BLOCK_CHAIN (subblocks))
21084 gen_block_die (subblocks, context_die);
21085 }
21086
21087 /* Is this a typedef we can avoid emitting? */
21088
21089 static inline int
21090 is_redundant_typedef (const_tree decl)
21091 {
21092 if (TYPE_DECL_IS_STUB (decl))
21093 return 1;
21094
21095 if (DECL_ARTIFICIAL (decl)
21096 && DECL_CONTEXT (decl)
21097 && is_tagged_type (DECL_CONTEXT (decl))
21098 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21099 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21100 /* Also ignore the artificial member typedef for the class name. */
21101 return 1;
21102
21103 return 0;
21104 }
21105
21106 /* Return TRUE if TYPE is a typedef that names a type for linkage
21107 purposes. This kind of typedefs is produced by the C++ FE for
21108 constructs like:
21109
21110 typedef struct {...} foo;
21111
21112 In that case, there is no typedef variant type produced for foo.
21113 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21114 struct type. */
21115
21116 static bool
21117 is_naming_typedef_decl (const_tree decl)
21118 {
21119 if (decl == NULL_TREE
21120 || TREE_CODE (decl) != TYPE_DECL
21121 || !is_tagged_type (TREE_TYPE (decl))
21122 || DECL_IS_BUILTIN (decl)
21123 || is_redundant_typedef (decl)
21124 /* It looks like Ada produces TYPE_DECLs that are very similar
21125 to C++ naming typedefs but that have different
21126 semantics. Let's be specific to c++ for now. */
21127 || !is_cxx ())
21128 return FALSE;
21129
21130 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21131 && TYPE_NAME (TREE_TYPE (decl)) == decl
21132 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21133 != TYPE_NAME (TREE_TYPE (decl))));
21134 }
21135
21136 /* Looks up the DIE for a context. */
21137
21138 static inline dw_die_ref
21139 lookup_context_die (tree context)
21140 {
21141 if (context)
21142 {
21143 /* Find die that represents this context. */
21144 if (TYPE_P (context))
21145 {
21146 context = TYPE_MAIN_VARIANT (context);
21147 dw_die_ref ctx = lookup_type_die (context);
21148 if (!ctx)
21149 return NULL;
21150 return strip_naming_typedef (context, ctx);
21151 }
21152 else
21153 return lookup_decl_die (context);
21154 }
21155 return comp_unit_die ();
21156 }
21157
21158 /* Returns the DIE for a context. */
21159
21160 static inline dw_die_ref
21161 get_context_die (tree context)
21162 {
21163 if (context)
21164 {
21165 /* Find die that represents this context. */
21166 if (TYPE_P (context))
21167 {
21168 context = TYPE_MAIN_VARIANT (context);
21169 return strip_naming_typedef (context, force_type_die (context));
21170 }
21171 else
21172 return force_decl_die (context);
21173 }
21174 return comp_unit_die ();
21175 }
21176
21177 /* Returns the DIE for decl. A DIE will always be returned. */
21178
21179 static dw_die_ref
21180 force_decl_die (tree decl)
21181 {
21182 dw_die_ref decl_die;
21183 unsigned saved_external_flag;
21184 tree save_fn = NULL_TREE;
21185 decl_die = lookup_decl_die (decl);
21186 if (!decl_die)
21187 {
21188 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21189
21190 decl_die = lookup_decl_die (decl);
21191 if (decl_die)
21192 return decl_die;
21193
21194 switch (TREE_CODE (decl))
21195 {
21196 case FUNCTION_DECL:
21197 /* Clear current_function_decl, so that gen_subprogram_die thinks
21198 that this is a declaration. At this point, we just want to force
21199 declaration die. */
21200 save_fn = current_function_decl;
21201 current_function_decl = NULL_TREE;
21202 gen_subprogram_die (decl, context_die);
21203 current_function_decl = save_fn;
21204 break;
21205
21206 case VAR_DECL:
21207 /* Set external flag to force declaration die. Restore it after
21208 gen_decl_die() call. */
21209 saved_external_flag = DECL_EXTERNAL (decl);
21210 DECL_EXTERNAL (decl) = 1;
21211 gen_decl_die (decl, NULL, context_die);
21212 DECL_EXTERNAL (decl) = saved_external_flag;
21213 break;
21214
21215 case NAMESPACE_DECL:
21216 if (dwarf_version >= 3 || !dwarf_strict)
21217 dwarf2out_decl (decl);
21218 else
21219 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21220 decl_die = comp_unit_die ();
21221 break;
21222
21223 case TRANSLATION_UNIT_DECL:
21224 decl_die = comp_unit_die ();
21225 break;
21226
21227 default:
21228 gcc_unreachable ();
21229 }
21230
21231 /* We should be able to find the DIE now. */
21232 if (!decl_die)
21233 decl_die = lookup_decl_die (decl);
21234 gcc_assert (decl_die);
21235 }
21236
21237 return decl_die;
21238 }
21239
21240 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21241 always returned. */
21242
21243 static dw_die_ref
21244 force_type_die (tree type)
21245 {
21246 dw_die_ref type_die;
21247
21248 type_die = lookup_type_die (type);
21249 if (!type_die)
21250 {
21251 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21252
21253 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21254 context_die);
21255 gcc_assert (type_die);
21256 }
21257 return type_die;
21258 }
21259
21260 /* Force out any required namespaces to be able to output DECL,
21261 and return the new context_die for it, if it's changed. */
21262
21263 static dw_die_ref
21264 setup_namespace_context (tree thing, dw_die_ref context_die)
21265 {
21266 tree context = (DECL_P (thing)
21267 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21268 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21269 /* Force out the namespace. */
21270 context_die = force_decl_die (context);
21271
21272 return context_die;
21273 }
21274
21275 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21276 type) within its namespace, if appropriate.
21277
21278 For compatibility with older debuggers, namespace DIEs only contain
21279 declarations; all definitions are emitted at CU scope, with
21280 DW_AT_specification pointing to the declaration (like with class
21281 members). */
21282
21283 static dw_die_ref
21284 declare_in_namespace (tree thing, dw_die_ref context_die)
21285 {
21286 dw_die_ref ns_context;
21287
21288 if (debug_info_level <= DINFO_LEVEL_TERSE)
21289 return context_die;
21290
21291 /* External declarations in the local scope only need to be emitted
21292 once, not once in the namespace and once in the scope.
21293
21294 This avoids declaring the `extern' below in the
21295 namespace DIE as well as in the innermost scope:
21296
21297 namespace S
21298 {
21299 int i=5;
21300 int foo()
21301 {
21302 int i=8;
21303 extern int i;
21304 return i;
21305 }
21306 }
21307 */
21308 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21309 return context_die;
21310
21311 /* If this decl is from an inlined function, then don't try to emit it in its
21312 namespace, as we will get confused. It would have already been emitted
21313 when the abstract instance of the inline function was emitted anyways. */
21314 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21315 return context_die;
21316
21317 ns_context = setup_namespace_context (thing, context_die);
21318
21319 if (ns_context != context_die)
21320 {
21321 if (is_fortran ())
21322 return ns_context;
21323 if (DECL_P (thing))
21324 gen_decl_die (thing, NULL, ns_context);
21325 else
21326 gen_type_die (thing, ns_context);
21327 }
21328 return context_die;
21329 }
21330
21331 /* Generate a DIE for a namespace or namespace alias. */
21332
21333 static void
21334 gen_namespace_die (tree decl, dw_die_ref context_die)
21335 {
21336 dw_die_ref namespace_die;
21337
21338 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21339 they are an alias of. */
21340 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21341 {
21342 /* Output a real namespace or module. */
21343 context_die = setup_namespace_context (decl, comp_unit_die ());
21344 namespace_die = new_die (is_fortran ()
21345 ? DW_TAG_module : DW_TAG_namespace,
21346 context_die, decl);
21347 /* For Fortran modules defined in different CU don't add src coords. */
21348 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21349 {
21350 const char *name = dwarf2_name (decl, 0);
21351 if (name)
21352 add_name_attribute (namespace_die, name);
21353 }
21354 else
21355 add_name_and_src_coords_attributes (namespace_die, decl);
21356 if (DECL_EXTERNAL (decl))
21357 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21358 equate_decl_number_to_die (decl, namespace_die);
21359 }
21360 else
21361 {
21362 /* Output a namespace alias. */
21363
21364 /* Force out the namespace we are an alias of, if necessary. */
21365 dw_die_ref origin_die
21366 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21367
21368 if (DECL_FILE_SCOPE_P (decl)
21369 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21370 context_die = setup_namespace_context (decl, comp_unit_die ());
21371 /* Now create the namespace alias DIE. */
21372 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21373 add_name_and_src_coords_attributes (namespace_die, decl);
21374 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21375 equate_decl_number_to_die (decl, namespace_die);
21376 }
21377 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21378 if (want_pubnames ())
21379 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21380 }
21381
21382 /* Generate Dwarf debug information for a decl described by DECL.
21383 The return value is currently only meaningful for PARM_DECLs,
21384 for all other decls it returns NULL. */
21385
21386 static dw_die_ref
21387 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21388 {
21389 tree decl_or_origin = decl ? decl : origin;
21390 tree class_origin = NULL, ultimate_origin;
21391
21392 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21393 return NULL;
21394
21395 /* Ignore pointer bounds decls. */
21396 if (DECL_P (decl_or_origin)
21397 && TREE_TYPE (decl_or_origin)
21398 && POINTER_BOUNDS_P (decl_or_origin))
21399 return NULL;
21400
21401 switch (TREE_CODE (decl_or_origin))
21402 {
21403 case ERROR_MARK:
21404 break;
21405
21406 case CONST_DECL:
21407 if (!is_fortran () && !is_ada ())
21408 {
21409 /* The individual enumerators of an enum type get output when we output
21410 the Dwarf representation of the relevant enum type itself. */
21411 break;
21412 }
21413
21414 /* Emit its type. */
21415 gen_type_die (TREE_TYPE (decl), context_die);
21416
21417 /* And its containing namespace. */
21418 context_die = declare_in_namespace (decl, context_die);
21419
21420 gen_const_die (decl, context_die);
21421 break;
21422
21423 case FUNCTION_DECL:
21424 /* Don't output any DIEs to represent mere function declarations,
21425 unless they are class members or explicit block externs. */
21426 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21427 && DECL_FILE_SCOPE_P (decl_or_origin)
21428 && (current_function_decl == NULL_TREE
21429 || DECL_ARTIFICIAL (decl_or_origin)))
21430 break;
21431
21432 #if 0
21433 /* FIXME */
21434 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21435 on local redeclarations of global functions. That seems broken. */
21436 if (current_function_decl != decl)
21437 /* This is only a declaration. */;
21438 #endif
21439
21440 /* If we're emitting a clone, emit info for the abstract instance. */
21441 if (origin || DECL_ORIGIN (decl) != decl)
21442 dwarf2out_abstract_function (origin
21443 ? DECL_ORIGIN (origin)
21444 : DECL_ABSTRACT_ORIGIN (decl));
21445
21446 /* If we're emitting an out-of-line copy of an inline function,
21447 emit info for the abstract instance and set up to refer to it. */
21448 else if (cgraph_function_possibly_inlined_p (decl)
21449 && ! DECL_ABSTRACT_P (decl)
21450 && ! class_or_namespace_scope_p (context_die)
21451 /* dwarf2out_abstract_function won't emit a die if this is just
21452 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21453 that case, because that works only if we have a die. */
21454 && DECL_INITIAL (decl) != NULL_TREE)
21455 {
21456 dwarf2out_abstract_function (decl);
21457 set_decl_origin_self (decl);
21458 }
21459
21460 /* Otherwise we're emitting the primary DIE for this decl. */
21461 else if (debug_info_level > DINFO_LEVEL_TERSE)
21462 {
21463 /* Before we describe the FUNCTION_DECL itself, make sure that we
21464 have its containing type. */
21465 if (!origin)
21466 origin = decl_class_context (decl);
21467 if (origin != NULL_TREE)
21468 gen_type_die (origin, context_die);
21469
21470 /* And its return type. */
21471 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21472
21473 /* And its virtual context. */
21474 if (DECL_VINDEX (decl) != NULL_TREE)
21475 gen_type_die (DECL_CONTEXT (decl), context_die);
21476
21477 /* Make sure we have a member DIE for decl. */
21478 if (origin != NULL_TREE)
21479 gen_type_die_for_member (origin, decl, context_die);
21480
21481 /* And its containing namespace. */
21482 context_die = declare_in_namespace (decl, context_die);
21483 }
21484
21485 /* Now output a DIE to represent the function itself. */
21486 if (decl)
21487 gen_subprogram_die (decl, context_die);
21488 break;
21489
21490 case TYPE_DECL:
21491 /* If we are in terse mode, don't generate any DIEs to represent any
21492 actual typedefs. */
21493 if (debug_info_level <= DINFO_LEVEL_TERSE)
21494 break;
21495
21496 /* In the special case of a TYPE_DECL node representing the declaration
21497 of some type tag, if the given TYPE_DECL is marked as having been
21498 instantiated from some other (original) TYPE_DECL node (e.g. one which
21499 was generated within the original definition of an inline function) we
21500 used to generate a special (abbreviated) DW_TAG_structure_type,
21501 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21502 should be actually referencing those DIEs, as variable DIEs with that
21503 type would be emitted already in the abstract origin, so it was always
21504 removed during unused type prunning. Don't add anything in this
21505 case. */
21506 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21507 break;
21508
21509 if (is_redundant_typedef (decl))
21510 gen_type_die (TREE_TYPE (decl), context_die);
21511 else
21512 /* Output a DIE to represent the typedef itself. */
21513 gen_typedef_die (decl, context_die);
21514 break;
21515
21516 case LABEL_DECL:
21517 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21518 gen_label_die (decl, context_die);
21519 break;
21520
21521 case VAR_DECL:
21522 case RESULT_DECL:
21523 /* If we are in terse mode, don't generate any DIEs to represent any
21524 variable declarations or definitions. */
21525 if (debug_info_level <= DINFO_LEVEL_TERSE)
21526 break;
21527
21528 /* Output any DIEs that are needed to specify the type of this data
21529 object. */
21530 if (decl_by_reference_p (decl_or_origin))
21531 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21532 else
21533 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21534
21535 /* And its containing type. */
21536 class_origin = decl_class_context (decl_or_origin);
21537 if (class_origin != NULL_TREE)
21538 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21539
21540 /* And its containing namespace. */
21541 context_die = declare_in_namespace (decl_or_origin, context_die);
21542
21543 /* Now output the DIE to represent the data object itself. This gets
21544 complicated because of the possibility that the VAR_DECL really
21545 represents an inlined instance of a formal parameter for an inline
21546 function. */
21547 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21548 if (ultimate_origin != NULL_TREE
21549 && TREE_CODE (ultimate_origin) == PARM_DECL)
21550 gen_formal_parameter_die (decl, origin,
21551 true /* Emit name attribute. */,
21552 context_die);
21553 else
21554 gen_variable_die (decl, origin, context_die);
21555 break;
21556
21557 case FIELD_DECL:
21558 /* Ignore the nameless fields that are used to skip bits but handle C++
21559 anonymous unions and structs. */
21560 if (DECL_NAME (decl) != NULL_TREE
21561 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21562 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21563 {
21564 gen_type_die (member_declared_type (decl), context_die);
21565 gen_field_die (decl, context_die);
21566 }
21567 break;
21568
21569 case PARM_DECL:
21570 if (DECL_BY_REFERENCE (decl_or_origin))
21571 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21572 else
21573 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21574 return gen_formal_parameter_die (decl, origin,
21575 true /* Emit name attribute. */,
21576 context_die);
21577
21578 case NAMESPACE_DECL:
21579 case IMPORTED_DECL:
21580 if (dwarf_version >= 3 || !dwarf_strict)
21581 gen_namespace_die (decl, context_die);
21582 break;
21583
21584 case NAMELIST_DECL:
21585 gen_namelist_decl (DECL_NAME (decl), context_die,
21586 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21587 break;
21588
21589 default:
21590 /* Probably some frontend-internal decl. Assume we don't care. */
21591 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21592 break;
21593 }
21594
21595 return NULL;
21596 }
21597 \f
21598 /* Output initial debug information for global DECL. Called at the
21599 end of the parsing process.
21600
21601 This is the initial debug generation process. As such, the DIEs
21602 generated may be incomplete. A later debug generation pass
21603 (dwarf2out_late_global_decl) will augment the information generated
21604 in this pass (e.g., with complete location info). */
21605
21606 static void
21607 dwarf2out_early_global_decl (tree decl)
21608 {
21609 set_early_dwarf s;
21610
21611 /* gen_decl_die() will set DECL_ABSTRACT because
21612 cgraph_function_possibly_inlined_p() returns true. This is in
21613 turn will cause DW_AT_inline attributes to be set.
21614
21615 This happens because at early dwarf generation, there is no
21616 cgraph information, causing cgraph_function_possibly_inlined_p()
21617 to return true. Trick cgraph_function_possibly_inlined_p()
21618 while we generate dwarf early. */
21619 bool save = symtab->global_info_ready;
21620 symtab->global_info_ready = true;
21621
21622 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21623 other DECLs and they can point to template types or other things
21624 that dwarf2out can't handle when done via dwarf2out_decl. */
21625 if (TREE_CODE (decl) != TYPE_DECL
21626 && TREE_CODE (decl) != PARM_DECL)
21627 {
21628 tree save_fndecl = current_function_decl;
21629 if (TREE_CODE (decl) == FUNCTION_DECL)
21630 {
21631 /* No cfun means the symbol has no body, so there's nothing
21632 to emit. */
21633 if (!DECL_STRUCT_FUNCTION (decl))
21634 goto early_decl_exit;
21635
21636 current_function_decl = decl;
21637 }
21638 dwarf2out_decl (decl);
21639 if (TREE_CODE (decl) == FUNCTION_DECL)
21640 current_function_decl = save_fndecl;
21641 }
21642 early_decl_exit:
21643 symtab->global_info_ready = save;
21644 }
21645
21646 /* Output debug information for global decl DECL. Called from
21647 toplev.c after compilation proper has finished. */
21648
21649 static void
21650 dwarf2out_late_global_decl (tree decl)
21651 {
21652 /* We have to generate early debug late for LTO. */
21653 if (in_lto_p)
21654 dwarf2out_early_global_decl (decl);
21655
21656 /* Fill-in any location information we were unable to determine
21657 on the first pass. */
21658 if (TREE_CODE (decl) == VAR_DECL
21659 && !POINTER_BOUNDS_P (decl))
21660 {
21661 dw_die_ref die = lookup_decl_die (decl);
21662 if (die)
21663 add_location_or_const_value_attribute (die, decl, false);
21664 }
21665 }
21666
21667 /* Output debug information for type decl DECL. Called from toplev.c
21668 and from language front ends (to record built-in types). */
21669 static void
21670 dwarf2out_type_decl (tree decl, int local)
21671 {
21672 if (!local)
21673 {
21674 set_early_dwarf s;
21675 dwarf2out_decl (decl);
21676 }
21677 }
21678
21679 /* Output debug information for imported module or decl DECL.
21680 NAME is non-NULL name in the lexical block if the decl has been renamed.
21681 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21682 that DECL belongs to.
21683 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21684 static void
21685 dwarf2out_imported_module_or_decl_1 (tree decl,
21686 tree name,
21687 tree lexical_block,
21688 dw_die_ref lexical_block_die)
21689 {
21690 expanded_location xloc;
21691 dw_die_ref imported_die = NULL;
21692 dw_die_ref at_import_die;
21693
21694 if (TREE_CODE (decl) == IMPORTED_DECL)
21695 {
21696 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21697 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21698 gcc_assert (decl);
21699 }
21700 else
21701 xloc = expand_location (input_location);
21702
21703 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21704 {
21705 at_import_die = force_type_die (TREE_TYPE (decl));
21706 /* For namespace N { typedef void T; } using N::T; base_type_die
21707 returns NULL, but DW_TAG_imported_declaration requires
21708 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21709 if (!at_import_die)
21710 {
21711 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21712 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21713 at_import_die = lookup_type_die (TREE_TYPE (decl));
21714 gcc_assert (at_import_die);
21715 }
21716 }
21717 else
21718 {
21719 at_import_die = lookup_decl_die (decl);
21720 if (!at_import_die)
21721 {
21722 /* If we're trying to avoid duplicate debug info, we may not have
21723 emitted the member decl for this field. Emit it now. */
21724 if (TREE_CODE (decl) == FIELD_DECL)
21725 {
21726 tree type = DECL_CONTEXT (decl);
21727
21728 if (TYPE_CONTEXT (type)
21729 && TYPE_P (TYPE_CONTEXT (type))
21730 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21731 DINFO_USAGE_DIR_USE))
21732 return;
21733 gen_type_die_for_member (type, decl,
21734 get_context_die (TYPE_CONTEXT (type)));
21735 }
21736 if (TREE_CODE (decl) == NAMELIST_DECL)
21737 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21738 get_context_die (DECL_CONTEXT (decl)),
21739 NULL_TREE);
21740 else
21741 at_import_die = force_decl_die (decl);
21742 }
21743 }
21744
21745 if (TREE_CODE (decl) == NAMESPACE_DECL)
21746 {
21747 if (dwarf_version >= 3 || !dwarf_strict)
21748 imported_die = new_die (DW_TAG_imported_module,
21749 lexical_block_die,
21750 lexical_block);
21751 else
21752 return;
21753 }
21754 else
21755 imported_die = new_die (DW_TAG_imported_declaration,
21756 lexical_block_die,
21757 lexical_block);
21758
21759 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21760 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21761 if (name)
21762 add_AT_string (imported_die, DW_AT_name,
21763 IDENTIFIER_POINTER (name));
21764 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21765 }
21766
21767 /* Output debug information for imported module or decl DECL.
21768 NAME is non-NULL name in context if the decl has been renamed.
21769 CHILD is true if decl is one of the renamed decls as part of
21770 importing whole module. */
21771
21772 static void
21773 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21774 bool child)
21775 {
21776 /* dw_die_ref at_import_die; */
21777 dw_die_ref scope_die;
21778
21779 if (debug_info_level <= DINFO_LEVEL_TERSE)
21780 return;
21781
21782 gcc_assert (decl);
21783
21784 set_early_dwarf s;
21785
21786 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21787 We need decl DIE for reference and scope die. First, get DIE for the decl
21788 itself. */
21789
21790 /* Get the scope die for decl context. Use comp_unit_die for global module
21791 or decl. If die is not found for non globals, force new die. */
21792 if (context
21793 && TYPE_P (context)
21794 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21795 return;
21796
21797 if (!(dwarf_version >= 3 || !dwarf_strict))
21798 return;
21799
21800 scope_die = get_context_die (context);
21801
21802 if (child)
21803 {
21804 gcc_assert (scope_die->die_child);
21805 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21806 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21807 scope_die = scope_die->die_child;
21808 }
21809
21810 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21811 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21812 }
21813
21814 /* Output debug information for namelists. */
21815
21816 static dw_die_ref
21817 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21818 {
21819 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21820 tree value;
21821 unsigned i;
21822
21823 if (debug_info_level <= DINFO_LEVEL_TERSE)
21824 return NULL;
21825
21826 gcc_assert (scope_die != NULL);
21827 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21828 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21829
21830 /* If there are no item_decls, we have a nondefining namelist, e.g.
21831 with USE association; hence, set DW_AT_declaration. */
21832 if (item_decls == NULL_TREE)
21833 {
21834 add_AT_flag (nml_die, DW_AT_declaration, 1);
21835 return nml_die;
21836 }
21837
21838 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21839 {
21840 nml_item_ref_die = lookup_decl_die (value);
21841 if (!nml_item_ref_die)
21842 nml_item_ref_die = force_decl_die (value);
21843
21844 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21845 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21846 }
21847 return nml_die;
21848 }
21849
21850
21851 /* Write the debugging output for DECL and return the DIE. */
21852
21853 static void
21854 dwarf2out_decl (tree decl)
21855 {
21856 dw_die_ref context_die = comp_unit_die ();
21857
21858 switch (TREE_CODE (decl))
21859 {
21860 case ERROR_MARK:
21861 return;
21862
21863 case FUNCTION_DECL:
21864 /* What we would really like to do here is to filter out all mere
21865 file-scope declarations of file-scope functions which are never
21866 referenced later within this translation unit (and keep all of ones
21867 that *are* referenced later on) but we aren't clairvoyant, so we have
21868 no idea which functions will be referenced in the future (i.e. later
21869 on within the current translation unit). So here we just ignore all
21870 file-scope function declarations which are not also definitions. If
21871 and when the debugger needs to know something about these functions,
21872 it will have to hunt around and find the DWARF information associated
21873 with the definition of the function.
21874
21875 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21876 nodes represent definitions and which ones represent mere
21877 declarations. We have to check DECL_INITIAL instead. That's because
21878 the C front-end supports some weird semantics for "extern inline"
21879 function definitions. These can get inlined within the current
21880 translation unit (and thus, we need to generate Dwarf info for their
21881 abstract instances so that the Dwarf info for the concrete inlined
21882 instances can have something to refer to) but the compiler never
21883 generates any out-of-lines instances of such things (despite the fact
21884 that they *are* definitions).
21885
21886 The important point is that the C front-end marks these "extern
21887 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21888 them anyway. Note that the C++ front-end also plays some similar games
21889 for inline function definitions appearing within include files which
21890 also contain `#pragma interface' pragmas.
21891
21892 If we are called from dwarf2out_abstract_function output a DIE
21893 anyway. We can end up here this way with early inlining and LTO
21894 where the inlined function is output in a different LTRANS unit
21895 or not at all. */
21896 if (DECL_INITIAL (decl) == NULL_TREE
21897 && ! DECL_ABSTRACT_P (decl))
21898 return;
21899
21900 /* If we're a nested function, initially use a parent of NULL; if we're
21901 a plain function, this will be fixed up in decls_for_scope. If
21902 we're a method, it will be ignored, since we already have a DIE. */
21903 if (decl_function_context (decl)
21904 /* But if we're in terse mode, we don't care about scope. */
21905 && debug_info_level > DINFO_LEVEL_TERSE)
21906 context_die = NULL;
21907 break;
21908
21909 case VAR_DECL:
21910 /* For local statics lookup proper context die. */
21911 if (local_function_static (decl))
21912 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21913
21914 /* If we are in terse mode, don't generate any DIEs to represent any
21915 variable declarations or definitions. */
21916 if (debug_info_level <= DINFO_LEVEL_TERSE)
21917 return;
21918 break;
21919
21920 case CONST_DECL:
21921 if (debug_info_level <= DINFO_LEVEL_TERSE)
21922 return;
21923 if (!is_fortran () && !is_ada ())
21924 return;
21925 if (TREE_STATIC (decl) && decl_function_context (decl))
21926 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21927 break;
21928
21929 case NAMESPACE_DECL:
21930 case IMPORTED_DECL:
21931 if (debug_info_level <= DINFO_LEVEL_TERSE)
21932 return;
21933 if (lookup_decl_die (decl) != NULL)
21934 return;
21935 break;
21936
21937 case TYPE_DECL:
21938 /* Don't emit stubs for types unless they are needed by other DIEs. */
21939 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21940 return;
21941
21942 /* Don't bother trying to generate any DIEs to represent any of the
21943 normal built-in types for the language we are compiling. */
21944 if (DECL_IS_BUILTIN (decl))
21945 return;
21946
21947 /* If we are in terse mode, don't generate any DIEs for types. */
21948 if (debug_info_level <= DINFO_LEVEL_TERSE)
21949 return;
21950
21951 /* If we're a function-scope tag, initially use a parent of NULL;
21952 this will be fixed up in decls_for_scope. */
21953 if (decl_function_context (decl))
21954 context_die = NULL;
21955
21956 break;
21957
21958 case NAMELIST_DECL:
21959 break;
21960
21961 default:
21962 return;
21963 }
21964
21965 gen_decl_die (decl, NULL, context_die);
21966
21967 if (flag_checking)
21968 {
21969 dw_die_ref die = lookup_decl_die (decl);
21970 if (die)
21971 check_die (die);
21972 }
21973 }
21974
21975 /* Write the debugging output for DECL. */
21976
21977 static void
21978 dwarf2out_function_decl (tree decl)
21979 {
21980 dwarf2out_decl (decl);
21981 call_arg_locations = NULL;
21982 call_arg_loc_last = NULL;
21983 call_site_count = -1;
21984 tail_call_site_count = -1;
21985 decl_loc_table->empty ();
21986 cached_dw_loc_list_table->empty ();
21987 }
21988
21989 /* Output a marker (i.e. a label) for the beginning of the generated code for
21990 a lexical block. */
21991
21992 static void
21993 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21994 unsigned int blocknum)
21995 {
21996 switch_to_section (current_function_section ());
21997 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21998 }
21999
22000 /* Output a marker (i.e. a label) for the end of the generated code for a
22001 lexical block. */
22002
22003 static void
22004 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22005 {
22006 switch_to_section (current_function_section ());
22007 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22008 }
22009
22010 /* Returns nonzero if it is appropriate not to emit any debugging
22011 information for BLOCK, because it doesn't contain any instructions.
22012
22013 Don't allow this for blocks with nested functions or local classes
22014 as we would end up with orphans, and in the presence of scheduling
22015 we may end up calling them anyway. */
22016
22017 static bool
22018 dwarf2out_ignore_block (const_tree block)
22019 {
22020 tree decl;
22021 unsigned int i;
22022
22023 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22024 if (TREE_CODE (decl) == FUNCTION_DECL
22025 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22026 return 0;
22027 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22028 {
22029 decl = BLOCK_NONLOCALIZED_VAR (block, i);
22030 if (TREE_CODE (decl) == FUNCTION_DECL
22031 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22032 return 0;
22033 }
22034
22035 return 1;
22036 }
22037
22038 /* Hash table routines for file_hash. */
22039
22040 bool
22041 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22042 {
22043 return filename_cmp (p1->filename, p2) == 0;
22044 }
22045
22046 hashval_t
22047 dwarf_file_hasher::hash (dwarf_file_data *p)
22048 {
22049 return htab_hash_string (p->filename);
22050 }
22051
22052 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22053 dwarf2out.c) and return its "index". The index of each (known) filename is
22054 just a unique number which is associated with only that one filename. We
22055 need such numbers for the sake of generating labels (in the .debug_sfnames
22056 section) and references to those files numbers (in the .debug_srcinfo
22057 and .debug_macinfo sections). If the filename given as an argument is not
22058 found in our current list, add it to the list and assign it the next
22059 available unique index number. */
22060
22061 static struct dwarf_file_data *
22062 lookup_filename (const char *file_name)
22063 {
22064 struct dwarf_file_data * created;
22065
22066 if (!file_name)
22067 return NULL;
22068
22069 dwarf_file_data **slot
22070 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22071 INSERT);
22072 if (*slot)
22073 return *slot;
22074
22075 created = ggc_alloc<dwarf_file_data> ();
22076 created->filename = file_name;
22077 created->emitted_number = 0;
22078 *slot = created;
22079 return created;
22080 }
22081
22082 /* If the assembler will construct the file table, then translate the compiler
22083 internal file table number into the assembler file table number, and emit
22084 a .file directive if we haven't already emitted one yet. The file table
22085 numbers are different because we prune debug info for unused variables and
22086 types, which may include filenames. */
22087
22088 static int
22089 maybe_emit_file (struct dwarf_file_data * fd)
22090 {
22091 if (! fd->emitted_number)
22092 {
22093 if (last_emitted_file)
22094 fd->emitted_number = last_emitted_file->emitted_number + 1;
22095 else
22096 fd->emitted_number = 1;
22097 last_emitted_file = fd;
22098
22099 if (DWARF2_ASM_LINE_DEBUG_INFO)
22100 {
22101 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22102 output_quoted_string (asm_out_file,
22103 remap_debug_filename (fd->filename));
22104 fputc ('\n', asm_out_file);
22105 }
22106 }
22107
22108 return fd->emitted_number;
22109 }
22110
22111 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22112 That generation should happen after function debug info has been
22113 generated. The value of the attribute is the constant value of ARG. */
22114
22115 static void
22116 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22117 {
22118 die_arg_entry entry;
22119
22120 if (!die || !arg)
22121 return;
22122
22123 gcc_assert (early_dwarf);
22124
22125 if (!tmpl_value_parm_die_table)
22126 vec_alloc (tmpl_value_parm_die_table, 32);
22127
22128 entry.die = die;
22129 entry.arg = arg;
22130 vec_safe_push (tmpl_value_parm_die_table, entry);
22131 }
22132
22133 /* Return TRUE if T is an instance of generic type, FALSE
22134 otherwise. */
22135
22136 static bool
22137 generic_type_p (tree t)
22138 {
22139 if (t == NULL_TREE || !TYPE_P (t))
22140 return false;
22141 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22142 }
22143
22144 /* Schedule the generation of the generic parameter dies for the
22145 instance of generic type T. The proper generation itself is later
22146 done by gen_scheduled_generic_parms_dies. */
22147
22148 static void
22149 schedule_generic_params_dies_gen (tree t)
22150 {
22151 if (!generic_type_p (t))
22152 return;
22153
22154 gcc_assert (early_dwarf);
22155
22156 if (!generic_type_instances)
22157 vec_alloc (generic_type_instances, 256);
22158
22159 vec_safe_push (generic_type_instances, t);
22160 }
22161
22162 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22163 by append_entry_to_tmpl_value_parm_die_table. This function must
22164 be called after function DIEs have been generated. */
22165
22166 static void
22167 gen_remaining_tmpl_value_param_die_attribute (void)
22168 {
22169 if (tmpl_value_parm_die_table)
22170 {
22171 unsigned i, j;
22172 die_arg_entry *e;
22173
22174 /* We do this in two phases - first get the cases we can
22175 handle during early-finish, preserving those we cannot
22176 (containing symbolic constants where we don't yet know
22177 whether we are going to output the referenced symbols).
22178 For those we try again at late-finish. */
22179 j = 0;
22180 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22181 {
22182 if (!tree_add_const_value_attribute (e->die, e->arg))
22183 (*tmpl_value_parm_die_table)[j++] = *e;
22184 }
22185 tmpl_value_parm_die_table->truncate (j);
22186 }
22187 }
22188
22189 /* Generate generic parameters DIEs for instances of generic types
22190 that have been previously scheduled by
22191 schedule_generic_params_dies_gen. This function must be called
22192 after all the types of the CU have been laid out. */
22193
22194 static void
22195 gen_scheduled_generic_parms_dies (void)
22196 {
22197 unsigned i;
22198 tree t;
22199
22200 if (!generic_type_instances)
22201 return;
22202
22203 /* We end up "recursing" into schedule_generic_params_dies_gen, so
22204 pretend this generation is part of "early dwarf" as well. */
22205 set_early_dwarf s;
22206
22207 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22208 if (COMPLETE_TYPE_P (t))
22209 gen_generic_params_dies (t);
22210
22211 generic_type_instances = NULL;
22212 }
22213
22214
22215 /* Replace DW_AT_name for the decl with name. */
22216
22217 static void
22218 dwarf2out_set_name (tree decl, tree name)
22219 {
22220 dw_die_ref die;
22221 dw_attr_node *attr;
22222 const char *dname;
22223
22224 die = TYPE_SYMTAB_DIE (decl);
22225 if (!die)
22226 return;
22227
22228 dname = dwarf2_name (name, 0);
22229 if (!dname)
22230 return;
22231
22232 attr = get_AT (die, DW_AT_name);
22233 if (attr)
22234 {
22235 struct indirect_string_node *node;
22236
22237 node = find_AT_string (dname);
22238 /* replace the string. */
22239 attr->dw_attr_val.v.val_str = node;
22240 }
22241
22242 else
22243 add_name_attribute (die, dname);
22244 }
22245
22246 /* True if before or during processing of the first function being emitted. */
22247 static bool in_first_function_p = true;
22248 /* True if loc_note during dwarf2out_var_location call might still be
22249 before first real instruction at address equal to .Ltext0. */
22250 static bool maybe_at_text_label_p = true;
22251 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22252 static unsigned int first_loclabel_num_not_at_text_label;
22253
22254 /* Called by the final INSN scan whenever we see a var location. We
22255 use it to drop labels in the right places, and throw the location in
22256 our lookup table. */
22257
22258 static void
22259 dwarf2out_var_location (rtx_insn *loc_note)
22260 {
22261 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22262 struct var_loc_node *newloc;
22263 rtx_insn *next_real, *next_note;
22264 static const char *last_label;
22265 static const char *last_postcall_label;
22266 static bool last_in_cold_section_p;
22267 static rtx_insn *expected_next_loc_note;
22268 tree decl;
22269 bool var_loc_p;
22270
22271 if (!NOTE_P (loc_note))
22272 {
22273 if (CALL_P (loc_note))
22274 {
22275 call_site_count++;
22276 if (SIBLING_CALL_P (loc_note))
22277 tail_call_site_count++;
22278 }
22279 return;
22280 }
22281
22282 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22283 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22284 return;
22285
22286 /* Optimize processing a large consecutive sequence of location
22287 notes so we don't spend too much time in next_real_insn. If the
22288 next insn is another location note, remember the next_real_insn
22289 calculation for next time. */
22290 next_real = cached_next_real_insn;
22291 if (next_real)
22292 {
22293 if (expected_next_loc_note != loc_note)
22294 next_real = NULL;
22295 }
22296
22297 next_note = NEXT_INSN (loc_note);
22298 if (! next_note
22299 || next_note->deleted ()
22300 || ! NOTE_P (next_note)
22301 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22302 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22303 next_note = NULL;
22304
22305 if (! next_real)
22306 next_real = next_real_insn (loc_note);
22307
22308 if (next_note)
22309 {
22310 expected_next_loc_note = next_note;
22311 cached_next_real_insn = next_real;
22312 }
22313 else
22314 cached_next_real_insn = NULL;
22315
22316 /* If there are no instructions which would be affected by this note,
22317 don't do anything. */
22318 if (var_loc_p
22319 && next_real == NULL_RTX
22320 && !NOTE_DURING_CALL_P (loc_note))
22321 return;
22322
22323 if (next_real == NULL_RTX)
22324 next_real = get_last_insn ();
22325
22326 /* If there were any real insns between note we processed last time
22327 and this note (or if it is the first note), clear
22328 last_{,postcall_}label so that they are not reused this time. */
22329 if (last_var_location_insn == NULL_RTX
22330 || last_var_location_insn != next_real
22331 || last_in_cold_section_p != in_cold_section_p)
22332 {
22333 last_label = NULL;
22334 last_postcall_label = NULL;
22335 }
22336
22337 if (var_loc_p)
22338 {
22339 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22340 newloc = add_var_loc_to_decl (decl, loc_note,
22341 NOTE_DURING_CALL_P (loc_note)
22342 ? last_postcall_label : last_label);
22343 if (newloc == NULL)
22344 return;
22345 }
22346 else
22347 {
22348 decl = NULL_TREE;
22349 newloc = NULL;
22350 }
22351
22352 /* If there were no real insns between note we processed last time
22353 and this note, use the label we emitted last time. Otherwise
22354 create a new label and emit it. */
22355 if (last_label == NULL)
22356 {
22357 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22358 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22359 loclabel_num++;
22360 last_label = ggc_strdup (loclabel);
22361 /* See if loclabel might be equal to .Ltext0. If yes,
22362 bump first_loclabel_num_not_at_text_label. */
22363 if (!have_multiple_function_sections
22364 && in_first_function_p
22365 && maybe_at_text_label_p)
22366 {
22367 static rtx_insn *last_start;
22368 rtx_insn *insn;
22369 for (insn = loc_note; insn; insn = previous_insn (insn))
22370 if (insn == last_start)
22371 break;
22372 else if (!NONDEBUG_INSN_P (insn))
22373 continue;
22374 else
22375 {
22376 rtx body = PATTERN (insn);
22377 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22378 continue;
22379 /* Inline asm could occupy zero bytes. */
22380 else if (GET_CODE (body) == ASM_INPUT
22381 || asm_noperands (body) >= 0)
22382 continue;
22383 #ifdef HAVE_attr_length
22384 else if (get_attr_min_length (insn) == 0)
22385 continue;
22386 #endif
22387 else
22388 {
22389 /* Assume insn has non-zero length. */
22390 maybe_at_text_label_p = false;
22391 break;
22392 }
22393 }
22394 if (maybe_at_text_label_p)
22395 {
22396 last_start = loc_note;
22397 first_loclabel_num_not_at_text_label = loclabel_num;
22398 }
22399 }
22400 }
22401
22402 if (!var_loc_p)
22403 {
22404 struct call_arg_loc_node *ca_loc
22405 = ggc_cleared_alloc<call_arg_loc_node> ();
22406 rtx_insn *prev = prev_real_insn (loc_note);
22407 rtx x;
22408 ca_loc->call_arg_loc_note = loc_note;
22409 ca_loc->next = NULL;
22410 ca_loc->label = last_label;
22411 gcc_assert (prev
22412 && (CALL_P (prev)
22413 || (NONJUMP_INSN_P (prev)
22414 && GET_CODE (PATTERN (prev)) == SEQUENCE
22415 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22416 if (!CALL_P (prev))
22417 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22418 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22419 x = get_call_rtx_from (PATTERN (prev));
22420 if (x)
22421 {
22422 x = XEXP (XEXP (x, 0), 0);
22423 if (GET_CODE (x) == SYMBOL_REF
22424 && SYMBOL_REF_DECL (x)
22425 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22426 ca_loc->symbol_ref = x;
22427 }
22428 ca_loc->block = insn_scope (prev);
22429 if (call_arg_locations)
22430 call_arg_loc_last->next = ca_loc;
22431 else
22432 call_arg_locations = ca_loc;
22433 call_arg_loc_last = ca_loc;
22434 }
22435 else if (!NOTE_DURING_CALL_P (loc_note))
22436 newloc->label = last_label;
22437 else
22438 {
22439 if (!last_postcall_label)
22440 {
22441 sprintf (loclabel, "%s-1", last_label);
22442 last_postcall_label = ggc_strdup (loclabel);
22443 }
22444 newloc->label = last_postcall_label;
22445 }
22446
22447 last_var_location_insn = next_real;
22448 last_in_cold_section_p = in_cold_section_p;
22449 }
22450
22451 /* Note in one location list that text section has changed. */
22452
22453 int
22454 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22455 {
22456 var_loc_list *list = *slot;
22457 if (list->first)
22458 list->last_before_switch
22459 = list->last->next ? list->last->next : list->last;
22460 return 1;
22461 }
22462
22463 /* Note in all location lists that text section has changed. */
22464
22465 static void
22466 var_location_switch_text_section (void)
22467 {
22468 if (decl_loc_table == NULL)
22469 return;
22470
22471 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22472 }
22473
22474 /* Create a new line number table. */
22475
22476 static dw_line_info_table *
22477 new_line_info_table (void)
22478 {
22479 dw_line_info_table *table;
22480
22481 table = ggc_cleared_alloc<dw_line_info_table> ();
22482 table->file_num = 1;
22483 table->line_num = 1;
22484 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22485
22486 return table;
22487 }
22488
22489 /* Lookup the "current" table into which we emit line info, so
22490 that we don't have to do it for every source line. */
22491
22492 static void
22493 set_cur_line_info_table (section *sec)
22494 {
22495 dw_line_info_table *table;
22496
22497 if (sec == text_section)
22498 table = text_section_line_info;
22499 else if (sec == cold_text_section)
22500 {
22501 table = cold_text_section_line_info;
22502 if (!table)
22503 {
22504 cold_text_section_line_info = table = new_line_info_table ();
22505 table->end_label = cold_end_label;
22506 }
22507 }
22508 else
22509 {
22510 const char *end_label;
22511
22512 if (flag_reorder_blocks_and_partition)
22513 {
22514 if (in_cold_section_p)
22515 end_label = crtl->subsections.cold_section_end_label;
22516 else
22517 end_label = crtl->subsections.hot_section_end_label;
22518 }
22519 else
22520 {
22521 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22522 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22523 current_function_funcdef_no);
22524 end_label = ggc_strdup (label);
22525 }
22526
22527 table = new_line_info_table ();
22528 table->end_label = end_label;
22529
22530 vec_safe_push (separate_line_info, table);
22531 }
22532
22533 if (DWARF2_ASM_LINE_DEBUG_INFO)
22534 table->is_stmt = (cur_line_info_table
22535 ? cur_line_info_table->is_stmt
22536 : DWARF_LINE_DEFAULT_IS_STMT_START);
22537 cur_line_info_table = table;
22538 }
22539
22540
22541 /* We need to reset the locations at the beginning of each
22542 function. We can't do this in the end_function hook, because the
22543 declarations that use the locations won't have been output when
22544 that hook is called. Also compute have_multiple_function_sections here. */
22545
22546 static void
22547 dwarf2out_begin_function (tree fun)
22548 {
22549 section *sec = function_section (fun);
22550
22551 if (sec != text_section)
22552 have_multiple_function_sections = true;
22553
22554 if (flag_reorder_blocks_and_partition && !cold_text_section)
22555 {
22556 gcc_assert (current_function_decl == fun);
22557 cold_text_section = unlikely_text_section ();
22558 switch_to_section (cold_text_section);
22559 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22560 switch_to_section (sec);
22561 }
22562
22563 dwarf2out_note_section_used ();
22564 call_site_count = 0;
22565 tail_call_site_count = 0;
22566
22567 set_cur_line_info_table (sec);
22568 }
22569
22570 /* Helper function of dwarf2out_end_function, called only after emitting
22571 the very first function into assembly. Check if some .debug_loc range
22572 might end with a .LVL* label that could be equal to .Ltext0.
22573 In that case we must force using absolute addresses in .debug_loc ranges,
22574 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22575 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22576 list terminator.
22577 Set have_multiple_function_sections to true in that case and
22578 terminate htab traversal. */
22579
22580 int
22581 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22582 {
22583 var_loc_list *entry = *slot;
22584 struct var_loc_node *node;
22585
22586 node = entry->first;
22587 if (node && node->next && node->next->label)
22588 {
22589 unsigned int i;
22590 const char *label = node->next->label;
22591 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22592
22593 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22594 {
22595 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22596 if (strcmp (label, loclabel) == 0)
22597 {
22598 have_multiple_function_sections = true;
22599 return 0;
22600 }
22601 }
22602 }
22603 return 1;
22604 }
22605
22606 /* Hook called after emitting a function into assembly.
22607 This does something only for the very first function emitted. */
22608
22609 static void
22610 dwarf2out_end_function (unsigned int)
22611 {
22612 if (in_first_function_p
22613 && !have_multiple_function_sections
22614 && first_loclabel_num_not_at_text_label
22615 && decl_loc_table)
22616 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22617 in_first_function_p = false;
22618 maybe_at_text_label_p = false;
22619 }
22620
22621 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22622 front-ends register a translation unit even before dwarf2out_init is
22623 called. */
22624 static tree main_translation_unit = NULL_TREE;
22625
22626 /* Hook called by front-ends after they built their main translation unit.
22627 Associate comp_unit_die to UNIT. */
22628
22629 static void
22630 dwarf2out_register_main_translation_unit (tree unit)
22631 {
22632 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22633 && main_translation_unit == NULL_TREE);
22634 main_translation_unit = unit;
22635 /* If dwarf2out_init has not been called yet, it will perform the association
22636 itself looking at main_translation_unit. */
22637 if (decl_die_table != NULL)
22638 equate_decl_number_to_die (unit, comp_unit_die ());
22639 }
22640
22641 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22642
22643 static void
22644 push_dw_line_info_entry (dw_line_info_table *table,
22645 enum dw_line_info_opcode opcode, unsigned int val)
22646 {
22647 dw_line_info_entry e;
22648 e.opcode = opcode;
22649 e.val = val;
22650 vec_safe_push (table->entries, e);
22651 }
22652
22653 /* Output a label to mark the beginning of a source code line entry
22654 and record information relating to this source line, in
22655 'line_info_table' for later output of the .debug_line section. */
22656 /* ??? The discriminator parameter ought to be unsigned. */
22657
22658 static void
22659 dwarf2out_source_line (unsigned int line, const char *filename,
22660 int discriminator, bool is_stmt)
22661 {
22662 unsigned int file_num;
22663 dw_line_info_table *table;
22664
22665 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22666 return;
22667
22668 /* The discriminator column was added in dwarf4. Simplify the below
22669 by simply removing it if we're not supposed to output it. */
22670 if (dwarf_version < 4 && dwarf_strict)
22671 discriminator = 0;
22672
22673 table = cur_line_info_table;
22674 file_num = maybe_emit_file (lookup_filename (filename));
22675
22676 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22677 the debugger has used the second (possibly duplicate) line number
22678 at the beginning of the function to mark the end of the prologue.
22679 We could eliminate any other duplicates within the function. For
22680 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22681 that second line number entry. */
22682 /* Recall that this end-of-prologue indication is *not* the same thing
22683 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22684 to which the hook corresponds, follows the last insn that was
22685 emitted by gen_prologue. What we need is to precede the first insn
22686 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22687 insn that corresponds to something the user wrote. These may be
22688 very different locations once scheduling is enabled. */
22689
22690 if (0 && file_num == table->file_num
22691 && line == table->line_num
22692 && discriminator == table->discrim_num
22693 && is_stmt == table->is_stmt)
22694 return;
22695
22696 switch_to_section (current_function_section ());
22697
22698 /* If requested, emit something human-readable. */
22699 if (flag_debug_asm)
22700 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22701
22702 if (DWARF2_ASM_LINE_DEBUG_INFO)
22703 {
22704 /* Emit the .loc directive understood by GNU as. */
22705 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22706 file_num, line, is_stmt, discriminator */
22707 fputs ("\t.loc ", asm_out_file);
22708 fprint_ul (asm_out_file, file_num);
22709 putc (' ', asm_out_file);
22710 fprint_ul (asm_out_file, line);
22711 putc (' ', asm_out_file);
22712 putc ('0', asm_out_file);
22713
22714 if (is_stmt != table->is_stmt)
22715 {
22716 fputs (" is_stmt ", asm_out_file);
22717 putc (is_stmt ? '1' : '0', asm_out_file);
22718 }
22719 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22720 {
22721 gcc_assert (discriminator > 0);
22722 fputs (" discriminator ", asm_out_file);
22723 fprint_ul (asm_out_file, (unsigned long) discriminator);
22724 }
22725 putc ('\n', asm_out_file);
22726 }
22727 else
22728 {
22729 unsigned int label_num = ++line_info_label_num;
22730
22731 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22732
22733 push_dw_line_info_entry (table, LI_set_address, label_num);
22734 if (file_num != table->file_num)
22735 push_dw_line_info_entry (table, LI_set_file, file_num);
22736 if (discriminator != table->discrim_num)
22737 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22738 if (is_stmt != table->is_stmt)
22739 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22740 push_dw_line_info_entry (table, LI_set_line, line);
22741 }
22742
22743 table->file_num = file_num;
22744 table->line_num = line;
22745 table->discrim_num = discriminator;
22746 table->is_stmt = is_stmt;
22747 table->in_use = true;
22748 }
22749
22750 /* Record the beginning of a new source file. */
22751
22752 static void
22753 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22754 {
22755 if (flag_eliminate_dwarf2_dups)
22756 {
22757 /* Record the beginning of the file for break_out_includes. */
22758 dw_die_ref bincl_die;
22759
22760 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22761 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22762 }
22763
22764 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22765 {
22766 macinfo_entry e;
22767 e.code = DW_MACINFO_start_file;
22768 e.lineno = lineno;
22769 e.info = ggc_strdup (filename);
22770 vec_safe_push (macinfo_table, e);
22771 }
22772 }
22773
22774 /* Record the end of a source file. */
22775
22776 static void
22777 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22778 {
22779 if (flag_eliminate_dwarf2_dups)
22780 /* Record the end of the file for break_out_includes. */
22781 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22782
22783 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22784 {
22785 macinfo_entry e;
22786 e.code = DW_MACINFO_end_file;
22787 e.lineno = lineno;
22788 e.info = NULL;
22789 vec_safe_push (macinfo_table, e);
22790 }
22791 }
22792
22793 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22794 the tail part of the directive line, i.e. the part which is past the
22795 initial whitespace, #, whitespace, directive-name, whitespace part. */
22796
22797 static void
22798 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22799 const char *buffer ATTRIBUTE_UNUSED)
22800 {
22801 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22802 {
22803 macinfo_entry e;
22804 /* Insert a dummy first entry to be able to optimize the whole
22805 predefined macro block using DW_MACRO_GNU_transparent_include. */
22806 if (macinfo_table->is_empty () && lineno <= 1)
22807 {
22808 e.code = 0;
22809 e.lineno = 0;
22810 e.info = NULL;
22811 vec_safe_push (macinfo_table, e);
22812 }
22813 e.code = DW_MACINFO_define;
22814 e.lineno = lineno;
22815 e.info = ggc_strdup (buffer);
22816 vec_safe_push (macinfo_table, e);
22817 }
22818 }
22819
22820 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22821 the tail part of the directive line, i.e. the part which is past the
22822 initial whitespace, #, whitespace, directive-name, whitespace part. */
22823
22824 static void
22825 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22826 const char *buffer ATTRIBUTE_UNUSED)
22827 {
22828 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22829 {
22830 macinfo_entry e;
22831 /* Insert a dummy first entry to be able to optimize the whole
22832 predefined macro block using DW_MACRO_GNU_transparent_include. */
22833 if (macinfo_table->is_empty () && lineno <= 1)
22834 {
22835 e.code = 0;
22836 e.lineno = 0;
22837 e.info = NULL;
22838 vec_safe_push (macinfo_table, e);
22839 }
22840 e.code = DW_MACINFO_undef;
22841 e.lineno = lineno;
22842 e.info = ggc_strdup (buffer);
22843 vec_safe_push (macinfo_table, e);
22844 }
22845 }
22846
22847 /* Helpers to manipulate hash table of CUs. */
22848
22849 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22850 {
22851 static inline hashval_t hash (const macinfo_entry *);
22852 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22853 };
22854
22855 inline hashval_t
22856 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22857 {
22858 return htab_hash_string (entry->info);
22859 }
22860
22861 inline bool
22862 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22863 const macinfo_entry *entry2)
22864 {
22865 return !strcmp (entry1->info, entry2->info);
22866 }
22867
22868 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22869
22870 /* Output a single .debug_macinfo entry. */
22871
22872 static void
22873 output_macinfo_op (macinfo_entry *ref)
22874 {
22875 int file_num;
22876 size_t len;
22877 struct indirect_string_node *node;
22878 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22879 struct dwarf_file_data *fd;
22880
22881 switch (ref->code)
22882 {
22883 case DW_MACINFO_start_file:
22884 fd = lookup_filename (ref->info);
22885 file_num = maybe_emit_file (fd);
22886 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22887 dw2_asm_output_data_uleb128 (ref->lineno,
22888 "Included from line number %lu",
22889 (unsigned long) ref->lineno);
22890 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22891 break;
22892 case DW_MACINFO_end_file:
22893 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22894 break;
22895 case DW_MACINFO_define:
22896 case DW_MACINFO_undef:
22897 len = strlen (ref->info) + 1;
22898 if (!dwarf_strict
22899 && len > DWARF_OFFSET_SIZE
22900 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22901 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22902 {
22903 ref->code = ref->code == DW_MACINFO_define
22904 ? DW_MACRO_GNU_define_indirect
22905 : DW_MACRO_GNU_undef_indirect;
22906 output_macinfo_op (ref);
22907 return;
22908 }
22909 dw2_asm_output_data (1, ref->code,
22910 ref->code == DW_MACINFO_define
22911 ? "Define macro" : "Undefine macro");
22912 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22913 (unsigned long) ref->lineno);
22914 dw2_asm_output_nstring (ref->info, -1, "The macro");
22915 break;
22916 case DW_MACRO_GNU_define_indirect:
22917 case DW_MACRO_GNU_undef_indirect:
22918 node = find_AT_string (ref->info);
22919 gcc_assert (node
22920 && ((node->form == DW_FORM_strp)
22921 || (node->form == DW_FORM_GNU_str_index)));
22922 dw2_asm_output_data (1, ref->code,
22923 ref->code == DW_MACRO_GNU_define_indirect
22924 ? "Define macro indirect"
22925 : "Undefine macro indirect");
22926 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22927 (unsigned long) ref->lineno);
22928 if (node->form == DW_FORM_strp)
22929 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22930 debug_str_section, "The macro: \"%s\"",
22931 ref->info);
22932 else
22933 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22934 ref->info);
22935 break;
22936 case DW_MACRO_GNU_transparent_include:
22937 dw2_asm_output_data (1, ref->code, "Transparent include");
22938 ASM_GENERATE_INTERNAL_LABEL (label,
22939 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22940 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22941 break;
22942 default:
22943 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22944 ASM_COMMENT_START, (unsigned long) ref->code);
22945 break;
22946 }
22947 }
22948
22949 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22950 other compilation unit .debug_macinfo sections. IDX is the first
22951 index of a define/undef, return the number of ops that should be
22952 emitted in a comdat .debug_macinfo section and emit
22953 a DW_MACRO_GNU_transparent_include entry referencing it.
22954 If the define/undef entry should be emitted normally, return 0. */
22955
22956 static unsigned
22957 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22958 macinfo_hash_type **macinfo_htab)
22959 {
22960 macinfo_entry *first, *second, *cur, *inc;
22961 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22962 unsigned char checksum[16];
22963 struct md5_ctx ctx;
22964 char *grp_name, *tail;
22965 const char *base;
22966 unsigned int i, count, encoded_filename_len, linebuf_len;
22967 macinfo_entry **slot;
22968
22969 first = &(*macinfo_table)[idx];
22970 second = &(*macinfo_table)[idx + 1];
22971
22972 /* Optimize only if there are at least two consecutive define/undef ops,
22973 and either all of them are before first DW_MACINFO_start_file
22974 with lineno {0,1} (i.e. predefined macro block), or all of them are
22975 in some included header file. */
22976 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22977 return 0;
22978 if (vec_safe_is_empty (files))
22979 {
22980 if (first->lineno > 1 || second->lineno > 1)
22981 return 0;
22982 }
22983 else if (first->lineno == 0)
22984 return 0;
22985
22986 /* Find the last define/undef entry that can be grouped together
22987 with first and at the same time compute md5 checksum of their
22988 codes, linenumbers and strings. */
22989 md5_init_ctx (&ctx);
22990 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22991 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22992 break;
22993 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22994 break;
22995 else
22996 {
22997 unsigned char code = cur->code;
22998 md5_process_bytes (&code, 1, &ctx);
22999 checksum_uleb128 (cur->lineno, &ctx);
23000 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
23001 }
23002 md5_finish_ctx (&ctx, checksum);
23003 count = i - idx;
23004
23005 /* From the containing include filename (if any) pick up just
23006 usable characters from its basename. */
23007 if (vec_safe_is_empty (files))
23008 base = "";
23009 else
23010 base = lbasename (files->last ().info);
23011 for (encoded_filename_len = 0, i = 0; base[i]; i++)
23012 if (ISIDNUM (base[i]) || base[i] == '.')
23013 encoded_filename_len++;
23014 /* Count . at the end. */
23015 if (encoded_filename_len)
23016 encoded_filename_len++;
23017
23018 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
23019 linebuf_len = strlen (linebuf);
23020
23021 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
23022 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
23023 + 16 * 2 + 1);
23024 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
23025 tail = grp_name + 4;
23026 if (encoded_filename_len)
23027 {
23028 for (i = 0; base[i]; i++)
23029 if (ISIDNUM (base[i]) || base[i] == '.')
23030 *tail++ = base[i];
23031 *tail++ = '.';
23032 }
23033 memcpy (tail, linebuf, linebuf_len);
23034 tail += linebuf_len;
23035 *tail++ = '.';
23036 for (i = 0; i < 16; i++)
23037 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23038
23039 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23040 in the empty vector entry before the first define/undef. */
23041 inc = &(*macinfo_table)[idx - 1];
23042 inc->code = DW_MACRO_GNU_transparent_include;
23043 inc->lineno = 0;
23044 inc->info = ggc_strdup (grp_name);
23045 if (!*macinfo_htab)
23046 *macinfo_htab = new macinfo_hash_type (10);
23047 /* Avoid emitting duplicates. */
23048 slot = (*macinfo_htab)->find_slot (inc, INSERT);
23049 if (*slot != NULL)
23050 {
23051 inc->code = 0;
23052 inc->info = NULL;
23053 /* If such an entry has been used before, just emit
23054 a DW_MACRO_GNU_transparent_include op. */
23055 inc = *slot;
23056 output_macinfo_op (inc);
23057 /* And clear all macinfo_entry in the range to avoid emitting them
23058 in the second pass. */
23059 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23060 {
23061 cur->code = 0;
23062 cur->info = NULL;
23063 }
23064 }
23065 else
23066 {
23067 *slot = inc;
23068 inc->lineno = (*macinfo_htab)->elements ();
23069 output_macinfo_op (inc);
23070 }
23071 return count;
23072 }
23073
23074 /* Save any strings needed by the macinfo table in the debug str
23075 table. All strings must be collected into the table by the time
23076 index_string is called. */
23077
23078 static void
23079 save_macinfo_strings (void)
23080 {
23081 unsigned len;
23082 unsigned i;
23083 macinfo_entry *ref;
23084
23085 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23086 {
23087 switch (ref->code)
23088 {
23089 /* Match the logic in output_macinfo_op to decide on
23090 indirect strings. */
23091 case DW_MACINFO_define:
23092 case DW_MACINFO_undef:
23093 len = strlen (ref->info) + 1;
23094 if (!dwarf_strict
23095 && len > DWARF_OFFSET_SIZE
23096 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23097 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23098 set_indirect_string (find_AT_string (ref->info));
23099 break;
23100 case DW_MACRO_GNU_define_indirect:
23101 case DW_MACRO_GNU_undef_indirect:
23102 set_indirect_string (find_AT_string (ref->info));
23103 break;
23104 default:
23105 break;
23106 }
23107 }
23108 }
23109
23110 /* Output macinfo section(s). */
23111
23112 static void
23113 output_macinfo (void)
23114 {
23115 unsigned i;
23116 unsigned long length = vec_safe_length (macinfo_table);
23117 macinfo_entry *ref;
23118 vec<macinfo_entry, va_gc> *files = NULL;
23119 macinfo_hash_type *macinfo_htab = NULL;
23120
23121 if (! length)
23122 return;
23123
23124 /* output_macinfo* uses these interchangeably. */
23125 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23126 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23127 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23128 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23129
23130 /* For .debug_macro emit the section header. */
23131 if (!dwarf_strict)
23132 {
23133 dw2_asm_output_data (2, 4, "DWARF macro version number");
23134 if (DWARF_OFFSET_SIZE == 8)
23135 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23136 else
23137 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23138 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23139 (!dwarf_split_debug_info ? debug_line_section_label
23140 : debug_skeleton_line_section_label),
23141 debug_line_section, NULL);
23142 }
23143
23144 /* In the first loop, it emits the primary .debug_macinfo section
23145 and after each emitted op the macinfo_entry is cleared.
23146 If a longer range of define/undef ops can be optimized using
23147 DW_MACRO_GNU_transparent_include, the
23148 DW_MACRO_GNU_transparent_include op is emitted and kept in
23149 the vector before the first define/undef in the range and the
23150 whole range of define/undef ops is not emitted and kept. */
23151 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23152 {
23153 switch (ref->code)
23154 {
23155 case DW_MACINFO_start_file:
23156 vec_safe_push (files, *ref);
23157 break;
23158 case DW_MACINFO_end_file:
23159 if (!vec_safe_is_empty (files))
23160 files->pop ();
23161 break;
23162 case DW_MACINFO_define:
23163 case DW_MACINFO_undef:
23164 if (!dwarf_strict
23165 && HAVE_COMDAT_GROUP
23166 && vec_safe_length (files) != 1
23167 && i > 0
23168 && i + 1 < length
23169 && (*macinfo_table)[i - 1].code == 0)
23170 {
23171 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23172 if (count)
23173 {
23174 i += count - 1;
23175 continue;
23176 }
23177 }
23178 break;
23179 case 0:
23180 /* A dummy entry may be inserted at the beginning to be able
23181 to optimize the whole block of predefined macros. */
23182 if (i == 0)
23183 continue;
23184 default:
23185 break;
23186 }
23187 output_macinfo_op (ref);
23188 ref->info = NULL;
23189 ref->code = 0;
23190 }
23191
23192 if (!macinfo_htab)
23193 return;
23194
23195 delete macinfo_htab;
23196 macinfo_htab = NULL;
23197
23198 /* If any DW_MACRO_GNU_transparent_include were used, on those
23199 DW_MACRO_GNU_transparent_include entries terminate the
23200 current chain and switch to a new comdat .debug_macinfo
23201 section and emit the define/undef entries within it. */
23202 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23203 switch (ref->code)
23204 {
23205 case 0:
23206 continue;
23207 case DW_MACRO_GNU_transparent_include:
23208 {
23209 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23210 tree comdat_key = get_identifier (ref->info);
23211 /* Terminate the previous .debug_macinfo section. */
23212 dw2_asm_output_data (1, 0, "End compilation unit");
23213 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23214 SECTION_DEBUG
23215 | SECTION_LINKONCE,
23216 comdat_key);
23217 ASM_GENERATE_INTERNAL_LABEL (label,
23218 DEBUG_MACRO_SECTION_LABEL,
23219 ref->lineno);
23220 ASM_OUTPUT_LABEL (asm_out_file, label);
23221 ref->code = 0;
23222 ref->info = NULL;
23223 dw2_asm_output_data (2, 4, "DWARF macro version number");
23224 if (DWARF_OFFSET_SIZE == 8)
23225 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23226 else
23227 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23228 }
23229 break;
23230 case DW_MACINFO_define:
23231 case DW_MACINFO_undef:
23232 output_macinfo_op (ref);
23233 ref->code = 0;
23234 ref->info = NULL;
23235 break;
23236 default:
23237 gcc_unreachable ();
23238 }
23239 }
23240
23241 /* Set up for Dwarf output at the start of compilation. */
23242
23243 static void
23244 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23245 {
23246 /* This option is currently broken, see (PR53118 and PR46102). */
23247 if (flag_eliminate_dwarf2_dups
23248 && strstr (lang_hooks.name, "C++"))
23249 {
23250 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23251 flag_eliminate_dwarf2_dups = 0;
23252 }
23253
23254 /* Allocate the file_table. */
23255 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23256
23257 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23258 /* Allocate the decl_die_table. */
23259 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23260
23261 /* Allocate the decl_loc_table. */
23262 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23263
23264 /* Allocate the cached_dw_loc_list_table. */
23265 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23266
23267 /* Allocate the initial hunk of the decl_scope_table. */
23268 vec_alloc (decl_scope_table, 256);
23269
23270 /* Allocate the initial hunk of the abbrev_die_table. */
23271 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23272 (ABBREV_DIE_TABLE_INCREMENT);
23273 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23274 /* Zero-th entry is allocated, but unused. */
23275 abbrev_die_table_in_use = 1;
23276
23277 /* Allocate the pubtypes and pubnames vectors. */
23278 vec_alloc (pubname_table, 32);
23279 vec_alloc (pubtype_table, 32);
23280
23281 vec_alloc (incomplete_types, 64);
23282
23283 vec_alloc (used_rtx_array, 32);
23284
23285 if (!dwarf_split_debug_info)
23286 {
23287 debug_info_section = get_section (DEBUG_INFO_SECTION,
23288 SECTION_DEBUG, NULL);
23289 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23290 SECTION_DEBUG, NULL);
23291 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23292 SECTION_DEBUG, NULL);
23293 }
23294 else
23295 {
23296 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23297 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23298 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23299 SECTION_DEBUG | SECTION_EXCLUDE,
23300 NULL);
23301 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23302 SECTION_DEBUG, NULL);
23303 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23304 SECTION_DEBUG, NULL);
23305 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23306 SECTION_DEBUG, NULL);
23307 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23308 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23309
23310 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23311 the main .o, but the skeleton_line goes into the split off dwo. */
23312 debug_skeleton_line_section
23313 = get_section (DEBUG_DWO_LINE_SECTION,
23314 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23315 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23316 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23317 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23318 SECTION_DEBUG | SECTION_EXCLUDE,
23319 NULL);
23320 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23321 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23322 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23323 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23324 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23325 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23326 }
23327 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23328 SECTION_DEBUG, NULL);
23329 debug_macinfo_section = get_section (dwarf_strict
23330 ? DEBUG_MACINFO_SECTION
23331 : DEBUG_MACRO_SECTION,
23332 DEBUG_MACRO_SECTION_FLAGS, NULL);
23333 debug_line_section = get_section (DEBUG_LINE_SECTION,
23334 SECTION_DEBUG, NULL);
23335 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23336 SECTION_DEBUG, NULL);
23337 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23338 SECTION_DEBUG, NULL);
23339 debug_str_section = get_section (DEBUG_STR_SECTION,
23340 DEBUG_STR_SECTION_FLAGS, NULL);
23341 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23342 SECTION_DEBUG, NULL);
23343 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23344 SECTION_DEBUG, NULL);
23345
23346 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23347 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23348 DEBUG_ABBREV_SECTION_LABEL, 0);
23349 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23350 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23351 COLD_TEXT_SECTION_LABEL, 0);
23352 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23353
23354 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23355 DEBUG_INFO_SECTION_LABEL, 0);
23356 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23357 DEBUG_LINE_SECTION_LABEL, 0);
23358 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23359 DEBUG_RANGES_SECTION_LABEL, 0);
23360 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23361 DEBUG_ADDR_SECTION_LABEL, 0);
23362 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23363 dwarf_strict
23364 ? DEBUG_MACINFO_SECTION_LABEL
23365 : DEBUG_MACRO_SECTION_LABEL, 0);
23366 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23367
23368 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23369 vec_alloc (macinfo_table, 64);
23370
23371 switch_to_section (text_section);
23372 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23373 #endif
23374
23375 /* Make sure the line number table for .text always exists. */
23376 text_section_line_info = new_line_info_table ();
23377 text_section_line_info->end_label = text_end_label;
23378
23379 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23380 cur_line_info_table = text_section_line_info;
23381 #endif
23382
23383 /* If front-ends already registered a main translation unit but we were not
23384 ready to perform the association, do this now. */
23385 if (main_translation_unit != NULL_TREE)
23386 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23387 }
23388
23389 /* Called before compile () starts outputtting functions, variables
23390 and toplevel asms into assembly. */
23391
23392 static void
23393 dwarf2out_assembly_start (void)
23394 {
23395 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23396 && dwarf2out_do_cfi_asm ()
23397 && (!(flag_unwind_tables || flag_exceptions)
23398 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23399 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23400 }
23401
23402 /* A helper function for dwarf2out_finish called through
23403 htab_traverse. Assign a string its index. All strings must be
23404 collected into the table by the time index_string is called,
23405 because the indexing code relies on htab_traverse to traverse nodes
23406 in the same order for each run. */
23407
23408 int
23409 index_string (indirect_string_node **h, unsigned int *index)
23410 {
23411 indirect_string_node *node = *h;
23412
23413 find_string_form (node);
23414 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23415 {
23416 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23417 node->index = *index;
23418 *index += 1;
23419 }
23420 return 1;
23421 }
23422
23423 /* A helper function for output_indirect_strings called through
23424 htab_traverse. Output the offset to a string and update the
23425 current offset. */
23426
23427 int
23428 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23429 {
23430 indirect_string_node *node = *h;
23431
23432 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23433 {
23434 /* Assert that this node has been assigned an index. */
23435 gcc_assert (node->index != NO_INDEX_ASSIGNED
23436 && node->index != NOT_INDEXED);
23437 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23438 "indexed string 0x%x: %s", node->index, node->str);
23439 *offset += strlen (node->str) + 1;
23440 }
23441 return 1;
23442 }
23443
23444 /* A helper function for dwarf2out_finish called through
23445 htab_traverse. Output the indexed string. */
23446
23447 int
23448 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23449 {
23450 struct indirect_string_node *node = *h;
23451
23452 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23453 {
23454 /* Assert that the strings are output in the same order as their
23455 indexes were assigned. */
23456 gcc_assert (*cur_idx == node->index);
23457 assemble_string (node->str, strlen (node->str) + 1);
23458 *cur_idx += 1;
23459 }
23460 return 1;
23461 }
23462
23463 /* A helper function for dwarf2out_finish called through
23464 htab_traverse. Emit one queued .debug_str string. */
23465
23466 int
23467 output_indirect_string (indirect_string_node **h, void *)
23468 {
23469 struct indirect_string_node *node = *h;
23470
23471 node->form = find_string_form (node);
23472 if (node->form == DW_FORM_strp && node->refcount > 0)
23473 {
23474 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23475 assemble_string (node->str, strlen (node->str) + 1);
23476 }
23477
23478 return 1;
23479 }
23480
23481 /* Output the indexed string table. */
23482
23483 static void
23484 output_indirect_strings (void)
23485 {
23486 switch_to_section (debug_str_section);
23487 if (!dwarf_split_debug_info)
23488 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23489 else
23490 {
23491 unsigned int offset = 0;
23492 unsigned int cur_idx = 0;
23493
23494 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23495
23496 switch_to_section (debug_str_offsets_section);
23497 debug_str_hash->traverse_noresize
23498 <unsigned int *, output_index_string_offset> (&offset);
23499 switch_to_section (debug_str_dwo_section);
23500 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23501 (&cur_idx);
23502 }
23503 }
23504
23505 /* Callback for htab_traverse to assign an index to an entry in the
23506 table, and to write that entry to the .debug_addr section. */
23507
23508 int
23509 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23510 {
23511 addr_table_entry *entry = *slot;
23512
23513 if (entry->refcount == 0)
23514 {
23515 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23516 || entry->index == NOT_INDEXED);
23517 return 1;
23518 }
23519
23520 gcc_assert (entry->index == *cur_index);
23521 (*cur_index)++;
23522
23523 switch (entry->kind)
23524 {
23525 case ate_kind_rtx:
23526 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23527 "0x%x", entry->index);
23528 break;
23529 case ate_kind_rtx_dtprel:
23530 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23531 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23532 DWARF2_ADDR_SIZE,
23533 entry->addr.rtl);
23534 fputc ('\n', asm_out_file);
23535 break;
23536 case ate_kind_label:
23537 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23538 "0x%x", entry->index);
23539 break;
23540 default:
23541 gcc_unreachable ();
23542 }
23543 return 1;
23544 }
23545
23546 /* Produce the .debug_addr section. */
23547
23548 static void
23549 output_addr_table (void)
23550 {
23551 unsigned int index = 0;
23552 if (addr_index_table == NULL || addr_index_table->size () == 0)
23553 return;
23554
23555 switch_to_section (debug_addr_section);
23556 addr_index_table
23557 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23558 }
23559
23560 #if ENABLE_ASSERT_CHECKING
23561 /* Verify that all marks are clear. */
23562
23563 static void
23564 verify_marks_clear (dw_die_ref die)
23565 {
23566 dw_die_ref c;
23567
23568 gcc_assert (! die->die_mark);
23569 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23570 }
23571 #endif /* ENABLE_ASSERT_CHECKING */
23572
23573 /* Clear the marks for a die and its children.
23574 Be cool if the mark isn't set. */
23575
23576 static void
23577 prune_unmark_dies (dw_die_ref die)
23578 {
23579 dw_die_ref c;
23580
23581 if (die->die_mark)
23582 die->die_mark = 0;
23583 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23584 }
23585
23586 /* Given DIE that we're marking as used, find any other dies
23587 it references as attributes and mark them as used. */
23588
23589 static void
23590 prune_unused_types_walk_attribs (dw_die_ref die)
23591 {
23592 dw_attr_node *a;
23593 unsigned ix;
23594
23595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23596 {
23597 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23598 {
23599 /* A reference to another DIE.
23600 Make sure that it will get emitted.
23601 If it was broken out into a comdat group, don't follow it. */
23602 if (! AT_ref (a)->comdat_type_p
23603 || a->dw_attr == DW_AT_specification)
23604 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23605 }
23606 /* Set the string's refcount to 0 so that prune_unused_types_mark
23607 accounts properly for it. */
23608 if (AT_class (a) == dw_val_class_str)
23609 a->dw_attr_val.v.val_str->refcount = 0;
23610 }
23611 }
23612
23613 /* Mark the generic parameters and arguments children DIEs of DIE. */
23614
23615 static void
23616 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23617 {
23618 dw_die_ref c;
23619
23620 if (die == NULL || die->die_child == NULL)
23621 return;
23622 c = die->die_child;
23623 do
23624 {
23625 if (is_template_parameter (c))
23626 prune_unused_types_mark (c, 1);
23627 c = c->die_sib;
23628 } while (c && c != die->die_child);
23629 }
23630
23631 /* Mark DIE as being used. If DOKIDS is true, then walk down
23632 to DIE's children. */
23633
23634 static void
23635 prune_unused_types_mark (dw_die_ref die, int dokids)
23636 {
23637 dw_die_ref c;
23638
23639 if (die->die_mark == 0)
23640 {
23641 /* We haven't done this node yet. Mark it as used. */
23642 die->die_mark = 1;
23643 /* If this is the DIE of a generic type instantiation,
23644 mark the children DIEs that describe its generic parms and
23645 args. */
23646 prune_unused_types_mark_generic_parms_dies (die);
23647
23648 /* We also have to mark its parents as used.
23649 (But we don't want to mark our parent's kids due to this,
23650 unless it is a class.) */
23651 if (die->die_parent)
23652 prune_unused_types_mark (die->die_parent,
23653 class_scope_p (die->die_parent));
23654
23655 /* Mark any referenced nodes. */
23656 prune_unused_types_walk_attribs (die);
23657
23658 /* If this node is a specification,
23659 also mark the definition, if it exists. */
23660 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23661 prune_unused_types_mark (die->die_definition, 1);
23662 }
23663
23664 if (dokids && die->die_mark != 2)
23665 {
23666 /* We need to walk the children, but haven't done so yet.
23667 Remember that we've walked the kids. */
23668 die->die_mark = 2;
23669
23670 /* If this is an array type, we need to make sure our
23671 kids get marked, even if they're types. If we're
23672 breaking out types into comdat sections, do this
23673 for all type definitions. */
23674 if (die->die_tag == DW_TAG_array_type
23675 || (use_debug_types
23676 && is_type_die (die) && ! is_declaration_die (die)))
23677 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23678 else
23679 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23680 }
23681 }
23682
23683 /* For local classes, look if any static member functions were emitted
23684 and if so, mark them. */
23685
23686 static void
23687 prune_unused_types_walk_local_classes (dw_die_ref die)
23688 {
23689 dw_die_ref c;
23690
23691 if (die->die_mark == 2)
23692 return;
23693
23694 switch (die->die_tag)
23695 {
23696 case DW_TAG_structure_type:
23697 case DW_TAG_union_type:
23698 case DW_TAG_class_type:
23699 break;
23700
23701 case DW_TAG_subprogram:
23702 if (!get_AT_flag (die, DW_AT_declaration)
23703 || die->die_definition != NULL)
23704 prune_unused_types_mark (die, 1);
23705 return;
23706
23707 default:
23708 return;
23709 }
23710
23711 /* Mark children. */
23712 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23713 }
23714
23715 /* Walk the tree DIE and mark types that we actually use. */
23716
23717 static void
23718 prune_unused_types_walk (dw_die_ref die)
23719 {
23720 dw_die_ref c;
23721
23722 /* Don't do anything if this node is already marked and
23723 children have been marked as well. */
23724 if (die->die_mark == 2)
23725 return;
23726
23727 switch (die->die_tag)
23728 {
23729 case DW_TAG_structure_type:
23730 case DW_TAG_union_type:
23731 case DW_TAG_class_type:
23732 if (die->die_perennial_p)
23733 break;
23734
23735 for (c = die->die_parent; c; c = c->die_parent)
23736 if (c->die_tag == DW_TAG_subprogram)
23737 break;
23738
23739 /* Finding used static member functions inside of classes
23740 is needed just for local classes, because for other classes
23741 static member function DIEs with DW_AT_specification
23742 are emitted outside of the DW_TAG_*_type. If we ever change
23743 it, we'd need to call this even for non-local classes. */
23744 if (c)
23745 prune_unused_types_walk_local_classes (die);
23746
23747 /* It's a type node --- don't mark it. */
23748 return;
23749
23750 case DW_TAG_const_type:
23751 case DW_TAG_packed_type:
23752 case DW_TAG_pointer_type:
23753 case DW_TAG_reference_type:
23754 case DW_TAG_rvalue_reference_type:
23755 case DW_TAG_volatile_type:
23756 case DW_TAG_typedef:
23757 case DW_TAG_array_type:
23758 case DW_TAG_interface_type:
23759 case DW_TAG_friend:
23760 case DW_TAG_variant_part:
23761 case DW_TAG_enumeration_type:
23762 case DW_TAG_subroutine_type:
23763 case DW_TAG_string_type:
23764 case DW_TAG_set_type:
23765 case DW_TAG_subrange_type:
23766 case DW_TAG_ptr_to_member_type:
23767 case DW_TAG_file_type:
23768 if (die->die_perennial_p)
23769 break;
23770
23771 /* It's a type node --- don't mark it. */
23772 return;
23773
23774 default:
23775 /* Mark everything else. */
23776 break;
23777 }
23778
23779 if (die->die_mark == 0)
23780 {
23781 die->die_mark = 1;
23782
23783 /* Now, mark any dies referenced from here. */
23784 prune_unused_types_walk_attribs (die);
23785 }
23786
23787 die->die_mark = 2;
23788
23789 /* Mark children. */
23790 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23791 }
23792
23793 /* Increment the string counts on strings referred to from DIE's
23794 attributes. */
23795
23796 static void
23797 prune_unused_types_update_strings (dw_die_ref die)
23798 {
23799 dw_attr_node *a;
23800 unsigned ix;
23801
23802 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23803 if (AT_class (a) == dw_val_class_str)
23804 {
23805 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23806 s->refcount++;
23807 /* Avoid unnecessarily putting strings that are used less than
23808 twice in the hash table. */
23809 if (s->refcount
23810 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23811 {
23812 indirect_string_node **slot
23813 = debug_str_hash->find_slot_with_hash (s->str,
23814 htab_hash_string (s->str),
23815 INSERT);
23816 gcc_assert (*slot == NULL);
23817 *slot = s;
23818 }
23819 }
23820 }
23821
23822 /* Remove from the tree DIE any dies that aren't marked. */
23823
23824 static void
23825 prune_unused_types_prune (dw_die_ref die)
23826 {
23827 dw_die_ref c;
23828
23829 gcc_assert (die->die_mark);
23830 prune_unused_types_update_strings (die);
23831
23832 if (! die->die_child)
23833 return;
23834
23835 c = die->die_child;
23836 do {
23837 dw_die_ref prev = c;
23838 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23839 if (c == die->die_child)
23840 {
23841 /* No marked children between 'prev' and the end of the list. */
23842 if (prev == c)
23843 /* No marked children at all. */
23844 die->die_child = NULL;
23845 else
23846 {
23847 prev->die_sib = c->die_sib;
23848 die->die_child = prev;
23849 }
23850 return;
23851 }
23852
23853 if (c != prev->die_sib)
23854 prev->die_sib = c;
23855 prune_unused_types_prune (c);
23856 } while (c != die->die_child);
23857 }
23858
23859 /* Remove dies representing declarations that we never use. */
23860
23861 static void
23862 prune_unused_types (void)
23863 {
23864 unsigned int i;
23865 limbo_die_node *node;
23866 comdat_type_node *ctnode;
23867 pubname_entry *pub;
23868 dw_die_ref base_type;
23869
23870 #if ENABLE_ASSERT_CHECKING
23871 /* All the marks should already be clear. */
23872 verify_marks_clear (comp_unit_die ());
23873 for (node = limbo_die_list; node; node = node->next)
23874 verify_marks_clear (node->die);
23875 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23876 verify_marks_clear (ctnode->root_die);
23877 #endif /* ENABLE_ASSERT_CHECKING */
23878
23879 /* Mark types that are used in global variables. */
23880 premark_types_used_by_global_vars ();
23881
23882 /* Set the mark on nodes that are actually used. */
23883 prune_unused_types_walk (comp_unit_die ());
23884 for (node = limbo_die_list; node; node = node->next)
23885 prune_unused_types_walk (node->die);
23886 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23887 {
23888 prune_unused_types_walk (ctnode->root_die);
23889 prune_unused_types_mark (ctnode->type_die, 1);
23890 }
23891
23892 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23893 are unusual in that they are pubnames that are the children of pubtypes.
23894 They should only be marked via their parent DW_TAG_enumeration_type die,
23895 not as roots in themselves. */
23896 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23897 if (pub->die->die_tag != DW_TAG_enumerator)
23898 prune_unused_types_mark (pub->die, 1);
23899 for (i = 0; base_types.iterate (i, &base_type); i++)
23900 prune_unused_types_mark (base_type, 1);
23901
23902 if (debug_str_hash)
23903 debug_str_hash->empty ();
23904 if (skeleton_debug_str_hash)
23905 skeleton_debug_str_hash->empty ();
23906 prune_unused_types_prune (comp_unit_die ());
23907 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23908 {
23909 node = *pnode;
23910 if (!node->die->die_mark)
23911 *pnode = node->next;
23912 else
23913 {
23914 prune_unused_types_prune (node->die);
23915 pnode = &node->next;
23916 }
23917 }
23918 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23919 prune_unused_types_prune (ctnode->root_die);
23920
23921 /* Leave the marks clear. */
23922 prune_unmark_dies (comp_unit_die ());
23923 for (node = limbo_die_list; node; node = node->next)
23924 prune_unmark_dies (node->die);
23925 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23926 prune_unmark_dies (ctnode->root_die);
23927 }
23928
23929 /* Set the parameter to true if there are any relative pathnames in
23930 the file table. */
23931 int
23932 file_table_relative_p (dwarf_file_data **slot, bool *p)
23933 {
23934 struct dwarf_file_data *d = *slot;
23935 if (!IS_ABSOLUTE_PATH (d->filename))
23936 {
23937 *p = true;
23938 return 0;
23939 }
23940 return 1;
23941 }
23942
23943 /* Helpers to manipulate hash table of comdat type units. */
23944
23945 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23946 {
23947 static inline hashval_t hash (const comdat_type_node *);
23948 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23949 };
23950
23951 inline hashval_t
23952 comdat_type_hasher::hash (const comdat_type_node *type_node)
23953 {
23954 hashval_t h;
23955 memcpy (&h, type_node->signature, sizeof (h));
23956 return h;
23957 }
23958
23959 inline bool
23960 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23961 const comdat_type_node *type_node_2)
23962 {
23963 return (! memcmp (type_node_1->signature, type_node_2->signature,
23964 DWARF_TYPE_SIGNATURE_SIZE));
23965 }
23966
23967 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23968 to the location it would have been added, should we know its
23969 DECL_ASSEMBLER_NAME when we added other attributes. This will
23970 probably improve compactness of debug info, removing equivalent
23971 abbrevs, and hide any differences caused by deferring the
23972 computation of the assembler name, triggered by e.g. PCH. */
23973
23974 static inline void
23975 move_linkage_attr (dw_die_ref die)
23976 {
23977 unsigned ix = vec_safe_length (die->die_attr);
23978 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23979
23980 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23981 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23982
23983 while (--ix > 0)
23984 {
23985 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23986
23987 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23988 break;
23989 }
23990
23991 if (ix != vec_safe_length (die->die_attr) - 1)
23992 {
23993 die->die_attr->pop ();
23994 die->die_attr->quick_insert (ix, linkage);
23995 }
23996 }
23997
23998 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23999 referenced from typed stack ops and count how often they are used. */
24000
24001 static void
24002 mark_base_types (dw_loc_descr_ref loc)
24003 {
24004 dw_die_ref base_type = NULL;
24005
24006 for (; loc; loc = loc->dw_loc_next)
24007 {
24008 switch (loc->dw_loc_opc)
24009 {
24010 case DW_OP_GNU_regval_type:
24011 case DW_OP_GNU_deref_type:
24012 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24013 break;
24014 case DW_OP_GNU_convert:
24015 case DW_OP_GNU_reinterpret:
24016 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
24017 continue;
24018 /* FALLTHRU */
24019 case DW_OP_GNU_const_type:
24020 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24021 break;
24022 case DW_OP_GNU_entry_value:
24023 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24024 continue;
24025 default:
24026 continue;
24027 }
24028 gcc_assert (base_type->die_parent == comp_unit_die ());
24029 if (base_type->die_mark)
24030 base_type->die_mark++;
24031 else
24032 {
24033 base_types.safe_push (base_type);
24034 base_type->die_mark = 1;
24035 }
24036 }
24037 }
24038
24039 /* Comparison function for sorting marked base types. */
24040
24041 static int
24042 base_type_cmp (const void *x, const void *y)
24043 {
24044 dw_die_ref dx = *(const dw_die_ref *) x;
24045 dw_die_ref dy = *(const dw_die_ref *) y;
24046 unsigned int byte_size1, byte_size2;
24047 unsigned int encoding1, encoding2;
24048 if (dx->die_mark > dy->die_mark)
24049 return -1;
24050 if (dx->die_mark < dy->die_mark)
24051 return 1;
24052 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24053 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24054 if (byte_size1 < byte_size2)
24055 return 1;
24056 if (byte_size1 > byte_size2)
24057 return -1;
24058 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24059 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24060 if (encoding1 < encoding2)
24061 return 1;
24062 if (encoding1 > encoding2)
24063 return -1;
24064 return 0;
24065 }
24066
24067 /* Move base types marked by mark_base_types as early as possible
24068 in the CU, sorted by decreasing usage count both to make the
24069 uleb128 references as small as possible and to make sure they
24070 will have die_offset already computed by calc_die_sizes when
24071 sizes of typed stack loc ops is computed. */
24072
24073 static void
24074 move_marked_base_types (void)
24075 {
24076 unsigned int i;
24077 dw_die_ref base_type, die, c;
24078
24079 if (base_types.is_empty ())
24080 return;
24081
24082 /* Sort by decreasing usage count, they will be added again in that
24083 order later on. */
24084 base_types.qsort (base_type_cmp);
24085 die = comp_unit_die ();
24086 c = die->die_child;
24087 do
24088 {
24089 dw_die_ref prev = c;
24090 c = c->die_sib;
24091 while (c->die_mark)
24092 {
24093 remove_child_with_prev (c, prev);
24094 /* As base types got marked, there must be at least
24095 one node other than DW_TAG_base_type. */
24096 gcc_assert (c != c->die_sib);
24097 c = c->die_sib;
24098 }
24099 }
24100 while (c != die->die_child);
24101 gcc_assert (die->die_child);
24102 c = die->die_child;
24103 for (i = 0; base_types.iterate (i, &base_type); i++)
24104 {
24105 base_type->die_mark = 0;
24106 base_type->die_sib = c->die_sib;
24107 c->die_sib = base_type;
24108 c = base_type;
24109 }
24110 }
24111
24112 /* Helper function for resolve_addr, attempt to resolve
24113 one CONST_STRING, return true if successful. Similarly verify that
24114 SYMBOL_REFs refer to variables emitted in the current CU. */
24115
24116 static bool
24117 resolve_one_addr (rtx *addr)
24118 {
24119 rtx rtl = *addr;
24120
24121 if (GET_CODE (rtl) == CONST_STRING)
24122 {
24123 size_t len = strlen (XSTR (rtl, 0)) + 1;
24124 tree t = build_string (len, XSTR (rtl, 0));
24125 tree tlen = size_int (len - 1);
24126 TREE_TYPE (t)
24127 = build_array_type (char_type_node, build_index_type (tlen));
24128 rtl = lookup_constant_def (t);
24129 if (!rtl || !MEM_P (rtl))
24130 return false;
24131 rtl = XEXP (rtl, 0);
24132 if (GET_CODE (rtl) == SYMBOL_REF
24133 && SYMBOL_REF_DECL (rtl)
24134 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24135 return false;
24136 vec_safe_push (used_rtx_array, rtl);
24137 *addr = rtl;
24138 return true;
24139 }
24140
24141 if (GET_CODE (rtl) == SYMBOL_REF
24142 && SYMBOL_REF_DECL (rtl))
24143 {
24144 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24145 {
24146 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24147 return false;
24148 }
24149 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24150 return false;
24151 }
24152
24153 if (GET_CODE (rtl) == CONST)
24154 {
24155 subrtx_ptr_iterator::array_type array;
24156 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24157 if (!resolve_one_addr (*iter))
24158 return false;
24159 }
24160
24161 return true;
24162 }
24163
24164 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24165 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24166 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24167
24168 static rtx
24169 string_cst_pool_decl (tree t)
24170 {
24171 rtx rtl = output_constant_def (t, 1);
24172 unsigned char *array;
24173 dw_loc_descr_ref l;
24174 tree decl;
24175 size_t len;
24176 dw_die_ref ref;
24177
24178 if (!rtl || !MEM_P (rtl))
24179 return NULL_RTX;
24180 rtl = XEXP (rtl, 0);
24181 if (GET_CODE (rtl) != SYMBOL_REF
24182 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24183 return NULL_RTX;
24184
24185 decl = SYMBOL_REF_DECL (rtl);
24186 if (!lookup_decl_die (decl))
24187 {
24188 len = TREE_STRING_LENGTH (t);
24189 vec_safe_push (used_rtx_array, rtl);
24190 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24191 array = ggc_vec_alloc<unsigned char> (len);
24192 memcpy (array, TREE_STRING_POINTER (t), len);
24193 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24194 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24195 l->dw_loc_oprnd2.v.val_vec.length = len;
24196 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24197 l->dw_loc_oprnd2.v.val_vec.array = array;
24198 add_AT_loc (ref, DW_AT_location, l);
24199 equate_decl_number_to_die (decl, ref);
24200 }
24201 return rtl;
24202 }
24203
24204 /* Helper function of resolve_addr_in_expr. LOC is
24205 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24206 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24207 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24208 with DW_OP_GNU_implicit_pointer if possible
24209 and return true, if unsuccessful, return false. */
24210
24211 static bool
24212 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24213 {
24214 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24215 HOST_WIDE_INT offset = 0;
24216 dw_die_ref ref = NULL;
24217 tree decl;
24218
24219 if (GET_CODE (rtl) == CONST
24220 && GET_CODE (XEXP (rtl, 0)) == PLUS
24221 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24222 {
24223 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24224 rtl = XEXP (XEXP (rtl, 0), 0);
24225 }
24226 if (GET_CODE (rtl) == CONST_STRING)
24227 {
24228 size_t len = strlen (XSTR (rtl, 0)) + 1;
24229 tree t = build_string (len, XSTR (rtl, 0));
24230 tree tlen = size_int (len - 1);
24231
24232 TREE_TYPE (t)
24233 = build_array_type (char_type_node, build_index_type (tlen));
24234 rtl = string_cst_pool_decl (t);
24235 if (!rtl)
24236 return false;
24237 }
24238 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24239 {
24240 decl = SYMBOL_REF_DECL (rtl);
24241 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24242 {
24243 ref = lookup_decl_die (decl);
24244 if (ref && (get_AT (ref, DW_AT_location)
24245 || get_AT (ref, DW_AT_const_value)))
24246 {
24247 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24248 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24249 loc->dw_loc_oprnd1.val_entry = NULL;
24250 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24251 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24252 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24253 loc->dw_loc_oprnd2.v.val_int = offset;
24254 return true;
24255 }
24256 }
24257 }
24258 return false;
24259 }
24260
24261 /* Helper function for resolve_addr, handle one location
24262 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24263 the location list couldn't be resolved. */
24264
24265 static bool
24266 resolve_addr_in_expr (dw_loc_descr_ref loc)
24267 {
24268 dw_loc_descr_ref keep = NULL;
24269 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24270 switch (loc->dw_loc_opc)
24271 {
24272 case DW_OP_addr:
24273 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24274 {
24275 if ((prev == NULL
24276 || prev->dw_loc_opc == DW_OP_piece
24277 || prev->dw_loc_opc == DW_OP_bit_piece)
24278 && loc->dw_loc_next
24279 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24280 && !dwarf_strict
24281 && optimize_one_addr_into_implicit_ptr (loc))
24282 break;
24283 return false;
24284 }
24285 break;
24286 case DW_OP_GNU_addr_index:
24287 case DW_OP_GNU_const_index:
24288 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24289 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24290 {
24291 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24292 if (!resolve_one_addr (&rtl))
24293 return false;
24294 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24295 loc->dw_loc_oprnd1.val_entry =
24296 add_addr_table_entry (rtl, ate_kind_rtx);
24297 }
24298 break;
24299 case DW_OP_const4u:
24300 case DW_OP_const8u:
24301 if (loc->dtprel
24302 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24303 return false;
24304 break;
24305 case DW_OP_plus_uconst:
24306 if (size_of_loc_descr (loc)
24307 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24308 + 1
24309 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24310 {
24311 dw_loc_descr_ref repl
24312 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24313 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24314 add_loc_descr (&repl, loc->dw_loc_next);
24315 *loc = *repl;
24316 }
24317 break;
24318 case DW_OP_implicit_value:
24319 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24320 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24321 return false;
24322 break;
24323 case DW_OP_GNU_implicit_pointer:
24324 case DW_OP_GNU_parameter_ref:
24325 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24326 {
24327 dw_die_ref ref
24328 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24329 if (ref == NULL)
24330 return false;
24331 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24332 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24333 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24334 }
24335 break;
24336 case DW_OP_GNU_const_type:
24337 case DW_OP_GNU_regval_type:
24338 case DW_OP_GNU_deref_type:
24339 case DW_OP_GNU_convert:
24340 case DW_OP_GNU_reinterpret:
24341 while (loc->dw_loc_next
24342 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24343 {
24344 dw_die_ref base1, base2;
24345 unsigned enc1, enc2, size1, size2;
24346 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24347 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24348 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24349 else if (loc->dw_loc_oprnd1.val_class
24350 == dw_val_class_unsigned_const)
24351 break;
24352 else
24353 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24354 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24355 == dw_val_class_unsigned_const)
24356 break;
24357 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24358 gcc_assert (base1->die_tag == DW_TAG_base_type
24359 && base2->die_tag == DW_TAG_base_type);
24360 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24361 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24362 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24363 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24364 if (size1 == size2
24365 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24366 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24367 && loc != keep)
24368 || enc1 == enc2))
24369 {
24370 /* Optimize away next DW_OP_GNU_convert after
24371 adjusting LOC's base type die reference. */
24372 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24373 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24374 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24375 else
24376 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24377 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24378 continue;
24379 }
24380 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24381 point typed stack entry. */
24382 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24383 keep = loc->dw_loc_next;
24384 break;
24385 }
24386 break;
24387 default:
24388 break;
24389 }
24390 return true;
24391 }
24392
24393 /* Helper function of resolve_addr. DIE had DW_AT_location of
24394 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24395 and DW_OP_addr couldn't be resolved. resolve_addr has already
24396 removed the DW_AT_location attribute. This function attempts to
24397 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24398 to it or DW_AT_const_value attribute, if possible. */
24399
24400 static void
24401 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24402 {
24403 if (TREE_CODE (decl) != VAR_DECL
24404 || lookup_decl_die (decl) != die
24405 || DECL_EXTERNAL (decl)
24406 || !TREE_STATIC (decl)
24407 || DECL_INITIAL (decl) == NULL_TREE
24408 || DECL_P (DECL_INITIAL (decl))
24409 || get_AT (die, DW_AT_const_value))
24410 return;
24411
24412 tree init = DECL_INITIAL (decl);
24413 HOST_WIDE_INT offset = 0;
24414 /* For variables that have been optimized away and thus
24415 don't have a memory location, see if we can emit
24416 DW_AT_const_value instead. */
24417 if (tree_add_const_value_attribute (die, init))
24418 return;
24419 if (dwarf_strict)
24420 return;
24421 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24422 and ADDR_EXPR refers to a decl that has DW_AT_location or
24423 DW_AT_const_value (but isn't addressable, otherwise
24424 resolving the original DW_OP_addr wouldn't fail), see if
24425 we can add DW_OP_GNU_implicit_pointer. */
24426 STRIP_NOPS (init);
24427 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24428 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24429 {
24430 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24431 init = TREE_OPERAND (init, 0);
24432 STRIP_NOPS (init);
24433 }
24434 if (TREE_CODE (init) != ADDR_EXPR)
24435 return;
24436 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24437 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24438 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24439 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24440 && TREE_OPERAND (init, 0) != decl))
24441 {
24442 dw_die_ref ref;
24443 dw_loc_descr_ref l;
24444
24445 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24446 {
24447 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24448 if (!rtl)
24449 return;
24450 decl = SYMBOL_REF_DECL (rtl);
24451 }
24452 else
24453 decl = TREE_OPERAND (init, 0);
24454 ref = lookup_decl_die (decl);
24455 if (ref == NULL
24456 || (!get_AT (ref, DW_AT_location)
24457 && !get_AT (ref, DW_AT_const_value)))
24458 return;
24459 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24460 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24461 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24462 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24463 add_AT_loc (die, DW_AT_location, l);
24464 }
24465 }
24466
24467 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24468 an address in .rodata section if the string literal is emitted there,
24469 or remove the containing location list or replace DW_AT_const_value
24470 with DW_AT_location and empty location expression, if it isn't found
24471 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24472 to something that has been emitted in the current CU. */
24473
24474 static void
24475 resolve_addr (dw_die_ref die)
24476 {
24477 dw_die_ref c;
24478 dw_attr_node *a;
24479 dw_loc_list_ref *curr, *start, loc;
24480 unsigned ix;
24481
24482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24483 switch (AT_class (a))
24484 {
24485 case dw_val_class_loc_list:
24486 start = curr = AT_loc_list_ptr (a);
24487 loc = *curr;
24488 gcc_assert (loc);
24489 /* The same list can be referenced more than once. See if we have
24490 already recorded the result from a previous pass. */
24491 if (loc->replaced)
24492 *curr = loc->dw_loc_next;
24493 else if (!loc->resolved_addr)
24494 {
24495 /* As things stand, we do not expect or allow one die to
24496 reference a suffix of another die's location list chain.
24497 References must be identical or completely separate.
24498 There is therefore no need to cache the result of this
24499 pass on any list other than the first; doing so
24500 would lead to unnecessary writes. */
24501 while (*curr)
24502 {
24503 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24504 if (!resolve_addr_in_expr ((*curr)->expr))
24505 {
24506 dw_loc_list_ref next = (*curr)->dw_loc_next;
24507 dw_loc_descr_ref l = (*curr)->expr;
24508
24509 if (next && (*curr)->ll_symbol)
24510 {
24511 gcc_assert (!next->ll_symbol);
24512 next->ll_symbol = (*curr)->ll_symbol;
24513 }
24514 if (dwarf_split_debug_info)
24515 remove_loc_list_addr_table_entries (l);
24516 *curr = next;
24517 }
24518 else
24519 {
24520 mark_base_types ((*curr)->expr);
24521 curr = &(*curr)->dw_loc_next;
24522 }
24523 }
24524 if (loc == *start)
24525 loc->resolved_addr = 1;
24526 else
24527 {
24528 loc->replaced = 1;
24529 loc->dw_loc_next = *start;
24530 }
24531 }
24532 if (!*start)
24533 {
24534 remove_AT (die, a->dw_attr);
24535 ix--;
24536 }
24537 break;
24538 case dw_val_class_loc:
24539 {
24540 dw_loc_descr_ref l = AT_loc (a);
24541 /* For -gdwarf-2 don't attempt to optimize
24542 DW_AT_data_member_location containing
24543 DW_OP_plus_uconst - older consumers might
24544 rely on it being that op instead of a more complex,
24545 but shorter, location description. */
24546 if ((dwarf_version > 2
24547 || a->dw_attr != DW_AT_data_member_location
24548 || l == NULL
24549 || l->dw_loc_opc != DW_OP_plus_uconst
24550 || l->dw_loc_next != NULL)
24551 && !resolve_addr_in_expr (l))
24552 {
24553 if (dwarf_split_debug_info)
24554 remove_loc_list_addr_table_entries (l);
24555 if (l != NULL
24556 && l->dw_loc_next == NULL
24557 && l->dw_loc_opc == DW_OP_addr
24558 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24559 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24560 && a->dw_attr == DW_AT_location)
24561 {
24562 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24563 remove_AT (die, a->dw_attr);
24564 ix--;
24565 optimize_location_into_implicit_ptr (die, decl);
24566 break;
24567 }
24568 remove_AT (die, a->dw_attr);
24569 ix--;
24570 }
24571 else
24572 mark_base_types (l);
24573 }
24574 break;
24575 case dw_val_class_addr:
24576 if (a->dw_attr == DW_AT_const_value
24577 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24578 {
24579 if (AT_index (a) != NOT_INDEXED)
24580 remove_addr_table_entry (a->dw_attr_val.val_entry);
24581 remove_AT (die, a->dw_attr);
24582 ix--;
24583 }
24584 if (die->die_tag == DW_TAG_GNU_call_site
24585 && a->dw_attr == DW_AT_abstract_origin)
24586 {
24587 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24588 dw_die_ref tdie = lookup_decl_die (tdecl);
24589 dw_die_ref cdie;
24590 if (tdie == NULL
24591 && DECL_EXTERNAL (tdecl)
24592 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24593 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24594 {
24595 /* Creating a full DIE for tdecl is overly expensive and
24596 at this point even wrong when in the LTO phase
24597 as it can end up generating new type DIEs we didn't
24598 output and thus optimize_external_refs will crash. */
24599 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24600 add_AT_flag (tdie, DW_AT_external, 1);
24601 add_AT_flag (tdie, DW_AT_declaration, 1);
24602 add_linkage_attr (tdie, tdecl);
24603 add_name_and_src_coords_attributes (tdie, tdecl);
24604 equate_decl_number_to_die (tdecl, tdie);
24605 }
24606 if (tdie)
24607 {
24608 a->dw_attr_val.val_class = dw_val_class_die_ref;
24609 a->dw_attr_val.v.val_die_ref.die = tdie;
24610 a->dw_attr_val.v.val_die_ref.external = 0;
24611 }
24612 else
24613 {
24614 if (AT_index (a) != NOT_INDEXED)
24615 remove_addr_table_entry (a->dw_attr_val.val_entry);
24616 remove_AT (die, a->dw_attr);
24617 ix--;
24618 }
24619 }
24620 break;
24621 default:
24622 break;
24623 }
24624
24625 FOR_EACH_CHILD (die, c, resolve_addr (c));
24626 }
24627 \f
24628 /* Helper routines for optimize_location_lists.
24629 This pass tries to share identical local lists in .debug_loc
24630 section. */
24631
24632 /* Iteratively hash operands of LOC opcode into HSTATE. */
24633
24634 static void
24635 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24636 {
24637 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24638 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24639
24640 switch (loc->dw_loc_opc)
24641 {
24642 case DW_OP_const4u:
24643 case DW_OP_const8u:
24644 if (loc->dtprel)
24645 goto hash_addr;
24646 /* FALLTHRU */
24647 case DW_OP_const1u:
24648 case DW_OP_const1s:
24649 case DW_OP_const2u:
24650 case DW_OP_const2s:
24651 case DW_OP_const4s:
24652 case DW_OP_const8s:
24653 case DW_OP_constu:
24654 case DW_OP_consts:
24655 case DW_OP_pick:
24656 case DW_OP_plus_uconst:
24657 case DW_OP_breg0:
24658 case DW_OP_breg1:
24659 case DW_OP_breg2:
24660 case DW_OP_breg3:
24661 case DW_OP_breg4:
24662 case DW_OP_breg5:
24663 case DW_OP_breg6:
24664 case DW_OP_breg7:
24665 case DW_OP_breg8:
24666 case DW_OP_breg9:
24667 case DW_OP_breg10:
24668 case DW_OP_breg11:
24669 case DW_OP_breg12:
24670 case DW_OP_breg13:
24671 case DW_OP_breg14:
24672 case DW_OP_breg15:
24673 case DW_OP_breg16:
24674 case DW_OP_breg17:
24675 case DW_OP_breg18:
24676 case DW_OP_breg19:
24677 case DW_OP_breg20:
24678 case DW_OP_breg21:
24679 case DW_OP_breg22:
24680 case DW_OP_breg23:
24681 case DW_OP_breg24:
24682 case DW_OP_breg25:
24683 case DW_OP_breg26:
24684 case DW_OP_breg27:
24685 case DW_OP_breg28:
24686 case DW_OP_breg29:
24687 case DW_OP_breg30:
24688 case DW_OP_breg31:
24689 case DW_OP_regx:
24690 case DW_OP_fbreg:
24691 case DW_OP_piece:
24692 case DW_OP_deref_size:
24693 case DW_OP_xderef_size:
24694 hstate.add_object (val1->v.val_int);
24695 break;
24696 case DW_OP_skip:
24697 case DW_OP_bra:
24698 {
24699 int offset;
24700
24701 gcc_assert (val1->val_class == dw_val_class_loc);
24702 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24703 hstate.add_object (offset);
24704 }
24705 break;
24706 case DW_OP_implicit_value:
24707 hstate.add_object (val1->v.val_unsigned);
24708 switch (val2->val_class)
24709 {
24710 case dw_val_class_const:
24711 hstate.add_object (val2->v.val_int);
24712 break;
24713 case dw_val_class_vec:
24714 {
24715 unsigned int elt_size = val2->v.val_vec.elt_size;
24716 unsigned int len = val2->v.val_vec.length;
24717
24718 hstate.add_int (elt_size);
24719 hstate.add_int (len);
24720 hstate.add (val2->v.val_vec.array, len * elt_size);
24721 }
24722 break;
24723 case dw_val_class_const_double:
24724 hstate.add_object (val2->v.val_double.low);
24725 hstate.add_object (val2->v.val_double.high);
24726 break;
24727 case dw_val_class_wide_int:
24728 hstate.add (val2->v.val_wide->get_val (),
24729 get_full_len (*val2->v.val_wide)
24730 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24731 break;
24732 case dw_val_class_addr:
24733 inchash::add_rtx (val2->v.val_addr, hstate);
24734 break;
24735 default:
24736 gcc_unreachable ();
24737 }
24738 break;
24739 case DW_OP_bregx:
24740 case DW_OP_bit_piece:
24741 hstate.add_object (val1->v.val_int);
24742 hstate.add_object (val2->v.val_int);
24743 break;
24744 case DW_OP_addr:
24745 hash_addr:
24746 if (loc->dtprel)
24747 {
24748 unsigned char dtprel = 0xd1;
24749 hstate.add_object (dtprel);
24750 }
24751 inchash::add_rtx (val1->v.val_addr, hstate);
24752 break;
24753 case DW_OP_GNU_addr_index:
24754 case DW_OP_GNU_const_index:
24755 {
24756 if (loc->dtprel)
24757 {
24758 unsigned char dtprel = 0xd1;
24759 hstate.add_object (dtprel);
24760 }
24761 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24762 }
24763 break;
24764 case DW_OP_GNU_implicit_pointer:
24765 hstate.add_int (val2->v.val_int);
24766 break;
24767 case DW_OP_GNU_entry_value:
24768 hstate.add_object (val1->v.val_loc);
24769 break;
24770 case DW_OP_GNU_regval_type:
24771 case DW_OP_GNU_deref_type:
24772 {
24773 unsigned int byte_size
24774 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24775 unsigned int encoding
24776 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24777 hstate.add_object (val1->v.val_int);
24778 hstate.add_object (byte_size);
24779 hstate.add_object (encoding);
24780 }
24781 break;
24782 case DW_OP_GNU_convert:
24783 case DW_OP_GNU_reinterpret:
24784 if (val1->val_class == dw_val_class_unsigned_const)
24785 {
24786 hstate.add_object (val1->v.val_unsigned);
24787 break;
24788 }
24789 /* FALLTHRU */
24790 case DW_OP_GNU_const_type:
24791 {
24792 unsigned int byte_size
24793 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24794 unsigned int encoding
24795 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24796 hstate.add_object (byte_size);
24797 hstate.add_object (encoding);
24798 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24799 break;
24800 hstate.add_object (val2->val_class);
24801 switch (val2->val_class)
24802 {
24803 case dw_val_class_const:
24804 hstate.add_object (val2->v.val_int);
24805 break;
24806 case dw_val_class_vec:
24807 {
24808 unsigned int elt_size = val2->v.val_vec.elt_size;
24809 unsigned int len = val2->v.val_vec.length;
24810
24811 hstate.add_object (elt_size);
24812 hstate.add_object (len);
24813 hstate.add (val2->v.val_vec.array, len * elt_size);
24814 }
24815 break;
24816 case dw_val_class_const_double:
24817 hstate.add_object (val2->v.val_double.low);
24818 hstate.add_object (val2->v.val_double.high);
24819 break;
24820 case dw_val_class_wide_int:
24821 hstate.add (val2->v.val_wide->get_val (),
24822 get_full_len (*val2->v.val_wide)
24823 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24824 break;
24825 default:
24826 gcc_unreachable ();
24827 }
24828 }
24829 break;
24830
24831 default:
24832 /* Other codes have no operands. */
24833 break;
24834 }
24835 }
24836
24837 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24838
24839 static inline void
24840 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24841 {
24842 dw_loc_descr_ref l;
24843 bool sizes_computed = false;
24844 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24845 size_of_locs (loc);
24846
24847 for (l = loc; l != NULL; l = l->dw_loc_next)
24848 {
24849 enum dwarf_location_atom opc = l->dw_loc_opc;
24850 hstate.add_object (opc);
24851 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24852 {
24853 size_of_locs (loc);
24854 sizes_computed = true;
24855 }
24856 hash_loc_operands (l, hstate);
24857 }
24858 }
24859
24860 /* Compute hash of the whole location list LIST_HEAD. */
24861
24862 static inline void
24863 hash_loc_list (dw_loc_list_ref list_head)
24864 {
24865 dw_loc_list_ref curr = list_head;
24866 inchash::hash hstate;
24867
24868 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24869 {
24870 hstate.add (curr->begin, strlen (curr->begin) + 1);
24871 hstate.add (curr->end, strlen (curr->end) + 1);
24872 if (curr->section)
24873 hstate.add (curr->section, strlen (curr->section) + 1);
24874 hash_locs (curr->expr, hstate);
24875 }
24876 list_head->hash = hstate.end ();
24877 }
24878
24879 /* Return true if X and Y opcodes have the same operands. */
24880
24881 static inline bool
24882 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24883 {
24884 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24885 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24886 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24887 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24888
24889 switch (x->dw_loc_opc)
24890 {
24891 case DW_OP_const4u:
24892 case DW_OP_const8u:
24893 if (x->dtprel)
24894 goto hash_addr;
24895 /* FALLTHRU */
24896 case DW_OP_const1u:
24897 case DW_OP_const1s:
24898 case DW_OP_const2u:
24899 case DW_OP_const2s:
24900 case DW_OP_const4s:
24901 case DW_OP_const8s:
24902 case DW_OP_constu:
24903 case DW_OP_consts:
24904 case DW_OP_pick:
24905 case DW_OP_plus_uconst:
24906 case DW_OP_breg0:
24907 case DW_OP_breg1:
24908 case DW_OP_breg2:
24909 case DW_OP_breg3:
24910 case DW_OP_breg4:
24911 case DW_OP_breg5:
24912 case DW_OP_breg6:
24913 case DW_OP_breg7:
24914 case DW_OP_breg8:
24915 case DW_OP_breg9:
24916 case DW_OP_breg10:
24917 case DW_OP_breg11:
24918 case DW_OP_breg12:
24919 case DW_OP_breg13:
24920 case DW_OP_breg14:
24921 case DW_OP_breg15:
24922 case DW_OP_breg16:
24923 case DW_OP_breg17:
24924 case DW_OP_breg18:
24925 case DW_OP_breg19:
24926 case DW_OP_breg20:
24927 case DW_OP_breg21:
24928 case DW_OP_breg22:
24929 case DW_OP_breg23:
24930 case DW_OP_breg24:
24931 case DW_OP_breg25:
24932 case DW_OP_breg26:
24933 case DW_OP_breg27:
24934 case DW_OP_breg28:
24935 case DW_OP_breg29:
24936 case DW_OP_breg30:
24937 case DW_OP_breg31:
24938 case DW_OP_regx:
24939 case DW_OP_fbreg:
24940 case DW_OP_piece:
24941 case DW_OP_deref_size:
24942 case DW_OP_xderef_size:
24943 return valx1->v.val_int == valy1->v.val_int;
24944 case DW_OP_skip:
24945 case DW_OP_bra:
24946 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24947 can cause irrelevant differences in dw_loc_addr. */
24948 gcc_assert (valx1->val_class == dw_val_class_loc
24949 && valy1->val_class == dw_val_class_loc
24950 && (dwarf_split_debug_info
24951 || x->dw_loc_addr == y->dw_loc_addr));
24952 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24953 case DW_OP_implicit_value:
24954 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24955 || valx2->val_class != valy2->val_class)
24956 return false;
24957 switch (valx2->val_class)
24958 {
24959 case dw_val_class_const:
24960 return valx2->v.val_int == valy2->v.val_int;
24961 case dw_val_class_vec:
24962 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24963 && valx2->v.val_vec.length == valy2->v.val_vec.length
24964 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24965 valx2->v.val_vec.elt_size
24966 * valx2->v.val_vec.length) == 0;
24967 case dw_val_class_const_double:
24968 return valx2->v.val_double.low == valy2->v.val_double.low
24969 && valx2->v.val_double.high == valy2->v.val_double.high;
24970 case dw_val_class_wide_int:
24971 return *valx2->v.val_wide == *valy2->v.val_wide;
24972 case dw_val_class_addr:
24973 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24974 default:
24975 gcc_unreachable ();
24976 }
24977 case DW_OP_bregx:
24978 case DW_OP_bit_piece:
24979 return valx1->v.val_int == valy1->v.val_int
24980 && valx2->v.val_int == valy2->v.val_int;
24981 case DW_OP_addr:
24982 hash_addr:
24983 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24984 case DW_OP_GNU_addr_index:
24985 case DW_OP_GNU_const_index:
24986 {
24987 rtx ax1 = valx1->val_entry->addr.rtl;
24988 rtx ay1 = valy1->val_entry->addr.rtl;
24989 return rtx_equal_p (ax1, ay1);
24990 }
24991 case DW_OP_GNU_implicit_pointer:
24992 return valx1->val_class == dw_val_class_die_ref
24993 && valx1->val_class == valy1->val_class
24994 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24995 && valx2->v.val_int == valy2->v.val_int;
24996 case DW_OP_GNU_entry_value:
24997 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24998 case DW_OP_GNU_const_type:
24999 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
25000 || valx2->val_class != valy2->val_class)
25001 return false;
25002 switch (valx2->val_class)
25003 {
25004 case dw_val_class_const:
25005 return valx2->v.val_int == valy2->v.val_int;
25006 case dw_val_class_vec:
25007 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
25008 && valx2->v.val_vec.length == valy2->v.val_vec.length
25009 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
25010 valx2->v.val_vec.elt_size
25011 * valx2->v.val_vec.length) == 0;
25012 case dw_val_class_const_double:
25013 return valx2->v.val_double.low == valy2->v.val_double.low
25014 && valx2->v.val_double.high == valy2->v.val_double.high;
25015 case dw_val_class_wide_int:
25016 return *valx2->v.val_wide == *valy2->v.val_wide;
25017 default:
25018 gcc_unreachable ();
25019 }
25020 case DW_OP_GNU_regval_type:
25021 case DW_OP_GNU_deref_type:
25022 return valx1->v.val_int == valy1->v.val_int
25023 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
25024 case DW_OP_GNU_convert:
25025 case DW_OP_GNU_reinterpret:
25026 if (valx1->val_class != valy1->val_class)
25027 return false;
25028 if (valx1->val_class == dw_val_class_unsigned_const)
25029 return valx1->v.val_unsigned == valy1->v.val_unsigned;
25030 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25031 case DW_OP_GNU_parameter_ref:
25032 return valx1->val_class == dw_val_class_die_ref
25033 && valx1->val_class == valy1->val_class
25034 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25035 default:
25036 /* Other codes have no operands. */
25037 return true;
25038 }
25039 }
25040
25041 /* Return true if DWARF location expressions X and Y are the same. */
25042
25043 static inline bool
25044 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25045 {
25046 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25047 if (x->dw_loc_opc != y->dw_loc_opc
25048 || x->dtprel != y->dtprel
25049 || !compare_loc_operands (x, y))
25050 break;
25051 return x == NULL && y == NULL;
25052 }
25053
25054 /* Hashtable helpers. */
25055
25056 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25057 {
25058 static inline hashval_t hash (const dw_loc_list_struct *);
25059 static inline bool equal (const dw_loc_list_struct *,
25060 const dw_loc_list_struct *);
25061 };
25062
25063 /* Return precomputed hash of location list X. */
25064
25065 inline hashval_t
25066 loc_list_hasher::hash (const dw_loc_list_struct *x)
25067 {
25068 return x->hash;
25069 }
25070
25071 /* Return true if location lists A and B are the same. */
25072
25073 inline bool
25074 loc_list_hasher::equal (const dw_loc_list_struct *a,
25075 const dw_loc_list_struct *b)
25076 {
25077 if (a == b)
25078 return 1;
25079 if (a->hash != b->hash)
25080 return 0;
25081 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25082 if (strcmp (a->begin, b->begin) != 0
25083 || strcmp (a->end, b->end) != 0
25084 || (a->section == NULL) != (b->section == NULL)
25085 || (a->section && strcmp (a->section, b->section) != 0)
25086 || !compare_locs (a->expr, b->expr))
25087 break;
25088 return a == NULL && b == NULL;
25089 }
25090
25091 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25092
25093
25094 /* Recursively optimize location lists referenced from DIE
25095 children and share them whenever possible. */
25096
25097 static void
25098 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25099 {
25100 dw_die_ref c;
25101 dw_attr_node *a;
25102 unsigned ix;
25103 dw_loc_list_struct **slot;
25104
25105 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25106 if (AT_class (a) == dw_val_class_loc_list)
25107 {
25108 dw_loc_list_ref list = AT_loc_list (a);
25109 /* TODO: perform some optimizations here, before hashing
25110 it and storing into the hash table. */
25111 hash_loc_list (list);
25112 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25113 if (*slot == NULL)
25114 *slot = list;
25115 else
25116 a->dw_attr_val.v.val_loc_list = *slot;
25117 }
25118
25119 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25120 }
25121
25122
25123 /* Recursively assign each location list a unique index into the debug_addr
25124 section. */
25125
25126 static void
25127 index_location_lists (dw_die_ref die)
25128 {
25129 dw_die_ref c;
25130 dw_attr_node *a;
25131 unsigned ix;
25132
25133 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25134 if (AT_class (a) == dw_val_class_loc_list)
25135 {
25136 dw_loc_list_ref list = AT_loc_list (a);
25137 dw_loc_list_ref curr;
25138 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25139 {
25140 /* Don't index an entry that has already been indexed
25141 or won't be output. */
25142 if (curr->begin_entry != NULL
25143 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25144 continue;
25145
25146 curr->begin_entry
25147 = add_addr_table_entry (xstrdup (curr->begin),
25148 ate_kind_label);
25149 }
25150 }
25151
25152 FOR_EACH_CHILD (die, c, index_location_lists (c));
25153 }
25154
25155 /* Optimize location lists referenced from DIE
25156 children and share them whenever possible. */
25157
25158 static void
25159 optimize_location_lists (dw_die_ref die)
25160 {
25161 loc_list_hash_type htab (500);
25162 optimize_location_lists_1 (die, &htab);
25163 }
25164 \f
25165 /* Traverse the limbo die list, and add parent/child links. The only
25166 dies without parents that should be here are concrete instances of
25167 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25168 For concrete instances, we can get the parent die from the abstract
25169 instance. */
25170
25171 static void
25172 flush_limbo_die_list (void)
25173 {
25174 limbo_die_node *node, *next_node;
25175
25176 for (node = limbo_die_list; node; node = next_node)
25177 {
25178 dw_die_ref die = node->die;
25179 next_node = node->next;
25180
25181 if (die->die_parent == NULL)
25182 {
25183 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25184
25185 if (origin && origin->die_parent)
25186 add_child_die (origin->die_parent, die);
25187 else if (is_cu_die (die))
25188 ;
25189 else if (seen_error ())
25190 /* It's OK to be confused by errors in the input. */
25191 add_child_die (comp_unit_die (), die);
25192 else
25193 {
25194 /* In certain situations, the lexical block containing a
25195 nested function can be optimized away, which results
25196 in the nested function die being orphaned. Likewise
25197 with the return type of that nested function. Force
25198 this to be a child of the containing function.
25199
25200 It may happen that even the containing function got fully
25201 inlined and optimized out. In that case we are lost and
25202 assign the empty child. This should not be big issue as
25203 the function is likely unreachable too. */
25204 gcc_assert (node->created_for);
25205
25206 if (DECL_P (node->created_for))
25207 origin = get_context_die (DECL_CONTEXT (node->created_for));
25208 else if (TYPE_P (node->created_for))
25209 origin = scope_die_for (node->created_for, comp_unit_die ());
25210 else
25211 origin = comp_unit_die ();
25212
25213 add_child_die (origin, die);
25214 }
25215 }
25216 }
25217
25218 limbo_die_list = NULL;
25219 }
25220
25221 /* Output stuff that dwarf requires at the end of every file,
25222 and generate the DWARF-2 debugging info. */
25223
25224 static void
25225 dwarf2out_finish (const char *filename)
25226 {
25227 comdat_type_node *ctnode;
25228 dw_die_ref main_comp_unit_die;
25229
25230 /* Flush out any latecomers to the limbo party. */
25231 flush_limbo_die_list ();
25232
25233 /* We shouldn't have any symbols with delayed asm names for
25234 DIEs generated after early finish. */
25235 gcc_assert (deferred_asm_name == NULL);
25236
25237 /* PCH might result in DW_AT_producer string being restored from the
25238 header compilation, so always fill it with empty string initially
25239 and overwrite only here. */
25240 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
25241 producer_string = gen_producer_string ();
25242 producer->dw_attr_val.v.val_str->refcount--;
25243 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25244
25245 gen_remaining_tmpl_value_param_die_attribute ();
25246
25247 /* Add the name for the main input file now. We delayed this from
25248 dwarf2out_init to avoid complications with PCH.
25249 For LTO produced units use a fixed artificial name to avoid
25250 leaking tempfile names into the dwarf. */
25251 if (!in_lto_p)
25252 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25253 else
25254 add_name_attribute (comp_unit_die (), "<artificial>");
25255 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25256 add_comp_dir_attribute (comp_unit_die ());
25257 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25258 {
25259 bool p = false;
25260 file_table->traverse<bool *, file_table_relative_p> (&p);
25261 if (p)
25262 add_comp_dir_attribute (comp_unit_die ());
25263 }
25264
25265 #if ENABLE_ASSERT_CHECKING
25266 {
25267 dw_die_ref die = comp_unit_die (), c;
25268 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25269 }
25270 #endif
25271 resolve_addr (comp_unit_die ());
25272 move_marked_base_types ();
25273
25274 /* Walk through the list of incomplete types again, trying once more to
25275 emit full debugging info for them. */
25276 retry_incomplete_types ();
25277
25278 if (flag_eliminate_unused_debug_types)
25279 prune_unused_types ();
25280
25281 /* Generate separate COMDAT sections for type DIEs. */
25282 if (use_debug_types)
25283 {
25284 break_out_comdat_types (comp_unit_die ());
25285
25286 /* Each new type_unit DIE was added to the limbo die list when created.
25287 Since these have all been added to comdat_type_list, clear the
25288 limbo die list. */
25289 limbo_die_list = NULL;
25290
25291 /* For each new comdat type unit, copy declarations for incomplete
25292 types to make the new unit self-contained (i.e., no direct
25293 references to the main compile unit). */
25294 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25295 copy_decls_for_unworthy_types (ctnode->root_die);
25296 copy_decls_for_unworthy_types (comp_unit_die ());
25297
25298 /* In the process of copying declarations from one unit to another,
25299 we may have left some declarations behind that are no longer
25300 referenced. Prune them. */
25301 prune_unused_types ();
25302 }
25303
25304 /* Generate separate CUs for each of the include files we've seen.
25305 They will go into limbo_die_list. */
25306 if (flag_eliminate_dwarf2_dups)
25307 break_out_includes (comp_unit_die ());
25308
25309 /* Traverse the DIE's and add sibling attributes to those DIE's that
25310 have children. */
25311 add_sibling_attributes (comp_unit_die ());
25312 limbo_die_node *node;
25313 for (node = limbo_die_list; node; node = node->next)
25314 add_sibling_attributes (node->die);
25315 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25316 add_sibling_attributes (ctnode->root_die);
25317
25318 /* When splitting DWARF info, we put some attributes in the
25319 skeleton compile_unit DIE that remains in the .o, while
25320 most attributes go in the DWO compile_unit_die. */
25321 if (dwarf_split_debug_info)
25322 main_comp_unit_die = gen_compile_unit_die (NULL);
25323 else
25324 main_comp_unit_die = comp_unit_die ();
25325
25326 /* Output a terminator label for the .text section. */
25327 switch_to_section (text_section);
25328 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25329 if (cold_text_section)
25330 {
25331 switch_to_section (cold_text_section);
25332 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25333 }
25334
25335 /* We can only use the low/high_pc attributes if all of the code was
25336 in .text. */
25337 if (!have_multiple_function_sections
25338 || (dwarf_version < 3 && dwarf_strict))
25339 {
25340 /* Don't add if the CU has no associated code. */
25341 if (text_section_used)
25342 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25343 text_end_label, true);
25344 }
25345 else
25346 {
25347 unsigned fde_idx;
25348 dw_fde_ref fde;
25349 bool range_list_added = false;
25350
25351 if (text_section_used)
25352 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25353 text_end_label, &range_list_added, true);
25354 if (cold_text_section_used)
25355 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25356 cold_end_label, &range_list_added, true);
25357
25358 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25359 {
25360 if (DECL_IGNORED_P (fde->decl))
25361 continue;
25362 if (!fde->in_std_section)
25363 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25364 fde->dw_fde_end, &range_list_added,
25365 true);
25366 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25367 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25368 fde->dw_fde_second_end, &range_list_added,
25369 true);
25370 }
25371
25372 if (range_list_added)
25373 {
25374 /* We need to give .debug_loc and .debug_ranges an appropriate
25375 "base address". Use zero so that these addresses become
25376 absolute. Historically, we've emitted the unexpected
25377 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25378 Emit both to give time for other tools to adapt. */
25379 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25380 if (! dwarf_strict && dwarf_version < 4)
25381 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25382
25383 add_ranges (NULL);
25384 }
25385 }
25386
25387 if (debug_info_level >= DINFO_LEVEL_TERSE)
25388 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25389 debug_line_section_label);
25390
25391 if (have_macinfo)
25392 add_AT_macptr (comp_unit_die (),
25393 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25394 macinfo_section_label);
25395
25396 if (dwarf_split_debug_info)
25397 {
25398 /* optimize_location_lists calculates the size of the lists,
25399 so index them first, and assign indices to the entries.
25400 Although optimize_location_lists will remove entries from
25401 the table, it only does so for duplicates, and therefore
25402 only reduces ref_counts to 1. */
25403 index_location_lists (comp_unit_die ());
25404
25405 if (addr_index_table != NULL)
25406 {
25407 unsigned int index = 0;
25408 addr_index_table
25409 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25410 (&index);
25411 }
25412 }
25413
25414 if (have_location_lists)
25415 optimize_location_lists (comp_unit_die ());
25416
25417 save_macinfo_strings ();
25418
25419 if (dwarf_split_debug_info)
25420 {
25421 unsigned int index = 0;
25422
25423 /* Add attributes common to skeleton compile_units and
25424 type_units. Because these attributes include strings, it
25425 must be done before freezing the string table. Top-level
25426 skeleton die attrs are added when the skeleton type unit is
25427 created, so ensure it is created by this point. */
25428 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25429 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25430 }
25431
25432 /* Output all of the compilation units. We put the main one last so that
25433 the offsets are available to output_pubnames. */
25434 for (node = limbo_die_list; node; node = node->next)
25435 output_comp_unit (node->die, 0);
25436
25437 hash_table<comdat_type_hasher> comdat_type_table (100);
25438 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25439 {
25440 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25441
25442 /* Don't output duplicate types. */
25443 if (*slot != HTAB_EMPTY_ENTRY)
25444 continue;
25445
25446 /* Add a pointer to the line table for the main compilation unit
25447 so that the debugger can make sense of DW_AT_decl_file
25448 attributes. */
25449 if (debug_info_level >= DINFO_LEVEL_TERSE)
25450 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25451 (!dwarf_split_debug_info
25452 ? debug_line_section_label
25453 : debug_skeleton_line_section_label));
25454
25455 output_comdat_type_unit (ctnode);
25456 *slot = ctnode;
25457 }
25458
25459 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25460 both the main_cu and all skeleton TUs. Making this call unconditional
25461 would end up either adding a second copy of the AT_pubnames attribute, or
25462 requiring a special case in add_top_level_skeleton_die_attrs. */
25463 if (!dwarf_split_debug_info)
25464 add_AT_pubnames (comp_unit_die ());
25465
25466 if (dwarf_split_debug_info)
25467 {
25468 int mark;
25469 unsigned char checksum[16];
25470 struct md5_ctx ctx;
25471
25472 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25473 md5_init_ctx (&ctx);
25474 mark = 0;
25475 die_checksum (comp_unit_die (), &ctx, &mark);
25476 unmark_all_dies (comp_unit_die ());
25477 md5_finish_ctx (&ctx, checksum);
25478
25479 /* Use the first 8 bytes of the checksum as the dwo_id,
25480 and add it to both comp-unit DIEs. */
25481 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25482 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25483
25484 /* Add the base offset of the ranges table to the skeleton
25485 comp-unit DIE. */
25486 if (ranges_table_in_use)
25487 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25488 ranges_section_label);
25489
25490 switch_to_section (debug_addr_section);
25491 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25492 output_addr_table ();
25493 }
25494
25495 /* Output the main compilation unit if non-empty or if .debug_macinfo
25496 or .debug_macro will be emitted. */
25497 output_comp_unit (comp_unit_die (), have_macinfo);
25498
25499 if (dwarf_split_debug_info && info_section_emitted)
25500 output_skeleton_debug_sections (main_comp_unit_die);
25501
25502 /* Output the abbreviation table. */
25503 if (abbrev_die_table_in_use != 1)
25504 {
25505 switch_to_section (debug_abbrev_section);
25506 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25507 output_abbrev_section ();
25508 }
25509
25510 /* Output location list section if necessary. */
25511 if (have_location_lists)
25512 {
25513 /* Output the location lists info. */
25514 switch_to_section (debug_loc_section);
25515 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25516 output_location_lists (comp_unit_die ());
25517 }
25518
25519 output_pubtables ();
25520
25521 /* Output the address range information if a CU (.debug_info section)
25522 was emitted. We output an empty table even if we had no functions
25523 to put in it. This because the consumer has no way to tell the
25524 difference between an empty table that we omitted and failure to
25525 generate a table that would have contained data. */
25526 if (info_section_emitted)
25527 {
25528 switch_to_section (debug_aranges_section);
25529 output_aranges ();
25530 }
25531
25532 /* Output ranges section if necessary. */
25533 if (ranges_table_in_use)
25534 {
25535 switch_to_section (debug_ranges_section);
25536 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25537 output_ranges ();
25538 }
25539
25540 /* Have to end the macro section. */
25541 if (have_macinfo)
25542 {
25543 switch_to_section (debug_macinfo_section);
25544 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25545 output_macinfo ();
25546 dw2_asm_output_data (1, 0, "End compilation unit");
25547 }
25548
25549 /* Output the source line correspondence table. We must do this
25550 even if there is no line information. Otherwise, on an empty
25551 translation unit, we will generate a present, but empty,
25552 .debug_info section. IRIX 6.5 `nm' will then complain when
25553 examining the file. This is done late so that any filenames
25554 used by the debug_info section are marked as 'used'. */
25555 switch_to_section (debug_line_section);
25556 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25557 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25558 output_line_info (false);
25559
25560 if (dwarf_split_debug_info && info_section_emitted)
25561 {
25562 switch_to_section (debug_skeleton_line_section);
25563 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25564 output_line_info (true);
25565 }
25566
25567 /* If we emitted any indirect strings, output the string table too. */
25568 if (debug_str_hash || skeleton_debug_str_hash)
25569 output_indirect_strings ();
25570 }
25571
25572 /* Perform any cleanups needed after the early debug generation pass
25573 has run. */
25574
25575 static void
25576 dwarf2out_early_finish (void)
25577 {
25578 limbo_die_node *node;
25579
25580 /* Add DW_AT_linkage_name for all deferred DIEs. */
25581 for (node = deferred_asm_name; node; node = node->next)
25582 {
25583 tree decl = node->created_for;
25584 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25585 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25586 ended up in deferred_asm_name before we knew it was
25587 constant and never written to disk. */
25588 && DECL_ASSEMBLER_NAME (decl))
25589 {
25590 add_linkage_attr (node->die, decl);
25591 move_linkage_attr (node->die);
25592 }
25593 }
25594 deferred_asm_name = NULL;
25595
25596 /* The point here is to flush out the limbo list so that it is empty
25597 and we don't need to stream it for LTO. */
25598 flush_limbo_die_list ();
25599
25600 gen_scheduled_generic_parms_dies ();
25601 gen_remaining_tmpl_value_param_die_attribute ();
25602 }
25603
25604 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25605 within the same process. For use by toplev::finalize. */
25606
25607 void
25608 dwarf2out_c_finalize (void)
25609 {
25610 last_var_location_insn = NULL;
25611 cached_next_real_insn = NULL;
25612 used_rtx_array = NULL;
25613 incomplete_types = NULL;
25614 decl_scope_table = NULL;
25615 debug_info_section = NULL;
25616 debug_skeleton_info_section = NULL;
25617 debug_abbrev_section = NULL;
25618 debug_skeleton_abbrev_section = NULL;
25619 debug_aranges_section = NULL;
25620 debug_addr_section = NULL;
25621 debug_macinfo_section = NULL;
25622 debug_line_section = NULL;
25623 debug_skeleton_line_section = NULL;
25624 debug_loc_section = NULL;
25625 debug_pubnames_section = NULL;
25626 debug_pubtypes_section = NULL;
25627 debug_str_section = NULL;
25628 debug_str_dwo_section = NULL;
25629 debug_str_offsets_section = NULL;
25630 debug_ranges_section = NULL;
25631 debug_frame_section = NULL;
25632 fde_vec = NULL;
25633 debug_str_hash = NULL;
25634 skeleton_debug_str_hash = NULL;
25635 dw2_string_counter = 0;
25636 have_multiple_function_sections = false;
25637 text_section_used = false;
25638 cold_text_section_used = false;
25639 cold_text_section = NULL;
25640 current_unit_personality = NULL;
25641
25642 next_die_offset = 0;
25643 single_comp_unit_die = NULL;
25644 comdat_type_list = NULL;
25645 limbo_die_list = NULL;
25646 file_table = NULL;
25647 decl_die_table = NULL;
25648 common_block_die_table = NULL;
25649 decl_loc_table = NULL;
25650 call_arg_locations = NULL;
25651 call_arg_loc_last = NULL;
25652 call_site_count = -1;
25653 tail_call_site_count = -1;
25654 cached_dw_loc_list_table = NULL;
25655 abbrev_die_table = NULL;
25656 abbrev_die_table_allocated = 0;
25657 abbrev_die_table_in_use = 0;
25658 line_info_label_num = 0;
25659 cur_line_info_table = NULL;
25660 text_section_line_info = NULL;
25661 cold_text_section_line_info = NULL;
25662 separate_line_info = NULL;
25663 info_section_emitted = false;
25664 pubname_table = NULL;
25665 pubtype_table = NULL;
25666 macinfo_table = NULL;
25667 ranges_table = NULL;
25668 ranges_table_allocated = 0;
25669 ranges_table_in_use = 0;
25670 ranges_by_label = 0;
25671 ranges_by_label_allocated = 0;
25672 ranges_by_label_in_use = 0;
25673 have_location_lists = false;
25674 loclabel_num = 0;
25675 poc_label_num = 0;
25676 last_emitted_file = NULL;
25677 label_num = 0;
25678 tmpl_value_parm_die_table = NULL;
25679 generic_type_instances = NULL;
25680 frame_pointer_fb_offset = 0;
25681 frame_pointer_fb_offset_valid = false;
25682 base_types.release ();
25683 XDELETEVEC (producer_string);
25684 producer_string = NULL;
25685 }
25686
25687 #include "gt-dwarf2out.h"