]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dwarf2out.c
PR debug/38757 continued. Handle C11, C++11 and C++14.
[thirdparty/gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 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 "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "hashtab.h"
68 #include "hash-set.h"
69 #include "vec.h"
70 #include "machmode.h"
71 #include "hard-reg-set.h"
72 #include "input.h"
73 #include "function.h"
74 #include "emit-rtl.h"
75 #include "hash-table.h"
76 #include "version.h"
77 #include "flags.h"
78 #include "regs.h"
79 #include "rtlhash.h"
80 #include "insn-config.h"
81 #include "reload.h"
82 #include "output.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "hash-map.h"
98 #include "is-a.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "ira.h"
103 #include "lra.h"
104 #include "dumpfile.h"
105 #include "opts.h"
106 #include "tree-dfa.h"
107 #include "gdb/gdb-index.h"
108 #include "rtl-iter.h"
109
110 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
111 static rtx_insn *last_var_location_insn;
112 static rtx_insn *cached_next_real_insn;
113 static void dwarf2out_decl (tree);
114
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
117
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
130
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
138
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
143
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
147
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
153
154 /* A pointer to the base of a table of references to declaration
155 scopes. This table is a display which tracks the nesting
156 of declaration scopes at the current scope and containing
157 scopes. This table is used to find the proper place to
158 define type declaration DIE's. */
159 static GTY(()) vec<tree, va_gc> *decl_scope_table;
160
161 /* Pointers to various DWARF2 sections. */
162 static GTY(()) section *debug_info_section;
163 static GTY(()) section *debug_skeleton_info_section;
164 static GTY(()) section *debug_abbrev_section;
165 static GTY(()) section *debug_skeleton_abbrev_section;
166 static GTY(()) section *debug_aranges_section;
167 static GTY(()) section *debug_addr_section;
168 static GTY(()) section *debug_macinfo_section;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
179
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
182
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
187
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
190
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
194
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
198
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
206
207
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
212
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
214
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
221 };
222
223 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
224 {
225 typedef const char *compare_type;
226
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
229 };
230
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
232
233 /* With split_debug_info, both the comp_dir and dwo_name go in the
234 main object file, rather than the dwo, similar to the force_direct
235 parameter elsewhere but with additional complications:
236
237 1) The string is needed in both the main object file and the dwo.
238 That is, the comp_dir and dwo_name will appear in both places.
239
240 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
241 DW_FORM_GNU_str_index.
242
243 3) GCC chooses the form to use late, depending on the size and
244 reference count.
245
246 Rather than forcing the all debug string handling functions and
247 callers to deal with these complications, simply use a separate,
248 special-cased string table for any attribute that should go in the
249 main object file. This limits the complexity to just the places
250 that need it. */
251
252 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
253
254 static GTY(()) int dw2_string_counter;
255
256 /* True if the compilation unit places functions in more than one section. */
257 static GTY(()) bool have_multiple_function_sections = false;
258
259 /* Whether the default text and cold text sections have been used at all. */
260
261 static GTY(()) bool text_section_used = false;
262 static GTY(()) bool cold_text_section_used = false;
263
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
266
267 /* The DIE for C++14 'auto' in a function return type. */
268 static GTY(()) dw_die_ref auto_die;
269
270 /* The DIE for C++14 'decltype(auto)' in a function return type. */
271 static GTY(()) dw_die_ref decltype_auto_die;
272
273 /* Forward declarations for functions defined in this file. */
274
275 static char *stripattributes (const char *);
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
278
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
282
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
286
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
290
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
294
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
298
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
302
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
306
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
320 \f
321 /* Match the base name of a file to the base name of a compilation unit. */
322
323 static int
324 matches_main_base (const char *path)
325 {
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
330 {
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
336 }
337 return last_match;
338 }
339
340 #ifdef DEBUG_DEBUG_STRUCT
341
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
346 {
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
355
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
366 }
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
369
370 #else
371
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
374
375 #endif
376
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. */
379
380 static unsigned int
381 get_full_len (const wide_int &op)
382 {
383 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
384 / HOST_BITS_PER_WIDE_INT);
385 }
386
387 static bool
388 should_emit_struct_debug (tree type, enum debug_info_usage usage)
389 {
390 enum debug_struct_file criterion;
391 tree type_decl;
392 bool generic = lang_hooks.types.generic_p (type);
393
394 if (generic)
395 criterion = debug_struct_generic[usage];
396 else
397 criterion = debug_struct_ordinary[usage];
398
399 if (criterion == DINFO_STRUCT_FILE_NONE)
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 if (criterion == DINFO_STRUCT_FILE_ANY)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
403
404 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
405
406 if (type_decl != NULL)
407 {
408 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
410
411 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
413 }
414
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
416 }
417 \f
418 /* Return a pointer to a copy of the section string name S with all
419 attributes stripped off, and an asterisk prepended (for assemble_name). */
420
421 static inline char *
422 stripattributes (const char *s)
423 {
424 char *stripped = XNEWVEC (char, strlen (s) + 2);
425 char *p = stripped;
426
427 *p++ = '*';
428
429 while (*s && *s != ',')
430 *p++ = *s++;
431
432 *p = '\0';
433 return stripped;
434 }
435
436 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
437 switch to the data section instead, and write out a synthetic start label
438 for collect2 the first time around. */
439
440 static void
441 switch_to_eh_frame_section (bool back)
442 {
443 tree label;
444
445 #ifdef EH_FRAME_SECTION_NAME
446 if (eh_frame_section == 0)
447 {
448 int flags;
449
450 if (EH_TABLES_CAN_BE_READ_ONLY)
451 {
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
455
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
470 }
471 else
472 flags = SECTION_WRITE;
473 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
474 }
475 #endif /* EH_FRAME_SECTION_NAME */
476
477 if (eh_frame_section)
478 switch_to_section (eh_frame_section);
479 else
480 {
481 /* We have no special eh_frame section. Put the information in
482 the data section and emit special labels to guide collect2. */
483 switch_to_section (data_section);
484
485 if (!back)
486 {
487 label = get_file_function_name ("F");
488 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
489 targetm.asm_out.globalize_label (asm_out_file,
490 IDENTIFIER_POINTER (label));
491 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
492 }
493 }
494 }
495
496 /* Switch [BACK] to the eh or debug frame table section, depending on
497 FOR_EH. */
498
499 static void
500 switch_to_frame_table_section (int for_eh, bool back)
501 {
502 if (for_eh)
503 switch_to_eh_frame_section (back);
504 else
505 {
506 if (!debug_frame_section)
507 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
508 SECTION_DEBUG, NULL);
509 switch_to_section (debug_frame_section);
510 }
511 }
512
513 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
514
515 enum dw_cfi_oprnd_type
516 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
517 {
518 switch (cfi)
519 {
520 case DW_CFA_nop:
521 case DW_CFA_GNU_window_save:
522 case DW_CFA_remember_state:
523 case DW_CFA_restore_state:
524 return dw_cfi_oprnd_unused;
525
526 case DW_CFA_set_loc:
527 case DW_CFA_advance_loc1:
528 case DW_CFA_advance_loc2:
529 case DW_CFA_advance_loc4:
530 case DW_CFA_MIPS_advance_loc8:
531 return dw_cfi_oprnd_addr;
532
533 case DW_CFA_offset:
534 case DW_CFA_offset_extended:
535 case DW_CFA_def_cfa:
536 case DW_CFA_offset_extended_sf:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_restore:
539 case DW_CFA_restore_extended:
540 case DW_CFA_undefined:
541 case DW_CFA_same_value:
542 case DW_CFA_def_cfa_register:
543 case DW_CFA_register:
544 case DW_CFA_expression:
545 return dw_cfi_oprnd_reg_num;
546
547 case DW_CFA_def_cfa_offset:
548 case DW_CFA_GNU_args_size:
549 case DW_CFA_def_cfa_offset_sf:
550 return dw_cfi_oprnd_offset;
551
552 case DW_CFA_def_cfa_expression:
553 return dw_cfi_oprnd_loc;
554
555 default:
556 gcc_unreachable ();
557 }
558 }
559
560 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
561
562 enum dw_cfi_oprnd_type
563 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
564 {
565 switch (cfi)
566 {
567 case DW_CFA_def_cfa:
568 case DW_CFA_def_cfa_sf:
569 case DW_CFA_offset:
570 case DW_CFA_offset_extended_sf:
571 case DW_CFA_offset_extended:
572 return dw_cfi_oprnd_offset;
573
574 case DW_CFA_register:
575 return dw_cfi_oprnd_reg_num;
576
577 case DW_CFA_expression:
578 return dw_cfi_oprnd_loc;
579
580 default:
581 return dw_cfi_oprnd_unused;
582 }
583 }
584
585 /* Output one FDE. */
586
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
591 {
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[20], l2[20];
595
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
603 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
604 " indicating 64-bit DWARF extension");
605 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
606 "FDE Length");
607 ASM_OUTPUT_LABEL (asm_out_file, l1);
608
609 if (for_eh)
610 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
611 else
612 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
613 debug_frame_section, "FDE CIE offset");
614
615 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
616 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
617
618 if (for_eh)
619 {
620 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
621 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
622 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
623 "FDE initial location");
624 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
625 end, begin, "FDE address range");
626 }
627 else
628 {
629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
630 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
631 }
632
633 if (augmentation[0])
634 {
635 if (any_lsda_needed)
636 {
637 int size = size_of_encoded_value (lsda_encoding);
638
639 if (lsda_encoding == DW_EH_PE_aligned)
640 {
641 int offset = ( 4 /* Length */
642 + 4 /* CIE offset */
643 + 2 * size_of_encoded_value (fde_encoding)
644 + 1 /* Augmentation size */ );
645 int pad = -offset & (PTR_SIZE - 1);
646
647 size += pad;
648 gcc_assert (size_of_uleb128 (size) == 1);
649 }
650
651 dw2_asm_output_data_uleb128 (size, "Augmentation size");
652
653 if (fde->uses_eh_lsda)
654 {
655 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
656 fde->funcdef_number);
657 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
658 gen_rtx_SYMBOL_REF (Pmode, l1),
659 false,
660 "Language Specific Data Area");
661 }
662 else
663 {
664 if (lsda_encoding == DW_EH_PE_aligned)
665 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
666 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
667 "Language Specific Data Area (none)");
668 }
669 }
670 else
671 dw2_asm_output_data_uleb128 (0, "Augmentation size");
672 }
673
674 /* Loop through the Call Frame Instructions associated with this FDE. */
675 fde->dw_fde_current_label = begin;
676 {
677 size_t from, until, i;
678
679 from = 0;
680 until = vec_safe_length (fde->dw_fde_cfi);
681
682 if (fde->dw_fde_second_begin == NULL)
683 ;
684 else if (!second)
685 until = fde->dw_fde_switch_cfi_index;
686 else
687 from = fde->dw_fde_switch_cfi_index;
688
689 for (i = from; i < until; i++)
690 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
691 }
692
693 /* If we are to emit a ref/link from function bodies to their frame tables,
694 do it now. This is typically performed to make sure that tables
695 associated with functions are dragged with them and not discarded in
696 garbage collecting links. We need to do this on a per function basis to
697 cope with -ffunction-sections. */
698
699 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
700 /* Switch to the function section, emit the ref to the tables, and
701 switch *back* into the table section. */
702 switch_to_section (function_section (fde->decl));
703 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
704 switch_to_frame_table_section (for_eh, true);
705 #endif
706
707 /* Pad the FDE out to an address sized boundary. */
708 ASM_OUTPUT_ALIGN (asm_out_file,
709 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
710 ASM_OUTPUT_LABEL (asm_out_file, l2);
711
712 j += 2;
713 }
714
715 /* Return true if frame description entry FDE is needed for EH. */
716
717 static bool
718 fde_needed_for_eh_p (dw_fde_ref fde)
719 {
720 if (flag_asynchronous_unwind_tables)
721 return true;
722
723 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
724 return true;
725
726 if (fde->uses_eh_lsda)
727 return true;
728
729 /* If exceptions are enabled, we have collected nothrow info. */
730 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
731 return false;
732
733 return true;
734 }
735
736 /* Output the call frame information used to record information
737 that relates to calculating the frame pointer, and records the
738 location of saved registers. */
739
740 static void
741 output_call_frame_info (int for_eh)
742 {
743 unsigned int i;
744 dw_fde_ref fde;
745 dw_cfi_ref cfi;
746 char l1[20], l2[20], section_start_label[20];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
756
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
760
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
764
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
772 {
773 bool any_eh_needed = false;
774
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
776 {
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
783 }
784
785 if (!any_eh_needed)
786 return;
787 }
788
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
792
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
795
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
798
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
803 dw2_asm_output_data (4, 0xffffffff,
804 "Initial length escape value indicating 64-bit DWARF extension");
805 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
806 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
808
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
814
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
823
824 augmentation[0] = 0;
825 augmentation_size = 0;
826
827 personality = current_unit_personality;
828 if (for_eh)
829 {
830 char *p;
831
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
841
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
845
846 p = augmentation + 1;
847 if (personality)
848 {
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
852 }
853 if (any_lsda_needed)
854 {
855 *p++ = 'L';
856 augmentation_size += 1;
857 }
858 if (fde_encoding != DW_EH_PE_absptr)
859 {
860 *p++ = 'R';
861 augmentation_size += 1;
862 }
863 if (p > augmentation + 1)
864 {
865 augmentation[0] = 'z';
866 *p = '\0';
867 }
868
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
871 {
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
882
883 augmentation_size += pad;
884
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
888 }
889 }
890
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
893 {
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
896 }
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
900
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
905
906 if (augmentation[0])
907 {
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
910 {
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
916 }
917
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
921
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
925 }
926
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
929
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
934
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
937 {
938 unsigned int k;
939
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
943
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
947 }
948
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
951
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
955 }
956
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
958
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
961 {
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
965
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
967
968 if (personality)
969 {
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
972
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
979
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
983 }
984
985 if (crtl->uses_eh_lsda)
986 {
987 char lab[20];
988
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
994
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
997
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1001 }
1002 }
1003
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1006
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1009 {
1010 dw_fde_ref fde;
1011
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1021
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1025
1026 return fde;
1027 }
1028
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1031
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1035 {
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1041
1042 current_function_func_begin_label = NULL;
1043
1044 do_frame = dwarf2out_do_frame ();
1045
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1052
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1061
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1065
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1073
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1079
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1086
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1090 {
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1094
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1101 }
1102 }
1103
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1107
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1111 {
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1113
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 }
1122
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1126
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1130 {
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1133
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1136
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 }
1145
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1149
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1153 {
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1156
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1159
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1162
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1172 }
1173
1174 void
1175 dwarf2out_frame_finish (void)
1176 {
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1180
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1185 }
1186
1187 /* Note that the current function section is being used for code. */
1188
1189 static void
1190 dwarf2out_note_section_used (void)
1191 {
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1197 }
1198
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1201
1202 void
1203 dwarf2out_switch_text_section (void)
1204 {
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1207
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1209
1210 if (!in_cold_section_p)
1211 {
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1215 }
1216 else
1217 {
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1221 }
1222 have_multiple_function_sections = true;
1223
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1227
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1230
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1234
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1238
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1241
1242 var_location_switch_text_section ();
1243
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1246 }
1247 \f
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1250
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1255 };
1256
1257 typedef struct GTY(()) deferred_locations_struct
1258 {
1259 tree variable;
1260 dw_die_ref die;
1261 } deferred_locations;
1262
1263
1264 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1265
1266
1267 /* Describe an entry into the .debug_addr section. */
1268
1269 enum ate_kind {
1270 ate_kind_rtx,
1271 ate_kind_rtx_dtprel,
1272 ate_kind_label
1273 };
1274
1275 typedef struct GTY((for_user)) addr_table_entry_struct {
1276 enum ate_kind kind;
1277 unsigned int refcount;
1278 unsigned int index;
1279 union addr_table_entry_struct_union
1280 {
1281 rtx GTY ((tag ("0"))) rtl;
1282 char * GTY ((tag ("1"))) label;
1283 }
1284 GTY ((desc ("%1.kind"))) addr;
1285 }
1286 addr_table_entry;
1287
1288 /* Location lists are ranges + location descriptions for that range,
1289 so you can track variables that are in different places over
1290 their entire life. */
1291 typedef struct GTY(()) dw_loc_list_struct {
1292 dw_loc_list_ref dw_loc_next;
1293 const char *begin; /* Label and addr_entry for start of range */
1294 addr_table_entry *begin_entry;
1295 const char *end; /* Label for end of range */
1296 char *ll_symbol; /* Label for beginning of location list.
1297 Only on head of list */
1298 const char *section; /* Section this loclist is relative to */
1299 dw_loc_descr_ref expr;
1300 hashval_t hash;
1301 /* True if all addresses in this and subsequent lists are known to be
1302 resolved. */
1303 bool resolved_addr;
1304 /* True if this list has been replaced by dw_loc_next. */
1305 bool replaced;
1306 bool emitted;
1307 /* True if the range should be emitted even if begin and end
1308 are the same. */
1309 bool force;
1310 } dw_loc_list_node;
1311
1312 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1313
1314 /* Convert a DWARF stack opcode into its string name. */
1315
1316 static const char *
1317 dwarf_stack_op_name (unsigned int op)
1318 {
1319 const char *name = get_DW_OP_name (op);
1320
1321 if (name != NULL)
1322 return name;
1323
1324 return "OP_<unknown>";
1325 }
1326
1327 /* Return a pointer to a newly allocated location description. Location
1328 descriptions are simple expression terms that can be strung
1329 together to form more complicated location (address) descriptions. */
1330
1331 static inline dw_loc_descr_ref
1332 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1333 unsigned HOST_WIDE_INT oprnd2)
1334 {
1335 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1336
1337 descr->dw_loc_opc = op;
1338 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1339 descr->dw_loc_oprnd1.val_entry = NULL;
1340 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1341 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1342 descr->dw_loc_oprnd2.val_entry = NULL;
1343 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1344
1345 return descr;
1346 }
1347
1348 /* Return a pointer to a newly allocated location description for
1349 REG and OFFSET. */
1350
1351 static inline dw_loc_descr_ref
1352 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1353 {
1354 if (reg <= 31)
1355 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1356 offset, 0);
1357 else
1358 return new_loc_descr (DW_OP_bregx, reg, offset);
1359 }
1360
1361 /* Add a location description term to a location description expression. */
1362
1363 static inline void
1364 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1365 {
1366 dw_loc_descr_ref *d;
1367
1368 /* Find the end of the chain. */
1369 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1370 ;
1371
1372 *d = descr;
1373 }
1374
1375 /* Compare two location operands for exact equality. */
1376
1377 static bool
1378 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1379 {
1380 if (a->val_class != b->val_class)
1381 return false;
1382 switch (a->val_class)
1383 {
1384 case dw_val_class_none:
1385 return true;
1386 case dw_val_class_addr:
1387 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1388
1389 case dw_val_class_offset:
1390 case dw_val_class_unsigned_const:
1391 case dw_val_class_const:
1392 case dw_val_class_range_list:
1393 case dw_val_class_lineptr:
1394 case dw_val_class_macptr:
1395 /* These are all HOST_WIDE_INT, signed or unsigned. */
1396 return a->v.val_unsigned == b->v.val_unsigned;
1397
1398 case dw_val_class_loc:
1399 return a->v.val_loc == b->v.val_loc;
1400 case dw_val_class_loc_list:
1401 return a->v.val_loc_list == b->v.val_loc_list;
1402 case dw_val_class_die_ref:
1403 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1404 case dw_val_class_fde_ref:
1405 return a->v.val_fde_index == b->v.val_fde_index;
1406 case dw_val_class_lbl_id:
1407 case dw_val_class_high_pc:
1408 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1409 case dw_val_class_str:
1410 return a->v.val_str == b->v.val_str;
1411 case dw_val_class_flag:
1412 return a->v.val_flag == b->v.val_flag;
1413 case dw_val_class_file:
1414 return a->v.val_file == b->v.val_file;
1415 case dw_val_class_decl_ref:
1416 return a->v.val_decl_ref == b->v.val_decl_ref;
1417
1418 case dw_val_class_const_double:
1419 return (a->v.val_double.high == b->v.val_double.high
1420 && a->v.val_double.low == b->v.val_double.low);
1421
1422 case dw_val_class_wide_int:
1423 return *a->v.val_wide == *b->v.val_wide;
1424
1425 case dw_val_class_vec:
1426 {
1427 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1428 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1429
1430 return (a_len == b_len
1431 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1432 }
1433
1434 case dw_val_class_data8:
1435 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1436
1437 case dw_val_class_vms_delta:
1438 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1439 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1440 }
1441 gcc_unreachable ();
1442 }
1443
1444 /* Compare two location atoms for exact equality. */
1445
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1448 {
1449 if (a->dw_loc_opc != b->dw_loc_opc)
1450 return false;
1451
1452 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453 address size, but since we always allocate cleared storage it
1454 should be zero for other types of locations. */
1455 if (a->dtprel != b->dtprel)
1456 return false;
1457
1458 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1460 }
1461
1462 /* Compare two complete location expressions for exact equality. */
1463
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1466 {
1467 while (1)
1468 {
1469 if (a == b)
1470 return true;
1471 if (a == NULL || b == NULL)
1472 return false;
1473 if (!loc_descr_equal_p_1 (a, b))
1474 return false;
1475
1476 a = a->dw_loc_next;
1477 b = b->dw_loc_next;
1478 }
1479 }
1480
1481
1482 /* Add a constant OFFSET to a location expression. */
1483
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1486 {
1487 dw_loc_descr_ref loc;
1488 HOST_WIDE_INT *p;
1489
1490 gcc_assert (*list_head != NULL);
1491
1492 if (!offset)
1493 return;
1494
1495 /* Find the end of the chain. */
1496 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1497 ;
1498
1499 p = NULL;
1500 if (loc->dw_loc_opc == DW_OP_fbreg
1501 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502 p = &loc->dw_loc_oprnd1.v.val_int;
1503 else if (loc->dw_loc_opc == DW_OP_bregx)
1504 p = &loc->dw_loc_oprnd2.v.val_int;
1505
1506 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507 offset. Don't optimize if an signed integer overflow would happen. */
1508 if (p != NULL
1509 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511 *p += offset;
1512
1513 else if (offset > 0)
1514 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1515
1516 else
1517 {
1518 loc->dw_loc_next = int_loc_descriptor (-offset);
1519 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1520 }
1521 }
1522
1523 /* Add a constant OFFSET to a location list. */
1524
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1527 {
1528 dw_loc_list_ref d;
1529 for (d = list_head; d != NULL; d = d->dw_loc_next)
1530 loc_descr_plus_const (&d->expr, offset);
1531 }
1532
1533 #define DWARF_REF_SIZE \
1534 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1535
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1537
1538 /* Return the size of a location descriptor. */
1539
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1542 {
1543 unsigned long size = 1;
1544
1545 switch (loc->dw_loc_opc)
1546 {
1547 case DW_OP_addr:
1548 size += DWARF2_ADDR_SIZE;
1549 break;
1550 case DW_OP_GNU_addr_index:
1551 case DW_OP_GNU_const_index:
1552 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554 break;
1555 case DW_OP_const1u:
1556 case DW_OP_const1s:
1557 size += 1;
1558 break;
1559 case DW_OP_const2u:
1560 case DW_OP_const2s:
1561 size += 2;
1562 break;
1563 case DW_OP_const4u:
1564 case DW_OP_const4s:
1565 size += 4;
1566 break;
1567 case DW_OP_const8u:
1568 case DW_OP_const8s:
1569 size += 8;
1570 break;
1571 case DW_OP_constu:
1572 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573 break;
1574 case DW_OP_consts:
1575 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576 break;
1577 case DW_OP_pick:
1578 size += 1;
1579 break;
1580 case DW_OP_plus_uconst:
1581 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582 break;
1583 case DW_OP_skip:
1584 case DW_OP_bra:
1585 size += 2;
1586 break;
1587 case DW_OP_breg0:
1588 case DW_OP_breg1:
1589 case DW_OP_breg2:
1590 case DW_OP_breg3:
1591 case DW_OP_breg4:
1592 case DW_OP_breg5:
1593 case DW_OP_breg6:
1594 case DW_OP_breg7:
1595 case DW_OP_breg8:
1596 case DW_OP_breg9:
1597 case DW_OP_breg10:
1598 case DW_OP_breg11:
1599 case DW_OP_breg12:
1600 case DW_OP_breg13:
1601 case DW_OP_breg14:
1602 case DW_OP_breg15:
1603 case DW_OP_breg16:
1604 case DW_OP_breg17:
1605 case DW_OP_breg18:
1606 case DW_OP_breg19:
1607 case DW_OP_breg20:
1608 case DW_OP_breg21:
1609 case DW_OP_breg22:
1610 case DW_OP_breg23:
1611 case DW_OP_breg24:
1612 case DW_OP_breg25:
1613 case DW_OP_breg26:
1614 case DW_OP_breg27:
1615 case DW_OP_breg28:
1616 case DW_OP_breg29:
1617 case DW_OP_breg30:
1618 case DW_OP_breg31:
1619 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620 break;
1621 case DW_OP_regx:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_fbreg:
1625 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626 break;
1627 case DW_OP_bregx:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630 break;
1631 case DW_OP_piece:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633 break;
1634 case DW_OP_bit_piece:
1635 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637 break;
1638 case DW_OP_deref_size:
1639 case DW_OP_xderef_size:
1640 size += 1;
1641 break;
1642 case DW_OP_call2:
1643 size += 2;
1644 break;
1645 case DW_OP_call4:
1646 size += 4;
1647 break;
1648 case DW_OP_call_ref:
1649 size += DWARF_REF_SIZE;
1650 break;
1651 case DW_OP_implicit_value:
1652 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653 + loc->dw_loc_oprnd1.v.val_unsigned;
1654 break;
1655 case DW_OP_GNU_implicit_pointer:
1656 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657 break;
1658 case DW_OP_GNU_entry_value:
1659 {
1660 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661 size += size_of_uleb128 (op_size) + op_size;
1662 break;
1663 }
1664 case DW_OP_GNU_const_type:
1665 {
1666 unsigned long o
1667 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668 size += size_of_uleb128 (o) + 1;
1669 switch (loc->dw_loc_oprnd2.val_class)
1670 {
1671 case dw_val_class_vec:
1672 size += loc->dw_loc_oprnd2.v.val_vec.length
1673 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674 break;
1675 case dw_val_class_const:
1676 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677 break;
1678 case dw_val_class_const_double:
1679 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680 break;
1681 case dw_val_class_wide_int:
1682 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684 break;
1685 default:
1686 gcc_unreachable ();
1687 }
1688 break;
1689 }
1690 case DW_OP_GNU_regval_type:
1691 {
1692 unsigned long o
1693 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695 + size_of_uleb128 (o);
1696 }
1697 break;
1698 case DW_OP_GNU_deref_type:
1699 {
1700 unsigned long o
1701 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702 size += 1 + size_of_uleb128 (o);
1703 }
1704 break;
1705 case DW_OP_GNU_convert:
1706 case DW_OP_GNU_reinterpret:
1707 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709 else
1710 {
1711 unsigned long o
1712 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713 size += size_of_uleb128 (o);
1714 }
1715 break;
1716 case DW_OP_GNU_parameter_ref:
1717 size += 4;
1718 break;
1719 default:
1720 break;
1721 }
1722
1723 return size;
1724 }
1725
1726 /* Return the size of a series of location descriptors. */
1727
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1730 {
1731 dw_loc_descr_ref l;
1732 unsigned long size;
1733
1734 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735 field, to avoid writing to a PCH file. */
1736 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1737 {
1738 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739 break;
1740 size += size_of_loc_descr (l);
1741 }
1742 if (! l)
1743 return size;
1744
1745 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1746 {
1747 l->dw_loc_addr = size;
1748 size += size_of_loc_descr (l);
1749 }
1750
1751 return size;
1752 }
1753
1754 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1755 static void get_ref_die_offset_label (char *, dw_die_ref);
1756 static unsigned long int get_ref_die_offset (dw_die_ref);
1757
1758 /* Output location description stack opcode's operands (if any).
1759 The for_eh_or_skip parameter controls whether register numbers are
1760 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1761 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1762 info). This should be suppressed for the cases that have not been converted
1763 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1764
1765 static void
1766 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1767 {
1768 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1769 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1770
1771 switch (loc->dw_loc_opc)
1772 {
1773 #ifdef DWARF2_DEBUGGING_INFO
1774 case DW_OP_const2u:
1775 case DW_OP_const2s:
1776 dw2_asm_output_data (2, val1->v.val_int, NULL);
1777 break;
1778 case DW_OP_const4u:
1779 if (loc->dtprel)
1780 {
1781 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1782 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1783 val1->v.val_addr);
1784 fputc ('\n', asm_out_file);
1785 break;
1786 }
1787 /* FALLTHRU */
1788 case DW_OP_const4s:
1789 dw2_asm_output_data (4, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_const8u:
1792 if (loc->dtprel)
1793 {
1794 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1795 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1796 val1->v.val_addr);
1797 fputc ('\n', asm_out_file);
1798 break;
1799 }
1800 /* FALLTHRU */
1801 case DW_OP_const8s:
1802 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1803 dw2_asm_output_data (8, val1->v.val_int, NULL);
1804 break;
1805 case DW_OP_skip:
1806 case DW_OP_bra:
1807 {
1808 int offset;
1809
1810 gcc_assert (val1->val_class == dw_val_class_loc);
1811 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1812
1813 dw2_asm_output_data (2, offset, NULL);
1814 }
1815 break;
1816 case DW_OP_implicit_value:
1817 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1818 switch (val2->val_class)
1819 {
1820 case dw_val_class_const:
1821 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1822 break;
1823 case dw_val_class_vec:
1824 {
1825 unsigned int elt_size = val2->v.val_vec.elt_size;
1826 unsigned int len = val2->v.val_vec.length;
1827 unsigned int i;
1828 unsigned char *p;
1829
1830 if (elt_size > sizeof (HOST_WIDE_INT))
1831 {
1832 elt_size /= 2;
1833 len *= 2;
1834 }
1835 for (i = 0, p = val2->v.val_vec.array;
1836 i < len;
1837 i++, p += elt_size)
1838 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1839 "fp or vector constant word %u", i);
1840 }
1841 break;
1842 case dw_val_class_const_double:
1843 {
1844 unsigned HOST_WIDE_INT first, second;
1845
1846 if (WORDS_BIG_ENDIAN)
1847 {
1848 first = val2->v.val_double.high;
1849 second = val2->v.val_double.low;
1850 }
1851 else
1852 {
1853 first = val2->v.val_double.low;
1854 second = val2->v.val_double.high;
1855 }
1856 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857 first, NULL);
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 second, NULL);
1860 }
1861 break;
1862 case dw_val_class_wide_int:
1863 {
1864 int i;
1865 int len = get_full_len (*val2->v.val_wide);
1866 if (WORDS_BIG_ENDIAN)
1867 for (i = len - 1; i >= 0; --i)
1868 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1869 val2->v.val_wide->elt (i), NULL);
1870 else
1871 for (i = 0; i < len; ++i)
1872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 val2->v.val_wide->elt (i), NULL);
1874 }
1875 break;
1876 case dw_val_class_addr:
1877 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1878 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1879 break;
1880 default:
1881 gcc_unreachable ();
1882 }
1883 break;
1884 #else
1885 case DW_OP_const2u:
1886 case DW_OP_const2s:
1887 case DW_OP_const4u:
1888 case DW_OP_const4s:
1889 case DW_OP_const8u:
1890 case DW_OP_const8s:
1891 case DW_OP_skip:
1892 case DW_OP_bra:
1893 case DW_OP_implicit_value:
1894 /* We currently don't make any attempt to make sure these are
1895 aligned properly like we do for the main unwind info, so
1896 don't support emitting things larger than a byte if we're
1897 only doing unwinding. */
1898 gcc_unreachable ();
1899 #endif
1900 case DW_OP_const1u:
1901 case DW_OP_const1s:
1902 dw2_asm_output_data (1, val1->v.val_int, NULL);
1903 break;
1904 case DW_OP_constu:
1905 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1906 break;
1907 case DW_OP_consts:
1908 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1909 break;
1910 case DW_OP_pick:
1911 dw2_asm_output_data (1, val1->v.val_int, NULL);
1912 break;
1913 case DW_OP_plus_uconst:
1914 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1915 break;
1916 case DW_OP_breg0:
1917 case DW_OP_breg1:
1918 case DW_OP_breg2:
1919 case DW_OP_breg3:
1920 case DW_OP_breg4:
1921 case DW_OP_breg5:
1922 case DW_OP_breg6:
1923 case DW_OP_breg7:
1924 case DW_OP_breg8:
1925 case DW_OP_breg9:
1926 case DW_OP_breg10:
1927 case DW_OP_breg11:
1928 case DW_OP_breg12:
1929 case DW_OP_breg13:
1930 case DW_OP_breg14:
1931 case DW_OP_breg15:
1932 case DW_OP_breg16:
1933 case DW_OP_breg17:
1934 case DW_OP_breg18:
1935 case DW_OP_breg19:
1936 case DW_OP_breg20:
1937 case DW_OP_breg21:
1938 case DW_OP_breg22:
1939 case DW_OP_breg23:
1940 case DW_OP_breg24:
1941 case DW_OP_breg25:
1942 case DW_OP_breg26:
1943 case DW_OP_breg27:
1944 case DW_OP_breg28:
1945 case DW_OP_breg29:
1946 case DW_OP_breg30:
1947 case DW_OP_breg31:
1948 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1949 break;
1950 case DW_OP_regx:
1951 {
1952 unsigned r = val1->v.val_unsigned;
1953 if (for_eh_or_skip >= 0)
1954 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1955 gcc_assert (size_of_uleb128 (r)
1956 == size_of_uleb128 (val1->v.val_unsigned));
1957 dw2_asm_output_data_uleb128 (r, NULL);
1958 }
1959 break;
1960 case DW_OP_fbreg:
1961 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_bregx:
1964 {
1965 unsigned r = val1->v.val_unsigned;
1966 if (for_eh_or_skip >= 0)
1967 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1968 gcc_assert (size_of_uleb128 (r)
1969 == size_of_uleb128 (val1->v.val_unsigned));
1970 dw2_asm_output_data_uleb128 (r, NULL);
1971 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1972 }
1973 break;
1974 case DW_OP_piece:
1975 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1976 break;
1977 case DW_OP_bit_piece:
1978 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1979 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1980 break;
1981 case DW_OP_deref_size:
1982 case DW_OP_xderef_size:
1983 dw2_asm_output_data (1, val1->v.val_int, NULL);
1984 break;
1985
1986 case DW_OP_addr:
1987 if (loc->dtprel)
1988 {
1989 if (targetm.asm_out.output_dwarf_dtprel)
1990 {
1991 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1992 DWARF2_ADDR_SIZE,
1993 val1->v.val_addr);
1994 fputc ('\n', asm_out_file);
1995 }
1996 else
1997 gcc_unreachable ();
1998 }
1999 else
2000 {
2001 #ifdef DWARF2_DEBUGGING_INFO
2002 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2003 #else
2004 gcc_unreachable ();
2005 #endif
2006 }
2007 break;
2008
2009 case DW_OP_GNU_addr_index:
2010 case DW_OP_GNU_const_index:
2011 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2012 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2013 "(index into .debug_addr)");
2014 break;
2015
2016 case DW_OP_GNU_implicit_pointer:
2017 {
2018 char label[MAX_ARTIFICIAL_LABEL_BYTES
2019 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2020 gcc_assert (val1->val_class == dw_val_class_die_ref);
2021 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2022 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2023 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2024 }
2025 break;
2026
2027 case DW_OP_GNU_entry_value:
2028 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2029 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2030 break;
2031
2032 case DW_OP_GNU_const_type:
2033 {
2034 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2035 gcc_assert (o);
2036 dw2_asm_output_data_uleb128 (o, NULL);
2037 switch (val2->val_class)
2038 {
2039 case dw_val_class_const:
2040 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2041 dw2_asm_output_data (1, l, NULL);
2042 dw2_asm_output_data (l, val2->v.val_int, NULL);
2043 break;
2044 case dw_val_class_vec:
2045 {
2046 unsigned int elt_size = val2->v.val_vec.elt_size;
2047 unsigned int len = val2->v.val_vec.length;
2048 unsigned int i;
2049 unsigned char *p;
2050
2051 l = len * elt_size;
2052 dw2_asm_output_data (1, l, NULL);
2053 if (elt_size > sizeof (HOST_WIDE_INT))
2054 {
2055 elt_size /= 2;
2056 len *= 2;
2057 }
2058 for (i = 0, p = val2->v.val_vec.array;
2059 i < len;
2060 i++, p += elt_size)
2061 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2062 "fp or vector constant word %u", i);
2063 }
2064 break;
2065 case dw_val_class_const_double:
2066 {
2067 unsigned HOST_WIDE_INT first, second;
2068 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2069
2070 dw2_asm_output_data (1, 2 * l, NULL);
2071 if (WORDS_BIG_ENDIAN)
2072 {
2073 first = val2->v.val_double.high;
2074 second = val2->v.val_double.low;
2075 }
2076 else
2077 {
2078 first = val2->v.val_double.low;
2079 second = val2->v.val_double.high;
2080 }
2081 dw2_asm_output_data (l, first, NULL);
2082 dw2_asm_output_data (l, second, NULL);
2083 }
2084 break;
2085 case dw_val_class_wide_int:
2086 {
2087 int i;
2088 int len = get_full_len (*val2->v.val_wide);
2089 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2090
2091 dw2_asm_output_data (1, len * l, NULL);
2092 if (WORDS_BIG_ENDIAN)
2093 for (i = len - 1; i >= 0; --i)
2094 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2095 else
2096 for (i = 0; i < len; ++i)
2097 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2098 }
2099 break;
2100 default:
2101 gcc_unreachable ();
2102 }
2103 }
2104 break;
2105 case DW_OP_GNU_regval_type:
2106 {
2107 unsigned r = val1->v.val_unsigned;
2108 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2109 gcc_assert (o);
2110 if (for_eh_or_skip >= 0)
2111 {
2112 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2113 gcc_assert (size_of_uleb128 (r)
2114 == size_of_uleb128 (val1->v.val_unsigned));
2115 }
2116 dw2_asm_output_data_uleb128 (r, NULL);
2117 dw2_asm_output_data_uleb128 (o, NULL);
2118 }
2119 break;
2120 case DW_OP_GNU_deref_type:
2121 {
2122 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123 gcc_assert (o);
2124 dw2_asm_output_data (1, val1->v.val_int, NULL);
2125 dw2_asm_output_data_uleb128 (o, NULL);
2126 }
2127 break;
2128 case DW_OP_GNU_convert:
2129 case DW_OP_GNU_reinterpret:
2130 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2131 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2132 else
2133 {
2134 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2135 gcc_assert (o);
2136 dw2_asm_output_data_uleb128 (o, NULL);
2137 }
2138 break;
2139
2140 case DW_OP_GNU_parameter_ref:
2141 {
2142 unsigned long o;
2143 gcc_assert (val1->val_class == dw_val_class_die_ref);
2144 o = get_ref_die_offset (val1->v.val_die_ref.die);
2145 dw2_asm_output_data (4, o, NULL);
2146 }
2147 break;
2148
2149 default:
2150 /* Other codes have no operands. */
2151 break;
2152 }
2153 }
2154
2155 /* Output a sequence of location operations.
2156 The for_eh_or_skip parameter controls whether register numbers are
2157 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2158 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2159 info). This should be suppressed for the cases that have not been converted
2160 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2161
2162 void
2163 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2164 {
2165 for (; loc != NULL; loc = loc->dw_loc_next)
2166 {
2167 enum dwarf_location_atom opc = loc->dw_loc_opc;
2168 /* Output the opcode. */
2169 if (for_eh_or_skip >= 0
2170 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2171 {
2172 unsigned r = (opc - DW_OP_breg0);
2173 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2174 gcc_assert (r <= 31);
2175 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2176 }
2177 else if (for_eh_or_skip >= 0
2178 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2179 {
2180 unsigned r = (opc - DW_OP_reg0);
2181 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2182 gcc_assert (r <= 31);
2183 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2184 }
2185
2186 dw2_asm_output_data (1, opc,
2187 "%s", dwarf_stack_op_name (opc));
2188
2189 /* Output the operand(s) (if any). */
2190 output_loc_operands (loc, for_eh_or_skip);
2191 }
2192 }
2193
2194 /* Output location description stack opcode's operands (if any).
2195 The output is single bytes on a line, suitable for .cfi_escape. */
2196
2197 static void
2198 output_loc_operands_raw (dw_loc_descr_ref loc)
2199 {
2200 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2201 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2202
2203 switch (loc->dw_loc_opc)
2204 {
2205 case DW_OP_addr:
2206 case DW_OP_GNU_addr_index:
2207 case DW_OP_GNU_const_index:
2208 case DW_OP_implicit_value:
2209 /* We cannot output addresses in .cfi_escape, only bytes. */
2210 gcc_unreachable ();
2211
2212 case DW_OP_const1u:
2213 case DW_OP_const1s:
2214 case DW_OP_pick:
2215 case DW_OP_deref_size:
2216 case DW_OP_xderef_size:
2217 fputc (',', asm_out_file);
2218 dw2_asm_output_data_raw (1, val1->v.val_int);
2219 break;
2220
2221 case DW_OP_const2u:
2222 case DW_OP_const2s:
2223 fputc (',', asm_out_file);
2224 dw2_asm_output_data_raw (2, val1->v.val_int);
2225 break;
2226
2227 case DW_OP_const4u:
2228 case DW_OP_const4s:
2229 fputc (',', asm_out_file);
2230 dw2_asm_output_data_raw (4, val1->v.val_int);
2231 break;
2232
2233 case DW_OP_const8u:
2234 case DW_OP_const8s:
2235 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2236 fputc (',', asm_out_file);
2237 dw2_asm_output_data_raw (8, val1->v.val_int);
2238 break;
2239
2240 case DW_OP_skip:
2241 case DW_OP_bra:
2242 {
2243 int offset;
2244
2245 gcc_assert (val1->val_class == dw_val_class_loc);
2246 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2247
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_raw (2, offset);
2250 }
2251 break;
2252
2253 case DW_OP_regx:
2254 {
2255 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2256 gcc_assert (size_of_uleb128 (r)
2257 == size_of_uleb128 (val1->v.val_unsigned));
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_uleb128_raw (r);
2260 }
2261 break;
2262
2263 case DW_OP_constu:
2264 case DW_OP_plus_uconst:
2265 case DW_OP_piece:
2266 fputc (',', asm_out_file);
2267 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2268 break;
2269
2270 case DW_OP_bit_piece:
2271 fputc (',', asm_out_file);
2272 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2273 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2274 break;
2275
2276 case DW_OP_consts:
2277 case DW_OP_breg0:
2278 case DW_OP_breg1:
2279 case DW_OP_breg2:
2280 case DW_OP_breg3:
2281 case DW_OP_breg4:
2282 case DW_OP_breg5:
2283 case DW_OP_breg6:
2284 case DW_OP_breg7:
2285 case DW_OP_breg8:
2286 case DW_OP_breg9:
2287 case DW_OP_breg10:
2288 case DW_OP_breg11:
2289 case DW_OP_breg12:
2290 case DW_OP_breg13:
2291 case DW_OP_breg14:
2292 case DW_OP_breg15:
2293 case DW_OP_breg16:
2294 case DW_OP_breg17:
2295 case DW_OP_breg18:
2296 case DW_OP_breg19:
2297 case DW_OP_breg20:
2298 case DW_OP_breg21:
2299 case DW_OP_breg22:
2300 case DW_OP_breg23:
2301 case DW_OP_breg24:
2302 case DW_OP_breg25:
2303 case DW_OP_breg26:
2304 case DW_OP_breg27:
2305 case DW_OP_breg28:
2306 case DW_OP_breg29:
2307 case DW_OP_breg30:
2308 case DW_OP_breg31:
2309 case DW_OP_fbreg:
2310 fputc (',', asm_out_file);
2311 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2312 break;
2313
2314 case DW_OP_bregx:
2315 {
2316 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 fputc (',', asm_out_file);
2320 dw2_asm_output_data_uleb128_raw (r);
2321 fputc (',', asm_out_file);
2322 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2323 }
2324 break;
2325
2326 case DW_OP_GNU_implicit_pointer:
2327 case DW_OP_GNU_entry_value:
2328 case DW_OP_GNU_const_type:
2329 case DW_OP_GNU_regval_type:
2330 case DW_OP_GNU_deref_type:
2331 case DW_OP_GNU_convert:
2332 case DW_OP_GNU_reinterpret:
2333 case DW_OP_GNU_parameter_ref:
2334 gcc_unreachable ();
2335 break;
2336
2337 default:
2338 /* Other codes have no operands. */
2339 break;
2340 }
2341 }
2342
2343 void
2344 output_loc_sequence_raw (dw_loc_descr_ref loc)
2345 {
2346 while (1)
2347 {
2348 enum dwarf_location_atom opc = loc->dw_loc_opc;
2349 /* Output the opcode. */
2350 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2351 {
2352 unsigned r = (opc - DW_OP_breg0);
2353 r = DWARF2_FRAME_REG_OUT (r, 1);
2354 gcc_assert (r <= 31);
2355 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2356 }
2357 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2358 {
2359 unsigned r = (opc - DW_OP_reg0);
2360 r = DWARF2_FRAME_REG_OUT (r, 1);
2361 gcc_assert (r <= 31);
2362 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2363 }
2364 /* Output the opcode. */
2365 fprintf (asm_out_file, "%#x", opc);
2366 output_loc_operands_raw (loc);
2367
2368 if (!loc->dw_loc_next)
2369 break;
2370 loc = loc->dw_loc_next;
2371
2372 fputc (',', asm_out_file);
2373 }
2374 }
2375
2376 /* This function builds a dwarf location descriptor sequence from a
2377 dw_cfa_location, adding the given OFFSET to the result of the
2378 expression. */
2379
2380 struct dw_loc_descr_node *
2381 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2382 {
2383 struct dw_loc_descr_node *head, *tmp;
2384
2385 offset += cfa->offset;
2386
2387 if (cfa->indirect)
2388 {
2389 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2390 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2391 head->dw_loc_oprnd1.val_entry = NULL;
2392 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2393 add_loc_descr (&head, tmp);
2394 if (offset != 0)
2395 {
2396 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2397 add_loc_descr (&head, tmp);
2398 }
2399 }
2400 else
2401 head = new_reg_loc_descr (cfa->reg, offset);
2402
2403 return head;
2404 }
2405
2406 /* This function builds a dwarf location descriptor sequence for
2407 the address at OFFSET from the CFA when stack is aligned to
2408 ALIGNMENT byte. */
2409
2410 struct dw_loc_descr_node *
2411 build_cfa_aligned_loc (dw_cfa_location *cfa,
2412 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2413 {
2414 struct dw_loc_descr_node *head;
2415 unsigned int dwarf_fp
2416 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2417
2418 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2419 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2420 {
2421 head = new_reg_loc_descr (dwarf_fp, 0);
2422 add_loc_descr (&head, int_loc_descriptor (alignment));
2423 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2424 loc_descr_plus_const (&head, offset);
2425 }
2426 else
2427 head = new_reg_loc_descr (dwarf_fp, offset);
2428 return head;
2429 }
2430 \f
2431 /* And now, the support for symbolic debugging information. */
2432
2433 /* .debug_str support. */
2434
2435 static void dwarf2out_init (const char *);
2436 static void dwarf2out_finish (const char *);
2437 static void dwarf2out_assembly_start (void);
2438 static void dwarf2out_define (unsigned int, const char *);
2439 static void dwarf2out_undef (unsigned int, const char *);
2440 static void dwarf2out_start_source_file (unsigned, const char *);
2441 static void dwarf2out_end_source_file (unsigned);
2442 static void dwarf2out_function_decl (tree);
2443 static void dwarf2out_begin_block (unsigned, unsigned);
2444 static void dwarf2out_end_block (unsigned, unsigned);
2445 static bool dwarf2out_ignore_block (const_tree);
2446 static void dwarf2out_global_decl (tree);
2447 static void dwarf2out_type_decl (tree, int);
2448 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2449 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2450 dw_die_ref);
2451 static void dwarf2out_abstract_function (tree);
2452 static void dwarf2out_var_location (rtx_insn *);
2453 static void dwarf2out_begin_function (tree);
2454 static void dwarf2out_end_function (unsigned int);
2455 static void dwarf2out_set_name (tree, tree);
2456
2457 /* The debug hooks structure. */
2458
2459 const struct gcc_debug_hooks dwarf2_debug_hooks =
2460 {
2461 dwarf2out_init,
2462 dwarf2out_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_function_decl, /* function_decl */
2484 dwarf2out_global_decl,
2485 dwarf2out_type_decl, /* type_decl */
2486 dwarf2out_imported_module_or_decl,
2487 debug_nothing_tree, /* deferred_inline_function */
2488 /* The DWARF 2 backend tries to reduce debugging bloat by not
2489 emitting the abstract description of inline functions until
2490 something tries to reference them. */
2491 dwarf2out_abstract_function, /* outlining_inline_function */
2492 debug_nothing_rtx_code_label, /* label */
2493 debug_nothing_int, /* handle_pch */
2494 dwarf2out_var_location,
2495 dwarf2out_switch_text_section,
2496 dwarf2out_set_name,
2497 1, /* start_end_main_source_file */
2498 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2499 };
2500 \f
2501 /* NOTE: In the comments in this file, many references are made to
2502 "Debugging Information Entries". This term is abbreviated as `DIE'
2503 throughout the remainder of this file. */
2504
2505 /* An internal representation of the DWARF output is built, and then
2506 walked to generate the DWARF debugging info. The walk of the internal
2507 representation is done after the entire program has been compiled.
2508 The types below are used to describe the internal representation. */
2509
2510 /* Whether to put type DIEs into their own section .debug_types instead
2511 of making them part of the .debug_info section. Only supported for
2512 Dwarf V4 or higher and the user didn't disable them through
2513 -fno-debug-types-section. It is more efficient to put them in a
2514 separate comdat sections since the linker will then be able to
2515 remove duplicates. But not all tools support .debug_types sections
2516 yet. */
2517
2518 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2519
2520 /* Various DIE's use offsets relative to the beginning of the
2521 .debug_info section to refer to each other. */
2522
2523 typedef long int dw_offset;
2524
2525 /* Define typedefs here to avoid circular dependencies. */
2526
2527 typedef struct dw_attr_struct *dw_attr_ref;
2528 typedef struct dw_line_info_struct *dw_line_info_ref;
2529 typedef struct pubname_struct *pubname_ref;
2530 typedef struct dw_ranges_struct *dw_ranges_ref;
2531 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2532 typedef struct comdat_type_struct *comdat_type_node_ref;
2533
2534 /* The entries in the line_info table more-or-less mirror the opcodes
2535 that are used in the real dwarf line table. Arrays of these entries
2536 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2537 supported. */
2538
2539 enum dw_line_info_opcode {
2540 /* Emit DW_LNE_set_address; the operand is the label index. */
2541 LI_set_address,
2542
2543 /* Emit a row to the matrix with the given line. This may be done
2544 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2545 special opcodes. */
2546 LI_set_line,
2547
2548 /* Emit a DW_LNS_set_file. */
2549 LI_set_file,
2550
2551 /* Emit a DW_LNS_set_column. */
2552 LI_set_column,
2553
2554 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2555 LI_negate_stmt,
2556
2557 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2558 LI_set_prologue_end,
2559 LI_set_epilogue_begin,
2560
2561 /* Emit a DW_LNE_set_discriminator. */
2562 LI_set_discriminator
2563 };
2564
2565 typedef struct GTY(()) dw_line_info_struct {
2566 enum dw_line_info_opcode opcode;
2567 unsigned int val;
2568 } dw_line_info_entry;
2569
2570
2571 typedef struct GTY(()) dw_line_info_table_struct {
2572 /* The label that marks the end of this section. */
2573 const char *end_label;
2574
2575 /* The values for the last row of the matrix, as collected in the table.
2576 These are used to minimize the changes to the next row. */
2577 unsigned int file_num;
2578 unsigned int line_num;
2579 unsigned int column_num;
2580 int discrim_num;
2581 bool is_stmt;
2582 bool in_use;
2583
2584 vec<dw_line_info_entry, va_gc> *entries;
2585 } dw_line_info_table;
2586
2587 typedef dw_line_info_table *dw_line_info_table_p;
2588
2589
2590 /* Each DIE attribute has a field specifying the attribute kind,
2591 a link to the next attribute in the chain, and an attribute value.
2592 Attributes are typically linked below the DIE they modify. */
2593
2594 typedef struct GTY(()) dw_attr_struct {
2595 enum dwarf_attribute dw_attr;
2596 dw_val_node dw_attr_val;
2597 }
2598 dw_attr_node;
2599
2600
2601 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2602 The children of each node form a circular list linked by
2603 die_sib. die_child points to the node *before* the "first" child node. */
2604
2605 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2606 union die_symbol_or_type_node
2607 {
2608 const char * GTY ((tag ("0"))) die_symbol;
2609 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2610 }
2611 GTY ((desc ("%0.comdat_type_p"))) die_id;
2612 vec<dw_attr_node, va_gc> *die_attr;
2613 dw_die_ref die_parent;
2614 dw_die_ref die_child;
2615 dw_die_ref die_sib;
2616 dw_die_ref die_definition; /* ref from a specification to its definition */
2617 dw_offset die_offset;
2618 unsigned long die_abbrev;
2619 int die_mark;
2620 unsigned int decl_id;
2621 enum dwarf_tag die_tag;
2622 /* Die is used and must not be pruned as unused. */
2623 BOOL_BITFIELD die_perennial_p : 1;
2624 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2625 /* Lots of spare bits. */
2626 }
2627 die_node;
2628
2629 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2630 #define FOR_EACH_CHILD(die, c, expr) do { \
2631 c = die->die_child; \
2632 if (c) do { \
2633 c = c->die_sib; \
2634 expr; \
2635 } while (c != die->die_child); \
2636 } while (0)
2637
2638 /* The pubname structure */
2639
2640 typedef struct GTY(()) pubname_struct {
2641 dw_die_ref die;
2642 const char *name;
2643 }
2644 pubname_entry;
2645
2646
2647 struct GTY(()) dw_ranges_struct {
2648 /* If this is positive, it's a block number, otherwise it's a
2649 bitwise-negated index into dw_ranges_by_label. */
2650 int num;
2651 };
2652
2653 /* A structure to hold a macinfo entry. */
2654
2655 typedef struct GTY(()) macinfo_struct {
2656 unsigned char code;
2657 unsigned HOST_WIDE_INT lineno;
2658 const char *info;
2659 }
2660 macinfo_entry;
2661
2662
2663 struct GTY(()) dw_ranges_by_label_struct {
2664 const char *begin;
2665 const char *end;
2666 };
2667
2668 /* The comdat type node structure. */
2669 typedef struct GTY(()) comdat_type_struct
2670 {
2671 dw_die_ref root_die;
2672 dw_die_ref type_die;
2673 dw_die_ref skeleton_die;
2674 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2675 struct comdat_type_struct *next;
2676 }
2677 comdat_type_node;
2678
2679 /* The limbo die list structure. */
2680 typedef struct GTY(()) limbo_die_struct {
2681 dw_die_ref die;
2682 tree created_for;
2683 struct limbo_die_struct *next;
2684 }
2685 limbo_die_node;
2686
2687 typedef struct skeleton_chain_struct
2688 {
2689 dw_die_ref old_die;
2690 dw_die_ref new_die;
2691 struct skeleton_chain_struct *parent;
2692 }
2693 skeleton_chain_node;
2694
2695 /* Define a macro which returns nonzero for a TYPE_DECL which was
2696 implicitly generated for a type.
2697
2698 Note that, unlike the C front-end (which generates a NULL named
2699 TYPE_DECL node for each complete tagged type, each array type,
2700 and each function type node created) the C++ front-end generates
2701 a _named_ TYPE_DECL node for each tagged type node created.
2702 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2703 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2704 front-end, but for each type, tagged or not. */
2705
2706 #define TYPE_DECL_IS_STUB(decl) \
2707 (DECL_NAME (decl) == NULL_TREE \
2708 || (DECL_ARTIFICIAL (decl) \
2709 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2710 /* This is necessary for stub decls that \
2711 appear in nested inline functions. */ \
2712 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2713 && (decl_ultimate_origin (decl) \
2714 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2715
2716 /* Information concerning the compilation unit's programming
2717 language, and compiler version. */
2718
2719 /* Fixed size portion of the DWARF compilation unit header. */
2720 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2721 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2722
2723 /* Fixed size portion of the DWARF comdat type unit header. */
2724 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2725 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2726 + DWARF_OFFSET_SIZE)
2727
2728 /* Fixed size portion of public names info. */
2729 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2730
2731 /* Fixed size portion of the address range info. */
2732 #define DWARF_ARANGES_HEADER_SIZE \
2733 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2734 DWARF2_ADDR_SIZE * 2) \
2735 - DWARF_INITIAL_LENGTH_SIZE)
2736
2737 /* Size of padding portion in the address range info. It must be
2738 aligned to twice the pointer size. */
2739 #define DWARF_ARANGES_PAD_SIZE \
2740 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2741 DWARF2_ADDR_SIZE * 2) \
2742 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2743
2744 /* Use assembler line directives if available. */
2745 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2746 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2747 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2748 #else
2749 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2750 #endif
2751 #endif
2752
2753 /* Minimum line offset in a special line info. opcode.
2754 This value was chosen to give a reasonable range of values. */
2755 #define DWARF_LINE_BASE -10
2756
2757 /* First special line opcode - leave room for the standard opcodes. */
2758 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2759
2760 /* Range of line offsets in a special line info. opcode. */
2761 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2762
2763 /* Flag that indicates the initial value of the is_stmt_start flag.
2764 In the present implementation, we do not mark any lines as
2765 the beginning of a source statement, because that information
2766 is not made available by the GCC front-end. */
2767 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2768
2769 /* Maximum number of operations per instruction bundle. */
2770 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2771 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2772 #endif
2773
2774 /* This location is used by calc_die_sizes() to keep track
2775 the offset of each DIE within the .debug_info section. */
2776 static unsigned long next_die_offset;
2777
2778 /* Record the root of the DIE's built for the current compilation unit. */
2779 static GTY(()) dw_die_ref single_comp_unit_die;
2780
2781 /* A list of type DIEs that have been separated into comdat sections. */
2782 static GTY(()) comdat_type_node *comdat_type_list;
2783
2784 /* A list of DIEs with a NULL parent waiting to be relocated. */
2785 static GTY(()) limbo_die_node *limbo_die_list;
2786
2787 /* A list of DIEs for which we may have to generate
2788 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2789 static GTY(()) limbo_die_node *deferred_asm_name;
2790
2791 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2792 {
2793 typedef const char *compare_type;
2794
2795 static hashval_t hash (dwarf_file_data *);
2796 static bool equal (dwarf_file_data *, const char *);
2797 };
2798
2799 /* Filenames referenced by this compilation unit. */
2800 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2801
2802 struct decl_die_hasher : ggc_hasher<die_node *>
2803 {
2804 typedef tree compare_type;
2805
2806 static hashval_t hash (die_node *);
2807 static bool equal (die_node *, tree);
2808 };
2809 /* A hash table of references to DIE's that describe declarations.
2810 The key is a DECL_UID() which is a unique number identifying each decl. */
2811 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2812
2813 struct block_die_hasher : ggc_hasher<die_struct *>
2814 {
2815 static hashval_t hash (die_struct *);
2816 static bool equal (die_struct *, die_struct *);
2817 };
2818
2819 /* A hash table of references to DIE's that describe COMMON blocks.
2820 The key is DECL_UID() ^ die_parent. */
2821 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2822
2823 typedef struct GTY(()) die_arg_entry_struct {
2824 dw_die_ref die;
2825 tree arg;
2826 } die_arg_entry;
2827
2828
2829 /* Node of the variable location list. */
2830 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2831 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2832 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2833 in mode of the EXPR_LIST node and first EXPR_LIST operand
2834 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2835 location or NULL for padding. For larger bitsizes,
2836 mode is 0 and first operand is a CONCAT with bitsize
2837 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2838 NULL as second operand. */
2839 rtx GTY (()) loc;
2840 const char * GTY (()) label;
2841 struct var_loc_node * GTY (()) next;
2842 };
2843
2844 /* Variable location list. */
2845 struct GTY ((for_user)) var_loc_list_def {
2846 struct var_loc_node * GTY (()) first;
2847
2848 /* Pointer to the last but one or last element of the
2849 chained list. If the list is empty, both first and
2850 last are NULL, if the list contains just one node
2851 or the last node certainly is not redundant, it points
2852 to the last node, otherwise points to the last but one.
2853 Do not mark it for GC because it is marked through the chain. */
2854 struct var_loc_node * GTY ((skip ("%h"))) last;
2855
2856 /* Pointer to the last element before section switch,
2857 if NULL, either sections weren't switched or first
2858 is after section switch. */
2859 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2860
2861 /* DECL_UID of the variable decl. */
2862 unsigned int decl_id;
2863 };
2864 typedef struct var_loc_list_def var_loc_list;
2865
2866 /* Call argument location list. */
2867 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2868 rtx GTY (()) call_arg_loc_note;
2869 const char * GTY (()) label;
2870 tree GTY (()) block;
2871 bool tail_call_p;
2872 rtx GTY (()) symbol_ref;
2873 struct call_arg_loc_node * GTY (()) next;
2874 };
2875
2876
2877 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2878 {
2879 typedef const_tree compare_type;
2880
2881 static hashval_t hash (var_loc_list *);
2882 static bool equal (var_loc_list *, const_tree);
2883 };
2884
2885 /* Table of decl location linked lists. */
2886 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2887
2888 /* Head and tail of call_arg_loc chain. */
2889 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2890 static struct call_arg_loc_node *call_arg_loc_last;
2891
2892 /* Number of call sites in the current function. */
2893 static int call_site_count = -1;
2894 /* Number of tail call sites in the current function. */
2895 static int tail_call_site_count = -1;
2896
2897 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2898 DIEs. */
2899 static vec<dw_die_ref> block_map;
2900
2901 /* A cached location list. */
2902 struct GTY ((for_user)) cached_dw_loc_list_def {
2903 /* The DECL_UID of the decl that this entry describes. */
2904 unsigned int decl_id;
2905
2906 /* The cached location list. */
2907 dw_loc_list_ref loc_list;
2908 };
2909 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2910
2911 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2912 {
2913
2914 typedef const_tree compare_type;
2915
2916 static hashval_t hash (cached_dw_loc_list *);
2917 static bool equal (cached_dw_loc_list *, const_tree);
2918 };
2919
2920 /* Table of cached location lists. */
2921 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2922
2923 /* A pointer to the base of a list of references to DIE's that
2924 are uniquely identified by their tag, presence/absence of
2925 children DIE's, and list of attribute/value pairs. */
2926 static GTY((length ("abbrev_die_table_allocated")))
2927 dw_die_ref *abbrev_die_table;
2928
2929 /* Number of elements currently allocated for abbrev_die_table. */
2930 static GTY(()) unsigned abbrev_die_table_allocated;
2931
2932 /* Number of elements in type_die_table currently in use. */
2933 static GTY(()) unsigned abbrev_die_table_in_use;
2934
2935 /* Size (in elements) of increments by which we may expand the
2936 abbrev_die_table. */
2937 #define ABBREV_DIE_TABLE_INCREMENT 256
2938
2939 /* A global counter for generating labels for line number data. */
2940 static unsigned int line_info_label_num;
2941
2942 /* The current table to which we should emit line number information
2943 for the current function. This will be set up at the beginning of
2944 assembly for the function. */
2945 static dw_line_info_table *cur_line_info_table;
2946
2947 /* The two default tables of line number info. */
2948 static GTY(()) dw_line_info_table *text_section_line_info;
2949 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2950
2951 /* The set of all non-default tables of line number info. */
2952 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2953
2954 /* A flag to tell pubnames/types export if there is an info section to
2955 refer to. */
2956 static bool info_section_emitted;
2957
2958 /* A pointer to the base of a table that contains a list of publicly
2959 accessible names. */
2960 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2961
2962 /* A pointer to the base of a table that contains a list of publicly
2963 accessible types. */
2964 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2965
2966 /* A pointer to the base of a table that contains a list of macro
2967 defines/undefines (and file start/end markers). */
2968 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2969
2970 /* True if .debug_macinfo or .debug_macros section is going to be
2971 emitted. */
2972 #define have_macinfo \
2973 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2974 && !macinfo_table->is_empty ())
2975
2976 /* Array of dies for which we should generate .debug_ranges info. */
2977 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2978
2979 /* Number of elements currently allocated for ranges_table. */
2980 static GTY(()) unsigned ranges_table_allocated;
2981
2982 /* Number of elements in ranges_table currently in use. */
2983 static GTY(()) unsigned ranges_table_in_use;
2984
2985 /* Array of pairs of labels referenced in ranges_table. */
2986 static GTY ((length ("ranges_by_label_allocated")))
2987 dw_ranges_by_label_ref ranges_by_label;
2988
2989 /* Number of elements currently allocated for ranges_by_label. */
2990 static GTY(()) unsigned ranges_by_label_allocated;
2991
2992 /* Number of elements in ranges_by_label currently in use. */
2993 static GTY(()) unsigned ranges_by_label_in_use;
2994
2995 /* Size (in elements) of increments by which we may expand the
2996 ranges_table. */
2997 #define RANGES_TABLE_INCREMENT 64
2998
2999 /* Whether we have location lists that need outputting */
3000 static GTY(()) bool have_location_lists;
3001
3002 /* Unique label counter. */
3003 static GTY(()) unsigned int loclabel_num;
3004
3005 /* Unique label counter for point-of-call tables. */
3006 static GTY(()) unsigned int poc_label_num;
3007
3008 /* The last file entry emitted by maybe_emit_file(). */
3009 static GTY(()) struct dwarf_file_data * last_emitted_file;
3010
3011 /* Number of internal labels generated by gen_internal_sym(). */
3012 static GTY(()) int label_num;
3013
3014 /* Cached result of previous call to lookup_filename. */
3015 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3016
3017 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3018
3019 /* Instances of generic types for which we need to generate debug
3020 info that describe their generic parameters and arguments. That
3021 generation needs to happen once all types are properly laid out so
3022 we do it at the end of compilation. */
3023 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3024
3025 /* Offset from the "steady-state frame pointer" to the frame base,
3026 within the current function. */
3027 static HOST_WIDE_INT frame_pointer_fb_offset;
3028 static bool frame_pointer_fb_offset_valid;
3029
3030 static vec<dw_die_ref> base_types;
3031
3032 /* Forward declarations for functions defined in this file. */
3033
3034 static int is_pseudo_reg (const_rtx);
3035 static tree type_main_variant (tree);
3036 static int is_tagged_type (const_tree);
3037 static const char *dwarf_tag_name (unsigned);
3038 static const char *dwarf_attr_name (unsigned);
3039 static const char *dwarf_form_name (unsigned);
3040 static tree decl_ultimate_origin (const_tree);
3041 static tree decl_class_context (tree);
3042 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3043 static inline enum dw_val_class AT_class (dw_attr_ref);
3044 static inline unsigned int AT_index (dw_attr_ref);
3045 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3046 static inline unsigned AT_flag (dw_attr_ref);
3047 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3048 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3049 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3050 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3051 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3052 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3053 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3054 unsigned int, unsigned char *);
3055 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3056 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3057 static inline const char *AT_string (dw_attr_ref);
3058 static enum dwarf_form AT_string_form (dw_attr_ref);
3059 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3060 static void add_AT_specification (dw_die_ref, dw_die_ref);
3061 static inline dw_die_ref AT_ref (dw_attr_ref);
3062 static inline int AT_ref_external (dw_attr_ref);
3063 static inline void set_AT_ref_external (dw_attr_ref, int);
3064 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3065 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3066 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3067 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3068 dw_loc_list_ref);
3069 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3070 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3071 static void remove_addr_table_entry (addr_table_entry *);
3072 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3073 static inline rtx AT_addr (dw_attr_ref);
3074 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3075 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3076 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3077 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3078 unsigned HOST_WIDE_INT);
3079 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3080 unsigned long, bool);
3081 static inline const char *AT_lbl (dw_attr_ref);
3082 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3083 static const char *get_AT_low_pc (dw_die_ref);
3084 static const char *get_AT_hi_pc (dw_die_ref);
3085 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3086 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3087 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3088 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3089 static bool is_cxx (void);
3090 static bool is_fortran (void);
3091 static bool is_ada (void);
3092 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3093 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3094 static void add_child_die (dw_die_ref, dw_die_ref);
3095 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3096 static dw_die_ref lookup_type_die (tree);
3097 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3098 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3099 static void equate_type_number_to_die (tree, dw_die_ref);
3100 static dw_die_ref lookup_decl_die (tree);
3101 static var_loc_list *lookup_decl_loc (const_tree);
3102 static void equate_decl_number_to_die (tree, dw_die_ref);
3103 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3104 static void print_spaces (FILE *);
3105 static void print_die (dw_die_ref, FILE *);
3106 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3107 static dw_die_ref pop_compile_unit (dw_die_ref);
3108 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3109 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3110 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3111 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3112 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3113 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3114 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3115 struct md5_ctx *, int *);
3116 struct checksum_attributes;
3117 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3118 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3119 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3120 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3121 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3122 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3123 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3124 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3125 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3126 static void compute_section_prefix (dw_die_ref);
3127 static int is_type_die (dw_die_ref);
3128 static int is_comdat_die (dw_die_ref);
3129 static int is_symbol_die (dw_die_ref);
3130 static inline bool is_template_instantiation (dw_die_ref);
3131 static void assign_symbol_names (dw_die_ref);
3132 static void break_out_includes (dw_die_ref);
3133 static int is_declaration_die (dw_die_ref);
3134 static int should_move_die_to_comdat (dw_die_ref);
3135 static dw_die_ref clone_as_declaration (dw_die_ref);
3136 static dw_die_ref clone_die (dw_die_ref);
3137 static dw_die_ref clone_tree (dw_die_ref);
3138 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3139 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3140 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3141 static dw_die_ref generate_skeleton (dw_die_ref);
3142 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3143 dw_die_ref,
3144 dw_die_ref);
3145 static void break_out_comdat_types (dw_die_ref);
3146 static void copy_decls_for_unworthy_types (dw_die_ref);
3147
3148 static void add_sibling_attributes (dw_die_ref);
3149 static void output_location_lists (dw_die_ref);
3150 static int constant_size (unsigned HOST_WIDE_INT);
3151 static unsigned long size_of_die (dw_die_ref);
3152 static void calc_die_sizes (dw_die_ref);
3153 static void calc_base_type_die_sizes (void);
3154 static void mark_dies (dw_die_ref);
3155 static void unmark_dies (dw_die_ref);
3156 static void unmark_all_dies (dw_die_ref);
3157 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3158 static unsigned long size_of_aranges (void);
3159 static enum dwarf_form value_format (dw_attr_ref);
3160 static void output_value_format (dw_attr_ref);
3161 static void output_abbrev_section (void);
3162 static void output_die_abbrevs (unsigned long, dw_die_ref);
3163 static void output_die_symbol (dw_die_ref);
3164 static void output_die (dw_die_ref);
3165 static void output_compilation_unit_header (void);
3166 static void output_comp_unit (dw_die_ref, int);
3167 static void output_comdat_type_unit (comdat_type_node *);
3168 static const char *dwarf2_name (tree, int);
3169 static void add_pubname (tree, dw_die_ref);
3170 static void add_enumerator_pubname (const char *, dw_die_ref);
3171 static void add_pubname_string (const char *, dw_die_ref);
3172 static void add_pubtype (tree, dw_die_ref);
3173 static void output_pubnames (vec<pubname_entry, va_gc> *);
3174 static void output_aranges (unsigned long);
3175 static unsigned int add_ranges_num (int);
3176 static unsigned int add_ranges (const_tree);
3177 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3178 bool *, bool);
3179 static void output_ranges (void);
3180 static dw_line_info_table *new_line_info_table (void);
3181 static void output_line_info (bool);
3182 static void output_file_names (void);
3183 static dw_die_ref base_type_die (tree);
3184 static int is_base_type (tree);
3185 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3186 static int decl_quals (const_tree);
3187 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3188 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3189 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3190 static int type_is_enum (const_tree);
3191 static unsigned int dbx_reg_number (const_rtx);
3192 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3193 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3194 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3195 enum var_init_status);
3196 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3197 enum var_init_status);
3198 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3199 enum var_init_status);
3200 static int is_based_loc (const_rtx);
3201 static bool resolve_one_addr (rtx *);
3202 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3203 enum var_init_status);
3204 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3205 enum var_init_status);
3206 static dw_loc_list_ref loc_list_from_tree (tree, int);
3207 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3208 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3209 static tree field_type (const_tree);
3210 static unsigned int simple_type_align_in_bits (const_tree);
3211 static unsigned int simple_decl_align_in_bits (const_tree);
3212 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3213 static HOST_WIDE_INT field_byte_offset (const_tree);
3214 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3215 dw_loc_list_ref);
3216 static void add_data_member_location_attribute (dw_die_ref, tree);
3217 static bool add_const_value_attribute (dw_die_ref, rtx);
3218 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3219 static void insert_wide_int (const wide_int &, unsigned char *, int);
3220 static void insert_float (const_rtx, unsigned char *);
3221 static rtx rtl_for_decl_location (tree);
3222 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3223 enum dwarf_attribute);
3224 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3225 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3226 static void add_name_attribute (dw_die_ref, const char *);
3227 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3228 static void add_comp_dir_attribute (dw_die_ref);
3229 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3230 static void add_subscript_info (dw_die_ref, tree, bool);
3231 static void add_byte_size_attribute (dw_die_ref, tree);
3232 static void add_bit_offset_attribute (dw_die_ref, tree);
3233 static void add_bit_size_attribute (dw_die_ref, tree);
3234 static void add_prototyped_attribute (dw_die_ref, tree);
3235 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3236 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3237 static void add_src_coords_attributes (dw_die_ref, tree);
3238 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3239 static void push_decl_scope (tree);
3240 static void pop_decl_scope (void);
3241 static dw_die_ref scope_die_for (tree, dw_die_ref);
3242 static inline int local_scope_p (dw_die_ref);
3243 static inline int class_scope_p (dw_die_ref);
3244 static inline int class_or_namespace_scope_p (dw_die_ref);
3245 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3246 static void add_calling_convention_attribute (dw_die_ref, tree);
3247 static const char *type_tag (const_tree);
3248 static tree member_declared_type (const_tree);
3249 #if 0
3250 static const char *decl_start_label (tree);
3251 #endif
3252 static void gen_array_type_die (tree, dw_die_ref);
3253 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3254 #if 0
3255 static void gen_entry_point_die (tree, dw_die_ref);
3256 #endif
3257 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3258 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3259 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3260 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3261 static void gen_formal_types_die (tree, dw_die_ref);
3262 static void gen_subprogram_die (tree, dw_die_ref);
3263 static void gen_variable_die (tree, tree, dw_die_ref);
3264 static void gen_const_die (tree, dw_die_ref);
3265 static void gen_label_die (tree, dw_die_ref);
3266 static void gen_lexical_block_die (tree, dw_die_ref, int);
3267 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3268 static void gen_field_die (tree, dw_die_ref);
3269 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3270 static dw_die_ref gen_compile_unit_die (const char *);
3271 static void gen_inheritance_die (tree, tree, dw_die_ref);
3272 static void gen_member_die (tree, dw_die_ref);
3273 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3274 enum debug_info_usage);
3275 static void gen_subroutine_type_die (tree, dw_die_ref);
3276 static void gen_typedef_die (tree, dw_die_ref);
3277 static void gen_type_die (tree, dw_die_ref);
3278 static void gen_block_die (tree, dw_die_ref, int);
3279 static void decls_for_scope (tree, dw_die_ref, int);
3280 static inline int is_redundant_typedef (const_tree);
3281 static bool is_naming_typedef_decl (const_tree);
3282 static inline dw_die_ref get_context_die (tree);
3283 static void gen_namespace_die (tree, dw_die_ref);
3284 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3285 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3286 static dw_die_ref force_decl_die (tree);
3287 static dw_die_ref force_type_die (tree);
3288 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3289 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3290 static struct dwarf_file_data * lookup_filename (const char *);
3291 static void retry_incomplete_types (void);
3292 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3293 static void gen_generic_params_dies (tree);
3294 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3295 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3296 static void splice_child_die (dw_die_ref, dw_die_ref);
3297 static int file_info_cmp (const void *, const void *);
3298 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3299 const char *, const char *);
3300 static void output_loc_list (dw_loc_list_ref);
3301 static char *gen_internal_sym (const char *);
3302 static bool want_pubnames (void);
3303
3304 static void prune_unmark_dies (dw_die_ref);
3305 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3306 static void prune_unused_types_mark (dw_die_ref, int);
3307 static void prune_unused_types_walk (dw_die_ref);
3308 static void prune_unused_types_walk_attribs (dw_die_ref);
3309 static void prune_unused_types_prune (dw_die_ref);
3310 static void prune_unused_types (void);
3311 static int maybe_emit_file (struct dwarf_file_data *fd);
3312 static inline const char *AT_vms_delta1 (dw_attr_ref);
3313 static inline const char *AT_vms_delta2 (dw_attr_ref);
3314 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3315 const char *, const char *);
3316 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3317 static void gen_remaining_tmpl_value_param_die_attribute (void);
3318 static bool generic_type_p (tree);
3319 static void schedule_generic_params_dies_gen (tree t);
3320 static void gen_scheduled_generic_parms_dies (void);
3321
3322 static const char *comp_dir_string (void);
3323
3324 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3325
3326 /* enum for tracking thread-local variables whose address is really an offset
3327 relative to the TLS pointer, which will need link-time relocation, but will
3328 not need relocation by the DWARF consumer. */
3329
3330 enum dtprel_bool
3331 {
3332 dtprel_false = 0,
3333 dtprel_true = 1
3334 };
3335
3336 /* Return the operator to use for an address of a variable. For dtprel_true, we
3337 use DW_OP_const*. For regular variables, which need both link-time
3338 relocation and consumer-level relocation (e.g., to account for shared objects
3339 loaded at a random address), we use DW_OP_addr*. */
3340
3341 static inline enum dwarf_location_atom
3342 dw_addr_op (enum dtprel_bool dtprel)
3343 {
3344 if (dtprel == dtprel_true)
3345 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3346 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3347 else
3348 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3349 }
3350
3351 /* Return a pointer to a newly allocated address location description. If
3352 dwarf_split_debug_info is true, then record the address with the appropriate
3353 relocation. */
3354 static inline dw_loc_descr_ref
3355 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3356 {
3357 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3358
3359 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3360 ref->dw_loc_oprnd1.v.val_addr = addr;
3361 ref->dtprel = dtprel;
3362 if (dwarf_split_debug_info)
3363 ref->dw_loc_oprnd1.val_entry
3364 = add_addr_table_entry (addr,
3365 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3366 else
3367 ref->dw_loc_oprnd1.val_entry = NULL;
3368
3369 return ref;
3370 }
3371
3372 /* Section names used to hold DWARF debugging information. */
3373
3374 #ifndef DEBUG_INFO_SECTION
3375 #define DEBUG_INFO_SECTION ".debug_info"
3376 #endif
3377 #ifndef DEBUG_DWO_INFO_SECTION
3378 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3379 #endif
3380 #ifndef DEBUG_ABBREV_SECTION
3381 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3382 #endif
3383 #ifndef DEBUG_DWO_ABBREV_SECTION
3384 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3385 #endif
3386 #ifndef DEBUG_ARANGES_SECTION
3387 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3388 #endif
3389 #ifndef DEBUG_ADDR_SECTION
3390 #define DEBUG_ADDR_SECTION ".debug_addr"
3391 #endif
3392 #ifndef DEBUG_NORM_MACINFO_SECTION
3393 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3394 #endif
3395 #ifndef DEBUG_DWO_MACINFO_SECTION
3396 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3397 #endif
3398 #ifndef DEBUG_MACINFO_SECTION
3399 #define DEBUG_MACINFO_SECTION \
3400 (!dwarf_split_debug_info \
3401 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3402 #endif
3403 #ifndef DEBUG_NORM_MACRO_SECTION
3404 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3405 #endif
3406 #ifndef DEBUG_DWO_MACRO_SECTION
3407 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3408 #endif
3409 #ifndef DEBUG_MACRO_SECTION
3410 #define DEBUG_MACRO_SECTION \
3411 (!dwarf_split_debug_info \
3412 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3413 #endif
3414 #ifndef DEBUG_LINE_SECTION
3415 #define DEBUG_LINE_SECTION ".debug_line"
3416 #endif
3417 #ifndef DEBUG_DWO_LINE_SECTION
3418 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3419 #endif
3420 #ifndef DEBUG_LOC_SECTION
3421 #define DEBUG_LOC_SECTION ".debug_loc"
3422 #endif
3423 #ifndef DEBUG_DWO_LOC_SECTION
3424 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3425 #endif
3426 #ifndef DEBUG_PUBNAMES_SECTION
3427 #define DEBUG_PUBNAMES_SECTION \
3428 ((debug_generate_pub_sections == 2) \
3429 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3430 #endif
3431 #ifndef DEBUG_PUBTYPES_SECTION
3432 #define DEBUG_PUBTYPES_SECTION \
3433 ((debug_generate_pub_sections == 2) \
3434 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3435 #endif
3436 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3437 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3438 #ifndef DEBUG_STR_OFFSETS_SECTION
3439 #define DEBUG_STR_OFFSETS_SECTION \
3440 (!dwarf_split_debug_info \
3441 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3442 #endif
3443 #ifndef DEBUG_STR_DWO_SECTION
3444 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3445 #endif
3446 #ifndef DEBUG_STR_SECTION
3447 #define DEBUG_STR_SECTION ".debug_str"
3448 #endif
3449 #ifndef DEBUG_RANGES_SECTION
3450 #define DEBUG_RANGES_SECTION ".debug_ranges"
3451 #endif
3452
3453 /* Standard ELF section names for compiled code and data. */
3454 #ifndef TEXT_SECTION_NAME
3455 #define TEXT_SECTION_NAME ".text"
3456 #endif
3457
3458 /* Section flags for .debug_macinfo/.debug_macro section. */
3459 #define DEBUG_MACRO_SECTION_FLAGS \
3460 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3461
3462 /* Section flags for .debug_str section. */
3463 #define DEBUG_STR_SECTION_FLAGS \
3464 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3465 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3466 : SECTION_DEBUG)
3467
3468 /* Section flags for .debug_str.dwo section. */
3469 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3470
3471 /* Labels we insert at beginning sections we can reference instead of
3472 the section names themselves. */
3473
3474 #ifndef TEXT_SECTION_LABEL
3475 #define TEXT_SECTION_LABEL "Ltext"
3476 #endif
3477 #ifndef COLD_TEXT_SECTION_LABEL
3478 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3479 #endif
3480 #ifndef DEBUG_LINE_SECTION_LABEL
3481 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3482 #endif
3483 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3484 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3485 #endif
3486 #ifndef DEBUG_INFO_SECTION_LABEL
3487 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3488 #endif
3489 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3490 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3491 #endif
3492 #ifndef DEBUG_ABBREV_SECTION_LABEL
3493 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3494 #endif
3495 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3496 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3497 #endif
3498 #ifndef DEBUG_ADDR_SECTION_LABEL
3499 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3500 #endif
3501 #ifndef DEBUG_LOC_SECTION_LABEL
3502 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3503 #endif
3504 #ifndef DEBUG_RANGES_SECTION_LABEL
3505 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3506 #endif
3507 #ifndef DEBUG_MACINFO_SECTION_LABEL
3508 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3509 #endif
3510 #ifndef DEBUG_MACRO_SECTION_LABEL
3511 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3512 #endif
3513 #define SKELETON_COMP_DIE_ABBREV 1
3514 #define SKELETON_TYPE_DIE_ABBREV 2
3515
3516 /* Definitions of defaults for formats and names of various special
3517 (artificial) labels which may be generated within this file (when the -g
3518 options is used and DWARF2_DEBUGGING_INFO is in effect.
3519 If necessary, these may be overridden from within the tm.h file, but
3520 typically, overriding these defaults is unnecessary. */
3521
3522 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3523 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3524 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3525 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3526 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3527 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3528 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3529 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3530 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3531 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3532 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3533 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3534 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3535 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3536
3537 #ifndef TEXT_END_LABEL
3538 #define TEXT_END_LABEL "Letext"
3539 #endif
3540 #ifndef COLD_END_LABEL
3541 #define COLD_END_LABEL "Letext_cold"
3542 #endif
3543 #ifndef BLOCK_BEGIN_LABEL
3544 #define BLOCK_BEGIN_LABEL "LBB"
3545 #endif
3546 #ifndef BLOCK_END_LABEL
3547 #define BLOCK_END_LABEL "LBE"
3548 #endif
3549 #ifndef LINE_CODE_LABEL
3550 #define LINE_CODE_LABEL "LM"
3551 #endif
3552
3553 \f
3554 /* Return the root of the DIE's built for the current compilation unit. */
3555 static dw_die_ref
3556 comp_unit_die (void)
3557 {
3558 if (!single_comp_unit_die)
3559 single_comp_unit_die = gen_compile_unit_die (NULL);
3560 return single_comp_unit_die;
3561 }
3562
3563 /* We allow a language front-end to designate a function that is to be
3564 called to "demangle" any name before it is put into a DIE. */
3565
3566 static const char *(*demangle_name_func) (const char *);
3567
3568 void
3569 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3570 {
3571 demangle_name_func = func;
3572 }
3573
3574 /* Test if rtl node points to a pseudo register. */
3575
3576 static inline int
3577 is_pseudo_reg (const_rtx rtl)
3578 {
3579 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3580 || (GET_CODE (rtl) == SUBREG
3581 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3582 }
3583
3584 /* Return a reference to a type, with its const and volatile qualifiers
3585 removed. */
3586
3587 static inline tree
3588 type_main_variant (tree type)
3589 {
3590 type = TYPE_MAIN_VARIANT (type);
3591
3592 /* ??? There really should be only one main variant among any group of
3593 variants of a given type (and all of the MAIN_VARIANT values for all
3594 members of the group should point to that one type) but sometimes the C
3595 front-end messes this up for array types, so we work around that bug
3596 here. */
3597 if (TREE_CODE (type) == ARRAY_TYPE)
3598 while (type != TYPE_MAIN_VARIANT (type))
3599 type = TYPE_MAIN_VARIANT (type);
3600
3601 return type;
3602 }
3603
3604 /* Return nonzero if the given type node represents a tagged type. */
3605
3606 static inline int
3607 is_tagged_type (const_tree type)
3608 {
3609 enum tree_code code = TREE_CODE (type);
3610
3611 return (code == RECORD_TYPE || code == UNION_TYPE
3612 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3613 }
3614
3615 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3616
3617 static void
3618 get_ref_die_offset_label (char *label, dw_die_ref ref)
3619 {
3620 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3621 }
3622
3623 /* Return die_offset of a DIE reference to a base type. */
3624
3625 static unsigned long int
3626 get_base_type_offset (dw_die_ref ref)
3627 {
3628 if (ref->die_offset)
3629 return ref->die_offset;
3630 if (comp_unit_die ()->die_abbrev)
3631 {
3632 calc_base_type_die_sizes ();
3633 gcc_assert (ref->die_offset);
3634 }
3635 return ref->die_offset;
3636 }
3637
3638 /* Return die_offset of a DIE reference other than base type. */
3639
3640 static unsigned long int
3641 get_ref_die_offset (dw_die_ref ref)
3642 {
3643 gcc_assert (ref->die_offset);
3644 return ref->die_offset;
3645 }
3646
3647 /* Convert a DIE tag into its string name. */
3648
3649 static const char *
3650 dwarf_tag_name (unsigned int tag)
3651 {
3652 const char *name = get_DW_TAG_name (tag);
3653
3654 if (name != NULL)
3655 return name;
3656
3657 return "DW_TAG_<unknown>";
3658 }
3659
3660 /* Convert a DWARF attribute code into its string name. */
3661
3662 static const char *
3663 dwarf_attr_name (unsigned int attr)
3664 {
3665 const char *name;
3666
3667 switch (attr)
3668 {
3669 #if VMS_DEBUGGING_INFO
3670 case DW_AT_HP_prologue:
3671 return "DW_AT_HP_prologue";
3672 #else
3673 case DW_AT_MIPS_loop_unroll_factor:
3674 return "DW_AT_MIPS_loop_unroll_factor";
3675 #endif
3676
3677 #if VMS_DEBUGGING_INFO
3678 case DW_AT_HP_epilogue:
3679 return "DW_AT_HP_epilogue";
3680 #else
3681 case DW_AT_MIPS_stride:
3682 return "DW_AT_MIPS_stride";
3683 #endif
3684 }
3685
3686 name = get_DW_AT_name (attr);
3687
3688 if (name != NULL)
3689 return name;
3690
3691 return "DW_AT_<unknown>";
3692 }
3693
3694 /* Convert a DWARF value form code into its string name. */
3695
3696 static const char *
3697 dwarf_form_name (unsigned int form)
3698 {
3699 const char *name = get_DW_FORM_name (form);
3700
3701 if (name != NULL)
3702 return name;
3703
3704 return "DW_FORM_<unknown>";
3705 }
3706 \f
3707 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3708 instance of an inlined instance of a decl which is local to an inline
3709 function, so we have to trace all of the way back through the origin chain
3710 to find out what sort of node actually served as the original seed for the
3711 given block. */
3712
3713 static tree
3714 decl_ultimate_origin (const_tree decl)
3715 {
3716 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3717 return NULL_TREE;
3718
3719 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3720 we're trying to output the abstract instance of this function. */
3721 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3722 return NULL_TREE;
3723
3724 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3725 most distant ancestor, this should never happen. */
3726 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3727
3728 return DECL_ABSTRACT_ORIGIN (decl);
3729 }
3730
3731 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3732 of a virtual function may refer to a base class, so we check the 'this'
3733 parameter. */
3734
3735 static tree
3736 decl_class_context (tree decl)
3737 {
3738 tree context = NULL_TREE;
3739
3740 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3741 context = DECL_CONTEXT (decl);
3742 else
3743 context = TYPE_MAIN_VARIANT
3744 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3745
3746 if (context && !TYPE_P (context))
3747 context = NULL_TREE;
3748
3749 return context;
3750 }
3751 \f
3752 /* Add an attribute/value pair to a DIE. */
3753
3754 static inline void
3755 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3756 {
3757 /* Maybe this should be an assert? */
3758 if (die == NULL)
3759 return;
3760
3761 vec_safe_reserve (die->die_attr, 1);
3762 vec_safe_push (die->die_attr, *attr);
3763 }
3764
3765 static inline enum dw_val_class
3766 AT_class (dw_attr_ref a)
3767 {
3768 return a->dw_attr_val.val_class;
3769 }
3770
3771 /* Return the index for any attribute that will be referenced with a
3772 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3773 are stored in dw_attr_val.v.val_str for reference counting
3774 pruning. */
3775
3776 static inline unsigned int
3777 AT_index (dw_attr_ref a)
3778 {
3779 if (AT_class (a) == dw_val_class_str)
3780 return a->dw_attr_val.v.val_str->index;
3781 else if (a->dw_attr_val.val_entry != NULL)
3782 return a->dw_attr_val.val_entry->index;
3783 return NOT_INDEXED;
3784 }
3785
3786 /* Add a flag value attribute to a DIE. */
3787
3788 static inline void
3789 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3790 {
3791 dw_attr_node attr;
3792
3793 attr.dw_attr = attr_kind;
3794 attr.dw_attr_val.val_class = dw_val_class_flag;
3795 attr.dw_attr_val.val_entry = NULL;
3796 attr.dw_attr_val.v.val_flag = flag;
3797 add_dwarf_attr (die, &attr);
3798 }
3799
3800 static inline unsigned
3801 AT_flag (dw_attr_ref a)
3802 {
3803 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3804 return a->dw_attr_val.v.val_flag;
3805 }
3806
3807 /* Add a signed integer attribute value to a DIE. */
3808
3809 static inline void
3810 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3811 {
3812 dw_attr_node attr;
3813
3814 attr.dw_attr = attr_kind;
3815 attr.dw_attr_val.val_class = dw_val_class_const;
3816 attr.dw_attr_val.val_entry = NULL;
3817 attr.dw_attr_val.v.val_int = int_val;
3818 add_dwarf_attr (die, &attr);
3819 }
3820
3821 static inline HOST_WIDE_INT
3822 AT_int (dw_attr_ref a)
3823 {
3824 gcc_assert (a && AT_class (a) == dw_val_class_const);
3825 return a->dw_attr_val.v.val_int;
3826 }
3827
3828 /* Add an unsigned integer attribute value to a DIE. */
3829
3830 static inline void
3831 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3832 unsigned HOST_WIDE_INT unsigned_val)
3833 {
3834 dw_attr_node attr;
3835
3836 attr.dw_attr = attr_kind;
3837 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3838 attr.dw_attr_val.val_entry = NULL;
3839 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3840 add_dwarf_attr (die, &attr);
3841 }
3842
3843 static inline unsigned HOST_WIDE_INT
3844 AT_unsigned (dw_attr_ref a)
3845 {
3846 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3847 return a->dw_attr_val.v.val_unsigned;
3848 }
3849
3850 /* Add an unsigned wide integer attribute value to a DIE. */
3851
3852 static inline void
3853 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3854 const wide_int& w)
3855 {
3856 dw_attr_node attr;
3857
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3860 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3861 *attr.dw_attr_val.v.val_wide = w;
3862 add_dwarf_attr (die, &attr);
3863 }
3864
3865 /* Add an unsigned double integer attribute value to a DIE. */
3866
3867 static inline void
3868 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3869 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3870 {
3871 dw_attr_node attr;
3872
3873 attr.dw_attr = attr_kind;
3874 attr.dw_attr_val.val_class = dw_val_class_const_double;
3875 attr.dw_attr_val.val_entry = NULL;
3876 attr.dw_attr_val.v.val_double.high = high;
3877 attr.dw_attr_val.v.val_double.low = low;
3878 add_dwarf_attr (die, &attr);
3879 }
3880
3881 /* Add a floating point attribute value to a DIE and return it. */
3882
3883 static inline void
3884 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3885 unsigned int length, unsigned int elt_size, unsigned char *array)
3886 {
3887 dw_attr_node attr;
3888
3889 attr.dw_attr = attr_kind;
3890 attr.dw_attr_val.val_class = dw_val_class_vec;
3891 attr.dw_attr_val.val_entry = NULL;
3892 attr.dw_attr_val.v.val_vec.length = length;
3893 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3894 attr.dw_attr_val.v.val_vec.array = array;
3895 add_dwarf_attr (die, &attr);
3896 }
3897
3898 /* Add an 8-byte data attribute value to a DIE. */
3899
3900 static inline void
3901 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 unsigned char data8[8])
3903 {
3904 dw_attr_node attr;
3905
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_data8;
3908 attr.dw_attr_val.val_entry = NULL;
3909 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3910 add_dwarf_attr (die, &attr);
3911 }
3912
3913 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3914 dwarf_split_debug_info, address attributes in dies destined for the
3915 final executable have force_direct set to avoid using indexed
3916 references. */
3917
3918 static inline void
3919 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3920 bool force_direct)
3921 {
3922 dw_attr_node attr;
3923 char * lbl_id;
3924
3925 lbl_id = xstrdup (lbl_low);
3926 attr.dw_attr = DW_AT_low_pc;
3927 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3928 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3929 if (dwarf_split_debug_info && !force_direct)
3930 attr.dw_attr_val.val_entry
3931 = add_addr_table_entry (lbl_id, ate_kind_label);
3932 else
3933 attr.dw_attr_val.val_entry = NULL;
3934 add_dwarf_attr (die, &attr);
3935
3936 attr.dw_attr = DW_AT_high_pc;
3937 if (dwarf_version < 4)
3938 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3939 else
3940 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3941 lbl_id = xstrdup (lbl_high);
3942 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3943 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3944 && dwarf_split_debug_info && !force_direct)
3945 attr.dw_attr_val.val_entry
3946 = add_addr_table_entry (lbl_id, ate_kind_label);
3947 else
3948 attr.dw_attr_val.val_entry = NULL;
3949 add_dwarf_attr (die, &attr);
3950 }
3951
3952 /* Hash and equality functions for debug_str_hash. */
3953
3954 hashval_t
3955 indirect_string_hasher::hash (indirect_string_node *x)
3956 {
3957 return htab_hash_string (x->str);
3958 }
3959
3960 bool
3961 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3962 {
3963 return strcmp (x1->str, x2) == 0;
3964 }
3965
3966 /* Add STR to the given string hash table. */
3967
3968 static struct indirect_string_node *
3969 find_AT_string_in_table (const char *str,
3970 hash_table<indirect_string_hasher> *table)
3971 {
3972 struct indirect_string_node *node;
3973
3974 indirect_string_node **slot
3975 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3976 if (*slot == NULL)
3977 {
3978 node = ggc_cleared_alloc<indirect_string_node> ();
3979 node->str = ggc_strdup (str);
3980 *slot = node;
3981 }
3982 else
3983 node = *slot;
3984
3985 node->refcount++;
3986 return node;
3987 }
3988
3989 /* Add STR to the indirect string hash table. */
3990
3991 static struct indirect_string_node *
3992 find_AT_string (const char *str)
3993 {
3994 if (! debug_str_hash)
3995 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
3996
3997 return find_AT_string_in_table (str, debug_str_hash);
3998 }
3999
4000 /* Add a string attribute value to a DIE. */
4001
4002 static inline void
4003 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4004 {
4005 dw_attr_node attr;
4006 struct indirect_string_node *node;
4007
4008 node = find_AT_string (str);
4009
4010 attr.dw_attr = attr_kind;
4011 attr.dw_attr_val.val_class = dw_val_class_str;
4012 attr.dw_attr_val.val_entry = NULL;
4013 attr.dw_attr_val.v.val_str = node;
4014 add_dwarf_attr (die, &attr);
4015 }
4016
4017 static inline const char *
4018 AT_string (dw_attr_ref a)
4019 {
4020 gcc_assert (a && AT_class (a) == dw_val_class_str);
4021 return a->dw_attr_val.v.val_str->str;
4022 }
4023
4024 /* Call this function directly to bypass AT_string_form's logic to put
4025 the string inline in the die. */
4026
4027 static void
4028 set_indirect_string (struct indirect_string_node *node)
4029 {
4030 char label[32];
4031 /* Already indirect is a no op. */
4032 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4033 {
4034 gcc_assert (node->label);
4035 return;
4036 }
4037 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4038 ++dw2_string_counter;
4039 node->label = xstrdup (label);
4040
4041 if (!dwarf_split_debug_info)
4042 {
4043 node->form = DW_FORM_strp;
4044 node->index = NOT_INDEXED;
4045 }
4046 else
4047 {
4048 node->form = DW_FORM_GNU_str_index;
4049 node->index = NO_INDEX_ASSIGNED;
4050 }
4051 }
4052
4053 /* Find out whether a string should be output inline in DIE
4054 or out-of-line in .debug_str section. */
4055
4056 static enum dwarf_form
4057 find_string_form (struct indirect_string_node *node)
4058 {
4059 unsigned int len;
4060
4061 if (node->form)
4062 return node->form;
4063
4064 len = strlen (node->str) + 1;
4065
4066 /* If the string is shorter or equal to the size of the reference, it is
4067 always better to put it inline. */
4068 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4069 return node->form = DW_FORM_string;
4070
4071 /* If we cannot expect the linker to merge strings in .debug_str
4072 section, only put it into .debug_str if it is worth even in this
4073 single module. */
4074 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4075 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4076 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4077 return node->form = DW_FORM_string;
4078
4079 set_indirect_string (node);
4080
4081 return node->form;
4082 }
4083
4084 /* Find out whether the string referenced from the attribute should be
4085 output inline in DIE or out-of-line in .debug_str section. */
4086
4087 static enum dwarf_form
4088 AT_string_form (dw_attr_ref a)
4089 {
4090 gcc_assert (a && AT_class (a) == dw_val_class_str);
4091 return find_string_form (a->dw_attr_val.v.val_str);
4092 }
4093
4094 /* Add a DIE reference attribute value to a DIE. */
4095
4096 static inline void
4097 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4098 {
4099 dw_attr_node attr;
4100
4101 #ifdef ENABLE_CHECKING
4102 gcc_assert (targ_die != NULL);
4103 #else
4104 /* With LTO we can end up trying to reference something we didn't create
4105 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4106 if (targ_die == NULL)
4107 return;
4108 #endif
4109
4110 attr.dw_attr = attr_kind;
4111 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4112 attr.dw_attr_val.val_entry = NULL;
4113 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4114 attr.dw_attr_val.v.val_die_ref.external = 0;
4115 add_dwarf_attr (die, &attr);
4116 }
4117
4118 /* Change DIE reference REF to point to NEW_DIE instead. */
4119
4120 static inline void
4121 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4122 {
4123 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4124 ref->dw_attr_val.v.val_die_ref.die = new_die;
4125 ref->dw_attr_val.v.val_die_ref.external = 0;
4126 }
4127
4128 /* Add an AT_specification attribute to a DIE, and also make the back
4129 pointer from the specification to the definition. */
4130
4131 static inline void
4132 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4133 {
4134 add_AT_die_ref (die, DW_AT_specification, targ_die);
4135 gcc_assert (!targ_die->die_definition);
4136 targ_die->die_definition = die;
4137 }
4138
4139 static inline dw_die_ref
4140 AT_ref (dw_attr_ref a)
4141 {
4142 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4143 return a->dw_attr_val.v.val_die_ref.die;
4144 }
4145
4146 static inline int
4147 AT_ref_external (dw_attr_ref a)
4148 {
4149 if (a && AT_class (a) == dw_val_class_die_ref)
4150 return a->dw_attr_val.v.val_die_ref.external;
4151
4152 return 0;
4153 }
4154
4155 static inline void
4156 set_AT_ref_external (dw_attr_ref a, int i)
4157 {
4158 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4159 a->dw_attr_val.v.val_die_ref.external = i;
4160 }
4161
4162 /* Add an FDE reference attribute value to a DIE. */
4163
4164 static inline void
4165 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4166 {
4167 dw_attr_node attr;
4168
4169 attr.dw_attr = attr_kind;
4170 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4171 attr.dw_attr_val.val_entry = NULL;
4172 attr.dw_attr_val.v.val_fde_index = targ_fde;
4173 add_dwarf_attr (die, &attr);
4174 }
4175
4176 /* Add a location description attribute value to a DIE. */
4177
4178 static inline void
4179 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4180 {
4181 dw_attr_node attr;
4182
4183 attr.dw_attr = attr_kind;
4184 attr.dw_attr_val.val_class = dw_val_class_loc;
4185 attr.dw_attr_val.val_entry = NULL;
4186 attr.dw_attr_val.v.val_loc = loc;
4187 add_dwarf_attr (die, &attr);
4188 }
4189
4190 static inline dw_loc_descr_ref
4191 AT_loc (dw_attr_ref a)
4192 {
4193 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4194 return a->dw_attr_val.v.val_loc;
4195 }
4196
4197 static inline void
4198 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4199 {
4200 dw_attr_node attr;
4201
4202 attr.dw_attr = attr_kind;
4203 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4204 attr.dw_attr_val.val_entry = NULL;
4205 attr.dw_attr_val.v.val_loc_list = loc_list;
4206 add_dwarf_attr (die, &attr);
4207 have_location_lists = true;
4208 }
4209
4210 static inline dw_loc_list_ref
4211 AT_loc_list (dw_attr_ref a)
4212 {
4213 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4214 return a->dw_attr_val.v.val_loc_list;
4215 }
4216
4217 static inline dw_loc_list_ref *
4218 AT_loc_list_ptr (dw_attr_ref a)
4219 {
4220 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4221 return &a->dw_attr_val.v.val_loc_list;
4222 }
4223
4224 struct addr_hasher : ggc_hasher<addr_table_entry *>
4225 {
4226 static hashval_t hash (addr_table_entry *);
4227 static bool equal (addr_table_entry *, addr_table_entry *);
4228 };
4229
4230 /* Table of entries into the .debug_addr section. */
4231
4232 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4233
4234 /* Hash an address_table_entry. */
4235
4236 hashval_t
4237 addr_hasher::hash (addr_table_entry *a)
4238 {
4239 inchash::hash hstate;
4240 switch (a->kind)
4241 {
4242 case ate_kind_rtx:
4243 hstate.add_int (0);
4244 break;
4245 case ate_kind_rtx_dtprel:
4246 hstate.add_int (1);
4247 break;
4248 case ate_kind_label:
4249 return htab_hash_string (a->addr.label);
4250 default:
4251 gcc_unreachable ();
4252 }
4253 inchash::add_rtx (a->addr.rtl, hstate);
4254 return hstate.end ();
4255 }
4256
4257 /* Determine equality for two address_table_entries. */
4258
4259 bool
4260 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4261 {
4262 if (a1->kind != a2->kind)
4263 return 0;
4264 switch (a1->kind)
4265 {
4266 case ate_kind_rtx:
4267 case ate_kind_rtx_dtprel:
4268 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4269 case ate_kind_label:
4270 return strcmp (a1->addr.label, a2->addr.label) == 0;
4271 default:
4272 gcc_unreachable ();
4273 }
4274 }
4275
4276 /* Initialize an addr_table_entry. */
4277
4278 void
4279 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4280 {
4281 e->kind = kind;
4282 switch (kind)
4283 {
4284 case ate_kind_rtx:
4285 case ate_kind_rtx_dtprel:
4286 e->addr.rtl = (rtx) addr;
4287 break;
4288 case ate_kind_label:
4289 e->addr.label = (char *) addr;
4290 break;
4291 }
4292 e->refcount = 0;
4293 e->index = NO_INDEX_ASSIGNED;
4294 }
4295
4296 /* Add attr to the address table entry to the table. Defer setting an
4297 index until output time. */
4298
4299 static addr_table_entry *
4300 add_addr_table_entry (void *addr, enum ate_kind kind)
4301 {
4302 addr_table_entry *node;
4303 addr_table_entry finder;
4304
4305 gcc_assert (dwarf_split_debug_info);
4306 if (! addr_index_table)
4307 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4308 init_addr_table_entry (&finder, kind, addr);
4309 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4310
4311 if (*slot == HTAB_EMPTY_ENTRY)
4312 {
4313 node = ggc_cleared_alloc<addr_table_entry> ();
4314 init_addr_table_entry (node, kind, addr);
4315 *slot = node;
4316 }
4317 else
4318 node = *slot;
4319
4320 node->refcount++;
4321 return node;
4322 }
4323
4324 /* Remove an entry from the addr table by decrementing its refcount.
4325 Strictly, decrementing the refcount would be enough, but the
4326 assertion that the entry is actually in the table has found
4327 bugs. */
4328
4329 static void
4330 remove_addr_table_entry (addr_table_entry *entry)
4331 {
4332 gcc_assert (dwarf_split_debug_info && addr_index_table);
4333 /* After an index is assigned, the table is frozen. */
4334 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4335 entry->refcount--;
4336 }
4337
4338 /* Given a location list, remove all addresses it refers to from the
4339 address_table. */
4340
4341 static void
4342 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4343 {
4344 for (; descr; descr = descr->dw_loc_next)
4345 if (descr->dw_loc_oprnd1.val_entry != NULL)
4346 {
4347 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4348 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4349 }
4350 }
4351
4352 /* A helper function for dwarf2out_finish called through
4353 htab_traverse. Assign an addr_table_entry its index. All entries
4354 must be collected into the table when this function is called,
4355 because the indexing code relies on htab_traverse to traverse nodes
4356 in the same order for each run. */
4357
4358 int
4359 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4360 {
4361 addr_table_entry *node = *h;
4362
4363 /* Don't index unreferenced nodes. */
4364 if (node->refcount == 0)
4365 return 1;
4366
4367 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4368 node->index = *index;
4369 *index += 1;
4370
4371 return 1;
4372 }
4373
4374 /* Add an address constant attribute value to a DIE. When using
4375 dwarf_split_debug_info, address attributes in dies destined for the
4376 final executable should be direct references--setting the parameter
4377 force_direct ensures this behavior. */
4378
4379 static inline void
4380 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4381 bool force_direct)
4382 {
4383 dw_attr_node attr;
4384
4385 attr.dw_attr = attr_kind;
4386 attr.dw_attr_val.val_class = dw_val_class_addr;
4387 attr.dw_attr_val.v.val_addr = addr;
4388 if (dwarf_split_debug_info && !force_direct)
4389 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4390 else
4391 attr.dw_attr_val.val_entry = NULL;
4392 add_dwarf_attr (die, &attr);
4393 }
4394
4395 /* Get the RTX from to an address DIE attribute. */
4396
4397 static inline rtx
4398 AT_addr (dw_attr_ref a)
4399 {
4400 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4401 return a->dw_attr_val.v.val_addr;
4402 }
4403
4404 /* Add a file attribute value to a DIE. */
4405
4406 static inline void
4407 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4408 struct dwarf_file_data *fd)
4409 {
4410 dw_attr_node attr;
4411
4412 attr.dw_attr = attr_kind;
4413 attr.dw_attr_val.val_class = dw_val_class_file;
4414 attr.dw_attr_val.val_entry = NULL;
4415 attr.dw_attr_val.v.val_file = fd;
4416 add_dwarf_attr (die, &attr);
4417 }
4418
4419 /* Get the dwarf_file_data from a file DIE attribute. */
4420
4421 static inline struct dwarf_file_data *
4422 AT_file (dw_attr_ref a)
4423 {
4424 gcc_assert (a && AT_class (a) == dw_val_class_file);
4425 return a->dw_attr_val.v.val_file;
4426 }
4427
4428 /* Add a vms delta attribute value to a DIE. */
4429
4430 static inline void
4431 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4432 const char *lbl1, const char *lbl2)
4433 {
4434 dw_attr_node attr;
4435
4436 attr.dw_attr = attr_kind;
4437 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4438 attr.dw_attr_val.val_entry = NULL;
4439 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4440 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4441 add_dwarf_attr (die, &attr);
4442 }
4443
4444 /* Add a label identifier attribute value to a DIE. */
4445
4446 static inline void
4447 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4448 const char *lbl_id)
4449 {
4450 dw_attr_node attr;
4451
4452 attr.dw_attr = attr_kind;
4453 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4454 attr.dw_attr_val.val_entry = NULL;
4455 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4456 if (dwarf_split_debug_info)
4457 attr.dw_attr_val.val_entry
4458 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4459 ate_kind_label);
4460 add_dwarf_attr (die, &attr);
4461 }
4462
4463 /* Add a section offset attribute value to a DIE, an offset into the
4464 debug_line section. */
4465
4466 static inline void
4467 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4468 const char *label)
4469 {
4470 dw_attr_node attr;
4471
4472 attr.dw_attr = attr_kind;
4473 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4474 attr.dw_attr_val.val_entry = NULL;
4475 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4476 add_dwarf_attr (die, &attr);
4477 }
4478
4479 /* Add a section offset attribute value to a DIE, an offset into the
4480 debug_macinfo section. */
4481
4482 static inline void
4483 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4484 const char *label)
4485 {
4486 dw_attr_node attr;
4487
4488 attr.dw_attr = attr_kind;
4489 attr.dw_attr_val.val_class = dw_val_class_macptr;
4490 attr.dw_attr_val.val_entry = NULL;
4491 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4492 add_dwarf_attr (die, &attr);
4493 }
4494
4495 /* Add an offset attribute value to a DIE. */
4496
4497 static inline void
4498 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4499 unsigned HOST_WIDE_INT offset)
4500 {
4501 dw_attr_node attr;
4502
4503 attr.dw_attr = attr_kind;
4504 attr.dw_attr_val.val_class = dw_val_class_offset;
4505 attr.dw_attr_val.val_entry = NULL;
4506 attr.dw_attr_val.v.val_offset = offset;
4507 add_dwarf_attr (die, &attr);
4508 }
4509
4510 /* Add a range_list attribute value to a DIE. When using
4511 dwarf_split_debug_info, address attributes in dies destined for the
4512 final executable should be direct references--setting the parameter
4513 force_direct ensures this behavior. */
4514
4515 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4516 #define RELOCATED_OFFSET (NULL)
4517
4518 static void
4519 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4520 long unsigned int offset, bool force_direct)
4521 {
4522 dw_attr_node attr;
4523
4524 attr.dw_attr = attr_kind;
4525 attr.dw_attr_val.val_class = dw_val_class_range_list;
4526 /* For the range_list attribute, use val_entry to store whether the
4527 offset should follow split-debug-info or normal semantics. This
4528 value is read in output_range_list_offset. */
4529 if (dwarf_split_debug_info && !force_direct)
4530 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4531 else
4532 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4533 attr.dw_attr_val.v.val_offset = offset;
4534 add_dwarf_attr (die, &attr);
4535 }
4536
4537 /* Return the start label of a delta attribute. */
4538
4539 static inline const char *
4540 AT_vms_delta1 (dw_attr_ref a)
4541 {
4542 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4543 return a->dw_attr_val.v.val_vms_delta.lbl1;
4544 }
4545
4546 /* Return the end label of a delta attribute. */
4547
4548 static inline const char *
4549 AT_vms_delta2 (dw_attr_ref a)
4550 {
4551 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4552 return a->dw_attr_val.v.val_vms_delta.lbl2;
4553 }
4554
4555 static inline const char *
4556 AT_lbl (dw_attr_ref a)
4557 {
4558 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4559 || AT_class (a) == dw_val_class_lineptr
4560 || AT_class (a) == dw_val_class_macptr
4561 || AT_class (a) == dw_val_class_high_pc));
4562 return a->dw_attr_val.v.val_lbl_id;
4563 }
4564
4565 /* Get the attribute of type attr_kind. */
4566
4567 static dw_attr_ref
4568 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4569 {
4570 dw_attr_ref a;
4571 unsigned ix;
4572 dw_die_ref spec = NULL;
4573
4574 if (! die)
4575 return NULL;
4576
4577 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4578 if (a->dw_attr == attr_kind)
4579 return a;
4580 else if (a->dw_attr == DW_AT_specification
4581 || a->dw_attr == DW_AT_abstract_origin)
4582 spec = AT_ref (a);
4583
4584 if (spec)
4585 return get_AT (spec, attr_kind);
4586
4587 return NULL;
4588 }
4589
4590 /* Returns the parent of the declaration of DIE. */
4591
4592 static dw_die_ref
4593 get_die_parent (dw_die_ref die)
4594 {
4595 dw_die_ref t;
4596
4597 if (!die)
4598 return NULL;
4599
4600 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4601 || (t = get_AT_ref (die, DW_AT_specification)))
4602 die = t;
4603
4604 return die->die_parent;
4605 }
4606
4607 /* Return the "low pc" attribute value, typically associated with a subprogram
4608 DIE. Return null if the "low pc" attribute is either not present, or if it
4609 cannot be represented as an assembler label identifier. */
4610
4611 static inline const char *
4612 get_AT_low_pc (dw_die_ref die)
4613 {
4614 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4615
4616 return a ? AT_lbl (a) : NULL;
4617 }
4618
4619 /* Return the "high pc" attribute value, typically associated with a subprogram
4620 DIE. Return null if the "high pc" attribute is either not present, or if it
4621 cannot be represented as an assembler label identifier. */
4622
4623 static inline const char *
4624 get_AT_hi_pc (dw_die_ref die)
4625 {
4626 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4627
4628 return a ? AT_lbl (a) : NULL;
4629 }
4630
4631 /* Return the value of the string attribute designated by ATTR_KIND, or
4632 NULL if it is not present. */
4633
4634 static inline const char *
4635 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4636 {
4637 dw_attr_ref a = get_AT (die, attr_kind);
4638
4639 return a ? AT_string (a) : NULL;
4640 }
4641
4642 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4643 if it is not present. */
4644
4645 static inline int
4646 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4647 {
4648 dw_attr_ref a = get_AT (die, attr_kind);
4649
4650 return a ? AT_flag (a) : 0;
4651 }
4652
4653 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4654 if it is not present. */
4655
4656 static inline unsigned
4657 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4658 {
4659 dw_attr_ref a = get_AT (die, attr_kind);
4660
4661 return a ? AT_unsigned (a) : 0;
4662 }
4663
4664 static inline dw_die_ref
4665 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4666 {
4667 dw_attr_ref a = get_AT (die, attr_kind);
4668
4669 return a ? AT_ref (a) : NULL;
4670 }
4671
4672 static inline struct dwarf_file_data *
4673 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4674 {
4675 dw_attr_ref a = get_AT (die, attr_kind);
4676
4677 return a ? AT_file (a) : NULL;
4678 }
4679
4680 /* Return TRUE if the language is C++. */
4681
4682 static inline bool
4683 is_cxx (void)
4684 {
4685 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4686
4687 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4688 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4689 }
4690
4691 /* Return TRUE if the language is Java. */
4692
4693 static inline bool
4694 is_java (void)
4695 {
4696 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4697
4698 return lang == DW_LANG_Java;
4699 }
4700
4701 /* Return TRUE if the language is Fortran. */
4702
4703 static inline bool
4704 is_fortran (void)
4705 {
4706 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4707
4708 return (lang == DW_LANG_Fortran77
4709 || lang == DW_LANG_Fortran90
4710 || lang == DW_LANG_Fortran95);
4711 }
4712
4713 /* Return TRUE if the language is Ada. */
4714
4715 static inline bool
4716 is_ada (void)
4717 {
4718 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4719
4720 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4721 }
4722
4723 /* Remove the specified attribute if present. */
4724
4725 static void
4726 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4727 {
4728 dw_attr_ref a;
4729 unsigned ix;
4730
4731 if (! die)
4732 return;
4733
4734 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4735 if (a->dw_attr == attr_kind)
4736 {
4737 if (AT_class (a) == dw_val_class_str)
4738 if (a->dw_attr_val.v.val_str->refcount)
4739 a->dw_attr_val.v.val_str->refcount--;
4740
4741 /* vec::ordered_remove should help reduce the number of abbrevs
4742 that are needed. */
4743 die->die_attr->ordered_remove (ix);
4744 return;
4745 }
4746 }
4747
4748 /* Remove CHILD from its parent. PREV must have the property that
4749 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4750
4751 static void
4752 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4753 {
4754 gcc_assert (child->die_parent == prev->die_parent);
4755 gcc_assert (prev->die_sib == child);
4756 if (prev == child)
4757 {
4758 gcc_assert (child->die_parent->die_child == child);
4759 prev = NULL;
4760 }
4761 else
4762 prev->die_sib = child->die_sib;
4763 if (child->die_parent->die_child == child)
4764 child->die_parent->die_child = prev;
4765 }
4766
4767 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4768 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4769
4770 static void
4771 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4772 {
4773 dw_die_ref parent = old_child->die_parent;
4774
4775 gcc_assert (parent == prev->die_parent);
4776 gcc_assert (prev->die_sib == old_child);
4777
4778 new_child->die_parent = parent;
4779 if (prev == old_child)
4780 {
4781 gcc_assert (parent->die_child == old_child);
4782 new_child->die_sib = new_child;
4783 }
4784 else
4785 {
4786 prev->die_sib = new_child;
4787 new_child->die_sib = old_child->die_sib;
4788 }
4789 if (old_child->die_parent->die_child == old_child)
4790 old_child->die_parent->die_child = new_child;
4791 }
4792
4793 /* Move all children from OLD_PARENT to NEW_PARENT. */
4794
4795 static void
4796 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4797 {
4798 dw_die_ref c;
4799 new_parent->die_child = old_parent->die_child;
4800 old_parent->die_child = NULL;
4801 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4802 }
4803
4804 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4805 matches TAG. */
4806
4807 static void
4808 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4809 {
4810 dw_die_ref c;
4811
4812 c = die->die_child;
4813 if (c) do {
4814 dw_die_ref prev = c;
4815 c = c->die_sib;
4816 while (c->die_tag == tag)
4817 {
4818 remove_child_with_prev (c, prev);
4819 /* Might have removed every child. */
4820 if (c == c->die_sib)
4821 return;
4822 c = c->die_sib;
4823 }
4824 } while (c != die->die_child);
4825 }
4826
4827 /* Add a CHILD_DIE as the last child of DIE. */
4828
4829 static void
4830 add_child_die (dw_die_ref die, dw_die_ref child_die)
4831 {
4832 /* FIXME this should probably be an assert. */
4833 if (! die || ! child_die)
4834 return;
4835 gcc_assert (die != child_die);
4836
4837 child_die->die_parent = die;
4838 if (die->die_child)
4839 {
4840 child_die->die_sib = die->die_child->die_sib;
4841 die->die_child->die_sib = child_die;
4842 }
4843 else
4844 child_die->die_sib = child_die;
4845 die->die_child = child_die;
4846 }
4847
4848 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4849 is the specification, to the end of PARENT's list of children.
4850 This is done by removing and re-adding it. */
4851
4852 static void
4853 splice_child_die (dw_die_ref parent, dw_die_ref child)
4854 {
4855 dw_die_ref p;
4856
4857 /* We want the declaration DIE from inside the class, not the
4858 specification DIE at toplevel. */
4859 if (child->die_parent != parent)
4860 {
4861 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4862
4863 if (tmp)
4864 child = tmp;
4865 }
4866
4867 gcc_assert (child->die_parent == parent
4868 || (child->die_parent
4869 == get_AT_ref (parent, DW_AT_specification)));
4870
4871 for (p = child->die_parent->die_child; ; p = p->die_sib)
4872 if (p->die_sib == child)
4873 {
4874 remove_child_with_prev (child, p);
4875 break;
4876 }
4877
4878 add_child_die (parent, child);
4879 }
4880
4881 /* Return a pointer to a newly created DIE node. */
4882
4883 static inline dw_die_ref
4884 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4885 {
4886 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4887
4888 die->die_tag = tag_value;
4889
4890 if (parent_die != NULL)
4891 add_child_die (parent_die, die);
4892 else
4893 {
4894 limbo_die_node *limbo_node;
4895
4896 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4897 limbo_node->die = die;
4898 limbo_node->created_for = t;
4899 limbo_node->next = limbo_die_list;
4900 limbo_die_list = limbo_node;
4901 }
4902
4903 return die;
4904 }
4905
4906 /* Return the DIE associated with the given type specifier. */
4907
4908 static inline dw_die_ref
4909 lookup_type_die (tree type)
4910 {
4911 return TYPE_SYMTAB_DIE (type);
4912 }
4913
4914 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4915 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4916 anonymous type instead the one of the naming typedef. */
4917
4918 static inline dw_die_ref
4919 strip_naming_typedef (tree type, dw_die_ref type_die)
4920 {
4921 if (type
4922 && TREE_CODE (type) == RECORD_TYPE
4923 && type_die
4924 && type_die->die_tag == DW_TAG_typedef
4925 && is_naming_typedef_decl (TYPE_NAME (type)))
4926 type_die = get_AT_ref (type_die, DW_AT_type);
4927 return type_die;
4928 }
4929
4930 /* Like lookup_type_die, but if type is an anonymous type named by a
4931 typedef[1], return the DIE of the anonymous type instead the one of
4932 the naming typedef. This is because in gen_typedef_die, we did
4933 equate the anonymous struct named by the typedef with the DIE of
4934 the naming typedef. So by default, lookup_type_die on an anonymous
4935 struct yields the DIE of the naming typedef.
4936
4937 [1]: Read the comment of is_naming_typedef_decl to learn about what
4938 a naming typedef is. */
4939
4940 static inline dw_die_ref
4941 lookup_type_die_strip_naming_typedef (tree type)
4942 {
4943 dw_die_ref die = lookup_type_die (type);
4944 return strip_naming_typedef (type, die);
4945 }
4946
4947 /* Equate a DIE to a given type specifier. */
4948
4949 static inline void
4950 equate_type_number_to_die (tree type, dw_die_ref type_die)
4951 {
4952 TYPE_SYMTAB_DIE (type) = type_die;
4953 }
4954
4955 /* Returns a hash value for X (which really is a die_struct). */
4956
4957 inline hashval_t
4958 decl_die_hasher::hash (die_node *x)
4959 {
4960 return (hashval_t) x->decl_id;
4961 }
4962
4963 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4964
4965 inline bool
4966 decl_die_hasher::equal (die_node *x, tree y)
4967 {
4968 return (x->decl_id == DECL_UID (y));
4969 }
4970
4971 /* Return the DIE associated with a given declaration. */
4972
4973 static inline dw_die_ref
4974 lookup_decl_die (tree decl)
4975 {
4976 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
4977 }
4978
4979 /* Returns a hash value for X (which really is a var_loc_list). */
4980
4981 inline hashval_t
4982 decl_loc_hasher::hash (var_loc_list *x)
4983 {
4984 return (hashval_t) x->decl_id;
4985 }
4986
4987 /* Return nonzero if decl_id of var_loc_list X is the same as
4988 UID of decl *Y. */
4989
4990 inline bool
4991 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
4992 {
4993 return (x->decl_id == DECL_UID (y));
4994 }
4995
4996 /* Return the var_loc list associated with a given declaration. */
4997
4998 static inline var_loc_list *
4999 lookup_decl_loc (const_tree decl)
5000 {
5001 if (!decl_loc_table)
5002 return NULL;
5003 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5004 }
5005
5006 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5007
5008 inline hashval_t
5009 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5010 {
5011 return (hashval_t) x->decl_id;
5012 }
5013
5014 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5015 UID of decl *Y. */
5016
5017 inline bool
5018 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5019 {
5020 return (x->decl_id == DECL_UID (y));
5021 }
5022
5023 /* Equate a DIE to a particular declaration. */
5024
5025 static void
5026 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5027 {
5028 unsigned int decl_id = DECL_UID (decl);
5029
5030 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5031 decl_die->decl_id = decl_id;
5032 }
5033
5034 /* Return how many bits covers PIECE EXPR_LIST. */
5035
5036 static int
5037 decl_piece_bitsize (rtx piece)
5038 {
5039 int ret = (int) GET_MODE (piece);
5040 if (ret)
5041 return ret;
5042 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5043 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5044 return INTVAL (XEXP (XEXP (piece, 0), 0));
5045 }
5046
5047 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5048
5049 static rtx *
5050 decl_piece_varloc_ptr (rtx piece)
5051 {
5052 if ((int) GET_MODE (piece))
5053 return &XEXP (piece, 0);
5054 else
5055 return &XEXP (XEXP (piece, 0), 1);
5056 }
5057
5058 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5059 Next is the chain of following piece nodes. */
5060
5061 static rtx_expr_list *
5062 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5063 {
5064 if (bitsize <= (int) MAX_MACHINE_MODE)
5065 return alloc_EXPR_LIST (bitsize, loc_note, next);
5066 else
5067 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5068 GEN_INT (bitsize),
5069 loc_note), next);
5070 }
5071
5072 /* Return rtx that should be stored into loc field for
5073 LOC_NOTE and BITPOS/BITSIZE. */
5074
5075 static rtx
5076 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5077 HOST_WIDE_INT bitsize)
5078 {
5079 if (bitsize != -1)
5080 {
5081 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5082 if (bitpos != 0)
5083 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5084 }
5085 return loc_note;
5086 }
5087
5088 /* This function either modifies location piece list *DEST in
5089 place (if SRC and INNER is NULL), or copies location piece list
5090 *SRC to *DEST while modifying it. Location BITPOS is modified
5091 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5092 not copied and if needed some padding around it is added.
5093 When modifying in place, DEST should point to EXPR_LIST where
5094 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5095 to the start of the whole list and INNER points to the EXPR_LIST
5096 where earlier pieces cover PIECE_BITPOS bits. */
5097
5098 static void
5099 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5100 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5101 HOST_WIDE_INT bitsize, rtx loc_note)
5102 {
5103 int diff;
5104 bool copy = inner != NULL;
5105
5106 if (copy)
5107 {
5108 /* First copy all nodes preceding the current bitpos. */
5109 while (src != inner)
5110 {
5111 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5112 decl_piece_bitsize (*src), NULL_RTX);
5113 dest = &XEXP (*dest, 1);
5114 src = &XEXP (*src, 1);
5115 }
5116 }
5117 /* Add padding if needed. */
5118 if (bitpos != piece_bitpos)
5119 {
5120 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5121 copy ? NULL_RTX : *dest);
5122 dest = &XEXP (*dest, 1);
5123 }
5124 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5125 {
5126 gcc_assert (!copy);
5127 /* A piece with correct bitpos and bitsize already exist,
5128 just update the location for it and return. */
5129 *decl_piece_varloc_ptr (*dest) = loc_note;
5130 return;
5131 }
5132 /* Add the piece that changed. */
5133 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5134 dest = &XEXP (*dest, 1);
5135 /* Skip over pieces that overlap it. */
5136 diff = bitpos - piece_bitpos + bitsize;
5137 if (!copy)
5138 src = dest;
5139 while (diff > 0 && *src)
5140 {
5141 rtx piece = *src;
5142 diff -= decl_piece_bitsize (piece);
5143 if (copy)
5144 src = &XEXP (piece, 1);
5145 else
5146 {
5147 *src = XEXP (piece, 1);
5148 free_EXPR_LIST_node (piece);
5149 }
5150 }
5151 /* Add padding if needed. */
5152 if (diff < 0 && *src)
5153 {
5154 if (!copy)
5155 dest = src;
5156 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5157 dest = &XEXP (*dest, 1);
5158 }
5159 if (!copy)
5160 return;
5161 /* Finally copy all nodes following it. */
5162 while (*src)
5163 {
5164 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5165 decl_piece_bitsize (*src), NULL_RTX);
5166 dest = &XEXP (*dest, 1);
5167 src = &XEXP (*src, 1);
5168 }
5169 }
5170
5171 /* Add a variable location node to the linked list for DECL. */
5172
5173 static struct var_loc_node *
5174 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5175 {
5176 unsigned int decl_id;
5177 var_loc_list *temp;
5178 struct var_loc_node *loc = NULL;
5179 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5180
5181 if (TREE_CODE (decl) == VAR_DECL
5182 && DECL_HAS_DEBUG_EXPR_P (decl))
5183 {
5184 tree realdecl = DECL_DEBUG_EXPR (decl);
5185 if (handled_component_p (realdecl)
5186 || (TREE_CODE (realdecl) == MEM_REF
5187 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5188 {
5189 HOST_WIDE_INT maxsize;
5190 tree innerdecl;
5191 innerdecl
5192 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5193 if (!DECL_P (innerdecl)
5194 || DECL_IGNORED_P (innerdecl)
5195 || TREE_STATIC (innerdecl)
5196 || bitsize <= 0
5197 || bitpos + bitsize > 256
5198 || bitsize != maxsize)
5199 return NULL;
5200 decl = innerdecl;
5201 }
5202 }
5203
5204 decl_id = DECL_UID (decl);
5205 var_loc_list **slot
5206 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5207 if (*slot == NULL)
5208 {
5209 temp = ggc_cleared_alloc<var_loc_list> ();
5210 temp->decl_id = decl_id;
5211 *slot = temp;
5212 }
5213 else
5214 temp = *slot;
5215
5216 /* For PARM_DECLs try to keep around the original incoming value,
5217 even if that means we'll emit a zero-range .debug_loc entry. */
5218 if (temp->last
5219 && temp->first == temp->last
5220 && TREE_CODE (decl) == PARM_DECL
5221 && NOTE_P (temp->first->loc)
5222 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5223 && DECL_INCOMING_RTL (decl)
5224 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5225 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5226 == GET_CODE (DECL_INCOMING_RTL (decl))
5227 && prev_real_insn (temp->first->loc) == NULL_RTX
5228 && (bitsize != -1
5229 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5230 NOTE_VAR_LOCATION_LOC (loc_note))
5231 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5232 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5233 {
5234 loc = ggc_cleared_alloc<var_loc_node> ();
5235 temp->first->next = loc;
5236 temp->last = loc;
5237 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5238 }
5239 else if (temp->last)
5240 {
5241 struct var_loc_node *last = temp->last, *unused = NULL;
5242 rtx *piece_loc = NULL, last_loc_note;
5243 int piece_bitpos = 0;
5244 if (last->next)
5245 {
5246 last = last->next;
5247 gcc_assert (last->next == NULL);
5248 }
5249 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5250 {
5251 piece_loc = &last->loc;
5252 do
5253 {
5254 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5255 if (piece_bitpos + cur_bitsize > bitpos)
5256 break;
5257 piece_bitpos += cur_bitsize;
5258 piece_loc = &XEXP (*piece_loc, 1);
5259 }
5260 while (*piece_loc);
5261 }
5262 /* TEMP->LAST here is either pointer to the last but one or
5263 last element in the chained list, LAST is pointer to the
5264 last element. */
5265 if (label && strcmp (last->label, label) == 0)
5266 {
5267 /* For SRA optimized variables if there weren't any real
5268 insns since last note, just modify the last node. */
5269 if (piece_loc != NULL)
5270 {
5271 adjust_piece_list (piece_loc, NULL, NULL,
5272 bitpos, piece_bitpos, bitsize, loc_note);
5273 return NULL;
5274 }
5275 /* If the last note doesn't cover any instructions, remove it. */
5276 if (temp->last != last)
5277 {
5278 temp->last->next = NULL;
5279 unused = last;
5280 last = temp->last;
5281 gcc_assert (strcmp (last->label, label) != 0);
5282 }
5283 else
5284 {
5285 gcc_assert (temp->first == temp->last
5286 || (temp->first->next == temp->last
5287 && TREE_CODE (decl) == PARM_DECL));
5288 memset (temp->last, '\0', sizeof (*temp->last));
5289 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5290 return temp->last;
5291 }
5292 }
5293 if (bitsize == -1 && NOTE_P (last->loc))
5294 last_loc_note = last->loc;
5295 else if (piece_loc != NULL
5296 && *piece_loc != NULL_RTX
5297 && piece_bitpos == bitpos
5298 && decl_piece_bitsize (*piece_loc) == bitsize)
5299 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5300 else
5301 last_loc_note = NULL_RTX;
5302 /* If the current location is the same as the end of the list,
5303 and either both or neither of the locations is uninitialized,
5304 we have nothing to do. */
5305 if (last_loc_note == NULL_RTX
5306 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5307 NOTE_VAR_LOCATION_LOC (loc_note)))
5308 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5309 != NOTE_VAR_LOCATION_STATUS (loc_note))
5310 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5311 == VAR_INIT_STATUS_UNINITIALIZED)
5312 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5313 == VAR_INIT_STATUS_UNINITIALIZED))))
5314 {
5315 /* Add LOC to the end of list and update LAST. If the last
5316 element of the list has been removed above, reuse its
5317 memory for the new node, otherwise allocate a new one. */
5318 if (unused)
5319 {
5320 loc = unused;
5321 memset (loc, '\0', sizeof (*loc));
5322 }
5323 else
5324 loc = ggc_cleared_alloc<var_loc_node> ();
5325 if (bitsize == -1 || piece_loc == NULL)
5326 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5327 else
5328 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5329 bitpos, piece_bitpos, bitsize, loc_note);
5330 last->next = loc;
5331 /* Ensure TEMP->LAST will point either to the new last but one
5332 element of the chain, or to the last element in it. */
5333 if (last != temp->last)
5334 temp->last = last;
5335 }
5336 else if (unused)
5337 ggc_free (unused);
5338 }
5339 else
5340 {
5341 loc = ggc_cleared_alloc<var_loc_node> ();
5342 temp->first = loc;
5343 temp->last = loc;
5344 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5345 }
5346 return loc;
5347 }
5348 \f
5349 /* Keep track of the number of spaces used to indent the
5350 output of the debugging routines that print the structure of
5351 the DIE internal representation. */
5352 static int print_indent;
5353
5354 /* Indent the line the number of spaces given by print_indent. */
5355
5356 static inline void
5357 print_spaces (FILE *outfile)
5358 {
5359 fprintf (outfile, "%*s", print_indent, "");
5360 }
5361
5362 /* Print a type signature in hex. */
5363
5364 static inline void
5365 print_signature (FILE *outfile, char *sig)
5366 {
5367 int i;
5368
5369 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5370 fprintf (outfile, "%02x", sig[i] & 0xff);
5371 }
5372
5373 /* Print the information associated with a given DIE, and its children.
5374 This routine is a debugging aid only. */
5375
5376 static void
5377 print_die (dw_die_ref die, FILE *outfile)
5378 {
5379 dw_attr_ref a;
5380 dw_die_ref c;
5381 unsigned ix;
5382
5383 print_spaces (outfile);
5384 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5385 die->die_offset, dwarf_tag_name (die->die_tag),
5386 (void*) die);
5387 print_spaces (outfile);
5388 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5389 fprintf (outfile, " offset: %ld", die->die_offset);
5390 fprintf (outfile, " mark: %d\n", die->die_mark);
5391
5392 if (die->comdat_type_p)
5393 {
5394 print_spaces (outfile);
5395 fprintf (outfile, " signature: ");
5396 print_signature (outfile, die->die_id.die_type_node->signature);
5397 fprintf (outfile, "\n");
5398 }
5399
5400 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5401 {
5402 print_spaces (outfile);
5403 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5404
5405 switch (AT_class (a))
5406 {
5407 case dw_val_class_addr:
5408 fprintf (outfile, "address");
5409 break;
5410 case dw_val_class_offset:
5411 fprintf (outfile, "offset");
5412 break;
5413 case dw_val_class_loc:
5414 fprintf (outfile, "location descriptor");
5415 break;
5416 case dw_val_class_loc_list:
5417 fprintf (outfile, "location list -> label:%s",
5418 AT_loc_list (a)->ll_symbol);
5419 break;
5420 case dw_val_class_range_list:
5421 fprintf (outfile, "range list");
5422 break;
5423 case dw_val_class_const:
5424 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5425 break;
5426 case dw_val_class_unsigned_const:
5427 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5428 break;
5429 case dw_val_class_const_double:
5430 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5431 HOST_WIDE_INT_PRINT_UNSIGNED")",
5432 a->dw_attr_val.v.val_double.high,
5433 a->dw_attr_val.v.val_double.low);
5434 break;
5435 case dw_val_class_wide_int:
5436 {
5437 int i = a->dw_attr_val.v.val_wide->get_len ();
5438 fprintf (outfile, "constant (");
5439 gcc_assert (i > 0);
5440 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5441 fprintf (outfile, "0x");
5442 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5443 a->dw_attr_val.v.val_wide->elt (--i));
5444 while (--i >= 0)
5445 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5446 a->dw_attr_val.v.val_wide->elt (i));
5447 fprintf (outfile, ")");
5448 break;
5449 }
5450 case dw_val_class_vec:
5451 fprintf (outfile, "floating-point or vector constant");
5452 break;
5453 case dw_val_class_flag:
5454 fprintf (outfile, "%u", AT_flag (a));
5455 break;
5456 case dw_val_class_die_ref:
5457 if (AT_ref (a) != NULL)
5458 {
5459 if (AT_ref (a)->comdat_type_p)
5460 {
5461 fprintf (outfile, "die -> signature: ");
5462 print_signature (outfile,
5463 AT_ref (a)->die_id.die_type_node->signature);
5464 }
5465 else if (AT_ref (a)->die_id.die_symbol)
5466 fprintf (outfile, "die -> label: %s",
5467 AT_ref (a)->die_id.die_symbol);
5468 else
5469 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5470 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5471 }
5472 else
5473 fprintf (outfile, "die -> <null>");
5474 break;
5475 case dw_val_class_vms_delta:
5476 fprintf (outfile, "delta: @slotcount(%s-%s)",
5477 AT_vms_delta2 (a), AT_vms_delta1 (a));
5478 break;
5479 case dw_val_class_lbl_id:
5480 case dw_val_class_lineptr:
5481 case dw_val_class_macptr:
5482 case dw_val_class_high_pc:
5483 fprintf (outfile, "label: %s", AT_lbl (a));
5484 break;
5485 case dw_val_class_str:
5486 if (AT_string (a) != NULL)
5487 fprintf (outfile, "\"%s\"", AT_string (a));
5488 else
5489 fprintf (outfile, "<null>");
5490 break;
5491 case dw_val_class_file:
5492 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5493 AT_file (a)->emitted_number);
5494 break;
5495 case dw_val_class_data8:
5496 {
5497 int i;
5498
5499 for (i = 0; i < 8; i++)
5500 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5501 break;
5502 }
5503 default:
5504 break;
5505 }
5506
5507 fprintf (outfile, "\n");
5508 }
5509
5510 if (die->die_child != NULL)
5511 {
5512 print_indent += 4;
5513 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5514 print_indent -= 4;
5515 }
5516 if (print_indent == 0)
5517 fprintf (outfile, "\n");
5518 }
5519
5520 /* Print the information collected for a given DIE. */
5521
5522 DEBUG_FUNCTION void
5523 debug_dwarf_die (dw_die_ref die)
5524 {
5525 print_die (die, stderr);
5526 }
5527
5528 DEBUG_FUNCTION void
5529 debug (die_struct &ref)
5530 {
5531 print_die (&ref, stderr);
5532 }
5533
5534 DEBUG_FUNCTION void
5535 debug (die_struct *ptr)
5536 {
5537 if (ptr)
5538 debug (*ptr);
5539 else
5540 fprintf (stderr, "<nil>\n");
5541 }
5542
5543
5544 /* Print all DWARF information collected for the compilation unit.
5545 This routine is a debugging aid only. */
5546
5547 DEBUG_FUNCTION void
5548 debug_dwarf (void)
5549 {
5550 print_indent = 0;
5551 print_die (comp_unit_die (), stderr);
5552 }
5553 \f
5554 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5555 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5556 DIE that marks the start of the DIEs for this include file. */
5557
5558 static dw_die_ref
5559 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5560 {
5561 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5562 dw_die_ref new_unit = gen_compile_unit_die (filename);
5563
5564 new_unit->die_sib = old_unit;
5565 return new_unit;
5566 }
5567
5568 /* Close an include-file CU and reopen the enclosing one. */
5569
5570 static dw_die_ref
5571 pop_compile_unit (dw_die_ref old_unit)
5572 {
5573 dw_die_ref new_unit = old_unit->die_sib;
5574
5575 old_unit->die_sib = NULL;
5576 return new_unit;
5577 }
5578
5579 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5580 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5581 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5582
5583 /* Calculate the checksum of a location expression. */
5584
5585 static inline void
5586 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5587 {
5588 int tem;
5589 inchash::hash hstate;
5590 hashval_t hash;
5591
5592 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5593 CHECKSUM (tem);
5594 hash_loc_operands (loc, hstate);
5595 hash = hstate.end();
5596 CHECKSUM (hash);
5597 }
5598
5599 /* Calculate the checksum of an attribute. */
5600
5601 static void
5602 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5603 {
5604 dw_loc_descr_ref loc;
5605 rtx r;
5606
5607 CHECKSUM (at->dw_attr);
5608
5609 /* We don't care that this was compiled with a different compiler
5610 snapshot; if the output is the same, that's what matters. */
5611 if (at->dw_attr == DW_AT_producer)
5612 return;
5613
5614 switch (AT_class (at))
5615 {
5616 case dw_val_class_const:
5617 CHECKSUM (at->dw_attr_val.v.val_int);
5618 break;
5619 case dw_val_class_unsigned_const:
5620 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5621 break;
5622 case dw_val_class_const_double:
5623 CHECKSUM (at->dw_attr_val.v.val_double);
5624 break;
5625 case dw_val_class_wide_int:
5626 CHECKSUM (*at->dw_attr_val.v.val_wide);
5627 break;
5628 case dw_val_class_vec:
5629 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5630 (at->dw_attr_val.v.val_vec.length
5631 * at->dw_attr_val.v.val_vec.elt_size));
5632 break;
5633 case dw_val_class_flag:
5634 CHECKSUM (at->dw_attr_val.v.val_flag);
5635 break;
5636 case dw_val_class_str:
5637 CHECKSUM_STRING (AT_string (at));
5638 break;
5639
5640 case dw_val_class_addr:
5641 r = AT_addr (at);
5642 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5643 CHECKSUM_STRING (XSTR (r, 0));
5644 break;
5645
5646 case dw_val_class_offset:
5647 CHECKSUM (at->dw_attr_val.v.val_offset);
5648 break;
5649
5650 case dw_val_class_loc:
5651 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5652 loc_checksum (loc, ctx);
5653 break;
5654
5655 case dw_val_class_die_ref:
5656 die_checksum (AT_ref (at), ctx, mark);
5657 break;
5658
5659 case dw_val_class_fde_ref:
5660 case dw_val_class_vms_delta:
5661 case dw_val_class_lbl_id:
5662 case dw_val_class_lineptr:
5663 case dw_val_class_macptr:
5664 case dw_val_class_high_pc:
5665 break;
5666
5667 case dw_val_class_file:
5668 CHECKSUM_STRING (AT_file (at)->filename);
5669 break;
5670
5671 case dw_val_class_data8:
5672 CHECKSUM (at->dw_attr_val.v.val_data8);
5673 break;
5674
5675 default:
5676 break;
5677 }
5678 }
5679
5680 /* Calculate the checksum of a DIE. */
5681
5682 static void
5683 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5684 {
5685 dw_die_ref c;
5686 dw_attr_ref a;
5687 unsigned ix;
5688
5689 /* To avoid infinite recursion. */
5690 if (die->die_mark)
5691 {
5692 CHECKSUM (die->die_mark);
5693 return;
5694 }
5695 die->die_mark = ++(*mark);
5696
5697 CHECKSUM (die->die_tag);
5698
5699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5700 attr_checksum (a, ctx, mark);
5701
5702 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5703 }
5704
5705 #undef CHECKSUM
5706 #undef CHECKSUM_BLOCK
5707 #undef CHECKSUM_STRING
5708
5709 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5710 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5711 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5712 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5713 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5714 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5715 #define CHECKSUM_ATTR(FOO) \
5716 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5717
5718 /* Calculate the checksum of a number in signed LEB128 format. */
5719
5720 static void
5721 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5722 {
5723 unsigned char byte;
5724 bool more;
5725
5726 while (1)
5727 {
5728 byte = (value & 0x7f);
5729 value >>= 7;
5730 more = !((value == 0 && (byte & 0x40) == 0)
5731 || (value == -1 && (byte & 0x40) != 0));
5732 if (more)
5733 byte |= 0x80;
5734 CHECKSUM (byte);
5735 if (!more)
5736 break;
5737 }
5738 }
5739
5740 /* Calculate the checksum of a number in unsigned LEB128 format. */
5741
5742 static void
5743 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5744 {
5745 while (1)
5746 {
5747 unsigned char byte = (value & 0x7f);
5748 value >>= 7;
5749 if (value != 0)
5750 /* More bytes to follow. */
5751 byte |= 0x80;
5752 CHECKSUM (byte);
5753 if (value == 0)
5754 break;
5755 }
5756 }
5757
5758 /* Checksum the context of the DIE. This adds the names of any
5759 surrounding namespaces or structures to the checksum. */
5760
5761 static void
5762 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5763 {
5764 const char *name;
5765 dw_die_ref spec;
5766 int tag = die->die_tag;
5767
5768 if (tag != DW_TAG_namespace
5769 && tag != DW_TAG_structure_type
5770 && tag != DW_TAG_class_type)
5771 return;
5772
5773 name = get_AT_string (die, DW_AT_name);
5774
5775 spec = get_AT_ref (die, DW_AT_specification);
5776 if (spec != NULL)
5777 die = spec;
5778
5779 if (die->die_parent != NULL)
5780 checksum_die_context (die->die_parent, ctx);
5781
5782 CHECKSUM_ULEB128 ('C');
5783 CHECKSUM_ULEB128 (tag);
5784 if (name != NULL)
5785 CHECKSUM_STRING (name);
5786 }
5787
5788 /* Calculate the checksum of a location expression. */
5789
5790 static inline void
5791 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5792 {
5793 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5794 were emitted as a DW_FORM_sdata instead of a location expression. */
5795 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5796 {
5797 CHECKSUM_ULEB128 (DW_FORM_sdata);
5798 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5799 return;
5800 }
5801
5802 /* Otherwise, just checksum the raw location expression. */
5803 while (loc != NULL)
5804 {
5805 inchash::hash hstate;
5806 hashval_t hash;
5807
5808 CHECKSUM_ULEB128 (loc->dtprel);
5809 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5810 hash_loc_operands (loc, hstate);
5811 hash = hstate.end ();
5812 CHECKSUM (hash);
5813 loc = loc->dw_loc_next;
5814 }
5815 }
5816
5817 /* Calculate the checksum of an attribute. */
5818
5819 static void
5820 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5821 struct md5_ctx *ctx, int *mark)
5822 {
5823 dw_loc_descr_ref loc;
5824 rtx r;
5825
5826 if (AT_class (at) == dw_val_class_die_ref)
5827 {
5828 dw_die_ref target_die = AT_ref (at);
5829
5830 /* For pointer and reference types, we checksum only the (qualified)
5831 name of the target type (if there is a name). For friend entries,
5832 we checksum only the (qualified) name of the target type or function.
5833 This allows the checksum to remain the same whether the target type
5834 is complete or not. */
5835 if ((at->dw_attr == DW_AT_type
5836 && (tag == DW_TAG_pointer_type
5837 || tag == DW_TAG_reference_type
5838 || tag == DW_TAG_rvalue_reference_type
5839 || tag == DW_TAG_ptr_to_member_type))
5840 || (at->dw_attr == DW_AT_friend
5841 && tag == DW_TAG_friend))
5842 {
5843 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5844
5845 if (name_attr != NULL)
5846 {
5847 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5848
5849 if (decl == NULL)
5850 decl = target_die;
5851 CHECKSUM_ULEB128 ('N');
5852 CHECKSUM_ULEB128 (at->dw_attr);
5853 if (decl->die_parent != NULL)
5854 checksum_die_context (decl->die_parent, ctx);
5855 CHECKSUM_ULEB128 ('E');
5856 CHECKSUM_STRING (AT_string (name_attr));
5857 return;
5858 }
5859 }
5860
5861 /* For all other references to another DIE, we check to see if the
5862 target DIE has already been visited. If it has, we emit a
5863 backward reference; if not, we descend recursively. */
5864 if (target_die->die_mark > 0)
5865 {
5866 CHECKSUM_ULEB128 ('R');
5867 CHECKSUM_ULEB128 (at->dw_attr);
5868 CHECKSUM_ULEB128 (target_die->die_mark);
5869 }
5870 else
5871 {
5872 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5873
5874 if (decl == NULL)
5875 decl = target_die;
5876 target_die->die_mark = ++(*mark);
5877 CHECKSUM_ULEB128 ('T');
5878 CHECKSUM_ULEB128 (at->dw_attr);
5879 if (decl->die_parent != NULL)
5880 checksum_die_context (decl->die_parent, ctx);
5881 die_checksum_ordered (target_die, ctx, mark);
5882 }
5883 return;
5884 }
5885
5886 CHECKSUM_ULEB128 ('A');
5887 CHECKSUM_ULEB128 (at->dw_attr);
5888
5889 switch (AT_class (at))
5890 {
5891 case dw_val_class_const:
5892 CHECKSUM_ULEB128 (DW_FORM_sdata);
5893 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5894 break;
5895
5896 case dw_val_class_unsigned_const:
5897 CHECKSUM_ULEB128 (DW_FORM_sdata);
5898 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5899 break;
5900
5901 case dw_val_class_const_double:
5902 CHECKSUM_ULEB128 (DW_FORM_block);
5903 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5904 CHECKSUM (at->dw_attr_val.v.val_double);
5905 break;
5906
5907 case dw_val_class_wide_int:
5908 CHECKSUM_ULEB128 (DW_FORM_block);
5909 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5910 CHECKSUM (*at->dw_attr_val.v.val_wide);
5911 break;
5912
5913 case dw_val_class_vec:
5914 CHECKSUM_ULEB128 (DW_FORM_block);
5915 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5916 * at->dw_attr_val.v.val_vec.elt_size);
5917 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5918 (at->dw_attr_val.v.val_vec.length
5919 * at->dw_attr_val.v.val_vec.elt_size));
5920 break;
5921
5922 case dw_val_class_flag:
5923 CHECKSUM_ULEB128 (DW_FORM_flag);
5924 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5925 break;
5926
5927 case dw_val_class_str:
5928 CHECKSUM_ULEB128 (DW_FORM_string);
5929 CHECKSUM_STRING (AT_string (at));
5930 break;
5931
5932 case dw_val_class_addr:
5933 r = AT_addr (at);
5934 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5935 CHECKSUM_ULEB128 (DW_FORM_string);
5936 CHECKSUM_STRING (XSTR (r, 0));
5937 break;
5938
5939 case dw_val_class_offset:
5940 CHECKSUM_ULEB128 (DW_FORM_sdata);
5941 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5942 break;
5943
5944 case dw_val_class_loc:
5945 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5946 loc_checksum_ordered (loc, ctx);
5947 break;
5948
5949 case dw_val_class_fde_ref:
5950 case dw_val_class_lbl_id:
5951 case dw_val_class_lineptr:
5952 case dw_val_class_macptr:
5953 case dw_val_class_high_pc:
5954 break;
5955
5956 case dw_val_class_file:
5957 CHECKSUM_ULEB128 (DW_FORM_string);
5958 CHECKSUM_STRING (AT_file (at)->filename);
5959 break;
5960
5961 case dw_val_class_data8:
5962 CHECKSUM (at->dw_attr_val.v.val_data8);
5963 break;
5964
5965 default:
5966 break;
5967 }
5968 }
5969
5970 struct checksum_attributes
5971 {
5972 dw_attr_ref at_name;
5973 dw_attr_ref at_type;
5974 dw_attr_ref at_friend;
5975 dw_attr_ref at_accessibility;
5976 dw_attr_ref at_address_class;
5977 dw_attr_ref at_allocated;
5978 dw_attr_ref at_artificial;
5979 dw_attr_ref at_associated;
5980 dw_attr_ref at_binary_scale;
5981 dw_attr_ref at_bit_offset;
5982 dw_attr_ref at_bit_size;
5983 dw_attr_ref at_bit_stride;
5984 dw_attr_ref at_byte_size;
5985 dw_attr_ref at_byte_stride;
5986 dw_attr_ref at_const_value;
5987 dw_attr_ref at_containing_type;
5988 dw_attr_ref at_count;
5989 dw_attr_ref at_data_location;
5990 dw_attr_ref at_data_member_location;
5991 dw_attr_ref at_decimal_scale;
5992 dw_attr_ref at_decimal_sign;
5993 dw_attr_ref at_default_value;
5994 dw_attr_ref at_digit_count;
5995 dw_attr_ref at_discr;
5996 dw_attr_ref at_discr_list;
5997 dw_attr_ref at_discr_value;
5998 dw_attr_ref at_encoding;
5999 dw_attr_ref at_endianity;
6000 dw_attr_ref at_explicit;
6001 dw_attr_ref at_is_optional;
6002 dw_attr_ref at_location;
6003 dw_attr_ref at_lower_bound;
6004 dw_attr_ref at_mutable;
6005 dw_attr_ref at_ordering;
6006 dw_attr_ref at_picture_string;
6007 dw_attr_ref at_prototyped;
6008 dw_attr_ref at_small;
6009 dw_attr_ref at_segment;
6010 dw_attr_ref at_string_length;
6011 dw_attr_ref at_threads_scaled;
6012 dw_attr_ref at_upper_bound;
6013 dw_attr_ref at_use_location;
6014 dw_attr_ref at_use_UTF8;
6015 dw_attr_ref at_variable_parameter;
6016 dw_attr_ref at_virtuality;
6017 dw_attr_ref at_visibility;
6018 dw_attr_ref at_vtable_elem_location;
6019 };
6020
6021 /* Collect the attributes that we will want to use for the checksum. */
6022
6023 static void
6024 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6025 {
6026 dw_attr_ref a;
6027 unsigned ix;
6028
6029 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6030 {
6031 switch (a->dw_attr)
6032 {
6033 case DW_AT_name:
6034 attrs->at_name = a;
6035 break;
6036 case DW_AT_type:
6037 attrs->at_type = a;
6038 break;
6039 case DW_AT_friend:
6040 attrs->at_friend = a;
6041 break;
6042 case DW_AT_accessibility:
6043 attrs->at_accessibility = a;
6044 break;
6045 case DW_AT_address_class:
6046 attrs->at_address_class = a;
6047 break;
6048 case DW_AT_allocated:
6049 attrs->at_allocated = a;
6050 break;
6051 case DW_AT_artificial:
6052 attrs->at_artificial = a;
6053 break;
6054 case DW_AT_associated:
6055 attrs->at_associated = a;
6056 break;
6057 case DW_AT_binary_scale:
6058 attrs->at_binary_scale = a;
6059 break;
6060 case DW_AT_bit_offset:
6061 attrs->at_bit_offset = a;
6062 break;
6063 case DW_AT_bit_size:
6064 attrs->at_bit_size = a;
6065 break;
6066 case DW_AT_bit_stride:
6067 attrs->at_bit_stride = a;
6068 break;
6069 case DW_AT_byte_size:
6070 attrs->at_byte_size = a;
6071 break;
6072 case DW_AT_byte_stride:
6073 attrs->at_byte_stride = a;
6074 break;
6075 case DW_AT_const_value:
6076 attrs->at_const_value = a;
6077 break;
6078 case DW_AT_containing_type:
6079 attrs->at_containing_type = a;
6080 break;
6081 case DW_AT_count:
6082 attrs->at_count = a;
6083 break;
6084 case DW_AT_data_location:
6085 attrs->at_data_location = a;
6086 break;
6087 case DW_AT_data_member_location:
6088 attrs->at_data_member_location = a;
6089 break;
6090 case DW_AT_decimal_scale:
6091 attrs->at_decimal_scale = a;
6092 break;
6093 case DW_AT_decimal_sign:
6094 attrs->at_decimal_sign = a;
6095 break;
6096 case DW_AT_default_value:
6097 attrs->at_default_value = a;
6098 break;
6099 case DW_AT_digit_count:
6100 attrs->at_digit_count = a;
6101 break;
6102 case DW_AT_discr:
6103 attrs->at_discr = a;
6104 break;
6105 case DW_AT_discr_list:
6106 attrs->at_discr_list = a;
6107 break;
6108 case DW_AT_discr_value:
6109 attrs->at_discr_value = a;
6110 break;
6111 case DW_AT_encoding:
6112 attrs->at_encoding = a;
6113 break;
6114 case DW_AT_endianity:
6115 attrs->at_endianity = a;
6116 break;
6117 case DW_AT_explicit:
6118 attrs->at_explicit = a;
6119 break;
6120 case DW_AT_is_optional:
6121 attrs->at_is_optional = a;
6122 break;
6123 case DW_AT_location:
6124 attrs->at_location = a;
6125 break;
6126 case DW_AT_lower_bound:
6127 attrs->at_lower_bound = a;
6128 break;
6129 case DW_AT_mutable:
6130 attrs->at_mutable = a;
6131 break;
6132 case DW_AT_ordering:
6133 attrs->at_ordering = a;
6134 break;
6135 case DW_AT_picture_string:
6136 attrs->at_picture_string = a;
6137 break;
6138 case DW_AT_prototyped:
6139 attrs->at_prototyped = a;
6140 break;
6141 case DW_AT_small:
6142 attrs->at_small = a;
6143 break;
6144 case DW_AT_segment:
6145 attrs->at_segment = a;
6146 break;
6147 case DW_AT_string_length:
6148 attrs->at_string_length = a;
6149 break;
6150 case DW_AT_threads_scaled:
6151 attrs->at_threads_scaled = a;
6152 break;
6153 case DW_AT_upper_bound:
6154 attrs->at_upper_bound = a;
6155 break;
6156 case DW_AT_use_location:
6157 attrs->at_use_location = a;
6158 break;
6159 case DW_AT_use_UTF8:
6160 attrs->at_use_UTF8 = a;
6161 break;
6162 case DW_AT_variable_parameter:
6163 attrs->at_variable_parameter = a;
6164 break;
6165 case DW_AT_virtuality:
6166 attrs->at_virtuality = a;
6167 break;
6168 case DW_AT_visibility:
6169 attrs->at_visibility = a;
6170 break;
6171 case DW_AT_vtable_elem_location:
6172 attrs->at_vtable_elem_location = a;
6173 break;
6174 default:
6175 break;
6176 }
6177 }
6178 }
6179
6180 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6181
6182 static void
6183 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6184 {
6185 dw_die_ref c;
6186 dw_die_ref decl;
6187 struct checksum_attributes attrs;
6188
6189 CHECKSUM_ULEB128 ('D');
6190 CHECKSUM_ULEB128 (die->die_tag);
6191
6192 memset (&attrs, 0, sizeof (attrs));
6193
6194 decl = get_AT_ref (die, DW_AT_specification);
6195 if (decl != NULL)
6196 collect_checksum_attributes (&attrs, decl);
6197 collect_checksum_attributes (&attrs, die);
6198
6199 CHECKSUM_ATTR (attrs.at_name);
6200 CHECKSUM_ATTR (attrs.at_accessibility);
6201 CHECKSUM_ATTR (attrs.at_address_class);
6202 CHECKSUM_ATTR (attrs.at_allocated);
6203 CHECKSUM_ATTR (attrs.at_artificial);
6204 CHECKSUM_ATTR (attrs.at_associated);
6205 CHECKSUM_ATTR (attrs.at_binary_scale);
6206 CHECKSUM_ATTR (attrs.at_bit_offset);
6207 CHECKSUM_ATTR (attrs.at_bit_size);
6208 CHECKSUM_ATTR (attrs.at_bit_stride);
6209 CHECKSUM_ATTR (attrs.at_byte_size);
6210 CHECKSUM_ATTR (attrs.at_byte_stride);
6211 CHECKSUM_ATTR (attrs.at_const_value);
6212 CHECKSUM_ATTR (attrs.at_containing_type);
6213 CHECKSUM_ATTR (attrs.at_count);
6214 CHECKSUM_ATTR (attrs.at_data_location);
6215 CHECKSUM_ATTR (attrs.at_data_member_location);
6216 CHECKSUM_ATTR (attrs.at_decimal_scale);
6217 CHECKSUM_ATTR (attrs.at_decimal_sign);
6218 CHECKSUM_ATTR (attrs.at_default_value);
6219 CHECKSUM_ATTR (attrs.at_digit_count);
6220 CHECKSUM_ATTR (attrs.at_discr);
6221 CHECKSUM_ATTR (attrs.at_discr_list);
6222 CHECKSUM_ATTR (attrs.at_discr_value);
6223 CHECKSUM_ATTR (attrs.at_encoding);
6224 CHECKSUM_ATTR (attrs.at_endianity);
6225 CHECKSUM_ATTR (attrs.at_explicit);
6226 CHECKSUM_ATTR (attrs.at_is_optional);
6227 CHECKSUM_ATTR (attrs.at_location);
6228 CHECKSUM_ATTR (attrs.at_lower_bound);
6229 CHECKSUM_ATTR (attrs.at_mutable);
6230 CHECKSUM_ATTR (attrs.at_ordering);
6231 CHECKSUM_ATTR (attrs.at_picture_string);
6232 CHECKSUM_ATTR (attrs.at_prototyped);
6233 CHECKSUM_ATTR (attrs.at_small);
6234 CHECKSUM_ATTR (attrs.at_segment);
6235 CHECKSUM_ATTR (attrs.at_string_length);
6236 CHECKSUM_ATTR (attrs.at_threads_scaled);
6237 CHECKSUM_ATTR (attrs.at_upper_bound);
6238 CHECKSUM_ATTR (attrs.at_use_location);
6239 CHECKSUM_ATTR (attrs.at_use_UTF8);
6240 CHECKSUM_ATTR (attrs.at_variable_parameter);
6241 CHECKSUM_ATTR (attrs.at_virtuality);
6242 CHECKSUM_ATTR (attrs.at_visibility);
6243 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6244 CHECKSUM_ATTR (attrs.at_type);
6245 CHECKSUM_ATTR (attrs.at_friend);
6246
6247 /* Checksum the child DIEs. */
6248 c = die->die_child;
6249 if (c) do {
6250 dw_attr_ref name_attr;
6251
6252 c = c->die_sib;
6253 name_attr = get_AT (c, DW_AT_name);
6254 if (is_template_instantiation (c))
6255 {
6256 /* Ignore instantiations of member type and function templates. */
6257 }
6258 else if (name_attr != NULL
6259 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6260 {
6261 /* Use a shallow checksum for named nested types and member
6262 functions. */
6263 CHECKSUM_ULEB128 ('S');
6264 CHECKSUM_ULEB128 (c->die_tag);
6265 CHECKSUM_STRING (AT_string (name_attr));
6266 }
6267 else
6268 {
6269 /* Use a deep checksum for other children. */
6270 /* Mark this DIE so it gets processed when unmarking. */
6271 if (c->die_mark == 0)
6272 c->die_mark = -1;
6273 die_checksum_ordered (c, ctx, mark);
6274 }
6275 } while (c != die->die_child);
6276
6277 CHECKSUM_ULEB128 (0);
6278 }
6279
6280 /* Add a type name and tag to a hash. */
6281 static void
6282 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6283 {
6284 CHECKSUM_ULEB128 (tag);
6285 CHECKSUM_STRING (name);
6286 }
6287
6288 #undef CHECKSUM
6289 #undef CHECKSUM_STRING
6290 #undef CHECKSUM_ATTR
6291 #undef CHECKSUM_LEB128
6292 #undef CHECKSUM_ULEB128
6293
6294 /* Generate the type signature for DIE. This is computed by generating an
6295 MD5 checksum over the DIE's tag, its relevant attributes, and its
6296 children. Attributes that are references to other DIEs are processed
6297 by recursion, using the MARK field to prevent infinite recursion.
6298 If the DIE is nested inside a namespace or another type, we also
6299 need to include that context in the signature. The lower 64 bits
6300 of the resulting MD5 checksum comprise the signature. */
6301
6302 static void
6303 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6304 {
6305 int mark;
6306 const char *name;
6307 unsigned char checksum[16];
6308 struct md5_ctx ctx;
6309 dw_die_ref decl;
6310 dw_die_ref parent;
6311
6312 name = get_AT_string (die, DW_AT_name);
6313 decl = get_AT_ref (die, DW_AT_specification);
6314 parent = get_die_parent (die);
6315
6316 /* First, compute a signature for just the type name (and its surrounding
6317 context, if any. This is stored in the type unit DIE for link-time
6318 ODR (one-definition rule) checking. */
6319
6320 if (is_cxx () && name != NULL)
6321 {
6322 md5_init_ctx (&ctx);
6323
6324 /* Checksum the names of surrounding namespaces and structures. */
6325 if (parent != NULL)
6326 checksum_die_context (parent, &ctx);
6327
6328 /* Checksum the current DIE. */
6329 die_odr_checksum (die->die_tag, name, &ctx);
6330 md5_finish_ctx (&ctx, checksum);
6331
6332 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6333 }
6334
6335 /* Next, compute the complete type signature. */
6336
6337 md5_init_ctx (&ctx);
6338 mark = 1;
6339 die->die_mark = mark;
6340
6341 /* Checksum the names of surrounding namespaces and structures. */
6342 if (parent != NULL)
6343 checksum_die_context (parent, &ctx);
6344
6345 /* Checksum the DIE and its children. */
6346 die_checksum_ordered (die, &ctx, &mark);
6347 unmark_all_dies (die);
6348 md5_finish_ctx (&ctx, checksum);
6349
6350 /* Store the signature in the type node and link the type DIE and the
6351 type node together. */
6352 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6353 DWARF_TYPE_SIGNATURE_SIZE);
6354 die->comdat_type_p = true;
6355 die->die_id.die_type_node = type_node;
6356 type_node->type_die = die;
6357
6358 /* If the DIE is a specification, link its declaration to the type node
6359 as well. */
6360 if (decl != NULL)
6361 {
6362 decl->comdat_type_p = true;
6363 decl->die_id.die_type_node = type_node;
6364 }
6365 }
6366
6367 /* Do the location expressions look same? */
6368 static inline int
6369 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6370 {
6371 return loc1->dw_loc_opc == loc2->dw_loc_opc
6372 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6373 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6374 }
6375
6376 /* Do the values look the same? */
6377 static int
6378 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6379 {
6380 dw_loc_descr_ref loc1, loc2;
6381 rtx r1, r2;
6382
6383 if (v1->val_class != v2->val_class)
6384 return 0;
6385
6386 switch (v1->val_class)
6387 {
6388 case dw_val_class_const:
6389 return v1->v.val_int == v2->v.val_int;
6390 case dw_val_class_unsigned_const:
6391 return v1->v.val_unsigned == v2->v.val_unsigned;
6392 case dw_val_class_const_double:
6393 return v1->v.val_double.high == v2->v.val_double.high
6394 && v1->v.val_double.low == v2->v.val_double.low;
6395 case dw_val_class_wide_int:
6396 return *v1->v.val_wide == *v2->v.val_wide;
6397 case dw_val_class_vec:
6398 if (v1->v.val_vec.length != v2->v.val_vec.length
6399 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6400 return 0;
6401 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6402 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6403 return 0;
6404 return 1;
6405 case dw_val_class_flag:
6406 return v1->v.val_flag == v2->v.val_flag;
6407 case dw_val_class_str:
6408 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6409
6410 case dw_val_class_addr:
6411 r1 = v1->v.val_addr;
6412 r2 = v2->v.val_addr;
6413 if (GET_CODE (r1) != GET_CODE (r2))
6414 return 0;
6415 return !rtx_equal_p (r1, r2);
6416
6417 case dw_val_class_offset:
6418 return v1->v.val_offset == v2->v.val_offset;
6419
6420 case dw_val_class_loc:
6421 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6422 loc1 && loc2;
6423 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6424 if (!same_loc_p (loc1, loc2, mark))
6425 return 0;
6426 return !loc1 && !loc2;
6427
6428 case dw_val_class_die_ref:
6429 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6430
6431 case dw_val_class_fde_ref:
6432 case dw_val_class_vms_delta:
6433 case dw_val_class_lbl_id:
6434 case dw_val_class_lineptr:
6435 case dw_val_class_macptr:
6436 case dw_val_class_high_pc:
6437 return 1;
6438
6439 case dw_val_class_file:
6440 return v1->v.val_file == v2->v.val_file;
6441
6442 case dw_val_class_data8:
6443 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6444
6445 default:
6446 return 1;
6447 }
6448 }
6449
6450 /* Do the attributes look the same? */
6451
6452 static int
6453 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6454 {
6455 if (at1->dw_attr != at2->dw_attr)
6456 return 0;
6457
6458 /* We don't care that this was compiled with a different compiler
6459 snapshot; if the output is the same, that's what matters. */
6460 if (at1->dw_attr == DW_AT_producer)
6461 return 1;
6462
6463 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6464 }
6465
6466 /* Do the dies look the same? */
6467
6468 static int
6469 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6470 {
6471 dw_die_ref c1, c2;
6472 dw_attr_ref a1;
6473 unsigned ix;
6474
6475 /* To avoid infinite recursion. */
6476 if (die1->die_mark)
6477 return die1->die_mark == die2->die_mark;
6478 die1->die_mark = die2->die_mark = ++(*mark);
6479
6480 if (die1->die_tag != die2->die_tag)
6481 return 0;
6482
6483 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6484 return 0;
6485
6486 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6487 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6488 return 0;
6489
6490 c1 = die1->die_child;
6491 c2 = die2->die_child;
6492 if (! c1)
6493 {
6494 if (c2)
6495 return 0;
6496 }
6497 else
6498 for (;;)
6499 {
6500 if (!same_die_p (c1, c2, mark))
6501 return 0;
6502 c1 = c1->die_sib;
6503 c2 = c2->die_sib;
6504 if (c1 == die1->die_child)
6505 {
6506 if (c2 == die2->die_child)
6507 break;
6508 else
6509 return 0;
6510 }
6511 }
6512
6513 return 1;
6514 }
6515
6516 /* Do the dies look the same? Wrapper around same_die_p. */
6517
6518 static int
6519 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6520 {
6521 int mark = 0;
6522 int ret = same_die_p (die1, die2, &mark);
6523
6524 unmark_all_dies (die1);
6525 unmark_all_dies (die2);
6526
6527 return ret;
6528 }
6529
6530 /* The prefix to attach to symbols on DIEs in the current comdat debug
6531 info section. */
6532 static const char *comdat_symbol_id;
6533
6534 /* The index of the current symbol within the current comdat CU. */
6535 static unsigned int comdat_symbol_number;
6536
6537 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6538 children, and set comdat_symbol_id accordingly. */
6539
6540 static void
6541 compute_section_prefix (dw_die_ref unit_die)
6542 {
6543 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6544 const char *base = die_name ? lbasename (die_name) : "anonymous";
6545 char *name = XALLOCAVEC (char, strlen (base) + 64);
6546 char *p;
6547 int i, mark;
6548 unsigned char checksum[16];
6549 struct md5_ctx ctx;
6550
6551 /* Compute the checksum of the DIE, then append part of it as hex digits to
6552 the name filename of the unit. */
6553
6554 md5_init_ctx (&ctx);
6555 mark = 0;
6556 die_checksum (unit_die, &ctx, &mark);
6557 unmark_all_dies (unit_die);
6558 md5_finish_ctx (&ctx, checksum);
6559
6560 sprintf (name, "%s.", base);
6561 clean_symbol_name (name);
6562
6563 p = name + strlen (name);
6564 for (i = 0; i < 4; i++)
6565 {
6566 sprintf (p, "%.2x", checksum[i]);
6567 p += 2;
6568 }
6569
6570 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6571 comdat_symbol_number = 0;
6572 }
6573
6574 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6575
6576 static int
6577 is_type_die (dw_die_ref die)
6578 {
6579 switch (die->die_tag)
6580 {
6581 case DW_TAG_array_type:
6582 case DW_TAG_class_type:
6583 case DW_TAG_interface_type:
6584 case DW_TAG_enumeration_type:
6585 case DW_TAG_pointer_type:
6586 case DW_TAG_reference_type:
6587 case DW_TAG_rvalue_reference_type:
6588 case DW_TAG_string_type:
6589 case DW_TAG_structure_type:
6590 case DW_TAG_subroutine_type:
6591 case DW_TAG_union_type:
6592 case DW_TAG_ptr_to_member_type:
6593 case DW_TAG_set_type:
6594 case DW_TAG_subrange_type:
6595 case DW_TAG_base_type:
6596 case DW_TAG_const_type:
6597 case DW_TAG_file_type:
6598 case DW_TAG_packed_type:
6599 case DW_TAG_volatile_type:
6600 case DW_TAG_typedef:
6601 return 1;
6602 default:
6603 return 0;
6604 }
6605 }
6606
6607 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6608 Basically, we want to choose the bits that are likely to be shared between
6609 compilations (types) and leave out the bits that are specific to individual
6610 compilations (functions). */
6611
6612 static int
6613 is_comdat_die (dw_die_ref c)
6614 {
6615 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6616 we do for stabs. The advantage is a greater likelihood of sharing between
6617 objects that don't include headers in the same order (and therefore would
6618 put the base types in a different comdat). jason 8/28/00 */
6619
6620 if (c->die_tag == DW_TAG_base_type)
6621 return 0;
6622
6623 if (c->die_tag == DW_TAG_pointer_type
6624 || c->die_tag == DW_TAG_reference_type
6625 || c->die_tag == DW_TAG_rvalue_reference_type
6626 || c->die_tag == DW_TAG_const_type
6627 || c->die_tag == DW_TAG_volatile_type)
6628 {
6629 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6630
6631 return t ? is_comdat_die (t) : 0;
6632 }
6633
6634 return is_type_die (c);
6635 }
6636
6637 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6638 compilation unit. */
6639
6640 static int
6641 is_symbol_die (dw_die_ref c)
6642 {
6643 return (is_type_die (c)
6644 || is_declaration_die (c)
6645 || c->die_tag == DW_TAG_namespace
6646 || c->die_tag == DW_TAG_module);
6647 }
6648
6649 /* Returns true iff C is a compile-unit DIE. */
6650
6651 static inline bool
6652 is_cu_die (dw_die_ref c)
6653 {
6654 return c && c->die_tag == DW_TAG_compile_unit;
6655 }
6656
6657 /* Returns true iff C is a unit DIE of some sort. */
6658
6659 static inline bool
6660 is_unit_die (dw_die_ref c)
6661 {
6662 return c && (c->die_tag == DW_TAG_compile_unit
6663 || c->die_tag == DW_TAG_partial_unit
6664 || c->die_tag == DW_TAG_type_unit);
6665 }
6666
6667 /* Returns true iff C is a namespace DIE. */
6668
6669 static inline bool
6670 is_namespace_die (dw_die_ref c)
6671 {
6672 return c && c->die_tag == DW_TAG_namespace;
6673 }
6674
6675 /* Returns true iff C is a class or structure DIE. */
6676
6677 static inline bool
6678 is_class_die (dw_die_ref c)
6679 {
6680 return c && (c->die_tag == DW_TAG_class_type
6681 || c->die_tag == DW_TAG_structure_type);
6682 }
6683
6684 /* Return non-zero if this DIE is a template parameter. */
6685
6686 static inline bool
6687 is_template_parameter (dw_die_ref die)
6688 {
6689 switch (die->die_tag)
6690 {
6691 case DW_TAG_template_type_param:
6692 case DW_TAG_template_value_param:
6693 case DW_TAG_GNU_template_template_param:
6694 case DW_TAG_GNU_template_parameter_pack:
6695 return true;
6696 default:
6697 return false;
6698 }
6699 }
6700
6701 /* Return non-zero if this DIE represents a template instantiation. */
6702
6703 static inline bool
6704 is_template_instantiation (dw_die_ref die)
6705 {
6706 dw_die_ref c;
6707
6708 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6709 return false;
6710 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6711 return false;
6712 }
6713
6714 static char *
6715 gen_internal_sym (const char *prefix)
6716 {
6717 char buf[256];
6718
6719 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6720 return xstrdup (buf);
6721 }
6722
6723 /* Assign symbols to all worthy DIEs under DIE. */
6724
6725 static void
6726 assign_symbol_names (dw_die_ref die)
6727 {
6728 dw_die_ref c;
6729
6730 if (is_symbol_die (die) && !die->comdat_type_p)
6731 {
6732 if (comdat_symbol_id)
6733 {
6734 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6735
6736 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6737 comdat_symbol_id, comdat_symbol_number++);
6738 die->die_id.die_symbol = xstrdup (p);
6739 }
6740 else
6741 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6742 }
6743
6744 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6745 }
6746
6747 struct cu_hash_table_entry
6748 {
6749 dw_die_ref cu;
6750 unsigned min_comdat_num, max_comdat_num;
6751 struct cu_hash_table_entry *next;
6752 };
6753
6754 /* Helpers to manipulate hash table of CUs. */
6755
6756 struct cu_hash_table_entry_hasher
6757 {
6758 typedef cu_hash_table_entry value_type;
6759 typedef die_struct compare_type;
6760 static inline hashval_t hash (const value_type *);
6761 static inline bool equal (const value_type *, const compare_type *);
6762 static inline void remove (value_type *);
6763 };
6764
6765 inline hashval_t
6766 cu_hash_table_entry_hasher::hash (const value_type *entry)
6767 {
6768 return htab_hash_string (entry->cu->die_id.die_symbol);
6769 }
6770
6771 inline bool
6772 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6773 const compare_type *entry2)
6774 {
6775 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6776 }
6777
6778 inline void
6779 cu_hash_table_entry_hasher::remove (value_type *entry)
6780 {
6781 struct cu_hash_table_entry *next;
6782
6783 while (entry)
6784 {
6785 next = entry->next;
6786 free (entry);
6787 entry = next;
6788 }
6789 }
6790
6791 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6792
6793 /* Check whether we have already seen this CU and set up SYM_NUM
6794 accordingly. */
6795 static int
6796 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6797 {
6798 struct cu_hash_table_entry dummy;
6799 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6800
6801 dummy.max_comdat_num = 0;
6802
6803 slot = htable->find_slot_with_hash (cu,
6804 htab_hash_string (cu->die_id.die_symbol),
6805 INSERT);
6806 entry = *slot;
6807
6808 for (; entry; last = entry, entry = entry->next)
6809 {
6810 if (same_die_p_wrap (cu, entry->cu))
6811 break;
6812 }
6813
6814 if (entry)
6815 {
6816 *sym_num = entry->min_comdat_num;
6817 return 1;
6818 }
6819
6820 entry = XCNEW (struct cu_hash_table_entry);
6821 entry->cu = cu;
6822 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6823 entry->next = *slot;
6824 *slot = entry;
6825
6826 return 0;
6827 }
6828
6829 /* Record SYM_NUM to record of CU in HTABLE. */
6830 static void
6831 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6832 unsigned int sym_num)
6833 {
6834 struct cu_hash_table_entry **slot, *entry;
6835
6836 slot = htable->find_slot_with_hash (cu,
6837 htab_hash_string (cu->die_id.die_symbol),
6838 NO_INSERT);
6839 entry = *slot;
6840
6841 entry->max_comdat_num = sym_num;
6842 }
6843
6844 /* Traverse the DIE (which is always comp_unit_die), and set up
6845 additional compilation units for each of the include files we see
6846 bracketed by BINCL/EINCL. */
6847
6848 static void
6849 break_out_includes (dw_die_ref die)
6850 {
6851 dw_die_ref c;
6852 dw_die_ref unit = NULL;
6853 limbo_die_node *node, **pnode;
6854
6855 c = die->die_child;
6856 if (c) do {
6857 dw_die_ref prev = c;
6858 c = c->die_sib;
6859 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6860 || (unit && is_comdat_die (c)))
6861 {
6862 dw_die_ref next = c->die_sib;
6863
6864 /* This DIE is for a secondary CU; remove it from the main one. */
6865 remove_child_with_prev (c, prev);
6866
6867 if (c->die_tag == DW_TAG_GNU_BINCL)
6868 unit = push_new_compile_unit (unit, c);
6869 else if (c->die_tag == DW_TAG_GNU_EINCL)
6870 unit = pop_compile_unit (unit);
6871 else
6872 add_child_die (unit, c);
6873 c = next;
6874 if (c == die->die_child)
6875 break;
6876 }
6877 } while (c != die->die_child);
6878
6879 #if 0
6880 /* We can only use this in debugging, since the frontend doesn't check
6881 to make sure that we leave every include file we enter. */
6882 gcc_assert (!unit);
6883 #endif
6884
6885 assign_symbol_names (die);
6886 cu_hash_type cu_hash_table (10);
6887 for (node = limbo_die_list, pnode = &limbo_die_list;
6888 node;
6889 node = node->next)
6890 {
6891 int is_dupl;
6892
6893 compute_section_prefix (node->die);
6894 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6895 &comdat_symbol_number);
6896 assign_symbol_names (node->die);
6897 if (is_dupl)
6898 *pnode = node->next;
6899 else
6900 {
6901 pnode = &node->next;
6902 record_comdat_symbol_number (node->die, &cu_hash_table,
6903 comdat_symbol_number);
6904 }
6905 }
6906 }
6907
6908 /* Return non-zero if this DIE is a declaration. */
6909
6910 static int
6911 is_declaration_die (dw_die_ref die)
6912 {
6913 dw_attr_ref a;
6914 unsigned ix;
6915
6916 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6917 if (a->dw_attr == DW_AT_declaration)
6918 return 1;
6919
6920 return 0;
6921 }
6922
6923 /* Return non-zero if this DIE is nested inside a subprogram. */
6924
6925 static int
6926 is_nested_in_subprogram (dw_die_ref die)
6927 {
6928 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6929
6930 if (decl == NULL)
6931 decl = die;
6932 return local_scope_p (decl);
6933 }
6934
6935 /* Return non-zero if this DIE contains a defining declaration of a
6936 subprogram. */
6937
6938 static int
6939 contains_subprogram_definition (dw_die_ref die)
6940 {
6941 dw_die_ref c;
6942
6943 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6944 return 1;
6945 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6946 return 0;
6947 }
6948
6949 /* Return non-zero if this is a type DIE that should be moved to a
6950 COMDAT .debug_types section. */
6951
6952 static int
6953 should_move_die_to_comdat (dw_die_ref die)
6954 {
6955 switch (die->die_tag)
6956 {
6957 case DW_TAG_class_type:
6958 case DW_TAG_structure_type:
6959 case DW_TAG_enumeration_type:
6960 case DW_TAG_union_type:
6961 /* Don't move declarations, inlined instances, types nested in a
6962 subprogram, or types that contain subprogram definitions. */
6963 if (is_declaration_die (die)
6964 || get_AT (die, DW_AT_abstract_origin)
6965 || is_nested_in_subprogram (die)
6966 || contains_subprogram_definition (die))
6967 return 0;
6968 return 1;
6969 case DW_TAG_array_type:
6970 case DW_TAG_interface_type:
6971 case DW_TAG_pointer_type:
6972 case DW_TAG_reference_type:
6973 case DW_TAG_rvalue_reference_type:
6974 case DW_TAG_string_type:
6975 case DW_TAG_subroutine_type:
6976 case DW_TAG_ptr_to_member_type:
6977 case DW_TAG_set_type:
6978 case DW_TAG_subrange_type:
6979 case DW_TAG_base_type:
6980 case DW_TAG_const_type:
6981 case DW_TAG_file_type:
6982 case DW_TAG_packed_type:
6983 case DW_TAG_volatile_type:
6984 case DW_TAG_typedef:
6985 default:
6986 return 0;
6987 }
6988 }
6989
6990 /* Make a clone of DIE. */
6991
6992 static dw_die_ref
6993 clone_die (dw_die_ref die)
6994 {
6995 dw_die_ref clone;
6996 dw_attr_ref a;
6997 unsigned ix;
6998
6999 clone = ggc_cleared_alloc<die_node> ();
7000 clone->die_tag = die->die_tag;
7001
7002 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7003 add_dwarf_attr (clone, a);
7004
7005 return clone;
7006 }
7007
7008 /* Make a clone of the tree rooted at DIE. */
7009
7010 static dw_die_ref
7011 clone_tree (dw_die_ref die)
7012 {
7013 dw_die_ref c;
7014 dw_die_ref clone = clone_die (die);
7015
7016 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7017
7018 return clone;
7019 }
7020
7021 /* Make a clone of DIE as a declaration. */
7022
7023 static dw_die_ref
7024 clone_as_declaration (dw_die_ref die)
7025 {
7026 dw_die_ref clone;
7027 dw_die_ref decl;
7028 dw_attr_ref a;
7029 unsigned ix;
7030
7031 /* If the DIE is already a declaration, just clone it. */
7032 if (is_declaration_die (die))
7033 return clone_die (die);
7034
7035 /* If the DIE is a specification, just clone its declaration DIE. */
7036 decl = get_AT_ref (die, DW_AT_specification);
7037 if (decl != NULL)
7038 {
7039 clone = clone_die (decl);
7040 if (die->comdat_type_p)
7041 add_AT_die_ref (clone, DW_AT_signature, die);
7042 return clone;
7043 }
7044
7045 clone = ggc_cleared_alloc<die_node> ();
7046 clone->die_tag = die->die_tag;
7047
7048 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7049 {
7050 /* We don't want to copy over all attributes.
7051 For example we don't want DW_AT_byte_size because otherwise we will no
7052 longer have a declaration and GDB will treat it as a definition. */
7053
7054 switch (a->dw_attr)
7055 {
7056 case DW_AT_abstract_origin:
7057 case DW_AT_artificial:
7058 case DW_AT_containing_type:
7059 case DW_AT_external:
7060 case DW_AT_name:
7061 case DW_AT_type:
7062 case DW_AT_virtuality:
7063 case DW_AT_linkage_name:
7064 case DW_AT_MIPS_linkage_name:
7065 add_dwarf_attr (clone, a);
7066 break;
7067 case DW_AT_byte_size:
7068 default:
7069 break;
7070 }
7071 }
7072
7073 if (die->comdat_type_p)
7074 add_AT_die_ref (clone, DW_AT_signature, die);
7075
7076 add_AT_flag (clone, DW_AT_declaration, 1);
7077 return clone;
7078 }
7079
7080
7081 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7082
7083 struct decl_table_entry
7084 {
7085 dw_die_ref orig;
7086 dw_die_ref copy;
7087 };
7088
7089 /* Helpers to manipulate hash table of copied declarations. */
7090
7091 /* Hashtable helpers. */
7092
7093 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7094 {
7095 typedef decl_table_entry value_type;
7096 typedef die_struct compare_type;
7097 static inline hashval_t hash (const value_type *);
7098 static inline bool equal (const value_type *, const compare_type *);
7099 };
7100
7101 inline hashval_t
7102 decl_table_entry_hasher::hash (const value_type *entry)
7103 {
7104 return htab_hash_pointer (entry->orig);
7105 }
7106
7107 inline bool
7108 decl_table_entry_hasher::equal (const value_type *entry1,
7109 const compare_type *entry2)
7110 {
7111 return entry1->orig == entry2;
7112 }
7113
7114 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7115
7116 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7117 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7118 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7119 to check if the ancestor has already been copied into UNIT. */
7120
7121 static dw_die_ref
7122 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7123 decl_hash_type *decl_table)
7124 {
7125 dw_die_ref parent = die->die_parent;
7126 dw_die_ref new_parent = unit;
7127 dw_die_ref copy;
7128 decl_table_entry **slot = NULL;
7129 struct decl_table_entry *entry = NULL;
7130
7131 if (decl_table)
7132 {
7133 /* Check if the entry has already been copied to UNIT. */
7134 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7135 INSERT);
7136 if (*slot != HTAB_EMPTY_ENTRY)
7137 {
7138 entry = *slot;
7139 return entry->copy;
7140 }
7141
7142 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7143 entry = XCNEW (struct decl_table_entry);
7144 entry->orig = die;
7145 entry->copy = NULL;
7146 *slot = entry;
7147 }
7148
7149 if (parent != NULL)
7150 {
7151 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7152 if (spec != NULL)
7153 parent = spec;
7154 if (!is_unit_die (parent))
7155 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7156 }
7157
7158 copy = clone_as_declaration (die);
7159 add_child_die (new_parent, copy);
7160
7161 if (decl_table)
7162 {
7163 /* Record the pointer to the copy. */
7164 entry->copy = copy;
7165 }
7166
7167 return copy;
7168 }
7169 /* Copy the declaration context to the new type unit DIE. This includes
7170 any surrounding namespace or type declarations. If the DIE has an
7171 AT_specification attribute, it also includes attributes and children
7172 attached to the specification, and returns a pointer to the original
7173 parent of the declaration DIE. Returns NULL otherwise. */
7174
7175 static dw_die_ref
7176 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7177 {
7178 dw_die_ref decl;
7179 dw_die_ref new_decl;
7180 dw_die_ref orig_parent = NULL;
7181
7182 decl = get_AT_ref (die, DW_AT_specification);
7183 if (decl == NULL)
7184 decl = die;
7185 else
7186 {
7187 unsigned ix;
7188 dw_die_ref c;
7189 dw_attr_ref a;
7190
7191 /* The original DIE will be changed to a declaration, and must
7192 be moved to be a child of the original declaration DIE. */
7193 orig_parent = decl->die_parent;
7194
7195 /* Copy the type node pointer from the new DIE to the original
7196 declaration DIE so we can forward references later. */
7197 decl->comdat_type_p = true;
7198 decl->die_id.die_type_node = die->die_id.die_type_node;
7199
7200 remove_AT (die, DW_AT_specification);
7201
7202 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7203 {
7204 if (a->dw_attr != DW_AT_name
7205 && a->dw_attr != DW_AT_declaration
7206 && a->dw_attr != DW_AT_external)
7207 add_dwarf_attr (die, a);
7208 }
7209
7210 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7211 }
7212
7213 if (decl->die_parent != NULL
7214 && !is_unit_die (decl->die_parent))
7215 {
7216 new_decl = copy_ancestor_tree (unit, decl, NULL);
7217 if (new_decl != NULL)
7218 {
7219 remove_AT (new_decl, DW_AT_signature);
7220 add_AT_specification (die, new_decl);
7221 }
7222 }
7223
7224 return orig_parent;
7225 }
7226
7227 /* Generate the skeleton ancestor tree for the given NODE, then clone
7228 the DIE and add the clone into the tree. */
7229
7230 static void
7231 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7232 {
7233 if (node->new_die != NULL)
7234 return;
7235
7236 node->new_die = clone_as_declaration (node->old_die);
7237
7238 if (node->parent != NULL)
7239 {
7240 generate_skeleton_ancestor_tree (node->parent);
7241 add_child_die (node->parent->new_die, node->new_die);
7242 }
7243 }
7244
7245 /* Generate a skeleton tree of DIEs containing any declarations that are
7246 found in the original tree. We traverse the tree looking for declaration
7247 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7248
7249 static void
7250 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7251 {
7252 skeleton_chain_node node;
7253 dw_die_ref c;
7254 dw_die_ref first;
7255 dw_die_ref prev = NULL;
7256 dw_die_ref next = NULL;
7257
7258 node.parent = parent;
7259
7260 first = c = parent->old_die->die_child;
7261 if (c)
7262 next = c->die_sib;
7263 if (c) do {
7264 if (prev == NULL || prev->die_sib == c)
7265 prev = c;
7266 c = next;
7267 next = (c == first ? NULL : c->die_sib);
7268 node.old_die = c;
7269 node.new_die = NULL;
7270 if (is_declaration_die (c))
7271 {
7272 if (is_template_instantiation (c))
7273 {
7274 /* Instantiated templates do not need to be cloned into the
7275 type unit. Just move the DIE and its children back to
7276 the skeleton tree (in the main CU). */
7277 remove_child_with_prev (c, prev);
7278 add_child_die (parent->new_die, c);
7279 c = prev;
7280 }
7281 else
7282 {
7283 /* Clone the existing DIE, move the original to the skeleton
7284 tree (which is in the main CU), and put the clone, with
7285 all the original's children, where the original came from
7286 (which is about to be moved to the type unit). */
7287 dw_die_ref clone = clone_die (c);
7288 move_all_children (c, clone);
7289
7290 /* If the original has a DW_AT_object_pointer attribute,
7291 it would now point to a child DIE just moved to the
7292 cloned tree, so we need to remove that attribute from
7293 the original. */
7294 remove_AT (c, DW_AT_object_pointer);
7295
7296 replace_child (c, clone, prev);
7297 generate_skeleton_ancestor_tree (parent);
7298 add_child_die (parent->new_die, c);
7299 node.new_die = c;
7300 c = clone;
7301 }
7302 }
7303 generate_skeleton_bottom_up (&node);
7304 } while (next != NULL);
7305 }
7306
7307 /* Wrapper function for generate_skeleton_bottom_up. */
7308
7309 static dw_die_ref
7310 generate_skeleton (dw_die_ref die)
7311 {
7312 skeleton_chain_node node;
7313
7314 node.old_die = die;
7315 node.new_die = NULL;
7316 node.parent = NULL;
7317
7318 /* If this type definition is nested inside another type,
7319 and is not an instantiation of a template, always leave
7320 at least a declaration in its place. */
7321 if (die->die_parent != NULL
7322 && is_type_die (die->die_parent)
7323 && !is_template_instantiation (die))
7324 node.new_die = clone_as_declaration (die);
7325
7326 generate_skeleton_bottom_up (&node);
7327 return node.new_die;
7328 }
7329
7330 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7331 declaration. The original DIE is moved to a new compile unit so that
7332 existing references to it follow it to the new location. If any of the
7333 original DIE's descendants is a declaration, we need to replace the
7334 original DIE with a skeleton tree and move the declarations back into the
7335 skeleton tree. */
7336
7337 static dw_die_ref
7338 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7339 dw_die_ref prev)
7340 {
7341 dw_die_ref skeleton, orig_parent;
7342
7343 /* Copy the declaration context to the type unit DIE. If the returned
7344 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7345 that DIE. */
7346 orig_parent = copy_declaration_context (unit, child);
7347
7348 skeleton = generate_skeleton (child);
7349 if (skeleton == NULL)
7350 remove_child_with_prev (child, prev);
7351 else
7352 {
7353 skeleton->comdat_type_p = true;
7354 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7355
7356 /* If the original DIE was a specification, we need to put
7357 the skeleton under the parent DIE of the declaration.
7358 This leaves the original declaration in the tree, but
7359 it will be pruned later since there are no longer any
7360 references to it. */
7361 if (orig_parent != NULL)
7362 {
7363 remove_child_with_prev (child, prev);
7364 add_child_die (orig_parent, skeleton);
7365 }
7366 else
7367 replace_child (child, skeleton, prev);
7368 }
7369
7370 return skeleton;
7371 }
7372
7373 /* Traverse the DIE and set up additional .debug_types sections for each
7374 type worthy of being placed in a COMDAT section. */
7375
7376 static void
7377 break_out_comdat_types (dw_die_ref die)
7378 {
7379 dw_die_ref c;
7380 dw_die_ref first;
7381 dw_die_ref prev = NULL;
7382 dw_die_ref next = NULL;
7383 dw_die_ref unit = NULL;
7384
7385 first = c = die->die_child;
7386 if (c)
7387 next = c->die_sib;
7388 if (c) do {
7389 if (prev == NULL || prev->die_sib == c)
7390 prev = c;
7391 c = next;
7392 next = (c == first ? NULL : c->die_sib);
7393 if (should_move_die_to_comdat (c))
7394 {
7395 dw_die_ref replacement;
7396 comdat_type_node_ref type_node;
7397
7398 /* Break out nested types into their own type units. */
7399 break_out_comdat_types (c);
7400
7401 /* Create a new type unit DIE as the root for the new tree, and
7402 add it to the list of comdat types. */
7403 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7404 add_AT_unsigned (unit, DW_AT_language,
7405 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7406 type_node = ggc_cleared_alloc<comdat_type_node> ();
7407 type_node->root_die = unit;
7408 type_node->next = comdat_type_list;
7409 comdat_type_list = type_node;
7410
7411 /* Generate the type signature. */
7412 generate_type_signature (c, type_node);
7413
7414 /* Copy the declaration context, attributes, and children of the
7415 declaration into the new type unit DIE, then remove this DIE
7416 from the main CU (or replace it with a skeleton if necessary). */
7417 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7418 type_node->skeleton_die = replacement;
7419
7420 /* Add the DIE to the new compunit. */
7421 add_child_die (unit, c);
7422
7423 if (replacement != NULL)
7424 c = replacement;
7425 }
7426 else if (c->die_tag == DW_TAG_namespace
7427 || c->die_tag == DW_TAG_class_type
7428 || c->die_tag == DW_TAG_structure_type
7429 || c->die_tag == DW_TAG_union_type)
7430 {
7431 /* Look for nested types that can be broken out. */
7432 break_out_comdat_types (c);
7433 }
7434 } while (next != NULL);
7435 }
7436
7437 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7438 Enter all the cloned children into the hash table decl_table. */
7439
7440 static dw_die_ref
7441 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7442 {
7443 dw_die_ref c;
7444 dw_die_ref clone;
7445 struct decl_table_entry *entry;
7446 decl_table_entry **slot;
7447
7448 if (die->die_tag == DW_TAG_subprogram)
7449 clone = clone_as_declaration (die);
7450 else
7451 clone = clone_die (die);
7452
7453 slot = decl_table->find_slot_with_hash (die,
7454 htab_hash_pointer (die), INSERT);
7455
7456 /* Assert that DIE isn't in the hash table yet. If it would be there
7457 before, the ancestors would be necessarily there as well, therefore
7458 clone_tree_partial wouldn't be called. */
7459 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7460
7461 entry = XCNEW (struct decl_table_entry);
7462 entry->orig = die;
7463 entry->copy = clone;
7464 *slot = entry;
7465
7466 if (die->die_tag != DW_TAG_subprogram)
7467 FOR_EACH_CHILD (die, c,
7468 add_child_die (clone, clone_tree_partial (c, decl_table)));
7469
7470 return clone;
7471 }
7472
7473 /* Walk the DIE and its children, looking for references to incomplete
7474 or trivial types that are unmarked (i.e., that are not in the current
7475 type_unit). */
7476
7477 static void
7478 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7479 {
7480 dw_die_ref c;
7481 dw_attr_ref a;
7482 unsigned ix;
7483
7484 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7485 {
7486 if (AT_class (a) == dw_val_class_die_ref)
7487 {
7488 dw_die_ref targ = AT_ref (a);
7489 decl_table_entry **slot;
7490 struct decl_table_entry *entry;
7491
7492 if (targ->die_mark != 0 || targ->comdat_type_p)
7493 continue;
7494
7495 slot = decl_table->find_slot_with_hash (targ,
7496 htab_hash_pointer (targ),
7497 INSERT);
7498
7499 if (*slot != HTAB_EMPTY_ENTRY)
7500 {
7501 /* TARG has already been copied, so we just need to
7502 modify the reference to point to the copy. */
7503 entry = *slot;
7504 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7505 }
7506 else
7507 {
7508 dw_die_ref parent = unit;
7509 dw_die_ref copy = clone_die (targ);
7510
7511 /* Record in DECL_TABLE that TARG has been copied.
7512 Need to do this now, before the recursive call,
7513 because DECL_TABLE may be expanded and SLOT
7514 would no longer be a valid pointer. */
7515 entry = XCNEW (struct decl_table_entry);
7516 entry->orig = targ;
7517 entry->copy = copy;
7518 *slot = entry;
7519
7520 /* If TARG is not a declaration DIE, we need to copy its
7521 children. */
7522 if (!is_declaration_die (targ))
7523 {
7524 FOR_EACH_CHILD (
7525 targ, c,
7526 add_child_die (copy,
7527 clone_tree_partial (c, decl_table)));
7528 }
7529
7530 /* Make sure the cloned tree is marked as part of the
7531 type unit. */
7532 mark_dies (copy);
7533
7534 /* If TARG has surrounding context, copy its ancestor tree
7535 into the new type unit. */
7536 if (targ->die_parent != NULL
7537 && !is_unit_die (targ->die_parent))
7538 parent = copy_ancestor_tree (unit, targ->die_parent,
7539 decl_table);
7540
7541 add_child_die (parent, copy);
7542 a->dw_attr_val.v.val_die_ref.die = copy;
7543
7544 /* Make sure the newly-copied DIE is walked. If it was
7545 installed in a previously-added context, it won't
7546 get visited otherwise. */
7547 if (parent != unit)
7548 {
7549 /* Find the highest point of the newly-added tree,
7550 mark each node along the way, and walk from there. */
7551 parent->die_mark = 1;
7552 while (parent->die_parent
7553 && parent->die_parent->die_mark == 0)
7554 {
7555 parent = parent->die_parent;
7556 parent->die_mark = 1;
7557 }
7558 copy_decls_walk (unit, parent, decl_table);
7559 }
7560 }
7561 }
7562 }
7563
7564 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7565 }
7566
7567 /* Copy declarations for "unworthy" types into the new comdat section.
7568 Incomplete types, modified types, and certain other types aren't broken
7569 out into comdat sections of their own, so they don't have a signature,
7570 and we need to copy the declaration into the same section so that we
7571 don't have an external reference. */
7572
7573 static void
7574 copy_decls_for_unworthy_types (dw_die_ref unit)
7575 {
7576 mark_dies (unit);
7577 decl_hash_type decl_table (10);
7578 copy_decls_walk (unit, unit, &decl_table);
7579 unmark_dies (unit);
7580 }
7581
7582 /* Traverse the DIE and add a sibling attribute if it may have the
7583 effect of speeding up access to siblings. To save some space,
7584 avoid generating sibling attributes for DIE's without children. */
7585
7586 static void
7587 add_sibling_attributes (dw_die_ref die)
7588 {
7589 dw_die_ref c;
7590
7591 if (! die->die_child)
7592 return;
7593
7594 if (die->die_parent && die != die->die_parent->die_child)
7595 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7596
7597 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7598 }
7599
7600 /* Output all location lists for the DIE and its children. */
7601
7602 static void
7603 output_location_lists (dw_die_ref die)
7604 {
7605 dw_die_ref c;
7606 dw_attr_ref a;
7607 unsigned ix;
7608
7609 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7610 if (AT_class (a) == dw_val_class_loc_list)
7611 output_loc_list (AT_loc_list (a));
7612
7613 FOR_EACH_CHILD (die, c, output_location_lists (c));
7614 }
7615
7616 /* We want to limit the number of external references, because they are
7617 larger than local references: a relocation takes multiple words, and
7618 even a sig8 reference is always eight bytes, whereas a local reference
7619 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7620 So if we encounter multiple external references to the same type DIE, we
7621 make a local typedef stub for it and redirect all references there.
7622
7623 This is the element of the hash table for keeping track of these
7624 references. */
7625
7626 struct external_ref
7627 {
7628 dw_die_ref type;
7629 dw_die_ref stub;
7630 unsigned n_refs;
7631 };
7632
7633 /* Hashtable helpers. */
7634
7635 struct external_ref_hasher : typed_free_remove <external_ref>
7636 {
7637 typedef external_ref value_type;
7638 typedef external_ref compare_type;
7639 static inline hashval_t hash (const value_type *);
7640 static inline bool equal (const value_type *, const compare_type *);
7641 };
7642
7643 inline hashval_t
7644 external_ref_hasher::hash (const value_type *r)
7645 {
7646 dw_die_ref die = r->type;
7647 hashval_t h = 0;
7648
7649 /* We can't use the address of the DIE for hashing, because
7650 that will make the order of the stub DIEs non-deterministic. */
7651 if (! die->comdat_type_p)
7652 /* We have a symbol; use it to compute a hash. */
7653 h = htab_hash_string (die->die_id.die_symbol);
7654 else
7655 {
7656 /* We have a type signature; use a subset of the bits as the hash.
7657 The 8-byte signature is at least as large as hashval_t. */
7658 comdat_type_node_ref type_node = die->die_id.die_type_node;
7659 memcpy (&h, type_node->signature, sizeof (h));
7660 }
7661 return h;
7662 }
7663
7664 inline bool
7665 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7666 {
7667 return r1->type == r2->type;
7668 }
7669
7670 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7671
7672 /* Return a pointer to the external_ref for references to DIE. */
7673
7674 static struct external_ref *
7675 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7676 {
7677 struct external_ref ref, *ref_p;
7678 external_ref **slot;
7679
7680 ref.type = die;
7681 slot = map->find_slot (&ref, INSERT);
7682 if (*slot != HTAB_EMPTY_ENTRY)
7683 return *slot;
7684
7685 ref_p = XCNEW (struct external_ref);
7686 ref_p->type = die;
7687 *slot = ref_p;
7688 return ref_p;
7689 }
7690
7691 /* Subroutine of optimize_external_refs, below.
7692
7693 If we see a type skeleton, record it as our stub. If we see external
7694 references, remember how many we've seen. */
7695
7696 static void
7697 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7698 {
7699 dw_die_ref c;
7700 dw_attr_ref a;
7701 unsigned ix;
7702 struct external_ref *ref_p;
7703
7704 if (is_type_die (die)
7705 && (c = get_AT_ref (die, DW_AT_signature)))
7706 {
7707 /* This is a local skeleton; use it for local references. */
7708 ref_p = lookup_external_ref (map, c);
7709 ref_p->stub = die;
7710 }
7711
7712 /* Scan the DIE references, and remember any that refer to DIEs from
7713 other CUs (i.e. those which are not marked). */
7714 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7715 if (AT_class (a) == dw_val_class_die_ref
7716 && (c = AT_ref (a))->die_mark == 0
7717 && is_type_die (c))
7718 {
7719 ref_p = lookup_external_ref (map, c);
7720 ref_p->n_refs++;
7721 }
7722
7723 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7724 }
7725
7726 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7727 points to an external_ref, DATA is the CU we're processing. If we don't
7728 already have a local stub, and we have multiple refs, build a stub. */
7729
7730 int
7731 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7732 {
7733 struct external_ref *ref_p = *slot;
7734
7735 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7736 {
7737 /* We have multiple references to this type, so build a small stub.
7738 Both of these forms are a bit dodgy from the perspective of the
7739 DWARF standard, since technically they should have names. */
7740 dw_die_ref cu = data;
7741 dw_die_ref type = ref_p->type;
7742 dw_die_ref stub = NULL;
7743
7744 if (type->comdat_type_p)
7745 {
7746 /* If we refer to this type via sig8, use AT_signature. */
7747 stub = new_die (type->die_tag, cu, NULL_TREE);
7748 add_AT_die_ref (stub, DW_AT_signature, type);
7749 }
7750 else
7751 {
7752 /* Otherwise, use a typedef with no name. */
7753 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7754 add_AT_die_ref (stub, DW_AT_type, type);
7755 }
7756
7757 stub->die_mark++;
7758 ref_p->stub = stub;
7759 }
7760 return 1;
7761 }
7762
7763 /* DIE is a unit; look through all the DIE references to see if there are
7764 any external references to types, and if so, create local stubs for
7765 them which will be applied in build_abbrev_table. This is useful because
7766 references to local DIEs are smaller. */
7767
7768 static external_ref_hash_type *
7769 optimize_external_refs (dw_die_ref die)
7770 {
7771 external_ref_hash_type *map = new external_ref_hash_type (10);
7772 optimize_external_refs_1 (die, map);
7773 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7774 return map;
7775 }
7776
7777 /* The format of each DIE (and its attribute value pairs) is encoded in an
7778 abbreviation table. This routine builds the abbreviation table and assigns
7779 a unique abbreviation id for each abbreviation entry. The children of each
7780 die are visited recursively. */
7781
7782 static void
7783 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7784 {
7785 unsigned long abbrev_id;
7786 unsigned int n_alloc;
7787 dw_die_ref c;
7788 dw_attr_ref a;
7789 unsigned ix;
7790
7791 /* Scan the DIE references, and replace any that refer to
7792 DIEs from other CUs (i.e. those which are not marked) with
7793 the local stubs we built in optimize_external_refs. */
7794 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7795 if (AT_class (a) == dw_val_class_die_ref
7796 && (c = AT_ref (a))->die_mark == 0)
7797 {
7798 struct external_ref *ref_p;
7799 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7800
7801 ref_p = lookup_external_ref (extern_map, c);
7802 if (ref_p->stub && ref_p->stub != die)
7803 change_AT_die_ref (a, ref_p->stub);
7804 else
7805 /* We aren't changing this reference, so mark it external. */
7806 set_AT_ref_external (a, 1);
7807 }
7808
7809 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7810 {
7811 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7812 dw_attr_ref die_a, abbrev_a;
7813 unsigned ix;
7814 bool ok = true;
7815
7816 if (abbrev->die_tag != die->die_tag)
7817 continue;
7818 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7819 continue;
7820
7821 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7822 continue;
7823
7824 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7825 {
7826 abbrev_a = &(*abbrev->die_attr)[ix];
7827 if ((abbrev_a->dw_attr != die_a->dw_attr)
7828 || (value_format (abbrev_a) != value_format (die_a)))
7829 {
7830 ok = false;
7831 break;
7832 }
7833 }
7834 if (ok)
7835 break;
7836 }
7837
7838 if (abbrev_id >= abbrev_die_table_in_use)
7839 {
7840 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7841 {
7842 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7843 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7844 n_alloc);
7845
7846 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7847 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7848 abbrev_die_table_allocated = n_alloc;
7849 }
7850
7851 ++abbrev_die_table_in_use;
7852 abbrev_die_table[abbrev_id] = die;
7853 }
7854
7855 die->die_abbrev = abbrev_id;
7856 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7857 }
7858 \f
7859 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7860
7861 static int
7862 constant_size (unsigned HOST_WIDE_INT value)
7863 {
7864 int log;
7865
7866 if (value == 0)
7867 log = 0;
7868 else
7869 log = floor_log2 (value);
7870
7871 log = log / 8;
7872 log = 1 << (floor_log2 (log) + 1);
7873
7874 return log;
7875 }
7876
7877 /* Return the size of a DIE as it is represented in the
7878 .debug_info section. */
7879
7880 static unsigned long
7881 size_of_die (dw_die_ref die)
7882 {
7883 unsigned long size = 0;
7884 dw_attr_ref a;
7885 unsigned ix;
7886 enum dwarf_form form;
7887
7888 size += size_of_uleb128 (die->die_abbrev);
7889 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7890 {
7891 switch (AT_class (a))
7892 {
7893 case dw_val_class_addr:
7894 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7895 {
7896 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7897 size += size_of_uleb128 (AT_index (a));
7898 }
7899 else
7900 size += DWARF2_ADDR_SIZE;
7901 break;
7902 case dw_val_class_offset:
7903 size += DWARF_OFFSET_SIZE;
7904 break;
7905 case dw_val_class_loc:
7906 {
7907 unsigned long lsize = size_of_locs (AT_loc (a));
7908
7909 /* Block length. */
7910 if (dwarf_version >= 4)
7911 size += size_of_uleb128 (lsize);
7912 else
7913 size += constant_size (lsize);
7914 size += lsize;
7915 }
7916 break;
7917 case dw_val_class_loc_list:
7918 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7919 {
7920 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7921 size += size_of_uleb128 (AT_index (a));
7922 }
7923 else
7924 size += DWARF_OFFSET_SIZE;
7925 break;
7926 case dw_val_class_range_list:
7927 size += DWARF_OFFSET_SIZE;
7928 break;
7929 case dw_val_class_const:
7930 size += size_of_sleb128 (AT_int (a));
7931 break;
7932 case dw_val_class_unsigned_const:
7933 {
7934 int csize = constant_size (AT_unsigned (a));
7935 if (dwarf_version == 3
7936 && a->dw_attr == DW_AT_data_member_location
7937 && csize >= 4)
7938 size += size_of_uleb128 (AT_unsigned (a));
7939 else
7940 size += csize;
7941 }
7942 break;
7943 case dw_val_class_const_double:
7944 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7945 if (HOST_BITS_PER_WIDE_INT >= 64)
7946 size++; /* block */
7947 break;
7948 case dw_val_class_wide_int:
7949 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7950 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7951 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7952 > 64)
7953 size++; /* block */
7954 break;
7955 case dw_val_class_vec:
7956 size += constant_size (a->dw_attr_val.v.val_vec.length
7957 * a->dw_attr_val.v.val_vec.elt_size)
7958 + a->dw_attr_val.v.val_vec.length
7959 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7960 break;
7961 case dw_val_class_flag:
7962 if (dwarf_version >= 4)
7963 /* Currently all add_AT_flag calls pass in 1 as last argument,
7964 so DW_FORM_flag_present can be used. If that ever changes,
7965 we'll need to use DW_FORM_flag and have some optimization
7966 in build_abbrev_table that will change those to
7967 DW_FORM_flag_present if it is set to 1 in all DIEs using
7968 the same abbrev entry. */
7969 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7970 else
7971 size += 1;
7972 break;
7973 case dw_val_class_die_ref:
7974 if (AT_ref_external (a))
7975 {
7976 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7977 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7978 is sized by target address length, whereas in DWARF3
7979 it's always sized as an offset. */
7980 if (use_debug_types)
7981 size += DWARF_TYPE_SIGNATURE_SIZE;
7982 else if (dwarf_version == 2)
7983 size += DWARF2_ADDR_SIZE;
7984 else
7985 size += DWARF_OFFSET_SIZE;
7986 }
7987 else
7988 size += DWARF_OFFSET_SIZE;
7989 break;
7990 case dw_val_class_fde_ref:
7991 size += DWARF_OFFSET_SIZE;
7992 break;
7993 case dw_val_class_lbl_id:
7994 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7995 {
7996 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7997 size += size_of_uleb128 (AT_index (a));
7998 }
7999 else
8000 size += DWARF2_ADDR_SIZE;
8001 break;
8002 case dw_val_class_lineptr:
8003 case dw_val_class_macptr:
8004 size += DWARF_OFFSET_SIZE;
8005 break;
8006 case dw_val_class_str:
8007 form = AT_string_form (a);
8008 if (form == DW_FORM_strp)
8009 size += DWARF_OFFSET_SIZE;
8010 else if (form == DW_FORM_GNU_str_index)
8011 size += size_of_uleb128 (AT_index (a));
8012 else
8013 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8014 break;
8015 case dw_val_class_file:
8016 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8017 break;
8018 case dw_val_class_data8:
8019 size += 8;
8020 break;
8021 case dw_val_class_vms_delta:
8022 size += DWARF_OFFSET_SIZE;
8023 break;
8024 case dw_val_class_high_pc:
8025 size += DWARF2_ADDR_SIZE;
8026 break;
8027 default:
8028 gcc_unreachable ();
8029 }
8030 }
8031
8032 return size;
8033 }
8034
8035 /* Size the debugging information associated with a given DIE. Visits the
8036 DIE's children recursively. Updates the global variable next_die_offset, on
8037 each time through. Uses the current value of next_die_offset to update the
8038 die_offset field in each DIE. */
8039
8040 static void
8041 calc_die_sizes (dw_die_ref die)
8042 {
8043 dw_die_ref c;
8044
8045 gcc_assert (die->die_offset == 0
8046 || (unsigned long int) die->die_offset == next_die_offset);
8047 die->die_offset = next_die_offset;
8048 next_die_offset += size_of_die (die);
8049
8050 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8051
8052 if (die->die_child != NULL)
8053 /* Count the null byte used to terminate sibling lists. */
8054 next_die_offset += 1;
8055 }
8056
8057 /* Size just the base type children at the start of the CU.
8058 This is needed because build_abbrev needs to size locs
8059 and sizing of type based stack ops needs to know die_offset
8060 values for the base types. */
8061
8062 static void
8063 calc_base_type_die_sizes (void)
8064 {
8065 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8066 unsigned int i;
8067 dw_die_ref base_type;
8068 #if ENABLE_ASSERT_CHECKING
8069 dw_die_ref prev = comp_unit_die ()->die_child;
8070 #endif
8071
8072 die_offset += size_of_die (comp_unit_die ());
8073 for (i = 0; base_types.iterate (i, &base_type); i++)
8074 {
8075 #if ENABLE_ASSERT_CHECKING
8076 gcc_assert (base_type->die_offset == 0
8077 && prev->die_sib == base_type
8078 && base_type->die_child == NULL
8079 && base_type->die_abbrev);
8080 prev = base_type;
8081 #endif
8082 base_type->die_offset = die_offset;
8083 die_offset += size_of_die (base_type);
8084 }
8085 }
8086
8087 /* Set the marks for a die and its children. We do this so
8088 that we know whether or not a reference needs to use FORM_ref_addr; only
8089 DIEs in the same CU will be marked. We used to clear out the offset
8090 and use that as the flag, but ran into ordering problems. */
8091
8092 static void
8093 mark_dies (dw_die_ref die)
8094 {
8095 dw_die_ref c;
8096
8097 gcc_assert (!die->die_mark);
8098
8099 die->die_mark = 1;
8100 FOR_EACH_CHILD (die, c, mark_dies (c));
8101 }
8102
8103 /* Clear the marks for a die and its children. */
8104
8105 static void
8106 unmark_dies (dw_die_ref die)
8107 {
8108 dw_die_ref c;
8109
8110 if (! use_debug_types)
8111 gcc_assert (die->die_mark);
8112
8113 die->die_mark = 0;
8114 FOR_EACH_CHILD (die, c, unmark_dies (c));
8115 }
8116
8117 /* Clear the marks for a die, its children and referred dies. */
8118
8119 static void
8120 unmark_all_dies (dw_die_ref die)
8121 {
8122 dw_die_ref c;
8123 dw_attr_ref a;
8124 unsigned ix;
8125
8126 if (!die->die_mark)
8127 return;
8128 die->die_mark = 0;
8129
8130 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8131
8132 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8133 if (AT_class (a) == dw_val_class_die_ref)
8134 unmark_all_dies (AT_ref (a));
8135 }
8136
8137 /* Calculate if the entry should appear in the final output file. It may be
8138 from a pruned a type. */
8139
8140 static bool
8141 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8142 {
8143 /* By limiting gnu pubnames to definitions only, gold can generate a
8144 gdb index without entries for declarations, which don't include
8145 enough information to be useful. */
8146 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8147 return false;
8148
8149 if (table == pubname_table)
8150 {
8151 /* Enumerator names are part of the pubname table, but the
8152 parent DW_TAG_enumeration_type die may have been pruned.
8153 Don't output them if that is the case. */
8154 if (p->die->die_tag == DW_TAG_enumerator &&
8155 (p->die->die_parent == NULL
8156 || !p->die->die_parent->die_perennial_p))
8157 return false;
8158
8159 /* Everything else in the pubname table is included. */
8160 return true;
8161 }
8162
8163 /* The pubtypes table shouldn't include types that have been
8164 pruned. */
8165 return (p->die->die_offset != 0
8166 || !flag_eliminate_unused_debug_types);
8167 }
8168
8169 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8170 generated for the compilation unit. */
8171
8172 static unsigned long
8173 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8174 {
8175 unsigned long size;
8176 unsigned i;
8177 pubname_ref p;
8178 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8179
8180 size = DWARF_PUBNAMES_HEADER_SIZE;
8181 FOR_EACH_VEC_ELT (*names, i, p)
8182 if (include_pubname_in_output (names, p))
8183 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8184
8185 size += DWARF_OFFSET_SIZE;
8186 return size;
8187 }
8188
8189 /* Return the size of the information in the .debug_aranges section. */
8190
8191 static unsigned long
8192 size_of_aranges (void)
8193 {
8194 unsigned long size;
8195
8196 size = DWARF_ARANGES_HEADER_SIZE;
8197
8198 /* Count the address/length pair for this compilation unit. */
8199 if (text_section_used)
8200 size += 2 * DWARF2_ADDR_SIZE;
8201 if (cold_text_section_used)
8202 size += 2 * DWARF2_ADDR_SIZE;
8203 if (have_multiple_function_sections)
8204 {
8205 unsigned fde_idx;
8206 dw_fde_ref fde;
8207
8208 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8209 {
8210 if (DECL_IGNORED_P (fde->decl))
8211 continue;
8212 if (!fde->in_std_section)
8213 size += 2 * DWARF2_ADDR_SIZE;
8214 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8215 size += 2 * DWARF2_ADDR_SIZE;
8216 }
8217 }
8218
8219 /* Count the two zero words used to terminated the address range table. */
8220 size += 2 * DWARF2_ADDR_SIZE;
8221 return size;
8222 }
8223 \f
8224 /* Select the encoding of an attribute value. */
8225
8226 static enum dwarf_form
8227 value_format (dw_attr_ref a)
8228 {
8229 switch (AT_class (a))
8230 {
8231 case dw_val_class_addr:
8232 /* Only very few attributes allow DW_FORM_addr. */
8233 switch (a->dw_attr)
8234 {
8235 case DW_AT_low_pc:
8236 case DW_AT_high_pc:
8237 case DW_AT_entry_pc:
8238 case DW_AT_trampoline:
8239 return (AT_index (a) == NOT_INDEXED
8240 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8241 default:
8242 break;
8243 }
8244 switch (DWARF2_ADDR_SIZE)
8245 {
8246 case 1:
8247 return DW_FORM_data1;
8248 case 2:
8249 return DW_FORM_data2;
8250 case 4:
8251 return DW_FORM_data4;
8252 case 8:
8253 return DW_FORM_data8;
8254 default:
8255 gcc_unreachable ();
8256 }
8257 case dw_val_class_range_list:
8258 case dw_val_class_loc_list:
8259 if (dwarf_version >= 4)
8260 return DW_FORM_sec_offset;
8261 /* FALLTHRU */
8262 case dw_val_class_vms_delta:
8263 case dw_val_class_offset:
8264 switch (DWARF_OFFSET_SIZE)
8265 {
8266 case 4:
8267 return DW_FORM_data4;
8268 case 8:
8269 return DW_FORM_data8;
8270 default:
8271 gcc_unreachable ();
8272 }
8273 case dw_val_class_loc:
8274 if (dwarf_version >= 4)
8275 return DW_FORM_exprloc;
8276 switch (constant_size (size_of_locs (AT_loc (a))))
8277 {
8278 case 1:
8279 return DW_FORM_block1;
8280 case 2:
8281 return DW_FORM_block2;
8282 case 4:
8283 return DW_FORM_block4;
8284 default:
8285 gcc_unreachable ();
8286 }
8287 case dw_val_class_const:
8288 return DW_FORM_sdata;
8289 case dw_val_class_unsigned_const:
8290 switch (constant_size (AT_unsigned (a)))
8291 {
8292 case 1:
8293 return DW_FORM_data1;
8294 case 2:
8295 return DW_FORM_data2;
8296 case 4:
8297 /* In DWARF3 DW_AT_data_member_location with
8298 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8299 constant, so we need to use DW_FORM_udata if we need
8300 a large constant. */
8301 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8302 return DW_FORM_udata;
8303 return DW_FORM_data4;
8304 case 8:
8305 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8306 return DW_FORM_udata;
8307 return DW_FORM_data8;
8308 default:
8309 gcc_unreachable ();
8310 }
8311 case dw_val_class_const_double:
8312 switch (HOST_BITS_PER_WIDE_INT)
8313 {
8314 case 8:
8315 return DW_FORM_data2;
8316 case 16:
8317 return DW_FORM_data4;
8318 case 32:
8319 return DW_FORM_data8;
8320 case 64:
8321 default:
8322 return DW_FORM_block1;
8323 }
8324 case dw_val_class_wide_int:
8325 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8326 {
8327 case 8:
8328 return DW_FORM_data1;
8329 case 16:
8330 return DW_FORM_data2;
8331 case 32:
8332 return DW_FORM_data4;
8333 case 64:
8334 return DW_FORM_data8;
8335 default:
8336 return DW_FORM_block1;
8337 }
8338 case dw_val_class_vec:
8339 switch (constant_size (a->dw_attr_val.v.val_vec.length
8340 * a->dw_attr_val.v.val_vec.elt_size))
8341 {
8342 case 1:
8343 return DW_FORM_block1;
8344 case 2:
8345 return DW_FORM_block2;
8346 case 4:
8347 return DW_FORM_block4;
8348 default:
8349 gcc_unreachable ();
8350 }
8351 case dw_val_class_flag:
8352 if (dwarf_version >= 4)
8353 {
8354 /* Currently all add_AT_flag calls pass in 1 as last argument,
8355 so DW_FORM_flag_present can be used. If that ever changes,
8356 we'll need to use DW_FORM_flag and have some optimization
8357 in build_abbrev_table that will change those to
8358 DW_FORM_flag_present if it is set to 1 in all DIEs using
8359 the same abbrev entry. */
8360 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8361 return DW_FORM_flag_present;
8362 }
8363 return DW_FORM_flag;
8364 case dw_val_class_die_ref:
8365 if (AT_ref_external (a))
8366 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8367 else
8368 return DW_FORM_ref;
8369 case dw_val_class_fde_ref:
8370 return DW_FORM_data;
8371 case dw_val_class_lbl_id:
8372 return (AT_index (a) == NOT_INDEXED
8373 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8374 case dw_val_class_lineptr:
8375 case dw_val_class_macptr:
8376 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8377 case dw_val_class_str:
8378 return AT_string_form (a);
8379 case dw_val_class_file:
8380 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8381 {
8382 case 1:
8383 return DW_FORM_data1;
8384 case 2:
8385 return DW_FORM_data2;
8386 case 4:
8387 return DW_FORM_data4;
8388 default:
8389 gcc_unreachable ();
8390 }
8391
8392 case dw_val_class_data8:
8393 return DW_FORM_data8;
8394
8395 case dw_val_class_high_pc:
8396 switch (DWARF2_ADDR_SIZE)
8397 {
8398 case 1:
8399 return DW_FORM_data1;
8400 case 2:
8401 return DW_FORM_data2;
8402 case 4:
8403 return DW_FORM_data4;
8404 case 8:
8405 return DW_FORM_data8;
8406 default:
8407 gcc_unreachable ();
8408 }
8409
8410 default:
8411 gcc_unreachable ();
8412 }
8413 }
8414
8415 /* Output the encoding of an attribute value. */
8416
8417 static void
8418 output_value_format (dw_attr_ref a)
8419 {
8420 enum dwarf_form form = value_format (a);
8421
8422 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8423 }
8424
8425 /* Given a die and id, produce the appropriate abbreviations. */
8426
8427 static void
8428 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8429 {
8430 unsigned ix;
8431 dw_attr_ref a_attr;
8432
8433 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8434 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8435 dwarf_tag_name (abbrev->die_tag));
8436
8437 if (abbrev->die_child != NULL)
8438 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8439 else
8440 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8441
8442 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8443 {
8444 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8445 dwarf_attr_name (a_attr->dw_attr));
8446 output_value_format (a_attr);
8447 }
8448
8449 dw2_asm_output_data (1, 0, NULL);
8450 dw2_asm_output_data (1, 0, NULL);
8451 }
8452
8453
8454 /* Output the .debug_abbrev section which defines the DIE abbreviation
8455 table. */
8456
8457 static void
8458 output_abbrev_section (void)
8459 {
8460 unsigned long abbrev_id;
8461
8462 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8463 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8464
8465 /* Terminate the table. */
8466 dw2_asm_output_data (1, 0, NULL);
8467 }
8468
8469 /* Output a symbol we can use to refer to this DIE from another CU. */
8470
8471 static inline void
8472 output_die_symbol (dw_die_ref die)
8473 {
8474 const char *sym = die->die_id.die_symbol;
8475
8476 gcc_assert (!die->comdat_type_p);
8477
8478 if (sym == 0)
8479 return;
8480
8481 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8482 /* We make these global, not weak; if the target doesn't support
8483 .linkonce, it doesn't support combining the sections, so debugging
8484 will break. */
8485 targetm.asm_out.globalize_label (asm_out_file, sym);
8486
8487 ASM_OUTPUT_LABEL (asm_out_file, sym);
8488 }
8489
8490 /* Return a new location list, given the begin and end range, and the
8491 expression. */
8492
8493 static inline dw_loc_list_ref
8494 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8495 const char *section)
8496 {
8497 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8498
8499 retlist->begin = begin;
8500 retlist->begin_entry = NULL;
8501 retlist->end = end;
8502 retlist->expr = expr;
8503 retlist->section = section;
8504
8505 return retlist;
8506 }
8507
8508 /* Generate a new internal symbol for this location list node, if it
8509 hasn't got one yet. */
8510
8511 static inline void
8512 gen_llsym (dw_loc_list_ref list)
8513 {
8514 gcc_assert (!list->ll_symbol);
8515 list->ll_symbol = gen_internal_sym ("LLST");
8516 }
8517
8518 /* Output the location list given to us. */
8519
8520 static void
8521 output_loc_list (dw_loc_list_ref list_head)
8522 {
8523 dw_loc_list_ref curr = list_head;
8524
8525 if (list_head->emitted)
8526 return;
8527 list_head->emitted = true;
8528
8529 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8530
8531 /* Walk the location list, and output each range + expression. */
8532 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8533 {
8534 unsigned long size;
8535 /* Don't output an entry that starts and ends at the same address. */
8536 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8537 continue;
8538 size = size_of_locs (curr->expr);
8539 /* If the expression is too large, drop it on the floor. We could
8540 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8541 in the expression, but >= 64KB expressions for a single value
8542 in a single range are unlikely very useful. */
8543 if (size > 0xffff)
8544 continue;
8545 if (dwarf_split_debug_info)
8546 {
8547 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8548 "Location list start/length entry (%s)",
8549 list_head->ll_symbol);
8550 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8551 "Location list range start index (%s)",
8552 curr->begin);
8553 /* The length field is 4 bytes. If we ever need to support
8554 an 8-byte length, we can add a new DW_LLE code or fall back
8555 to DW_LLE_GNU_start_end_entry. */
8556 dw2_asm_output_delta (4, curr->end, curr->begin,
8557 "Location list range length (%s)",
8558 list_head->ll_symbol);
8559 }
8560 else if (!have_multiple_function_sections)
8561 {
8562 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8563 "Location list begin address (%s)",
8564 list_head->ll_symbol);
8565 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8566 "Location list end address (%s)",
8567 list_head->ll_symbol);
8568 }
8569 else
8570 {
8571 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8572 "Location list begin address (%s)",
8573 list_head->ll_symbol);
8574 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8575 "Location list end address (%s)",
8576 list_head->ll_symbol);
8577 }
8578
8579 /* Output the block length for this list of location operations. */
8580 gcc_assert (size <= 0xffff);
8581 dw2_asm_output_data (2, size, "%s", "Location expression size");
8582
8583 output_loc_sequence (curr->expr, -1);
8584 }
8585
8586 if (dwarf_split_debug_info)
8587 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8588 "Location list terminator (%s)",
8589 list_head->ll_symbol);
8590 else
8591 {
8592 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8593 "Location list terminator begin (%s)",
8594 list_head->ll_symbol);
8595 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8596 "Location list terminator end (%s)",
8597 list_head->ll_symbol);
8598 }
8599 }
8600
8601 /* Output a range_list offset into the debug_range section. Emit a
8602 relocated reference if val_entry is NULL, otherwise, emit an
8603 indirect reference. */
8604
8605 static void
8606 output_range_list_offset (dw_attr_ref a)
8607 {
8608 const char *name = dwarf_attr_name (a->dw_attr);
8609
8610 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8611 {
8612 char *p = strchr (ranges_section_label, '\0');
8613 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8614 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8615 debug_ranges_section, "%s", name);
8616 *p = '\0';
8617 }
8618 else
8619 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8620 "%s (offset from %s)", name, ranges_section_label);
8621 }
8622
8623 /* Output the offset into the debug_loc section. */
8624
8625 static void
8626 output_loc_list_offset (dw_attr_ref a)
8627 {
8628 char *sym = AT_loc_list (a)->ll_symbol;
8629
8630 gcc_assert (sym);
8631 if (dwarf_split_debug_info)
8632 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8633 "%s", dwarf_attr_name (a->dw_attr));
8634 else
8635 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8636 "%s", dwarf_attr_name (a->dw_attr));
8637 }
8638
8639 /* Output an attribute's index or value appropriately. */
8640
8641 static void
8642 output_attr_index_or_value (dw_attr_ref a)
8643 {
8644 const char *name = dwarf_attr_name (a->dw_attr);
8645
8646 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8647 {
8648 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8649 return;
8650 }
8651 switch (AT_class (a))
8652 {
8653 case dw_val_class_addr:
8654 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8655 break;
8656 case dw_val_class_high_pc:
8657 case dw_val_class_lbl_id:
8658 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8659 break;
8660 case dw_val_class_loc_list:
8661 output_loc_list_offset (a);
8662 break;
8663 default:
8664 gcc_unreachable ();
8665 }
8666 }
8667
8668 /* Output a type signature. */
8669
8670 static inline void
8671 output_signature (const char *sig, const char *name)
8672 {
8673 int i;
8674
8675 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8676 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8677 }
8678
8679 /* Output the DIE and its attributes. Called recursively to generate
8680 the definitions of each child DIE. */
8681
8682 static void
8683 output_die (dw_die_ref die)
8684 {
8685 dw_attr_ref a;
8686 dw_die_ref c;
8687 unsigned long size;
8688 unsigned ix;
8689
8690 /* If someone in another CU might refer to us, set up a symbol for
8691 them to point to. */
8692 if (! die->comdat_type_p && die->die_id.die_symbol)
8693 output_die_symbol (die);
8694
8695 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8696 (unsigned long)die->die_offset,
8697 dwarf_tag_name (die->die_tag));
8698
8699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8700 {
8701 const char *name = dwarf_attr_name (a->dw_attr);
8702
8703 switch (AT_class (a))
8704 {
8705 case dw_val_class_addr:
8706 output_attr_index_or_value (a);
8707 break;
8708
8709 case dw_val_class_offset:
8710 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8711 "%s", name);
8712 break;
8713
8714 case dw_val_class_range_list:
8715 output_range_list_offset (a);
8716 break;
8717
8718 case dw_val_class_loc:
8719 size = size_of_locs (AT_loc (a));
8720
8721 /* Output the block length for this list of location operations. */
8722 if (dwarf_version >= 4)
8723 dw2_asm_output_data_uleb128 (size, "%s", name);
8724 else
8725 dw2_asm_output_data (constant_size (size), size, "%s", name);
8726
8727 output_loc_sequence (AT_loc (a), -1);
8728 break;
8729
8730 case dw_val_class_const:
8731 /* ??? It would be slightly more efficient to use a scheme like is
8732 used for unsigned constants below, but gdb 4.x does not sign
8733 extend. Gdb 5.x does sign extend. */
8734 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8735 break;
8736
8737 case dw_val_class_unsigned_const:
8738 {
8739 int csize = constant_size (AT_unsigned (a));
8740 if (dwarf_version == 3
8741 && a->dw_attr == DW_AT_data_member_location
8742 && csize >= 4)
8743 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8744 else
8745 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8746 }
8747 break;
8748
8749 case dw_val_class_const_double:
8750 {
8751 unsigned HOST_WIDE_INT first, second;
8752
8753 if (HOST_BITS_PER_WIDE_INT >= 64)
8754 dw2_asm_output_data (1,
8755 HOST_BITS_PER_DOUBLE_INT
8756 / HOST_BITS_PER_CHAR,
8757 NULL);
8758
8759 if (WORDS_BIG_ENDIAN)
8760 {
8761 first = a->dw_attr_val.v.val_double.high;
8762 second = a->dw_attr_val.v.val_double.low;
8763 }
8764 else
8765 {
8766 first = a->dw_attr_val.v.val_double.low;
8767 second = a->dw_attr_val.v.val_double.high;
8768 }
8769
8770 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8771 first, "%s", name);
8772 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8773 second, NULL);
8774 }
8775 break;
8776
8777 case dw_val_class_wide_int:
8778 {
8779 int i;
8780 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8781 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8782 if (len * HOST_BITS_PER_WIDE_INT > 64)
8783 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8784 NULL);
8785
8786 if (WORDS_BIG_ENDIAN)
8787 for (i = len - 1; i >= 0; --i)
8788 {
8789 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8790 name);
8791 name = NULL;
8792 }
8793 else
8794 for (i = 0; i < len; ++i)
8795 {
8796 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8797 name);
8798 name = NULL;
8799 }
8800 }
8801 break;
8802
8803 case dw_val_class_vec:
8804 {
8805 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8806 unsigned int len = a->dw_attr_val.v.val_vec.length;
8807 unsigned int i;
8808 unsigned char *p;
8809
8810 dw2_asm_output_data (constant_size (len * elt_size),
8811 len * elt_size, "%s", name);
8812 if (elt_size > sizeof (HOST_WIDE_INT))
8813 {
8814 elt_size /= 2;
8815 len *= 2;
8816 }
8817 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8818 i < len;
8819 i++, p += elt_size)
8820 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8821 "fp or vector constant word %u", i);
8822 break;
8823 }
8824
8825 case dw_val_class_flag:
8826 if (dwarf_version >= 4)
8827 {
8828 /* Currently all add_AT_flag calls pass in 1 as last argument,
8829 so DW_FORM_flag_present can be used. If that ever changes,
8830 we'll need to use DW_FORM_flag and have some optimization
8831 in build_abbrev_table that will change those to
8832 DW_FORM_flag_present if it is set to 1 in all DIEs using
8833 the same abbrev entry. */
8834 gcc_assert (AT_flag (a) == 1);
8835 if (flag_debug_asm)
8836 fprintf (asm_out_file, "\t\t\t%s %s\n",
8837 ASM_COMMENT_START, name);
8838 break;
8839 }
8840 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8841 break;
8842
8843 case dw_val_class_loc_list:
8844 output_attr_index_or_value (a);
8845 break;
8846
8847 case dw_val_class_die_ref:
8848 if (AT_ref_external (a))
8849 {
8850 if (AT_ref (a)->comdat_type_p)
8851 {
8852 comdat_type_node_ref type_node =
8853 AT_ref (a)->die_id.die_type_node;
8854
8855 gcc_assert (type_node);
8856 output_signature (type_node->signature, name);
8857 }
8858 else
8859 {
8860 const char *sym = AT_ref (a)->die_id.die_symbol;
8861 int size;
8862
8863 gcc_assert (sym);
8864 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8865 length, whereas in DWARF3 it's always sized as an
8866 offset. */
8867 if (dwarf_version == 2)
8868 size = DWARF2_ADDR_SIZE;
8869 else
8870 size = DWARF_OFFSET_SIZE;
8871 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8872 name);
8873 }
8874 }
8875 else
8876 {
8877 gcc_assert (AT_ref (a)->die_offset);
8878 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8879 "%s", name);
8880 }
8881 break;
8882
8883 case dw_val_class_fde_ref:
8884 {
8885 char l1[20];
8886
8887 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8888 a->dw_attr_val.v.val_fde_index * 2);
8889 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8890 "%s", name);
8891 }
8892 break;
8893
8894 case dw_val_class_vms_delta:
8895 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8896 AT_vms_delta2 (a), AT_vms_delta1 (a),
8897 "%s", name);
8898 break;
8899
8900 case dw_val_class_lbl_id:
8901 output_attr_index_or_value (a);
8902 break;
8903
8904 case dw_val_class_lineptr:
8905 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8906 debug_line_section, "%s", name);
8907 break;
8908
8909 case dw_val_class_macptr:
8910 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8911 debug_macinfo_section, "%s", name);
8912 break;
8913
8914 case dw_val_class_str:
8915 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8916 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8917 a->dw_attr_val.v.val_str->label,
8918 debug_str_section,
8919 "%s: \"%s\"", name, AT_string (a));
8920 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8921 dw2_asm_output_data_uleb128 (AT_index (a),
8922 "%s: \"%s\"", name, AT_string (a));
8923 else
8924 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8925 break;
8926
8927 case dw_val_class_file:
8928 {
8929 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8930
8931 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8932 a->dw_attr_val.v.val_file->filename);
8933 break;
8934 }
8935
8936 case dw_val_class_data8:
8937 {
8938 int i;
8939
8940 for (i = 0; i < 8; i++)
8941 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8942 i == 0 ? "%s" : NULL, name);
8943 break;
8944 }
8945
8946 case dw_val_class_high_pc:
8947 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8948 get_AT_low_pc (die), "DW_AT_high_pc");
8949 break;
8950
8951 default:
8952 gcc_unreachable ();
8953 }
8954 }
8955
8956 FOR_EACH_CHILD (die, c, output_die (c));
8957
8958 /* Add null byte to terminate sibling list. */
8959 if (die->die_child != NULL)
8960 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8961 (unsigned long) die->die_offset);
8962 }
8963
8964 /* Output the compilation unit that appears at the beginning of the
8965 .debug_info section, and precedes the DIE descriptions. */
8966
8967 static void
8968 output_compilation_unit_header (void)
8969 {
8970 /* We don't support actual DWARFv5 units yet, we just use some
8971 DWARFv5 draft DIE tags in DWARFv4 format. */
8972 int ver = dwarf_version < 5 ? dwarf_version : 4;
8973
8974 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8975 dw2_asm_output_data (4, 0xffffffff,
8976 "Initial length escape value indicating 64-bit DWARF extension");
8977 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8978 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8979 "Length of Compilation Unit Info");
8980 dw2_asm_output_data (2, ver, "DWARF version number");
8981 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8982 debug_abbrev_section,
8983 "Offset Into Abbrev. Section");
8984 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8985 }
8986
8987 /* Output the compilation unit DIE and its children. */
8988
8989 static void
8990 output_comp_unit (dw_die_ref die, int output_if_empty)
8991 {
8992 const char *secname, *oldsym;
8993 char *tmp;
8994
8995 /* Unless we are outputting main CU, we may throw away empty ones. */
8996 if (!output_if_empty && die->die_child == NULL)
8997 return;
8998
8999 /* Even if there are no children of this DIE, we must output the information
9000 about the compilation unit. Otherwise, on an empty translation unit, we
9001 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9002 will then complain when examining the file. First mark all the DIEs in
9003 this CU so we know which get local refs. */
9004 mark_dies (die);
9005
9006 external_ref_hash_type *extern_map = optimize_external_refs (die);
9007
9008 build_abbrev_table (die, extern_map);
9009
9010 delete extern_map;
9011
9012 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9013 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9014 calc_die_sizes (die);
9015
9016 oldsym = die->die_id.die_symbol;
9017 if (oldsym)
9018 {
9019 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9020
9021 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9022 secname = tmp;
9023 die->die_id.die_symbol = NULL;
9024 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9025 }
9026 else
9027 {
9028 switch_to_section (debug_info_section);
9029 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9030 info_section_emitted = true;
9031 }
9032
9033 /* Output debugging information. */
9034 output_compilation_unit_header ();
9035 output_die (die);
9036
9037 /* Leave the marks on the main CU, so we can check them in
9038 output_pubnames. */
9039 if (oldsym)
9040 {
9041 unmark_dies (die);
9042 die->die_id.die_symbol = oldsym;
9043 }
9044 }
9045
9046 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9047 and .debug_pubtypes. This is configured per-target, but can be
9048 overridden by the -gpubnames or -gno-pubnames options. */
9049
9050 static inline bool
9051 want_pubnames (void)
9052 {
9053 if (debug_info_level <= DINFO_LEVEL_TERSE)
9054 return false;
9055 if (debug_generate_pub_sections != -1)
9056 return debug_generate_pub_sections;
9057 return targetm.want_debug_pub_sections;
9058 }
9059
9060 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9061
9062 static void
9063 add_AT_pubnames (dw_die_ref die)
9064 {
9065 if (want_pubnames ())
9066 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9067 }
9068
9069 /* Add a string attribute value to a skeleton DIE. */
9070
9071 static inline void
9072 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9073 const char *str)
9074 {
9075 dw_attr_node attr;
9076 struct indirect_string_node *node;
9077
9078 if (! skeleton_debug_str_hash)
9079 skeleton_debug_str_hash
9080 = hash_table<indirect_string_hasher>::create_ggc (10);
9081
9082 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9083 find_string_form (node);
9084 if (node->form == DW_FORM_GNU_str_index)
9085 node->form = DW_FORM_strp;
9086
9087 attr.dw_attr = attr_kind;
9088 attr.dw_attr_val.val_class = dw_val_class_str;
9089 attr.dw_attr_val.val_entry = NULL;
9090 attr.dw_attr_val.v.val_str = node;
9091 add_dwarf_attr (die, &attr);
9092 }
9093
9094 /* Helper function to generate top-level dies for skeleton debug_info and
9095 debug_types. */
9096
9097 static void
9098 add_top_level_skeleton_die_attrs (dw_die_ref die)
9099 {
9100 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9101 const char *comp_dir = comp_dir_string ();
9102
9103 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9104 if (comp_dir != NULL)
9105 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9106 add_AT_pubnames (die);
9107 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9108 }
9109
9110 /* Output skeleton debug sections that point to the dwo file. */
9111
9112 static void
9113 output_skeleton_debug_sections (dw_die_ref comp_unit)
9114 {
9115 /* We don't support actual DWARFv5 units yet, we just use some
9116 DWARFv5 draft DIE tags in DWARFv4 format. */
9117 int ver = dwarf_version < 5 ? dwarf_version : 4;
9118
9119 /* These attributes will be found in the full debug_info section. */
9120 remove_AT (comp_unit, DW_AT_producer);
9121 remove_AT (comp_unit, DW_AT_language);
9122
9123 switch_to_section (debug_skeleton_info_section);
9124 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9125
9126 /* Produce the skeleton compilation-unit header. This one differs enough from
9127 a normal CU header that it's better not to call output_compilation_unit
9128 header. */
9129 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9130 dw2_asm_output_data (4, 0xffffffff,
9131 "Initial length escape value indicating 64-bit DWARF extension");
9132
9133 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9134 DWARF_COMPILE_UNIT_HEADER_SIZE
9135 - DWARF_INITIAL_LENGTH_SIZE
9136 + size_of_die (comp_unit),
9137 "Length of Compilation Unit Info");
9138 dw2_asm_output_data (2, ver, "DWARF version number");
9139 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9140 debug_abbrev_section,
9141 "Offset Into Abbrev. Section");
9142 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9143
9144 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9145 output_die (comp_unit);
9146
9147 /* Build the skeleton debug_abbrev section. */
9148 switch_to_section (debug_skeleton_abbrev_section);
9149 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9150
9151 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9152
9153 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9154 }
9155
9156 /* Output a comdat type unit DIE and its children. */
9157
9158 static void
9159 output_comdat_type_unit (comdat_type_node *node)
9160 {
9161 const char *secname;
9162 char *tmp;
9163 int i;
9164 #if defined (OBJECT_FORMAT_ELF)
9165 tree comdat_key;
9166 #endif
9167
9168 /* First mark all the DIEs in this CU so we know which get local refs. */
9169 mark_dies (node->root_die);
9170
9171 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9172
9173 build_abbrev_table (node->root_die, extern_map);
9174
9175 delete extern_map;
9176 extern_map = NULL;
9177
9178 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9179 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9180 calc_die_sizes (node->root_die);
9181
9182 #if defined (OBJECT_FORMAT_ELF)
9183 if (!dwarf_split_debug_info)
9184 secname = ".debug_types";
9185 else
9186 secname = ".debug_types.dwo";
9187
9188 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9189 sprintf (tmp, "wt.");
9190 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9191 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9192 comdat_key = get_identifier (tmp);
9193 targetm.asm_out.named_section (secname,
9194 SECTION_DEBUG | SECTION_LINKONCE,
9195 comdat_key);
9196 #else
9197 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9198 sprintf (tmp, ".gnu.linkonce.wt.");
9199 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9200 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9201 secname = tmp;
9202 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9203 #endif
9204
9205 /* Output debugging information. */
9206 output_compilation_unit_header ();
9207 output_signature (node->signature, "Type Signature");
9208 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9209 "Offset to Type DIE");
9210 output_die (node->root_die);
9211
9212 unmark_dies (node->root_die);
9213 }
9214
9215 /* Return the DWARF2/3 pubname associated with a decl. */
9216
9217 static const char *
9218 dwarf2_name (tree decl, int scope)
9219 {
9220 if (DECL_NAMELESS (decl))
9221 return NULL;
9222 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9223 }
9224
9225 /* Add a new entry to .debug_pubnames if appropriate. */
9226
9227 static void
9228 add_pubname_string (const char *str, dw_die_ref die)
9229 {
9230 pubname_entry e;
9231
9232 e.die = die;
9233 e.name = xstrdup (str);
9234 vec_safe_push (pubname_table, e);
9235 }
9236
9237 static void
9238 add_pubname (tree decl, dw_die_ref die)
9239 {
9240 if (!want_pubnames ())
9241 return;
9242
9243 /* Don't add items to the table when we expect that the consumer will have
9244 just read the enclosing die. For example, if the consumer is looking at a
9245 class_member, it will either be inside the class already, or will have just
9246 looked up the class to find the member. Either way, searching the class is
9247 faster than searching the index. */
9248 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9249 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9250 {
9251 const char *name = dwarf2_name (decl, 1);
9252
9253 if (name)
9254 add_pubname_string (name, die);
9255 }
9256 }
9257
9258 /* Add an enumerator to the pubnames section. */
9259
9260 static void
9261 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9262 {
9263 pubname_entry e;
9264
9265 gcc_assert (scope_name);
9266 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9267 e.die = die;
9268 vec_safe_push (pubname_table, e);
9269 }
9270
9271 /* Add a new entry to .debug_pubtypes if appropriate. */
9272
9273 static void
9274 add_pubtype (tree decl, dw_die_ref die)
9275 {
9276 pubname_entry e;
9277
9278 if (!want_pubnames ())
9279 return;
9280
9281 if ((TREE_PUBLIC (decl)
9282 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9283 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9284 {
9285 tree scope = NULL;
9286 const char *scope_name = "";
9287 const char *sep = is_cxx () ? "::" : ".";
9288 const char *name;
9289
9290 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9291 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9292 {
9293 scope_name = lang_hooks.dwarf_name (scope, 1);
9294 if (scope_name != NULL && scope_name[0] != '\0')
9295 scope_name = concat (scope_name, sep, NULL);
9296 else
9297 scope_name = "";
9298 }
9299
9300 if (TYPE_P (decl))
9301 name = type_tag (decl);
9302 else
9303 name = lang_hooks.dwarf_name (decl, 1);
9304
9305 /* If we don't have a name for the type, there's no point in adding
9306 it to the table. */
9307 if (name != NULL && name[0] != '\0')
9308 {
9309 e.die = die;
9310 e.name = concat (scope_name, name, NULL);
9311 vec_safe_push (pubtype_table, e);
9312 }
9313
9314 /* Although it might be more consistent to add the pubinfo for the
9315 enumerators as their dies are created, they should only be added if the
9316 enum type meets the criteria above. So rather than re-check the parent
9317 enum type whenever an enumerator die is created, just output them all
9318 here. This isn't protected by the name conditional because anonymous
9319 enums don't have names. */
9320 if (die->die_tag == DW_TAG_enumeration_type)
9321 {
9322 dw_die_ref c;
9323
9324 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9325 }
9326 }
9327 }
9328
9329 /* Output a single entry in the pubnames table. */
9330
9331 static void
9332 output_pubname (dw_offset die_offset, pubname_entry *entry)
9333 {
9334 dw_die_ref die = entry->die;
9335 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9336
9337 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9338
9339 if (debug_generate_pub_sections == 2)
9340 {
9341 /* This logic follows gdb's method for determining the value of the flag
9342 byte. */
9343 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9344 switch (die->die_tag)
9345 {
9346 case DW_TAG_typedef:
9347 case DW_TAG_base_type:
9348 case DW_TAG_subrange_type:
9349 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9350 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9351 break;
9352 case DW_TAG_enumerator:
9353 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9354 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9355 if (!is_cxx () && !is_java ())
9356 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9357 break;
9358 case DW_TAG_subprogram:
9359 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9360 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9361 if (!is_ada ())
9362 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9363 break;
9364 case DW_TAG_constant:
9365 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9366 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9367 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9368 break;
9369 case DW_TAG_variable:
9370 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9371 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9372 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9373 break;
9374 case DW_TAG_namespace:
9375 case DW_TAG_imported_declaration:
9376 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9377 break;
9378 case DW_TAG_class_type:
9379 case DW_TAG_interface_type:
9380 case DW_TAG_structure_type:
9381 case DW_TAG_union_type:
9382 case DW_TAG_enumeration_type:
9383 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9384 if (!is_cxx () && !is_java ())
9385 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9386 break;
9387 default:
9388 /* An unusual tag. Leave the flag-byte empty. */
9389 break;
9390 }
9391 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9392 "GDB-index flags");
9393 }
9394
9395 dw2_asm_output_nstring (entry->name, -1, "external name");
9396 }
9397
9398
9399 /* Output the public names table used to speed up access to externally
9400 visible names; or the public types table used to find type definitions. */
9401
9402 static void
9403 output_pubnames (vec<pubname_entry, va_gc> *names)
9404 {
9405 unsigned i;
9406 unsigned long pubnames_length = size_of_pubnames (names);
9407 pubname_ref pub;
9408
9409 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9410 dw2_asm_output_data (4, 0xffffffff,
9411 "Initial length escape value indicating 64-bit DWARF extension");
9412 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9413
9414 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9415 dw2_asm_output_data (2, 2, "DWARF Version");
9416
9417 if (dwarf_split_debug_info)
9418 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9419 debug_skeleton_info_section,
9420 "Offset of Compilation Unit Info");
9421 else
9422 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9423 debug_info_section,
9424 "Offset of Compilation Unit Info");
9425 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9426 "Compilation Unit Length");
9427
9428 FOR_EACH_VEC_ELT (*names, i, pub)
9429 {
9430 if (include_pubname_in_output (names, pub))
9431 {
9432 dw_offset die_offset = pub->die->die_offset;
9433
9434 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9435 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9436 gcc_assert (pub->die->die_mark);
9437
9438 /* If we're putting types in their own .debug_types sections,
9439 the .debug_pubtypes table will still point to the compile
9440 unit (not the type unit), so we want to use the offset of
9441 the skeleton DIE (if there is one). */
9442 if (pub->die->comdat_type_p && names == pubtype_table)
9443 {
9444 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9445
9446 if (type_node != NULL)
9447 die_offset = (type_node->skeleton_die != NULL
9448 ? type_node->skeleton_die->die_offset
9449 : comp_unit_die ()->die_offset);
9450 }
9451
9452 output_pubname (die_offset, pub);
9453 }
9454 }
9455
9456 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9457 }
9458
9459 /* Output public names and types tables if necessary. */
9460
9461 static void
9462 output_pubtables (void)
9463 {
9464 if (!want_pubnames () || !info_section_emitted)
9465 return;
9466
9467 switch_to_section (debug_pubnames_section);
9468 output_pubnames (pubname_table);
9469 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9470 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9471 simply won't look for the section. */
9472 switch_to_section (debug_pubtypes_section);
9473 output_pubnames (pubtype_table);
9474 }
9475
9476
9477 /* Output the information that goes into the .debug_aranges table.
9478 Namely, define the beginning and ending address range of the
9479 text section generated for this compilation unit. */
9480
9481 static void
9482 output_aranges (unsigned long aranges_length)
9483 {
9484 unsigned i;
9485
9486 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9487 dw2_asm_output_data (4, 0xffffffff,
9488 "Initial length escape value indicating 64-bit DWARF extension");
9489 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9490 "Length of Address Ranges Info");
9491 /* Version number for aranges is still 2, even up to DWARF5. */
9492 dw2_asm_output_data (2, 2, "DWARF Version");
9493 if (dwarf_split_debug_info)
9494 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9495 debug_skeleton_info_section,
9496 "Offset of Compilation Unit Info");
9497 else
9498 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9499 debug_info_section,
9500 "Offset of Compilation Unit Info");
9501 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9502 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9503
9504 /* We need to align to twice the pointer size here. */
9505 if (DWARF_ARANGES_PAD_SIZE)
9506 {
9507 /* Pad using a 2 byte words so that padding is correct for any
9508 pointer size. */
9509 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9510 2 * DWARF2_ADDR_SIZE);
9511 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9512 dw2_asm_output_data (2, 0, NULL);
9513 }
9514
9515 /* It is necessary not to output these entries if the sections were
9516 not used; if the sections were not used, the length will be 0 and
9517 the address may end up as 0 if the section is discarded by ld
9518 --gc-sections, leaving an invalid (0, 0) entry that can be
9519 confused with the terminator. */
9520 if (text_section_used)
9521 {
9522 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9523 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9524 text_section_label, "Length");
9525 }
9526 if (cold_text_section_used)
9527 {
9528 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9529 "Address");
9530 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9531 cold_text_section_label, "Length");
9532 }
9533
9534 if (have_multiple_function_sections)
9535 {
9536 unsigned fde_idx;
9537 dw_fde_ref fde;
9538
9539 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9540 {
9541 if (DECL_IGNORED_P (fde->decl))
9542 continue;
9543 if (!fde->in_std_section)
9544 {
9545 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9546 "Address");
9547 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9548 fde->dw_fde_begin, "Length");
9549 }
9550 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9551 {
9552 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9553 "Address");
9554 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9555 fde->dw_fde_second_begin, "Length");
9556 }
9557 }
9558 }
9559
9560 /* Output the terminator words. */
9561 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9562 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9563 }
9564
9565 /* Add a new entry to .debug_ranges. Return the offset at which it
9566 was placed. */
9567
9568 static unsigned int
9569 add_ranges_num (int num)
9570 {
9571 unsigned int in_use = ranges_table_in_use;
9572
9573 if (in_use == ranges_table_allocated)
9574 {
9575 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9576 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9577 ranges_table_allocated);
9578 memset (ranges_table + ranges_table_in_use, 0,
9579 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9580 }
9581
9582 ranges_table[in_use].num = num;
9583 ranges_table_in_use = in_use + 1;
9584
9585 return in_use * 2 * DWARF2_ADDR_SIZE;
9586 }
9587
9588 /* Add a new entry to .debug_ranges corresponding to a block, or a
9589 range terminator if BLOCK is NULL. */
9590
9591 static unsigned int
9592 add_ranges (const_tree block)
9593 {
9594 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9595 }
9596
9597 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9598 When using dwarf_split_debug_info, address attributes in dies destined
9599 for the final executable should be direct references--setting the
9600 parameter force_direct ensures this behavior. */
9601
9602 static void
9603 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9604 bool *added, bool force_direct)
9605 {
9606 unsigned int in_use = ranges_by_label_in_use;
9607 unsigned int offset;
9608
9609 if (in_use == ranges_by_label_allocated)
9610 {
9611 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9612 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9613 ranges_by_label,
9614 ranges_by_label_allocated);
9615 memset (ranges_by_label + ranges_by_label_in_use, 0,
9616 RANGES_TABLE_INCREMENT
9617 * sizeof (struct dw_ranges_by_label_struct));
9618 }
9619
9620 ranges_by_label[in_use].begin = begin;
9621 ranges_by_label[in_use].end = end;
9622 ranges_by_label_in_use = in_use + 1;
9623
9624 offset = add_ranges_num (-(int)in_use - 1);
9625 if (!*added)
9626 {
9627 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9628 *added = true;
9629 }
9630 }
9631
9632 static void
9633 output_ranges (void)
9634 {
9635 unsigned i;
9636 static const char *const start_fmt = "Offset %#x";
9637 const char *fmt = start_fmt;
9638
9639 for (i = 0; i < ranges_table_in_use; i++)
9640 {
9641 int block_num = ranges_table[i].num;
9642
9643 if (block_num > 0)
9644 {
9645 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9646 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9647
9648 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9649 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9650
9651 /* If all code is in the text section, then the compilation
9652 unit base address defaults to DW_AT_low_pc, which is the
9653 base of the text section. */
9654 if (!have_multiple_function_sections)
9655 {
9656 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9657 text_section_label,
9658 fmt, i * 2 * DWARF2_ADDR_SIZE);
9659 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9660 text_section_label, NULL);
9661 }
9662
9663 /* Otherwise, the compilation unit base address is zero,
9664 which allows us to use absolute addresses, and not worry
9665 about whether the target supports cross-section
9666 arithmetic. */
9667 else
9668 {
9669 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9670 fmt, i * 2 * DWARF2_ADDR_SIZE);
9671 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9672 }
9673
9674 fmt = NULL;
9675 }
9676
9677 /* Negative block_num stands for an index into ranges_by_label. */
9678 else if (block_num < 0)
9679 {
9680 int lab_idx = - block_num - 1;
9681
9682 if (!have_multiple_function_sections)
9683 {
9684 gcc_unreachable ();
9685 #if 0
9686 /* If we ever use add_ranges_by_labels () for a single
9687 function section, all we have to do is to take out
9688 the #if 0 above. */
9689 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9690 ranges_by_label[lab_idx].begin,
9691 text_section_label,
9692 fmt, i * 2 * DWARF2_ADDR_SIZE);
9693 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9694 ranges_by_label[lab_idx].end,
9695 text_section_label, NULL);
9696 #endif
9697 }
9698 else
9699 {
9700 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9701 ranges_by_label[lab_idx].begin,
9702 fmt, i * 2 * DWARF2_ADDR_SIZE);
9703 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9704 ranges_by_label[lab_idx].end,
9705 NULL);
9706 }
9707 }
9708 else
9709 {
9710 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9711 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9712 fmt = start_fmt;
9713 }
9714 }
9715 }
9716
9717 /* Data structure containing information about input files. */
9718 struct file_info
9719 {
9720 const char *path; /* Complete file name. */
9721 const char *fname; /* File name part. */
9722 int length; /* Length of entire string. */
9723 struct dwarf_file_data * file_idx; /* Index in input file table. */
9724 int dir_idx; /* Index in directory table. */
9725 };
9726
9727 /* Data structure containing information about directories with source
9728 files. */
9729 struct dir_info
9730 {
9731 const char *path; /* Path including directory name. */
9732 int length; /* Path length. */
9733 int prefix; /* Index of directory entry which is a prefix. */
9734 int count; /* Number of files in this directory. */
9735 int dir_idx; /* Index of directory used as base. */
9736 };
9737
9738 /* Callback function for file_info comparison. We sort by looking at
9739 the directories in the path. */
9740
9741 static int
9742 file_info_cmp (const void *p1, const void *p2)
9743 {
9744 const struct file_info *const s1 = (const struct file_info *) p1;
9745 const struct file_info *const s2 = (const struct file_info *) p2;
9746 const unsigned char *cp1;
9747 const unsigned char *cp2;
9748
9749 /* Take care of file names without directories. We need to make sure that
9750 we return consistent values to qsort since some will get confused if
9751 we return the same value when identical operands are passed in opposite
9752 orders. So if neither has a directory, return 0 and otherwise return
9753 1 or -1 depending on which one has the directory. */
9754 if ((s1->path == s1->fname || s2->path == s2->fname))
9755 return (s2->path == s2->fname) - (s1->path == s1->fname);
9756
9757 cp1 = (const unsigned char *) s1->path;
9758 cp2 = (const unsigned char *) s2->path;
9759
9760 while (1)
9761 {
9762 ++cp1;
9763 ++cp2;
9764 /* Reached the end of the first path? If so, handle like above. */
9765 if ((cp1 == (const unsigned char *) s1->fname)
9766 || (cp2 == (const unsigned char *) s2->fname))
9767 return ((cp2 == (const unsigned char *) s2->fname)
9768 - (cp1 == (const unsigned char *) s1->fname));
9769
9770 /* Character of current path component the same? */
9771 else if (*cp1 != *cp2)
9772 return *cp1 - *cp2;
9773 }
9774 }
9775
9776 struct file_name_acquire_data
9777 {
9778 struct file_info *files;
9779 int used_files;
9780 int max_files;
9781 };
9782
9783 /* Traversal function for the hash table. */
9784
9785 int
9786 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9787 {
9788 struct dwarf_file_data *d = *slot;
9789 struct file_info *fi;
9790 const char *f;
9791
9792 gcc_assert (fnad->max_files >= d->emitted_number);
9793
9794 if (! d->emitted_number)
9795 return 1;
9796
9797 gcc_assert (fnad->max_files != fnad->used_files);
9798
9799 fi = fnad->files + fnad->used_files++;
9800
9801 /* Skip all leading "./". */
9802 f = d->filename;
9803 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9804 f += 2;
9805
9806 /* Create a new array entry. */
9807 fi->path = f;
9808 fi->length = strlen (f);
9809 fi->file_idx = d;
9810
9811 /* Search for the file name part. */
9812 f = strrchr (f, DIR_SEPARATOR);
9813 #if defined (DIR_SEPARATOR_2)
9814 {
9815 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9816
9817 if (g != NULL)
9818 {
9819 if (f == NULL || f < g)
9820 f = g;
9821 }
9822 }
9823 #endif
9824
9825 fi->fname = f == NULL ? fi->path : f + 1;
9826 return 1;
9827 }
9828
9829 /* Output the directory table and the file name table. We try to minimize
9830 the total amount of memory needed. A heuristic is used to avoid large
9831 slowdowns with many input files. */
9832
9833 static void
9834 output_file_names (void)
9835 {
9836 struct file_name_acquire_data fnad;
9837 int numfiles;
9838 struct file_info *files;
9839 struct dir_info *dirs;
9840 int *saved;
9841 int *savehere;
9842 int *backmap;
9843 int ndirs;
9844 int idx_offset;
9845 int i;
9846
9847 if (!last_emitted_file)
9848 {
9849 dw2_asm_output_data (1, 0, "End directory table");
9850 dw2_asm_output_data (1, 0, "End file name table");
9851 return;
9852 }
9853
9854 numfiles = last_emitted_file->emitted_number;
9855
9856 /* Allocate the various arrays we need. */
9857 files = XALLOCAVEC (struct file_info, numfiles);
9858 dirs = XALLOCAVEC (struct dir_info, numfiles);
9859
9860 fnad.files = files;
9861 fnad.used_files = 0;
9862 fnad.max_files = numfiles;
9863 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9864 gcc_assert (fnad.used_files == fnad.max_files);
9865
9866 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9867
9868 /* Find all the different directories used. */
9869 dirs[0].path = files[0].path;
9870 dirs[0].length = files[0].fname - files[0].path;
9871 dirs[0].prefix = -1;
9872 dirs[0].count = 1;
9873 dirs[0].dir_idx = 0;
9874 files[0].dir_idx = 0;
9875 ndirs = 1;
9876
9877 for (i = 1; i < numfiles; i++)
9878 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9879 && memcmp (dirs[ndirs - 1].path, files[i].path,
9880 dirs[ndirs - 1].length) == 0)
9881 {
9882 /* Same directory as last entry. */
9883 files[i].dir_idx = ndirs - 1;
9884 ++dirs[ndirs - 1].count;
9885 }
9886 else
9887 {
9888 int j;
9889
9890 /* This is a new directory. */
9891 dirs[ndirs].path = files[i].path;
9892 dirs[ndirs].length = files[i].fname - files[i].path;
9893 dirs[ndirs].count = 1;
9894 dirs[ndirs].dir_idx = ndirs;
9895 files[i].dir_idx = ndirs;
9896
9897 /* Search for a prefix. */
9898 dirs[ndirs].prefix = -1;
9899 for (j = 0; j < ndirs; j++)
9900 if (dirs[j].length < dirs[ndirs].length
9901 && dirs[j].length > 1
9902 && (dirs[ndirs].prefix == -1
9903 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9904 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9905 dirs[ndirs].prefix = j;
9906
9907 ++ndirs;
9908 }
9909
9910 /* Now to the actual work. We have to find a subset of the directories which
9911 allow expressing the file name using references to the directory table
9912 with the least amount of characters. We do not do an exhaustive search
9913 where we would have to check out every combination of every single
9914 possible prefix. Instead we use a heuristic which provides nearly optimal
9915 results in most cases and never is much off. */
9916 saved = XALLOCAVEC (int, ndirs);
9917 savehere = XALLOCAVEC (int, ndirs);
9918
9919 memset (saved, '\0', ndirs * sizeof (saved[0]));
9920 for (i = 0; i < ndirs; i++)
9921 {
9922 int j;
9923 int total;
9924
9925 /* We can always save some space for the current directory. But this
9926 does not mean it will be enough to justify adding the directory. */
9927 savehere[i] = dirs[i].length;
9928 total = (savehere[i] - saved[i]) * dirs[i].count;
9929
9930 for (j = i + 1; j < ndirs; j++)
9931 {
9932 savehere[j] = 0;
9933 if (saved[j] < dirs[i].length)
9934 {
9935 /* Determine whether the dirs[i] path is a prefix of the
9936 dirs[j] path. */
9937 int k;
9938
9939 k = dirs[j].prefix;
9940 while (k != -1 && k != (int) i)
9941 k = dirs[k].prefix;
9942
9943 if (k == (int) i)
9944 {
9945 /* Yes it is. We can possibly save some memory by
9946 writing the filenames in dirs[j] relative to
9947 dirs[i]. */
9948 savehere[j] = dirs[i].length;
9949 total += (savehere[j] - saved[j]) * dirs[j].count;
9950 }
9951 }
9952 }
9953
9954 /* Check whether we can save enough to justify adding the dirs[i]
9955 directory. */
9956 if (total > dirs[i].length + 1)
9957 {
9958 /* It's worthwhile adding. */
9959 for (j = i; j < ndirs; j++)
9960 if (savehere[j] > 0)
9961 {
9962 /* Remember how much we saved for this directory so far. */
9963 saved[j] = savehere[j];
9964
9965 /* Remember the prefix directory. */
9966 dirs[j].dir_idx = i;
9967 }
9968 }
9969 }
9970
9971 /* Emit the directory name table. */
9972 idx_offset = dirs[0].length > 0 ? 1 : 0;
9973 for (i = 1 - idx_offset; i < ndirs; i++)
9974 dw2_asm_output_nstring (dirs[i].path,
9975 dirs[i].length
9976 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9977 "Directory Entry: %#x", i + idx_offset);
9978
9979 dw2_asm_output_data (1, 0, "End directory table");
9980
9981 /* We have to emit them in the order of emitted_number since that's
9982 used in the debug info generation. To do this efficiently we
9983 generate a back-mapping of the indices first. */
9984 backmap = XALLOCAVEC (int, numfiles);
9985 for (i = 0; i < numfiles; i++)
9986 backmap[files[i].file_idx->emitted_number - 1] = i;
9987
9988 /* Now write all the file names. */
9989 for (i = 0; i < numfiles; i++)
9990 {
9991 int file_idx = backmap[i];
9992 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9993
9994 #ifdef VMS_DEBUGGING_INFO
9995 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9996
9997 /* Setting these fields can lead to debugger miscomparisons,
9998 but VMS Debug requires them to be set correctly. */
9999
10000 int ver;
10001 long long cdt;
10002 long siz;
10003 int maxfilelen = strlen (files[file_idx].path)
10004 + dirs[dir_idx].length
10005 + MAX_VMS_VERSION_LEN + 1;
10006 char *filebuf = XALLOCAVEC (char, maxfilelen);
10007
10008 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10009 snprintf (filebuf, maxfilelen, "%s;%d",
10010 files[file_idx].path + dirs[dir_idx].length, ver);
10011
10012 dw2_asm_output_nstring
10013 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10014
10015 /* Include directory index. */
10016 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10017
10018 /* Modification time. */
10019 dw2_asm_output_data_uleb128
10020 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10021 ? cdt : 0,
10022 NULL);
10023
10024 /* File length in bytes. */
10025 dw2_asm_output_data_uleb128
10026 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10027 ? siz : 0,
10028 NULL);
10029 #else
10030 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10031 "File Entry: %#x", (unsigned) i + 1);
10032
10033 /* Include directory index. */
10034 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10035
10036 /* Modification time. */
10037 dw2_asm_output_data_uleb128 (0, NULL);
10038
10039 /* File length in bytes. */
10040 dw2_asm_output_data_uleb128 (0, NULL);
10041 #endif /* VMS_DEBUGGING_INFO */
10042 }
10043
10044 dw2_asm_output_data (1, 0, "End file name table");
10045 }
10046
10047
10048 /* Output one line number table into the .debug_line section. */
10049
10050 static void
10051 output_one_line_info_table (dw_line_info_table *table)
10052 {
10053 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10054 unsigned int current_line = 1;
10055 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10056 dw_line_info_entry *ent;
10057 size_t i;
10058
10059 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10060 {
10061 switch (ent->opcode)
10062 {
10063 case LI_set_address:
10064 /* ??? Unfortunately, we have little choice here currently, and
10065 must always use the most general form. GCC does not know the
10066 address delta itself, so we can't use DW_LNS_advance_pc. Many
10067 ports do have length attributes which will give an upper bound
10068 on the address range. We could perhaps use length attributes
10069 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10070 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10071
10072 /* This can handle any delta. This takes
10073 4+DWARF2_ADDR_SIZE bytes. */
10074 dw2_asm_output_data (1, 0, "set address %s", line_label);
10075 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10076 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10077 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10078 break;
10079
10080 case LI_set_line:
10081 if (ent->val == current_line)
10082 {
10083 /* We still need to start a new row, so output a copy insn. */
10084 dw2_asm_output_data (1, DW_LNS_copy,
10085 "copy line %u", current_line);
10086 }
10087 else
10088 {
10089 int line_offset = ent->val - current_line;
10090 int line_delta = line_offset - DWARF_LINE_BASE;
10091
10092 current_line = ent->val;
10093 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10094 {
10095 /* This can handle deltas from -10 to 234, using the current
10096 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10097 This takes 1 byte. */
10098 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10099 "line %u", current_line);
10100 }
10101 else
10102 {
10103 /* This can handle any delta. This takes at least 4 bytes,
10104 depending on the value being encoded. */
10105 dw2_asm_output_data (1, DW_LNS_advance_line,
10106 "advance to line %u", current_line);
10107 dw2_asm_output_data_sleb128 (line_offset, NULL);
10108 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10109 }
10110 }
10111 break;
10112
10113 case LI_set_file:
10114 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10115 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10116 break;
10117
10118 case LI_set_column:
10119 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10120 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10121 break;
10122
10123 case LI_negate_stmt:
10124 current_is_stmt = !current_is_stmt;
10125 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10126 "is_stmt %d", current_is_stmt);
10127 break;
10128
10129 case LI_set_prologue_end:
10130 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10131 "set prologue end");
10132 break;
10133
10134 case LI_set_epilogue_begin:
10135 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10136 "set epilogue begin");
10137 break;
10138
10139 case LI_set_discriminator:
10140 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10141 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10142 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10143 dw2_asm_output_data_uleb128 (ent->val, NULL);
10144 break;
10145 }
10146 }
10147
10148 /* Emit debug info for the address of the end of the table. */
10149 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10150 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10151 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10152 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10153
10154 dw2_asm_output_data (1, 0, "end sequence");
10155 dw2_asm_output_data_uleb128 (1, NULL);
10156 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10157 }
10158
10159 /* Output the source line number correspondence information. This
10160 information goes into the .debug_line section. */
10161
10162 static void
10163 output_line_info (bool prologue_only)
10164 {
10165 char l1[20], l2[20], p1[20], p2[20];
10166 /* We don't support DWARFv5 line tables yet. */
10167 int ver = dwarf_version < 5 ? dwarf_version : 4;
10168 bool saw_one = false;
10169 int opc;
10170
10171 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10172 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10173 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10174 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10175
10176 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10177 dw2_asm_output_data (4, 0xffffffff,
10178 "Initial length escape value indicating 64-bit DWARF extension");
10179 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10180 "Length of Source Line Info");
10181 ASM_OUTPUT_LABEL (asm_out_file, l1);
10182
10183 dw2_asm_output_data (2, ver, "DWARF Version");
10184 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10185 ASM_OUTPUT_LABEL (asm_out_file, p1);
10186
10187 /* Define the architecture-dependent minimum instruction length (in bytes).
10188 In this implementation of DWARF, this field is used for information
10189 purposes only. Since GCC generates assembly language, we have no
10190 a priori knowledge of how many instruction bytes are generated for each
10191 source line, and therefore can use only the DW_LNE_set_address and
10192 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10193 this as '1', which is "correct enough" for all architectures,
10194 and don't let the target override. */
10195 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10196
10197 if (ver >= 4)
10198 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10199 "Maximum Operations Per Instruction");
10200 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10201 "Default is_stmt_start flag");
10202 dw2_asm_output_data (1, DWARF_LINE_BASE,
10203 "Line Base Value (Special Opcodes)");
10204 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10205 "Line Range Value (Special Opcodes)");
10206 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10207 "Special Opcode Base");
10208
10209 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10210 {
10211 int n_op_args;
10212 switch (opc)
10213 {
10214 case DW_LNS_advance_pc:
10215 case DW_LNS_advance_line:
10216 case DW_LNS_set_file:
10217 case DW_LNS_set_column:
10218 case DW_LNS_fixed_advance_pc:
10219 case DW_LNS_set_isa:
10220 n_op_args = 1;
10221 break;
10222 default:
10223 n_op_args = 0;
10224 break;
10225 }
10226
10227 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10228 opc, n_op_args);
10229 }
10230
10231 /* Write out the information about the files we use. */
10232 output_file_names ();
10233 ASM_OUTPUT_LABEL (asm_out_file, p2);
10234 if (prologue_only)
10235 {
10236 /* Output the marker for the end of the line number info. */
10237 ASM_OUTPUT_LABEL (asm_out_file, l2);
10238 return;
10239 }
10240
10241 if (separate_line_info)
10242 {
10243 dw_line_info_table *table;
10244 size_t i;
10245
10246 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10247 if (table->in_use)
10248 {
10249 output_one_line_info_table (table);
10250 saw_one = true;
10251 }
10252 }
10253 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10254 {
10255 output_one_line_info_table (cold_text_section_line_info);
10256 saw_one = true;
10257 }
10258
10259 /* ??? Some Darwin linkers crash on a .debug_line section with no
10260 sequences. Further, merely a DW_LNE_end_sequence entry is not
10261 sufficient -- the address column must also be initialized.
10262 Make sure to output at least one set_address/end_sequence pair,
10263 choosing .text since that section is always present. */
10264 if (text_section_line_info->in_use || !saw_one)
10265 output_one_line_info_table (text_section_line_info);
10266
10267 /* Output the marker for the end of the line number info. */
10268 ASM_OUTPUT_LABEL (asm_out_file, l2);
10269 }
10270 \f
10271 /* Given a pointer to a tree node for some base type, return a pointer to
10272 a DIE that describes the given type.
10273
10274 This routine must only be called for GCC type nodes that correspond to
10275 Dwarf base (fundamental) types. */
10276
10277 static dw_die_ref
10278 base_type_die (tree type)
10279 {
10280 dw_die_ref base_type_result;
10281 enum dwarf_type encoding;
10282
10283 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10284 return 0;
10285
10286 /* If this is a subtype that should not be emitted as a subrange type,
10287 use the base type. See subrange_type_for_debug_p. */
10288 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10289 type = TREE_TYPE (type);
10290
10291 switch (TREE_CODE (type))
10292 {
10293 case INTEGER_TYPE:
10294 if ((dwarf_version >= 4 || !dwarf_strict)
10295 && TYPE_NAME (type)
10296 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10297 && DECL_IS_BUILTIN (TYPE_NAME (type))
10298 && DECL_NAME (TYPE_NAME (type)))
10299 {
10300 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10301 if (strcmp (name, "char16_t") == 0
10302 || strcmp (name, "char32_t") == 0)
10303 {
10304 encoding = DW_ATE_UTF;
10305 break;
10306 }
10307 }
10308 if (TYPE_STRING_FLAG (type))
10309 {
10310 if (TYPE_UNSIGNED (type))
10311 encoding = DW_ATE_unsigned_char;
10312 else
10313 encoding = DW_ATE_signed_char;
10314 }
10315 else if (TYPE_UNSIGNED (type))
10316 encoding = DW_ATE_unsigned;
10317 else
10318 encoding = DW_ATE_signed;
10319 break;
10320
10321 case REAL_TYPE:
10322 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10323 {
10324 if (dwarf_version >= 3 || !dwarf_strict)
10325 encoding = DW_ATE_decimal_float;
10326 else
10327 encoding = DW_ATE_lo_user;
10328 }
10329 else
10330 encoding = DW_ATE_float;
10331 break;
10332
10333 case FIXED_POINT_TYPE:
10334 if (!(dwarf_version >= 3 || !dwarf_strict))
10335 encoding = DW_ATE_lo_user;
10336 else if (TYPE_UNSIGNED (type))
10337 encoding = DW_ATE_unsigned_fixed;
10338 else
10339 encoding = DW_ATE_signed_fixed;
10340 break;
10341
10342 /* Dwarf2 doesn't know anything about complex ints, so use
10343 a user defined type for it. */
10344 case COMPLEX_TYPE:
10345 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10346 encoding = DW_ATE_complex_float;
10347 else
10348 encoding = DW_ATE_lo_user;
10349 break;
10350
10351 case BOOLEAN_TYPE:
10352 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10353 encoding = DW_ATE_boolean;
10354 break;
10355
10356 default:
10357 /* No other TREE_CODEs are Dwarf fundamental types. */
10358 gcc_unreachable ();
10359 }
10360
10361 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10362
10363 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10364 int_size_in_bytes (type));
10365 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10366 add_pubtype (type, base_type_result);
10367
10368 return base_type_result;
10369 }
10370
10371 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10372 named 'auto' in its type: return true for it, false otherwise. */
10373
10374 static inline bool
10375 is_cxx_auto (tree type)
10376 {
10377 if (is_cxx ())
10378 {
10379 tree name = TYPE_IDENTIFIER (type);
10380 if (name == get_identifier ("auto")
10381 || name == get_identifier ("decltype(auto)"))
10382 return true;
10383 }
10384 return false;
10385 }
10386
10387 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10388 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10389
10390 static inline int
10391 is_base_type (tree type)
10392 {
10393 switch (TREE_CODE (type))
10394 {
10395 case ERROR_MARK:
10396 case VOID_TYPE:
10397 case INTEGER_TYPE:
10398 case REAL_TYPE:
10399 case FIXED_POINT_TYPE:
10400 case COMPLEX_TYPE:
10401 case BOOLEAN_TYPE:
10402 case POINTER_BOUNDS_TYPE:
10403 return 1;
10404
10405 case ARRAY_TYPE:
10406 case RECORD_TYPE:
10407 case UNION_TYPE:
10408 case QUAL_UNION_TYPE:
10409 case ENUMERAL_TYPE:
10410 case FUNCTION_TYPE:
10411 case METHOD_TYPE:
10412 case POINTER_TYPE:
10413 case REFERENCE_TYPE:
10414 case NULLPTR_TYPE:
10415 case OFFSET_TYPE:
10416 case LANG_TYPE:
10417 case VECTOR_TYPE:
10418 return 0;
10419
10420 default:
10421 if (is_cxx_auto (type))
10422 return 0;
10423 gcc_unreachable ();
10424 }
10425
10426 return 0;
10427 }
10428
10429 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10430 node, return the size in bits for the type if it is a constant, or else
10431 return the alignment for the type if the type's size is not constant, or
10432 else return BITS_PER_WORD if the type actually turns out to be an
10433 ERROR_MARK node. */
10434
10435 static inline unsigned HOST_WIDE_INT
10436 simple_type_size_in_bits (const_tree type)
10437 {
10438 if (TREE_CODE (type) == ERROR_MARK)
10439 return BITS_PER_WORD;
10440 else if (TYPE_SIZE (type) == NULL_TREE)
10441 return 0;
10442 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10443 return tree_to_uhwi (TYPE_SIZE (type));
10444 else
10445 return TYPE_ALIGN (type);
10446 }
10447
10448 /* Similarly, but return an offset_int instead of UHWI. */
10449
10450 static inline offset_int
10451 offset_int_type_size_in_bits (const_tree type)
10452 {
10453 if (TREE_CODE (type) == ERROR_MARK)
10454 return BITS_PER_WORD;
10455 else if (TYPE_SIZE (type) == NULL_TREE)
10456 return 0;
10457 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10458 return wi::to_offset (TYPE_SIZE (type));
10459 else
10460 return TYPE_ALIGN (type);
10461 }
10462
10463 /* Given a pointer to a tree node for a subrange type, return a pointer
10464 to a DIE that describes the given type. */
10465
10466 static dw_die_ref
10467 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10468 {
10469 dw_die_ref subrange_die;
10470 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10471
10472 if (context_die == NULL)
10473 context_die = comp_unit_die ();
10474
10475 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10476
10477 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10478 {
10479 /* The size of the subrange type and its base type do not match,
10480 so we need to generate a size attribute for the subrange type. */
10481 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10482 }
10483
10484 if (low)
10485 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10486 if (high)
10487 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10488
10489 return subrange_die;
10490 }
10491
10492 /* Returns the (const and/or volatile) cv_qualifiers associated with
10493 the decl node. This will normally be augmented with the
10494 cv_qualifiers of the underlying type in add_type_attribute. */
10495
10496 static int
10497 decl_quals (const_tree decl)
10498 {
10499 return ((TREE_READONLY (decl)
10500 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10501 | (TREE_THIS_VOLATILE (decl)
10502 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10503 }
10504
10505 /* Determine the TYPE whose qualifiers match the largest strict subset
10506 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10507 qualifiers outside QUAL_MASK. */
10508
10509 static int
10510 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10511 {
10512 tree t;
10513 int best_rank = 0, best_qual = 0, max_rank;
10514
10515 type_quals &= qual_mask;
10516 max_rank = popcount_hwi (type_quals) - 1;
10517
10518 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10519 t = TYPE_NEXT_VARIANT (t))
10520 {
10521 int q = TYPE_QUALS (t) & qual_mask;
10522
10523 if ((q & type_quals) == q && q != type_quals
10524 && check_base_type (t, type))
10525 {
10526 int rank = popcount_hwi (q);
10527
10528 if (rank > best_rank)
10529 {
10530 best_rank = rank;
10531 best_qual = q;
10532 }
10533 }
10534 }
10535
10536 return best_qual;
10537 }
10538
10539 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10540 entry that chains various modifiers in front of the given type. */
10541
10542 static dw_die_ref
10543 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10544 {
10545 enum tree_code code = TREE_CODE (type);
10546 dw_die_ref mod_type_die;
10547 dw_die_ref sub_die = NULL;
10548 tree item_type = NULL;
10549 tree qualified_type;
10550 tree name, low, high;
10551 dw_die_ref mod_scope;
10552 /* Only these cv-qualifiers are currently handled. */
10553 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10554 | TYPE_QUAL_RESTRICT);
10555
10556 if (code == ERROR_MARK)
10557 return NULL;
10558
10559 cv_quals &= cv_qual_mask;
10560
10561 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10562 tag modifier (and not an attribute) old consumers won't be able
10563 to handle it. */
10564 if (dwarf_version < 3)
10565 cv_quals &= ~TYPE_QUAL_RESTRICT;
10566
10567 /* See if we already have the appropriately qualified variant of
10568 this type. */
10569 qualified_type = get_qualified_type (type, cv_quals);
10570
10571 if (qualified_type == sizetype
10572 && TYPE_NAME (qualified_type)
10573 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10574 {
10575 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10576
10577 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10578 && TYPE_PRECISION (t)
10579 == TYPE_PRECISION (qualified_type)
10580 && TYPE_UNSIGNED (t)
10581 == TYPE_UNSIGNED (qualified_type));
10582 qualified_type = t;
10583 }
10584
10585 /* If we do, then we can just use its DIE, if it exists. */
10586 if (qualified_type)
10587 {
10588 mod_type_die = lookup_type_die (qualified_type);
10589 if (mod_type_die)
10590 return mod_type_die;
10591 }
10592
10593 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10594
10595 /* Handle C typedef types. */
10596 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10597 && !DECL_ARTIFICIAL (name))
10598 {
10599 tree dtype = TREE_TYPE (name);
10600
10601 if (qualified_type == dtype)
10602 {
10603 /* For a named type, use the typedef. */
10604 gen_type_die (qualified_type, context_die);
10605 return lookup_type_die (qualified_type);
10606 }
10607 else
10608 {
10609 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10610 dquals &= cv_qual_mask;
10611 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10612 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10613 /* cv-unqualified version of named type. Just use
10614 the unnamed type to which it refers. */
10615 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10616 cv_quals, context_die);
10617 /* Else cv-qualified version of named type; fall through. */
10618 }
10619 }
10620
10621 mod_scope = scope_die_for (type, context_die);
10622
10623 if (cv_quals)
10624 {
10625 struct qual_info { int q; enum dwarf_tag t; };
10626 static const struct qual_info qual_info[] =
10627 {
10628 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10629 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10630 { TYPE_QUAL_CONST, DW_TAG_const_type },
10631 };
10632 int sub_quals;
10633 unsigned i;
10634
10635 /* Determine a lesser qualified type that most closely matches
10636 this one. Then generate DW_TAG_* entries for the remaining
10637 qualifiers. */
10638 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10639 cv_qual_mask);
10640 mod_type_die = modified_type_die (type, sub_quals, context_die);
10641
10642 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10643 if (qual_info[i].q & cv_quals & ~sub_quals)
10644 {
10645 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10646 if (mod_type_die)
10647 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10648 mod_type_die = d;
10649 }
10650 }
10651 else if (code == POINTER_TYPE)
10652 {
10653 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10654 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10655 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10656 item_type = TREE_TYPE (type);
10657 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10658 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10659 TYPE_ADDR_SPACE (item_type));
10660 }
10661 else if (code == REFERENCE_TYPE)
10662 {
10663 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10664 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10665 type);
10666 else
10667 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10668 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10669 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10670 item_type = TREE_TYPE (type);
10671 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10672 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10673 TYPE_ADDR_SPACE (item_type));
10674 }
10675 else if (code == INTEGER_TYPE
10676 && TREE_TYPE (type) != NULL_TREE
10677 && subrange_type_for_debug_p (type, &low, &high))
10678 {
10679 mod_type_die = subrange_type_die (type, low, high, context_die);
10680 item_type = TREE_TYPE (type);
10681 }
10682 else if (is_base_type (type))
10683 mod_type_die = base_type_die (type);
10684 else
10685 {
10686 gen_type_die (type, context_die);
10687
10688 /* We have to get the type_main_variant here (and pass that to the
10689 `lookup_type_die' routine) because the ..._TYPE node we have
10690 might simply be a *copy* of some original type node (where the
10691 copy was created to help us keep track of typedef names) and
10692 that copy might have a different TYPE_UID from the original
10693 ..._TYPE node. */
10694 if (TREE_CODE (type) != VECTOR_TYPE)
10695 return lookup_type_die (type_main_variant (type));
10696 else
10697 /* Vectors have the debugging information in the type,
10698 not the main variant. */
10699 return lookup_type_die (type);
10700 }
10701
10702 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10703 don't output a DW_TAG_typedef, since there isn't one in the
10704 user's program; just attach a DW_AT_name to the type.
10705 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10706 if the base type already has the same name. */
10707 if (name
10708 && ((TREE_CODE (name) != TYPE_DECL
10709 && (qualified_type == TYPE_MAIN_VARIANT (type)
10710 || (cv_quals == TYPE_UNQUALIFIED)))
10711 || (TREE_CODE (name) == TYPE_DECL
10712 && TREE_TYPE (name) == qualified_type
10713 && DECL_NAME (name))))
10714 {
10715 if (TREE_CODE (name) == TYPE_DECL)
10716 /* Could just call add_name_and_src_coords_attributes here,
10717 but since this is a builtin type it doesn't have any
10718 useful source coordinates anyway. */
10719 name = DECL_NAME (name);
10720 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10721 }
10722 /* This probably indicates a bug. */
10723 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10724 {
10725 name = TYPE_IDENTIFIER (type);
10726 add_name_attribute (mod_type_die,
10727 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10728 }
10729
10730 if (qualified_type)
10731 equate_type_number_to_die (qualified_type, mod_type_die);
10732
10733 if (item_type)
10734 /* We must do this after the equate_type_number_to_die call, in case
10735 this is a recursive type. This ensures that the modified_type_die
10736 recursion will terminate even if the type is recursive. Recursive
10737 types are possible in Ada. */
10738 sub_die = modified_type_die (item_type,
10739 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10740 context_die);
10741
10742 if (sub_die != NULL)
10743 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10744
10745 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10746 if (TYPE_ARTIFICIAL (type))
10747 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10748
10749 return mod_type_die;
10750 }
10751
10752 /* Generate DIEs for the generic parameters of T.
10753 T must be either a generic type or a generic function.
10754 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10755
10756 static void
10757 gen_generic_params_dies (tree t)
10758 {
10759 tree parms, args;
10760 int parms_num, i;
10761 dw_die_ref die = NULL;
10762 int non_default;
10763
10764 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10765 return;
10766
10767 if (TYPE_P (t))
10768 die = lookup_type_die (t);
10769 else if (DECL_P (t))
10770 die = lookup_decl_die (t);
10771
10772 gcc_assert (die);
10773
10774 parms = lang_hooks.get_innermost_generic_parms (t);
10775 if (!parms)
10776 /* T has no generic parameter. It means T is neither a generic type
10777 or function. End of story. */
10778 return;
10779
10780 parms_num = TREE_VEC_LENGTH (parms);
10781 args = lang_hooks.get_innermost_generic_args (t);
10782 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10783 non_default = int_cst_value (TREE_CHAIN (args));
10784 else
10785 non_default = TREE_VEC_LENGTH (args);
10786 for (i = 0; i < parms_num; i++)
10787 {
10788 tree parm, arg, arg_pack_elems;
10789 dw_die_ref parm_die;
10790
10791 parm = TREE_VEC_ELT (parms, i);
10792 arg = TREE_VEC_ELT (args, i);
10793 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10794 gcc_assert (parm && TREE_VALUE (parm) && arg);
10795
10796 if (parm && TREE_VALUE (parm) && arg)
10797 {
10798 /* If PARM represents a template parameter pack,
10799 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10800 by DW_TAG_template_*_parameter DIEs for the argument
10801 pack elements of ARG. Note that ARG would then be
10802 an argument pack. */
10803 if (arg_pack_elems)
10804 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10805 arg_pack_elems,
10806 die);
10807 else
10808 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10809 true /* emit name */, die);
10810 if (i >= non_default)
10811 add_AT_flag (parm_die, DW_AT_default_value, 1);
10812 }
10813 }
10814 }
10815
10816 /* Create and return a DIE for PARM which should be
10817 the representation of a generic type parameter.
10818 For instance, in the C++ front end, PARM would be a template parameter.
10819 ARG is the argument to PARM.
10820 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10821 name of the PARM.
10822 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10823 as a child node. */
10824
10825 static dw_die_ref
10826 generic_parameter_die (tree parm, tree arg,
10827 bool emit_name_p,
10828 dw_die_ref parent_die)
10829 {
10830 dw_die_ref tmpl_die = NULL;
10831 const char *name = NULL;
10832
10833 if (!parm || !DECL_NAME (parm) || !arg)
10834 return NULL;
10835
10836 /* We support non-type generic parameters and arguments,
10837 type generic parameters and arguments, as well as
10838 generic generic parameters (a.k.a. template template parameters in C++)
10839 and arguments. */
10840 if (TREE_CODE (parm) == PARM_DECL)
10841 /* PARM is a nontype generic parameter */
10842 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10843 else if (TREE_CODE (parm) == TYPE_DECL)
10844 /* PARM is a type generic parameter. */
10845 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10846 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10847 /* PARM is a generic generic parameter.
10848 Its DIE is a GNU extension. It shall have a
10849 DW_AT_name attribute to represent the name of the template template
10850 parameter, and a DW_AT_GNU_template_name attribute to represent the
10851 name of the template template argument. */
10852 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10853 parent_die, parm);
10854 else
10855 gcc_unreachable ();
10856
10857 if (tmpl_die)
10858 {
10859 tree tmpl_type;
10860
10861 /* If PARM is a generic parameter pack, it means we are
10862 emitting debug info for a template argument pack element.
10863 In other terms, ARG is a template argument pack element.
10864 In that case, we don't emit any DW_AT_name attribute for
10865 the die. */
10866 if (emit_name_p)
10867 {
10868 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10869 gcc_assert (name);
10870 add_AT_string (tmpl_die, DW_AT_name, name);
10871 }
10872
10873 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10874 {
10875 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10876 TMPL_DIE should have a child DW_AT_type attribute that is set
10877 to the type of the argument to PARM, which is ARG.
10878 If PARM is a type generic parameter, TMPL_DIE should have a
10879 child DW_AT_type that is set to ARG. */
10880 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10881 add_type_attribute (tmpl_die, tmpl_type,
10882 (TREE_THIS_VOLATILE (tmpl_type)
10883 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10884 parent_die);
10885 }
10886 else
10887 {
10888 /* So TMPL_DIE is a DIE representing a
10889 a generic generic template parameter, a.k.a template template
10890 parameter in C++ and arg is a template. */
10891
10892 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10893 to the name of the argument. */
10894 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10895 if (name)
10896 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10897 }
10898
10899 if (TREE_CODE (parm) == PARM_DECL)
10900 /* So PARM is a non-type generic parameter.
10901 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10902 attribute of TMPL_DIE which value represents the value
10903 of ARG.
10904 We must be careful here:
10905 The value of ARG might reference some function decls.
10906 We might currently be emitting debug info for a generic
10907 type and types are emitted before function decls, we don't
10908 know if the function decls referenced by ARG will actually be
10909 emitted after cgraph computations.
10910 So must defer the generation of the DW_AT_const_value to
10911 after cgraph is ready. */
10912 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10913 }
10914
10915 return tmpl_die;
10916 }
10917
10918 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10919 PARM_PACK must be a template parameter pack. The returned DIE
10920 will be child DIE of PARENT_DIE. */
10921
10922 static dw_die_ref
10923 template_parameter_pack_die (tree parm_pack,
10924 tree parm_pack_args,
10925 dw_die_ref parent_die)
10926 {
10927 dw_die_ref die;
10928 int j;
10929
10930 gcc_assert (parent_die && parm_pack);
10931
10932 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10933 add_name_and_src_coords_attributes (die, parm_pack);
10934 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10935 generic_parameter_die (parm_pack,
10936 TREE_VEC_ELT (parm_pack_args, j),
10937 false /* Don't emit DW_AT_name */,
10938 die);
10939 return die;
10940 }
10941
10942 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10943 an enumerated type. */
10944
10945 static inline int
10946 type_is_enum (const_tree type)
10947 {
10948 return TREE_CODE (type) == ENUMERAL_TYPE;
10949 }
10950
10951 /* Return the DBX register number described by a given RTL node. */
10952
10953 static unsigned int
10954 dbx_reg_number (const_rtx rtl)
10955 {
10956 unsigned regno = REGNO (rtl);
10957
10958 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10959
10960 #ifdef LEAF_REG_REMAP
10961 if (crtl->uses_only_leaf_regs)
10962 {
10963 int leaf_reg = LEAF_REG_REMAP (regno);
10964 if (leaf_reg != -1)
10965 regno = (unsigned) leaf_reg;
10966 }
10967 #endif
10968
10969 regno = DBX_REGISTER_NUMBER (regno);
10970 gcc_assert (regno != INVALID_REGNUM);
10971 return regno;
10972 }
10973
10974 /* Optionally add a DW_OP_piece term to a location description expression.
10975 DW_OP_piece is only added if the location description expression already
10976 doesn't end with DW_OP_piece. */
10977
10978 static void
10979 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10980 {
10981 dw_loc_descr_ref loc;
10982
10983 if (*list_head != NULL)
10984 {
10985 /* Find the end of the chain. */
10986 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10987 ;
10988
10989 if (loc->dw_loc_opc != DW_OP_piece)
10990 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10991 }
10992 }
10993
10994 /* Return a location descriptor that designates a machine register or
10995 zero if there is none. */
10996
10997 static dw_loc_descr_ref
10998 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10999 {
11000 rtx regs;
11001
11002 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11003 return 0;
11004
11005 /* We only use "frame base" when we're sure we're talking about the
11006 post-prologue local stack frame. We do this by *not* running
11007 register elimination until this point, and recognizing the special
11008 argument pointer and soft frame pointer rtx's.
11009 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11010 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11011 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11012 {
11013 dw_loc_descr_ref result = NULL;
11014
11015 if (dwarf_version >= 4 || !dwarf_strict)
11016 {
11017 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11018 initialized);
11019 if (result)
11020 add_loc_descr (&result,
11021 new_loc_descr (DW_OP_stack_value, 0, 0));
11022 }
11023 return result;
11024 }
11025
11026 regs = targetm.dwarf_register_span (rtl);
11027
11028 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11029 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11030 else
11031 {
11032 unsigned int dbx_regnum = dbx_reg_number (rtl);
11033 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11034 return 0;
11035 return one_reg_loc_descriptor (dbx_regnum, initialized);
11036 }
11037 }
11038
11039 /* Return a location descriptor that designates a machine register for
11040 a given hard register number. */
11041
11042 static dw_loc_descr_ref
11043 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11044 {
11045 dw_loc_descr_ref reg_loc_descr;
11046
11047 if (regno <= 31)
11048 reg_loc_descr
11049 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11050 else
11051 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11052
11053 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11054 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11055
11056 return reg_loc_descr;
11057 }
11058
11059 /* Given an RTL of a register, return a location descriptor that
11060 designates a value that spans more than one register. */
11061
11062 static dw_loc_descr_ref
11063 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11064 enum var_init_status initialized)
11065 {
11066 int size, i;
11067 dw_loc_descr_ref loc_result = NULL;
11068
11069 /* Simple, contiguous registers. */
11070 if (regs == NULL_RTX)
11071 {
11072 unsigned reg = REGNO (rtl);
11073 int nregs;
11074
11075 #ifdef LEAF_REG_REMAP
11076 if (crtl->uses_only_leaf_regs)
11077 {
11078 int leaf_reg = LEAF_REG_REMAP (reg);
11079 if (leaf_reg != -1)
11080 reg = (unsigned) leaf_reg;
11081 }
11082 #endif
11083
11084 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11085 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11086
11087 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11088
11089 loc_result = NULL;
11090 while (nregs--)
11091 {
11092 dw_loc_descr_ref t;
11093
11094 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11095 VAR_INIT_STATUS_INITIALIZED);
11096 add_loc_descr (&loc_result, t);
11097 add_loc_descr_op_piece (&loc_result, size);
11098 ++reg;
11099 }
11100 return loc_result;
11101 }
11102
11103 /* Now onto stupid register sets in non contiguous locations. */
11104
11105 gcc_assert (GET_CODE (regs) == PARALLEL);
11106
11107 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11108 loc_result = NULL;
11109
11110 for (i = 0; i < XVECLEN (regs, 0); ++i)
11111 {
11112 dw_loc_descr_ref t;
11113
11114 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11115 VAR_INIT_STATUS_INITIALIZED);
11116 add_loc_descr (&loc_result, t);
11117 add_loc_descr_op_piece (&loc_result, size);
11118 }
11119
11120 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11121 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11122 return loc_result;
11123 }
11124
11125 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11126
11127 /* Return a location descriptor that designates a constant i,
11128 as a compound operation from constant (i >> shift), constant shift
11129 and DW_OP_shl. */
11130
11131 static dw_loc_descr_ref
11132 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11133 {
11134 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11135 add_loc_descr (&ret, int_loc_descriptor (shift));
11136 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11137 return ret;
11138 }
11139
11140 /* Return a location descriptor that designates a constant. */
11141
11142 static dw_loc_descr_ref
11143 int_loc_descriptor (HOST_WIDE_INT i)
11144 {
11145 enum dwarf_location_atom op;
11146
11147 /* Pick the smallest representation of a constant, rather than just
11148 defaulting to the LEB encoding. */
11149 if (i >= 0)
11150 {
11151 int clz = clz_hwi (i);
11152 int ctz = ctz_hwi (i);
11153 if (i <= 31)
11154 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11155 else if (i <= 0xff)
11156 op = DW_OP_const1u;
11157 else if (i <= 0xffff)
11158 op = DW_OP_const2u;
11159 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11160 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11161 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11162 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11163 while DW_OP_const4u is 5 bytes. */
11164 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11165 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11166 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11167 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11168 while DW_OP_const4u is 5 bytes. */
11169 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11170 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11171 op = DW_OP_const4u;
11172 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11173 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11174 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11175 while DW_OP_constu of constant >= 0x100000000 takes at least
11176 6 bytes. */
11177 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11178 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11179 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11180 >= HOST_BITS_PER_WIDE_INT)
11181 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11182 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11183 while DW_OP_constu takes in this case at least 6 bytes. */
11184 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11185 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11186 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11187 && size_of_uleb128 (i) > 6)
11188 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11189 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11190 else
11191 op = DW_OP_constu;
11192 }
11193 else
11194 {
11195 if (i >= -0x80)
11196 op = DW_OP_const1s;
11197 else if (i >= -0x8000)
11198 op = DW_OP_const2s;
11199 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11200 {
11201 if (size_of_int_loc_descriptor (i) < 5)
11202 {
11203 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11204 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11205 return ret;
11206 }
11207 op = DW_OP_const4s;
11208 }
11209 else
11210 {
11211 if (size_of_int_loc_descriptor (i)
11212 < (unsigned long) 1 + size_of_sleb128 (i))
11213 {
11214 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11215 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11216 return ret;
11217 }
11218 op = DW_OP_consts;
11219 }
11220 }
11221
11222 return new_loc_descr (op, i, 0);
11223 }
11224
11225 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11226 without actually allocating it. */
11227
11228 static unsigned long
11229 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11230 {
11231 return size_of_int_loc_descriptor (i >> shift)
11232 + size_of_int_loc_descriptor (shift)
11233 + 1;
11234 }
11235
11236 /* Return size_of_locs (int_loc_descriptor (i)) without
11237 actually allocating it. */
11238
11239 static unsigned long
11240 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11241 {
11242 unsigned long s;
11243
11244 if (i >= 0)
11245 {
11246 int clz, ctz;
11247 if (i <= 31)
11248 return 1;
11249 else if (i <= 0xff)
11250 return 2;
11251 else if (i <= 0xffff)
11252 return 3;
11253 clz = clz_hwi (i);
11254 ctz = ctz_hwi (i);
11255 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11256 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11257 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11258 - clz - 5);
11259 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11260 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11261 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11262 - clz - 8);
11263 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11264 return 5;
11265 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11266 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11267 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11268 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11269 - clz - 8);
11270 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11271 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11272 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11273 - clz - 16);
11274 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11275 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11276 && s > 6)
11277 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11278 - clz - 32);
11279 else
11280 return 1 + s;
11281 }
11282 else
11283 {
11284 if (i >= -0x80)
11285 return 2;
11286 else if (i >= -0x8000)
11287 return 3;
11288 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11289 {
11290 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11291 {
11292 s = size_of_int_loc_descriptor (-i) + 1;
11293 if (s < 5)
11294 return s;
11295 }
11296 return 5;
11297 }
11298 else
11299 {
11300 unsigned long r = 1 + size_of_sleb128 (i);
11301 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11302 {
11303 s = size_of_int_loc_descriptor (-i) + 1;
11304 if (s < r)
11305 return s;
11306 }
11307 return r;
11308 }
11309 }
11310 }
11311
11312 /* Return loc description representing "address" of integer value.
11313 This can appear only as toplevel expression. */
11314
11315 static dw_loc_descr_ref
11316 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11317 {
11318 int litsize;
11319 dw_loc_descr_ref loc_result = NULL;
11320
11321 if (!(dwarf_version >= 4 || !dwarf_strict))
11322 return NULL;
11323
11324 litsize = size_of_int_loc_descriptor (i);
11325 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11326 is more compact. For DW_OP_stack_value we need:
11327 litsize + 1 (DW_OP_stack_value)
11328 and for DW_OP_implicit_value:
11329 1 (DW_OP_implicit_value) + 1 (length) + size. */
11330 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11331 {
11332 loc_result = int_loc_descriptor (i);
11333 add_loc_descr (&loc_result,
11334 new_loc_descr (DW_OP_stack_value, 0, 0));
11335 return loc_result;
11336 }
11337
11338 loc_result = new_loc_descr (DW_OP_implicit_value,
11339 size, 0);
11340 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11341 loc_result->dw_loc_oprnd2.v.val_int = i;
11342 return loc_result;
11343 }
11344
11345 /* Return a location descriptor that designates a base+offset location. */
11346
11347 static dw_loc_descr_ref
11348 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11349 enum var_init_status initialized)
11350 {
11351 unsigned int regno;
11352 dw_loc_descr_ref result;
11353 dw_fde_ref fde = cfun->fde;
11354
11355 /* We only use "frame base" when we're sure we're talking about the
11356 post-prologue local stack frame. We do this by *not* running
11357 register elimination until this point, and recognizing the special
11358 argument pointer and soft frame pointer rtx's. */
11359 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11360 {
11361 rtx elim = (ira_use_lra_p
11362 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11363 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11364
11365 if (elim != reg)
11366 {
11367 if (GET_CODE (elim) == PLUS)
11368 {
11369 offset += INTVAL (XEXP (elim, 1));
11370 elim = XEXP (elim, 0);
11371 }
11372 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11373 && (elim == hard_frame_pointer_rtx
11374 || elim == stack_pointer_rtx))
11375 || elim == (frame_pointer_needed
11376 ? hard_frame_pointer_rtx
11377 : stack_pointer_rtx));
11378
11379 /* If drap register is used to align stack, use frame
11380 pointer + offset to access stack variables. If stack
11381 is aligned without drap, use stack pointer + offset to
11382 access stack variables. */
11383 if (crtl->stack_realign_tried
11384 && reg == frame_pointer_rtx)
11385 {
11386 int base_reg
11387 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11388 ? HARD_FRAME_POINTER_REGNUM
11389 : REGNO (elim));
11390 return new_reg_loc_descr (base_reg, offset);
11391 }
11392
11393 gcc_assert (frame_pointer_fb_offset_valid);
11394 offset += frame_pointer_fb_offset;
11395 return new_loc_descr (DW_OP_fbreg, offset, 0);
11396 }
11397 }
11398
11399 regno = REGNO (reg);
11400 #ifdef LEAF_REG_REMAP
11401 if (crtl->uses_only_leaf_regs)
11402 {
11403 int leaf_reg = LEAF_REG_REMAP (regno);
11404 if (leaf_reg != -1)
11405 regno = (unsigned) leaf_reg;
11406 }
11407 #endif
11408 regno = DWARF_FRAME_REGNUM (regno);
11409
11410 if (!optimize && fde
11411 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11412 {
11413 /* Use cfa+offset to represent the location of arguments passed
11414 on the stack when drap is used to align stack.
11415 Only do this when not optimizing, for optimized code var-tracking
11416 is supposed to track where the arguments live and the register
11417 used as vdrap or drap in some spot might be used for something
11418 else in other part of the routine. */
11419 return new_loc_descr (DW_OP_fbreg, offset, 0);
11420 }
11421
11422 if (regno <= 31)
11423 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11424 offset, 0);
11425 else
11426 result = new_loc_descr (DW_OP_bregx, regno, offset);
11427
11428 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11429 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11430
11431 return result;
11432 }
11433
11434 /* Return true if this RTL expression describes a base+offset calculation. */
11435
11436 static inline int
11437 is_based_loc (const_rtx rtl)
11438 {
11439 return (GET_CODE (rtl) == PLUS
11440 && ((REG_P (XEXP (rtl, 0))
11441 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11442 && CONST_INT_P (XEXP (rtl, 1)))));
11443 }
11444
11445 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11446 failed. */
11447
11448 static dw_loc_descr_ref
11449 tls_mem_loc_descriptor (rtx mem)
11450 {
11451 tree base;
11452 dw_loc_descr_ref loc_result;
11453
11454 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11455 return NULL;
11456
11457 base = get_base_address (MEM_EXPR (mem));
11458 if (base == NULL
11459 || TREE_CODE (base) != VAR_DECL
11460 || !DECL_THREAD_LOCAL_P (base))
11461 return NULL;
11462
11463 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11464 if (loc_result == NULL)
11465 return NULL;
11466
11467 if (MEM_OFFSET (mem))
11468 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11469
11470 return loc_result;
11471 }
11472
11473 /* Output debug info about reason why we failed to expand expression as dwarf
11474 expression. */
11475
11476 static void
11477 expansion_failed (tree expr, rtx rtl, char const *reason)
11478 {
11479 if (dump_file && (dump_flags & TDF_DETAILS))
11480 {
11481 fprintf (dump_file, "Failed to expand as dwarf: ");
11482 if (expr)
11483 print_generic_expr (dump_file, expr, dump_flags);
11484 if (rtl)
11485 {
11486 fprintf (dump_file, "\n");
11487 print_rtl (dump_file, rtl);
11488 }
11489 fprintf (dump_file, "\nReason: %s\n", reason);
11490 }
11491 }
11492
11493 /* Helper function for const_ok_for_output. */
11494
11495 static bool
11496 const_ok_for_output_1 (rtx rtl)
11497 {
11498 if (GET_CODE (rtl) == UNSPEC)
11499 {
11500 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11501 we can't express it in the debug info. */
11502 #ifdef ENABLE_CHECKING
11503 /* Don't complain about TLS UNSPECs, those are just too hard to
11504 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11505 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11506 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11507 if (XVECLEN (rtl, 0) == 0
11508 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11509 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11510 inform (current_function_decl
11511 ? DECL_SOURCE_LOCATION (current_function_decl)
11512 : UNKNOWN_LOCATION,
11513 #if NUM_UNSPEC_VALUES > 0
11514 "non-delegitimized UNSPEC %s (%d) found in variable location",
11515 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11516 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11517 XINT (rtl, 1));
11518 #else
11519 "non-delegitimized UNSPEC %d found in variable location",
11520 XINT (rtl, 1));
11521 #endif
11522 #endif
11523 expansion_failed (NULL_TREE, rtl,
11524 "UNSPEC hasn't been delegitimized.\n");
11525 return false;
11526 }
11527
11528 if (targetm.const_not_ok_for_debug_p (rtl))
11529 {
11530 expansion_failed (NULL_TREE, rtl,
11531 "Expression rejected for debug by the backend.\n");
11532 return false;
11533 }
11534
11535 /* FIXME: Refer to PR60655. It is possible for simplification
11536 of rtl expressions in var tracking to produce such expressions.
11537 We should really identify / validate expressions
11538 enclosed in CONST that can be handled by assemblers on various
11539 targets and only handle legitimate cases here. */
11540 if (GET_CODE (rtl) != SYMBOL_REF)
11541 {
11542 if (GET_CODE (rtl) == NOT)
11543 return false;
11544 return true;
11545 }
11546
11547 if (CONSTANT_POOL_ADDRESS_P (rtl))
11548 {
11549 bool marked;
11550 get_pool_constant_mark (rtl, &marked);
11551 /* If all references to this pool constant were optimized away,
11552 it was not output and thus we can't represent it. */
11553 if (!marked)
11554 {
11555 expansion_failed (NULL_TREE, rtl,
11556 "Constant was removed from constant pool.\n");
11557 return false;
11558 }
11559 }
11560
11561 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11562 return false;
11563
11564 /* Avoid references to external symbols in debug info, on several targets
11565 the linker might even refuse to link when linking a shared library,
11566 and in many other cases the relocations for .debug_info/.debug_loc are
11567 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11568 to be defined within the same shared library or executable are fine. */
11569 if (SYMBOL_REF_EXTERNAL_P (rtl))
11570 {
11571 tree decl = SYMBOL_REF_DECL (rtl);
11572
11573 if (decl == NULL || !targetm.binds_local_p (decl))
11574 {
11575 expansion_failed (NULL_TREE, rtl,
11576 "Symbol not defined in current TU.\n");
11577 return false;
11578 }
11579 }
11580
11581 return true;
11582 }
11583
11584 /* Return true if constant RTL can be emitted in DW_OP_addr or
11585 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11586 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11587
11588 static bool
11589 const_ok_for_output (rtx rtl)
11590 {
11591 if (GET_CODE (rtl) == SYMBOL_REF)
11592 return const_ok_for_output_1 (rtl);
11593
11594 if (GET_CODE (rtl) == CONST)
11595 {
11596 subrtx_var_iterator::array_type array;
11597 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11598 if (!const_ok_for_output_1 (*iter))
11599 return false;
11600 return true;
11601 }
11602
11603 return true;
11604 }
11605
11606 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11607 if possible, NULL otherwise. */
11608
11609 static dw_die_ref
11610 base_type_for_mode (machine_mode mode, bool unsignedp)
11611 {
11612 dw_die_ref type_die;
11613 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11614
11615 if (type == NULL)
11616 return NULL;
11617 switch (TREE_CODE (type))
11618 {
11619 case INTEGER_TYPE:
11620 case REAL_TYPE:
11621 break;
11622 default:
11623 return NULL;
11624 }
11625 type_die = lookup_type_die (type);
11626 if (!type_die)
11627 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11628 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11629 return NULL;
11630 return type_die;
11631 }
11632
11633 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11634 type matching MODE, or, if MODE is narrower than or as wide as
11635 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11636 possible. */
11637
11638 static dw_loc_descr_ref
11639 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11640 {
11641 machine_mode outer_mode = mode;
11642 dw_die_ref type_die;
11643 dw_loc_descr_ref cvt;
11644
11645 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11646 {
11647 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11648 return op;
11649 }
11650 type_die = base_type_for_mode (outer_mode, 1);
11651 if (type_die == NULL)
11652 return NULL;
11653 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11654 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11655 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11656 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11657 add_loc_descr (&op, cvt);
11658 return op;
11659 }
11660
11661 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11662
11663 static dw_loc_descr_ref
11664 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11665 dw_loc_descr_ref op1)
11666 {
11667 dw_loc_descr_ref ret = op0;
11668 add_loc_descr (&ret, op1);
11669 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11670 if (STORE_FLAG_VALUE != 1)
11671 {
11672 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11673 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11674 }
11675 return ret;
11676 }
11677
11678 /* Return location descriptor for signed comparison OP RTL. */
11679
11680 static dw_loc_descr_ref
11681 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11682 machine_mode mem_mode)
11683 {
11684 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11685 dw_loc_descr_ref op0, op1;
11686 int shift;
11687
11688 if (op_mode == VOIDmode)
11689 op_mode = GET_MODE (XEXP (rtl, 1));
11690 if (op_mode == VOIDmode)
11691 return NULL;
11692
11693 if (dwarf_strict
11694 && (GET_MODE_CLASS (op_mode) != MODE_INT
11695 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11696 return NULL;
11697
11698 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11699 VAR_INIT_STATUS_INITIALIZED);
11700 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11701 VAR_INIT_STATUS_INITIALIZED);
11702
11703 if (op0 == NULL || op1 == NULL)
11704 return NULL;
11705
11706 if (GET_MODE_CLASS (op_mode) != MODE_INT
11707 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11708 return compare_loc_descriptor (op, op0, op1);
11709
11710 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11711 {
11712 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11713 dw_loc_descr_ref cvt;
11714
11715 if (type_die == NULL)
11716 return NULL;
11717 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11718 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11719 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11720 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11721 add_loc_descr (&op0, cvt);
11722 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11723 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11724 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11725 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11726 add_loc_descr (&op1, cvt);
11727 return compare_loc_descriptor (op, op0, op1);
11728 }
11729
11730 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11731 /* For eq/ne, if the operands are known to be zero-extended,
11732 there is no need to do the fancy shifting up. */
11733 if (op == DW_OP_eq || op == DW_OP_ne)
11734 {
11735 dw_loc_descr_ref last0, last1;
11736 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11737 ;
11738 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11739 ;
11740 /* deref_size zero extends, and for constants we can check
11741 whether they are zero extended or not. */
11742 if (((last0->dw_loc_opc == DW_OP_deref_size
11743 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11744 || (CONST_INT_P (XEXP (rtl, 0))
11745 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11746 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11747 && ((last1->dw_loc_opc == DW_OP_deref_size
11748 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11749 || (CONST_INT_P (XEXP (rtl, 1))
11750 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11751 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11752 return compare_loc_descriptor (op, op0, op1);
11753
11754 /* EQ/NE comparison against constant in narrower type than
11755 DWARF2_ADDR_SIZE can be performed either as
11756 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11757 DW_OP_{eq,ne}
11758 or
11759 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11760 DW_OP_{eq,ne}. Pick whatever is shorter. */
11761 if (CONST_INT_P (XEXP (rtl, 1))
11762 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11763 && (size_of_int_loc_descriptor (shift) + 1
11764 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11765 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11766 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11767 & GET_MODE_MASK (op_mode))))
11768 {
11769 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11770 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11771 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11772 & GET_MODE_MASK (op_mode));
11773 return compare_loc_descriptor (op, op0, op1);
11774 }
11775 }
11776 add_loc_descr (&op0, int_loc_descriptor (shift));
11777 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11778 if (CONST_INT_P (XEXP (rtl, 1)))
11779 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11780 else
11781 {
11782 add_loc_descr (&op1, int_loc_descriptor (shift));
11783 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11784 }
11785 return compare_loc_descriptor (op, op0, op1);
11786 }
11787
11788 /* Return location descriptor for unsigned comparison OP RTL. */
11789
11790 static dw_loc_descr_ref
11791 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11792 machine_mode mem_mode)
11793 {
11794 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11795 dw_loc_descr_ref op0, op1;
11796
11797 if (op_mode == VOIDmode)
11798 op_mode = GET_MODE (XEXP (rtl, 1));
11799 if (op_mode == VOIDmode)
11800 return NULL;
11801 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11802 return NULL;
11803
11804 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11805 return NULL;
11806
11807 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11808 VAR_INIT_STATUS_INITIALIZED);
11809 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11810 VAR_INIT_STATUS_INITIALIZED);
11811
11812 if (op0 == NULL || op1 == NULL)
11813 return NULL;
11814
11815 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11816 {
11817 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11818 dw_loc_descr_ref last0, last1;
11819 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11820 ;
11821 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11822 ;
11823 if (CONST_INT_P (XEXP (rtl, 0)))
11824 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11825 /* deref_size zero extends, so no need to mask it again. */
11826 else if (last0->dw_loc_opc != DW_OP_deref_size
11827 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11828 {
11829 add_loc_descr (&op0, int_loc_descriptor (mask));
11830 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11831 }
11832 if (CONST_INT_P (XEXP (rtl, 1)))
11833 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11834 /* deref_size zero extends, so no need to mask it again. */
11835 else if (last1->dw_loc_opc != DW_OP_deref_size
11836 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11837 {
11838 add_loc_descr (&op1, int_loc_descriptor (mask));
11839 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11840 }
11841 }
11842 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11843 {
11844 HOST_WIDE_INT bias = 1;
11845 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11846 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11847 if (CONST_INT_P (XEXP (rtl, 1)))
11848 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11849 + INTVAL (XEXP (rtl, 1)));
11850 else
11851 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11852 bias, 0));
11853 }
11854 return compare_loc_descriptor (op, op0, op1);
11855 }
11856
11857 /* Return location descriptor for {U,S}{MIN,MAX}. */
11858
11859 static dw_loc_descr_ref
11860 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11861 machine_mode mem_mode)
11862 {
11863 enum dwarf_location_atom op;
11864 dw_loc_descr_ref op0, op1, ret;
11865 dw_loc_descr_ref bra_node, drop_node;
11866
11867 if (dwarf_strict
11868 && (GET_MODE_CLASS (mode) != MODE_INT
11869 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11870 return NULL;
11871
11872 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11873 VAR_INIT_STATUS_INITIALIZED);
11874 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11875 VAR_INIT_STATUS_INITIALIZED);
11876
11877 if (op0 == NULL || op1 == NULL)
11878 return NULL;
11879
11880 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11881 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11882 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11883 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11884 {
11885 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11886 {
11887 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11888 add_loc_descr (&op0, int_loc_descriptor (mask));
11889 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11890 add_loc_descr (&op1, int_loc_descriptor (mask));
11891 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11892 }
11893 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11894 {
11895 HOST_WIDE_INT bias = 1;
11896 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11897 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11898 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11899 }
11900 }
11901 else if (GET_MODE_CLASS (mode) == MODE_INT
11902 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11903 {
11904 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11905 add_loc_descr (&op0, int_loc_descriptor (shift));
11906 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11907 add_loc_descr (&op1, int_loc_descriptor (shift));
11908 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11909 }
11910 else if (GET_MODE_CLASS (mode) == MODE_INT
11911 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11912 {
11913 dw_die_ref type_die = base_type_for_mode (mode, 0);
11914 dw_loc_descr_ref cvt;
11915 if (type_die == NULL)
11916 return NULL;
11917 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11918 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11919 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11920 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11921 add_loc_descr (&op0, cvt);
11922 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11923 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11924 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11925 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11926 add_loc_descr (&op1, cvt);
11927 }
11928
11929 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11930 op = DW_OP_lt;
11931 else
11932 op = DW_OP_gt;
11933 ret = op0;
11934 add_loc_descr (&ret, op1);
11935 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11936 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11937 add_loc_descr (&ret, bra_node);
11938 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11939 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11940 add_loc_descr (&ret, drop_node);
11941 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11942 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11943 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11944 && GET_MODE_CLASS (mode) == MODE_INT
11945 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11946 ret = convert_descriptor_to_mode (mode, ret);
11947 return ret;
11948 }
11949
11950 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11951 but after converting arguments to type_die, afterwards
11952 convert back to unsigned. */
11953
11954 static dw_loc_descr_ref
11955 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11956 machine_mode mode, machine_mode mem_mode)
11957 {
11958 dw_loc_descr_ref cvt, op0, op1;
11959
11960 if (type_die == NULL)
11961 return NULL;
11962 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11963 VAR_INIT_STATUS_INITIALIZED);
11964 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11965 VAR_INIT_STATUS_INITIALIZED);
11966 if (op0 == NULL || op1 == NULL)
11967 return NULL;
11968 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11969 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11970 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11971 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11972 add_loc_descr (&op0, cvt);
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 (&op1, cvt);
11978 add_loc_descr (&op0, op1);
11979 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11980 return convert_descriptor_to_mode (mode, op0);
11981 }
11982
11983 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11984 const0 is DW_OP_lit0 or corresponding typed constant,
11985 const1 is DW_OP_lit1 or corresponding typed constant
11986 and constMSB is constant with just the MSB bit set
11987 for the mode):
11988 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11989 L1: const0 DW_OP_swap
11990 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11991 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11992 L3: DW_OP_drop
11993 L4: DW_OP_nop
11994
11995 CTZ is similar:
11996 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11997 L1: const0 DW_OP_swap
11998 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11999 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12000 L3: DW_OP_drop
12001 L4: DW_OP_nop
12002
12003 FFS is similar:
12004 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12005 L1: const1 DW_OP_swap
12006 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12007 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12008 L3: DW_OP_drop
12009 L4: DW_OP_nop */
12010
12011 static dw_loc_descr_ref
12012 clz_loc_descriptor (rtx rtl, machine_mode mode,
12013 machine_mode mem_mode)
12014 {
12015 dw_loc_descr_ref op0, ret, tmp;
12016 HOST_WIDE_INT valv;
12017 dw_loc_descr_ref l1jump, l1label;
12018 dw_loc_descr_ref l2jump, l2label;
12019 dw_loc_descr_ref l3jump, l3label;
12020 dw_loc_descr_ref l4jump, l4label;
12021 rtx msb;
12022
12023 if (GET_MODE_CLASS (mode) != MODE_INT
12024 || GET_MODE (XEXP (rtl, 0)) != mode)
12025 return NULL;
12026
12027 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12028 VAR_INIT_STATUS_INITIALIZED);
12029 if (op0 == NULL)
12030 return NULL;
12031 ret = op0;
12032 if (GET_CODE (rtl) == CLZ)
12033 {
12034 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12035 valv = GET_MODE_BITSIZE (mode);
12036 }
12037 else if (GET_CODE (rtl) == FFS)
12038 valv = 0;
12039 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12040 valv = GET_MODE_BITSIZE (mode);
12041 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12042 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12043 add_loc_descr (&ret, l1jump);
12044 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12045 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12046 VAR_INIT_STATUS_INITIALIZED);
12047 if (tmp == NULL)
12048 return NULL;
12049 add_loc_descr (&ret, tmp);
12050 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12051 add_loc_descr (&ret, l4jump);
12052 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12053 ? const1_rtx : const0_rtx,
12054 mode, mem_mode,
12055 VAR_INIT_STATUS_INITIALIZED);
12056 if (l1label == NULL)
12057 return NULL;
12058 add_loc_descr (&ret, l1label);
12059 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12060 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12061 add_loc_descr (&ret, l2label);
12062 if (GET_CODE (rtl) != CLZ)
12063 msb = const1_rtx;
12064 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12065 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12066 << (GET_MODE_BITSIZE (mode) - 1));
12067 else
12068 msb = immed_wide_int_const
12069 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12070 GET_MODE_PRECISION (mode)), mode);
12071 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12072 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12073 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12074 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12075 else
12076 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12077 VAR_INIT_STATUS_INITIALIZED);
12078 if (tmp == NULL)
12079 return NULL;
12080 add_loc_descr (&ret, tmp);
12081 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12082 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12083 add_loc_descr (&ret, l3jump);
12084 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12085 VAR_INIT_STATUS_INITIALIZED);
12086 if (tmp == NULL)
12087 return NULL;
12088 add_loc_descr (&ret, tmp);
12089 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12090 ? DW_OP_shl : DW_OP_shr, 0, 0));
12091 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12092 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12093 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12094 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12095 add_loc_descr (&ret, l2jump);
12096 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12097 add_loc_descr (&ret, l3label);
12098 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12099 add_loc_descr (&ret, l4label);
12100 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12101 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12102 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12103 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12104 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12105 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12106 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12107 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12108 return ret;
12109 }
12110
12111 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12112 const1 is DW_OP_lit1 or corresponding typed constant):
12113 const0 DW_OP_swap
12114 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12115 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12116 L2: DW_OP_drop
12117
12118 PARITY is similar:
12119 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12120 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12121 L2: DW_OP_drop */
12122
12123 static dw_loc_descr_ref
12124 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12125 machine_mode mem_mode)
12126 {
12127 dw_loc_descr_ref op0, ret, tmp;
12128 dw_loc_descr_ref l1jump, l1label;
12129 dw_loc_descr_ref l2jump, l2label;
12130
12131 if (GET_MODE_CLASS (mode) != MODE_INT
12132 || GET_MODE (XEXP (rtl, 0)) != mode)
12133 return NULL;
12134
12135 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12136 VAR_INIT_STATUS_INITIALIZED);
12137 if (op0 == NULL)
12138 return NULL;
12139 ret = op0;
12140 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12141 VAR_INIT_STATUS_INITIALIZED);
12142 if (tmp == NULL)
12143 return NULL;
12144 add_loc_descr (&ret, tmp);
12145 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12146 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12147 add_loc_descr (&ret, l1label);
12148 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12149 add_loc_descr (&ret, l2jump);
12150 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12151 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12152 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12153 VAR_INIT_STATUS_INITIALIZED);
12154 if (tmp == NULL)
12155 return NULL;
12156 add_loc_descr (&ret, tmp);
12157 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12158 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12159 ? DW_OP_plus : DW_OP_xor, 0, 0));
12160 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12161 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12162 VAR_INIT_STATUS_INITIALIZED);
12163 add_loc_descr (&ret, tmp);
12164 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12165 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12166 add_loc_descr (&ret, l1jump);
12167 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12168 add_loc_descr (&ret, l2label);
12169 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12170 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12171 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12172 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12173 return ret;
12174 }
12175
12176 /* BSWAP (constS is initial shift count, either 56 or 24):
12177 constS const0
12178 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12179 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12180 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12181 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12182 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12183
12184 static dw_loc_descr_ref
12185 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12186 machine_mode mem_mode)
12187 {
12188 dw_loc_descr_ref op0, ret, tmp;
12189 dw_loc_descr_ref l1jump, l1label;
12190 dw_loc_descr_ref l2jump, l2label;
12191
12192 if (GET_MODE_CLASS (mode) != MODE_INT
12193 || BITS_PER_UNIT != 8
12194 || (GET_MODE_BITSIZE (mode) != 32
12195 && GET_MODE_BITSIZE (mode) != 64))
12196 return NULL;
12197
12198 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12199 VAR_INIT_STATUS_INITIALIZED);
12200 if (op0 == NULL)
12201 return NULL;
12202
12203 ret = op0;
12204 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12205 mode, mem_mode,
12206 VAR_INIT_STATUS_INITIALIZED);
12207 if (tmp == NULL)
12208 return NULL;
12209 add_loc_descr (&ret, tmp);
12210 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12211 VAR_INIT_STATUS_INITIALIZED);
12212 if (tmp == NULL)
12213 return NULL;
12214 add_loc_descr (&ret, tmp);
12215 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12216 add_loc_descr (&ret, l1label);
12217 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12218 mode, mem_mode,
12219 VAR_INIT_STATUS_INITIALIZED);
12220 add_loc_descr (&ret, tmp);
12221 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12222 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12223 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12224 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12225 VAR_INIT_STATUS_INITIALIZED);
12226 if (tmp == NULL)
12227 return NULL;
12228 add_loc_descr (&ret, tmp);
12229 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12230 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12231 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12232 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12233 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12234 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12235 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12236 VAR_INIT_STATUS_INITIALIZED);
12237 add_loc_descr (&ret, tmp);
12238 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12239 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12240 add_loc_descr (&ret, l2jump);
12241 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12242 VAR_INIT_STATUS_INITIALIZED);
12243 add_loc_descr (&ret, tmp);
12244 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12245 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12246 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12247 add_loc_descr (&ret, l1jump);
12248 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12249 add_loc_descr (&ret, l2label);
12250 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12251 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12252 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12253 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12254 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12255 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12256 return ret;
12257 }
12258
12259 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12260 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12261 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12262 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12263
12264 ROTATERT is similar:
12265 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12266 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12267 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12268
12269 static dw_loc_descr_ref
12270 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12271 machine_mode mem_mode)
12272 {
12273 rtx rtlop1 = XEXP (rtl, 1);
12274 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12275 int i;
12276
12277 if (GET_MODE_CLASS (mode) != MODE_INT)
12278 return NULL;
12279
12280 if (GET_MODE (rtlop1) != VOIDmode
12281 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12282 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12283 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12284 VAR_INIT_STATUS_INITIALIZED);
12285 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12286 VAR_INIT_STATUS_INITIALIZED);
12287 if (op0 == NULL || op1 == NULL)
12288 return NULL;
12289 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12290 for (i = 0; i < 2; i++)
12291 {
12292 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12293 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12294 mode, mem_mode,
12295 VAR_INIT_STATUS_INITIALIZED);
12296 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12297 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12298 ? DW_OP_const4u
12299 : HOST_BITS_PER_WIDE_INT == 64
12300 ? DW_OP_const8u : DW_OP_constu,
12301 GET_MODE_MASK (mode), 0);
12302 else
12303 mask[i] = NULL;
12304 if (mask[i] == NULL)
12305 return NULL;
12306 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12307 }
12308 ret = op0;
12309 add_loc_descr (&ret, op1);
12310 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12311 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12312 if (GET_CODE (rtl) == ROTATERT)
12313 {
12314 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12315 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12316 GET_MODE_BITSIZE (mode), 0));
12317 }
12318 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12319 if (mask[0] != NULL)
12320 add_loc_descr (&ret, mask[0]);
12321 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12322 if (mask[1] != NULL)
12323 {
12324 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12325 add_loc_descr (&ret, mask[1]);
12326 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12327 }
12328 if (GET_CODE (rtl) == ROTATE)
12329 {
12330 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12331 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12332 GET_MODE_BITSIZE (mode), 0));
12333 }
12334 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12335 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12336 return ret;
12337 }
12338
12339 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12340 for DEBUG_PARAMETER_REF RTL. */
12341
12342 static dw_loc_descr_ref
12343 parameter_ref_descriptor (rtx rtl)
12344 {
12345 dw_loc_descr_ref ret;
12346 dw_die_ref ref;
12347
12348 if (dwarf_strict)
12349 return NULL;
12350 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12351 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12352 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12353 if (ref)
12354 {
12355 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12356 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12357 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12358 }
12359 else
12360 {
12361 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12362 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12363 }
12364 return ret;
12365 }
12366
12367 /* The following routine converts the RTL for a variable or parameter
12368 (resident in memory) into an equivalent Dwarf representation of a
12369 mechanism for getting the address of that same variable onto the top of a
12370 hypothetical "address evaluation" stack.
12371
12372 When creating memory location descriptors, we are effectively transforming
12373 the RTL for a memory-resident object into its Dwarf postfix expression
12374 equivalent. This routine recursively descends an RTL tree, turning
12375 it into Dwarf postfix code as it goes.
12376
12377 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12378
12379 MEM_MODE is the mode of the memory reference, needed to handle some
12380 autoincrement addressing modes.
12381
12382 Return 0 if we can't represent the location. */
12383
12384 dw_loc_descr_ref
12385 mem_loc_descriptor (rtx rtl, machine_mode mode,
12386 machine_mode mem_mode,
12387 enum var_init_status initialized)
12388 {
12389 dw_loc_descr_ref mem_loc_result = NULL;
12390 enum dwarf_location_atom op;
12391 dw_loc_descr_ref op0, op1;
12392 rtx inner = NULL_RTX;
12393
12394 if (mode == VOIDmode)
12395 mode = GET_MODE (rtl);
12396
12397 /* Note that for a dynamically sized array, the location we will generate a
12398 description of here will be the lowest numbered location which is
12399 actually within the array. That's *not* necessarily the same as the
12400 zeroth element of the array. */
12401
12402 rtl = targetm.delegitimize_address (rtl);
12403
12404 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12405 return NULL;
12406
12407 switch (GET_CODE (rtl))
12408 {
12409 case POST_INC:
12410 case POST_DEC:
12411 case POST_MODIFY:
12412 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12413
12414 case SUBREG:
12415 /* The case of a subreg may arise when we have a local (register)
12416 variable or a formal (register) parameter which doesn't quite fill
12417 up an entire register. For now, just assume that it is
12418 legitimate to make the Dwarf info refer to the whole register which
12419 contains the given subreg. */
12420 if (!subreg_lowpart_p (rtl))
12421 break;
12422 inner = SUBREG_REG (rtl);
12423 case TRUNCATE:
12424 if (inner == NULL_RTX)
12425 inner = XEXP (rtl, 0);
12426 if (GET_MODE_CLASS (mode) == MODE_INT
12427 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12428 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12429 #ifdef POINTERS_EXTEND_UNSIGNED
12430 || (mode == Pmode && mem_mode != VOIDmode)
12431 #endif
12432 )
12433 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12434 {
12435 mem_loc_result = mem_loc_descriptor (inner,
12436 GET_MODE (inner),
12437 mem_mode, initialized);
12438 break;
12439 }
12440 if (dwarf_strict)
12441 break;
12442 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12443 break;
12444 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12445 && (GET_MODE_CLASS (mode) != MODE_INT
12446 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12447 break;
12448 else
12449 {
12450 dw_die_ref type_die;
12451 dw_loc_descr_ref cvt;
12452
12453 mem_loc_result = mem_loc_descriptor (inner,
12454 GET_MODE (inner),
12455 mem_mode, initialized);
12456 if (mem_loc_result == NULL)
12457 break;
12458 type_die = base_type_for_mode (mode,
12459 GET_MODE_CLASS (mode) == MODE_INT);
12460 if (type_die == NULL)
12461 {
12462 mem_loc_result = NULL;
12463 break;
12464 }
12465 if (GET_MODE_SIZE (mode)
12466 != GET_MODE_SIZE (GET_MODE (inner)))
12467 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12468 else
12469 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12470 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12471 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12472 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12473 add_loc_descr (&mem_loc_result, cvt);
12474 }
12475 break;
12476
12477 case REG:
12478 if (GET_MODE_CLASS (mode) != MODE_INT
12479 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12480 && rtl != arg_pointer_rtx
12481 && rtl != frame_pointer_rtx
12482 #ifdef POINTERS_EXTEND_UNSIGNED
12483 && (mode != Pmode || mem_mode == VOIDmode)
12484 #endif
12485 ))
12486 {
12487 dw_die_ref type_die;
12488 unsigned int dbx_regnum;
12489
12490 if (dwarf_strict)
12491 break;
12492 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12493 break;
12494 type_die = base_type_for_mode (mode,
12495 GET_MODE_CLASS (mode) == MODE_INT);
12496 if (type_die == NULL)
12497 break;
12498
12499 dbx_regnum = dbx_reg_number (rtl);
12500 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12501 break;
12502 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12503 dbx_regnum, 0);
12504 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12505 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12506 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12507 break;
12508 }
12509 /* Whenever a register number forms a part of the description of the
12510 method for calculating the (dynamic) address of a memory resident
12511 object, DWARF rules require the register number be referred to as
12512 a "base register". This distinction is not based in any way upon
12513 what category of register the hardware believes the given register
12514 belongs to. This is strictly DWARF terminology we're dealing with
12515 here. Note that in cases where the location of a memory-resident
12516 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12517 OP_CONST (0)) the actual DWARF location descriptor that we generate
12518 may just be OP_BASEREG (basereg). This may look deceptively like
12519 the object in question was allocated to a register (rather than in
12520 memory) so DWARF consumers need to be aware of the subtle
12521 distinction between OP_REG and OP_BASEREG. */
12522 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12523 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12524 else if (stack_realign_drap
12525 && crtl->drap_reg
12526 && crtl->args.internal_arg_pointer == rtl
12527 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12528 {
12529 /* If RTL is internal_arg_pointer, which has been optimized
12530 out, use DRAP instead. */
12531 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12532 VAR_INIT_STATUS_INITIALIZED);
12533 }
12534 break;
12535
12536 case SIGN_EXTEND:
12537 case ZERO_EXTEND:
12538 if (GET_MODE_CLASS (mode) != MODE_INT)
12539 break;
12540 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12541 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12542 if (op0 == 0)
12543 break;
12544 else if (GET_CODE (rtl) == ZERO_EXTEND
12545 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12546 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12547 < HOST_BITS_PER_WIDE_INT
12548 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12549 to expand zero extend as two shifts instead of
12550 masking. */
12551 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12552 {
12553 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12554 mem_loc_result = op0;
12555 add_loc_descr (&mem_loc_result,
12556 int_loc_descriptor (GET_MODE_MASK (imode)));
12557 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12558 }
12559 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12560 {
12561 int shift = DWARF2_ADDR_SIZE
12562 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12563 shift *= BITS_PER_UNIT;
12564 if (GET_CODE (rtl) == SIGN_EXTEND)
12565 op = DW_OP_shra;
12566 else
12567 op = DW_OP_shr;
12568 mem_loc_result = op0;
12569 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12570 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12571 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12572 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12573 }
12574 else if (!dwarf_strict)
12575 {
12576 dw_die_ref type_die1, type_die2;
12577 dw_loc_descr_ref cvt;
12578
12579 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12580 GET_CODE (rtl) == ZERO_EXTEND);
12581 if (type_die1 == NULL)
12582 break;
12583 type_die2 = base_type_for_mode (mode, 1);
12584 if (type_die2 == NULL)
12585 break;
12586 mem_loc_result = op0;
12587 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12588 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12589 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12590 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12591 add_loc_descr (&mem_loc_result, cvt);
12592 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12593 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12594 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12595 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12596 add_loc_descr (&mem_loc_result, cvt);
12597 }
12598 break;
12599
12600 case MEM:
12601 {
12602 rtx new_rtl = avoid_constant_pool_reference (rtl);
12603 if (new_rtl != rtl)
12604 {
12605 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12606 initialized);
12607 if (mem_loc_result != NULL)
12608 return mem_loc_result;
12609 }
12610 }
12611 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12612 get_address_mode (rtl), mode,
12613 VAR_INIT_STATUS_INITIALIZED);
12614 if (mem_loc_result == NULL)
12615 mem_loc_result = tls_mem_loc_descriptor (rtl);
12616 if (mem_loc_result != NULL)
12617 {
12618 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12619 || GET_MODE_CLASS (mode) != MODE_INT)
12620 {
12621 dw_die_ref type_die;
12622 dw_loc_descr_ref deref;
12623
12624 if (dwarf_strict)
12625 return NULL;
12626 type_die
12627 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12628 if (type_die == NULL)
12629 return NULL;
12630 deref = new_loc_descr (DW_OP_GNU_deref_type,
12631 GET_MODE_SIZE (mode), 0);
12632 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12633 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12634 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12635 add_loc_descr (&mem_loc_result, deref);
12636 }
12637 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12638 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12639 else
12640 add_loc_descr (&mem_loc_result,
12641 new_loc_descr (DW_OP_deref_size,
12642 GET_MODE_SIZE (mode), 0));
12643 }
12644 break;
12645
12646 case LO_SUM:
12647 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12648
12649 case LABEL_REF:
12650 /* Some ports can transform a symbol ref into a label ref, because
12651 the symbol ref is too far away and has to be dumped into a constant
12652 pool. */
12653 case CONST:
12654 case SYMBOL_REF:
12655 if ((GET_MODE_CLASS (mode) != MODE_INT
12656 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12657 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12658 #ifdef POINTERS_EXTEND_UNSIGNED
12659 && (mode != Pmode || mem_mode == VOIDmode)
12660 #endif
12661 ))
12662 break;
12663 if (GET_CODE (rtl) == SYMBOL_REF
12664 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12665 {
12666 dw_loc_descr_ref temp;
12667
12668 /* If this is not defined, we have no way to emit the data. */
12669 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12670 break;
12671
12672 temp = new_addr_loc_descr (rtl, dtprel_true);
12673
12674 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12675 add_loc_descr (&mem_loc_result, temp);
12676
12677 break;
12678 }
12679
12680 if (!const_ok_for_output (rtl))
12681 break;
12682
12683 symref:
12684 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12685 vec_safe_push (used_rtx_array, rtl);
12686 break;
12687
12688 case CONCAT:
12689 case CONCATN:
12690 case VAR_LOCATION:
12691 case DEBUG_IMPLICIT_PTR:
12692 expansion_failed (NULL_TREE, rtl,
12693 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12694 return 0;
12695
12696 case ENTRY_VALUE:
12697 if (dwarf_strict)
12698 return NULL;
12699 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12700 {
12701 if (GET_MODE_CLASS (mode) != MODE_INT
12702 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12703 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12704 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12705 else
12706 {
12707 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12708 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12709 return NULL;
12710 op0 = one_reg_loc_descriptor (dbx_regnum,
12711 VAR_INIT_STATUS_INITIALIZED);
12712 }
12713 }
12714 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12715 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12716 {
12717 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12718 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12719 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12720 return NULL;
12721 }
12722 else
12723 gcc_unreachable ();
12724 if (op0 == NULL)
12725 return NULL;
12726 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12727 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12728 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12729 break;
12730
12731 case DEBUG_PARAMETER_REF:
12732 mem_loc_result = parameter_ref_descriptor (rtl);
12733 break;
12734
12735 case PRE_MODIFY:
12736 /* Extract the PLUS expression nested inside and fall into
12737 PLUS code below. */
12738 rtl = XEXP (rtl, 1);
12739 goto plus;
12740
12741 case PRE_INC:
12742 case PRE_DEC:
12743 /* Turn these into a PLUS expression and fall into the PLUS code
12744 below. */
12745 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12746 gen_int_mode (GET_CODE (rtl) == PRE_INC
12747 ? GET_MODE_UNIT_SIZE (mem_mode)
12748 : -GET_MODE_UNIT_SIZE (mem_mode),
12749 mode));
12750
12751 /* ... fall through ... */
12752
12753 case PLUS:
12754 plus:
12755 if (is_based_loc (rtl)
12756 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12757 || XEXP (rtl, 0) == arg_pointer_rtx
12758 || XEXP (rtl, 0) == frame_pointer_rtx)
12759 && GET_MODE_CLASS (mode) == MODE_INT)
12760 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12761 INTVAL (XEXP (rtl, 1)),
12762 VAR_INIT_STATUS_INITIALIZED);
12763 else
12764 {
12765 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12766 VAR_INIT_STATUS_INITIALIZED);
12767 if (mem_loc_result == 0)
12768 break;
12769
12770 if (CONST_INT_P (XEXP (rtl, 1))
12771 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12772 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12773 else
12774 {
12775 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12776 VAR_INIT_STATUS_INITIALIZED);
12777 if (op1 == 0)
12778 return NULL;
12779 add_loc_descr (&mem_loc_result, op1);
12780 add_loc_descr (&mem_loc_result,
12781 new_loc_descr (DW_OP_plus, 0, 0));
12782 }
12783 }
12784 break;
12785
12786 /* If a pseudo-reg is optimized away, it is possible for it to
12787 be replaced with a MEM containing a multiply or shift. */
12788 case MINUS:
12789 op = DW_OP_minus;
12790 goto do_binop;
12791
12792 case MULT:
12793 op = DW_OP_mul;
12794 goto do_binop;
12795
12796 case DIV:
12797 if (!dwarf_strict
12798 && GET_MODE_CLASS (mode) == MODE_INT
12799 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12800 {
12801 mem_loc_result = typed_binop (DW_OP_div, rtl,
12802 base_type_for_mode (mode, 0),
12803 mode, mem_mode);
12804 break;
12805 }
12806 op = DW_OP_div;
12807 goto do_binop;
12808
12809 case UMOD:
12810 op = DW_OP_mod;
12811 goto do_binop;
12812
12813 case ASHIFT:
12814 op = DW_OP_shl;
12815 goto do_shift;
12816
12817 case ASHIFTRT:
12818 op = DW_OP_shra;
12819 goto do_shift;
12820
12821 case LSHIFTRT:
12822 op = DW_OP_shr;
12823 goto do_shift;
12824
12825 do_shift:
12826 if (GET_MODE_CLASS (mode) != MODE_INT)
12827 break;
12828 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12829 VAR_INIT_STATUS_INITIALIZED);
12830 {
12831 rtx rtlop1 = XEXP (rtl, 1);
12832 if (GET_MODE (rtlop1) != VOIDmode
12833 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12834 < GET_MODE_BITSIZE (mode))
12835 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12836 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12837 VAR_INIT_STATUS_INITIALIZED);
12838 }
12839
12840 if (op0 == 0 || op1 == 0)
12841 break;
12842
12843 mem_loc_result = op0;
12844 add_loc_descr (&mem_loc_result, op1);
12845 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12846 break;
12847
12848 case AND:
12849 op = DW_OP_and;
12850 goto do_binop;
12851
12852 case IOR:
12853 op = DW_OP_or;
12854 goto do_binop;
12855
12856 case XOR:
12857 op = DW_OP_xor;
12858 goto do_binop;
12859
12860 do_binop:
12861 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12862 VAR_INIT_STATUS_INITIALIZED);
12863 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12864 VAR_INIT_STATUS_INITIALIZED);
12865
12866 if (op0 == 0 || op1 == 0)
12867 break;
12868
12869 mem_loc_result = op0;
12870 add_loc_descr (&mem_loc_result, op1);
12871 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12872 break;
12873
12874 case MOD:
12875 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12876 {
12877 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12878 base_type_for_mode (mode, 0),
12879 mode, mem_mode);
12880 break;
12881 }
12882
12883 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12884 VAR_INIT_STATUS_INITIALIZED);
12885 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12886 VAR_INIT_STATUS_INITIALIZED);
12887
12888 if (op0 == 0 || op1 == 0)
12889 break;
12890
12891 mem_loc_result = op0;
12892 add_loc_descr (&mem_loc_result, op1);
12893 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12894 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12895 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12896 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12897 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12898 break;
12899
12900 case UDIV:
12901 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12902 {
12903 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12904 {
12905 op = DW_OP_div;
12906 goto do_binop;
12907 }
12908 mem_loc_result = typed_binop (DW_OP_div, rtl,
12909 base_type_for_mode (mode, 1),
12910 mode, mem_mode);
12911 }
12912 break;
12913
12914 case NOT:
12915 op = DW_OP_not;
12916 goto do_unop;
12917
12918 case ABS:
12919 op = DW_OP_abs;
12920 goto do_unop;
12921
12922 case NEG:
12923 op = DW_OP_neg;
12924 goto do_unop;
12925
12926 do_unop:
12927 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12928 VAR_INIT_STATUS_INITIALIZED);
12929
12930 if (op0 == 0)
12931 break;
12932
12933 mem_loc_result = op0;
12934 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12935 break;
12936
12937 case CONST_INT:
12938 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12939 #ifdef POINTERS_EXTEND_UNSIGNED
12940 || (mode == Pmode
12941 && mem_mode != VOIDmode
12942 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12943 #endif
12944 )
12945 {
12946 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12947 break;
12948 }
12949 if (!dwarf_strict
12950 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12951 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12952 {
12953 dw_die_ref type_die = base_type_for_mode (mode, 1);
12954 machine_mode amode;
12955 if (type_die == NULL)
12956 return NULL;
12957 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12958 MODE_INT, 0);
12959 if (INTVAL (rtl) >= 0
12960 && amode != BLKmode
12961 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12962 /* const DW_OP_GNU_convert <XXX> vs.
12963 DW_OP_GNU_const_type <XXX, 1, const>. */
12964 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12965 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12966 {
12967 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12968 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12969 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12970 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12971 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12972 add_loc_descr (&mem_loc_result, op0);
12973 return mem_loc_result;
12974 }
12975 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12976 INTVAL (rtl));
12977 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12978 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12979 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12980 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12981 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12982 else
12983 {
12984 mem_loc_result->dw_loc_oprnd2.val_class
12985 = dw_val_class_const_double;
12986 mem_loc_result->dw_loc_oprnd2.v.val_double
12987 = double_int::from_shwi (INTVAL (rtl));
12988 }
12989 }
12990 break;
12991
12992 case CONST_DOUBLE:
12993 if (!dwarf_strict)
12994 {
12995 dw_die_ref type_die;
12996
12997 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12998 CONST_DOUBLE rtx could represent either a large integer
12999 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13000 the value is always a floating point constant.
13001
13002 When it is an integer, a CONST_DOUBLE is used whenever
13003 the constant requires 2 HWIs to be adequately represented.
13004 We output CONST_DOUBLEs as blocks. */
13005 if (mode == VOIDmode
13006 || (GET_MODE (rtl) == VOIDmode
13007 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13008 break;
13009 type_die = base_type_for_mode (mode,
13010 GET_MODE_CLASS (mode) == MODE_INT);
13011 if (type_die == NULL)
13012 return NULL;
13013 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13014 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13015 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13016 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13017 #if TARGET_SUPPORTS_WIDE_INT == 0
13018 if (!SCALAR_FLOAT_MODE_P (mode))
13019 {
13020 mem_loc_result->dw_loc_oprnd2.val_class
13021 = dw_val_class_const_double;
13022 mem_loc_result->dw_loc_oprnd2.v.val_double
13023 = rtx_to_double_int (rtl);
13024 }
13025 else
13026 #endif
13027 {
13028 unsigned int length = GET_MODE_SIZE (mode);
13029 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13030
13031 insert_float (rtl, array);
13032 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13033 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13034 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13035 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13036 }
13037 }
13038 break;
13039
13040 case CONST_WIDE_INT:
13041 if (!dwarf_strict)
13042 {
13043 dw_die_ref type_die;
13044
13045 type_die = base_type_for_mode (mode,
13046 GET_MODE_CLASS (mode) == MODE_INT);
13047 if (type_die == NULL)
13048 return NULL;
13049 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13050 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13051 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13052 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13053 mem_loc_result->dw_loc_oprnd2.val_class
13054 = dw_val_class_wide_int;
13055 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13056 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13057 }
13058 break;
13059
13060 case EQ:
13061 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13062 break;
13063
13064 case GE:
13065 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13066 break;
13067
13068 case GT:
13069 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13070 break;
13071
13072 case LE:
13073 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13074 break;
13075
13076 case LT:
13077 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13078 break;
13079
13080 case NE:
13081 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13082 break;
13083
13084 case GEU:
13085 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13086 break;
13087
13088 case GTU:
13089 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13090 break;
13091
13092 case LEU:
13093 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13094 break;
13095
13096 case LTU:
13097 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13098 break;
13099
13100 case UMIN:
13101 case UMAX:
13102 if (GET_MODE_CLASS (mode) != MODE_INT)
13103 break;
13104 /* FALLTHRU */
13105 case SMIN:
13106 case SMAX:
13107 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13108 break;
13109
13110 case ZERO_EXTRACT:
13111 case SIGN_EXTRACT:
13112 if (CONST_INT_P (XEXP (rtl, 1))
13113 && CONST_INT_P (XEXP (rtl, 2))
13114 && ((unsigned) INTVAL (XEXP (rtl, 1))
13115 + (unsigned) INTVAL (XEXP (rtl, 2))
13116 <= GET_MODE_BITSIZE (mode))
13117 && GET_MODE_CLASS (mode) == MODE_INT
13118 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13119 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13120 {
13121 int shift, size;
13122 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13123 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13124 if (op0 == 0)
13125 break;
13126 if (GET_CODE (rtl) == SIGN_EXTRACT)
13127 op = DW_OP_shra;
13128 else
13129 op = DW_OP_shr;
13130 mem_loc_result = op0;
13131 size = INTVAL (XEXP (rtl, 1));
13132 shift = INTVAL (XEXP (rtl, 2));
13133 if (BITS_BIG_ENDIAN)
13134 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13135 - shift - size;
13136 if (shift + size != (int) DWARF2_ADDR_SIZE)
13137 {
13138 add_loc_descr (&mem_loc_result,
13139 int_loc_descriptor (DWARF2_ADDR_SIZE
13140 - shift - size));
13141 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13142 }
13143 if (size != (int) DWARF2_ADDR_SIZE)
13144 {
13145 add_loc_descr (&mem_loc_result,
13146 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13147 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13148 }
13149 }
13150 break;
13151
13152 case IF_THEN_ELSE:
13153 {
13154 dw_loc_descr_ref op2, bra_node, drop_node;
13155 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13156 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13157 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13158 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13159 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13160 VAR_INIT_STATUS_INITIALIZED);
13161 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13162 VAR_INIT_STATUS_INITIALIZED);
13163 if (op0 == NULL || op1 == NULL || op2 == NULL)
13164 break;
13165
13166 mem_loc_result = op1;
13167 add_loc_descr (&mem_loc_result, op2);
13168 add_loc_descr (&mem_loc_result, op0);
13169 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13170 add_loc_descr (&mem_loc_result, bra_node);
13171 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13172 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13173 add_loc_descr (&mem_loc_result, drop_node);
13174 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13175 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13176 }
13177 break;
13178
13179 case FLOAT_EXTEND:
13180 case FLOAT_TRUNCATE:
13181 case FLOAT:
13182 case UNSIGNED_FLOAT:
13183 case FIX:
13184 case UNSIGNED_FIX:
13185 if (!dwarf_strict)
13186 {
13187 dw_die_ref type_die;
13188 dw_loc_descr_ref cvt;
13189
13190 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13191 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13192 if (op0 == NULL)
13193 break;
13194 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13195 && (GET_CODE (rtl) == FLOAT
13196 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13197 <= DWARF2_ADDR_SIZE))
13198 {
13199 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13200 GET_CODE (rtl) == UNSIGNED_FLOAT);
13201 if (type_die == NULL)
13202 break;
13203 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13204 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13205 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13206 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13207 add_loc_descr (&op0, cvt);
13208 }
13209 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13210 if (type_die == NULL)
13211 break;
13212 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13213 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13214 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13215 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13216 add_loc_descr (&op0, cvt);
13217 if (GET_MODE_CLASS (mode) == MODE_INT
13218 && (GET_CODE (rtl) == FIX
13219 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13220 {
13221 op0 = convert_descriptor_to_mode (mode, op0);
13222 if (op0 == NULL)
13223 break;
13224 }
13225 mem_loc_result = op0;
13226 }
13227 break;
13228
13229 case CLZ:
13230 case CTZ:
13231 case FFS:
13232 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13233 break;
13234
13235 case POPCOUNT:
13236 case PARITY:
13237 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13238 break;
13239
13240 case BSWAP:
13241 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13242 break;
13243
13244 case ROTATE:
13245 case ROTATERT:
13246 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13247 break;
13248
13249 case COMPARE:
13250 /* In theory, we could implement the above. */
13251 /* DWARF cannot represent the unsigned compare operations
13252 natively. */
13253 case SS_MULT:
13254 case US_MULT:
13255 case SS_DIV:
13256 case US_DIV:
13257 case SS_PLUS:
13258 case US_PLUS:
13259 case SS_MINUS:
13260 case US_MINUS:
13261 case SS_NEG:
13262 case US_NEG:
13263 case SS_ABS:
13264 case SS_ASHIFT:
13265 case US_ASHIFT:
13266 case SS_TRUNCATE:
13267 case US_TRUNCATE:
13268 case UNORDERED:
13269 case ORDERED:
13270 case UNEQ:
13271 case UNGE:
13272 case UNGT:
13273 case UNLE:
13274 case UNLT:
13275 case LTGT:
13276 case FRACT_CONVERT:
13277 case UNSIGNED_FRACT_CONVERT:
13278 case SAT_FRACT:
13279 case UNSIGNED_SAT_FRACT:
13280 case SQRT:
13281 case ASM_OPERANDS:
13282 case VEC_MERGE:
13283 case VEC_SELECT:
13284 case VEC_CONCAT:
13285 case VEC_DUPLICATE:
13286 case UNSPEC:
13287 case HIGH:
13288 case FMA:
13289 case STRICT_LOW_PART:
13290 case CONST_VECTOR:
13291 case CONST_FIXED:
13292 case CLRSB:
13293 case CLOBBER:
13294 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13295 can't express it in the debug info. This can happen e.g. with some
13296 TLS UNSPECs. */
13297 break;
13298
13299 case CONST_STRING:
13300 resolve_one_addr (&rtl);
13301 goto symref;
13302
13303 default:
13304 #ifdef ENABLE_CHECKING
13305 print_rtl (stderr, rtl);
13306 gcc_unreachable ();
13307 #else
13308 break;
13309 #endif
13310 }
13311
13312 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13313 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13314
13315 return mem_loc_result;
13316 }
13317
13318 /* Return a descriptor that describes the concatenation of two locations.
13319 This is typically a complex variable. */
13320
13321 static dw_loc_descr_ref
13322 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13323 {
13324 dw_loc_descr_ref cc_loc_result = NULL;
13325 dw_loc_descr_ref x0_ref
13326 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13327 dw_loc_descr_ref x1_ref
13328 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13329
13330 if (x0_ref == 0 || x1_ref == 0)
13331 return 0;
13332
13333 cc_loc_result = x0_ref;
13334 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13335
13336 add_loc_descr (&cc_loc_result, x1_ref);
13337 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13338
13339 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13340 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13341
13342 return cc_loc_result;
13343 }
13344
13345 /* Return a descriptor that describes the concatenation of N
13346 locations. */
13347
13348 static dw_loc_descr_ref
13349 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13350 {
13351 unsigned int i;
13352 dw_loc_descr_ref cc_loc_result = NULL;
13353 unsigned int n = XVECLEN (concatn, 0);
13354
13355 for (i = 0; i < n; ++i)
13356 {
13357 dw_loc_descr_ref ref;
13358 rtx x = XVECEXP (concatn, 0, i);
13359
13360 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13361 if (ref == NULL)
13362 return NULL;
13363
13364 add_loc_descr (&cc_loc_result, ref);
13365 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13366 }
13367
13368 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13369 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13370
13371 return cc_loc_result;
13372 }
13373
13374 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13375 for DEBUG_IMPLICIT_PTR RTL. */
13376
13377 static dw_loc_descr_ref
13378 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13379 {
13380 dw_loc_descr_ref ret;
13381 dw_die_ref ref;
13382
13383 if (dwarf_strict)
13384 return NULL;
13385 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13386 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13387 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13388 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13389 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13390 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13391 if (ref)
13392 {
13393 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13394 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13395 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13396 }
13397 else
13398 {
13399 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13400 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13401 }
13402 return ret;
13403 }
13404
13405 /* Output a proper Dwarf location descriptor for a variable or parameter
13406 which is either allocated in a register or in a memory location. For a
13407 register, we just generate an OP_REG and the register number. For a
13408 memory location we provide a Dwarf postfix expression describing how to
13409 generate the (dynamic) address of the object onto the address stack.
13410
13411 MODE is mode of the decl if this loc_descriptor is going to be used in
13412 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13413 allowed, VOIDmode otherwise.
13414
13415 If we don't know how to describe it, return 0. */
13416
13417 static dw_loc_descr_ref
13418 loc_descriptor (rtx rtl, machine_mode mode,
13419 enum var_init_status initialized)
13420 {
13421 dw_loc_descr_ref loc_result = NULL;
13422
13423 switch (GET_CODE (rtl))
13424 {
13425 case SUBREG:
13426 /* The case of a subreg may arise when we have a local (register)
13427 variable or a formal (register) parameter which doesn't quite fill
13428 up an entire register. For now, just assume that it is
13429 legitimate to make the Dwarf info refer to the whole register which
13430 contains the given subreg. */
13431 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13432 loc_result = loc_descriptor (SUBREG_REG (rtl),
13433 GET_MODE (SUBREG_REG (rtl)), initialized);
13434 else
13435 goto do_default;
13436 break;
13437
13438 case REG:
13439 loc_result = reg_loc_descriptor (rtl, initialized);
13440 break;
13441
13442 case MEM:
13443 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13444 GET_MODE (rtl), initialized);
13445 if (loc_result == NULL)
13446 loc_result = tls_mem_loc_descriptor (rtl);
13447 if (loc_result == NULL)
13448 {
13449 rtx new_rtl = avoid_constant_pool_reference (rtl);
13450 if (new_rtl != rtl)
13451 loc_result = loc_descriptor (new_rtl, mode, initialized);
13452 }
13453 break;
13454
13455 case CONCAT:
13456 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13457 initialized);
13458 break;
13459
13460 case CONCATN:
13461 loc_result = concatn_loc_descriptor (rtl, initialized);
13462 break;
13463
13464 case VAR_LOCATION:
13465 /* Single part. */
13466 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13467 {
13468 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13469 if (GET_CODE (loc) == EXPR_LIST)
13470 loc = XEXP (loc, 0);
13471 loc_result = loc_descriptor (loc, mode, initialized);
13472 break;
13473 }
13474
13475 rtl = XEXP (rtl, 1);
13476 /* FALLTHRU */
13477
13478 case PARALLEL:
13479 {
13480 rtvec par_elems = XVEC (rtl, 0);
13481 int num_elem = GET_NUM_ELEM (par_elems);
13482 machine_mode mode;
13483 int i;
13484
13485 /* Create the first one, so we have something to add to. */
13486 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13487 VOIDmode, initialized);
13488 if (loc_result == NULL)
13489 return NULL;
13490 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13491 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13492 for (i = 1; i < num_elem; i++)
13493 {
13494 dw_loc_descr_ref temp;
13495
13496 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13497 VOIDmode, initialized);
13498 if (temp == NULL)
13499 return NULL;
13500 add_loc_descr (&loc_result, temp);
13501 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13502 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13503 }
13504 }
13505 break;
13506
13507 case CONST_INT:
13508 if (mode != VOIDmode && mode != BLKmode)
13509 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13510 INTVAL (rtl));
13511 break;
13512
13513 case CONST_DOUBLE:
13514 if (mode == VOIDmode)
13515 mode = GET_MODE (rtl);
13516
13517 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13518 {
13519 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13520
13521 /* Note that a CONST_DOUBLE rtx could represent either an integer
13522 or a floating-point constant. A CONST_DOUBLE is used whenever
13523 the constant requires more than one word in order to be
13524 adequately represented. We output CONST_DOUBLEs as blocks. */
13525 loc_result = new_loc_descr (DW_OP_implicit_value,
13526 GET_MODE_SIZE (mode), 0);
13527 #if TARGET_SUPPORTS_WIDE_INT == 0
13528 if (!SCALAR_FLOAT_MODE_P (mode))
13529 {
13530 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13531 loc_result->dw_loc_oprnd2.v.val_double
13532 = rtx_to_double_int (rtl);
13533 }
13534 else
13535 #endif
13536 {
13537 unsigned int length = GET_MODE_SIZE (mode);
13538 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13539
13540 insert_float (rtl, array);
13541 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13542 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13543 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13544 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13545 }
13546 }
13547 break;
13548
13549 case CONST_WIDE_INT:
13550 if (mode == VOIDmode)
13551 mode = GET_MODE (rtl);
13552
13553 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13554 {
13555 loc_result = new_loc_descr (DW_OP_implicit_value,
13556 GET_MODE_SIZE (mode), 0);
13557 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13558 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13559 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13560 }
13561 break;
13562
13563 case CONST_VECTOR:
13564 if (mode == VOIDmode)
13565 mode = GET_MODE (rtl);
13566
13567 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13568 {
13569 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13570 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13571 unsigned char *array
13572 = ggc_vec_alloc<unsigned char> (length * elt_size);
13573 unsigned int i;
13574 unsigned char *p;
13575 machine_mode imode = GET_MODE_INNER (mode);
13576
13577 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13578 switch (GET_MODE_CLASS (mode))
13579 {
13580 case MODE_VECTOR_INT:
13581 for (i = 0, p = array; i < length; i++, p += elt_size)
13582 {
13583 rtx elt = CONST_VECTOR_ELT (rtl, i);
13584 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13585 }
13586 break;
13587
13588 case MODE_VECTOR_FLOAT:
13589 for (i = 0, p = array; i < length; i++, p += elt_size)
13590 {
13591 rtx elt = CONST_VECTOR_ELT (rtl, i);
13592 insert_float (elt, p);
13593 }
13594 break;
13595
13596 default:
13597 gcc_unreachable ();
13598 }
13599
13600 loc_result = new_loc_descr (DW_OP_implicit_value,
13601 length * elt_size, 0);
13602 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13603 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13604 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13605 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13606 }
13607 break;
13608
13609 case CONST:
13610 if (mode == VOIDmode
13611 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13612 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13613 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13614 {
13615 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13616 break;
13617 }
13618 /* FALLTHROUGH */
13619 case SYMBOL_REF:
13620 if (!const_ok_for_output (rtl))
13621 break;
13622 case LABEL_REF:
13623 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13624 && (dwarf_version >= 4 || !dwarf_strict))
13625 {
13626 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13627 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13628 vec_safe_push (used_rtx_array, rtl);
13629 }
13630 break;
13631
13632 case DEBUG_IMPLICIT_PTR:
13633 loc_result = implicit_ptr_descriptor (rtl, 0);
13634 break;
13635
13636 case PLUS:
13637 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13638 && CONST_INT_P (XEXP (rtl, 1)))
13639 {
13640 loc_result
13641 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13642 break;
13643 }
13644 /* FALLTHRU */
13645 do_default:
13646 default:
13647 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13648 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13649 && dwarf_version >= 4)
13650 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13651 {
13652 /* Value expression. */
13653 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13654 if (loc_result)
13655 add_loc_descr (&loc_result,
13656 new_loc_descr (DW_OP_stack_value, 0, 0));
13657 }
13658 break;
13659 }
13660
13661 return loc_result;
13662 }
13663
13664 /* We need to figure out what section we should use as the base for the
13665 address ranges where a given location is valid.
13666 1. If this particular DECL has a section associated with it, use that.
13667 2. If this function has a section associated with it, use that.
13668 3. Otherwise, use the text section.
13669 XXX: If you split a variable across multiple sections, we won't notice. */
13670
13671 static const char *
13672 secname_for_decl (const_tree decl)
13673 {
13674 const char *secname;
13675
13676 if (VAR_OR_FUNCTION_DECL_P (decl)
13677 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13678 && DECL_SECTION_NAME (decl))
13679 secname = DECL_SECTION_NAME (decl);
13680 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13681 secname = DECL_SECTION_NAME (current_function_decl);
13682 else if (cfun && in_cold_section_p)
13683 secname = crtl->subsections.cold_section_label;
13684 else
13685 secname = text_section_label;
13686
13687 return secname;
13688 }
13689
13690 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13691
13692 static bool
13693 decl_by_reference_p (tree decl)
13694 {
13695 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13696 || TREE_CODE (decl) == VAR_DECL)
13697 && DECL_BY_REFERENCE (decl));
13698 }
13699
13700 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13701 for VARLOC. */
13702
13703 static dw_loc_descr_ref
13704 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13705 enum var_init_status initialized)
13706 {
13707 int have_address = 0;
13708 dw_loc_descr_ref descr;
13709 machine_mode mode;
13710
13711 if (want_address != 2)
13712 {
13713 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13714 /* Single part. */
13715 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13716 {
13717 varloc = PAT_VAR_LOCATION_LOC (varloc);
13718 if (GET_CODE (varloc) == EXPR_LIST)
13719 varloc = XEXP (varloc, 0);
13720 mode = GET_MODE (varloc);
13721 if (MEM_P (varloc))
13722 {
13723 rtx addr = XEXP (varloc, 0);
13724 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13725 mode, initialized);
13726 if (descr)
13727 have_address = 1;
13728 else
13729 {
13730 rtx x = avoid_constant_pool_reference (varloc);
13731 if (x != varloc)
13732 descr = mem_loc_descriptor (x, mode, VOIDmode,
13733 initialized);
13734 }
13735 }
13736 else
13737 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13738 }
13739 else
13740 return 0;
13741 }
13742 else
13743 {
13744 if (GET_CODE (varloc) == VAR_LOCATION)
13745 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13746 else
13747 mode = DECL_MODE (loc);
13748 descr = loc_descriptor (varloc, mode, initialized);
13749 have_address = 1;
13750 }
13751
13752 if (!descr)
13753 return 0;
13754
13755 if (want_address == 2 && !have_address
13756 && (dwarf_version >= 4 || !dwarf_strict))
13757 {
13758 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13759 {
13760 expansion_failed (loc, NULL_RTX,
13761 "DWARF address size mismatch");
13762 return 0;
13763 }
13764 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13765 have_address = 1;
13766 }
13767 /* Show if we can't fill the request for an address. */
13768 if (want_address && !have_address)
13769 {
13770 expansion_failed (loc, NULL_RTX,
13771 "Want address and only have value");
13772 return 0;
13773 }
13774
13775 /* If we've got an address and don't want one, dereference. */
13776 if (!want_address && have_address)
13777 {
13778 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13779 enum dwarf_location_atom op;
13780
13781 if (size > DWARF2_ADDR_SIZE || size == -1)
13782 {
13783 expansion_failed (loc, NULL_RTX,
13784 "DWARF address size mismatch");
13785 return 0;
13786 }
13787 else if (size == DWARF2_ADDR_SIZE)
13788 op = DW_OP_deref;
13789 else
13790 op = DW_OP_deref_size;
13791
13792 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13793 }
13794
13795 return descr;
13796 }
13797
13798 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13799 if it is not possible. */
13800
13801 static dw_loc_descr_ref
13802 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13803 {
13804 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13805 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13806 else if (dwarf_version >= 3 || !dwarf_strict)
13807 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13808 else
13809 return NULL;
13810 }
13811
13812 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13813 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13814
13815 static dw_loc_descr_ref
13816 dw_sra_loc_expr (tree decl, rtx loc)
13817 {
13818 rtx p;
13819 unsigned int padsize = 0;
13820 dw_loc_descr_ref descr, *descr_tail;
13821 unsigned HOST_WIDE_INT decl_size;
13822 rtx varloc;
13823 enum var_init_status initialized;
13824
13825 if (DECL_SIZE (decl) == NULL
13826 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13827 return NULL;
13828
13829 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13830 descr = NULL;
13831 descr_tail = &descr;
13832
13833 for (p = loc; p; p = XEXP (p, 1))
13834 {
13835 unsigned int bitsize = decl_piece_bitsize (p);
13836 rtx loc_note = *decl_piece_varloc_ptr (p);
13837 dw_loc_descr_ref cur_descr;
13838 dw_loc_descr_ref *tail, last = NULL;
13839 unsigned int opsize = 0;
13840
13841 if (loc_note == NULL_RTX
13842 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13843 {
13844 padsize += bitsize;
13845 continue;
13846 }
13847 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13848 varloc = NOTE_VAR_LOCATION (loc_note);
13849 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13850 if (cur_descr == NULL)
13851 {
13852 padsize += bitsize;
13853 continue;
13854 }
13855
13856 /* Check that cur_descr either doesn't use
13857 DW_OP_*piece operations, or their sum is equal
13858 to bitsize. Otherwise we can't embed it. */
13859 for (tail = &cur_descr; *tail != NULL;
13860 tail = &(*tail)->dw_loc_next)
13861 if ((*tail)->dw_loc_opc == DW_OP_piece)
13862 {
13863 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13864 * BITS_PER_UNIT;
13865 last = *tail;
13866 }
13867 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13868 {
13869 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13870 last = *tail;
13871 }
13872
13873 if (last != NULL && opsize != bitsize)
13874 {
13875 padsize += bitsize;
13876 /* Discard the current piece of the descriptor and release any
13877 addr_table entries it uses. */
13878 remove_loc_list_addr_table_entries (cur_descr);
13879 continue;
13880 }
13881
13882 /* If there is a hole, add DW_OP_*piece after empty DWARF
13883 expression, which means that those bits are optimized out. */
13884 if (padsize)
13885 {
13886 if (padsize > decl_size)
13887 {
13888 remove_loc_list_addr_table_entries (cur_descr);
13889 goto discard_descr;
13890 }
13891 decl_size -= padsize;
13892 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13893 if (*descr_tail == NULL)
13894 {
13895 remove_loc_list_addr_table_entries (cur_descr);
13896 goto discard_descr;
13897 }
13898 descr_tail = &(*descr_tail)->dw_loc_next;
13899 padsize = 0;
13900 }
13901 *descr_tail = cur_descr;
13902 descr_tail = tail;
13903 if (bitsize > decl_size)
13904 goto discard_descr;
13905 decl_size -= bitsize;
13906 if (last == NULL)
13907 {
13908 HOST_WIDE_INT offset = 0;
13909 if (GET_CODE (varloc) == VAR_LOCATION
13910 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13911 {
13912 varloc = PAT_VAR_LOCATION_LOC (varloc);
13913 if (GET_CODE (varloc) == EXPR_LIST)
13914 varloc = XEXP (varloc, 0);
13915 }
13916 do
13917 {
13918 if (GET_CODE (varloc) == CONST
13919 || GET_CODE (varloc) == SIGN_EXTEND
13920 || GET_CODE (varloc) == ZERO_EXTEND)
13921 varloc = XEXP (varloc, 0);
13922 else if (GET_CODE (varloc) == SUBREG)
13923 varloc = SUBREG_REG (varloc);
13924 else
13925 break;
13926 }
13927 while (1);
13928 /* DW_OP_bit_size offset should be zero for register
13929 or implicit location descriptions and empty location
13930 descriptions, but for memory addresses needs big endian
13931 adjustment. */
13932 if (MEM_P (varloc))
13933 {
13934 unsigned HOST_WIDE_INT memsize
13935 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13936 if (memsize != bitsize)
13937 {
13938 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13939 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13940 goto discard_descr;
13941 if (memsize < bitsize)
13942 goto discard_descr;
13943 if (BITS_BIG_ENDIAN)
13944 offset = memsize - bitsize;
13945 }
13946 }
13947
13948 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13949 if (*descr_tail == NULL)
13950 goto discard_descr;
13951 descr_tail = &(*descr_tail)->dw_loc_next;
13952 }
13953 }
13954
13955 /* If there were any non-empty expressions, add padding till the end of
13956 the decl. */
13957 if (descr != NULL && decl_size != 0)
13958 {
13959 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13960 if (*descr_tail == NULL)
13961 goto discard_descr;
13962 }
13963 return descr;
13964
13965 discard_descr:
13966 /* Discard the descriptor and release any addr_table entries it uses. */
13967 remove_loc_list_addr_table_entries (descr);
13968 return NULL;
13969 }
13970
13971 /* Return the dwarf representation of the location list LOC_LIST of
13972 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13973 function. */
13974
13975 static dw_loc_list_ref
13976 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13977 {
13978 const char *endname, *secname;
13979 rtx varloc;
13980 enum var_init_status initialized;
13981 struct var_loc_node *node;
13982 dw_loc_descr_ref descr;
13983 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13984 dw_loc_list_ref list = NULL;
13985 dw_loc_list_ref *listp = &list;
13986
13987 /* Now that we know what section we are using for a base,
13988 actually construct the list of locations.
13989 The first location information is what is passed to the
13990 function that creates the location list, and the remaining
13991 locations just get added on to that list.
13992 Note that we only know the start address for a location
13993 (IE location changes), so to build the range, we use
13994 the range [current location start, next location start].
13995 This means we have to special case the last node, and generate
13996 a range of [last location start, end of function label]. */
13997
13998 secname = secname_for_decl (decl);
13999
14000 for (node = loc_list->first; node; node = node->next)
14001 if (GET_CODE (node->loc) == EXPR_LIST
14002 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14003 {
14004 if (GET_CODE (node->loc) == EXPR_LIST)
14005 {
14006 /* This requires DW_OP_{,bit_}piece, which is not usable
14007 inside DWARF expressions. */
14008 if (want_address != 2)
14009 continue;
14010 descr = dw_sra_loc_expr (decl, node->loc);
14011 if (descr == NULL)
14012 continue;
14013 }
14014 else
14015 {
14016 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14017 varloc = NOTE_VAR_LOCATION (node->loc);
14018 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14019 }
14020 if (descr)
14021 {
14022 bool range_across_switch = false;
14023 /* If section switch happens in between node->label
14024 and node->next->label (or end of function) and
14025 we can't emit it as a single entry list,
14026 emit two ranges, first one ending at the end
14027 of first partition and second one starting at the
14028 beginning of second partition. */
14029 if (node == loc_list->last_before_switch
14030 && (node != loc_list->first || loc_list->first->next)
14031 && current_function_decl)
14032 {
14033 endname = cfun->fde->dw_fde_end;
14034 range_across_switch = true;
14035 }
14036 /* The variable has a location between NODE->LABEL and
14037 NODE->NEXT->LABEL. */
14038 else if (node->next)
14039 endname = node->next->label;
14040 /* If the variable has a location at the last label
14041 it keeps its location until the end of function. */
14042 else if (!current_function_decl)
14043 endname = text_end_label;
14044 else
14045 {
14046 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14047 current_function_funcdef_no);
14048 endname = ggc_strdup (label_id);
14049 }
14050
14051 *listp = new_loc_list (descr, node->label, endname, secname);
14052 if (TREE_CODE (decl) == PARM_DECL
14053 && node == loc_list->first
14054 && NOTE_P (node->loc)
14055 && strcmp (node->label, endname) == 0)
14056 (*listp)->force = true;
14057 listp = &(*listp)->dw_loc_next;
14058
14059 if (range_across_switch)
14060 {
14061 if (GET_CODE (node->loc) == EXPR_LIST)
14062 descr = dw_sra_loc_expr (decl, node->loc);
14063 else
14064 {
14065 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14066 varloc = NOTE_VAR_LOCATION (node->loc);
14067 descr = dw_loc_list_1 (decl, varloc, want_address,
14068 initialized);
14069 }
14070 gcc_assert (descr);
14071 /* The variable has a location between NODE->LABEL and
14072 NODE->NEXT->LABEL. */
14073 if (node->next)
14074 endname = node->next->label;
14075 else
14076 endname = cfun->fde->dw_fde_second_end;
14077 *listp = new_loc_list (descr,
14078 cfun->fde->dw_fde_second_begin,
14079 endname, secname);
14080 listp = &(*listp)->dw_loc_next;
14081 }
14082 }
14083 }
14084
14085 /* Try to avoid the overhead of a location list emitting a location
14086 expression instead, but only if we didn't have more than one
14087 location entry in the first place. If some entries were not
14088 representable, we don't want to pretend a single entry that was
14089 applies to the entire scope in which the variable is
14090 available. */
14091 if (list && loc_list->first->next)
14092 gen_llsym (list);
14093
14094 return list;
14095 }
14096
14097 /* Return if the loc_list has only single element and thus can be represented
14098 as location description. */
14099
14100 static bool
14101 single_element_loc_list_p (dw_loc_list_ref list)
14102 {
14103 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14104 return !list->ll_symbol;
14105 }
14106
14107 /* To each location in list LIST add loc descr REF. */
14108
14109 static void
14110 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14111 {
14112 dw_loc_descr_ref copy;
14113 add_loc_descr (&list->expr, ref);
14114 list = list->dw_loc_next;
14115 while (list)
14116 {
14117 copy = ggc_alloc<dw_loc_descr_node> ();
14118 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14119 add_loc_descr (&list->expr, copy);
14120 while (copy->dw_loc_next)
14121 {
14122 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14123 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14124 copy->dw_loc_next = new_copy;
14125 copy = new_copy;
14126 }
14127 list = list->dw_loc_next;
14128 }
14129 }
14130
14131 /* Given two lists RET and LIST
14132 produce location list that is result of adding expression in LIST
14133 to expression in RET on each position in program.
14134 Might be destructive on both RET and LIST.
14135
14136 TODO: We handle only simple cases of RET or LIST having at most one
14137 element. General case would inolve sorting the lists in program order
14138 and merging them that will need some additional work.
14139 Adding that will improve quality of debug info especially for SRA-ed
14140 structures. */
14141
14142 static void
14143 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14144 {
14145 if (!list)
14146 return;
14147 if (!*ret)
14148 {
14149 *ret = list;
14150 return;
14151 }
14152 if (!list->dw_loc_next)
14153 {
14154 add_loc_descr_to_each (*ret, list->expr);
14155 return;
14156 }
14157 if (!(*ret)->dw_loc_next)
14158 {
14159 add_loc_descr_to_each (list, (*ret)->expr);
14160 *ret = list;
14161 return;
14162 }
14163 expansion_failed (NULL_TREE, NULL_RTX,
14164 "Don't know how to merge two non-trivial"
14165 " location lists.\n");
14166 *ret = NULL;
14167 return;
14168 }
14169
14170 /* LOC is constant expression. Try a luck, look it up in constant
14171 pool and return its loc_descr of its address. */
14172
14173 static dw_loc_descr_ref
14174 cst_pool_loc_descr (tree loc)
14175 {
14176 /* Get an RTL for this, if something has been emitted. */
14177 rtx rtl = lookup_constant_def (loc);
14178
14179 if (!rtl || !MEM_P (rtl))
14180 {
14181 gcc_assert (!rtl);
14182 return 0;
14183 }
14184 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14185
14186 /* TODO: We might get more coverage if we was actually delaying expansion
14187 of all expressions till end of compilation when constant pools are fully
14188 populated. */
14189 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14190 {
14191 expansion_failed (loc, NULL_RTX,
14192 "CST value in contant pool but not marked.");
14193 return 0;
14194 }
14195 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14196 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14197 }
14198
14199 /* Return dw_loc_list representing address of addr_expr LOC
14200 by looking for inner INDIRECT_REF expression and turning
14201 it into simple arithmetics. */
14202
14203 static dw_loc_list_ref
14204 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14205 {
14206 tree obj, offset;
14207 HOST_WIDE_INT bitsize, bitpos, bytepos;
14208 machine_mode mode;
14209 int unsignedp, volatilep = 0;
14210 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14211
14212 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14213 &bitsize, &bitpos, &offset, &mode,
14214 &unsignedp, &volatilep, false);
14215 STRIP_NOPS (obj);
14216 if (bitpos % BITS_PER_UNIT)
14217 {
14218 expansion_failed (loc, NULL_RTX, "bitfield access");
14219 return 0;
14220 }
14221 if (!INDIRECT_REF_P (obj))
14222 {
14223 expansion_failed (obj,
14224 NULL_RTX, "no indirect ref in inner refrence");
14225 return 0;
14226 }
14227 if (!offset && !bitpos)
14228 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14229 else if (toplev
14230 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14231 && (dwarf_version >= 4 || !dwarf_strict))
14232 {
14233 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14234 if (!list_ret)
14235 return 0;
14236 if (offset)
14237 {
14238 /* Variable offset. */
14239 list_ret1 = loc_list_from_tree (offset, 0);
14240 if (list_ret1 == 0)
14241 return 0;
14242 add_loc_list (&list_ret, list_ret1);
14243 if (!list_ret)
14244 return 0;
14245 add_loc_descr_to_each (list_ret,
14246 new_loc_descr (DW_OP_plus, 0, 0));
14247 }
14248 bytepos = bitpos / BITS_PER_UNIT;
14249 if (bytepos > 0)
14250 add_loc_descr_to_each (list_ret,
14251 new_loc_descr (DW_OP_plus_uconst,
14252 bytepos, 0));
14253 else if (bytepos < 0)
14254 loc_list_plus_const (list_ret, bytepos);
14255 add_loc_descr_to_each (list_ret,
14256 new_loc_descr (DW_OP_stack_value, 0, 0));
14257 }
14258 return list_ret;
14259 }
14260
14261
14262 /* Generate Dwarf location list representing LOC.
14263 If WANT_ADDRESS is false, expression computing LOC will be computed
14264 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14265 if WANT_ADDRESS is 2, expression computing address useable in location
14266 will be returned (i.e. DW_OP_reg can be used
14267 to refer to register values). */
14268
14269 static dw_loc_list_ref
14270 loc_list_from_tree (tree loc, int want_address)
14271 {
14272 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14273 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14274 int have_address = 0;
14275 enum dwarf_location_atom op;
14276
14277 /* ??? Most of the time we do not take proper care for sign/zero
14278 extending the values properly. Hopefully this won't be a real
14279 problem... */
14280
14281 switch (TREE_CODE (loc))
14282 {
14283 case ERROR_MARK:
14284 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14285 return 0;
14286
14287 case PLACEHOLDER_EXPR:
14288 /* This case involves extracting fields from an object to determine the
14289 position of other fields. We don't try to encode this here. The
14290 only user of this is Ada, which encodes the needed information using
14291 the names of types. */
14292 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14293 return 0;
14294
14295 case CALL_EXPR:
14296 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14297 /* There are no opcodes for these operations. */
14298 return 0;
14299
14300 case PREINCREMENT_EXPR:
14301 case PREDECREMENT_EXPR:
14302 case POSTINCREMENT_EXPR:
14303 case POSTDECREMENT_EXPR:
14304 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14305 /* There are no opcodes for these operations. */
14306 return 0;
14307
14308 case ADDR_EXPR:
14309 /* If we already want an address, see if there is INDIRECT_REF inside
14310 e.g. for &this->field. */
14311 if (want_address)
14312 {
14313 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14314 (loc, want_address == 2);
14315 if (list_ret)
14316 have_address = 1;
14317 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14318 && (ret = cst_pool_loc_descr (loc)))
14319 have_address = 1;
14320 }
14321 /* Otherwise, process the argument and look for the address. */
14322 if (!list_ret && !ret)
14323 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14324 else
14325 {
14326 if (want_address)
14327 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14328 return NULL;
14329 }
14330 break;
14331
14332 case VAR_DECL:
14333 if (DECL_THREAD_LOCAL_P (loc))
14334 {
14335 rtx rtl;
14336 enum dwarf_location_atom tls_op;
14337 enum dtprel_bool dtprel = dtprel_false;
14338
14339 if (targetm.have_tls)
14340 {
14341 /* If this is not defined, we have no way to emit the
14342 data. */
14343 if (!targetm.asm_out.output_dwarf_dtprel)
14344 return 0;
14345
14346 /* The way DW_OP_GNU_push_tls_address is specified, we
14347 can only look up addresses of objects in the current
14348 module. We used DW_OP_addr as first op, but that's
14349 wrong, because DW_OP_addr is relocated by the debug
14350 info consumer, while DW_OP_GNU_push_tls_address
14351 operand shouldn't be. */
14352 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14353 return 0;
14354 dtprel = dtprel_true;
14355 tls_op = DW_OP_GNU_push_tls_address;
14356 }
14357 else
14358 {
14359 if (!targetm.emutls.debug_form_tls_address
14360 || !(dwarf_version >= 3 || !dwarf_strict))
14361 return 0;
14362 /* We stuffed the control variable into the DECL_VALUE_EXPR
14363 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14364 no longer appear in gimple code. We used the control
14365 variable in specific so that we could pick it up here. */
14366 loc = DECL_VALUE_EXPR (loc);
14367 tls_op = DW_OP_form_tls_address;
14368 }
14369
14370 rtl = rtl_for_decl_location (loc);
14371 if (rtl == NULL_RTX)
14372 return 0;
14373
14374 if (!MEM_P (rtl))
14375 return 0;
14376 rtl = XEXP (rtl, 0);
14377 if (! CONSTANT_P (rtl))
14378 return 0;
14379
14380 ret = new_addr_loc_descr (rtl, dtprel);
14381 ret1 = new_loc_descr (tls_op, 0, 0);
14382 add_loc_descr (&ret, ret1);
14383
14384 have_address = 1;
14385 break;
14386 }
14387 /* FALLTHRU */
14388
14389 case PARM_DECL:
14390 case RESULT_DECL:
14391 if (DECL_HAS_VALUE_EXPR_P (loc))
14392 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14393 want_address);
14394 /* FALLTHRU */
14395
14396 case FUNCTION_DECL:
14397 {
14398 rtx rtl;
14399 var_loc_list *loc_list = lookup_decl_loc (loc);
14400
14401 if (loc_list && loc_list->first)
14402 {
14403 list_ret = dw_loc_list (loc_list, loc, want_address);
14404 have_address = want_address != 0;
14405 break;
14406 }
14407 rtl = rtl_for_decl_location (loc);
14408 if (rtl == NULL_RTX)
14409 {
14410 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14411 return 0;
14412 }
14413 else if (CONST_INT_P (rtl))
14414 {
14415 HOST_WIDE_INT val = INTVAL (rtl);
14416 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14417 val &= GET_MODE_MASK (DECL_MODE (loc));
14418 ret = int_loc_descriptor (val);
14419 }
14420 else if (GET_CODE (rtl) == CONST_STRING)
14421 {
14422 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14423 return 0;
14424 }
14425 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14426 ret = new_addr_loc_descr (rtl, dtprel_false);
14427 else
14428 {
14429 machine_mode mode, mem_mode;
14430
14431 /* Certain constructs can only be represented at top-level. */
14432 if (want_address == 2)
14433 {
14434 ret = loc_descriptor (rtl, VOIDmode,
14435 VAR_INIT_STATUS_INITIALIZED);
14436 have_address = 1;
14437 }
14438 else
14439 {
14440 mode = GET_MODE (rtl);
14441 mem_mode = VOIDmode;
14442 if (MEM_P (rtl))
14443 {
14444 mem_mode = mode;
14445 mode = get_address_mode (rtl);
14446 rtl = XEXP (rtl, 0);
14447 have_address = 1;
14448 }
14449 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14450 VAR_INIT_STATUS_INITIALIZED);
14451 }
14452 if (!ret)
14453 expansion_failed (loc, rtl,
14454 "failed to produce loc descriptor for rtl");
14455 }
14456 }
14457 break;
14458
14459 case MEM_REF:
14460 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14461 {
14462 have_address = 1;
14463 goto do_plus;
14464 }
14465 /* Fallthru. */
14466 case INDIRECT_REF:
14467 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14468 have_address = 1;
14469 break;
14470
14471 case TARGET_MEM_REF:
14472 case SSA_NAME:
14473 return NULL;
14474
14475 case COMPOUND_EXPR:
14476 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14477
14478 CASE_CONVERT:
14479 case VIEW_CONVERT_EXPR:
14480 case SAVE_EXPR:
14481 case MODIFY_EXPR:
14482 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14483
14484 case COMPONENT_REF:
14485 case BIT_FIELD_REF:
14486 case ARRAY_REF:
14487 case ARRAY_RANGE_REF:
14488 case REALPART_EXPR:
14489 case IMAGPART_EXPR:
14490 {
14491 tree obj, offset;
14492 HOST_WIDE_INT bitsize, bitpos, bytepos;
14493 machine_mode mode;
14494 int unsignedp, volatilep = 0;
14495
14496 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14497 &unsignedp, &volatilep, false);
14498
14499 gcc_assert (obj != loc);
14500
14501 list_ret = loc_list_from_tree (obj,
14502 want_address == 2
14503 && !bitpos && !offset ? 2 : 1);
14504 /* TODO: We can extract value of the small expression via shifting even
14505 for nonzero bitpos. */
14506 if (list_ret == 0)
14507 return 0;
14508 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14509 {
14510 expansion_failed (loc, NULL_RTX,
14511 "bitfield access");
14512 return 0;
14513 }
14514
14515 if (offset != NULL_TREE)
14516 {
14517 /* Variable offset. */
14518 list_ret1 = loc_list_from_tree (offset, 0);
14519 if (list_ret1 == 0)
14520 return 0;
14521 add_loc_list (&list_ret, list_ret1);
14522 if (!list_ret)
14523 return 0;
14524 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14525 }
14526
14527 bytepos = bitpos / BITS_PER_UNIT;
14528 if (bytepos > 0)
14529 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14530 else if (bytepos < 0)
14531 loc_list_plus_const (list_ret, bytepos);
14532
14533 have_address = 1;
14534 break;
14535 }
14536
14537 case INTEGER_CST:
14538 if ((want_address || !tree_fits_shwi_p (loc))
14539 && (ret = cst_pool_loc_descr (loc)))
14540 have_address = 1;
14541 else if (want_address == 2
14542 && tree_fits_shwi_p (loc)
14543 && (ret = address_of_int_loc_descriptor
14544 (int_size_in_bytes (TREE_TYPE (loc)),
14545 tree_to_shwi (loc))))
14546 have_address = 1;
14547 else if (tree_fits_shwi_p (loc))
14548 ret = int_loc_descriptor (tree_to_shwi (loc));
14549 else
14550 {
14551 expansion_failed (loc, NULL_RTX,
14552 "Integer operand is not host integer");
14553 return 0;
14554 }
14555 break;
14556
14557 case CONSTRUCTOR:
14558 case REAL_CST:
14559 case STRING_CST:
14560 case COMPLEX_CST:
14561 if ((ret = cst_pool_loc_descr (loc)))
14562 have_address = 1;
14563 else
14564 /* We can construct small constants here using int_loc_descriptor. */
14565 expansion_failed (loc, NULL_RTX,
14566 "constructor or constant not in constant pool");
14567 break;
14568
14569 case TRUTH_AND_EXPR:
14570 case TRUTH_ANDIF_EXPR:
14571 case BIT_AND_EXPR:
14572 op = DW_OP_and;
14573 goto do_binop;
14574
14575 case TRUTH_XOR_EXPR:
14576 case BIT_XOR_EXPR:
14577 op = DW_OP_xor;
14578 goto do_binop;
14579
14580 case TRUTH_OR_EXPR:
14581 case TRUTH_ORIF_EXPR:
14582 case BIT_IOR_EXPR:
14583 op = DW_OP_or;
14584 goto do_binop;
14585
14586 case FLOOR_DIV_EXPR:
14587 case CEIL_DIV_EXPR:
14588 case ROUND_DIV_EXPR:
14589 case TRUNC_DIV_EXPR:
14590 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14591 return 0;
14592 op = DW_OP_div;
14593 goto do_binop;
14594
14595 case MINUS_EXPR:
14596 op = DW_OP_minus;
14597 goto do_binop;
14598
14599 case FLOOR_MOD_EXPR:
14600 case CEIL_MOD_EXPR:
14601 case ROUND_MOD_EXPR:
14602 case TRUNC_MOD_EXPR:
14603 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14604 {
14605 op = DW_OP_mod;
14606 goto do_binop;
14607 }
14608 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14609 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14610 if (list_ret == 0 || list_ret1 == 0)
14611 return 0;
14612
14613 add_loc_list (&list_ret, list_ret1);
14614 if (list_ret == 0)
14615 return 0;
14616 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14617 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14618 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14619 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14620 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14621 break;
14622
14623 case MULT_EXPR:
14624 op = DW_OP_mul;
14625 goto do_binop;
14626
14627 case LSHIFT_EXPR:
14628 op = DW_OP_shl;
14629 goto do_binop;
14630
14631 case RSHIFT_EXPR:
14632 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14633 goto do_binop;
14634
14635 case POINTER_PLUS_EXPR:
14636 case PLUS_EXPR:
14637 do_plus:
14638 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14639 {
14640 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14641 if (list_ret == 0)
14642 return 0;
14643
14644 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14645 break;
14646 }
14647
14648 op = DW_OP_plus;
14649 goto do_binop;
14650
14651 case LE_EXPR:
14652 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14653 return 0;
14654
14655 op = DW_OP_le;
14656 goto do_binop;
14657
14658 case GE_EXPR:
14659 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14660 return 0;
14661
14662 op = DW_OP_ge;
14663 goto do_binop;
14664
14665 case LT_EXPR:
14666 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14667 return 0;
14668
14669 op = DW_OP_lt;
14670 goto do_binop;
14671
14672 case GT_EXPR:
14673 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14674 return 0;
14675
14676 op = DW_OP_gt;
14677 goto do_binop;
14678
14679 case EQ_EXPR:
14680 op = DW_OP_eq;
14681 goto do_binop;
14682
14683 case NE_EXPR:
14684 op = DW_OP_ne;
14685 goto do_binop;
14686
14687 do_binop:
14688 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14689 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14690 if (list_ret == 0 || list_ret1 == 0)
14691 return 0;
14692
14693 add_loc_list (&list_ret, list_ret1);
14694 if (list_ret == 0)
14695 return 0;
14696 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14697 break;
14698
14699 case TRUTH_NOT_EXPR:
14700 case BIT_NOT_EXPR:
14701 op = DW_OP_not;
14702 goto do_unop;
14703
14704 case ABS_EXPR:
14705 op = DW_OP_abs;
14706 goto do_unop;
14707
14708 case NEGATE_EXPR:
14709 op = DW_OP_neg;
14710 goto do_unop;
14711
14712 do_unop:
14713 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14714 if (list_ret == 0)
14715 return 0;
14716
14717 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14718 break;
14719
14720 case MIN_EXPR:
14721 case MAX_EXPR:
14722 {
14723 const enum tree_code code =
14724 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14725
14726 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14727 build2 (code, integer_type_node,
14728 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14729 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14730 }
14731
14732 /* ... fall through ... */
14733
14734 case COND_EXPR:
14735 {
14736 dw_loc_descr_ref lhs
14737 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14738 dw_loc_list_ref rhs
14739 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14740 dw_loc_descr_ref bra_node, jump_node, tmp;
14741
14742 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14743 if (list_ret == 0 || lhs == 0 || rhs == 0)
14744 return 0;
14745
14746 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14747 add_loc_descr_to_each (list_ret, bra_node);
14748
14749 add_loc_list (&list_ret, rhs);
14750 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14751 add_loc_descr_to_each (list_ret, jump_node);
14752
14753 add_loc_descr_to_each (list_ret, lhs);
14754 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14755 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14756
14757 /* ??? Need a node to point the skip at. Use a nop. */
14758 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14759 add_loc_descr_to_each (list_ret, tmp);
14760 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14761 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14762 }
14763 break;
14764
14765 case FIX_TRUNC_EXPR:
14766 return 0;
14767
14768 default:
14769 /* Leave front-end specific codes as simply unknown. This comes
14770 up, for instance, with the C STMT_EXPR. */
14771 if ((unsigned int) TREE_CODE (loc)
14772 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14773 {
14774 expansion_failed (loc, NULL_RTX,
14775 "language specific tree node");
14776 return 0;
14777 }
14778
14779 #ifdef ENABLE_CHECKING
14780 /* Otherwise this is a generic code; we should just lists all of
14781 these explicitly. We forgot one. */
14782 gcc_unreachable ();
14783 #else
14784 /* In a release build, we want to degrade gracefully: better to
14785 generate incomplete debugging information than to crash. */
14786 return NULL;
14787 #endif
14788 }
14789
14790 if (!ret && !list_ret)
14791 return 0;
14792
14793 if (want_address == 2 && !have_address
14794 && (dwarf_version >= 4 || !dwarf_strict))
14795 {
14796 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14797 {
14798 expansion_failed (loc, NULL_RTX,
14799 "DWARF address size mismatch");
14800 return 0;
14801 }
14802 if (ret)
14803 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14804 else
14805 add_loc_descr_to_each (list_ret,
14806 new_loc_descr (DW_OP_stack_value, 0, 0));
14807 have_address = 1;
14808 }
14809 /* Show if we can't fill the request for an address. */
14810 if (want_address && !have_address)
14811 {
14812 expansion_failed (loc, NULL_RTX,
14813 "Want address and only have value");
14814 return 0;
14815 }
14816
14817 gcc_assert (!ret || !list_ret);
14818
14819 /* If we've got an address and don't want one, dereference. */
14820 if (!want_address && have_address)
14821 {
14822 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14823
14824 if (size > DWARF2_ADDR_SIZE || size == -1)
14825 {
14826 expansion_failed (loc, NULL_RTX,
14827 "DWARF address size mismatch");
14828 return 0;
14829 }
14830 else if (size == DWARF2_ADDR_SIZE)
14831 op = DW_OP_deref;
14832 else
14833 op = DW_OP_deref_size;
14834
14835 if (ret)
14836 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14837 else
14838 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14839 }
14840 if (ret)
14841 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14842
14843 return list_ret;
14844 }
14845
14846 /* Same as above but return only single location expression. */
14847 static dw_loc_descr_ref
14848 loc_descriptor_from_tree (tree loc, int want_address)
14849 {
14850 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14851 if (!ret)
14852 return NULL;
14853 if (ret->dw_loc_next)
14854 {
14855 expansion_failed (loc, NULL_RTX,
14856 "Location list where only loc descriptor needed");
14857 return NULL;
14858 }
14859 return ret->expr;
14860 }
14861
14862 /* Given a value, round it up to the lowest multiple of `boundary'
14863 which is not less than the value itself. */
14864
14865 static inline HOST_WIDE_INT
14866 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14867 {
14868 return (((value + boundary - 1) / boundary) * boundary);
14869 }
14870
14871 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14872 pointer to the declared type for the relevant field variable, or return
14873 `integer_type_node' if the given node turns out to be an
14874 ERROR_MARK node. */
14875
14876 static inline tree
14877 field_type (const_tree decl)
14878 {
14879 tree type;
14880
14881 if (TREE_CODE (decl) == ERROR_MARK)
14882 return integer_type_node;
14883
14884 type = DECL_BIT_FIELD_TYPE (decl);
14885 if (type == NULL_TREE)
14886 type = TREE_TYPE (decl);
14887
14888 return type;
14889 }
14890
14891 /* Given a pointer to a tree node, return the alignment in bits for
14892 it, or else return BITS_PER_WORD if the node actually turns out to
14893 be an ERROR_MARK node. */
14894
14895 static inline unsigned
14896 simple_type_align_in_bits (const_tree type)
14897 {
14898 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14899 }
14900
14901 static inline unsigned
14902 simple_decl_align_in_bits (const_tree decl)
14903 {
14904 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14905 }
14906
14907 /* Return the result of rounding T up to ALIGN. */
14908
14909 static inline offset_int
14910 round_up_to_align (const offset_int &t, unsigned int align)
14911 {
14912 return wi::udiv_trunc (t + align - 1, align) * align;
14913 }
14914
14915 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14916 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14917 or return 0 if we are unable to determine what that offset is, either
14918 because the argument turns out to be a pointer to an ERROR_MARK node, or
14919 because the offset is actually variable. (We can't handle the latter case
14920 just yet). */
14921
14922 static HOST_WIDE_INT
14923 field_byte_offset (const_tree decl)
14924 {
14925 offset_int object_offset_in_bits;
14926 offset_int object_offset_in_bytes;
14927 offset_int bitpos_int;
14928
14929 if (TREE_CODE (decl) == ERROR_MARK)
14930 return 0;
14931
14932 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14933
14934 /* We cannot yet cope with fields whose positions are variable, so
14935 for now, when we see such things, we simply return 0. Someday, we may
14936 be able to handle such cases, but it will be damn difficult. */
14937 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14938 return 0;
14939
14940 bitpos_int = wi::to_offset (bit_position (decl));
14941
14942 #ifdef PCC_BITFIELD_TYPE_MATTERS
14943 if (PCC_BITFIELD_TYPE_MATTERS)
14944 {
14945 tree type;
14946 tree field_size_tree;
14947 offset_int deepest_bitpos;
14948 offset_int field_size_in_bits;
14949 unsigned int type_align_in_bits;
14950 unsigned int decl_align_in_bits;
14951 offset_int type_size_in_bits;
14952
14953 type = field_type (decl);
14954 type_size_in_bits = offset_int_type_size_in_bits (type);
14955 type_align_in_bits = simple_type_align_in_bits (type);
14956
14957 field_size_tree = DECL_SIZE (decl);
14958
14959 /* The size could be unspecified if there was an error, or for
14960 a flexible array member. */
14961 if (!field_size_tree)
14962 field_size_tree = bitsize_zero_node;
14963
14964 /* If the size of the field is not constant, use the type size. */
14965 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14966 field_size_in_bits = wi::to_offset (field_size_tree);
14967 else
14968 field_size_in_bits = type_size_in_bits;
14969
14970 decl_align_in_bits = simple_decl_align_in_bits (decl);
14971
14972 /* The GCC front-end doesn't make any attempt to keep track of the
14973 starting bit offset (relative to the start of the containing
14974 structure type) of the hypothetical "containing object" for a
14975 bit-field. Thus, when computing the byte offset value for the
14976 start of the "containing object" of a bit-field, we must deduce
14977 this information on our own. This can be rather tricky to do in
14978 some cases. For example, handling the following structure type
14979 definition when compiling for an i386/i486 target (which only
14980 aligns long long's to 32-bit boundaries) can be very tricky:
14981
14982 struct S { int field1; long long field2:31; };
14983
14984 Fortunately, there is a simple rule-of-thumb which can be used
14985 in such cases. When compiling for an i386/i486, GCC will
14986 allocate 8 bytes for the structure shown above. It decides to
14987 do this based upon one simple rule for bit-field allocation.
14988 GCC allocates each "containing object" for each bit-field at
14989 the first (i.e. lowest addressed) legitimate alignment boundary
14990 (based upon the required minimum alignment for the declared
14991 type of the field) which it can possibly use, subject to the
14992 condition that there is still enough available space remaining
14993 in the containing object (when allocated at the selected point)
14994 to fully accommodate all of the bits of the bit-field itself.
14995
14996 This simple rule makes it obvious why GCC allocates 8 bytes for
14997 each object of the structure type shown above. When looking
14998 for a place to allocate the "containing object" for `field2',
14999 the compiler simply tries to allocate a 64-bit "containing
15000 object" at each successive 32-bit boundary (starting at zero)
15001 until it finds a place to allocate that 64- bit field such that
15002 at least 31 contiguous (and previously unallocated) bits remain
15003 within that selected 64 bit field. (As it turns out, for the
15004 example above, the compiler finds it is OK to allocate the
15005 "containing object" 64-bit field at bit-offset zero within the
15006 structure type.)
15007
15008 Here we attempt to work backwards from the limited set of facts
15009 we're given, and we try to deduce from those facts, where GCC
15010 must have believed that the containing object started (within
15011 the structure type). The value we deduce is then used (by the
15012 callers of this routine) to generate DW_AT_location and
15013 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15014 the case of DW_AT_location, regular fields as well). */
15015
15016 /* Figure out the bit-distance from the start of the structure to
15017 the "deepest" bit of the bit-field. */
15018 deepest_bitpos = bitpos_int + field_size_in_bits;
15019
15020 /* This is the tricky part. Use some fancy footwork to deduce
15021 where the lowest addressed bit of the containing object must
15022 be. */
15023 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15024
15025 /* Round up to type_align by default. This works best for
15026 bitfields. */
15027 object_offset_in_bits
15028 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15029
15030 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15031 {
15032 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15033
15034 /* Round up to decl_align instead. */
15035 object_offset_in_bits
15036 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15037 }
15038 }
15039 else
15040 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15041 object_offset_in_bits = bitpos_int;
15042
15043 object_offset_in_bytes
15044 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15045 return object_offset_in_bytes.to_shwi ();
15046 }
15047 \f
15048 /* The following routines define various Dwarf attributes and any data
15049 associated with them. */
15050
15051 /* Add a location description attribute value to a DIE.
15052
15053 This emits location attributes suitable for whole variables and
15054 whole parameters. Note that the location attributes for struct fields are
15055 generated by the routine `data_member_location_attribute' below. */
15056
15057 static inline void
15058 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15059 dw_loc_list_ref descr)
15060 {
15061 if (descr == 0)
15062 return;
15063 if (single_element_loc_list_p (descr))
15064 add_AT_loc (die, attr_kind, descr->expr);
15065 else
15066 add_AT_loc_list (die, attr_kind, descr);
15067 }
15068
15069 /* Add DW_AT_accessibility attribute to DIE if needed. */
15070
15071 static void
15072 add_accessibility_attribute (dw_die_ref die, tree decl)
15073 {
15074 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15075 children, otherwise the default is DW_ACCESS_public. In DWARF2
15076 the default has always been DW_ACCESS_public. */
15077 if (TREE_PROTECTED (decl))
15078 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15079 else if (TREE_PRIVATE (decl))
15080 {
15081 if (dwarf_version == 2
15082 || die->die_parent == NULL
15083 || die->die_parent->die_tag != DW_TAG_class_type)
15084 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15085 }
15086 else if (dwarf_version > 2
15087 && die->die_parent
15088 && die->die_parent->die_tag == DW_TAG_class_type)
15089 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15090 }
15091
15092 /* Attach the specialized form of location attribute used for data members of
15093 struct and union types. In the special case of a FIELD_DECL node which
15094 represents a bit-field, the "offset" part of this special location
15095 descriptor must indicate the distance in bytes from the lowest-addressed
15096 byte of the containing struct or union type to the lowest-addressed byte of
15097 the "containing object" for the bit-field. (See the `field_byte_offset'
15098 function above).
15099
15100 For any given bit-field, the "containing object" is a hypothetical object
15101 (of some integral or enum type) within which the given bit-field lives. The
15102 type of this hypothetical "containing object" is always the same as the
15103 declared type of the individual bit-field itself (for GCC anyway... the
15104 DWARF spec doesn't actually mandate this). Note that it is the size (in
15105 bytes) of the hypothetical "containing object" which will be given in the
15106 DW_AT_byte_size attribute for this bit-field. (See the
15107 `byte_size_attribute' function below.) It is also used when calculating the
15108 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15109 function below.) */
15110
15111 static void
15112 add_data_member_location_attribute (dw_die_ref die, tree decl)
15113 {
15114 HOST_WIDE_INT offset;
15115 dw_loc_descr_ref loc_descr = 0;
15116
15117 if (TREE_CODE (decl) == TREE_BINFO)
15118 {
15119 /* We're working on the TAG_inheritance for a base class. */
15120 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15121 {
15122 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15123 aren't at a fixed offset from all (sub)objects of the same
15124 type. We need to extract the appropriate offset from our
15125 vtable. The following dwarf expression means
15126
15127 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15128
15129 This is specific to the V3 ABI, of course. */
15130
15131 dw_loc_descr_ref tmp;
15132
15133 /* Make a copy of the object address. */
15134 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15135 add_loc_descr (&loc_descr, tmp);
15136
15137 /* Extract the vtable address. */
15138 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15139 add_loc_descr (&loc_descr, tmp);
15140
15141 /* Calculate the address of the offset. */
15142 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15143 gcc_assert (offset < 0);
15144
15145 tmp = int_loc_descriptor (-offset);
15146 add_loc_descr (&loc_descr, tmp);
15147 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15148 add_loc_descr (&loc_descr, tmp);
15149
15150 /* Extract the offset. */
15151 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15152 add_loc_descr (&loc_descr, tmp);
15153
15154 /* Add it to the object address. */
15155 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15156 add_loc_descr (&loc_descr, tmp);
15157 }
15158 else
15159 offset = tree_to_shwi (BINFO_OFFSET (decl));
15160 }
15161 else
15162 offset = field_byte_offset (decl);
15163
15164 if (! loc_descr)
15165 {
15166 if (dwarf_version > 2)
15167 {
15168 /* Don't need to output a location expression, just the constant. */
15169 if (offset < 0)
15170 add_AT_int (die, DW_AT_data_member_location, offset);
15171 else
15172 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15173 return;
15174 }
15175 else
15176 {
15177 enum dwarf_location_atom op;
15178
15179 /* The DWARF2 standard says that we should assume that the structure
15180 address is already on the stack, so we can specify a structure
15181 field address by using DW_OP_plus_uconst. */
15182 op = DW_OP_plus_uconst;
15183 loc_descr = new_loc_descr (op, offset, 0);
15184 }
15185 }
15186
15187 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15188 }
15189
15190 /* Writes integer values to dw_vec_const array. */
15191
15192 static void
15193 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15194 {
15195 while (size != 0)
15196 {
15197 *dest++ = val & 0xff;
15198 val >>= 8;
15199 --size;
15200 }
15201 }
15202
15203 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15204
15205 static HOST_WIDE_INT
15206 extract_int (const unsigned char *src, unsigned int size)
15207 {
15208 HOST_WIDE_INT val = 0;
15209
15210 src += size;
15211 while (size != 0)
15212 {
15213 val <<= 8;
15214 val |= *--src & 0xff;
15215 --size;
15216 }
15217 return val;
15218 }
15219
15220 /* Writes wide_int values to dw_vec_const array. */
15221
15222 static void
15223 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15224 {
15225 int i;
15226
15227 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15228 {
15229 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15230 return;
15231 }
15232
15233 /* We'd have to extend this code to support odd sizes. */
15234 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15235
15236 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15237
15238 if (WORDS_BIG_ENDIAN)
15239 for (i = n - 1; i >= 0; i--)
15240 {
15241 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15242 dest += sizeof (HOST_WIDE_INT);
15243 }
15244 else
15245 for (i = 0; i < n; i++)
15246 {
15247 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15248 dest += sizeof (HOST_WIDE_INT);
15249 }
15250 }
15251
15252 /* Writes floating point values to dw_vec_const array. */
15253
15254 static void
15255 insert_float (const_rtx rtl, unsigned char *array)
15256 {
15257 REAL_VALUE_TYPE rv;
15258 long val[4];
15259 int i;
15260
15261 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15262 real_to_target (val, &rv, GET_MODE (rtl));
15263
15264 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15265 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15266 {
15267 insert_int (val[i], 4, array);
15268 array += 4;
15269 }
15270 }
15271
15272 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15273 does not have a "location" either in memory or in a register. These
15274 things can arise in GNU C when a constant is passed as an actual parameter
15275 to an inlined function. They can also arise in C++ where declared
15276 constants do not necessarily get memory "homes". */
15277
15278 static bool
15279 add_const_value_attribute (dw_die_ref die, rtx rtl)
15280 {
15281 switch (GET_CODE (rtl))
15282 {
15283 case CONST_INT:
15284 {
15285 HOST_WIDE_INT val = INTVAL (rtl);
15286
15287 if (val < 0)
15288 add_AT_int (die, DW_AT_const_value, val);
15289 else
15290 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15291 }
15292 return true;
15293
15294 case CONST_WIDE_INT:
15295 add_AT_wide (die, DW_AT_const_value,
15296 std::make_pair (rtl, GET_MODE (rtl)));
15297 return true;
15298
15299 case CONST_DOUBLE:
15300 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15301 floating-point constant. A CONST_DOUBLE is used whenever the
15302 constant requires more than one word in order to be adequately
15303 represented. */
15304 {
15305 machine_mode mode = GET_MODE (rtl);
15306
15307 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15308 add_AT_double (die, DW_AT_const_value,
15309 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15310 else
15311 {
15312 unsigned int length = GET_MODE_SIZE (mode);
15313 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15314
15315 insert_float (rtl, array);
15316 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15317 }
15318 }
15319 return true;
15320
15321 case CONST_VECTOR:
15322 {
15323 machine_mode mode = GET_MODE (rtl);
15324 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15325 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15326 unsigned char *array
15327 = ggc_vec_alloc<unsigned char> (length * elt_size);
15328 unsigned int i;
15329 unsigned char *p;
15330 machine_mode imode = GET_MODE_INNER (mode);
15331
15332 switch (GET_MODE_CLASS (mode))
15333 {
15334 case MODE_VECTOR_INT:
15335 for (i = 0, p = array; i < length; i++, p += elt_size)
15336 {
15337 rtx elt = CONST_VECTOR_ELT (rtl, i);
15338 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15339 }
15340 break;
15341
15342 case MODE_VECTOR_FLOAT:
15343 for (i = 0, p = array; i < length; i++, p += elt_size)
15344 {
15345 rtx elt = CONST_VECTOR_ELT (rtl, i);
15346 insert_float (elt, p);
15347 }
15348 break;
15349
15350 default:
15351 gcc_unreachable ();
15352 }
15353
15354 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15355 }
15356 return true;
15357
15358 case CONST_STRING:
15359 if (dwarf_version >= 4 || !dwarf_strict)
15360 {
15361 dw_loc_descr_ref loc_result;
15362 resolve_one_addr (&rtl);
15363 rtl_addr:
15364 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15365 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15366 add_AT_loc (die, DW_AT_location, loc_result);
15367 vec_safe_push (used_rtx_array, rtl);
15368 return true;
15369 }
15370 return false;
15371
15372 case CONST:
15373 if (CONSTANT_P (XEXP (rtl, 0)))
15374 return add_const_value_attribute (die, XEXP (rtl, 0));
15375 /* FALLTHROUGH */
15376 case SYMBOL_REF:
15377 if (!const_ok_for_output (rtl))
15378 return false;
15379 case LABEL_REF:
15380 if (dwarf_version >= 4 || !dwarf_strict)
15381 goto rtl_addr;
15382 return false;
15383
15384 case PLUS:
15385 /* In cases where an inlined instance of an inline function is passed
15386 the address of an `auto' variable (which is local to the caller) we
15387 can get a situation where the DECL_RTL of the artificial local
15388 variable (for the inlining) which acts as a stand-in for the
15389 corresponding formal parameter (of the inline function) will look
15390 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15391 exactly a compile-time constant expression, but it isn't the address
15392 of the (artificial) local variable either. Rather, it represents the
15393 *value* which the artificial local variable always has during its
15394 lifetime. We currently have no way to represent such quasi-constant
15395 values in Dwarf, so for now we just punt and generate nothing. */
15396 return false;
15397
15398 case HIGH:
15399 case CONST_FIXED:
15400 return false;
15401
15402 case MEM:
15403 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15404 && MEM_READONLY_P (rtl)
15405 && GET_MODE (rtl) == BLKmode)
15406 {
15407 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15408 return true;
15409 }
15410 return false;
15411
15412 default:
15413 /* No other kinds of rtx should be possible here. */
15414 gcc_unreachable ();
15415 }
15416 return false;
15417 }
15418
15419 /* Determine whether the evaluation of EXPR references any variables
15420 or functions which aren't otherwise used (and therefore may not be
15421 output). */
15422 static tree
15423 reference_to_unused (tree * tp, int * walk_subtrees,
15424 void * data ATTRIBUTE_UNUSED)
15425 {
15426 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15427 *walk_subtrees = 0;
15428
15429 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15430 && ! TREE_ASM_WRITTEN (*tp))
15431 return *tp;
15432 /* ??? The C++ FE emits debug information for using decls, so
15433 putting gcc_unreachable here falls over. See PR31899. For now
15434 be conservative. */
15435 else if (!symtab->global_info_ready
15436 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15437 return *tp;
15438 else if (TREE_CODE (*tp) == VAR_DECL)
15439 {
15440 varpool_node *node = varpool_node::get (*tp);
15441 if (!node || !node->definition)
15442 return *tp;
15443 }
15444 else if (TREE_CODE (*tp) == FUNCTION_DECL
15445 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15446 {
15447 /* The call graph machinery must have finished analyzing,
15448 optimizing and gimplifying the CU by now.
15449 So if *TP has no call graph node associated
15450 to it, it means *TP will not be emitted. */
15451 if (!cgraph_node::get (*tp))
15452 return *tp;
15453 }
15454 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15455 return *tp;
15456
15457 return NULL_TREE;
15458 }
15459
15460 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15461 for use in a later add_const_value_attribute call. */
15462
15463 static rtx
15464 rtl_for_decl_init (tree init, tree type)
15465 {
15466 rtx rtl = NULL_RTX;
15467
15468 STRIP_NOPS (init);
15469
15470 /* If a variable is initialized with a string constant without embedded
15471 zeros, build CONST_STRING. */
15472 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15473 {
15474 tree enttype = TREE_TYPE (type);
15475 tree domain = TYPE_DOMAIN (type);
15476 machine_mode mode = TYPE_MODE (enttype);
15477
15478 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15479 && domain
15480 && integer_zerop (TYPE_MIN_VALUE (domain))
15481 && compare_tree_int (TYPE_MAX_VALUE (domain),
15482 TREE_STRING_LENGTH (init) - 1) == 0
15483 && ((size_t) TREE_STRING_LENGTH (init)
15484 == strlen (TREE_STRING_POINTER (init)) + 1))
15485 {
15486 rtl = gen_rtx_CONST_STRING (VOIDmode,
15487 ggc_strdup (TREE_STRING_POINTER (init)));
15488 rtl = gen_rtx_MEM (BLKmode, rtl);
15489 MEM_READONLY_P (rtl) = 1;
15490 }
15491 }
15492 /* Other aggregates, and complex values, could be represented using
15493 CONCAT: FIXME! */
15494 else if (AGGREGATE_TYPE_P (type)
15495 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15496 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15497 || TREE_CODE (type) == COMPLEX_TYPE)
15498 ;
15499 /* Vectors only work if their mode is supported by the target.
15500 FIXME: generic vectors ought to work too. */
15501 else if (TREE_CODE (type) == VECTOR_TYPE
15502 && !VECTOR_MODE_P (TYPE_MODE (type)))
15503 ;
15504 /* If the initializer is something that we know will expand into an
15505 immediate RTL constant, expand it now. We must be careful not to
15506 reference variables which won't be output. */
15507 else if (initializer_constant_valid_p (init, type)
15508 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15509 {
15510 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15511 possible. */
15512 if (TREE_CODE (type) == VECTOR_TYPE)
15513 switch (TREE_CODE (init))
15514 {
15515 case VECTOR_CST:
15516 break;
15517 case CONSTRUCTOR:
15518 if (TREE_CONSTANT (init))
15519 {
15520 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15521 bool constant_p = true;
15522 tree value;
15523 unsigned HOST_WIDE_INT ix;
15524
15525 /* Even when ctor is constant, it might contain non-*_CST
15526 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15527 belong into VECTOR_CST nodes. */
15528 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15529 if (!CONSTANT_CLASS_P (value))
15530 {
15531 constant_p = false;
15532 break;
15533 }
15534
15535 if (constant_p)
15536 {
15537 init = build_vector_from_ctor (type, elts);
15538 break;
15539 }
15540 }
15541 /* FALLTHRU */
15542
15543 default:
15544 return NULL;
15545 }
15546
15547 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15548
15549 /* If expand_expr returns a MEM, it wasn't immediate. */
15550 gcc_assert (!rtl || !MEM_P (rtl));
15551 }
15552
15553 return rtl;
15554 }
15555
15556 /* Generate RTL for the variable DECL to represent its location. */
15557
15558 static rtx
15559 rtl_for_decl_location (tree decl)
15560 {
15561 rtx rtl;
15562
15563 /* Here we have to decide where we are going to say the parameter "lives"
15564 (as far as the debugger is concerned). We only have a couple of
15565 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15566
15567 DECL_RTL normally indicates where the parameter lives during most of the
15568 activation of the function. If optimization is enabled however, this
15569 could be either NULL or else a pseudo-reg. Both of those cases indicate
15570 that the parameter doesn't really live anywhere (as far as the code
15571 generation parts of GCC are concerned) during most of the function's
15572 activation. That will happen (for example) if the parameter is never
15573 referenced within the function.
15574
15575 We could just generate a location descriptor here for all non-NULL
15576 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15577 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15578 where DECL_RTL is NULL or is a pseudo-reg.
15579
15580 Note however that we can only get away with using DECL_INCOMING_RTL as
15581 a backup substitute for DECL_RTL in certain limited cases. In cases
15582 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15583 we can be sure that the parameter was passed using the same type as it is
15584 declared to have within the function, and that its DECL_INCOMING_RTL
15585 points us to a place where a value of that type is passed.
15586
15587 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15588 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15589 because in these cases DECL_INCOMING_RTL points us to a value of some
15590 type which is *different* from the type of the parameter itself. Thus,
15591 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15592 such cases, the debugger would end up (for example) trying to fetch a
15593 `float' from a place which actually contains the first part of a
15594 `double'. That would lead to really incorrect and confusing
15595 output at debug-time.
15596
15597 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15598 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15599 are a couple of exceptions however. On little-endian machines we can
15600 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15601 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15602 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15603 when (on a little-endian machine) a non-prototyped function has a
15604 parameter declared to be of type `short' or `char'. In such cases,
15605 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15606 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15607 passed `int' value. If the debugger then uses that address to fetch
15608 a `short' or a `char' (on a little-endian machine) the result will be
15609 the correct data, so we allow for such exceptional cases below.
15610
15611 Note that our goal here is to describe the place where the given formal
15612 parameter lives during most of the function's activation (i.e. between the
15613 end of the prologue and the start of the epilogue). We'll do that as best
15614 as we can. Note however that if the given formal parameter is modified
15615 sometime during the execution of the function, then a stack backtrace (at
15616 debug-time) will show the function as having been called with the *new*
15617 value rather than the value which was originally passed in. This happens
15618 rarely enough that it is not a major problem, but it *is* a problem, and
15619 I'd like to fix it.
15620
15621 A future version of dwarf2out.c may generate two additional attributes for
15622 any given DW_TAG_formal_parameter DIE which will describe the "passed
15623 type" and the "passed location" for the given formal parameter in addition
15624 to the attributes we now generate to indicate the "declared type" and the
15625 "active location" for each parameter. This additional set of attributes
15626 could be used by debuggers for stack backtraces. Separately, note that
15627 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15628 This happens (for example) for inlined-instances of inline function formal
15629 parameters which are never referenced. This really shouldn't be
15630 happening. All PARM_DECL nodes should get valid non-NULL
15631 DECL_INCOMING_RTL values. FIXME. */
15632
15633 /* Use DECL_RTL as the "location" unless we find something better. */
15634 rtl = DECL_RTL_IF_SET (decl);
15635
15636 /* When generating abstract instances, ignore everything except
15637 constants, symbols living in memory, and symbols living in
15638 fixed registers. */
15639 if (! reload_completed)
15640 {
15641 if (rtl
15642 && (CONSTANT_P (rtl)
15643 || (MEM_P (rtl)
15644 && CONSTANT_P (XEXP (rtl, 0)))
15645 || (REG_P (rtl)
15646 && TREE_CODE (decl) == VAR_DECL
15647 && TREE_STATIC (decl))))
15648 {
15649 rtl = targetm.delegitimize_address (rtl);
15650 return rtl;
15651 }
15652 rtl = NULL_RTX;
15653 }
15654 else if (TREE_CODE (decl) == PARM_DECL)
15655 {
15656 if (rtl == NULL_RTX
15657 || is_pseudo_reg (rtl)
15658 || (MEM_P (rtl)
15659 && is_pseudo_reg (XEXP (rtl, 0))
15660 && DECL_INCOMING_RTL (decl)
15661 && MEM_P (DECL_INCOMING_RTL (decl))
15662 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15663 {
15664 tree declared_type = TREE_TYPE (decl);
15665 tree passed_type = DECL_ARG_TYPE (decl);
15666 machine_mode dmode = TYPE_MODE (declared_type);
15667 machine_mode pmode = TYPE_MODE (passed_type);
15668
15669 /* This decl represents a formal parameter which was optimized out.
15670 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15671 all cases where (rtl == NULL_RTX) just below. */
15672 if (dmode == pmode)
15673 rtl = DECL_INCOMING_RTL (decl);
15674 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15675 && SCALAR_INT_MODE_P (dmode)
15676 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15677 && DECL_INCOMING_RTL (decl))
15678 {
15679 rtx inc = DECL_INCOMING_RTL (decl);
15680 if (REG_P (inc))
15681 rtl = inc;
15682 else if (MEM_P (inc))
15683 {
15684 if (BYTES_BIG_ENDIAN)
15685 rtl = adjust_address_nv (inc, dmode,
15686 GET_MODE_SIZE (pmode)
15687 - GET_MODE_SIZE (dmode));
15688 else
15689 rtl = inc;
15690 }
15691 }
15692 }
15693
15694 /* If the parm was passed in registers, but lives on the stack, then
15695 make a big endian correction if the mode of the type of the
15696 parameter is not the same as the mode of the rtl. */
15697 /* ??? This is the same series of checks that are made in dbxout.c before
15698 we reach the big endian correction code there. It isn't clear if all
15699 of these checks are necessary here, but keeping them all is the safe
15700 thing to do. */
15701 else if (MEM_P (rtl)
15702 && XEXP (rtl, 0) != const0_rtx
15703 && ! CONSTANT_P (XEXP (rtl, 0))
15704 /* Not passed in memory. */
15705 && !MEM_P (DECL_INCOMING_RTL (decl))
15706 /* Not passed by invisible reference. */
15707 && (!REG_P (XEXP (rtl, 0))
15708 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15709 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15710 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15711 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15712 #endif
15713 )
15714 /* Big endian correction check. */
15715 && BYTES_BIG_ENDIAN
15716 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15717 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15718 < UNITS_PER_WORD))
15719 {
15720 machine_mode addr_mode = get_address_mode (rtl);
15721 int offset = (UNITS_PER_WORD
15722 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15723
15724 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15725 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15726 }
15727 }
15728 else if (TREE_CODE (decl) == VAR_DECL
15729 && rtl
15730 && MEM_P (rtl)
15731 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15732 && BYTES_BIG_ENDIAN)
15733 {
15734 machine_mode addr_mode = get_address_mode (rtl);
15735 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15736 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15737
15738 /* If a variable is declared "register" yet is smaller than
15739 a register, then if we store the variable to memory, it
15740 looks like we're storing a register-sized value, when in
15741 fact we are not. We need to adjust the offset of the
15742 storage location to reflect the actual value's bytes,
15743 else gdb will not be able to display it. */
15744 if (rsize > dsize)
15745 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15746 plus_constant (addr_mode, XEXP (rtl, 0),
15747 rsize - dsize));
15748 }
15749
15750 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15751 and will have been substituted directly into all expressions that use it.
15752 C does not have such a concept, but C++ and other languages do. */
15753 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15754 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15755
15756 if (rtl)
15757 rtl = targetm.delegitimize_address (rtl);
15758
15759 /* If we don't look past the constant pool, we risk emitting a
15760 reference to a constant pool entry that isn't referenced from
15761 code, and thus is not emitted. */
15762 if (rtl)
15763 rtl = avoid_constant_pool_reference (rtl);
15764
15765 /* Try harder to get a rtl. If this symbol ends up not being emitted
15766 in the current CU, resolve_addr will remove the expression referencing
15767 it. */
15768 if (rtl == NULL_RTX
15769 && TREE_CODE (decl) == VAR_DECL
15770 && !DECL_EXTERNAL (decl)
15771 && TREE_STATIC (decl)
15772 && DECL_NAME (decl)
15773 && !DECL_HARD_REGISTER (decl)
15774 && DECL_MODE (decl) != VOIDmode)
15775 {
15776 rtl = make_decl_rtl_for_debug (decl);
15777 if (!MEM_P (rtl)
15778 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15779 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15780 rtl = NULL_RTX;
15781 }
15782
15783 return rtl;
15784 }
15785
15786 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15787 returned. If so, the decl for the COMMON block is returned, and the
15788 value is the offset into the common block for the symbol. */
15789
15790 static tree
15791 fortran_common (tree decl, HOST_WIDE_INT *value)
15792 {
15793 tree val_expr, cvar;
15794 machine_mode mode;
15795 HOST_WIDE_INT bitsize, bitpos;
15796 tree offset;
15797 int unsignedp, volatilep = 0;
15798
15799 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15800 it does not have a value (the offset into the common area), or if it
15801 is thread local (as opposed to global) then it isn't common, and shouldn't
15802 be handled as such. */
15803 if (TREE_CODE (decl) != VAR_DECL
15804 || !TREE_STATIC (decl)
15805 || !DECL_HAS_VALUE_EXPR_P (decl)
15806 || !is_fortran ())
15807 return NULL_TREE;
15808
15809 val_expr = DECL_VALUE_EXPR (decl);
15810 if (TREE_CODE (val_expr) != COMPONENT_REF)
15811 return NULL_TREE;
15812
15813 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15814 &mode, &unsignedp, &volatilep, true);
15815
15816 if (cvar == NULL_TREE
15817 || TREE_CODE (cvar) != VAR_DECL
15818 || DECL_ARTIFICIAL (cvar)
15819 || !TREE_PUBLIC (cvar))
15820 return NULL_TREE;
15821
15822 *value = 0;
15823 if (offset != NULL)
15824 {
15825 if (!tree_fits_shwi_p (offset))
15826 return NULL_TREE;
15827 *value = tree_to_shwi (offset);
15828 }
15829 if (bitpos != 0)
15830 *value += bitpos / BITS_PER_UNIT;
15831
15832 return cvar;
15833 }
15834
15835 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15836 data attribute for a variable or a parameter. We generate the
15837 DW_AT_const_value attribute only in those cases where the given variable
15838 or parameter does not have a true "location" either in memory or in a
15839 register. This can happen (for example) when a constant is passed as an
15840 actual argument in a call to an inline function. (It's possible that
15841 these things can crop up in other ways also.) Note that one type of
15842 constant value which can be passed into an inlined function is a constant
15843 pointer. This can happen for example if an actual argument in an inlined
15844 function call evaluates to a compile-time constant address.
15845
15846 CACHE_P is true if it is worth caching the location list for DECL,
15847 so that future calls can reuse it rather than regenerate it from scratch.
15848 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15849 since we will need to refer to them each time the function is inlined. */
15850
15851 static bool
15852 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15853 enum dwarf_attribute attr)
15854 {
15855 rtx rtl;
15856 dw_loc_list_ref list;
15857 var_loc_list *loc_list;
15858 cached_dw_loc_list *cache;
15859
15860 if (TREE_CODE (decl) == ERROR_MARK)
15861 return false;
15862
15863 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15864 || TREE_CODE (decl) == RESULT_DECL);
15865
15866 /* Try to get some constant RTL for this decl, and use that as the value of
15867 the location. */
15868
15869 rtl = rtl_for_decl_location (decl);
15870 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15871 && add_const_value_attribute (die, rtl))
15872 return true;
15873
15874 /* See if we have single element location list that is equivalent to
15875 a constant value. That way we are better to use add_const_value_attribute
15876 rather than expanding constant value equivalent. */
15877 loc_list = lookup_decl_loc (decl);
15878 if (loc_list
15879 && loc_list->first
15880 && loc_list->first->next == NULL
15881 && NOTE_P (loc_list->first->loc)
15882 && NOTE_VAR_LOCATION (loc_list->first->loc)
15883 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15884 {
15885 struct var_loc_node *node;
15886
15887 node = loc_list->first;
15888 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15889 if (GET_CODE (rtl) == EXPR_LIST)
15890 rtl = XEXP (rtl, 0);
15891 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15892 && add_const_value_attribute (die, rtl))
15893 return true;
15894 }
15895 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15896 list several times. See if we've already cached the contents. */
15897 list = NULL;
15898 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15899 cache_p = false;
15900 if (cache_p)
15901 {
15902 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
15903 if (cache)
15904 list = cache->loc_list;
15905 }
15906 if (list == NULL)
15907 {
15908 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15909 /* It is usually worth caching this result if the decl is from
15910 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15911 if (cache_p && list && list->dw_loc_next)
15912 {
15913 cached_dw_loc_list **slot
15914 = cached_dw_loc_list_table->find_slot_with_hash (decl,
15915 DECL_UID (decl),
15916 INSERT);
15917 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15918 cache->decl_id = DECL_UID (decl);
15919 cache->loc_list = list;
15920 *slot = cache;
15921 }
15922 }
15923 if (list)
15924 {
15925 add_AT_location_description (die, attr, list);
15926 return true;
15927 }
15928 /* None of that worked, so it must not really have a location;
15929 try adding a constant value attribute from the DECL_INITIAL. */
15930 return tree_add_const_value_attribute_for_decl (die, decl);
15931 }
15932
15933 /* Add VARIABLE and DIE into deferred locations list. */
15934
15935 static void
15936 defer_location (tree variable, dw_die_ref die)
15937 {
15938 deferred_locations entry;
15939 entry.variable = variable;
15940 entry.die = die;
15941 vec_safe_push (deferred_locations_list, entry);
15942 }
15943
15944 /* Helper function for tree_add_const_value_attribute. Natively encode
15945 initializer INIT into an array. Return true if successful. */
15946
15947 static bool
15948 native_encode_initializer (tree init, unsigned char *array, int size)
15949 {
15950 tree type;
15951
15952 if (init == NULL_TREE)
15953 return false;
15954
15955 STRIP_NOPS (init);
15956 switch (TREE_CODE (init))
15957 {
15958 case STRING_CST:
15959 type = TREE_TYPE (init);
15960 if (TREE_CODE (type) == ARRAY_TYPE)
15961 {
15962 tree enttype = TREE_TYPE (type);
15963 machine_mode mode = TYPE_MODE (enttype);
15964
15965 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15966 return false;
15967 if (int_size_in_bytes (type) != size)
15968 return false;
15969 if (size > TREE_STRING_LENGTH (init))
15970 {
15971 memcpy (array, TREE_STRING_POINTER (init),
15972 TREE_STRING_LENGTH (init));
15973 memset (array + TREE_STRING_LENGTH (init),
15974 '\0', size - TREE_STRING_LENGTH (init));
15975 }
15976 else
15977 memcpy (array, TREE_STRING_POINTER (init), size);
15978 return true;
15979 }
15980 return false;
15981 case CONSTRUCTOR:
15982 type = TREE_TYPE (init);
15983 if (int_size_in_bytes (type) != size)
15984 return false;
15985 if (TREE_CODE (type) == ARRAY_TYPE)
15986 {
15987 HOST_WIDE_INT min_index;
15988 unsigned HOST_WIDE_INT cnt;
15989 int curpos = 0, fieldsize;
15990 constructor_elt *ce;
15991
15992 if (TYPE_DOMAIN (type) == NULL_TREE
15993 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15994 return false;
15995
15996 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15997 if (fieldsize <= 0)
15998 return false;
15999
16000 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16001 memset (array, '\0', size);
16002 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16003 {
16004 tree val = ce->value;
16005 tree index = ce->index;
16006 int pos = curpos;
16007 if (index && TREE_CODE (index) == RANGE_EXPR)
16008 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16009 * fieldsize;
16010 else if (index)
16011 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16012
16013 if (val)
16014 {
16015 STRIP_NOPS (val);
16016 if (!native_encode_initializer (val, array + pos, fieldsize))
16017 return false;
16018 }
16019 curpos = pos + fieldsize;
16020 if (index && TREE_CODE (index) == RANGE_EXPR)
16021 {
16022 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16023 - tree_to_shwi (TREE_OPERAND (index, 0));
16024 while (count-- > 0)
16025 {
16026 if (val)
16027 memcpy (array + curpos, array + pos, fieldsize);
16028 curpos += fieldsize;
16029 }
16030 }
16031 gcc_assert (curpos <= size);
16032 }
16033 return true;
16034 }
16035 else if (TREE_CODE (type) == RECORD_TYPE
16036 || TREE_CODE (type) == UNION_TYPE)
16037 {
16038 tree field = NULL_TREE;
16039 unsigned HOST_WIDE_INT cnt;
16040 constructor_elt *ce;
16041
16042 if (int_size_in_bytes (type) != size)
16043 return false;
16044
16045 if (TREE_CODE (type) == RECORD_TYPE)
16046 field = TYPE_FIELDS (type);
16047
16048 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16049 {
16050 tree val = ce->value;
16051 int pos, fieldsize;
16052
16053 if (ce->index != 0)
16054 field = ce->index;
16055
16056 if (val)
16057 STRIP_NOPS (val);
16058
16059 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16060 return false;
16061
16062 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16063 && TYPE_DOMAIN (TREE_TYPE (field))
16064 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16065 return false;
16066 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16067 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16068 return false;
16069 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16070 pos = int_byte_position (field);
16071 gcc_assert (pos + fieldsize <= size);
16072 if (val
16073 && !native_encode_initializer (val, array + pos, fieldsize))
16074 return false;
16075 }
16076 return true;
16077 }
16078 return false;
16079 case VIEW_CONVERT_EXPR:
16080 case NON_LVALUE_EXPR:
16081 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16082 default:
16083 return native_encode_expr (init, array, size) == size;
16084 }
16085 }
16086
16087 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16088 attribute is the const value T. */
16089
16090 static bool
16091 tree_add_const_value_attribute (dw_die_ref die, tree t)
16092 {
16093 tree init;
16094 tree type = TREE_TYPE (t);
16095 rtx rtl;
16096
16097 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16098 return false;
16099
16100 init = t;
16101 gcc_assert (!DECL_P (init));
16102
16103 rtl = rtl_for_decl_init (init, type);
16104 if (rtl)
16105 return add_const_value_attribute (die, rtl);
16106 /* If the host and target are sane, try harder. */
16107 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16108 && initializer_constant_valid_p (init, type))
16109 {
16110 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16111 if (size > 0 && (int) size == size)
16112 {
16113 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16114
16115 if (native_encode_initializer (init, array, size))
16116 {
16117 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16118 return true;
16119 }
16120 ggc_free (array);
16121 }
16122 }
16123 return false;
16124 }
16125
16126 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16127 attribute is the const value of T, where T is an integral constant
16128 variable with static storage duration
16129 (so it can't be a PARM_DECL or a RESULT_DECL). */
16130
16131 static bool
16132 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16133 {
16134
16135 if (!decl
16136 || (TREE_CODE (decl) != VAR_DECL
16137 && TREE_CODE (decl) != CONST_DECL)
16138 || (TREE_CODE (decl) == VAR_DECL
16139 && !TREE_STATIC (decl)))
16140 return false;
16141
16142 if (TREE_READONLY (decl)
16143 && ! TREE_THIS_VOLATILE (decl)
16144 && DECL_INITIAL (decl))
16145 /* OK */;
16146 else
16147 return false;
16148
16149 /* Don't add DW_AT_const_value if abstract origin already has one. */
16150 if (get_AT (var_die, DW_AT_const_value))
16151 return false;
16152
16153 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16154 }
16155
16156 /* Convert the CFI instructions for the current function into a
16157 location list. This is used for DW_AT_frame_base when we targeting
16158 a dwarf2 consumer that does not support the dwarf3
16159 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16160 expressions. */
16161
16162 static dw_loc_list_ref
16163 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16164 {
16165 int ix;
16166 dw_fde_ref fde;
16167 dw_loc_list_ref list, *list_tail;
16168 dw_cfi_ref cfi;
16169 dw_cfa_location last_cfa, next_cfa;
16170 const char *start_label, *last_label, *section;
16171 dw_cfa_location remember;
16172
16173 fde = cfun->fde;
16174 gcc_assert (fde != NULL);
16175
16176 section = secname_for_decl (current_function_decl);
16177 list_tail = &list;
16178 list = NULL;
16179
16180 memset (&next_cfa, 0, sizeof (next_cfa));
16181 next_cfa.reg = INVALID_REGNUM;
16182 remember = next_cfa;
16183
16184 start_label = fde->dw_fde_begin;
16185
16186 /* ??? Bald assumption that the CIE opcode list does not contain
16187 advance opcodes. */
16188 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16189 lookup_cfa_1 (cfi, &next_cfa, &remember);
16190
16191 last_cfa = next_cfa;
16192 last_label = start_label;
16193
16194 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16195 {
16196 /* If the first partition contained no CFI adjustments, the
16197 CIE opcodes apply to the whole first partition. */
16198 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16199 fde->dw_fde_begin, fde->dw_fde_end, section);
16200 list_tail =&(*list_tail)->dw_loc_next;
16201 start_label = last_label = fde->dw_fde_second_begin;
16202 }
16203
16204 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16205 {
16206 switch (cfi->dw_cfi_opc)
16207 {
16208 case DW_CFA_set_loc:
16209 case DW_CFA_advance_loc1:
16210 case DW_CFA_advance_loc2:
16211 case DW_CFA_advance_loc4:
16212 if (!cfa_equal_p (&last_cfa, &next_cfa))
16213 {
16214 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16215 start_label, last_label, section);
16216
16217 list_tail = &(*list_tail)->dw_loc_next;
16218 last_cfa = next_cfa;
16219 start_label = last_label;
16220 }
16221 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16222 break;
16223
16224 case DW_CFA_advance_loc:
16225 /* The encoding is complex enough that we should never emit this. */
16226 gcc_unreachable ();
16227
16228 default:
16229 lookup_cfa_1 (cfi, &next_cfa, &remember);
16230 break;
16231 }
16232 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16233 {
16234 if (!cfa_equal_p (&last_cfa, &next_cfa))
16235 {
16236 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16237 start_label, last_label, section);
16238
16239 list_tail = &(*list_tail)->dw_loc_next;
16240 last_cfa = next_cfa;
16241 start_label = last_label;
16242 }
16243 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16244 start_label, fde->dw_fde_end, section);
16245 list_tail = &(*list_tail)->dw_loc_next;
16246 start_label = last_label = fde->dw_fde_second_begin;
16247 }
16248 }
16249
16250 if (!cfa_equal_p (&last_cfa, &next_cfa))
16251 {
16252 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16253 start_label, last_label, section);
16254 list_tail = &(*list_tail)->dw_loc_next;
16255 start_label = last_label;
16256 }
16257
16258 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16259 start_label,
16260 fde->dw_fde_second_begin
16261 ? fde->dw_fde_second_end : fde->dw_fde_end,
16262 section);
16263
16264 if (list && list->dw_loc_next)
16265 gen_llsym (list);
16266
16267 return list;
16268 }
16269
16270 /* Compute a displacement from the "steady-state frame pointer" to the
16271 frame base (often the same as the CFA), and store it in
16272 frame_pointer_fb_offset. OFFSET is added to the displacement
16273 before the latter is negated. */
16274
16275 static void
16276 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16277 {
16278 rtx reg, elim;
16279
16280 #ifdef FRAME_POINTER_CFA_OFFSET
16281 reg = frame_pointer_rtx;
16282 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16283 #else
16284 reg = arg_pointer_rtx;
16285 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16286 #endif
16287
16288 elim = (ira_use_lra_p
16289 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16290 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16291 if (GET_CODE (elim) == PLUS)
16292 {
16293 offset += INTVAL (XEXP (elim, 1));
16294 elim = XEXP (elim, 0);
16295 }
16296
16297 frame_pointer_fb_offset = -offset;
16298
16299 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16300 in which to eliminate. This is because it's stack pointer isn't
16301 directly accessible as a register within the ISA. To work around
16302 this, assume that while we cannot provide a proper value for
16303 frame_pointer_fb_offset, we won't need one either. */
16304 frame_pointer_fb_offset_valid
16305 = ((SUPPORTS_STACK_ALIGNMENT
16306 && (elim == hard_frame_pointer_rtx
16307 || elim == stack_pointer_rtx))
16308 || elim == (frame_pointer_needed
16309 ? hard_frame_pointer_rtx
16310 : stack_pointer_rtx));
16311 }
16312
16313 /* Generate a DW_AT_name attribute given some string value to be included as
16314 the value of the attribute. */
16315
16316 static void
16317 add_name_attribute (dw_die_ref die, const char *name_string)
16318 {
16319 if (name_string != NULL && *name_string != 0)
16320 {
16321 if (demangle_name_func)
16322 name_string = (*demangle_name_func) (name_string);
16323
16324 add_AT_string (die, DW_AT_name, name_string);
16325 }
16326 }
16327
16328 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16329 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16330 of TYPE accordingly.
16331
16332 ??? This is a temporary measure until after we're able to generate
16333 regular DWARF for the complex Ada type system. */
16334
16335 static void
16336 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16337 dw_die_ref context_die)
16338 {
16339 tree dtype;
16340 dw_die_ref dtype_die;
16341
16342 if (!lang_hooks.types.descriptive_type)
16343 return;
16344
16345 dtype = lang_hooks.types.descriptive_type (type);
16346 if (!dtype)
16347 return;
16348
16349 dtype_die = lookup_type_die (dtype);
16350 if (!dtype_die)
16351 {
16352 gen_type_die (dtype, context_die);
16353 dtype_die = lookup_type_die (dtype);
16354 gcc_assert (dtype_die);
16355 }
16356
16357 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16358 }
16359
16360 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16361
16362 static const char *
16363 comp_dir_string (void)
16364 {
16365 const char *wd;
16366 char *wd1;
16367 static const char *cached_wd = NULL;
16368
16369 if (cached_wd != NULL)
16370 return cached_wd;
16371
16372 wd = get_src_pwd ();
16373 if (wd == NULL)
16374 return NULL;
16375
16376 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16377 {
16378 int wdlen;
16379
16380 wdlen = strlen (wd);
16381 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16382 strcpy (wd1, wd);
16383 wd1 [wdlen] = DIR_SEPARATOR;
16384 wd1 [wdlen + 1] = 0;
16385 wd = wd1;
16386 }
16387
16388 cached_wd = remap_debug_filename (wd);
16389 return cached_wd;
16390 }
16391
16392 /* Generate a DW_AT_comp_dir attribute for DIE. */
16393
16394 static void
16395 add_comp_dir_attribute (dw_die_ref die)
16396 {
16397 const char * wd = comp_dir_string ();
16398 if (wd != NULL)
16399 add_AT_string (die, DW_AT_comp_dir, wd);
16400 }
16401
16402 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16403 default. */
16404
16405 static int
16406 lower_bound_default (void)
16407 {
16408 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16409 {
16410 case DW_LANG_C:
16411 case DW_LANG_C89:
16412 case DW_LANG_C99:
16413 case DW_LANG_C11:
16414 case DW_LANG_C_plus_plus:
16415 case DW_LANG_C_plus_plus_11:
16416 case DW_LANG_C_plus_plus_14:
16417 case DW_LANG_ObjC:
16418 case DW_LANG_ObjC_plus_plus:
16419 case DW_LANG_Java:
16420 return 0;
16421 case DW_LANG_Fortran77:
16422 case DW_LANG_Fortran90:
16423 case DW_LANG_Fortran95:
16424 return 1;
16425 case DW_LANG_UPC:
16426 case DW_LANG_D:
16427 case DW_LANG_Python:
16428 return dwarf_version >= 4 ? 0 : -1;
16429 case DW_LANG_Ada95:
16430 case DW_LANG_Ada83:
16431 case DW_LANG_Cobol74:
16432 case DW_LANG_Cobol85:
16433 case DW_LANG_Pascal83:
16434 case DW_LANG_Modula2:
16435 case DW_LANG_PLI:
16436 return dwarf_version >= 4 ? 1 : -1;
16437 default:
16438 return -1;
16439 }
16440 }
16441
16442 /* Given a tree node describing an array bound (either lower or upper) output
16443 a representation for that bound. */
16444
16445 static void
16446 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16447 {
16448 switch (TREE_CODE (bound))
16449 {
16450 case ERROR_MARK:
16451 return;
16452
16453 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16454 case INTEGER_CST:
16455 {
16456 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16457 int dflt;
16458
16459 /* Use the default if possible. */
16460 if (bound_attr == DW_AT_lower_bound
16461 && tree_fits_shwi_p (bound)
16462 && (dflt = lower_bound_default ()) != -1
16463 && tree_to_shwi (bound) == dflt)
16464 ;
16465
16466 /* If HOST_WIDE_INT is big enough then represent the bound as
16467 a constant value. We need to choose a form based on
16468 whether the type is signed or unsigned. We cannot just
16469 call add_AT_unsigned if the value itself is positive
16470 (add_AT_unsigned might add the unsigned value encoded as
16471 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16472 bounds type and then sign extend any unsigned values found
16473 for signed types. This is needed only for
16474 DW_AT_{lower,upper}_bound, since for most other attributes,
16475 consumers will treat DW_FORM_data[1248] as unsigned values,
16476 regardless of the underlying type. */
16477 else if (prec <= HOST_BITS_PER_WIDE_INT
16478 || tree_fits_uhwi_p (bound))
16479 {
16480 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16481 add_AT_unsigned (subrange_die, bound_attr,
16482 TREE_INT_CST_LOW (bound));
16483 else
16484 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16485 }
16486 else
16487 /* Otherwise represent the bound as an unsigned value with
16488 the precision of its type. The precision and signedness
16489 of the type will be necessary to re-interpret it
16490 unambiguously. */
16491 add_AT_wide (subrange_die, bound_attr, bound);
16492 }
16493 break;
16494
16495 CASE_CONVERT:
16496 case VIEW_CONVERT_EXPR:
16497 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16498 break;
16499
16500 case SAVE_EXPR:
16501 break;
16502
16503 case VAR_DECL:
16504 case PARM_DECL:
16505 case RESULT_DECL:
16506 {
16507 dw_die_ref decl_die = lookup_decl_die (bound);
16508
16509 /* ??? Can this happen, or should the variable have been bound
16510 first? Probably it can, since I imagine that we try to create
16511 the types of parameters in the order in which they exist in
16512 the list, and won't have created a forward reference to a
16513 later parameter. */
16514 if (decl_die != NULL)
16515 {
16516 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16517 break;
16518 }
16519 }
16520 /* FALLTHRU */
16521
16522 default:
16523 {
16524 /* Otherwise try to create a stack operation procedure to
16525 evaluate the value of the array bound. */
16526
16527 dw_die_ref ctx, decl_die;
16528 dw_loc_list_ref list;
16529
16530 list = loc_list_from_tree (bound, 2);
16531 if (list == NULL || single_element_loc_list_p (list))
16532 {
16533 /* If DW_AT_*bound is not a reference nor constant, it is
16534 a DWARF expression rather than location description.
16535 For that loc_list_from_tree (bound, 0) is needed.
16536 If that fails to give a single element list,
16537 fall back to outputting this as a reference anyway. */
16538 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16539 if (list2 && single_element_loc_list_p (list2))
16540 {
16541 add_AT_loc (subrange_die, bound_attr, list2->expr);
16542 break;
16543 }
16544 }
16545 if (list == NULL)
16546 break;
16547
16548 if (current_function_decl == 0)
16549 ctx = comp_unit_die ();
16550 else
16551 ctx = lookup_decl_die (current_function_decl);
16552
16553 decl_die = new_die (DW_TAG_variable, ctx, bound);
16554 add_AT_flag (decl_die, DW_AT_artificial, 1);
16555 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16556 add_AT_location_description (decl_die, DW_AT_location, list);
16557 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16558 break;
16559 }
16560 }
16561 }
16562
16563 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16564 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16565 Note that the block of subscript information for an array type also
16566 includes information about the element type of the given array type. */
16567
16568 static void
16569 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16570 {
16571 unsigned dimension_number;
16572 tree lower, upper;
16573 dw_die_ref subrange_die;
16574
16575 for (dimension_number = 0;
16576 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16577 type = TREE_TYPE (type), dimension_number++)
16578 {
16579 tree domain = TYPE_DOMAIN (type);
16580
16581 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16582 break;
16583
16584 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16585 and (in GNU C only) variable bounds. Handle all three forms
16586 here. */
16587 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16588 if (domain)
16589 {
16590 /* We have an array type with specified bounds. */
16591 lower = TYPE_MIN_VALUE (domain);
16592 upper = TYPE_MAX_VALUE (domain);
16593
16594 /* Define the index type. */
16595 if (TREE_TYPE (domain))
16596 {
16597 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16598 TREE_TYPE field. We can't emit debug info for this
16599 because it is an unnamed integral type. */
16600 if (TREE_CODE (domain) == INTEGER_TYPE
16601 && TYPE_NAME (domain) == NULL_TREE
16602 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16603 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16604 ;
16605 else
16606 add_type_attribute (subrange_die, TREE_TYPE (domain),
16607 TYPE_UNQUALIFIED, type_die);
16608 }
16609
16610 /* ??? If upper is NULL, the array has unspecified length,
16611 but it does have a lower bound. This happens with Fortran
16612 dimension arr(N:*)
16613 Since the debugger is definitely going to need to know N
16614 to produce useful results, go ahead and output the lower
16615 bound solo, and hope the debugger can cope. */
16616
16617 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16618 if (upper)
16619 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16620 }
16621
16622 /* Otherwise we have an array type with an unspecified length. The
16623 DWARF-2 spec does not say how to handle this; let's just leave out the
16624 bounds. */
16625 }
16626 }
16627
16628 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16629
16630 static void
16631 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16632 {
16633 dw_die_ref decl_die;
16634 HOST_WIDE_INT size;
16635
16636 switch (TREE_CODE (tree_node))
16637 {
16638 case ERROR_MARK:
16639 size = 0;
16640 break;
16641 case ENUMERAL_TYPE:
16642 case RECORD_TYPE:
16643 case UNION_TYPE:
16644 case QUAL_UNION_TYPE:
16645 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16646 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16647 {
16648 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16649 return;
16650 }
16651 size = int_size_in_bytes (tree_node);
16652 break;
16653 case FIELD_DECL:
16654 /* For a data member of a struct or union, the DW_AT_byte_size is
16655 generally given as the number of bytes normally allocated for an
16656 object of the *declared* type of the member itself. This is true
16657 even for bit-fields. */
16658 size = int_size_in_bytes (field_type (tree_node));
16659 break;
16660 default:
16661 gcc_unreachable ();
16662 }
16663
16664 /* Note that `size' might be -1 when we get to this point. If it is, that
16665 indicates that the byte size of the entity in question is variable. We
16666 have no good way of expressing this fact in Dwarf at the present time,
16667 when location description was not used by the caller code instead. */
16668 if (size >= 0)
16669 add_AT_unsigned (die, DW_AT_byte_size, size);
16670 }
16671
16672 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16673 which specifies the distance in bits from the highest order bit of the
16674 "containing object" for the bit-field to the highest order bit of the
16675 bit-field itself.
16676
16677 For any given bit-field, the "containing object" is a hypothetical object
16678 (of some integral or enum type) within which the given bit-field lives. The
16679 type of this hypothetical "containing object" is always the same as the
16680 declared type of the individual bit-field itself. The determination of the
16681 exact location of the "containing object" for a bit-field is rather
16682 complicated. It's handled by the `field_byte_offset' function (above).
16683
16684 Note that it is the size (in bytes) of the hypothetical "containing object"
16685 which will be given in the DW_AT_byte_size attribute for this bit-field.
16686 (See `byte_size_attribute' above). */
16687
16688 static inline void
16689 add_bit_offset_attribute (dw_die_ref die, tree decl)
16690 {
16691 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16692 tree type = DECL_BIT_FIELD_TYPE (decl);
16693 HOST_WIDE_INT bitpos_int;
16694 HOST_WIDE_INT highest_order_object_bit_offset;
16695 HOST_WIDE_INT highest_order_field_bit_offset;
16696 HOST_WIDE_INT bit_offset;
16697
16698 /* Must be a field and a bit field. */
16699 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16700
16701 /* We can't yet handle bit-fields whose offsets are variable, so if we
16702 encounter such things, just return without generating any attribute
16703 whatsoever. Likewise for variable or too large size. */
16704 if (! tree_fits_shwi_p (bit_position (decl))
16705 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16706 return;
16707
16708 bitpos_int = int_bit_position (decl);
16709
16710 /* Note that the bit offset is always the distance (in bits) from the
16711 highest-order bit of the "containing object" to the highest-order bit of
16712 the bit-field itself. Since the "high-order end" of any object or field
16713 is different on big-endian and little-endian machines, the computation
16714 below must take account of these differences. */
16715 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16716 highest_order_field_bit_offset = bitpos_int;
16717
16718 if (! BYTES_BIG_ENDIAN)
16719 {
16720 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16721 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16722 }
16723
16724 bit_offset
16725 = (! BYTES_BIG_ENDIAN
16726 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16727 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16728
16729 if (bit_offset < 0)
16730 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16731 else
16732 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16733 }
16734
16735 /* For a FIELD_DECL node which represents a bit field, output an attribute
16736 which specifies the length in bits of the given field. */
16737
16738 static inline void
16739 add_bit_size_attribute (dw_die_ref die, tree decl)
16740 {
16741 /* Must be a field and a bit field. */
16742 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16743 && DECL_BIT_FIELD_TYPE (decl));
16744
16745 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16746 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16747 }
16748
16749 /* If the compiled language is ANSI C, then add a 'prototyped'
16750 attribute, if arg types are given for the parameters of a function. */
16751
16752 static inline void
16753 add_prototyped_attribute (dw_die_ref die, tree func_type)
16754 {
16755 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16756 {
16757 case DW_LANG_C:
16758 case DW_LANG_C89:
16759 case DW_LANG_C99:
16760 case DW_LANG_C11:
16761 case DW_LANG_ObjC:
16762 if (prototype_p (func_type))
16763 add_AT_flag (die, DW_AT_prototyped, 1);
16764 break;
16765 default:
16766 break;
16767 }
16768 }
16769
16770 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16771 by looking in either the type declaration or object declaration
16772 equate table. */
16773
16774 static inline dw_die_ref
16775 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16776 {
16777 dw_die_ref origin_die = NULL;
16778
16779 if (TREE_CODE (origin) != FUNCTION_DECL)
16780 {
16781 /* We may have gotten separated from the block for the inlined
16782 function, if we're in an exception handler or some such; make
16783 sure that the abstract function has been written out.
16784
16785 Doing this for nested functions is wrong, however; functions are
16786 distinct units, and our context might not even be inline. */
16787 tree fn = origin;
16788
16789 if (TYPE_P (fn))
16790 fn = TYPE_STUB_DECL (fn);
16791
16792 fn = decl_function_context (fn);
16793 if (fn)
16794 dwarf2out_abstract_function (fn);
16795 }
16796
16797 if (DECL_P (origin))
16798 origin_die = lookup_decl_die (origin);
16799 else if (TYPE_P (origin))
16800 origin_die = lookup_type_die (origin);
16801
16802 /* XXX: Functions that are never lowered don't always have correct block
16803 trees (in the case of java, they simply have no block tree, in some other
16804 languages). For these functions, there is nothing we can really do to
16805 output correct debug info for inlined functions in all cases. Rather
16806 than die, we'll just produce deficient debug info now, in that we will
16807 have variables without a proper abstract origin. In the future, when all
16808 functions are lowered, we should re-add a gcc_assert (origin_die)
16809 here. */
16810
16811 if (origin_die)
16812 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16813 return origin_die;
16814 }
16815
16816 /* We do not currently support the pure_virtual attribute. */
16817
16818 static inline void
16819 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16820 {
16821 if (DECL_VINDEX (func_decl))
16822 {
16823 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16824
16825 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16826 add_AT_loc (die, DW_AT_vtable_elem_location,
16827 new_loc_descr (DW_OP_constu,
16828 tree_to_shwi (DECL_VINDEX (func_decl)),
16829 0));
16830
16831 /* GNU extension: Record what type this method came from originally. */
16832 if (debug_info_level > DINFO_LEVEL_TERSE
16833 && DECL_CONTEXT (func_decl))
16834 add_AT_die_ref (die, DW_AT_containing_type,
16835 lookup_type_die (DECL_CONTEXT (func_decl)));
16836 }
16837 }
16838 \f
16839 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16840 given decl. This used to be a vendor extension until after DWARF 4
16841 standardized it. */
16842
16843 static void
16844 add_linkage_attr (dw_die_ref die, tree decl)
16845 {
16846 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16847
16848 /* Mimic what assemble_name_raw does with a leading '*'. */
16849 if (name[0] == '*')
16850 name = &name[1];
16851
16852 if (dwarf_version >= 4)
16853 add_AT_string (die, DW_AT_linkage_name, name);
16854 else
16855 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16856 }
16857
16858 /* Add source coordinate attributes for the given decl. */
16859
16860 static void
16861 add_src_coords_attributes (dw_die_ref die, tree decl)
16862 {
16863 expanded_location s;
16864
16865 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16866 return;
16867 s = expand_location (DECL_SOURCE_LOCATION (decl));
16868 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16869 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16870 }
16871
16872 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16873
16874 static void
16875 add_linkage_name (dw_die_ref die, tree decl)
16876 {
16877 if (debug_info_level > DINFO_LEVEL_NONE
16878 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16879 && TREE_PUBLIC (decl)
16880 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16881 && die->die_tag != DW_TAG_member)
16882 {
16883 /* Defer until we have an assembler name set. */
16884 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16885 {
16886 limbo_die_node *asm_name;
16887
16888 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16889 asm_name->die = die;
16890 asm_name->created_for = decl;
16891 asm_name->next = deferred_asm_name;
16892 deferred_asm_name = asm_name;
16893 }
16894 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16895 add_linkage_attr (die, decl);
16896 }
16897 }
16898
16899 /* Add a DW_AT_name attribute and source coordinate attribute for the
16900 given decl, but only if it actually has a name. */
16901
16902 static void
16903 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16904 {
16905 tree decl_name;
16906
16907 decl_name = DECL_NAME (decl);
16908 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16909 {
16910 const char *name = dwarf2_name (decl, 0);
16911 if (name)
16912 add_name_attribute (die, name);
16913 if (! DECL_ARTIFICIAL (decl))
16914 add_src_coords_attributes (die, decl);
16915
16916 add_linkage_name (die, decl);
16917 }
16918
16919 #ifdef VMS_DEBUGGING_INFO
16920 /* Get the function's name, as described by its RTL. This may be different
16921 from the DECL_NAME name used in the source file. */
16922 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16923 {
16924 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16925 XEXP (DECL_RTL (decl), 0), false);
16926 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16927 }
16928 #endif /* VMS_DEBUGGING_INFO */
16929 }
16930
16931 #ifdef VMS_DEBUGGING_INFO
16932 /* Output the debug main pointer die for VMS */
16933
16934 void
16935 dwarf2out_vms_debug_main_pointer (void)
16936 {
16937 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16938 dw_die_ref die;
16939
16940 /* Allocate the VMS debug main subprogram die. */
16941 die = ggc_cleared_alloc<die_node> ();
16942 die->die_tag = DW_TAG_subprogram;
16943 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16944 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16945 current_function_funcdef_no);
16946 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16947
16948 /* Make it the first child of comp_unit_die (). */
16949 die->die_parent = comp_unit_die ();
16950 if (comp_unit_die ()->die_child)
16951 {
16952 die->die_sib = comp_unit_die ()->die_child->die_sib;
16953 comp_unit_die ()->die_child->die_sib = die;
16954 }
16955 else
16956 {
16957 die->die_sib = die;
16958 comp_unit_die ()->die_child = die;
16959 }
16960 }
16961 #endif /* VMS_DEBUGGING_INFO */
16962
16963 /* Push a new declaration scope. */
16964
16965 static void
16966 push_decl_scope (tree scope)
16967 {
16968 vec_safe_push (decl_scope_table, scope);
16969 }
16970
16971 /* Pop a declaration scope. */
16972
16973 static inline void
16974 pop_decl_scope (void)
16975 {
16976 decl_scope_table->pop ();
16977 }
16978
16979 /* walk_tree helper function for uses_local_type, below. */
16980
16981 static tree
16982 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16983 {
16984 if (!TYPE_P (*tp))
16985 *walk_subtrees = 0;
16986 else
16987 {
16988 tree name = TYPE_NAME (*tp);
16989 if (name && DECL_P (name) && decl_function_context (name))
16990 return *tp;
16991 }
16992 return NULL_TREE;
16993 }
16994
16995 /* If TYPE involves a function-local type (including a local typedef to a
16996 non-local type), returns that type; otherwise returns NULL_TREE. */
16997
16998 static tree
16999 uses_local_type (tree type)
17000 {
17001 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17002 return used;
17003 }
17004
17005 /* Return the DIE for the scope that immediately contains this type.
17006 Non-named types that do not involve a function-local type get global
17007 scope. Named types nested in namespaces or other types get their
17008 containing scope. All other types (i.e. function-local named types) get
17009 the current active scope. */
17010
17011 static dw_die_ref
17012 scope_die_for (tree t, dw_die_ref context_die)
17013 {
17014 dw_die_ref scope_die = NULL;
17015 tree containing_scope;
17016
17017 /* Non-types always go in the current scope. */
17018 gcc_assert (TYPE_P (t));
17019
17020 /* Use the scope of the typedef, rather than the scope of the type
17021 it refers to. */
17022 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17023 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17024 else
17025 containing_scope = TYPE_CONTEXT (t);
17026
17027 /* Use the containing namespace if there is one. */
17028 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17029 {
17030 if (context_die == lookup_decl_die (containing_scope))
17031 /* OK */;
17032 else if (debug_info_level > DINFO_LEVEL_TERSE)
17033 context_die = get_context_die (containing_scope);
17034 else
17035 containing_scope = NULL_TREE;
17036 }
17037
17038 /* Ignore function type "scopes" from the C frontend. They mean that
17039 a tagged type is local to a parmlist of a function declarator, but
17040 that isn't useful to DWARF. */
17041 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17042 containing_scope = NULL_TREE;
17043
17044 if (SCOPE_FILE_SCOPE_P (containing_scope))
17045 {
17046 /* If T uses a local type keep it local as well, to avoid references
17047 to function-local DIEs from outside the function. */
17048 if (current_function_decl && uses_local_type (t))
17049 scope_die = context_die;
17050 else
17051 scope_die = comp_unit_die ();
17052 }
17053 else if (TYPE_P (containing_scope))
17054 {
17055 /* For types, we can just look up the appropriate DIE. */
17056 if (debug_info_level > DINFO_LEVEL_TERSE)
17057 scope_die = get_context_die (containing_scope);
17058 else
17059 {
17060 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17061 if (scope_die == NULL)
17062 scope_die = comp_unit_die ();
17063 }
17064 }
17065 else
17066 scope_die = context_die;
17067
17068 return scope_die;
17069 }
17070
17071 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17072
17073 static inline int
17074 local_scope_p (dw_die_ref context_die)
17075 {
17076 for (; context_die; context_die = context_die->die_parent)
17077 if (context_die->die_tag == DW_TAG_inlined_subroutine
17078 || context_die->die_tag == DW_TAG_subprogram)
17079 return 1;
17080
17081 return 0;
17082 }
17083
17084 /* Returns nonzero if CONTEXT_DIE is a class. */
17085
17086 static inline int
17087 class_scope_p (dw_die_ref context_die)
17088 {
17089 return (context_die
17090 && (context_die->die_tag == DW_TAG_structure_type
17091 || context_die->die_tag == DW_TAG_class_type
17092 || context_die->die_tag == DW_TAG_interface_type
17093 || context_die->die_tag == DW_TAG_union_type));
17094 }
17095
17096 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17097 whether or not to treat a DIE in this context as a declaration. */
17098
17099 static inline int
17100 class_or_namespace_scope_p (dw_die_ref context_die)
17101 {
17102 return (class_scope_p (context_die)
17103 || (context_die && context_die->die_tag == DW_TAG_namespace));
17104 }
17105
17106 /* Many forms of DIEs require a "type description" attribute. This
17107 routine locates the proper "type descriptor" die for the type given
17108 by 'type' plus any additional qualifiers given by 'cv_quals', and
17109 adds a DW_AT_type attribute below the given die. */
17110
17111 static void
17112 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17113 dw_die_ref context_die)
17114 {
17115 enum tree_code code = TREE_CODE (type);
17116 dw_die_ref type_die = NULL;
17117
17118 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17119 or fixed-point type, use the inner type. This is because we have no
17120 support for unnamed types in base_type_die. This can happen if this is
17121 an Ada subrange type. Correct solution is emit a subrange type die. */
17122 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17123 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17124 type = TREE_TYPE (type), code = TREE_CODE (type);
17125
17126 if (code == ERROR_MARK
17127 /* Handle a special case. For functions whose return type is void, we
17128 generate *no* type attribute. (Note that no object may have type
17129 `void', so this only applies to function return types). */
17130 || code == VOID_TYPE)
17131 return;
17132
17133 type_die = modified_type_die (type,
17134 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17135 context_die);
17136
17137 if (type_die != NULL)
17138 add_AT_die_ref (object_die, DW_AT_type, type_die);
17139 }
17140
17141 /* Given an object die, add the calling convention attribute for the
17142 function call type. */
17143 static void
17144 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17145 {
17146 enum dwarf_calling_convention value = DW_CC_normal;
17147
17148 value = ((enum dwarf_calling_convention)
17149 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17150
17151 if (is_fortran ()
17152 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17153 {
17154 /* DWARF 2 doesn't provide a way to identify a program's source-level
17155 entry point. DW_AT_calling_convention attributes are only meant
17156 to describe functions' calling conventions. However, lacking a
17157 better way to signal the Fortran main program, we used this for
17158 a long time, following existing custom. Now, DWARF 4 has
17159 DW_AT_main_subprogram, which we add below, but some tools still
17160 rely on the old way, which we thus keep. */
17161 value = DW_CC_program;
17162
17163 if (dwarf_version >= 4 || !dwarf_strict)
17164 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17165 }
17166
17167 /* Only add the attribute if the backend requests it, and
17168 is not DW_CC_normal. */
17169 if (value && (value != DW_CC_normal))
17170 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17171 }
17172
17173 /* Given a tree pointer to a struct, class, union, or enum type node, return
17174 a pointer to the (string) tag name for the given type, or zero if the type
17175 was declared without a tag. */
17176
17177 static const char *
17178 type_tag (const_tree type)
17179 {
17180 const char *name = 0;
17181
17182 if (TYPE_NAME (type) != 0)
17183 {
17184 tree t = 0;
17185
17186 /* Find the IDENTIFIER_NODE for the type name. */
17187 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17188 && !TYPE_NAMELESS (type))
17189 t = TYPE_NAME (type);
17190
17191 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17192 a TYPE_DECL node, regardless of whether or not a `typedef' was
17193 involved. */
17194 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17195 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17196 {
17197 /* We want to be extra verbose. Don't call dwarf_name if
17198 DECL_NAME isn't set. The default hook for decl_printable_name
17199 doesn't like that, and in this context it's correct to return
17200 0, instead of "<anonymous>" or the like. */
17201 if (DECL_NAME (TYPE_NAME (type))
17202 && !DECL_NAMELESS (TYPE_NAME (type)))
17203 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17204 }
17205
17206 /* Now get the name as a string, or invent one. */
17207 if (!name && t != 0)
17208 name = IDENTIFIER_POINTER (t);
17209 }
17210
17211 return (name == 0 || *name == '\0') ? 0 : name;
17212 }
17213
17214 /* Return the type associated with a data member, make a special check
17215 for bit field types. */
17216
17217 static inline tree
17218 member_declared_type (const_tree member)
17219 {
17220 return (DECL_BIT_FIELD_TYPE (member)
17221 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17222 }
17223
17224 /* Get the decl's label, as described by its RTL. This may be different
17225 from the DECL_NAME name used in the source file. */
17226
17227 #if 0
17228 static const char *
17229 decl_start_label (tree decl)
17230 {
17231 rtx x;
17232 const char *fnname;
17233
17234 x = DECL_RTL (decl);
17235 gcc_assert (MEM_P (x));
17236
17237 x = XEXP (x, 0);
17238 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17239
17240 fnname = XSTR (x, 0);
17241 return fnname;
17242 }
17243 #endif
17244 \f
17245 /* These routines generate the internal representation of the DIE's for
17246 the compilation unit. Debugging information is collected by walking
17247 the declaration trees passed in from dwarf2out_decl(). */
17248
17249 static void
17250 gen_array_type_die (tree type, dw_die_ref context_die)
17251 {
17252 dw_die_ref scope_die = scope_die_for (type, context_die);
17253 dw_die_ref array_die;
17254
17255 /* GNU compilers represent multidimensional array types as sequences of one
17256 dimensional array types whose element types are themselves array types.
17257 We sometimes squish that down to a single array_type DIE with multiple
17258 subscripts in the Dwarf debugging info. The draft Dwarf specification
17259 say that we are allowed to do this kind of compression in C, because
17260 there is no difference between an array of arrays and a multidimensional
17261 array. We don't do this for Ada to remain as close as possible to the
17262 actual representation, which is especially important against the language
17263 flexibilty wrt arrays of variable size. */
17264
17265 bool collapse_nested_arrays = !is_ada ();
17266 tree element_type;
17267
17268 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17269 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17270 if (TYPE_STRING_FLAG (type)
17271 && TREE_CODE (type) == ARRAY_TYPE
17272 && is_fortran ()
17273 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17274 {
17275 HOST_WIDE_INT size;
17276
17277 array_die = new_die (DW_TAG_string_type, scope_die, type);
17278 add_name_attribute (array_die, type_tag (type));
17279 equate_type_number_to_die (type, array_die);
17280 size = int_size_in_bytes (type);
17281 if (size >= 0)
17282 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17283 else if (TYPE_DOMAIN (type) != NULL_TREE
17284 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17285 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17286 {
17287 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17288 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17289
17290 size = int_size_in_bytes (TREE_TYPE (szdecl));
17291 if (loc && size > 0)
17292 {
17293 add_AT_location_description (array_die, DW_AT_string_length, loc);
17294 if (size != DWARF2_ADDR_SIZE)
17295 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17296 }
17297 }
17298 return;
17299 }
17300
17301 array_die = new_die (DW_TAG_array_type, scope_die, type);
17302 add_name_attribute (array_die, type_tag (type));
17303 equate_type_number_to_die (type, array_die);
17304
17305 if (TREE_CODE (type) == VECTOR_TYPE)
17306 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17307
17308 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17309 if (is_fortran ()
17310 && TREE_CODE (type) == ARRAY_TYPE
17311 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17312 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17313 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17314
17315 #if 0
17316 /* We default the array ordering. SDB will probably do
17317 the right things even if DW_AT_ordering is not present. It's not even
17318 an issue until we start to get into multidimensional arrays anyway. If
17319 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17320 then we'll have to put the DW_AT_ordering attribute back in. (But if
17321 and when we find out that we need to put these in, we will only do so
17322 for multidimensional arrays. */
17323 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17324 #endif
17325
17326 if (TREE_CODE (type) == VECTOR_TYPE)
17327 {
17328 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17329 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17330 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17331 add_bound_info (subrange_die, DW_AT_upper_bound,
17332 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17333 }
17334 else
17335 add_subscript_info (array_die, type, collapse_nested_arrays);
17336
17337 /* Add representation of the type of the elements of this array type and
17338 emit the corresponding DIE if we haven't done it already. */
17339 element_type = TREE_TYPE (type);
17340 if (collapse_nested_arrays)
17341 while (TREE_CODE (element_type) == ARRAY_TYPE)
17342 {
17343 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17344 break;
17345 element_type = TREE_TYPE (element_type);
17346 }
17347
17348 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17349
17350 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17351 if (TYPE_ARTIFICIAL (type))
17352 add_AT_flag (array_die, DW_AT_artificial, 1);
17353
17354 if (get_AT (array_die, DW_AT_name))
17355 add_pubtype (type, array_die);
17356 }
17357
17358 static dw_loc_descr_ref
17359 descr_info_loc (tree val, tree base_decl)
17360 {
17361 HOST_WIDE_INT size;
17362 dw_loc_descr_ref loc, loc2;
17363 enum dwarf_location_atom op;
17364
17365 if (val == base_decl)
17366 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17367
17368 switch (TREE_CODE (val))
17369 {
17370 CASE_CONVERT:
17371 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17372 case VAR_DECL:
17373 return loc_descriptor_from_tree (val, 0);
17374 case INTEGER_CST:
17375 if (tree_fits_shwi_p (val))
17376 return int_loc_descriptor (tree_to_shwi (val));
17377 break;
17378 case INDIRECT_REF:
17379 size = int_size_in_bytes (TREE_TYPE (val));
17380 if (size < 0)
17381 break;
17382 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17383 if (!loc)
17384 break;
17385 if (size == DWARF2_ADDR_SIZE)
17386 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17387 else
17388 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17389 return loc;
17390 case POINTER_PLUS_EXPR:
17391 case PLUS_EXPR:
17392 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17393 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17394 {
17395 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17396 if (!loc)
17397 break;
17398 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17399 }
17400 else
17401 {
17402 op = DW_OP_plus;
17403 do_binop:
17404 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17405 if (!loc)
17406 break;
17407 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17408 if (!loc2)
17409 break;
17410 add_loc_descr (&loc, loc2);
17411 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17412 }
17413 return loc;
17414 case MINUS_EXPR:
17415 op = DW_OP_minus;
17416 goto do_binop;
17417 case MULT_EXPR:
17418 op = DW_OP_mul;
17419 goto do_binop;
17420 case EQ_EXPR:
17421 op = DW_OP_eq;
17422 goto do_binop;
17423 case NE_EXPR:
17424 op = DW_OP_ne;
17425 goto do_binop;
17426 default:
17427 break;
17428 }
17429 return NULL;
17430 }
17431
17432 static void
17433 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17434 tree val, tree base_decl)
17435 {
17436 dw_loc_descr_ref loc;
17437
17438 if (tree_fits_shwi_p (val))
17439 {
17440 add_AT_unsigned (die, attr, tree_to_shwi (val));
17441 return;
17442 }
17443
17444 loc = descr_info_loc (val, base_decl);
17445 if (!loc)
17446 return;
17447
17448 add_AT_loc (die, attr, loc);
17449 }
17450
17451 /* This routine generates DIE for array with hidden descriptor, details
17452 are filled into *info by a langhook. */
17453
17454 static void
17455 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17456 dw_die_ref context_die)
17457 {
17458 dw_die_ref scope_die = scope_die_for (type, context_die);
17459 dw_die_ref array_die;
17460 int dim;
17461
17462 array_die = new_die (DW_TAG_array_type, scope_die, type);
17463 add_name_attribute (array_die, type_tag (type));
17464 equate_type_number_to_die (type, array_die);
17465
17466 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17467 if (is_fortran ()
17468 && info->ndimensions >= 2)
17469 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17470
17471 if (info->data_location)
17472 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17473 info->base_decl);
17474 if (info->associated)
17475 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17476 info->base_decl);
17477 if (info->allocated)
17478 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17479 info->base_decl);
17480
17481 for (dim = 0; dim < info->ndimensions; dim++)
17482 {
17483 dw_die_ref subrange_die
17484 = new_die (DW_TAG_subrange_type, array_die, NULL);
17485
17486 if (info->dimen[dim].lower_bound)
17487 {
17488 /* If it is the default value, omit it. */
17489 int dflt;
17490
17491 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17492 && (dflt = lower_bound_default ()) != -1
17493 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17494 ;
17495 else
17496 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17497 info->dimen[dim].lower_bound,
17498 info->base_decl);
17499 }
17500 if (info->dimen[dim].upper_bound)
17501 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17502 info->dimen[dim].upper_bound,
17503 info->base_decl);
17504 if (info->dimen[dim].stride)
17505 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17506 info->dimen[dim].stride,
17507 info->base_decl);
17508 }
17509
17510 gen_type_die (info->element_type, context_die);
17511 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17512 context_die);
17513
17514 if (get_AT (array_die, DW_AT_name))
17515 add_pubtype (type, array_die);
17516 }
17517
17518 #if 0
17519 static void
17520 gen_entry_point_die (tree decl, dw_die_ref context_die)
17521 {
17522 tree origin = decl_ultimate_origin (decl);
17523 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17524
17525 if (origin != NULL)
17526 add_abstract_origin_attribute (decl_die, origin);
17527 else
17528 {
17529 add_name_and_src_coords_attributes (decl_die, decl);
17530 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17531 TYPE_UNQUALIFIED, context_die);
17532 }
17533
17534 if (DECL_ABSTRACT_P (decl))
17535 equate_decl_number_to_die (decl, decl_die);
17536 else
17537 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17538 }
17539 #endif
17540
17541 /* Walk through the list of incomplete types again, trying once more to
17542 emit full debugging info for them. */
17543
17544 static void
17545 retry_incomplete_types (void)
17546 {
17547 int i;
17548
17549 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17550 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17551 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17552 }
17553
17554 /* Determine what tag to use for a record type. */
17555
17556 static enum dwarf_tag
17557 record_type_tag (tree type)
17558 {
17559 if (! lang_hooks.types.classify_record)
17560 return DW_TAG_structure_type;
17561
17562 switch (lang_hooks.types.classify_record (type))
17563 {
17564 case RECORD_IS_STRUCT:
17565 return DW_TAG_structure_type;
17566
17567 case RECORD_IS_CLASS:
17568 return DW_TAG_class_type;
17569
17570 case RECORD_IS_INTERFACE:
17571 if (dwarf_version >= 3 || !dwarf_strict)
17572 return DW_TAG_interface_type;
17573 return DW_TAG_structure_type;
17574
17575 default:
17576 gcc_unreachable ();
17577 }
17578 }
17579
17580 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17581 include all of the information about the enumeration values also. Each
17582 enumerated type name/value is listed as a child of the enumerated type
17583 DIE. */
17584
17585 static dw_die_ref
17586 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17587 {
17588 dw_die_ref type_die = lookup_type_die (type);
17589
17590 if (type_die == NULL)
17591 {
17592 type_die = new_die (DW_TAG_enumeration_type,
17593 scope_die_for (type, context_die), type);
17594 equate_type_number_to_die (type, type_die);
17595 add_name_attribute (type_die, type_tag (type));
17596 if (dwarf_version >= 4 || !dwarf_strict)
17597 {
17598 if (ENUM_IS_SCOPED (type))
17599 add_AT_flag (type_die, DW_AT_enum_class, 1);
17600 if (ENUM_IS_OPAQUE (type))
17601 add_AT_flag (type_die, DW_AT_declaration, 1);
17602 }
17603 }
17604 else if (! TYPE_SIZE (type))
17605 return type_die;
17606 else
17607 remove_AT (type_die, DW_AT_declaration);
17608
17609 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17610 given enum type is incomplete, do not generate the DW_AT_byte_size
17611 attribute or the DW_AT_element_list attribute. */
17612 if (TYPE_SIZE (type))
17613 {
17614 tree link;
17615
17616 TREE_ASM_WRITTEN (type) = 1;
17617 add_byte_size_attribute (type_die, type);
17618 if (dwarf_version >= 3 || !dwarf_strict)
17619 {
17620 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17621 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17622 context_die);
17623 }
17624 if (TYPE_STUB_DECL (type) != NULL_TREE)
17625 {
17626 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17627 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17628 }
17629
17630 /* If the first reference to this type was as the return type of an
17631 inline function, then it may not have a parent. Fix this now. */
17632 if (type_die->die_parent == NULL)
17633 add_child_die (scope_die_for (type, context_die), type_die);
17634
17635 for (link = TYPE_VALUES (type);
17636 link != NULL; link = TREE_CHAIN (link))
17637 {
17638 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17639 tree value = TREE_VALUE (link);
17640
17641 add_name_attribute (enum_die,
17642 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17643
17644 if (TREE_CODE (value) == CONST_DECL)
17645 value = DECL_INITIAL (value);
17646
17647 if (simple_type_size_in_bits (TREE_TYPE (value))
17648 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17649 {
17650 /* For constant forms created by add_AT_unsigned DWARF
17651 consumers (GDB, elfutils, etc.) always zero extend
17652 the value. Only when the actual value is negative
17653 do we need to use add_AT_int to generate a constant
17654 form that can represent negative values. */
17655 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17656 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17657 add_AT_unsigned (enum_die, DW_AT_const_value,
17658 (unsigned HOST_WIDE_INT) val);
17659 else
17660 add_AT_int (enum_die, DW_AT_const_value, val);
17661 }
17662 else
17663 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17664 that here. TODO: This should be re-worked to use correct
17665 signed/unsigned double tags for all cases. */
17666 add_AT_wide (enum_die, DW_AT_const_value, value);
17667 }
17668
17669 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17670 if (TYPE_ARTIFICIAL (type))
17671 add_AT_flag (type_die, DW_AT_artificial, 1);
17672 }
17673 else
17674 add_AT_flag (type_die, DW_AT_declaration, 1);
17675
17676 add_pubtype (type, type_die);
17677
17678 return type_die;
17679 }
17680
17681 /* Generate a DIE to represent either a real live formal parameter decl or to
17682 represent just the type of some formal parameter position in some function
17683 type.
17684
17685 Note that this routine is a bit unusual because its argument may be a
17686 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17687 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17688 node. If it's the former then this function is being called to output a
17689 DIE to represent a formal parameter object (or some inlining thereof). If
17690 it's the latter, then this function is only being called to output a
17691 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17692 argument type of some subprogram type.
17693 If EMIT_NAME_P is true, name and source coordinate attributes
17694 are emitted. */
17695
17696 static dw_die_ref
17697 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17698 dw_die_ref context_die)
17699 {
17700 tree node_or_origin = node ? node : origin;
17701 tree ultimate_origin;
17702 dw_die_ref parm_die
17703 = new_die (DW_TAG_formal_parameter, context_die, node);
17704
17705 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17706 {
17707 case tcc_declaration:
17708 ultimate_origin = decl_ultimate_origin (node_or_origin);
17709 if (node || ultimate_origin)
17710 origin = ultimate_origin;
17711 if (origin != NULL)
17712 add_abstract_origin_attribute (parm_die, origin);
17713 else if (emit_name_p)
17714 add_name_and_src_coords_attributes (parm_die, node);
17715 if (origin == NULL
17716 || (! DECL_ABSTRACT_P (node_or_origin)
17717 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17718 decl_function_context
17719 (node_or_origin))))
17720 {
17721 tree type = TREE_TYPE (node_or_origin);
17722 if (decl_by_reference_p (node_or_origin))
17723 add_type_attribute (parm_die, TREE_TYPE (type),
17724 TYPE_UNQUALIFIED, context_die);
17725 else
17726 add_type_attribute (parm_die, type,
17727 decl_quals (node_or_origin),
17728 context_die);
17729 }
17730 if (origin == NULL && DECL_ARTIFICIAL (node))
17731 add_AT_flag (parm_die, DW_AT_artificial, 1);
17732
17733 if (node && node != origin)
17734 equate_decl_number_to_die (node, parm_die);
17735 if (! DECL_ABSTRACT_P (node_or_origin))
17736 add_location_or_const_value_attribute (parm_die, node_or_origin,
17737 node == NULL, DW_AT_location);
17738
17739 break;
17740
17741 case tcc_type:
17742 /* We were called with some kind of a ..._TYPE node. */
17743 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17744 context_die);
17745 break;
17746
17747 default:
17748 gcc_unreachable ();
17749 }
17750
17751 return parm_die;
17752 }
17753
17754 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17755 children DW_TAG_formal_parameter DIEs representing the arguments of the
17756 parameter pack.
17757
17758 PARM_PACK must be a function parameter pack.
17759 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17760 must point to the subsequent arguments of the function PACK_ARG belongs to.
17761 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17762 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17763 following the last one for which a DIE was generated. */
17764
17765 static dw_die_ref
17766 gen_formal_parameter_pack_die (tree parm_pack,
17767 tree pack_arg,
17768 dw_die_ref subr_die,
17769 tree *next_arg)
17770 {
17771 tree arg;
17772 dw_die_ref parm_pack_die;
17773
17774 gcc_assert (parm_pack
17775 && lang_hooks.function_parameter_pack_p (parm_pack)
17776 && subr_die);
17777
17778 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17779 add_src_coords_attributes (parm_pack_die, parm_pack);
17780
17781 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17782 {
17783 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17784 parm_pack))
17785 break;
17786 gen_formal_parameter_die (arg, NULL,
17787 false /* Don't emit name attribute. */,
17788 parm_pack_die);
17789 }
17790 if (next_arg)
17791 *next_arg = arg;
17792 return parm_pack_die;
17793 }
17794
17795 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17796 at the end of an (ANSI prototyped) formal parameters list. */
17797
17798 static void
17799 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17800 {
17801 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17802 }
17803
17804 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17805 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17806 parameters as specified in some function type specification (except for
17807 those which appear as part of a function *definition*). */
17808
17809 static void
17810 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17811 {
17812 tree link;
17813 tree formal_type = NULL;
17814 tree first_parm_type;
17815 tree arg;
17816
17817 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17818 {
17819 arg = DECL_ARGUMENTS (function_or_method_type);
17820 function_or_method_type = TREE_TYPE (function_or_method_type);
17821 }
17822 else
17823 arg = NULL_TREE;
17824
17825 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17826
17827 /* Make our first pass over the list of formal parameter types and output a
17828 DW_TAG_formal_parameter DIE for each one. */
17829 for (link = first_parm_type; link; )
17830 {
17831 dw_die_ref parm_die;
17832
17833 formal_type = TREE_VALUE (link);
17834 if (formal_type == void_type_node)
17835 break;
17836
17837 /* Output a (nameless) DIE to represent the formal parameter itself. */
17838 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17839 {
17840 parm_die = gen_formal_parameter_die (formal_type, NULL,
17841 true /* Emit name attribute. */,
17842 context_die);
17843 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17844 && link == first_parm_type)
17845 {
17846 add_AT_flag (parm_die, DW_AT_artificial, 1);
17847 if (dwarf_version >= 3 || !dwarf_strict)
17848 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17849 }
17850 else if (arg && DECL_ARTIFICIAL (arg))
17851 add_AT_flag (parm_die, DW_AT_artificial, 1);
17852 }
17853
17854 link = TREE_CHAIN (link);
17855 if (arg)
17856 arg = DECL_CHAIN (arg);
17857 }
17858
17859 /* If this function type has an ellipsis, add a
17860 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17861 if (formal_type != void_type_node)
17862 gen_unspecified_parameters_die (function_or_method_type, context_die);
17863
17864 /* Make our second (and final) pass over the list of formal parameter types
17865 and output DIEs to represent those types (as necessary). */
17866 for (link = TYPE_ARG_TYPES (function_or_method_type);
17867 link && TREE_VALUE (link);
17868 link = TREE_CHAIN (link))
17869 gen_type_die (TREE_VALUE (link), context_die);
17870 }
17871
17872 /* We want to generate the DIE for TYPE so that we can generate the
17873 die for MEMBER, which has been defined; we will need to refer back
17874 to the member declaration nested within TYPE. If we're trying to
17875 generate minimal debug info for TYPE, processing TYPE won't do the
17876 trick; we need to attach the member declaration by hand. */
17877
17878 static void
17879 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17880 {
17881 gen_type_die (type, context_die);
17882
17883 /* If we're trying to avoid duplicate debug info, we may not have
17884 emitted the member decl for this function. Emit it now. */
17885 if (TYPE_STUB_DECL (type)
17886 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17887 && ! lookup_decl_die (member))
17888 {
17889 dw_die_ref type_die;
17890 gcc_assert (!decl_ultimate_origin (member));
17891
17892 push_decl_scope (type);
17893 type_die = lookup_type_die_strip_naming_typedef (type);
17894 if (TREE_CODE (member) == FUNCTION_DECL)
17895 gen_subprogram_die (member, type_die);
17896 else if (TREE_CODE (member) == FIELD_DECL)
17897 {
17898 /* Ignore the nameless fields that are used to skip bits but handle
17899 C++ anonymous unions and structs. */
17900 if (DECL_NAME (member) != NULL_TREE
17901 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17902 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17903 {
17904 gen_type_die (member_declared_type (member), type_die);
17905 gen_field_die (member, type_die);
17906 }
17907 }
17908 else
17909 gen_variable_die (member, NULL_TREE, type_die);
17910
17911 pop_decl_scope ();
17912 }
17913 }
17914 \f
17915 /* Forward declare these functions, because they are mutually recursive
17916 with their set_block_* pairing functions. */
17917 static void set_decl_origin_self (tree);
17918 static void set_decl_abstract_flags (tree, int);
17919
17920 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17921 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17922 that it points to the node itself, thus indicating that the node is its
17923 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17924 the given node is NULL, recursively descend the decl/block tree which
17925 it is the root of, and for each other ..._DECL or BLOCK node contained
17926 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17927 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17928 values to point to themselves. */
17929
17930 static void
17931 set_block_origin_self (tree stmt)
17932 {
17933 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17934 {
17935 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17936
17937 {
17938 tree local_decl;
17939
17940 for (local_decl = BLOCK_VARS (stmt);
17941 local_decl != NULL_TREE;
17942 local_decl = DECL_CHAIN (local_decl))
17943 if (! DECL_EXTERNAL (local_decl))
17944 set_decl_origin_self (local_decl); /* Potential recursion. */
17945 }
17946
17947 {
17948 tree subblock;
17949
17950 for (subblock = BLOCK_SUBBLOCKS (stmt);
17951 subblock != NULL_TREE;
17952 subblock = BLOCK_CHAIN (subblock))
17953 set_block_origin_self (subblock); /* Recurse. */
17954 }
17955 }
17956 }
17957
17958 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17959 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17960 node to so that it points to the node itself, thus indicating that the
17961 node represents its own (abstract) origin. Additionally, if the
17962 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17963 the decl/block tree of which the given node is the root of, and for
17964 each other ..._DECL or BLOCK node contained therein whose
17965 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17966 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17967 point to themselves. */
17968
17969 static void
17970 set_decl_origin_self (tree decl)
17971 {
17972 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17973 {
17974 DECL_ABSTRACT_ORIGIN (decl) = decl;
17975 if (TREE_CODE (decl) == FUNCTION_DECL)
17976 {
17977 tree arg;
17978
17979 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17980 DECL_ABSTRACT_ORIGIN (arg) = arg;
17981 if (DECL_INITIAL (decl) != NULL_TREE
17982 && DECL_INITIAL (decl) != error_mark_node)
17983 set_block_origin_self (DECL_INITIAL (decl));
17984 }
17985 }
17986 }
17987 \f
17988 /* Given a pointer to some BLOCK node, and a boolean value to set the
17989 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17990 the given block, and for all local decls and all local sub-blocks
17991 (recursively) which are contained therein. */
17992
17993 static void
17994 set_block_abstract_flags (tree stmt, int setting)
17995 {
17996 tree local_decl;
17997 tree subblock;
17998 unsigned int i;
17999
18000 BLOCK_ABSTRACT (stmt) = setting;
18001
18002 for (local_decl = BLOCK_VARS (stmt);
18003 local_decl != NULL_TREE;
18004 local_decl = DECL_CHAIN (local_decl))
18005 if (! DECL_EXTERNAL (local_decl))
18006 set_decl_abstract_flags (local_decl, setting);
18007
18008 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18009 {
18010 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18011 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18012 || TREE_CODE (local_decl) == PARM_DECL)
18013 set_decl_abstract_flags (local_decl, setting);
18014 }
18015
18016 for (subblock = BLOCK_SUBBLOCKS (stmt);
18017 subblock != NULL_TREE;
18018 subblock = BLOCK_CHAIN (subblock))
18019 set_block_abstract_flags (subblock, setting);
18020 }
18021
18022 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18023 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18024 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18025 set the abstract flags for all of the parameters, local vars, local
18026 blocks and sub-blocks (recursively) to the same setting. */
18027
18028 static void
18029 set_decl_abstract_flags (tree decl, int setting)
18030 {
18031 DECL_ABSTRACT_P (decl) = setting;
18032 if (TREE_CODE (decl) == FUNCTION_DECL)
18033 {
18034 tree arg;
18035
18036 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18037 DECL_ABSTRACT_P (arg) = setting;
18038 if (DECL_INITIAL (decl) != NULL_TREE
18039 && DECL_INITIAL (decl) != error_mark_node)
18040 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18041 }
18042 }
18043
18044 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18045 may later generate inlined and/or out-of-line instances of. */
18046
18047 static void
18048 dwarf2out_abstract_function (tree decl)
18049 {
18050 dw_die_ref old_die;
18051 tree save_fn;
18052 tree context;
18053 int was_abstract;
18054 hash_table<decl_loc_hasher> *old_decl_loc_table;
18055 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18056 int old_call_site_count, old_tail_call_site_count;
18057 struct call_arg_loc_node *old_call_arg_locations;
18058
18059 /* Make sure we have the actual abstract inline, not a clone. */
18060 decl = DECL_ORIGIN (decl);
18061
18062 old_die = lookup_decl_die (decl);
18063 if (old_die && get_AT (old_die, DW_AT_inline))
18064 /* We've already generated the abstract instance. */
18065 return;
18066
18067 /* We can be called while recursively when seeing block defining inlined subroutine
18068 DIE. Be sure to not clobber the outer location table nor use it or we would
18069 get locations in abstract instantces. */
18070 old_decl_loc_table = decl_loc_table;
18071 decl_loc_table = NULL;
18072 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18073 cached_dw_loc_list_table = NULL;
18074 old_call_arg_locations = call_arg_locations;
18075 call_arg_locations = NULL;
18076 old_call_site_count = call_site_count;
18077 call_site_count = -1;
18078 old_tail_call_site_count = tail_call_site_count;
18079 tail_call_site_count = -1;
18080
18081 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18082 we don't get confused by DECL_ABSTRACT_P. */
18083 if (debug_info_level > DINFO_LEVEL_TERSE)
18084 {
18085 context = decl_class_context (decl);
18086 if (context)
18087 gen_type_die_for_member
18088 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18089 }
18090
18091 /* Pretend we've just finished compiling this function. */
18092 save_fn = current_function_decl;
18093 current_function_decl = decl;
18094
18095 was_abstract = DECL_ABSTRACT_P (decl);
18096 set_decl_abstract_flags (decl, 1);
18097 dwarf2out_decl (decl);
18098 if (! was_abstract)
18099 set_decl_abstract_flags (decl, 0);
18100
18101 current_function_decl = save_fn;
18102 decl_loc_table = old_decl_loc_table;
18103 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18104 call_arg_locations = old_call_arg_locations;
18105 call_site_count = old_call_site_count;
18106 tail_call_site_count = old_tail_call_site_count;
18107 }
18108
18109 /* Helper function of premark_used_types() which gets called through
18110 htab_traverse.
18111
18112 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18113 marked as unused by prune_unused_types. */
18114
18115 bool
18116 premark_used_types_helper (tree const &type, void *)
18117 {
18118 dw_die_ref die;
18119
18120 die = lookup_type_die (type);
18121 if (die != NULL)
18122 die->die_perennial_p = 1;
18123 return true;
18124 }
18125
18126 /* Helper function of premark_types_used_by_global_vars which gets called
18127 through htab_traverse.
18128
18129 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18130 marked as unused by prune_unused_types. The DIE of the type is marked
18131 only if the global variable using the type will actually be emitted. */
18132
18133 int
18134 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18135 void *)
18136 {
18137 struct types_used_by_vars_entry *entry;
18138 dw_die_ref die;
18139
18140 entry = (struct types_used_by_vars_entry *) *slot;
18141 gcc_assert (entry->type != NULL
18142 && entry->var_decl != NULL);
18143 die = lookup_type_die (entry->type);
18144 if (die)
18145 {
18146 /* Ask cgraph if the global variable really is to be emitted.
18147 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18148 varpool_node *node = varpool_node::get (entry->var_decl);
18149 if (node && node->definition)
18150 {
18151 die->die_perennial_p = 1;
18152 /* Keep the parent DIEs as well. */
18153 while ((die = die->die_parent) && die->die_perennial_p == 0)
18154 die->die_perennial_p = 1;
18155 }
18156 }
18157 return 1;
18158 }
18159
18160 /* Mark all members of used_types_hash as perennial. */
18161
18162 static void
18163 premark_used_types (struct function *fun)
18164 {
18165 if (fun && fun->used_types_hash)
18166 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18167 }
18168
18169 /* Mark all members of types_used_by_vars_entry as perennial. */
18170
18171 static void
18172 premark_types_used_by_global_vars (void)
18173 {
18174 if (types_used_by_vars_hash)
18175 types_used_by_vars_hash
18176 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18177 }
18178
18179 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18180 for CA_LOC call arg loc node. */
18181
18182 static dw_die_ref
18183 gen_call_site_die (tree decl, dw_die_ref subr_die,
18184 struct call_arg_loc_node *ca_loc)
18185 {
18186 dw_die_ref stmt_die = NULL, die;
18187 tree block = ca_loc->block;
18188
18189 while (block
18190 && block != DECL_INITIAL (decl)
18191 && TREE_CODE (block) == BLOCK)
18192 {
18193 if (block_map.length () > BLOCK_NUMBER (block))
18194 stmt_die = block_map[BLOCK_NUMBER (block)];
18195 if (stmt_die)
18196 break;
18197 block = BLOCK_SUPERCONTEXT (block);
18198 }
18199 if (stmt_die == NULL)
18200 stmt_die = subr_die;
18201 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18202 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18203 if (ca_loc->tail_call_p)
18204 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18205 if (ca_loc->symbol_ref)
18206 {
18207 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18208 if (tdie)
18209 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18210 else
18211 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18212 }
18213 return die;
18214 }
18215
18216 /* Generate a DIE to represent a declared function (either file-scope or
18217 block-local). */
18218
18219 static void
18220 gen_subprogram_die (tree decl, dw_die_ref context_die)
18221 {
18222 tree origin = decl_ultimate_origin (decl);
18223 dw_die_ref subr_die;
18224 tree outer_scope;
18225 dw_die_ref old_die = lookup_decl_die (decl);
18226 int declaration = (current_function_decl != decl
18227 || class_or_namespace_scope_p (context_die));
18228
18229 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18230
18231 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18232 started to generate the abstract instance of an inline, decided to output
18233 its containing class, and proceeded to emit the declaration of the inline
18234 from the member list for the class. If so, DECLARATION takes priority;
18235 we'll get back to the abstract instance when done with the class. */
18236
18237 /* The class-scope declaration DIE must be the primary DIE. */
18238 if (origin && declaration && class_or_namespace_scope_p (context_die))
18239 {
18240 origin = NULL;
18241 gcc_assert (!old_die);
18242 }
18243
18244 /* Now that the C++ front end lazily declares artificial member fns, we
18245 might need to retrofit the declaration into its class. */
18246 if (!declaration && !origin && !old_die
18247 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18248 && !class_or_namespace_scope_p (context_die)
18249 && debug_info_level > DINFO_LEVEL_TERSE)
18250 old_die = force_decl_die (decl);
18251
18252 if (origin != NULL)
18253 {
18254 gcc_assert (!declaration || local_scope_p (context_die));
18255
18256 /* Fixup die_parent for the abstract instance of a nested
18257 inline function. */
18258 if (old_die && old_die->die_parent == NULL)
18259 add_child_die (context_die, old_die);
18260
18261 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18262 add_abstract_origin_attribute (subr_die, origin);
18263 /* This is where the actual code for a cloned function is.
18264 Let's emit linkage name attribute for it. This helps
18265 debuggers to e.g, set breakpoints into
18266 constructors/destructors when the user asks "break
18267 K::K". */
18268 add_linkage_name (subr_die, decl);
18269 }
18270 else if (old_die)
18271 {
18272 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18273 struct dwarf_file_data * file_index = lookup_filename (s.file);
18274
18275 if (!get_AT_flag (old_die, DW_AT_declaration)
18276 /* We can have a normal definition following an inline one in the
18277 case of redefinition of GNU C extern inlines.
18278 It seems reasonable to use AT_specification in this case. */
18279 && !get_AT (old_die, DW_AT_inline))
18280 {
18281 /* Detect and ignore this case, where we are trying to output
18282 something we have already output. */
18283 return;
18284 }
18285
18286 /* If the definition comes from the same place as the declaration,
18287 maybe use the old DIE. We always want the DIE for this function
18288 that has the *_pc attributes to be under comp_unit_die so the
18289 debugger can find it. We also need to do this for abstract
18290 instances of inlines, since the spec requires the out-of-line copy
18291 to have the same parent. For local class methods, this doesn't
18292 apply; we just use the old DIE. */
18293 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18294 && (DECL_ARTIFICIAL (decl)
18295 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18296 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18297 == (unsigned) s.line))))
18298 {
18299 subr_die = old_die;
18300
18301 /* Clear out the declaration attribute and the formal parameters.
18302 Do not remove all children, because it is possible that this
18303 declaration die was forced using force_decl_die(). In such
18304 cases die that forced declaration die (e.g. TAG_imported_module)
18305 is one of the children that we do not want to remove. */
18306 remove_AT (subr_die, DW_AT_declaration);
18307 remove_AT (subr_die, DW_AT_object_pointer);
18308 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18309 }
18310 else
18311 {
18312 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18313 add_AT_specification (subr_die, old_die);
18314 add_pubname (decl, subr_die);
18315 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18316 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18317 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18318 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18319
18320 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18321 emit the real type on the definition die. */
18322 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18323 {
18324 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18325 if (die == auto_die || die == decltype_auto_die)
18326 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18327 TYPE_UNQUALIFIED, context_die);
18328 }
18329 }
18330 }
18331 else
18332 {
18333 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18334
18335 if (TREE_PUBLIC (decl))
18336 add_AT_flag (subr_die, DW_AT_external, 1);
18337
18338 add_name_and_src_coords_attributes (subr_die, decl);
18339 add_pubname (decl, subr_die);
18340 if (debug_info_level > DINFO_LEVEL_TERSE)
18341 {
18342 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18343 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18344 TYPE_UNQUALIFIED, context_die);
18345 }
18346
18347 add_pure_or_virtual_attribute (subr_die, decl);
18348 if (DECL_ARTIFICIAL (decl))
18349 add_AT_flag (subr_die, DW_AT_artificial, 1);
18350
18351 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18352 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18353
18354 add_accessibility_attribute (subr_die, decl);
18355 }
18356
18357 if (declaration)
18358 {
18359 if (!old_die || !get_AT (old_die, DW_AT_inline))
18360 {
18361 add_AT_flag (subr_die, DW_AT_declaration, 1);
18362
18363 /* If this is an explicit function declaration then generate
18364 a DW_AT_explicit attribute. */
18365 if (lang_hooks.decls.function_decl_explicit_p (decl)
18366 && (dwarf_version >= 3 || !dwarf_strict))
18367 add_AT_flag (subr_die, DW_AT_explicit, 1);
18368
18369 /* If this is a C++11 deleted special function member then generate
18370 a DW_AT_GNU_deleted attribute. */
18371 if (lang_hooks.decls.function_decl_deleted_p (decl)
18372 && (! dwarf_strict))
18373 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18374
18375 /* The first time we see a member function, it is in the context of
18376 the class to which it belongs. We make sure of this by emitting
18377 the class first. The next time is the definition, which is
18378 handled above. The two may come from the same source text.
18379
18380 Note that force_decl_die() forces function declaration die. It is
18381 later reused to represent definition. */
18382 equate_decl_number_to_die (decl, subr_die);
18383 }
18384 }
18385 else if (DECL_ABSTRACT_P (decl))
18386 {
18387 if (DECL_DECLARED_INLINE_P (decl))
18388 {
18389 if (cgraph_function_possibly_inlined_p (decl))
18390 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18391 else
18392 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18393 }
18394 else
18395 {
18396 if (cgraph_function_possibly_inlined_p (decl))
18397 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18398 else
18399 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18400 }
18401
18402 if (DECL_DECLARED_INLINE_P (decl)
18403 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18404 add_AT_flag (subr_die, DW_AT_artificial, 1);
18405
18406 equate_decl_number_to_die (decl, subr_die);
18407 }
18408 else if (!DECL_EXTERNAL (decl))
18409 {
18410 HOST_WIDE_INT cfa_fb_offset;
18411 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18412
18413 if (!old_die || !get_AT (old_die, DW_AT_inline))
18414 equate_decl_number_to_die (decl, subr_die);
18415
18416 gcc_checking_assert (fun);
18417 if (!flag_reorder_blocks_and_partition)
18418 {
18419 dw_fde_ref fde = fun->fde;
18420 if (fde->dw_fde_begin)
18421 {
18422 /* We have already generated the labels. */
18423 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18424 fde->dw_fde_end, false);
18425 }
18426 else
18427 {
18428 /* Create start/end labels and add the range. */
18429 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18430 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18431 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18432 current_function_funcdef_no);
18433 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18434 current_function_funcdef_no);
18435 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18436 false);
18437 }
18438
18439 #if VMS_DEBUGGING_INFO
18440 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18441 Section 2.3 Prologue and Epilogue Attributes:
18442 When a breakpoint is set on entry to a function, it is generally
18443 desirable for execution to be suspended, not on the very first
18444 instruction of the function, but rather at a point after the
18445 function's frame has been set up, after any language defined local
18446 declaration processing has been completed, and before execution of
18447 the first statement of the function begins. Debuggers generally
18448 cannot properly determine where this point is. Similarly for a
18449 breakpoint set on exit from a function. The prologue and epilogue
18450 attributes allow a compiler to communicate the location(s) to use. */
18451
18452 {
18453 if (fde->dw_fde_vms_end_prologue)
18454 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18455 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18456
18457 if (fde->dw_fde_vms_begin_epilogue)
18458 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18459 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18460 }
18461 #endif
18462
18463 }
18464 else
18465 {
18466 /* Generate pubnames entries for the split function code ranges. */
18467 dw_fde_ref fde = fun->fde;
18468
18469 if (fde->dw_fde_second_begin)
18470 {
18471 if (dwarf_version >= 3 || !dwarf_strict)
18472 {
18473 /* We should use ranges for non-contiguous code section
18474 addresses. Use the actual code range for the initial
18475 section, since the HOT/COLD labels might precede an
18476 alignment offset. */
18477 bool range_list_added = false;
18478 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18479 fde->dw_fde_end, &range_list_added,
18480 false);
18481 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18482 fde->dw_fde_second_end,
18483 &range_list_added, false);
18484 if (range_list_added)
18485 add_ranges (NULL);
18486 }
18487 else
18488 {
18489 /* There is no real support in DW2 for this .. so we make
18490 a work-around. First, emit the pub name for the segment
18491 containing the function label. Then make and emit a
18492 simplified subprogram DIE for the second segment with the
18493 name pre-fixed by __hot/cold_sect_of_. We use the same
18494 linkage name for the second die so that gdb will find both
18495 sections when given "b foo". */
18496 const char *name = NULL;
18497 tree decl_name = DECL_NAME (decl);
18498 dw_die_ref seg_die;
18499
18500 /* Do the 'primary' section. */
18501 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18502 fde->dw_fde_end, false);
18503
18504 /* Build a minimal DIE for the secondary section. */
18505 seg_die = new_die (DW_TAG_subprogram,
18506 subr_die->die_parent, decl);
18507
18508 if (TREE_PUBLIC (decl))
18509 add_AT_flag (seg_die, DW_AT_external, 1);
18510
18511 if (decl_name != NULL
18512 && IDENTIFIER_POINTER (decl_name) != NULL)
18513 {
18514 name = dwarf2_name (decl, 1);
18515 if (! DECL_ARTIFICIAL (decl))
18516 add_src_coords_attributes (seg_die, decl);
18517
18518 add_linkage_name (seg_die, decl);
18519 }
18520 gcc_assert (name != NULL);
18521 add_pure_or_virtual_attribute (seg_die, decl);
18522 if (DECL_ARTIFICIAL (decl))
18523 add_AT_flag (seg_die, DW_AT_artificial, 1);
18524
18525 name = concat ("__second_sect_of_", name, NULL);
18526 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18527 fde->dw_fde_second_end, false);
18528 add_name_attribute (seg_die, name);
18529 if (want_pubnames ())
18530 add_pubname_string (name, seg_die);
18531 }
18532 }
18533 else
18534 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18535 false);
18536 }
18537
18538 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18539
18540 /* We define the "frame base" as the function's CFA. This is more
18541 convenient for several reasons: (1) It's stable across the prologue
18542 and epilogue, which makes it better than just a frame pointer,
18543 (2) With dwarf3, there exists a one-byte encoding that allows us
18544 to reference the .debug_frame data by proxy, but failing that,
18545 (3) We can at least reuse the code inspection and interpretation
18546 code that determines the CFA position at various points in the
18547 function. */
18548 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18549 {
18550 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18551 add_AT_loc (subr_die, DW_AT_frame_base, op);
18552 }
18553 else
18554 {
18555 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18556 if (list->dw_loc_next)
18557 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18558 else
18559 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18560 }
18561
18562 /* Compute a displacement from the "steady-state frame pointer" to
18563 the CFA. The former is what all stack slots and argument slots
18564 will reference in the rtl; the latter is what we've told the
18565 debugger about. We'll need to adjust all frame_base references
18566 by this displacement. */
18567 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18568
18569 if (fun->static_chain_decl)
18570 add_AT_location_description (subr_die, DW_AT_static_link,
18571 loc_list_from_tree (fun->static_chain_decl, 2));
18572 }
18573
18574 /* Generate child dies for template paramaters. */
18575 if (debug_info_level > DINFO_LEVEL_TERSE)
18576 gen_generic_params_dies (decl);
18577
18578 /* Now output descriptions of the arguments for this function. This gets
18579 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18580 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18581 `...' at the end of the formal parameter list. In order to find out if
18582 there was a trailing ellipsis or not, we must instead look at the type
18583 associated with the FUNCTION_DECL. This will be a node of type
18584 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18585 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18586 an ellipsis at the end. */
18587
18588 /* In the case where we are describing a mere function declaration, all we
18589 need to do here (and all we *can* do here) is to describe the *types* of
18590 its formal parameters. */
18591 if (debug_info_level <= DINFO_LEVEL_TERSE)
18592 ;
18593 else if (declaration)
18594 gen_formal_types_die (decl, subr_die);
18595 else
18596 {
18597 /* Generate DIEs to represent all known formal parameters. */
18598 tree parm = DECL_ARGUMENTS (decl);
18599 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18600 tree generic_decl_parm = generic_decl
18601 ? DECL_ARGUMENTS (generic_decl)
18602 : NULL;
18603
18604 /* Now we want to walk the list of parameters of the function and
18605 emit their relevant DIEs.
18606
18607 We consider the case of DECL being an instance of a generic function
18608 as well as it being a normal function.
18609
18610 If DECL is an instance of a generic function we walk the
18611 parameters of the generic function declaration _and_ the parameters of
18612 DECL itself. This is useful because we want to emit specific DIEs for
18613 function parameter packs and those are declared as part of the
18614 generic function declaration. In that particular case,
18615 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18616 That DIE has children DIEs representing the set of arguments
18617 of the pack. Note that the set of pack arguments can be empty.
18618 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18619 children DIE.
18620
18621 Otherwise, we just consider the parameters of DECL. */
18622 while (generic_decl_parm || parm)
18623 {
18624 if (generic_decl_parm
18625 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18626 gen_formal_parameter_pack_die (generic_decl_parm,
18627 parm, subr_die,
18628 &parm);
18629 else if (parm && !POINTER_BOUNDS_P (parm))
18630 {
18631 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18632
18633 if (parm == DECL_ARGUMENTS (decl)
18634 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18635 && parm_die
18636 && (dwarf_version >= 3 || !dwarf_strict))
18637 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18638
18639 parm = DECL_CHAIN (parm);
18640 }
18641 else if (parm)
18642 parm = DECL_CHAIN (parm);
18643
18644 if (generic_decl_parm)
18645 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18646 }
18647
18648 /* Decide whether we need an unspecified_parameters DIE at the end.
18649 There are 2 more cases to do this for: 1) the ansi ... declaration -
18650 this is detectable when the end of the arg list is not a
18651 void_type_node 2) an unprototyped function declaration (not a
18652 definition). This just means that we have no info about the
18653 parameters at all. */
18654 if (prototype_p (TREE_TYPE (decl)))
18655 {
18656 /* This is the prototyped case, check for.... */
18657 if (stdarg_p (TREE_TYPE (decl)))
18658 gen_unspecified_parameters_die (decl, subr_die);
18659 }
18660 else if (DECL_INITIAL (decl) == NULL_TREE)
18661 gen_unspecified_parameters_die (decl, subr_die);
18662 }
18663
18664 /* Output Dwarf info for all of the stuff within the body of the function
18665 (if it has one - it may be just a declaration). */
18666 outer_scope = DECL_INITIAL (decl);
18667
18668 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18669 a function. This BLOCK actually represents the outermost binding contour
18670 for the function, i.e. the contour in which the function's formal
18671 parameters and labels get declared. Curiously, it appears that the front
18672 end doesn't actually put the PARM_DECL nodes for the current function onto
18673 the BLOCK_VARS list for this outer scope, but are strung off of the
18674 DECL_ARGUMENTS list for the function instead.
18675
18676 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18677 the LABEL_DECL nodes for the function however, and we output DWARF info
18678 for those in decls_for_scope. Just within the `outer_scope' there will be
18679 a BLOCK node representing the function's outermost pair of curly braces,
18680 and any blocks used for the base and member initializers of a C++
18681 constructor function. */
18682 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18683 {
18684 int call_site_note_count = 0;
18685 int tail_call_site_note_count = 0;
18686
18687 /* Emit a DW_TAG_variable DIE for a named return value. */
18688 if (DECL_NAME (DECL_RESULT (decl)))
18689 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18690
18691 decls_for_scope (outer_scope, subr_die, 0);
18692
18693 if (call_arg_locations && !dwarf_strict)
18694 {
18695 struct call_arg_loc_node *ca_loc;
18696 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18697 {
18698 dw_die_ref die = NULL;
18699 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18700 rtx arg, next_arg;
18701
18702 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18703 arg; arg = next_arg)
18704 {
18705 dw_loc_descr_ref reg, val;
18706 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18707 dw_die_ref cdie, tdie = NULL;
18708
18709 next_arg = XEXP (arg, 1);
18710 if (REG_P (XEXP (XEXP (arg, 0), 0))
18711 && next_arg
18712 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18713 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18714 && REGNO (XEXP (XEXP (arg, 0), 0))
18715 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18716 next_arg = XEXP (next_arg, 1);
18717 if (mode == VOIDmode)
18718 {
18719 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18720 if (mode == VOIDmode)
18721 mode = GET_MODE (XEXP (arg, 0));
18722 }
18723 if (mode == VOIDmode || mode == BLKmode)
18724 continue;
18725 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18726 {
18727 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18728 tloc = XEXP (XEXP (arg, 0), 1);
18729 continue;
18730 }
18731 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18732 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18733 {
18734 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18735 tlocc = XEXP (XEXP (arg, 0), 1);
18736 continue;
18737 }
18738 reg = NULL;
18739 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18740 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18741 VAR_INIT_STATUS_INITIALIZED);
18742 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18743 {
18744 rtx mem = XEXP (XEXP (arg, 0), 0);
18745 reg = mem_loc_descriptor (XEXP (mem, 0),
18746 get_address_mode (mem),
18747 GET_MODE (mem),
18748 VAR_INIT_STATUS_INITIALIZED);
18749 }
18750 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18751 == DEBUG_PARAMETER_REF)
18752 {
18753 tree tdecl
18754 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18755 tdie = lookup_decl_die (tdecl);
18756 if (tdie == NULL)
18757 continue;
18758 }
18759 else
18760 continue;
18761 if (reg == NULL
18762 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18763 != DEBUG_PARAMETER_REF)
18764 continue;
18765 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18766 VOIDmode,
18767 VAR_INIT_STATUS_INITIALIZED);
18768 if (val == NULL)
18769 continue;
18770 if (die == NULL)
18771 die = gen_call_site_die (decl, subr_die, ca_loc);
18772 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18773 NULL_TREE);
18774 if (reg != NULL)
18775 add_AT_loc (cdie, DW_AT_location, reg);
18776 else if (tdie != NULL)
18777 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18778 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18779 if (next_arg != XEXP (arg, 1))
18780 {
18781 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18782 if (mode == VOIDmode)
18783 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18784 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18785 0), 1),
18786 mode, VOIDmode,
18787 VAR_INIT_STATUS_INITIALIZED);
18788 if (val != NULL)
18789 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18790 }
18791 }
18792 if (die == NULL
18793 && (ca_loc->symbol_ref || tloc))
18794 die = gen_call_site_die (decl, subr_die, ca_loc);
18795 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18796 {
18797 dw_loc_descr_ref tval = NULL;
18798
18799 if (tloc != NULL_RTX)
18800 tval = mem_loc_descriptor (tloc,
18801 GET_MODE (tloc) == VOIDmode
18802 ? Pmode : GET_MODE (tloc),
18803 VOIDmode,
18804 VAR_INIT_STATUS_INITIALIZED);
18805 if (tval)
18806 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18807 else if (tlocc != NULL_RTX)
18808 {
18809 tval = mem_loc_descriptor (tlocc,
18810 GET_MODE (tlocc) == VOIDmode
18811 ? Pmode : GET_MODE (tlocc),
18812 VOIDmode,
18813 VAR_INIT_STATUS_INITIALIZED);
18814 if (tval)
18815 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18816 tval);
18817 }
18818 }
18819 if (die != NULL)
18820 {
18821 call_site_note_count++;
18822 if (ca_loc->tail_call_p)
18823 tail_call_site_note_count++;
18824 }
18825 }
18826 }
18827 call_arg_locations = NULL;
18828 call_arg_loc_last = NULL;
18829 if (tail_call_site_count >= 0
18830 && tail_call_site_count == tail_call_site_note_count
18831 && !dwarf_strict)
18832 {
18833 if (call_site_count >= 0
18834 && call_site_count == call_site_note_count)
18835 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18836 else
18837 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18838 }
18839 call_site_count = -1;
18840 tail_call_site_count = -1;
18841 }
18842
18843 if (subr_die != old_die)
18844 /* Add the calling convention attribute if requested. */
18845 add_calling_convention_attribute (subr_die, decl);
18846 }
18847
18848 /* Returns a hash value for X (which really is a die_struct). */
18849
18850 hashval_t
18851 block_die_hasher::hash (die_struct *d)
18852 {
18853 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18854 }
18855
18856 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18857 as decl_id and die_parent of die_struct Y. */
18858
18859 bool
18860 block_die_hasher::equal (die_struct *x, die_struct *y)
18861 {
18862 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18863 }
18864
18865 /* Generate a DIE to represent a declared data object.
18866 Either DECL or ORIGIN must be non-null. */
18867
18868 static void
18869 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18870 {
18871 HOST_WIDE_INT off = 0;
18872 tree com_decl;
18873 tree decl_or_origin = decl ? decl : origin;
18874 tree ultimate_origin;
18875 dw_die_ref var_die;
18876 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18877 dw_die_ref origin_die;
18878 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18879 || class_or_namespace_scope_p (context_die));
18880 bool specialization_p = false;
18881
18882 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18883 if (decl || ultimate_origin)
18884 origin = ultimate_origin;
18885 com_decl = fortran_common (decl_or_origin, &off);
18886
18887 /* Symbol in common gets emitted as a child of the common block, in the form
18888 of a data member. */
18889 if (com_decl)
18890 {
18891 dw_die_ref com_die;
18892 dw_loc_list_ref loc;
18893 die_node com_die_arg;
18894
18895 var_die = lookup_decl_die (decl_or_origin);
18896 if (var_die)
18897 {
18898 if (get_AT (var_die, DW_AT_location) == NULL)
18899 {
18900 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18901 if (loc)
18902 {
18903 if (off)
18904 {
18905 /* Optimize the common case. */
18906 if (single_element_loc_list_p (loc)
18907 && loc->expr->dw_loc_opc == DW_OP_addr
18908 && loc->expr->dw_loc_next == NULL
18909 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18910 == SYMBOL_REF)
18911 {
18912 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18913 loc->expr->dw_loc_oprnd1.v.val_addr
18914 = plus_constant (GET_MODE (x), x , off);
18915 }
18916 else
18917 loc_list_plus_const (loc, off);
18918 }
18919 add_AT_location_description (var_die, DW_AT_location, loc);
18920 remove_AT (var_die, DW_AT_declaration);
18921 }
18922 }
18923 return;
18924 }
18925
18926 if (common_block_die_table == NULL)
18927 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
18928
18929 com_die_arg.decl_id = DECL_UID (com_decl);
18930 com_die_arg.die_parent = context_die;
18931 com_die = common_block_die_table->find (&com_die_arg);
18932 loc = loc_list_from_tree (com_decl, 2);
18933 if (com_die == NULL)
18934 {
18935 const char *cnam
18936 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18937 die_node **slot;
18938
18939 com_die = new_die (DW_TAG_common_block, context_die, decl);
18940 add_name_and_src_coords_attributes (com_die, com_decl);
18941 if (loc)
18942 {
18943 add_AT_location_description (com_die, DW_AT_location, loc);
18944 /* Avoid sharing the same loc descriptor between
18945 DW_TAG_common_block and DW_TAG_variable. */
18946 loc = loc_list_from_tree (com_decl, 2);
18947 }
18948 else if (DECL_EXTERNAL (decl))
18949 add_AT_flag (com_die, DW_AT_declaration, 1);
18950 if (want_pubnames ())
18951 add_pubname_string (cnam, com_die); /* ??? needed? */
18952 com_die->decl_id = DECL_UID (com_decl);
18953 slot = common_block_die_table->find_slot (com_die, INSERT);
18954 *slot = com_die;
18955 }
18956 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18957 {
18958 add_AT_location_description (com_die, DW_AT_location, loc);
18959 loc = loc_list_from_tree (com_decl, 2);
18960 remove_AT (com_die, DW_AT_declaration);
18961 }
18962 var_die = new_die (DW_TAG_variable, com_die, decl);
18963 add_name_and_src_coords_attributes (var_die, decl);
18964 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18965 context_die);
18966 add_AT_flag (var_die, DW_AT_external, 1);
18967 if (loc)
18968 {
18969 if (off)
18970 {
18971 /* Optimize the common case. */
18972 if (single_element_loc_list_p (loc)
18973 && loc->expr->dw_loc_opc == DW_OP_addr
18974 && loc->expr->dw_loc_next == NULL
18975 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18976 {
18977 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18978 loc->expr->dw_loc_oprnd1.v.val_addr
18979 = plus_constant (GET_MODE (x), x, off);
18980 }
18981 else
18982 loc_list_plus_const (loc, off);
18983 }
18984 add_AT_location_description (var_die, DW_AT_location, loc);
18985 }
18986 else if (DECL_EXTERNAL (decl))
18987 add_AT_flag (var_die, DW_AT_declaration, 1);
18988 equate_decl_number_to_die (decl, var_die);
18989 return;
18990 }
18991
18992 /* If the compiler emitted a definition for the DECL declaration
18993 and if we already emitted a DIE for it, don't emit a second
18994 DIE for it again. Allow re-declarations of DECLs that are
18995 inside functions, though. */
18996 if (old_die && declaration && !local_scope_p (context_die))
18997 return;
18998
18999 /* For static data members, the declaration in the class is supposed
19000 to have DW_TAG_member tag; the specification should still be
19001 DW_TAG_variable referencing the DW_TAG_member DIE. */
19002 if (declaration && class_scope_p (context_die))
19003 var_die = new_die (DW_TAG_member, context_die, decl);
19004 else
19005 var_die = new_die (DW_TAG_variable, context_die, decl);
19006
19007 origin_die = NULL;
19008 if (origin != NULL)
19009 origin_die = add_abstract_origin_attribute (var_die, origin);
19010
19011 /* Loop unrolling can create multiple blocks that refer to the same
19012 static variable, so we must test for the DW_AT_declaration flag.
19013
19014 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19015 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19016 sharing them.
19017
19018 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19019
19020 ??? The declare_in_namespace support causes us to get two DIEs for one
19021 variable, both of which are declarations. We want to avoid considering
19022 one to be a specification, so we must test that this DIE is not a
19023 declaration. */
19024 else if (old_die && TREE_STATIC (decl) && ! declaration
19025 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19026 {
19027 /* This is a definition of a C++ class level static. */
19028 add_AT_specification (var_die, old_die);
19029 specialization_p = true;
19030 if (DECL_NAME (decl))
19031 {
19032 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19033 struct dwarf_file_data * file_index = lookup_filename (s.file);
19034
19035 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19036 add_AT_file (var_die, DW_AT_decl_file, file_index);
19037
19038 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19039 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19040
19041 if (old_die->die_tag == DW_TAG_member)
19042 add_linkage_name (var_die, decl);
19043 }
19044 }
19045 else
19046 add_name_and_src_coords_attributes (var_die, decl);
19047
19048 if ((origin == NULL && !specialization_p)
19049 || (origin != NULL
19050 && !DECL_ABSTRACT_P (decl_or_origin)
19051 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19052 decl_function_context
19053 (decl_or_origin))))
19054 {
19055 tree type = TREE_TYPE (decl_or_origin);
19056
19057 if (decl_by_reference_p (decl_or_origin))
19058 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19059 context_die);
19060 else
19061 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19062 context_die);
19063 }
19064
19065 if (origin == NULL && !specialization_p)
19066 {
19067 if (TREE_PUBLIC (decl))
19068 add_AT_flag (var_die, DW_AT_external, 1);
19069
19070 if (DECL_ARTIFICIAL (decl))
19071 add_AT_flag (var_die, DW_AT_artificial, 1);
19072
19073 add_accessibility_attribute (var_die, decl);
19074 }
19075
19076 if (declaration)
19077 add_AT_flag (var_die, DW_AT_declaration, 1);
19078
19079 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19080 equate_decl_number_to_die (decl, var_die);
19081
19082 if (! declaration
19083 && (! DECL_ABSTRACT_P (decl_or_origin)
19084 /* Local static vars are shared between all clones/inlines,
19085 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19086 already set. */
19087 || (TREE_CODE (decl_or_origin) == VAR_DECL
19088 && TREE_STATIC (decl_or_origin)
19089 && DECL_RTL_SET_P (decl_or_origin)))
19090 /* When abstract origin already has DW_AT_location attribute, no need
19091 to add it again. */
19092 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19093 {
19094 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19095 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19096 defer_location (decl_or_origin, var_die);
19097 else
19098 add_location_or_const_value_attribute (var_die, decl_or_origin,
19099 decl == NULL, DW_AT_location);
19100 add_pubname (decl_or_origin, var_die);
19101 }
19102 else
19103 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19104 }
19105
19106 /* Generate a DIE to represent a named constant. */
19107
19108 static void
19109 gen_const_die (tree decl, dw_die_ref context_die)
19110 {
19111 dw_die_ref const_die;
19112 tree type = TREE_TYPE (decl);
19113
19114 const_die = new_die (DW_TAG_constant, context_die, decl);
19115 add_name_and_src_coords_attributes (const_die, decl);
19116 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19117 if (TREE_PUBLIC (decl))
19118 add_AT_flag (const_die, DW_AT_external, 1);
19119 if (DECL_ARTIFICIAL (decl))
19120 add_AT_flag (const_die, DW_AT_artificial, 1);
19121 tree_add_const_value_attribute_for_decl (const_die, decl);
19122 }
19123
19124 /* Generate a DIE to represent a label identifier. */
19125
19126 static void
19127 gen_label_die (tree decl, dw_die_ref context_die)
19128 {
19129 tree origin = decl_ultimate_origin (decl);
19130 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19131 rtx insn;
19132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19133
19134 if (origin != NULL)
19135 add_abstract_origin_attribute (lbl_die, origin);
19136 else
19137 add_name_and_src_coords_attributes (lbl_die, decl);
19138
19139 if (DECL_ABSTRACT_P (decl))
19140 equate_decl_number_to_die (decl, lbl_die);
19141 else
19142 {
19143 insn = DECL_RTL_IF_SET (decl);
19144
19145 /* Deleted labels are programmer specified labels which have been
19146 eliminated because of various optimizations. We still emit them
19147 here so that it is possible to put breakpoints on them. */
19148 if (insn
19149 && (LABEL_P (insn)
19150 || ((NOTE_P (insn)
19151 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19152 {
19153 /* When optimization is enabled (via -O) some parts of the compiler
19154 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19155 represent source-level labels which were explicitly declared by
19156 the user. This really shouldn't be happening though, so catch
19157 it if it ever does happen. */
19158 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19159
19160 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19161 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19162 }
19163 else if (insn
19164 && NOTE_P (insn)
19165 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19166 && CODE_LABEL_NUMBER (insn) != -1)
19167 {
19168 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19169 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19170 }
19171 }
19172 }
19173
19174 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19175 attributes to the DIE for a block STMT, to describe where the inlined
19176 function was called from. This is similar to add_src_coords_attributes. */
19177
19178 static inline void
19179 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19180 {
19181 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19182
19183 if (dwarf_version >= 3 || !dwarf_strict)
19184 {
19185 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19186 add_AT_unsigned (die, DW_AT_call_line, s.line);
19187 }
19188 }
19189
19190
19191 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19192 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19193
19194 static inline void
19195 add_high_low_attributes (tree stmt, dw_die_ref die)
19196 {
19197 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19198
19199 if (BLOCK_FRAGMENT_CHAIN (stmt)
19200 && (dwarf_version >= 3 || !dwarf_strict))
19201 {
19202 tree chain, superblock = NULL_TREE;
19203 dw_die_ref pdie;
19204 dw_attr_ref attr = NULL;
19205
19206 if (inlined_function_outer_scope_p (stmt))
19207 {
19208 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19209 BLOCK_NUMBER (stmt));
19210 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19211 }
19212
19213 /* Optimize duplicate .debug_ranges lists or even tails of
19214 lists. If this BLOCK has same ranges as its supercontext,
19215 lookup DW_AT_ranges attribute in the supercontext (and
19216 recursively so), verify that the ranges_table contains the
19217 right values and use it instead of adding a new .debug_range. */
19218 for (chain = stmt, pdie = die;
19219 BLOCK_SAME_RANGE (chain);
19220 chain = BLOCK_SUPERCONTEXT (chain))
19221 {
19222 dw_attr_ref new_attr;
19223
19224 pdie = pdie->die_parent;
19225 if (pdie == NULL)
19226 break;
19227 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19228 break;
19229 new_attr = get_AT (pdie, DW_AT_ranges);
19230 if (new_attr == NULL
19231 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19232 break;
19233 attr = new_attr;
19234 superblock = BLOCK_SUPERCONTEXT (chain);
19235 }
19236 if (attr != NULL
19237 && (ranges_table[attr->dw_attr_val.v.val_offset
19238 / 2 / DWARF2_ADDR_SIZE].num
19239 == BLOCK_NUMBER (superblock))
19240 && BLOCK_FRAGMENT_CHAIN (superblock))
19241 {
19242 unsigned long off = attr->dw_attr_val.v.val_offset
19243 / 2 / DWARF2_ADDR_SIZE;
19244 unsigned long supercnt = 0, thiscnt = 0;
19245 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19246 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19247 {
19248 ++supercnt;
19249 gcc_checking_assert (ranges_table[off + supercnt].num
19250 == BLOCK_NUMBER (chain));
19251 }
19252 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19253 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19254 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19255 ++thiscnt;
19256 gcc_assert (supercnt >= thiscnt);
19257 add_AT_range_list (die, DW_AT_ranges,
19258 ((off + supercnt - thiscnt)
19259 * 2 * DWARF2_ADDR_SIZE),
19260 false);
19261 return;
19262 }
19263
19264 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19265
19266 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19267 do
19268 {
19269 add_ranges (chain);
19270 chain = BLOCK_FRAGMENT_CHAIN (chain);
19271 }
19272 while (chain);
19273 add_ranges (NULL);
19274 }
19275 else
19276 {
19277 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19278 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19279 BLOCK_NUMBER (stmt));
19280 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19281 BLOCK_NUMBER (stmt));
19282 add_AT_low_high_pc (die, label, label_high, false);
19283 }
19284 }
19285
19286 /* Generate a DIE for a lexical block. */
19287
19288 static void
19289 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19290 {
19291 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19292
19293 if (call_arg_locations)
19294 {
19295 if (block_map.length () <= BLOCK_NUMBER (stmt))
19296 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19297 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19298 }
19299
19300 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19301 add_high_low_attributes (stmt, stmt_die);
19302
19303 decls_for_scope (stmt, stmt_die, depth);
19304 }
19305
19306 /* Generate a DIE for an inlined subprogram. */
19307
19308 static void
19309 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19310 {
19311 tree decl;
19312
19313 /* The instance of function that is effectively being inlined shall not
19314 be abstract. */
19315 gcc_assert (! BLOCK_ABSTRACT (stmt));
19316
19317 decl = block_ultimate_origin (stmt);
19318
19319 /* Emit info for the abstract instance first, if we haven't yet. We
19320 must emit this even if the block is abstract, otherwise when we
19321 emit the block below (or elsewhere), we may end up trying to emit
19322 a die whose origin die hasn't been emitted, and crashing. */
19323 dwarf2out_abstract_function (decl);
19324
19325 if (! BLOCK_ABSTRACT (stmt))
19326 {
19327 dw_die_ref subr_die
19328 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19329
19330 if (call_arg_locations)
19331 {
19332 if (block_map.length () <= BLOCK_NUMBER (stmt))
19333 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19334 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19335 }
19336 add_abstract_origin_attribute (subr_die, decl);
19337 if (TREE_ASM_WRITTEN (stmt))
19338 add_high_low_attributes (stmt, subr_die);
19339 add_call_src_coords_attributes (stmt, subr_die);
19340
19341 decls_for_scope (stmt, subr_die, depth);
19342 }
19343 }
19344
19345 /* Generate a DIE for a field in a record, or structure. */
19346
19347 static void
19348 gen_field_die (tree decl, dw_die_ref context_die)
19349 {
19350 dw_die_ref decl_die;
19351
19352 if (TREE_TYPE (decl) == error_mark_node)
19353 return;
19354
19355 decl_die = new_die (DW_TAG_member, context_die, decl);
19356 add_name_and_src_coords_attributes (decl_die, decl);
19357 add_type_attribute (decl_die, member_declared_type (decl),
19358 decl_quals (decl), context_die);
19359
19360 if (DECL_BIT_FIELD_TYPE (decl))
19361 {
19362 add_byte_size_attribute (decl_die, decl);
19363 add_bit_size_attribute (decl_die, decl);
19364 add_bit_offset_attribute (decl_die, decl);
19365 }
19366
19367 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19368 add_data_member_location_attribute (decl_die, decl);
19369
19370 if (DECL_ARTIFICIAL (decl))
19371 add_AT_flag (decl_die, DW_AT_artificial, 1);
19372
19373 add_accessibility_attribute (decl_die, decl);
19374
19375 /* Equate decl number to die, so that we can look up this decl later on. */
19376 equate_decl_number_to_die (decl, decl_die);
19377 }
19378
19379 #if 0
19380 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19381 Use modified_type_die instead.
19382 We keep this code here just in case these types of DIEs may be needed to
19383 represent certain things in other languages (e.g. Pascal) someday. */
19384
19385 static void
19386 gen_pointer_type_die (tree type, dw_die_ref context_die)
19387 {
19388 dw_die_ref ptr_die
19389 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19390
19391 equate_type_number_to_die (type, ptr_die);
19392 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19393 context_die);
19394 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19395 }
19396
19397 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19398 Use modified_type_die instead.
19399 We keep this code here just in case these types of DIEs may be needed to
19400 represent certain things in other languages (e.g. Pascal) someday. */
19401
19402 static void
19403 gen_reference_type_die (tree type, dw_die_ref context_die)
19404 {
19405 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19406
19407 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19408 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19409 else
19410 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19411
19412 equate_type_number_to_die (type, ref_die);
19413 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19414 context_die);
19415 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19416 }
19417 #endif
19418
19419 /* Generate a DIE for a pointer to a member type. */
19420
19421 static void
19422 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19423 {
19424 dw_die_ref ptr_die
19425 = new_die (DW_TAG_ptr_to_member_type,
19426 scope_die_for (type, context_die), type);
19427
19428 equate_type_number_to_die (type, ptr_die);
19429 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19430 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19431 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19432 context_die);
19433 }
19434
19435 typedef const char *dchar_p; /* For DEF_VEC_P. */
19436
19437 static char *producer_string;
19438
19439 /* Return a heap allocated producer string including command line options
19440 if -grecord-gcc-switches. */
19441
19442 static char *
19443 gen_producer_string (void)
19444 {
19445 size_t j;
19446 auto_vec<dchar_p> switches;
19447 const char *language_string = lang_hooks.name;
19448 char *producer, *tail;
19449 const char *p;
19450 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19451 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19452
19453 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19454 switch (save_decoded_options[j].opt_index)
19455 {
19456 case OPT_o:
19457 case OPT_d:
19458 case OPT_dumpbase:
19459 case OPT_dumpdir:
19460 case OPT_auxbase:
19461 case OPT_auxbase_strip:
19462 case OPT_quiet:
19463 case OPT_version:
19464 case OPT_v:
19465 case OPT_w:
19466 case OPT_L:
19467 case OPT_D:
19468 case OPT_I:
19469 case OPT_U:
19470 case OPT_SPECIAL_unknown:
19471 case OPT_SPECIAL_ignore:
19472 case OPT_SPECIAL_program_name:
19473 case OPT_SPECIAL_input_file:
19474 case OPT_grecord_gcc_switches:
19475 case OPT_gno_record_gcc_switches:
19476 case OPT__output_pch_:
19477 case OPT_fdiagnostics_show_location_:
19478 case OPT_fdiagnostics_show_option:
19479 case OPT_fdiagnostics_show_caret:
19480 case OPT_fdiagnostics_color_:
19481 case OPT_fverbose_asm:
19482 case OPT____:
19483 case OPT__sysroot_:
19484 case OPT_nostdinc:
19485 case OPT_nostdinc__:
19486 /* Ignore these. */
19487 continue;
19488 default:
19489 if (cl_options[save_decoded_options[j].opt_index].flags
19490 & CL_NO_DWARF_RECORD)
19491 continue;
19492 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19493 == '-');
19494 switch (save_decoded_options[j].canonical_option[0][1])
19495 {
19496 case 'M':
19497 case 'i':
19498 case 'W':
19499 continue;
19500 case 'f':
19501 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19502 "dump", 4) == 0)
19503 continue;
19504 break;
19505 default:
19506 break;
19507 }
19508 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19509 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19510 break;
19511 }
19512
19513 producer = XNEWVEC (char, plen + 1 + len + 1);
19514 tail = producer;
19515 sprintf (tail, "%s %s", language_string, version_string);
19516 tail += plen;
19517
19518 FOR_EACH_VEC_ELT (switches, j, p)
19519 {
19520 len = strlen (p);
19521 *tail = ' ';
19522 memcpy (tail + 1, p, len);
19523 tail += len + 1;
19524 }
19525
19526 *tail = '\0';
19527 return producer;
19528 }
19529
19530 /* Given a C and/or C++ language/version string return the "highest".
19531 C++ is assumed to be "higher" than C in this case. Used for merging
19532 LTO translation unit languages. */
19533 static const char *
19534 highest_c_language (const char *lang1, const char *lang2)
19535 {
19536 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19537 return "GNU C++14";
19538 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19539 return "GNU C++11";
19540 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19541 return "GNU C++98";
19542
19543 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19544 return "GNU C11";
19545 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19546 return "GNU C99";
19547 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19548 return "GNU C89";
19549
19550 gcc_unreachable ();
19551 }
19552
19553
19554 /* Generate the DIE for the compilation unit. */
19555
19556 static dw_die_ref
19557 gen_compile_unit_die (const char *filename)
19558 {
19559 dw_die_ref die;
19560 const char *language_string = lang_hooks.name;
19561 int language;
19562
19563 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19564
19565 if (filename)
19566 {
19567 add_name_attribute (die, filename);
19568 /* Don't add cwd for <built-in>. */
19569 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19570 add_comp_dir_attribute (die);
19571 }
19572
19573 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19574
19575 /* If our producer is LTO try to figure out a common language to use
19576 from the global list of translation units. */
19577 if (strcmp (language_string, "GNU GIMPLE") == 0)
19578 {
19579 unsigned i;
19580 tree t;
19581 const char *common_lang = NULL;
19582
19583 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19584 {
19585 if (!TRANSLATION_UNIT_LANGUAGE (t))
19586 continue;
19587 if (!common_lang)
19588 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19589 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19590 ;
19591 else if (strncmp (common_lang, "GNU C", 5) == 0
19592 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19593 /* Mixing C and C++ is ok, use C++ in that case. */
19594 common_lang = highest_c_language (common_lang,
19595 TRANSLATION_UNIT_LANGUAGE (t));
19596 else
19597 {
19598 /* Fall back to C. */
19599 common_lang = NULL;
19600 break;
19601 }
19602 }
19603
19604 if (common_lang)
19605 language_string = common_lang;
19606 }
19607
19608 language = DW_LANG_C;
19609 if (strncmp (language_string, "GNU C", 5) == 0
19610 && ISDIGIT (language_string[5]))
19611 {
19612 language = DW_LANG_C89;
19613 if (dwarf_version >= 3 || !dwarf_strict)
19614 {
19615 if (strcmp (language_string, "GNU C89") != 0)
19616 language = DW_LANG_C99;
19617
19618 if (dwarf_version >= 5 /* || !dwarf_strict */)
19619 if (strcmp (language_string, "GNU C11") == 0)
19620 language = DW_LANG_C11;
19621 }
19622 }
19623 else if (strncmp (language_string, "GNU C++", 7) == 0)
19624 {
19625 language = DW_LANG_C_plus_plus;
19626 if (dwarf_version >= 5 /* || !dwarf_strict */)
19627 {
19628 if (strcmp (language_string, "GNU C++11") == 0)
19629 language = DW_LANG_C_plus_plus_11;
19630 else if (strcmp (language_string, "GNU C++14") == 0)
19631 language = DW_LANG_C_plus_plus_14;
19632 }
19633 }
19634 else if (strcmp (language_string, "GNU F77") == 0)
19635 language = DW_LANG_Fortran77;
19636 else if (strcmp (language_string, "GNU Pascal") == 0)
19637 language = DW_LANG_Pascal83;
19638 else if (dwarf_version >= 3 || !dwarf_strict)
19639 {
19640 if (strcmp (language_string, "GNU Ada") == 0)
19641 language = DW_LANG_Ada95;
19642 else if (strcmp (language_string, "GNU Fortran") == 0)
19643 language = DW_LANG_Fortran95;
19644 else if (strcmp (language_string, "GNU Java") == 0)
19645 language = DW_LANG_Java;
19646 else if (strcmp (language_string, "GNU Objective-C") == 0)
19647 language = DW_LANG_ObjC;
19648 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19649 language = DW_LANG_ObjC_plus_plus;
19650 else if (dwarf_version >= 5 || !dwarf_strict)
19651 {
19652 if (strcmp (language_string, "GNU Go") == 0)
19653 language = DW_LANG_Go;
19654 }
19655 }
19656 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19657 else if (strcmp (language_string, "GNU Fortran") == 0)
19658 language = DW_LANG_Fortran90;
19659
19660 add_AT_unsigned (die, DW_AT_language, language);
19661
19662 switch (language)
19663 {
19664 case DW_LANG_Fortran77:
19665 case DW_LANG_Fortran90:
19666 case DW_LANG_Fortran95:
19667 /* Fortran has case insensitive identifiers and the front-end
19668 lowercases everything. */
19669 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19670 break;
19671 default:
19672 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19673 break;
19674 }
19675 return die;
19676 }
19677
19678 /* Generate the DIE for a base class. */
19679
19680 static void
19681 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19682 {
19683 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19684
19685 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19686 add_data_member_location_attribute (die, binfo);
19687
19688 if (BINFO_VIRTUAL_P (binfo))
19689 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19690
19691 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19692 children, otherwise the default is DW_ACCESS_public. In DWARF2
19693 the default has always been DW_ACCESS_private. */
19694 if (access == access_public_node)
19695 {
19696 if (dwarf_version == 2
19697 || context_die->die_tag == DW_TAG_class_type)
19698 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19699 }
19700 else if (access == access_protected_node)
19701 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19702 else if (dwarf_version > 2
19703 && context_die->die_tag != DW_TAG_class_type)
19704 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19705 }
19706
19707 /* Generate a DIE for a class member. */
19708
19709 static void
19710 gen_member_die (tree type, dw_die_ref context_die)
19711 {
19712 tree member;
19713 tree binfo = TYPE_BINFO (type);
19714 dw_die_ref child;
19715
19716 /* If this is not an incomplete type, output descriptions of each of its
19717 members. Note that as we output the DIEs necessary to represent the
19718 members of this record or union type, we will also be trying to output
19719 DIEs to represent the *types* of those members. However the `type'
19720 function (above) will specifically avoid generating type DIEs for member
19721 types *within* the list of member DIEs for this (containing) type except
19722 for those types (of members) which are explicitly marked as also being
19723 members of this (containing) type themselves. The g++ front- end can
19724 force any given type to be treated as a member of some other (containing)
19725 type by setting the TYPE_CONTEXT of the given (member) type to point to
19726 the TREE node representing the appropriate (containing) type. */
19727
19728 /* First output info about the base classes. */
19729 if (binfo)
19730 {
19731 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19732 int i;
19733 tree base;
19734
19735 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19736 gen_inheritance_die (base,
19737 (accesses ? (*accesses)[i] : access_public_node),
19738 context_die);
19739 }
19740
19741 /* Now output info about the data members and type members. */
19742 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19743 {
19744 /* If we thought we were generating minimal debug info for TYPE
19745 and then changed our minds, some of the member declarations
19746 may have already been defined. Don't define them again, but
19747 do put them in the right order. */
19748
19749 child = lookup_decl_die (member);
19750 if (child)
19751 splice_child_die (context_die, child);
19752 else
19753 gen_decl_die (member, NULL, context_die);
19754 }
19755
19756 /* Now output info about the function members (if any). */
19757 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19758 {
19759 /* Don't include clones in the member list. */
19760 if (DECL_ABSTRACT_ORIGIN (member))
19761 continue;
19762
19763 child = lookup_decl_die (member);
19764 if (child)
19765 splice_child_die (context_die, child);
19766 else
19767 gen_decl_die (member, NULL, context_die);
19768 }
19769 }
19770
19771 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19772 is set, we pretend that the type was never defined, so we only get the
19773 member DIEs needed by later specification DIEs. */
19774
19775 static void
19776 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19777 enum debug_info_usage usage)
19778 {
19779 dw_die_ref type_die = lookup_type_die (type);
19780 dw_die_ref scope_die = 0;
19781 int nested = 0;
19782 int complete = (TYPE_SIZE (type)
19783 && (! TYPE_STUB_DECL (type)
19784 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19785 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19786 complete = complete && should_emit_struct_debug (type, usage);
19787
19788 if (type_die && ! complete)
19789 return;
19790
19791 if (TYPE_CONTEXT (type) != NULL_TREE
19792 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19793 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19794 nested = 1;
19795
19796 scope_die = scope_die_for (type, context_die);
19797
19798 /* Generate child dies for template paramaters. */
19799 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19800 schedule_generic_params_dies_gen (type);
19801
19802 if (! type_die || (nested && is_cu_die (scope_die)))
19803 /* First occurrence of type or toplevel definition of nested class. */
19804 {
19805 dw_die_ref old_die = type_die;
19806
19807 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19808 ? record_type_tag (type) : DW_TAG_union_type,
19809 scope_die, type);
19810 equate_type_number_to_die (type, type_die);
19811 if (old_die)
19812 add_AT_specification (type_die, old_die);
19813 else
19814 add_name_attribute (type_die, type_tag (type));
19815 }
19816 else
19817 remove_AT (type_die, DW_AT_declaration);
19818
19819 /* If this type has been completed, then give it a byte_size attribute and
19820 then give a list of members. */
19821 if (complete && !ns_decl)
19822 {
19823 /* Prevent infinite recursion in cases where the type of some member of
19824 this type is expressed in terms of this type itself. */
19825 TREE_ASM_WRITTEN (type) = 1;
19826 add_byte_size_attribute (type_die, type);
19827 if (TYPE_STUB_DECL (type) != NULL_TREE)
19828 {
19829 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19830 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19831 }
19832
19833 /* If the first reference to this type was as the return type of an
19834 inline function, then it may not have a parent. Fix this now. */
19835 if (type_die->die_parent == NULL)
19836 add_child_die (scope_die, type_die);
19837
19838 push_decl_scope (type);
19839 gen_member_die (type, type_die);
19840 pop_decl_scope ();
19841
19842 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19843 if (TYPE_ARTIFICIAL (type))
19844 add_AT_flag (type_die, DW_AT_artificial, 1);
19845
19846 /* GNU extension: Record what type our vtable lives in. */
19847 if (TYPE_VFIELD (type))
19848 {
19849 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19850
19851 gen_type_die (vtype, context_die);
19852 add_AT_die_ref (type_die, DW_AT_containing_type,
19853 lookup_type_die (vtype));
19854 }
19855 }
19856 else
19857 {
19858 add_AT_flag (type_die, DW_AT_declaration, 1);
19859
19860 /* We don't need to do this for function-local types. */
19861 if (TYPE_STUB_DECL (type)
19862 && ! decl_function_context (TYPE_STUB_DECL (type)))
19863 vec_safe_push (incomplete_types, type);
19864 }
19865
19866 if (get_AT (type_die, DW_AT_name))
19867 add_pubtype (type, type_die);
19868 }
19869
19870 /* Generate a DIE for a subroutine _type_. */
19871
19872 static void
19873 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19874 {
19875 tree return_type = TREE_TYPE (type);
19876 dw_die_ref subr_die
19877 = new_die (DW_TAG_subroutine_type,
19878 scope_die_for (type, context_die), type);
19879
19880 equate_type_number_to_die (type, subr_die);
19881 add_prototyped_attribute (subr_die, type);
19882 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19883 gen_formal_types_die (type, subr_die);
19884
19885 if (get_AT (subr_die, DW_AT_name))
19886 add_pubtype (type, subr_die);
19887 }
19888
19889 /* Generate a DIE for a type definition. */
19890
19891 static void
19892 gen_typedef_die (tree decl, dw_die_ref context_die)
19893 {
19894 dw_die_ref type_die;
19895 tree origin;
19896
19897 if (TREE_ASM_WRITTEN (decl))
19898 return;
19899
19900 TREE_ASM_WRITTEN (decl) = 1;
19901 type_die = new_die (DW_TAG_typedef, context_die, decl);
19902 origin = decl_ultimate_origin (decl);
19903 if (origin != NULL)
19904 add_abstract_origin_attribute (type_die, origin);
19905 else
19906 {
19907 tree type;
19908
19909 add_name_and_src_coords_attributes (type_die, decl);
19910 if (DECL_ORIGINAL_TYPE (decl))
19911 {
19912 type = DECL_ORIGINAL_TYPE (decl);
19913
19914 gcc_assert (type != TREE_TYPE (decl));
19915 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19916 }
19917 else
19918 {
19919 type = TREE_TYPE (decl);
19920
19921 if (is_naming_typedef_decl (TYPE_NAME (type)))
19922 {
19923 /* Here, we are in the case of decl being a typedef naming
19924 an anonymous type, e.g:
19925 typedef struct {...} foo;
19926 In that case TREE_TYPE (decl) is not a typedef variant
19927 type and TYPE_NAME of the anonymous type is set to the
19928 TYPE_DECL of the typedef. This construct is emitted by
19929 the C++ FE.
19930
19931 TYPE is the anonymous struct named by the typedef
19932 DECL. As we need the DW_AT_type attribute of the
19933 DW_TAG_typedef to point to the DIE of TYPE, let's
19934 generate that DIE right away. add_type_attribute
19935 called below will then pick (via lookup_type_die) that
19936 anonymous struct DIE. */
19937 if (!TREE_ASM_WRITTEN (type))
19938 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19939
19940 /* This is a GNU Extension. We are adding a
19941 DW_AT_linkage_name attribute to the DIE of the
19942 anonymous struct TYPE. The value of that attribute
19943 is the name of the typedef decl naming the anonymous
19944 struct. This greatly eases the work of consumers of
19945 this debug info. */
19946 add_linkage_attr (lookup_type_die (type), decl);
19947 }
19948 }
19949
19950 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19951
19952 if (is_naming_typedef_decl (decl))
19953 /* We want that all subsequent calls to lookup_type_die with
19954 TYPE in argument yield the DW_TAG_typedef we have just
19955 created. */
19956 equate_type_number_to_die (type, type_die);
19957
19958 add_accessibility_attribute (type_die, decl);
19959 }
19960
19961 if (DECL_ABSTRACT_P (decl))
19962 equate_decl_number_to_die (decl, type_die);
19963
19964 if (get_AT (type_die, DW_AT_name))
19965 add_pubtype (decl, type_die);
19966 }
19967
19968 /* Generate a DIE for a struct, class, enum or union type. */
19969
19970 static void
19971 gen_tagged_type_die (tree type,
19972 dw_die_ref context_die,
19973 enum debug_info_usage usage)
19974 {
19975 int need_pop;
19976
19977 if (type == NULL_TREE
19978 || !is_tagged_type (type))
19979 return;
19980
19981 /* If this is a nested type whose containing class hasn't been written
19982 out yet, writing it out will cover this one, too. This does not apply
19983 to instantiations of member class templates; they need to be added to
19984 the containing class as they are generated. FIXME: This hurts the
19985 idea of combining type decls from multiple TUs, since we can't predict
19986 what set of template instantiations we'll get. */
19987 if (TYPE_CONTEXT (type)
19988 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19989 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19990 {
19991 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19992
19993 if (TREE_ASM_WRITTEN (type))
19994 return;
19995
19996 /* If that failed, attach ourselves to the stub. */
19997 push_decl_scope (TYPE_CONTEXT (type));
19998 context_die = lookup_type_die (TYPE_CONTEXT (type));
19999 need_pop = 1;
20000 }
20001 else if (TYPE_CONTEXT (type) != NULL_TREE
20002 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20003 {
20004 /* If this type is local to a function that hasn't been written
20005 out yet, use a NULL context for now; it will be fixed up in
20006 decls_for_scope. */
20007 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20008 /* A declaration DIE doesn't count; nested types need to go in the
20009 specification. */
20010 if (context_die && is_declaration_die (context_die))
20011 context_die = NULL;
20012 need_pop = 0;
20013 }
20014 else
20015 {
20016 context_die = declare_in_namespace (type, context_die);
20017 need_pop = 0;
20018 }
20019
20020 if (TREE_CODE (type) == ENUMERAL_TYPE)
20021 {
20022 /* This might have been written out by the call to
20023 declare_in_namespace. */
20024 if (!TREE_ASM_WRITTEN (type))
20025 gen_enumeration_type_die (type, context_die);
20026 }
20027 else
20028 gen_struct_or_union_type_die (type, context_die, usage);
20029
20030 if (need_pop)
20031 pop_decl_scope ();
20032
20033 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20034 it up if it is ever completed. gen_*_type_die will set it for us
20035 when appropriate. */
20036 }
20037
20038 /* Generate a type description DIE. */
20039
20040 static void
20041 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20042 enum debug_info_usage usage)
20043 {
20044 struct array_descr_info info;
20045
20046 if (type == NULL_TREE || type == error_mark_node)
20047 return;
20048
20049 if (TYPE_NAME (type) != NULL_TREE
20050 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20051 && is_redundant_typedef (TYPE_NAME (type))
20052 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20053 /* The DECL of this type is a typedef we don't want to emit debug
20054 info for but we want debug info for its underlying typedef.
20055 This can happen for e.g, the injected-class-name of a C++
20056 type. */
20057 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20058
20059 /* If TYPE is a typedef type variant, let's generate debug info
20060 for the parent typedef which TYPE is a type of. */
20061 if (typedef_variant_p (type))
20062 {
20063 if (TREE_ASM_WRITTEN (type))
20064 return;
20065
20066 /* Prevent broken recursion; we can't hand off to the same type. */
20067 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20068
20069 /* Give typedefs the right scope. */
20070 context_die = scope_die_for (type, context_die);
20071
20072 TREE_ASM_WRITTEN (type) = 1;
20073
20074 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20075 return;
20076 }
20077
20078 /* If type is an anonymous tagged type named by a typedef, let's
20079 generate debug info for the typedef. */
20080 if (is_naming_typedef_decl (TYPE_NAME (type)))
20081 {
20082 /* Use the DIE of the containing namespace as the parent DIE of
20083 the type description DIE we want to generate. */
20084 if (DECL_CONTEXT (TYPE_NAME (type))
20085 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20086 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20087
20088 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20089 return;
20090 }
20091
20092 /* If this is an array type with hidden descriptor, handle it first. */
20093 if (!TREE_ASM_WRITTEN (type)
20094 && lang_hooks.types.get_array_descr_info
20095 && lang_hooks.types.get_array_descr_info (type, &info)
20096 && (dwarf_version >= 3 || !dwarf_strict))
20097 {
20098 gen_descr_array_type_die (type, &info, context_die);
20099 TREE_ASM_WRITTEN (type) = 1;
20100 return;
20101 }
20102
20103 /* We are going to output a DIE to represent the unqualified version
20104 of this type (i.e. without any const or volatile qualifiers) so
20105 get the main variant (i.e. the unqualified version) of this type
20106 now. (Vectors are special because the debugging info is in the
20107 cloned type itself). */
20108 if (TREE_CODE (type) != VECTOR_TYPE)
20109 type = type_main_variant (type);
20110
20111 if (TREE_ASM_WRITTEN (type))
20112 return;
20113
20114 switch (TREE_CODE (type))
20115 {
20116 case ERROR_MARK:
20117 break;
20118
20119 case POINTER_TYPE:
20120 case REFERENCE_TYPE:
20121 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20122 ensures that the gen_type_die recursion will terminate even if the
20123 type is recursive. Recursive types are possible in Ada. */
20124 /* ??? We could perhaps do this for all types before the switch
20125 statement. */
20126 TREE_ASM_WRITTEN (type) = 1;
20127
20128 /* For these types, all that is required is that we output a DIE (or a
20129 set of DIEs) to represent the "basis" type. */
20130 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20131 DINFO_USAGE_IND_USE);
20132 break;
20133
20134 case OFFSET_TYPE:
20135 /* This code is used for C++ pointer-to-data-member types.
20136 Output a description of the relevant class type. */
20137 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20138 DINFO_USAGE_IND_USE);
20139
20140 /* Output a description of the type of the object pointed to. */
20141 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20142 DINFO_USAGE_IND_USE);
20143
20144 /* Now output a DIE to represent this pointer-to-data-member type
20145 itself. */
20146 gen_ptr_to_mbr_type_die (type, context_die);
20147 break;
20148
20149 case FUNCTION_TYPE:
20150 /* Force out return type (in case it wasn't forced out already). */
20151 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20152 DINFO_USAGE_DIR_USE);
20153 gen_subroutine_type_die (type, context_die);
20154 break;
20155
20156 case METHOD_TYPE:
20157 /* Force out return type (in case it wasn't forced out already). */
20158 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20159 DINFO_USAGE_DIR_USE);
20160 gen_subroutine_type_die (type, context_die);
20161 break;
20162
20163 case ARRAY_TYPE:
20164 gen_array_type_die (type, context_die);
20165 break;
20166
20167 case VECTOR_TYPE:
20168 gen_array_type_die (type, context_die);
20169 break;
20170
20171 case ENUMERAL_TYPE:
20172 case RECORD_TYPE:
20173 case UNION_TYPE:
20174 case QUAL_UNION_TYPE:
20175 gen_tagged_type_die (type, context_die, usage);
20176 return;
20177
20178 case VOID_TYPE:
20179 case INTEGER_TYPE:
20180 case REAL_TYPE:
20181 case FIXED_POINT_TYPE:
20182 case COMPLEX_TYPE:
20183 case BOOLEAN_TYPE:
20184 case POINTER_BOUNDS_TYPE:
20185 /* No DIEs needed for fundamental types. */
20186 break;
20187
20188 case NULLPTR_TYPE:
20189 case LANG_TYPE:
20190 /* Just use DW_TAG_unspecified_type. */
20191 {
20192 dw_die_ref type_die = lookup_type_die (type);
20193 if (type_die == NULL)
20194 {
20195 tree name = TYPE_IDENTIFIER (type);
20196 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20197 type);
20198 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20199 equate_type_number_to_die (type, type_die);
20200 }
20201 }
20202 break;
20203
20204 default:
20205 if (is_cxx_auto (type))
20206 {
20207 tree name = TYPE_IDENTIFIER (type);
20208 dw_die_ref *die = (name == get_identifier ("auto")
20209 ? &auto_die : &decltype_auto_die);
20210 if (!*die)
20211 {
20212 *die = new_die (DW_TAG_unspecified_type,
20213 comp_unit_die (), NULL_TREE);
20214 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20215 }
20216 equate_type_number_to_die (type, *die);
20217 break;
20218 }
20219 gcc_unreachable ();
20220 }
20221
20222 TREE_ASM_WRITTEN (type) = 1;
20223 }
20224
20225 static void
20226 gen_type_die (tree type, dw_die_ref context_die)
20227 {
20228 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20229 }
20230
20231 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20232 things which are local to the given block. */
20233
20234 static void
20235 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20236 {
20237 int must_output_die = 0;
20238 bool inlined_func;
20239
20240 /* Ignore blocks that are NULL. */
20241 if (stmt == NULL_TREE)
20242 return;
20243
20244 inlined_func = inlined_function_outer_scope_p (stmt);
20245
20246 /* If the block is one fragment of a non-contiguous block, do not
20247 process the variables, since they will have been done by the
20248 origin block. Do process subblocks. */
20249 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20250 {
20251 tree sub;
20252
20253 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20254 gen_block_die (sub, context_die, depth + 1);
20255
20256 return;
20257 }
20258
20259 /* Determine if we need to output any Dwarf DIEs at all to represent this
20260 block. */
20261 if (inlined_func)
20262 /* The outer scopes for inlinings *must* always be represented. We
20263 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20264 must_output_die = 1;
20265 else
20266 {
20267 /* Determine if this block directly contains any "significant"
20268 local declarations which we will need to output DIEs for. */
20269 if (debug_info_level > DINFO_LEVEL_TERSE)
20270 /* We are not in terse mode so *any* local declaration counts
20271 as being a "significant" one. */
20272 must_output_die = ((BLOCK_VARS (stmt) != NULL
20273 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20274 && (TREE_USED (stmt)
20275 || TREE_ASM_WRITTEN (stmt)
20276 || BLOCK_ABSTRACT (stmt)));
20277 else if ((TREE_USED (stmt)
20278 || TREE_ASM_WRITTEN (stmt)
20279 || BLOCK_ABSTRACT (stmt))
20280 && !dwarf2out_ignore_block (stmt))
20281 must_output_die = 1;
20282 }
20283
20284 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20285 DIE for any block which contains no significant local declarations at
20286 all. Rather, in such cases we just call `decls_for_scope' so that any
20287 needed Dwarf info for any sub-blocks will get properly generated. Note
20288 that in terse mode, our definition of what constitutes a "significant"
20289 local declaration gets restricted to include only inlined function
20290 instances and local (nested) function definitions. */
20291 if (must_output_die)
20292 {
20293 if (inlined_func)
20294 {
20295 /* If STMT block is abstract, that means we have been called
20296 indirectly from dwarf2out_abstract_function.
20297 That function rightfully marks the descendent blocks (of
20298 the abstract function it is dealing with) as being abstract,
20299 precisely to prevent us from emitting any
20300 DW_TAG_inlined_subroutine DIE as a descendent
20301 of an abstract function instance. So in that case, we should
20302 not call gen_inlined_subroutine_die.
20303
20304 Later though, when cgraph asks dwarf2out to emit info
20305 for the concrete instance of the function decl into which
20306 the concrete instance of STMT got inlined, the later will lead
20307 to the generation of a DW_TAG_inlined_subroutine DIE. */
20308 if (! BLOCK_ABSTRACT (stmt))
20309 gen_inlined_subroutine_die (stmt, context_die, depth);
20310 }
20311 else
20312 gen_lexical_block_die (stmt, context_die, depth);
20313 }
20314 else
20315 decls_for_scope (stmt, context_die, depth);
20316 }
20317
20318 /* Process variable DECL (or variable with origin ORIGIN) within
20319 block STMT and add it to CONTEXT_DIE. */
20320 static void
20321 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20322 {
20323 dw_die_ref die;
20324 tree decl_or_origin = decl ? decl : origin;
20325
20326 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20327 die = lookup_decl_die (decl_or_origin);
20328 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20329 && TYPE_DECL_IS_STUB (decl_or_origin))
20330 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20331 else
20332 die = NULL;
20333
20334 if (die != NULL && die->die_parent == NULL)
20335 add_child_die (context_die, die);
20336 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20337 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20338 stmt, context_die);
20339 else
20340 gen_decl_die (decl, origin, context_die);
20341 }
20342
20343 /* Generate all of the decls declared within a given scope and (recursively)
20344 all of its sub-blocks. */
20345
20346 static void
20347 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20348 {
20349 tree decl;
20350 unsigned int i;
20351 tree subblocks;
20352
20353 /* Ignore NULL blocks. */
20354 if (stmt == NULL_TREE)
20355 return;
20356
20357 /* Output the DIEs to represent all of the data objects and typedefs
20358 declared directly within this block but not within any nested
20359 sub-blocks. Also, nested function and tag DIEs have been
20360 generated with a parent of NULL; fix that up now. We don't
20361 have to do this if we're at -g1. */
20362 if (debug_info_level > DINFO_LEVEL_TERSE)
20363 {
20364 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20365 process_scope_var (stmt, decl, NULL_TREE, context_die);
20366 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20367 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20368 context_die);
20369 }
20370
20371 /* Even if we're at -g1, we need to process the subblocks in order to get
20372 inlined call information. */
20373
20374 /* Output the DIEs to represent all sub-blocks (and the items declared
20375 therein) of this block. */
20376 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20377 subblocks != NULL;
20378 subblocks = BLOCK_CHAIN (subblocks))
20379 gen_block_die (subblocks, context_die, depth + 1);
20380 }
20381
20382 /* Is this a typedef we can avoid emitting? */
20383
20384 static inline int
20385 is_redundant_typedef (const_tree decl)
20386 {
20387 if (TYPE_DECL_IS_STUB (decl))
20388 return 1;
20389
20390 if (DECL_ARTIFICIAL (decl)
20391 && DECL_CONTEXT (decl)
20392 && is_tagged_type (DECL_CONTEXT (decl))
20393 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20394 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20395 /* Also ignore the artificial member typedef for the class name. */
20396 return 1;
20397
20398 return 0;
20399 }
20400
20401 /* Return TRUE if TYPE is a typedef that names a type for linkage
20402 purposes. This kind of typedefs is produced by the C++ FE for
20403 constructs like:
20404
20405 typedef struct {...} foo;
20406
20407 In that case, there is no typedef variant type produced for foo.
20408 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20409 struct type. */
20410
20411 static bool
20412 is_naming_typedef_decl (const_tree decl)
20413 {
20414 if (decl == NULL_TREE
20415 || TREE_CODE (decl) != TYPE_DECL
20416 || !is_tagged_type (TREE_TYPE (decl))
20417 || DECL_IS_BUILTIN (decl)
20418 || is_redundant_typedef (decl)
20419 /* It looks like Ada produces TYPE_DECLs that are very similar
20420 to C++ naming typedefs but that have different
20421 semantics. Let's be specific to c++ for now. */
20422 || !is_cxx ())
20423 return FALSE;
20424
20425 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20426 && TYPE_NAME (TREE_TYPE (decl)) == decl
20427 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20428 != TYPE_NAME (TREE_TYPE (decl))));
20429 }
20430
20431 /* Returns the DIE for a context. */
20432
20433 static inline dw_die_ref
20434 get_context_die (tree context)
20435 {
20436 if (context)
20437 {
20438 /* Find die that represents this context. */
20439 if (TYPE_P (context))
20440 {
20441 context = TYPE_MAIN_VARIANT (context);
20442 return strip_naming_typedef (context, force_type_die (context));
20443 }
20444 else
20445 return force_decl_die (context);
20446 }
20447 return comp_unit_die ();
20448 }
20449
20450 /* Returns the DIE for decl. A DIE will always be returned. */
20451
20452 static dw_die_ref
20453 force_decl_die (tree decl)
20454 {
20455 dw_die_ref decl_die;
20456 unsigned saved_external_flag;
20457 tree save_fn = NULL_TREE;
20458 decl_die = lookup_decl_die (decl);
20459 if (!decl_die)
20460 {
20461 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20462
20463 decl_die = lookup_decl_die (decl);
20464 if (decl_die)
20465 return decl_die;
20466
20467 switch (TREE_CODE (decl))
20468 {
20469 case FUNCTION_DECL:
20470 /* Clear current_function_decl, so that gen_subprogram_die thinks
20471 that this is a declaration. At this point, we just want to force
20472 declaration die. */
20473 save_fn = current_function_decl;
20474 current_function_decl = NULL_TREE;
20475 gen_subprogram_die (decl, context_die);
20476 current_function_decl = save_fn;
20477 break;
20478
20479 case VAR_DECL:
20480 /* Set external flag to force declaration die. Restore it after
20481 gen_decl_die() call. */
20482 saved_external_flag = DECL_EXTERNAL (decl);
20483 DECL_EXTERNAL (decl) = 1;
20484 gen_decl_die (decl, NULL, context_die);
20485 DECL_EXTERNAL (decl) = saved_external_flag;
20486 break;
20487
20488 case NAMESPACE_DECL:
20489 if (dwarf_version >= 3 || !dwarf_strict)
20490 dwarf2out_decl (decl);
20491 else
20492 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20493 decl_die = comp_unit_die ();
20494 break;
20495
20496 case TRANSLATION_UNIT_DECL:
20497 decl_die = comp_unit_die ();
20498 break;
20499
20500 default:
20501 gcc_unreachable ();
20502 }
20503
20504 /* We should be able to find the DIE now. */
20505 if (!decl_die)
20506 decl_die = lookup_decl_die (decl);
20507 gcc_assert (decl_die);
20508 }
20509
20510 return decl_die;
20511 }
20512
20513 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20514 always returned. */
20515
20516 static dw_die_ref
20517 force_type_die (tree type)
20518 {
20519 dw_die_ref type_die;
20520
20521 type_die = lookup_type_die (type);
20522 if (!type_die)
20523 {
20524 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20525
20526 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20527 context_die);
20528 gcc_assert (type_die);
20529 }
20530 return type_die;
20531 }
20532
20533 /* Force out any required namespaces to be able to output DECL,
20534 and return the new context_die for it, if it's changed. */
20535
20536 static dw_die_ref
20537 setup_namespace_context (tree thing, dw_die_ref context_die)
20538 {
20539 tree context = (DECL_P (thing)
20540 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20541 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20542 /* Force out the namespace. */
20543 context_die = force_decl_die (context);
20544
20545 return context_die;
20546 }
20547
20548 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20549 type) within its namespace, if appropriate.
20550
20551 For compatibility with older debuggers, namespace DIEs only contain
20552 declarations; all definitions are emitted at CU scope. */
20553
20554 static dw_die_ref
20555 declare_in_namespace (tree thing, dw_die_ref context_die)
20556 {
20557 dw_die_ref ns_context;
20558
20559 if (debug_info_level <= DINFO_LEVEL_TERSE)
20560 return context_die;
20561
20562 /* External declarations in the local scope only need to be emitted
20563 once, not once in the namespace and once in the scope.
20564
20565 This avoids declaring the `extern' below in the
20566 namespace DIE as well as in the innermost scope:
20567
20568 namespace S
20569 {
20570 int i=5;
20571 int foo()
20572 {
20573 int i=8;
20574 extern int i;
20575 return i;
20576 }
20577 }
20578 */
20579 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20580 return context_die;
20581
20582 /* If this decl is from an inlined function, then don't try to emit it in its
20583 namespace, as we will get confused. It would have already been emitted
20584 when the abstract instance of the inline function was emitted anyways. */
20585 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20586 return context_die;
20587
20588 ns_context = setup_namespace_context (thing, context_die);
20589
20590 if (ns_context != context_die)
20591 {
20592 if (is_fortran ())
20593 return ns_context;
20594 if (DECL_P (thing))
20595 gen_decl_die (thing, NULL, ns_context);
20596 else
20597 gen_type_die (thing, ns_context);
20598 }
20599 return context_die;
20600 }
20601
20602 /* Generate a DIE for a namespace or namespace alias. */
20603
20604 static void
20605 gen_namespace_die (tree decl, dw_die_ref context_die)
20606 {
20607 dw_die_ref namespace_die;
20608
20609 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20610 they are an alias of. */
20611 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20612 {
20613 /* Output a real namespace or module. */
20614 context_die = setup_namespace_context (decl, comp_unit_die ());
20615 namespace_die = new_die (is_fortran ()
20616 ? DW_TAG_module : DW_TAG_namespace,
20617 context_die, decl);
20618 /* For Fortran modules defined in different CU don't add src coords. */
20619 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20620 {
20621 const char *name = dwarf2_name (decl, 0);
20622 if (name)
20623 add_name_attribute (namespace_die, name);
20624 }
20625 else
20626 add_name_and_src_coords_attributes (namespace_die, decl);
20627 if (DECL_EXTERNAL (decl))
20628 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20629 equate_decl_number_to_die (decl, namespace_die);
20630 }
20631 else
20632 {
20633 /* Output a namespace alias. */
20634
20635 /* Force out the namespace we are an alias of, if necessary. */
20636 dw_die_ref origin_die
20637 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20638
20639 if (DECL_FILE_SCOPE_P (decl)
20640 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20641 context_die = setup_namespace_context (decl, comp_unit_die ());
20642 /* Now create the namespace alias DIE. */
20643 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20644 add_name_and_src_coords_attributes (namespace_die, decl);
20645 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20646 equate_decl_number_to_die (decl, namespace_die);
20647 }
20648 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20649 if (want_pubnames ())
20650 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20651 }
20652
20653 /* Generate Dwarf debug information for a decl described by DECL.
20654 The return value is currently only meaningful for PARM_DECLs,
20655 for all other decls it returns NULL. */
20656
20657 static dw_die_ref
20658 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20659 {
20660 tree decl_or_origin = decl ? decl : origin;
20661 tree class_origin = NULL, ultimate_origin;
20662
20663 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20664 return NULL;
20665
20666 /* Ignore pointer bounds decls. */
20667 if (DECL_P (decl_or_origin)
20668 && TREE_TYPE (decl_or_origin)
20669 && POINTER_BOUNDS_P (decl_or_origin))
20670 return NULL;
20671
20672 switch (TREE_CODE (decl_or_origin))
20673 {
20674 case ERROR_MARK:
20675 break;
20676
20677 case CONST_DECL:
20678 if (!is_fortran () && !is_ada ())
20679 {
20680 /* The individual enumerators of an enum type get output when we output
20681 the Dwarf representation of the relevant enum type itself. */
20682 break;
20683 }
20684
20685 /* Emit its type. */
20686 gen_type_die (TREE_TYPE (decl), context_die);
20687
20688 /* And its containing namespace. */
20689 context_die = declare_in_namespace (decl, context_die);
20690
20691 gen_const_die (decl, context_die);
20692 break;
20693
20694 case FUNCTION_DECL:
20695 /* Don't output any DIEs to represent mere function declarations,
20696 unless they are class members or explicit block externs. */
20697 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20698 && DECL_FILE_SCOPE_P (decl_or_origin)
20699 && (current_function_decl == NULL_TREE
20700 || DECL_ARTIFICIAL (decl_or_origin)))
20701 break;
20702
20703 #if 0
20704 /* FIXME */
20705 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20706 on local redeclarations of global functions. That seems broken. */
20707 if (current_function_decl != decl)
20708 /* This is only a declaration. */;
20709 #endif
20710
20711 /* If we're emitting a clone, emit info for the abstract instance. */
20712 if (origin || DECL_ORIGIN (decl) != decl)
20713 dwarf2out_abstract_function (origin
20714 ? DECL_ORIGIN (origin)
20715 : DECL_ABSTRACT_ORIGIN (decl));
20716
20717 /* If we're emitting an out-of-line copy of an inline function,
20718 emit info for the abstract instance and set up to refer to it. */
20719 else if (cgraph_function_possibly_inlined_p (decl)
20720 && ! DECL_ABSTRACT_P (decl)
20721 && ! class_or_namespace_scope_p (context_die)
20722 /* dwarf2out_abstract_function won't emit a die if this is just
20723 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20724 that case, because that works only if we have a die. */
20725 && DECL_INITIAL (decl) != NULL_TREE)
20726 {
20727 dwarf2out_abstract_function (decl);
20728 set_decl_origin_self (decl);
20729 }
20730
20731 /* Otherwise we're emitting the primary DIE for this decl. */
20732 else if (debug_info_level > DINFO_LEVEL_TERSE)
20733 {
20734 /* Before we describe the FUNCTION_DECL itself, make sure that we
20735 have its containing type. */
20736 if (!origin)
20737 origin = decl_class_context (decl);
20738 if (origin != NULL_TREE)
20739 gen_type_die (origin, context_die);
20740
20741 /* And its return type. */
20742 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20743
20744 /* And its virtual context. */
20745 if (DECL_VINDEX (decl) != NULL_TREE)
20746 gen_type_die (DECL_CONTEXT (decl), context_die);
20747
20748 /* Make sure we have a member DIE for decl. */
20749 if (origin != NULL_TREE)
20750 gen_type_die_for_member (origin, decl, context_die);
20751
20752 /* And its containing namespace. */
20753 context_die = declare_in_namespace (decl, context_die);
20754 }
20755
20756 /* Now output a DIE to represent the function itself. */
20757 if (decl)
20758 gen_subprogram_die (decl, context_die);
20759 break;
20760
20761 case TYPE_DECL:
20762 /* If we are in terse mode, don't generate any DIEs to represent any
20763 actual typedefs. */
20764 if (debug_info_level <= DINFO_LEVEL_TERSE)
20765 break;
20766
20767 /* In the special case of a TYPE_DECL node representing the declaration
20768 of some type tag, if the given TYPE_DECL is marked as having been
20769 instantiated from some other (original) TYPE_DECL node (e.g. one which
20770 was generated within the original definition of an inline function) we
20771 used to generate a special (abbreviated) DW_TAG_structure_type,
20772 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20773 should be actually referencing those DIEs, as variable DIEs with that
20774 type would be emitted already in the abstract origin, so it was always
20775 removed during unused type prunning. Don't add anything in this
20776 case. */
20777 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20778 break;
20779
20780 if (is_redundant_typedef (decl))
20781 gen_type_die (TREE_TYPE (decl), context_die);
20782 else
20783 /* Output a DIE to represent the typedef itself. */
20784 gen_typedef_die (decl, context_die);
20785 break;
20786
20787 case LABEL_DECL:
20788 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20789 gen_label_die (decl, context_die);
20790 break;
20791
20792 case VAR_DECL:
20793 case RESULT_DECL:
20794 /* If we are in terse mode, don't generate any DIEs to represent any
20795 variable declarations or definitions. */
20796 if (debug_info_level <= DINFO_LEVEL_TERSE)
20797 break;
20798
20799 /* Output any DIEs that are needed to specify the type of this data
20800 object. */
20801 if (decl_by_reference_p (decl_or_origin))
20802 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20803 else
20804 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20805
20806 /* And its containing type. */
20807 class_origin = decl_class_context (decl_or_origin);
20808 if (class_origin != NULL_TREE)
20809 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20810
20811 /* And its containing namespace. */
20812 context_die = declare_in_namespace (decl_or_origin, context_die);
20813
20814 /* Now output the DIE to represent the data object itself. This gets
20815 complicated because of the possibility that the VAR_DECL really
20816 represents an inlined instance of a formal parameter for an inline
20817 function. */
20818 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20819 if (ultimate_origin != NULL_TREE
20820 && TREE_CODE (ultimate_origin) == PARM_DECL)
20821 gen_formal_parameter_die (decl, origin,
20822 true /* Emit name attribute. */,
20823 context_die);
20824 else
20825 gen_variable_die (decl, origin, context_die);
20826 break;
20827
20828 case FIELD_DECL:
20829 /* Ignore the nameless fields that are used to skip bits but handle C++
20830 anonymous unions and structs. */
20831 if (DECL_NAME (decl) != NULL_TREE
20832 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20833 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20834 {
20835 gen_type_die (member_declared_type (decl), context_die);
20836 gen_field_die (decl, context_die);
20837 }
20838 break;
20839
20840 case PARM_DECL:
20841 if (DECL_BY_REFERENCE (decl_or_origin))
20842 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20843 else
20844 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20845 return gen_formal_parameter_die (decl, origin,
20846 true /* Emit name attribute. */,
20847 context_die);
20848
20849 case NAMESPACE_DECL:
20850 case IMPORTED_DECL:
20851 if (dwarf_version >= 3 || !dwarf_strict)
20852 gen_namespace_die (decl, context_die);
20853 break;
20854
20855 case NAMELIST_DECL:
20856 gen_namelist_decl (DECL_NAME (decl), context_die,
20857 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20858 break;
20859
20860 default:
20861 /* Probably some frontend-internal decl. Assume we don't care. */
20862 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20863 break;
20864 }
20865
20866 return NULL;
20867 }
20868 \f
20869 /* Output debug information for global decl DECL. Called from toplev.c after
20870 compilation proper has finished. */
20871
20872 static void
20873 dwarf2out_global_decl (tree decl)
20874 {
20875 /* Output DWARF2 information for file-scope tentative data object
20876 declarations, file-scope (extern) function declarations (which
20877 had no corresponding body) and file-scope tagged type declarations
20878 and definitions which have not yet been forced out. */
20879 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20880 && !POINTER_BOUNDS_P (decl))
20881 dwarf2out_decl (decl);
20882 }
20883
20884 /* Output debug information for type decl DECL. Called from toplev.c
20885 and from language front ends (to record built-in types). */
20886 static void
20887 dwarf2out_type_decl (tree decl, int local)
20888 {
20889 if (!local)
20890 dwarf2out_decl (decl);
20891 }
20892
20893 /* Output debug information for imported module or decl DECL.
20894 NAME is non-NULL name in the lexical block if the decl has been renamed.
20895 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20896 that DECL belongs to.
20897 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20898 static void
20899 dwarf2out_imported_module_or_decl_1 (tree decl,
20900 tree name,
20901 tree lexical_block,
20902 dw_die_ref lexical_block_die)
20903 {
20904 expanded_location xloc;
20905 dw_die_ref imported_die = NULL;
20906 dw_die_ref at_import_die;
20907
20908 if (TREE_CODE (decl) == IMPORTED_DECL)
20909 {
20910 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20911 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20912 gcc_assert (decl);
20913 }
20914 else
20915 xloc = expand_location (input_location);
20916
20917 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20918 {
20919 at_import_die = force_type_die (TREE_TYPE (decl));
20920 /* For namespace N { typedef void T; } using N::T; base_type_die
20921 returns NULL, but DW_TAG_imported_declaration requires
20922 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20923 if (!at_import_die)
20924 {
20925 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20926 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20927 at_import_die = lookup_type_die (TREE_TYPE (decl));
20928 gcc_assert (at_import_die);
20929 }
20930 }
20931 else
20932 {
20933 at_import_die = lookup_decl_die (decl);
20934 if (!at_import_die)
20935 {
20936 /* If we're trying to avoid duplicate debug info, we may not have
20937 emitted the member decl for this field. Emit it now. */
20938 if (TREE_CODE (decl) == FIELD_DECL)
20939 {
20940 tree type = DECL_CONTEXT (decl);
20941
20942 if (TYPE_CONTEXT (type)
20943 && TYPE_P (TYPE_CONTEXT (type))
20944 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20945 DINFO_USAGE_DIR_USE))
20946 return;
20947 gen_type_die_for_member (type, decl,
20948 get_context_die (TYPE_CONTEXT (type)));
20949 }
20950 if (TREE_CODE (decl) == NAMELIST_DECL)
20951 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20952 get_context_die (DECL_CONTEXT (decl)),
20953 NULL_TREE);
20954 else
20955 at_import_die = force_decl_die (decl);
20956 }
20957 }
20958
20959 if (TREE_CODE (decl) == NAMESPACE_DECL)
20960 {
20961 if (dwarf_version >= 3 || !dwarf_strict)
20962 imported_die = new_die (DW_TAG_imported_module,
20963 lexical_block_die,
20964 lexical_block);
20965 else
20966 return;
20967 }
20968 else
20969 imported_die = new_die (DW_TAG_imported_declaration,
20970 lexical_block_die,
20971 lexical_block);
20972
20973 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20974 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20975 if (name)
20976 add_AT_string (imported_die, DW_AT_name,
20977 IDENTIFIER_POINTER (name));
20978 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20979 }
20980
20981 /* Output debug information for imported module or decl DECL.
20982 NAME is non-NULL name in context if the decl has been renamed.
20983 CHILD is true if decl is one of the renamed decls as part of
20984 importing whole module. */
20985
20986 static void
20987 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20988 bool child)
20989 {
20990 /* dw_die_ref at_import_die; */
20991 dw_die_ref scope_die;
20992
20993 if (debug_info_level <= DINFO_LEVEL_TERSE)
20994 return;
20995
20996 gcc_assert (decl);
20997
20998 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20999 We need decl DIE for reference and scope die. First, get DIE for the decl
21000 itself. */
21001
21002 /* Get the scope die for decl context. Use comp_unit_die for global module
21003 or decl. If die is not found for non globals, force new die. */
21004 if (context
21005 && TYPE_P (context)
21006 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21007 return;
21008
21009 if (!(dwarf_version >= 3 || !dwarf_strict))
21010 return;
21011
21012 scope_die = get_context_die (context);
21013
21014 if (child)
21015 {
21016 gcc_assert (scope_die->die_child);
21017 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21018 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21019 scope_die = scope_die->die_child;
21020 }
21021
21022 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21023 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21024
21025 }
21026
21027 /* Output debug information for namelists. */
21028
21029 static dw_die_ref
21030 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21031 {
21032 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21033 tree value;
21034 unsigned i;
21035
21036 if (debug_info_level <= DINFO_LEVEL_TERSE)
21037 return NULL;
21038
21039 gcc_assert (scope_die != NULL);
21040 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21041 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21042
21043 /* If there are no item_decls, we have a nondefining namelist, e.g.
21044 with USE association; hence, set DW_AT_declaration. */
21045 if (item_decls == NULL_TREE)
21046 {
21047 add_AT_flag (nml_die, DW_AT_declaration, 1);
21048 return nml_die;
21049 }
21050
21051 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21052 {
21053 nml_item_ref_die = lookup_decl_die (value);
21054 if (!nml_item_ref_die)
21055 nml_item_ref_die = force_decl_die (value);
21056
21057 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21058 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21059 }
21060 return nml_die;
21061 }
21062
21063
21064 /* Write the debugging output for DECL. */
21065
21066 static void
21067 dwarf2out_decl (tree decl)
21068 {
21069 dw_die_ref context_die = comp_unit_die ();
21070
21071 switch (TREE_CODE (decl))
21072 {
21073 case ERROR_MARK:
21074 return;
21075
21076 case FUNCTION_DECL:
21077 /* What we would really like to do here is to filter out all mere
21078 file-scope declarations of file-scope functions which are never
21079 referenced later within this translation unit (and keep all of ones
21080 that *are* referenced later on) but we aren't clairvoyant, so we have
21081 no idea which functions will be referenced in the future (i.e. later
21082 on within the current translation unit). So here we just ignore all
21083 file-scope function declarations which are not also definitions. If
21084 and when the debugger needs to know something about these functions,
21085 it will have to hunt around and find the DWARF information associated
21086 with the definition of the function.
21087
21088 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21089 nodes represent definitions and which ones represent mere
21090 declarations. We have to check DECL_INITIAL instead. That's because
21091 the C front-end supports some weird semantics for "extern inline"
21092 function definitions. These can get inlined within the current
21093 translation unit (and thus, we need to generate Dwarf info for their
21094 abstract instances so that the Dwarf info for the concrete inlined
21095 instances can have something to refer to) but the compiler never
21096 generates any out-of-lines instances of such things (despite the fact
21097 that they *are* definitions).
21098
21099 The important point is that the C front-end marks these "extern
21100 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21101 them anyway. Note that the C++ front-end also plays some similar games
21102 for inline function definitions appearing within include files which
21103 also contain `#pragma interface' pragmas.
21104
21105 If we are called from dwarf2out_abstract_function output a DIE
21106 anyway. We can end up here this way with early inlining and LTO
21107 where the inlined function is output in a different LTRANS unit
21108 or not at all. */
21109 if (DECL_INITIAL (decl) == NULL_TREE
21110 && ! DECL_ABSTRACT_P (decl))
21111 return;
21112
21113 /* If we're a nested function, initially use a parent of NULL; if we're
21114 a plain function, this will be fixed up in decls_for_scope. If
21115 we're a method, it will be ignored, since we already have a DIE. */
21116 if (decl_function_context (decl)
21117 /* But if we're in terse mode, we don't care about scope. */
21118 && debug_info_level > DINFO_LEVEL_TERSE)
21119 context_die = NULL;
21120 break;
21121
21122 case VAR_DECL:
21123 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21124 declaration and if the declaration was never even referenced from
21125 within this entire compilation unit. We suppress these DIEs in
21126 order to save space in the .debug section (by eliminating entries
21127 which are probably useless). Note that we must not suppress
21128 block-local extern declarations (whether used or not) because that
21129 would screw-up the debugger's name lookup mechanism and cause it to
21130 miss things which really ought to be in scope at a given point. */
21131 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21132 return;
21133
21134 /* For local statics lookup proper context die. */
21135 if (TREE_STATIC (decl)
21136 && DECL_CONTEXT (decl)
21137 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21138 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21139
21140 /* If we are in terse mode, don't generate any DIEs to represent any
21141 variable declarations or definitions. */
21142 if (debug_info_level <= DINFO_LEVEL_TERSE)
21143 return;
21144 break;
21145
21146 case CONST_DECL:
21147 if (debug_info_level <= DINFO_LEVEL_TERSE)
21148 return;
21149 if (!is_fortran () && !is_ada ())
21150 return;
21151 if (TREE_STATIC (decl) && decl_function_context (decl))
21152 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21153 break;
21154
21155 case NAMESPACE_DECL:
21156 case IMPORTED_DECL:
21157 if (debug_info_level <= DINFO_LEVEL_TERSE)
21158 return;
21159 if (lookup_decl_die (decl) != NULL)
21160 return;
21161 break;
21162
21163 case TYPE_DECL:
21164 /* Don't emit stubs for types unless they are needed by other DIEs. */
21165 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21166 return;
21167
21168 /* Don't bother trying to generate any DIEs to represent any of the
21169 normal built-in types for the language we are compiling. */
21170 if (DECL_IS_BUILTIN (decl))
21171 return;
21172
21173 /* If we are in terse mode, don't generate any DIEs for types. */
21174 if (debug_info_level <= DINFO_LEVEL_TERSE)
21175 return;
21176
21177 /* If we're a function-scope tag, initially use a parent of NULL;
21178 this will be fixed up in decls_for_scope. */
21179 if (decl_function_context (decl))
21180 context_die = NULL;
21181
21182 break;
21183
21184 case NAMELIST_DECL:
21185 break;
21186
21187 default:
21188 return;
21189 }
21190
21191 gen_decl_die (decl, NULL, context_die);
21192 }
21193
21194 /* Write the debugging output for DECL. */
21195
21196 static void
21197 dwarf2out_function_decl (tree decl)
21198 {
21199 dwarf2out_decl (decl);
21200 call_arg_locations = NULL;
21201 call_arg_loc_last = NULL;
21202 call_site_count = -1;
21203 tail_call_site_count = -1;
21204 block_map.release ();
21205 decl_loc_table->empty ();
21206 cached_dw_loc_list_table->empty ();
21207 }
21208
21209 /* Output a marker (i.e. a label) for the beginning of the generated code for
21210 a lexical block. */
21211
21212 static void
21213 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21214 unsigned int blocknum)
21215 {
21216 switch_to_section (current_function_section ());
21217 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21218 }
21219
21220 /* Output a marker (i.e. a label) for the end of the generated code for a
21221 lexical block. */
21222
21223 static void
21224 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21225 {
21226 switch_to_section (current_function_section ());
21227 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21228 }
21229
21230 /* Returns nonzero if it is appropriate not to emit any debugging
21231 information for BLOCK, because it doesn't contain any instructions.
21232
21233 Don't allow this for blocks with nested functions or local classes
21234 as we would end up with orphans, and in the presence of scheduling
21235 we may end up calling them anyway. */
21236
21237 static bool
21238 dwarf2out_ignore_block (const_tree block)
21239 {
21240 tree decl;
21241 unsigned int i;
21242
21243 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21244 if (TREE_CODE (decl) == FUNCTION_DECL
21245 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21246 return 0;
21247 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21248 {
21249 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21250 if (TREE_CODE (decl) == FUNCTION_DECL
21251 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21252 return 0;
21253 }
21254
21255 return 1;
21256 }
21257
21258 /* Hash table routines for file_hash. */
21259
21260 bool
21261 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21262 {
21263 return filename_cmp (p1->filename, p2) == 0;
21264 }
21265
21266 hashval_t
21267 dwarf_file_hasher::hash (dwarf_file_data *p)
21268 {
21269 return htab_hash_string (p->filename);
21270 }
21271
21272 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21273 dwarf2out.c) and return its "index". The index of each (known) filename is
21274 just a unique number which is associated with only that one filename. We
21275 need such numbers for the sake of generating labels (in the .debug_sfnames
21276 section) and references to those files numbers (in the .debug_srcinfo
21277 and.debug_macinfo sections). If the filename given as an argument is not
21278 found in our current list, add it to the list and assign it the next
21279 available unique index number. In order to speed up searches, we remember
21280 the index of the filename was looked up last. This handles the majority of
21281 all searches. */
21282
21283 static struct dwarf_file_data *
21284 lookup_filename (const char *file_name)
21285 {
21286 struct dwarf_file_data * created;
21287
21288 /* Check to see if the file name that was searched on the previous
21289 call matches this file name. If so, return the index. */
21290 if (file_table_last_lookup
21291 && (file_name == file_table_last_lookup->filename
21292 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21293 return file_table_last_lookup;
21294
21295 /* Didn't match the previous lookup, search the table. */
21296 dwarf_file_data **slot
21297 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21298 INSERT);
21299 if (*slot)
21300 return *slot;
21301
21302 created = ggc_alloc<dwarf_file_data> ();
21303 created->filename = file_name;
21304 created->emitted_number = 0;
21305 *slot = created;
21306 return created;
21307 }
21308
21309 /* If the assembler will construct the file table, then translate the compiler
21310 internal file table number into the assembler file table number, and emit
21311 a .file directive if we haven't already emitted one yet. The file table
21312 numbers are different because we prune debug info for unused variables and
21313 types, which may include filenames. */
21314
21315 static int
21316 maybe_emit_file (struct dwarf_file_data * fd)
21317 {
21318 if (! fd->emitted_number)
21319 {
21320 if (last_emitted_file)
21321 fd->emitted_number = last_emitted_file->emitted_number + 1;
21322 else
21323 fd->emitted_number = 1;
21324 last_emitted_file = fd;
21325
21326 if (DWARF2_ASM_LINE_DEBUG_INFO)
21327 {
21328 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21329 output_quoted_string (asm_out_file,
21330 remap_debug_filename (fd->filename));
21331 fputc ('\n', asm_out_file);
21332 }
21333 }
21334
21335 return fd->emitted_number;
21336 }
21337
21338 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21339 That generation should happen after function debug info has been
21340 generated. The value of the attribute is the constant value of ARG. */
21341
21342 static void
21343 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21344 {
21345 die_arg_entry entry;
21346
21347 if (!die || !arg)
21348 return;
21349
21350 if (!tmpl_value_parm_die_table)
21351 vec_alloc (tmpl_value_parm_die_table, 32);
21352
21353 entry.die = die;
21354 entry.arg = arg;
21355 vec_safe_push (tmpl_value_parm_die_table, entry);
21356 }
21357
21358 /* Return TRUE if T is an instance of generic type, FALSE
21359 otherwise. */
21360
21361 static bool
21362 generic_type_p (tree t)
21363 {
21364 if (t == NULL_TREE || !TYPE_P (t))
21365 return false;
21366 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21367 }
21368
21369 /* Schedule the generation of the generic parameter dies for the
21370 instance of generic type T. The proper generation itself is later
21371 done by gen_scheduled_generic_parms_dies. */
21372
21373 static void
21374 schedule_generic_params_dies_gen (tree t)
21375 {
21376 if (!generic_type_p (t))
21377 return;
21378
21379 if (!generic_type_instances)
21380 vec_alloc (generic_type_instances, 256);
21381
21382 vec_safe_push (generic_type_instances, t);
21383 }
21384
21385 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21386 by append_entry_to_tmpl_value_parm_die_table. This function must
21387 be called after function DIEs have been generated. */
21388
21389 static void
21390 gen_remaining_tmpl_value_param_die_attribute (void)
21391 {
21392 if (tmpl_value_parm_die_table)
21393 {
21394 unsigned i;
21395 die_arg_entry *e;
21396
21397 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21398 tree_add_const_value_attribute (e->die, e->arg);
21399 }
21400 }
21401
21402 /* Generate generic parameters DIEs for instances of generic types
21403 that have been previously scheduled by
21404 schedule_generic_params_dies_gen. This function must be called
21405 after all the types of the CU have been laid out. */
21406
21407 static void
21408 gen_scheduled_generic_parms_dies (void)
21409 {
21410 unsigned i;
21411 tree t;
21412
21413 if (!generic_type_instances)
21414 return;
21415
21416 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21417 if (COMPLETE_TYPE_P (t))
21418 gen_generic_params_dies (t);
21419 }
21420
21421
21422 /* Replace DW_AT_name for the decl with name. */
21423
21424 static void
21425 dwarf2out_set_name (tree decl, tree name)
21426 {
21427 dw_die_ref die;
21428 dw_attr_ref attr;
21429 const char *dname;
21430
21431 die = TYPE_SYMTAB_DIE (decl);
21432 if (!die)
21433 return;
21434
21435 dname = dwarf2_name (name, 0);
21436 if (!dname)
21437 return;
21438
21439 attr = get_AT (die, DW_AT_name);
21440 if (attr)
21441 {
21442 struct indirect_string_node *node;
21443
21444 node = find_AT_string (dname);
21445 /* replace the string. */
21446 attr->dw_attr_val.v.val_str = node;
21447 }
21448
21449 else
21450 add_name_attribute (die, dname);
21451 }
21452
21453 /* True if before or during processing of the first function being emitted. */
21454 static bool in_first_function_p = true;
21455 /* True if loc_note during dwarf2out_var_location call might still be
21456 before first real instruction at address equal to .Ltext0. */
21457 static bool maybe_at_text_label_p = true;
21458 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21459 static unsigned int first_loclabel_num_not_at_text_label;
21460
21461 /* Called by the final INSN scan whenever we see a var location. We
21462 use it to drop labels in the right places, and throw the location in
21463 our lookup table. */
21464
21465 static void
21466 dwarf2out_var_location (rtx_insn *loc_note)
21467 {
21468 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21469 struct var_loc_node *newloc;
21470 rtx_insn *next_real, *next_note;
21471 static const char *last_label;
21472 static const char *last_postcall_label;
21473 static bool last_in_cold_section_p;
21474 static rtx_insn *expected_next_loc_note;
21475 tree decl;
21476 bool var_loc_p;
21477
21478 if (!NOTE_P (loc_note))
21479 {
21480 if (CALL_P (loc_note))
21481 {
21482 call_site_count++;
21483 if (SIBLING_CALL_P (loc_note))
21484 tail_call_site_count++;
21485 }
21486 return;
21487 }
21488
21489 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21490 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21491 return;
21492
21493 /* Optimize processing a large consecutive sequence of location
21494 notes so we don't spend too much time in next_real_insn. If the
21495 next insn is another location note, remember the next_real_insn
21496 calculation for next time. */
21497 next_real = cached_next_real_insn;
21498 if (next_real)
21499 {
21500 if (expected_next_loc_note != loc_note)
21501 next_real = NULL;
21502 }
21503
21504 next_note = NEXT_INSN (loc_note);
21505 if (! next_note
21506 || next_note->deleted ()
21507 || ! NOTE_P (next_note)
21508 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21509 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21510 next_note = NULL;
21511
21512 if (! next_real)
21513 next_real = next_real_insn (loc_note);
21514
21515 if (next_note)
21516 {
21517 expected_next_loc_note = next_note;
21518 cached_next_real_insn = next_real;
21519 }
21520 else
21521 cached_next_real_insn = NULL;
21522
21523 /* If there are no instructions which would be affected by this note,
21524 don't do anything. */
21525 if (var_loc_p
21526 && next_real == NULL_RTX
21527 && !NOTE_DURING_CALL_P (loc_note))
21528 return;
21529
21530 if (next_real == NULL_RTX)
21531 next_real = get_last_insn ();
21532
21533 /* If there were any real insns between note we processed last time
21534 and this note (or if it is the first note), clear
21535 last_{,postcall_}label so that they are not reused this time. */
21536 if (last_var_location_insn == NULL_RTX
21537 || last_var_location_insn != next_real
21538 || last_in_cold_section_p != in_cold_section_p)
21539 {
21540 last_label = NULL;
21541 last_postcall_label = NULL;
21542 }
21543
21544 if (var_loc_p)
21545 {
21546 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21547 newloc = add_var_loc_to_decl (decl, loc_note,
21548 NOTE_DURING_CALL_P (loc_note)
21549 ? last_postcall_label : last_label);
21550 if (newloc == NULL)
21551 return;
21552 }
21553 else
21554 {
21555 decl = NULL_TREE;
21556 newloc = NULL;
21557 }
21558
21559 /* If there were no real insns between note we processed last time
21560 and this note, use the label we emitted last time. Otherwise
21561 create a new label and emit it. */
21562 if (last_label == NULL)
21563 {
21564 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21565 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21566 loclabel_num++;
21567 last_label = ggc_strdup (loclabel);
21568 /* See if loclabel might be equal to .Ltext0. If yes,
21569 bump first_loclabel_num_not_at_text_label. */
21570 if (!have_multiple_function_sections
21571 && in_first_function_p
21572 && maybe_at_text_label_p)
21573 {
21574 static rtx_insn *last_start;
21575 rtx_insn *insn;
21576 for (insn = loc_note; insn; insn = previous_insn (insn))
21577 if (insn == last_start)
21578 break;
21579 else if (!NONDEBUG_INSN_P (insn))
21580 continue;
21581 else
21582 {
21583 rtx body = PATTERN (insn);
21584 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21585 continue;
21586 /* Inline asm could occupy zero bytes. */
21587 else if (GET_CODE (body) == ASM_INPUT
21588 || asm_noperands (body) >= 0)
21589 continue;
21590 #ifdef HAVE_attr_length
21591 else if (get_attr_min_length (insn) == 0)
21592 continue;
21593 #endif
21594 else
21595 {
21596 /* Assume insn has non-zero length. */
21597 maybe_at_text_label_p = false;
21598 break;
21599 }
21600 }
21601 if (maybe_at_text_label_p)
21602 {
21603 last_start = loc_note;
21604 first_loclabel_num_not_at_text_label = loclabel_num;
21605 }
21606 }
21607 }
21608
21609 if (!var_loc_p)
21610 {
21611 struct call_arg_loc_node *ca_loc
21612 = ggc_cleared_alloc<call_arg_loc_node> ();
21613 rtx_insn *prev = prev_real_insn (loc_note);
21614 rtx x;
21615 ca_loc->call_arg_loc_note = loc_note;
21616 ca_loc->next = NULL;
21617 ca_loc->label = last_label;
21618 gcc_assert (prev
21619 && (CALL_P (prev)
21620 || (NONJUMP_INSN_P (prev)
21621 && GET_CODE (PATTERN (prev)) == SEQUENCE
21622 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21623 if (!CALL_P (prev))
21624 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21625 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21626 x = get_call_rtx_from (PATTERN (prev));
21627 if (x)
21628 {
21629 x = XEXP (XEXP (x, 0), 0);
21630 if (GET_CODE (x) == SYMBOL_REF
21631 && SYMBOL_REF_DECL (x)
21632 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21633 ca_loc->symbol_ref = x;
21634 }
21635 ca_loc->block = insn_scope (prev);
21636 if (call_arg_locations)
21637 call_arg_loc_last->next = ca_loc;
21638 else
21639 call_arg_locations = ca_loc;
21640 call_arg_loc_last = ca_loc;
21641 }
21642 else if (!NOTE_DURING_CALL_P (loc_note))
21643 newloc->label = last_label;
21644 else
21645 {
21646 if (!last_postcall_label)
21647 {
21648 sprintf (loclabel, "%s-1", last_label);
21649 last_postcall_label = ggc_strdup (loclabel);
21650 }
21651 newloc->label = last_postcall_label;
21652 }
21653
21654 last_var_location_insn = next_real;
21655 last_in_cold_section_p = in_cold_section_p;
21656 }
21657
21658 /* Note in one location list that text section has changed. */
21659
21660 int
21661 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21662 {
21663 var_loc_list *list = *slot;
21664 if (list->first)
21665 list->last_before_switch
21666 = list->last->next ? list->last->next : list->last;
21667 return 1;
21668 }
21669
21670 /* Note in all location lists that text section has changed. */
21671
21672 static void
21673 var_location_switch_text_section (void)
21674 {
21675 if (decl_loc_table == NULL)
21676 return;
21677
21678 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21679 }
21680
21681 /* Create a new line number table. */
21682
21683 static dw_line_info_table *
21684 new_line_info_table (void)
21685 {
21686 dw_line_info_table *table;
21687
21688 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21689 table->file_num = 1;
21690 table->line_num = 1;
21691 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21692
21693 return table;
21694 }
21695
21696 /* Lookup the "current" table into which we emit line info, so
21697 that we don't have to do it for every source line. */
21698
21699 static void
21700 set_cur_line_info_table (section *sec)
21701 {
21702 dw_line_info_table *table;
21703
21704 if (sec == text_section)
21705 table = text_section_line_info;
21706 else if (sec == cold_text_section)
21707 {
21708 table = cold_text_section_line_info;
21709 if (!table)
21710 {
21711 cold_text_section_line_info = table = new_line_info_table ();
21712 table->end_label = cold_end_label;
21713 }
21714 }
21715 else
21716 {
21717 const char *end_label;
21718
21719 if (flag_reorder_blocks_and_partition)
21720 {
21721 if (in_cold_section_p)
21722 end_label = crtl->subsections.cold_section_end_label;
21723 else
21724 end_label = crtl->subsections.hot_section_end_label;
21725 }
21726 else
21727 {
21728 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21729 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21730 current_function_funcdef_no);
21731 end_label = ggc_strdup (label);
21732 }
21733
21734 table = new_line_info_table ();
21735 table->end_label = end_label;
21736
21737 vec_safe_push (separate_line_info, table);
21738 }
21739
21740 if (DWARF2_ASM_LINE_DEBUG_INFO)
21741 table->is_stmt = (cur_line_info_table
21742 ? cur_line_info_table->is_stmt
21743 : DWARF_LINE_DEFAULT_IS_STMT_START);
21744 cur_line_info_table = table;
21745 }
21746
21747
21748 /* We need to reset the locations at the beginning of each
21749 function. We can't do this in the end_function hook, because the
21750 declarations that use the locations won't have been output when
21751 that hook is called. Also compute have_multiple_function_sections here. */
21752
21753 static void
21754 dwarf2out_begin_function (tree fun)
21755 {
21756 section *sec = function_section (fun);
21757
21758 if (sec != text_section)
21759 have_multiple_function_sections = true;
21760
21761 if (flag_reorder_blocks_and_partition && !cold_text_section)
21762 {
21763 gcc_assert (current_function_decl == fun);
21764 cold_text_section = unlikely_text_section ();
21765 switch_to_section (cold_text_section);
21766 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21767 switch_to_section (sec);
21768 }
21769
21770 dwarf2out_note_section_used ();
21771 call_site_count = 0;
21772 tail_call_site_count = 0;
21773
21774 set_cur_line_info_table (sec);
21775 }
21776
21777 /* Helper function of dwarf2out_end_function, called only after emitting
21778 the very first function into assembly. Check if some .debug_loc range
21779 might end with a .LVL* label that could be equal to .Ltext0.
21780 In that case we must force using absolute addresses in .debug_loc ranges,
21781 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21782 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21783 list terminator.
21784 Set have_multiple_function_sections to true in that case and
21785 terminate htab traversal. */
21786
21787 int
21788 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21789 {
21790 var_loc_list *entry = *slot;
21791 struct var_loc_node *node;
21792
21793 node = entry->first;
21794 if (node && node->next && node->next->label)
21795 {
21796 unsigned int i;
21797 const char *label = node->next->label;
21798 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21799
21800 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21801 {
21802 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21803 if (strcmp (label, loclabel) == 0)
21804 {
21805 have_multiple_function_sections = true;
21806 return 0;
21807 }
21808 }
21809 }
21810 return 1;
21811 }
21812
21813 /* Hook called after emitting a function into assembly.
21814 This does something only for the very first function emitted. */
21815
21816 static void
21817 dwarf2out_end_function (unsigned int)
21818 {
21819 if (in_first_function_p
21820 && !have_multiple_function_sections
21821 && first_loclabel_num_not_at_text_label
21822 && decl_loc_table)
21823 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21824 in_first_function_p = false;
21825 maybe_at_text_label_p = false;
21826 }
21827
21828 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21829
21830 static void
21831 push_dw_line_info_entry (dw_line_info_table *table,
21832 enum dw_line_info_opcode opcode, unsigned int val)
21833 {
21834 dw_line_info_entry e;
21835 e.opcode = opcode;
21836 e.val = val;
21837 vec_safe_push (table->entries, e);
21838 }
21839
21840 /* Output a label to mark the beginning of a source code line entry
21841 and record information relating to this source line, in
21842 'line_info_table' for later output of the .debug_line section. */
21843 /* ??? The discriminator parameter ought to be unsigned. */
21844
21845 static void
21846 dwarf2out_source_line (unsigned int line, const char *filename,
21847 int discriminator, bool is_stmt)
21848 {
21849 unsigned int file_num;
21850 dw_line_info_table *table;
21851
21852 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21853 return;
21854
21855 /* The discriminator column was added in dwarf4. Simplify the below
21856 by simply removing it if we're not supposed to output it. */
21857 if (dwarf_version < 4 && dwarf_strict)
21858 discriminator = 0;
21859
21860 table = cur_line_info_table;
21861 file_num = maybe_emit_file (lookup_filename (filename));
21862
21863 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21864 the debugger has used the second (possibly duplicate) line number
21865 at the beginning of the function to mark the end of the prologue.
21866 We could eliminate any other duplicates within the function. For
21867 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21868 that second line number entry. */
21869 /* Recall that this end-of-prologue indication is *not* the same thing
21870 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21871 to which the hook corresponds, follows the last insn that was
21872 emitted by gen_prologue. What we need is to precede the first insn
21873 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21874 insn that corresponds to something the user wrote. These may be
21875 very different locations once scheduling is enabled. */
21876
21877 if (0 && file_num == table->file_num
21878 && line == table->line_num
21879 && discriminator == table->discrim_num
21880 && is_stmt == table->is_stmt)
21881 return;
21882
21883 switch_to_section (current_function_section ());
21884
21885 /* If requested, emit something human-readable. */
21886 if (flag_debug_asm)
21887 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21888
21889 if (DWARF2_ASM_LINE_DEBUG_INFO)
21890 {
21891 /* Emit the .loc directive understood by GNU as. */
21892 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21893 file_num, line, is_stmt, discriminator */
21894 fputs ("\t.loc ", asm_out_file);
21895 fprint_ul (asm_out_file, file_num);
21896 putc (' ', asm_out_file);
21897 fprint_ul (asm_out_file, line);
21898 putc (' ', asm_out_file);
21899 putc ('0', asm_out_file);
21900
21901 if (is_stmt != table->is_stmt)
21902 {
21903 fputs (" is_stmt ", asm_out_file);
21904 putc (is_stmt ? '1' : '0', asm_out_file);
21905 }
21906 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21907 {
21908 gcc_assert (discriminator > 0);
21909 fputs (" discriminator ", asm_out_file);
21910 fprint_ul (asm_out_file, (unsigned long) discriminator);
21911 }
21912 putc ('\n', asm_out_file);
21913 }
21914 else
21915 {
21916 unsigned int label_num = ++line_info_label_num;
21917
21918 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21919
21920 push_dw_line_info_entry (table, LI_set_address, label_num);
21921 if (file_num != table->file_num)
21922 push_dw_line_info_entry (table, LI_set_file, file_num);
21923 if (discriminator != table->discrim_num)
21924 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21925 if (is_stmt != table->is_stmt)
21926 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21927 push_dw_line_info_entry (table, LI_set_line, line);
21928 }
21929
21930 table->file_num = file_num;
21931 table->line_num = line;
21932 table->discrim_num = discriminator;
21933 table->is_stmt = is_stmt;
21934 table->in_use = true;
21935 }
21936
21937 /* Record the beginning of a new source file. */
21938
21939 static void
21940 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21941 {
21942 if (flag_eliminate_dwarf2_dups)
21943 {
21944 /* Record the beginning of the file for break_out_includes. */
21945 dw_die_ref bincl_die;
21946
21947 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21948 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21949 }
21950
21951 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21952 {
21953 macinfo_entry e;
21954 e.code = DW_MACINFO_start_file;
21955 e.lineno = lineno;
21956 e.info = ggc_strdup (filename);
21957 vec_safe_push (macinfo_table, e);
21958 }
21959 }
21960
21961 /* Record the end of a source file. */
21962
21963 static void
21964 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21965 {
21966 if (flag_eliminate_dwarf2_dups)
21967 /* Record the end of the file for break_out_includes. */
21968 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21969
21970 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21971 {
21972 macinfo_entry e;
21973 e.code = DW_MACINFO_end_file;
21974 e.lineno = lineno;
21975 e.info = NULL;
21976 vec_safe_push (macinfo_table, e);
21977 }
21978 }
21979
21980 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21981 the tail part of the directive line, i.e. the part which is past the
21982 initial whitespace, #, whitespace, directive-name, whitespace part. */
21983
21984 static void
21985 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21986 const char *buffer ATTRIBUTE_UNUSED)
21987 {
21988 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21989 {
21990 macinfo_entry e;
21991 /* Insert a dummy first entry to be able to optimize the whole
21992 predefined macro block using DW_MACRO_GNU_transparent_include. */
21993 if (macinfo_table->is_empty () && lineno <= 1)
21994 {
21995 e.code = 0;
21996 e.lineno = 0;
21997 e.info = NULL;
21998 vec_safe_push (macinfo_table, e);
21999 }
22000 e.code = DW_MACINFO_define;
22001 e.lineno = lineno;
22002 e.info = ggc_strdup (buffer);
22003 vec_safe_push (macinfo_table, e);
22004 }
22005 }
22006
22007 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22008 the tail part of the directive line, i.e. the part which is past the
22009 initial whitespace, #, whitespace, directive-name, whitespace part. */
22010
22011 static void
22012 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22013 const char *buffer ATTRIBUTE_UNUSED)
22014 {
22015 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22016 {
22017 macinfo_entry e;
22018 /* Insert a dummy first entry to be able to optimize the whole
22019 predefined macro block using DW_MACRO_GNU_transparent_include. */
22020 if (macinfo_table->is_empty () && lineno <= 1)
22021 {
22022 e.code = 0;
22023 e.lineno = 0;
22024 e.info = NULL;
22025 vec_safe_push (macinfo_table, e);
22026 }
22027 e.code = DW_MACINFO_undef;
22028 e.lineno = lineno;
22029 e.info = ggc_strdup (buffer);
22030 vec_safe_push (macinfo_table, e);
22031 }
22032 }
22033
22034 /* Helpers to manipulate hash table of CUs. */
22035
22036 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22037 {
22038 typedef macinfo_entry value_type;
22039 typedef macinfo_entry compare_type;
22040 static inline hashval_t hash (const value_type *);
22041 static inline bool equal (const value_type *, const compare_type *);
22042 };
22043
22044 inline hashval_t
22045 macinfo_entry_hasher::hash (const value_type *entry)
22046 {
22047 return htab_hash_string (entry->info);
22048 }
22049
22050 inline bool
22051 macinfo_entry_hasher::equal (const value_type *entry1,
22052 const compare_type *entry2)
22053 {
22054 return !strcmp (entry1->info, entry2->info);
22055 }
22056
22057 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22058
22059 /* Output a single .debug_macinfo entry. */
22060
22061 static void
22062 output_macinfo_op (macinfo_entry *ref)
22063 {
22064 int file_num;
22065 size_t len;
22066 struct indirect_string_node *node;
22067 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22068 struct dwarf_file_data *fd;
22069
22070 switch (ref->code)
22071 {
22072 case DW_MACINFO_start_file:
22073 fd = lookup_filename (ref->info);
22074 file_num = maybe_emit_file (fd);
22075 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22076 dw2_asm_output_data_uleb128 (ref->lineno,
22077 "Included from line number %lu",
22078 (unsigned long) ref->lineno);
22079 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22080 break;
22081 case DW_MACINFO_end_file:
22082 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22083 break;
22084 case DW_MACINFO_define:
22085 case DW_MACINFO_undef:
22086 len = strlen (ref->info) + 1;
22087 if (!dwarf_strict
22088 && len > DWARF_OFFSET_SIZE
22089 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22090 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22091 {
22092 ref->code = ref->code == DW_MACINFO_define
22093 ? DW_MACRO_GNU_define_indirect
22094 : DW_MACRO_GNU_undef_indirect;
22095 output_macinfo_op (ref);
22096 return;
22097 }
22098 dw2_asm_output_data (1, ref->code,
22099 ref->code == DW_MACINFO_define
22100 ? "Define macro" : "Undefine macro");
22101 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22102 (unsigned long) ref->lineno);
22103 dw2_asm_output_nstring (ref->info, -1, "The macro");
22104 break;
22105 case DW_MACRO_GNU_define_indirect:
22106 case DW_MACRO_GNU_undef_indirect:
22107 node = find_AT_string (ref->info);
22108 gcc_assert (node
22109 && ((node->form == DW_FORM_strp)
22110 || (node->form == DW_FORM_GNU_str_index)));
22111 dw2_asm_output_data (1, ref->code,
22112 ref->code == DW_MACRO_GNU_define_indirect
22113 ? "Define macro indirect"
22114 : "Undefine macro indirect");
22115 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22116 (unsigned long) ref->lineno);
22117 if (node->form == DW_FORM_strp)
22118 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22119 debug_str_section, "The macro: \"%s\"",
22120 ref->info);
22121 else
22122 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22123 ref->info);
22124 break;
22125 case DW_MACRO_GNU_transparent_include:
22126 dw2_asm_output_data (1, ref->code, "Transparent include");
22127 ASM_GENERATE_INTERNAL_LABEL (label,
22128 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22129 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22130 break;
22131 default:
22132 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22133 ASM_COMMENT_START, (unsigned long) ref->code);
22134 break;
22135 }
22136 }
22137
22138 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22139 other compilation unit .debug_macinfo sections. IDX is the first
22140 index of a define/undef, return the number of ops that should be
22141 emitted in a comdat .debug_macinfo section and emit
22142 a DW_MACRO_GNU_transparent_include entry referencing it.
22143 If the define/undef entry should be emitted normally, return 0. */
22144
22145 static unsigned
22146 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22147 macinfo_hash_type **macinfo_htab)
22148 {
22149 macinfo_entry *first, *second, *cur, *inc;
22150 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22151 unsigned char checksum[16];
22152 struct md5_ctx ctx;
22153 char *grp_name, *tail;
22154 const char *base;
22155 unsigned int i, count, encoded_filename_len, linebuf_len;
22156 macinfo_entry **slot;
22157
22158 first = &(*macinfo_table)[idx];
22159 second = &(*macinfo_table)[idx + 1];
22160
22161 /* Optimize only if there are at least two consecutive define/undef ops,
22162 and either all of them are before first DW_MACINFO_start_file
22163 with lineno {0,1} (i.e. predefined macro block), or all of them are
22164 in some included header file. */
22165 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22166 return 0;
22167 if (vec_safe_is_empty (files))
22168 {
22169 if (first->lineno > 1 || second->lineno > 1)
22170 return 0;
22171 }
22172 else if (first->lineno == 0)
22173 return 0;
22174
22175 /* Find the last define/undef entry that can be grouped together
22176 with first and at the same time compute md5 checksum of their
22177 codes, linenumbers and strings. */
22178 md5_init_ctx (&ctx);
22179 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22180 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22181 break;
22182 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22183 break;
22184 else
22185 {
22186 unsigned char code = cur->code;
22187 md5_process_bytes (&code, 1, &ctx);
22188 checksum_uleb128 (cur->lineno, &ctx);
22189 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22190 }
22191 md5_finish_ctx (&ctx, checksum);
22192 count = i - idx;
22193
22194 /* From the containing include filename (if any) pick up just
22195 usable characters from its basename. */
22196 if (vec_safe_is_empty (files))
22197 base = "";
22198 else
22199 base = lbasename (files->last ().info);
22200 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22201 if (ISIDNUM (base[i]) || base[i] == '.')
22202 encoded_filename_len++;
22203 /* Count . at the end. */
22204 if (encoded_filename_len)
22205 encoded_filename_len++;
22206
22207 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22208 linebuf_len = strlen (linebuf);
22209
22210 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22211 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22212 + 16 * 2 + 1);
22213 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22214 tail = grp_name + 4;
22215 if (encoded_filename_len)
22216 {
22217 for (i = 0; base[i]; i++)
22218 if (ISIDNUM (base[i]) || base[i] == '.')
22219 *tail++ = base[i];
22220 *tail++ = '.';
22221 }
22222 memcpy (tail, linebuf, linebuf_len);
22223 tail += linebuf_len;
22224 *tail++ = '.';
22225 for (i = 0; i < 16; i++)
22226 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22227
22228 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22229 in the empty vector entry before the first define/undef. */
22230 inc = &(*macinfo_table)[idx - 1];
22231 inc->code = DW_MACRO_GNU_transparent_include;
22232 inc->lineno = 0;
22233 inc->info = ggc_strdup (grp_name);
22234 if (!*macinfo_htab)
22235 *macinfo_htab = new macinfo_hash_type (10);
22236 /* Avoid emitting duplicates. */
22237 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22238 if (*slot != NULL)
22239 {
22240 inc->code = 0;
22241 inc->info = NULL;
22242 /* If such an entry has been used before, just emit
22243 a DW_MACRO_GNU_transparent_include op. */
22244 inc = *slot;
22245 output_macinfo_op (inc);
22246 /* And clear all macinfo_entry in the range to avoid emitting them
22247 in the second pass. */
22248 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22249 {
22250 cur->code = 0;
22251 cur->info = NULL;
22252 }
22253 }
22254 else
22255 {
22256 *slot = inc;
22257 inc->lineno = (*macinfo_htab)->elements ();
22258 output_macinfo_op (inc);
22259 }
22260 return count;
22261 }
22262
22263 /* Save any strings needed by the macinfo table in the debug str
22264 table. All strings must be collected into the table by the time
22265 index_string is called. */
22266
22267 static void
22268 save_macinfo_strings (void)
22269 {
22270 unsigned len;
22271 unsigned i;
22272 macinfo_entry *ref;
22273
22274 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22275 {
22276 switch (ref->code)
22277 {
22278 /* Match the logic in output_macinfo_op to decide on
22279 indirect strings. */
22280 case DW_MACINFO_define:
22281 case DW_MACINFO_undef:
22282 len = strlen (ref->info) + 1;
22283 if (!dwarf_strict
22284 && len > DWARF_OFFSET_SIZE
22285 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22286 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22287 set_indirect_string (find_AT_string (ref->info));
22288 break;
22289 case DW_MACRO_GNU_define_indirect:
22290 case DW_MACRO_GNU_undef_indirect:
22291 set_indirect_string (find_AT_string (ref->info));
22292 break;
22293 default:
22294 break;
22295 }
22296 }
22297 }
22298
22299 /* Output macinfo section(s). */
22300
22301 static void
22302 output_macinfo (void)
22303 {
22304 unsigned i;
22305 unsigned long length = vec_safe_length (macinfo_table);
22306 macinfo_entry *ref;
22307 vec<macinfo_entry, va_gc> *files = NULL;
22308 macinfo_hash_type *macinfo_htab = NULL;
22309
22310 if (! length)
22311 return;
22312
22313 /* output_macinfo* uses these interchangeably. */
22314 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22315 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22316 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22317 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22318
22319 /* For .debug_macro emit the section header. */
22320 if (!dwarf_strict)
22321 {
22322 dw2_asm_output_data (2, 4, "DWARF macro version number");
22323 if (DWARF_OFFSET_SIZE == 8)
22324 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22325 else
22326 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22327 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22328 (!dwarf_split_debug_info ? debug_line_section_label
22329 : debug_skeleton_line_section_label),
22330 debug_line_section, NULL);
22331 }
22332
22333 /* In the first loop, it emits the primary .debug_macinfo section
22334 and after each emitted op the macinfo_entry is cleared.
22335 If a longer range of define/undef ops can be optimized using
22336 DW_MACRO_GNU_transparent_include, the
22337 DW_MACRO_GNU_transparent_include op is emitted and kept in
22338 the vector before the first define/undef in the range and the
22339 whole range of define/undef ops is not emitted and kept. */
22340 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22341 {
22342 switch (ref->code)
22343 {
22344 case DW_MACINFO_start_file:
22345 vec_safe_push (files, *ref);
22346 break;
22347 case DW_MACINFO_end_file:
22348 if (!vec_safe_is_empty (files))
22349 files->pop ();
22350 break;
22351 case DW_MACINFO_define:
22352 case DW_MACINFO_undef:
22353 if (!dwarf_strict
22354 && HAVE_COMDAT_GROUP
22355 && vec_safe_length (files) != 1
22356 && i > 0
22357 && i + 1 < length
22358 && (*macinfo_table)[i - 1].code == 0)
22359 {
22360 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22361 if (count)
22362 {
22363 i += count - 1;
22364 continue;
22365 }
22366 }
22367 break;
22368 case 0:
22369 /* A dummy entry may be inserted at the beginning to be able
22370 to optimize the whole block of predefined macros. */
22371 if (i == 0)
22372 continue;
22373 default:
22374 break;
22375 }
22376 output_macinfo_op (ref);
22377 ref->info = NULL;
22378 ref->code = 0;
22379 }
22380
22381 if (!macinfo_htab)
22382 return;
22383
22384 delete macinfo_htab;
22385 macinfo_htab = NULL;
22386
22387 /* If any DW_MACRO_GNU_transparent_include were used, on those
22388 DW_MACRO_GNU_transparent_include entries terminate the
22389 current chain and switch to a new comdat .debug_macinfo
22390 section and emit the define/undef entries within it. */
22391 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22392 switch (ref->code)
22393 {
22394 case 0:
22395 continue;
22396 case DW_MACRO_GNU_transparent_include:
22397 {
22398 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22399 tree comdat_key = get_identifier (ref->info);
22400 /* Terminate the previous .debug_macinfo section. */
22401 dw2_asm_output_data (1, 0, "End compilation unit");
22402 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22403 SECTION_DEBUG
22404 | SECTION_LINKONCE,
22405 comdat_key);
22406 ASM_GENERATE_INTERNAL_LABEL (label,
22407 DEBUG_MACRO_SECTION_LABEL,
22408 ref->lineno);
22409 ASM_OUTPUT_LABEL (asm_out_file, label);
22410 ref->code = 0;
22411 ref->info = NULL;
22412 dw2_asm_output_data (2, 4, "DWARF macro version number");
22413 if (DWARF_OFFSET_SIZE == 8)
22414 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22415 else
22416 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22417 }
22418 break;
22419 case DW_MACINFO_define:
22420 case DW_MACINFO_undef:
22421 output_macinfo_op (ref);
22422 ref->code = 0;
22423 ref->info = NULL;
22424 break;
22425 default:
22426 gcc_unreachable ();
22427 }
22428 }
22429
22430 /* Set up for Dwarf output at the start of compilation. */
22431
22432 static void
22433 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22434 {
22435 /* Allocate the file_table. */
22436 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22437
22438 /* Allocate the decl_die_table. */
22439 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22440
22441 /* Allocate the decl_loc_table. */
22442 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22443
22444 /* Allocate the cached_dw_loc_list_table. */
22445 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22446
22447 /* Allocate the initial hunk of the decl_scope_table. */
22448 vec_alloc (decl_scope_table, 256);
22449
22450 /* Allocate the initial hunk of the abbrev_die_table. */
22451 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22452 (ABBREV_DIE_TABLE_INCREMENT);
22453 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22454 /* Zero-th entry is allocated, but unused. */
22455 abbrev_die_table_in_use = 1;
22456
22457 /* Allocate the pubtypes and pubnames vectors. */
22458 vec_alloc (pubname_table, 32);
22459 vec_alloc (pubtype_table, 32);
22460
22461 vec_alloc (incomplete_types, 64);
22462
22463 vec_alloc (used_rtx_array, 32);
22464
22465 if (!dwarf_split_debug_info)
22466 {
22467 debug_info_section = get_section (DEBUG_INFO_SECTION,
22468 SECTION_DEBUG, NULL);
22469 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22470 SECTION_DEBUG, NULL);
22471 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22472 SECTION_DEBUG, NULL);
22473 }
22474 else
22475 {
22476 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22477 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22478 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22479 SECTION_DEBUG | SECTION_EXCLUDE,
22480 NULL);
22481 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22482 SECTION_DEBUG, NULL);
22483 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22484 SECTION_DEBUG, NULL);
22485 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22486 SECTION_DEBUG, NULL);
22487 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22488 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22489
22490 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22491 the main .o, but the skeleton_line goes into the split off dwo. */
22492 debug_skeleton_line_section
22493 = get_section (DEBUG_DWO_LINE_SECTION,
22494 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22495 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22496 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22497 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22498 SECTION_DEBUG | SECTION_EXCLUDE,
22499 NULL);
22500 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22501 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22502 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22503 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22504 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22505 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22506 }
22507 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22508 SECTION_DEBUG, NULL);
22509 debug_macinfo_section = get_section (dwarf_strict
22510 ? DEBUG_MACINFO_SECTION
22511 : DEBUG_MACRO_SECTION,
22512 DEBUG_MACRO_SECTION_FLAGS, NULL);
22513 debug_line_section = get_section (DEBUG_LINE_SECTION,
22514 SECTION_DEBUG, NULL);
22515 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22516 SECTION_DEBUG, NULL);
22517 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22518 SECTION_DEBUG, NULL);
22519 debug_str_section = get_section (DEBUG_STR_SECTION,
22520 DEBUG_STR_SECTION_FLAGS, NULL);
22521 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22522 SECTION_DEBUG, NULL);
22523 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22524 SECTION_DEBUG, NULL);
22525
22526 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22527 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22528 DEBUG_ABBREV_SECTION_LABEL, 0);
22529 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22530 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22531 COLD_TEXT_SECTION_LABEL, 0);
22532 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22533
22534 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22535 DEBUG_INFO_SECTION_LABEL, 0);
22536 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22537 DEBUG_LINE_SECTION_LABEL, 0);
22538 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22539 DEBUG_RANGES_SECTION_LABEL, 0);
22540 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22541 DEBUG_ADDR_SECTION_LABEL, 0);
22542 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22543 dwarf_strict
22544 ? DEBUG_MACINFO_SECTION_LABEL
22545 : DEBUG_MACRO_SECTION_LABEL, 0);
22546 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22547
22548 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22549 vec_alloc (macinfo_table, 64);
22550
22551 switch_to_section (text_section);
22552 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22553
22554 /* Make sure the line number table for .text always exists. */
22555 text_section_line_info = new_line_info_table ();
22556 text_section_line_info->end_label = text_end_label;
22557 }
22558
22559 /* Called before compile () starts outputtting functions, variables
22560 and toplevel asms into assembly. */
22561
22562 static void
22563 dwarf2out_assembly_start (void)
22564 {
22565 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22566 && dwarf2out_do_cfi_asm ()
22567 && (!(flag_unwind_tables || flag_exceptions)
22568 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22569 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22570 }
22571
22572 /* A helper function for dwarf2out_finish called through
22573 htab_traverse. Assign a string its index. All strings must be
22574 collected into the table by the time index_string is called,
22575 because the indexing code relies on htab_traverse to traverse nodes
22576 in the same order for each run. */
22577
22578 int
22579 index_string (indirect_string_node **h, unsigned int *index)
22580 {
22581 indirect_string_node *node = *h;
22582
22583 find_string_form (node);
22584 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22585 {
22586 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22587 node->index = *index;
22588 *index += 1;
22589 }
22590 return 1;
22591 }
22592
22593 /* A helper function for output_indirect_strings called through
22594 htab_traverse. Output the offset to a string and update the
22595 current offset. */
22596
22597 int
22598 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22599 {
22600 indirect_string_node *node = *h;
22601
22602 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22603 {
22604 /* Assert that this node has been assigned an index. */
22605 gcc_assert (node->index != NO_INDEX_ASSIGNED
22606 && node->index != NOT_INDEXED);
22607 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22608 "indexed string 0x%x: %s", node->index, node->str);
22609 *offset += strlen (node->str) + 1;
22610 }
22611 return 1;
22612 }
22613
22614 /* A helper function for dwarf2out_finish called through
22615 htab_traverse. Output the indexed string. */
22616
22617 int
22618 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22619 {
22620 struct indirect_string_node *node = *h;
22621
22622 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22623 {
22624 /* Assert that the strings are output in the same order as their
22625 indexes were assigned. */
22626 gcc_assert (*cur_idx == node->index);
22627 assemble_string (node->str, strlen (node->str) + 1);
22628 *cur_idx += 1;
22629 }
22630 return 1;
22631 }
22632
22633 /* A helper function for dwarf2out_finish called through
22634 htab_traverse. Emit one queued .debug_str string. */
22635
22636 int
22637 output_indirect_string (indirect_string_node **h, void *)
22638 {
22639 struct indirect_string_node *node = *h;
22640
22641 node->form = find_string_form (node);
22642 if (node->form == DW_FORM_strp && node->refcount > 0)
22643 {
22644 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22645 assemble_string (node->str, strlen (node->str) + 1);
22646 }
22647
22648 return 1;
22649 }
22650
22651 /* Output the indexed string table. */
22652
22653 static void
22654 output_indirect_strings (void)
22655 {
22656 switch_to_section (debug_str_section);
22657 if (!dwarf_split_debug_info)
22658 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22659 else
22660 {
22661 unsigned int offset = 0;
22662 unsigned int cur_idx = 0;
22663
22664 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22665
22666 switch_to_section (debug_str_offsets_section);
22667 debug_str_hash->traverse_noresize
22668 <unsigned int *, output_index_string_offset> (&offset);
22669 switch_to_section (debug_str_dwo_section);
22670 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22671 (&cur_idx);
22672 }
22673 }
22674
22675 /* Callback for htab_traverse to assign an index to an entry in the
22676 table, and to write that entry to the .debug_addr section. */
22677
22678 int
22679 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22680 {
22681 addr_table_entry *entry = *slot;
22682
22683 if (entry->refcount == 0)
22684 {
22685 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22686 || entry->index == NOT_INDEXED);
22687 return 1;
22688 }
22689
22690 gcc_assert (entry->index == *cur_index);
22691 (*cur_index)++;
22692
22693 switch (entry->kind)
22694 {
22695 case ate_kind_rtx:
22696 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22697 "0x%x", entry->index);
22698 break;
22699 case ate_kind_rtx_dtprel:
22700 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22701 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22702 DWARF2_ADDR_SIZE,
22703 entry->addr.rtl);
22704 fputc ('\n', asm_out_file);
22705 break;
22706 case ate_kind_label:
22707 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22708 "0x%x", entry->index);
22709 break;
22710 default:
22711 gcc_unreachable ();
22712 }
22713 return 1;
22714 }
22715
22716 /* Produce the .debug_addr section. */
22717
22718 static void
22719 output_addr_table (void)
22720 {
22721 unsigned int index = 0;
22722 if (addr_index_table == NULL || addr_index_table->size () == 0)
22723 return;
22724
22725 switch_to_section (debug_addr_section);
22726 addr_index_table
22727 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22728 }
22729
22730 #if ENABLE_ASSERT_CHECKING
22731 /* Verify that all marks are clear. */
22732
22733 static void
22734 verify_marks_clear (dw_die_ref die)
22735 {
22736 dw_die_ref c;
22737
22738 gcc_assert (! die->die_mark);
22739 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22740 }
22741 #endif /* ENABLE_ASSERT_CHECKING */
22742
22743 /* Clear the marks for a die and its children.
22744 Be cool if the mark isn't set. */
22745
22746 static void
22747 prune_unmark_dies (dw_die_ref die)
22748 {
22749 dw_die_ref c;
22750
22751 if (die->die_mark)
22752 die->die_mark = 0;
22753 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22754 }
22755
22756 /* Given DIE that we're marking as used, find any other dies
22757 it references as attributes and mark them as used. */
22758
22759 static void
22760 prune_unused_types_walk_attribs (dw_die_ref die)
22761 {
22762 dw_attr_ref a;
22763 unsigned ix;
22764
22765 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22766 {
22767 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22768 {
22769 /* A reference to another DIE.
22770 Make sure that it will get emitted.
22771 If it was broken out into a comdat group, don't follow it. */
22772 if (! AT_ref (a)->comdat_type_p
22773 || a->dw_attr == DW_AT_specification)
22774 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22775 }
22776 /* Set the string's refcount to 0 so that prune_unused_types_mark
22777 accounts properly for it. */
22778 if (AT_class (a) == dw_val_class_str)
22779 a->dw_attr_val.v.val_str->refcount = 0;
22780 }
22781 }
22782
22783 /* Mark the generic parameters and arguments children DIEs of DIE. */
22784
22785 static void
22786 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22787 {
22788 dw_die_ref c;
22789
22790 if (die == NULL || die->die_child == NULL)
22791 return;
22792 c = die->die_child;
22793 do
22794 {
22795 if (is_template_parameter (c))
22796 prune_unused_types_mark (c, 1);
22797 c = c->die_sib;
22798 } while (c && c != die->die_child);
22799 }
22800
22801 /* Mark DIE as being used. If DOKIDS is true, then walk down
22802 to DIE's children. */
22803
22804 static void
22805 prune_unused_types_mark (dw_die_ref die, int dokids)
22806 {
22807 dw_die_ref c;
22808
22809 if (die->die_mark == 0)
22810 {
22811 /* We haven't done this node yet. Mark it as used. */
22812 die->die_mark = 1;
22813 /* If this is the DIE of a generic type instantiation,
22814 mark the children DIEs that describe its generic parms and
22815 args. */
22816 prune_unused_types_mark_generic_parms_dies (die);
22817
22818 /* We also have to mark its parents as used.
22819 (But we don't want to mark our parent's kids due to this,
22820 unless it is a class.) */
22821 if (die->die_parent)
22822 prune_unused_types_mark (die->die_parent,
22823 class_scope_p (die->die_parent));
22824
22825 /* Mark any referenced nodes. */
22826 prune_unused_types_walk_attribs (die);
22827
22828 /* If this node is a specification,
22829 also mark the definition, if it exists. */
22830 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22831 prune_unused_types_mark (die->die_definition, 1);
22832 }
22833
22834 if (dokids && die->die_mark != 2)
22835 {
22836 /* We need to walk the children, but haven't done so yet.
22837 Remember that we've walked the kids. */
22838 die->die_mark = 2;
22839
22840 /* If this is an array type, we need to make sure our
22841 kids get marked, even if they're types. If we're
22842 breaking out types into comdat sections, do this
22843 for all type definitions. */
22844 if (die->die_tag == DW_TAG_array_type
22845 || (use_debug_types
22846 && is_type_die (die) && ! is_declaration_die (die)))
22847 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22848 else
22849 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22850 }
22851 }
22852
22853 /* For local classes, look if any static member functions were emitted
22854 and if so, mark them. */
22855
22856 static void
22857 prune_unused_types_walk_local_classes (dw_die_ref die)
22858 {
22859 dw_die_ref c;
22860
22861 if (die->die_mark == 2)
22862 return;
22863
22864 switch (die->die_tag)
22865 {
22866 case DW_TAG_structure_type:
22867 case DW_TAG_union_type:
22868 case DW_TAG_class_type:
22869 break;
22870
22871 case DW_TAG_subprogram:
22872 if (!get_AT_flag (die, DW_AT_declaration)
22873 || die->die_definition != NULL)
22874 prune_unused_types_mark (die, 1);
22875 return;
22876
22877 default:
22878 return;
22879 }
22880
22881 /* Mark children. */
22882 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22883 }
22884
22885 /* Walk the tree DIE and mark types that we actually use. */
22886
22887 static void
22888 prune_unused_types_walk (dw_die_ref die)
22889 {
22890 dw_die_ref c;
22891
22892 /* Don't do anything if this node is already marked and
22893 children have been marked as well. */
22894 if (die->die_mark == 2)
22895 return;
22896
22897 switch (die->die_tag)
22898 {
22899 case DW_TAG_structure_type:
22900 case DW_TAG_union_type:
22901 case DW_TAG_class_type:
22902 if (die->die_perennial_p)
22903 break;
22904
22905 for (c = die->die_parent; c; c = c->die_parent)
22906 if (c->die_tag == DW_TAG_subprogram)
22907 break;
22908
22909 /* Finding used static member functions inside of classes
22910 is needed just for local classes, because for other classes
22911 static member function DIEs with DW_AT_specification
22912 are emitted outside of the DW_TAG_*_type. If we ever change
22913 it, we'd need to call this even for non-local classes. */
22914 if (c)
22915 prune_unused_types_walk_local_classes (die);
22916
22917 /* It's a type node --- don't mark it. */
22918 return;
22919
22920 case DW_TAG_const_type:
22921 case DW_TAG_packed_type:
22922 case DW_TAG_pointer_type:
22923 case DW_TAG_reference_type:
22924 case DW_TAG_rvalue_reference_type:
22925 case DW_TAG_volatile_type:
22926 case DW_TAG_typedef:
22927 case DW_TAG_array_type:
22928 case DW_TAG_interface_type:
22929 case DW_TAG_friend:
22930 case DW_TAG_variant_part:
22931 case DW_TAG_enumeration_type:
22932 case DW_TAG_subroutine_type:
22933 case DW_TAG_string_type:
22934 case DW_TAG_set_type:
22935 case DW_TAG_subrange_type:
22936 case DW_TAG_ptr_to_member_type:
22937 case DW_TAG_file_type:
22938 if (die->die_perennial_p)
22939 break;
22940
22941 /* It's a type node --- don't mark it. */
22942 return;
22943
22944 default:
22945 /* Mark everything else. */
22946 break;
22947 }
22948
22949 if (die->die_mark == 0)
22950 {
22951 die->die_mark = 1;
22952
22953 /* Now, mark any dies referenced from here. */
22954 prune_unused_types_walk_attribs (die);
22955 }
22956
22957 die->die_mark = 2;
22958
22959 /* Mark children. */
22960 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22961 }
22962
22963 /* Increment the string counts on strings referred to from DIE's
22964 attributes. */
22965
22966 static void
22967 prune_unused_types_update_strings (dw_die_ref die)
22968 {
22969 dw_attr_ref a;
22970 unsigned ix;
22971
22972 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22973 if (AT_class (a) == dw_val_class_str)
22974 {
22975 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22976 s->refcount++;
22977 /* Avoid unnecessarily putting strings that are used less than
22978 twice in the hash table. */
22979 if (s->refcount
22980 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22981 {
22982 indirect_string_node **slot
22983 = debug_str_hash->find_slot_with_hash (s->str,
22984 htab_hash_string (s->str),
22985 INSERT);
22986 gcc_assert (*slot == NULL);
22987 *slot = s;
22988 }
22989 }
22990 }
22991
22992 /* Remove from the tree DIE any dies that aren't marked. */
22993
22994 static void
22995 prune_unused_types_prune (dw_die_ref die)
22996 {
22997 dw_die_ref c;
22998
22999 gcc_assert (die->die_mark);
23000 prune_unused_types_update_strings (die);
23001
23002 if (! die->die_child)
23003 return;
23004
23005 c = die->die_child;
23006 do {
23007 dw_die_ref prev = c;
23008 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23009 if (c == die->die_child)
23010 {
23011 /* No marked children between 'prev' and the end of the list. */
23012 if (prev == c)
23013 /* No marked children at all. */
23014 die->die_child = NULL;
23015 else
23016 {
23017 prev->die_sib = c->die_sib;
23018 die->die_child = prev;
23019 }
23020 return;
23021 }
23022
23023 if (c != prev->die_sib)
23024 prev->die_sib = c;
23025 prune_unused_types_prune (c);
23026 } while (c != die->die_child);
23027 }
23028
23029 /* Remove dies representing declarations that we never use. */
23030
23031 static void
23032 prune_unused_types (void)
23033 {
23034 unsigned int i;
23035 limbo_die_node *node;
23036 comdat_type_node *ctnode;
23037 pubname_ref pub;
23038 dw_die_ref base_type;
23039
23040 #if ENABLE_ASSERT_CHECKING
23041 /* All the marks should already be clear. */
23042 verify_marks_clear (comp_unit_die ());
23043 for (node = limbo_die_list; node; node = node->next)
23044 verify_marks_clear (node->die);
23045 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23046 verify_marks_clear (ctnode->root_die);
23047 #endif /* ENABLE_ASSERT_CHECKING */
23048
23049 /* Mark types that are used in global variables. */
23050 premark_types_used_by_global_vars ();
23051
23052 /* Set the mark on nodes that are actually used. */
23053 prune_unused_types_walk (comp_unit_die ());
23054 for (node = limbo_die_list; node; node = node->next)
23055 prune_unused_types_walk (node->die);
23056 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23057 {
23058 prune_unused_types_walk (ctnode->root_die);
23059 prune_unused_types_mark (ctnode->type_die, 1);
23060 }
23061
23062 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23063 are unusual in that they are pubnames that are the children of pubtypes.
23064 They should only be marked via their parent DW_TAG_enumeration_type die,
23065 not as roots in themselves. */
23066 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23067 if (pub->die->die_tag != DW_TAG_enumerator)
23068 prune_unused_types_mark (pub->die, 1);
23069 for (i = 0; base_types.iterate (i, &base_type); i++)
23070 prune_unused_types_mark (base_type, 1);
23071
23072 if (debug_str_hash)
23073 debug_str_hash->empty ();
23074 if (skeleton_debug_str_hash)
23075 skeleton_debug_str_hash->empty ();
23076 prune_unused_types_prune (comp_unit_die ());
23077 for (node = limbo_die_list; node; node = node->next)
23078 prune_unused_types_prune (node->die);
23079 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23080 prune_unused_types_prune (ctnode->root_die);
23081
23082 /* Leave the marks clear. */
23083 prune_unmark_dies (comp_unit_die ());
23084 for (node = limbo_die_list; node; node = node->next)
23085 prune_unmark_dies (node->die);
23086 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23087 prune_unmark_dies (ctnode->root_die);
23088 }
23089
23090 /* Set the parameter to true if there are any relative pathnames in
23091 the file table. */
23092 int
23093 file_table_relative_p (dwarf_file_data **slot, bool *p)
23094 {
23095 struct dwarf_file_data *d = *slot;
23096 if (!IS_ABSOLUTE_PATH (d->filename))
23097 {
23098 *p = true;
23099 return 0;
23100 }
23101 return 1;
23102 }
23103
23104 /* Helpers to manipulate hash table of comdat type units. */
23105
23106 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23107 {
23108 typedef comdat_type_node value_type;
23109 typedef comdat_type_node compare_type;
23110 static inline hashval_t hash (const value_type *);
23111 static inline bool equal (const value_type *, const compare_type *);
23112 };
23113
23114 inline hashval_t
23115 comdat_type_hasher::hash (const value_type *type_node)
23116 {
23117 hashval_t h;
23118 memcpy (&h, type_node->signature, sizeof (h));
23119 return h;
23120 }
23121
23122 inline bool
23123 comdat_type_hasher::equal (const value_type *type_node_1,
23124 const compare_type *type_node_2)
23125 {
23126 return (! memcmp (type_node_1->signature, type_node_2->signature,
23127 DWARF_TYPE_SIGNATURE_SIZE));
23128 }
23129
23130 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23131 to the location it would have been added, should we know its
23132 DECL_ASSEMBLER_NAME when we added other attributes. This will
23133 probably improve compactness of debug info, removing equivalent
23134 abbrevs, and hide any differences caused by deferring the
23135 computation of the assembler name, triggered by e.g. PCH. */
23136
23137 static inline void
23138 move_linkage_attr (dw_die_ref die)
23139 {
23140 unsigned ix = vec_safe_length (die->die_attr);
23141 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23142
23143 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23144 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23145
23146 while (--ix > 0)
23147 {
23148 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23149
23150 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23151 break;
23152 }
23153
23154 if (ix != vec_safe_length (die->die_attr) - 1)
23155 {
23156 die->die_attr->pop ();
23157 die->die_attr->quick_insert (ix, linkage);
23158 }
23159 }
23160
23161 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23162 referenced from typed stack ops and count how often they are used. */
23163
23164 static void
23165 mark_base_types (dw_loc_descr_ref loc)
23166 {
23167 dw_die_ref base_type = NULL;
23168
23169 for (; loc; loc = loc->dw_loc_next)
23170 {
23171 switch (loc->dw_loc_opc)
23172 {
23173 case DW_OP_GNU_regval_type:
23174 case DW_OP_GNU_deref_type:
23175 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23176 break;
23177 case DW_OP_GNU_convert:
23178 case DW_OP_GNU_reinterpret:
23179 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23180 continue;
23181 /* FALLTHRU */
23182 case DW_OP_GNU_const_type:
23183 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23184 break;
23185 case DW_OP_GNU_entry_value:
23186 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23187 continue;
23188 default:
23189 continue;
23190 }
23191 gcc_assert (base_type->die_parent == comp_unit_die ());
23192 if (base_type->die_mark)
23193 base_type->die_mark++;
23194 else
23195 {
23196 base_types.safe_push (base_type);
23197 base_type->die_mark = 1;
23198 }
23199 }
23200 }
23201
23202 /* Comparison function for sorting marked base types. */
23203
23204 static int
23205 base_type_cmp (const void *x, const void *y)
23206 {
23207 dw_die_ref dx = *(const dw_die_ref *) x;
23208 dw_die_ref dy = *(const dw_die_ref *) y;
23209 unsigned int byte_size1, byte_size2;
23210 unsigned int encoding1, encoding2;
23211 if (dx->die_mark > dy->die_mark)
23212 return -1;
23213 if (dx->die_mark < dy->die_mark)
23214 return 1;
23215 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23216 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23217 if (byte_size1 < byte_size2)
23218 return 1;
23219 if (byte_size1 > byte_size2)
23220 return -1;
23221 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23222 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23223 if (encoding1 < encoding2)
23224 return 1;
23225 if (encoding1 > encoding2)
23226 return -1;
23227 return 0;
23228 }
23229
23230 /* Move base types marked by mark_base_types as early as possible
23231 in the CU, sorted by decreasing usage count both to make the
23232 uleb128 references as small as possible and to make sure they
23233 will have die_offset already computed by calc_die_sizes when
23234 sizes of typed stack loc ops is computed. */
23235
23236 static void
23237 move_marked_base_types (void)
23238 {
23239 unsigned int i;
23240 dw_die_ref base_type, die, c;
23241
23242 if (base_types.is_empty ())
23243 return;
23244
23245 /* Sort by decreasing usage count, they will be added again in that
23246 order later on. */
23247 base_types.qsort (base_type_cmp);
23248 die = comp_unit_die ();
23249 c = die->die_child;
23250 do
23251 {
23252 dw_die_ref prev = c;
23253 c = c->die_sib;
23254 while (c->die_mark)
23255 {
23256 remove_child_with_prev (c, prev);
23257 /* As base types got marked, there must be at least
23258 one node other than DW_TAG_base_type. */
23259 gcc_assert (c != c->die_sib);
23260 c = c->die_sib;
23261 }
23262 }
23263 while (c != die->die_child);
23264 gcc_assert (die->die_child);
23265 c = die->die_child;
23266 for (i = 0; base_types.iterate (i, &base_type); i++)
23267 {
23268 base_type->die_mark = 0;
23269 base_type->die_sib = c->die_sib;
23270 c->die_sib = base_type;
23271 c = base_type;
23272 }
23273 }
23274
23275 /* Helper function for resolve_addr, attempt to resolve
23276 one CONST_STRING, return true if successful. Similarly verify that
23277 SYMBOL_REFs refer to variables emitted in the current CU. */
23278
23279 static bool
23280 resolve_one_addr (rtx *addr)
23281 {
23282 rtx rtl = *addr;
23283
23284 if (GET_CODE (rtl) == CONST_STRING)
23285 {
23286 size_t len = strlen (XSTR (rtl, 0)) + 1;
23287 tree t = build_string (len, XSTR (rtl, 0));
23288 tree tlen = size_int (len - 1);
23289 TREE_TYPE (t)
23290 = build_array_type (char_type_node, build_index_type (tlen));
23291 rtl = lookup_constant_def (t);
23292 if (!rtl || !MEM_P (rtl))
23293 return false;
23294 rtl = XEXP (rtl, 0);
23295 if (GET_CODE (rtl) == SYMBOL_REF
23296 && SYMBOL_REF_DECL (rtl)
23297 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23298 return false;
23299 vec_safe_push (used_rtx_array, rtl);
23300 *addr = rtl;
23301 return true;
23302 }
23303
23304 if (GET_CODE (rtl) == SYMBOL_REF
23305 && SYMBOL_REF_DECL (rtl))
23306 {
23307 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23308 {
23309 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23310 return false;
23311 }
23312 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23313 return false;
23314 }
23315
23316 if (GET_CODE (rtl) == CONST)
23317 {
23318 subrtx_ptr_iterator::array_type array;
23319 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23320 if (!resolve_one_addr (*iter))
23321 return false;
23322 }
23323
23324 return true;
23325 }
23326
23327 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23328 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23329 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23330
23331 static rtx
23332 string_cst_pool_decl (tree t)
23333 {
23334 rtx rtl = output_constant_def (t, 1);
23335 unsigned char *array;
23336 dw_loc_descr_ref l;
23337 tree decl;
23338 size_t len;
23339 dw_die_ref ref;
23340
23341 if (!rtl || !MEM_P (rtl))
23342 return NULL_RTX;
23343 rtl = XEXP (rtl, 0);
23344 if (GET_CODE (rtl) != SYMBOL_REF
23345 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23346 return NULL_RTX;
23347
23348 decl = SYMBOL_REF_DECL (rtl);
23349 if (!lookup_decl_die (decl))
23350 {
23351 len = TREE_STRING_LENGTH (t);
23352 vec_safe_push (used_rtx_array, rtl);
23353 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23354 array = ggc_vec_alloc<unsigned char> (len);
23355 memcpy (array, TREE_STRING_POINTER (t), len);
23356 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23357 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23358 l->dw_loc_oprnd2.v.val_vec.length = len;
23359 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23360 l->dw_loc_oprnd2.v.val_vec.array = array;
23361 add_AT_loc (ref, DW_AT_location, l);
23362 equate_decl_number_to_die (decl, ref);
23363 }
23364 return rtl;
23365 }
23366
23367 /* Helper function of resolve_addr_in_expr. LOC is
23368 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23369 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23370 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23371 with DW_OP_GNU_implicit_pointer if possible
23372 and return true, if unsuccessful, return false. */
23373
23374 static bool
23375 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23376 {
23377 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23378 HOST_WIDE_INT offset = 0;
23379 dw_die_ref ref = NULL;
23380 tree decl;
23381
23382 if (GET_CODE (rtl) == CONST
23383 && GET_CODE (XEXP (rtl, 0)) == PLUS
23384 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23385 {
23386 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23387 rtl = XEXP (XEXP (rtl, 0), 0);
23388 }
23389 if (GET_CODE (rtl) == CONST_STRING)
23390 {
23391 size_t len = strlen (XSTR (rtl, 0)) + 1;
23392 tree t = build_string (len, XSTR (rtl, 0));
23393 tree tlen = size_int (len - 1);
23394
23395 TREE_TYPE (t)
23396 = build_array_type (char_type_node, build_index_type (tlen));
23397 rtl = string_cst_pool_decl (t);
23398 if (!rtl)
23399 return false;
23400 }
23401 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23402 {
23403 decl = SYMBOL_REF_DECL (rtl);
23404 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23405 {
23406 ref = lookup_decl_die (decl);
23407 if (ref && (get_AT (ref, DW_AT_location)
23408 || get_AT (ref, DW_AT_const_value)))
23409 {
23410 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23411 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23412 loc->dw_loc_oprnd1.val_entry = NULL;
23413 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23414 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23415 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23416 loc->dw_loc_oprnd2.v.val_int = offset;
23417 return true;
23418 }
23419 }
23420 }
23421 return false;
23422 }
23423
23424 /* Helper function for resolve_addr, handle one location
23425 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23426 the location list couldn't be resolved. */
23427
23428 static bool
23429 resolve_addr_in_expr (dw_loc_descr_ref loc)
23430 {
23431 dw_loc_descr_ref keep = NULL;
23432 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23433 switch (loc->dw_loc_opc)
23434 {
23435 case DW_OP_addr:
23436 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23437 {
23438 if ((prev == NULL
23439 || prev->dw_loc_opc == DW_OP_piece
23440 || prev->dw_loc_opc == DW_OP_bit_piece)
23441 && loc->dw_loc_next
23442 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23443 && !dwarf_strict
23444 && optimize_one_addr_into_implicit_ptr (loc))
23445 break;
23446 return false;
23447 }
23448 break;
23449 case DW_OP_GNU_addr_index:
23450 case DW_OP_GNU_const_index:
23451 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23452 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23453 {
23454 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23455 if (!resolve_one_addr (&rtl))
23456 return false;
23457 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23458 loc->dw_loc_oprnd1.val_entry =
23459 add_addr_table_entry (rtl, ate_kind_rtx);
23460 }
23461 break;
23462 case DW_OP_const4u:
23463 case DW_OP_const8u:
23464 if (loc->dtprel
23465 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23466 return false;
23467 break;
23468 case DW_OP_plus_uconst:
23469 if (size_of_loc_descr (loc)
23470 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23471 + 1
23472 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23473 {
23474 dw_loc_descr_ref repl
23475 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23476 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23477 add_loc_descr (&repl, loc->dw_loc_next);
23478 *loc = *repl;
23479 }
23480 break;
23481 case DW_OP_implicit_value:
23482 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23483 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23484 return false;
23485 break;
23486 case DW_OP_GNU_implicit_pointer:
23487 case DW_OP_GNU_parameter_ref:
23488 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23489 {
23490 dw_die_ref ref
23491 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23492 if (ref == NULL)
23493 return false;
23494 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23495 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23496 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23497 }
23498 break;
23499 case DW_OP_GNU_const_type:
23500 case DW_OP_GNU_regval_type:
23501 case DW_OP_GNU_deref_type:
23502 case DW_OP_GNU_convert:
23503 case DW_OP_GNU_reinterpret:
23504 while (loc->dw_loc_next
23505 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23506 {
23507 dw_die_ref base1, base2;
23508 unsigned enc1, enc2, size1, size2;
23509 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23510 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23511 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23512 else if (loc->dw_loc_oprnd1.val_class
23513 == dw_val_class_unsigned_const)
23514 break;
23515 else
23516 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23517 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23518 == dw_val_class_unsigned_const)
23519 break;
23520 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23521 gcc_assert (base1->die_tag == DW_TAG_base_type
23522 && base2->die_tag == DW_TAG_base_type);
23523 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23524 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23525 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23526 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23527 if (size1 == size2
23528 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23529 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23530 && loc != keep)
23531 || enc1 == enc2))
23532 {
23533 /* Optimize away next DW_OP_GNU_convert after
23534 adjusting LOC's base type die reference. */
23535 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23536 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23537 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23538 else
23539 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23540 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23541 continue;
23542 }
23543 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23544 point typed stack entry. */
23545 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23546 keep = loc->dw_loc_next;
23547 break;
23548 }
23549 break;
23550 default:
23551 break;
23552 }
23553 return true;
23554 }
23555
23556 /* Helper function of resolve_addr. DIE had DW_AT_location of
23557 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23558 and DW_OP_addr couldn't be resolved. resolve_addr has already
23559 removed the DW_AT_location attribute. This function attempts to
23560 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23561 to it or DW_AT_const_value attribute, if possible. */
23562
23563 static void
23564 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23565 {
23566 if (TREE_CODE (decl) != VAR_DECL
23567 || lookup_decl_die (decl) != die
23568 || DECL_EXTERNAL (decl)
23569 || !TREE_STATIC (decl)
23570 || DECL_INITIAL (decl) == NULL_TREE
23571 || DECL_P (DECL_INITIAL (decl))
23572 || get_AT (die, DW_AT_const_value))
23573 return;
23574
23575 tree init = DECL_INITIAL (decl);
23576 HOST_WIDE_INT offset = 0;
23577 /* For variables that have been optimized away and thus
23578 don't have a memory location, see if we can emit
23579 DW_AT_const_value instead. */
23580 if (tree_add_const_value_attribute (die, init))
23581 return;
23582 if (dwarf_strict)
23583 return;
23584 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23585 and ADDR_EXPR refers to a decl that has DW_AT_location or
23586 DW_AT_const_value (but isn't addressable, otherwise
23587 resolving the original DW_OP_addr wouldn't fail), see if
23588 we can add DW_OP_GNU_implicit_pointer. */
23589 STRIP_NOPS (init);
23590 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23591 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23592 {
23593 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23594 init = TREE_OPERAND (init, 0);
23595 STRIP_NOPS (init);
23596 }
23597 if (TREE_CODE (init) != ADDR_EXPR)
23598 return;
23599 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23600 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23601 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23602 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23603 && TREE_OPERAND (init, 0) != decl))
23604 {
23605 dw_die_ref ref;
23606 dw_loc_descr_ref l;
23607
23608 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23609 {
23610 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23611 if (!rtl)
23612 return;
23613 decl = SYMBOL_REF_DECL (rtl);
23614 }
23615 else
23616 decl = TREE_OPERAND (init, 0);
23617 ref = lookup_decl_die (decl);
23618 if (ref == NULL
23619 || (!get_AT (ref, DW_AT_location)
23620 && !get_AT (ref, DW_AT_const_value)))
23621 return;
23622 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23623 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23624 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23625 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23626 add_AT_loc (die, DW_AT_location, l);
23627 }
23628 }
23629
23630 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23631 an address in .rodata section if the string literal is emitted there,
23632 or remove the containing location list or replace DW_AT_const_value
23633 with DW_AT_location and empty location expression, if it isn't found
23634 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23635 to something that has been emitted in the current CU. */
23636
23637 static void
23638 resolve_addr (dw_die_ref die)
23639 {
23640 dw_die_ref c;
23641 dw_attr_ref a;
23642 dw_loc_list_ref *curr, *start, loc;
23643 unsigned ix;
23644
23645 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23646 switch (AT_class (a))
23647 {
23648 case dw_val_class_loc_list:
23649 start = curr = AT_loc_list_ptr (a);
23650 loc = *curr;
23651 gcc_assert (loc);
23652 /* The same list can be referenced more than once. See if we have
23653 already recorded the result from a previous pass. */
23654 if (loc->replaced)
23655 *curr = loc->dw_loc_next;
23656 else if (!loc->resolved_addr)
23657 {
23658 /* As things stand, we do not expect or allow one die to
23659 reference a suffix of another die's location list chain.
23660 References must be identical or completely separate.
23661 There is therefore no need to cache the result of this
23662 pass on any list other than the first; doing so
23663 would lead to unnecessary writes. */
23664 while (*curr)
23665 {
23666 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23667 if (!resolve_addr_in_expr ((*curr)->expr))
23668 {
23669 dw_loc_list_ref next = (*curr)->dw_loc_next;
23670 dw_loc_descr_ref l = (*curr)->expr;
23671
23672 if (next && (*curr)->ll_symbol)
23673 {
23674 gcc_assert (!next->ll_symbol);
23675 next->ll_symbol = (*curr)->ll_symbol;
23676 }
23677 if (dwarf_split_debug_info)
23678 remove_loc_list_addr_table_entries (l);
23679 *curr = next;
23680 }
23681 else
23682 {
23683 mark_base_types ((*curr)->expr);
23684 curr = &(*curr)->dw_loc_next;
23685 }
23686 }
23687 if (loc == *start)
23688 loc->resolved_addr = 1;
23689 else
23690 {
23691 loc->replaced = 1;
23692 loc->dw_loc_next = *start;
23693 }
23694 }
23695 if (!*start)
23696 {
23697 remove_AT (die, a->dw_attr);
23698 ix--;
23699 }
23700 break;
23701 case dw_val_class_loc:
23702 {
23703 dw_loc_descr_ref l = AT_loc (a);
23704 /* For -gdwarf-2 don't attempt to optimize
23705 DW_AT_data_member_location containing
23706 DW_OP_plus_uconst - older consumers might
23707 rely on it being that op instead of a more complex,
23708 but shorter, location description. */
23709 if ((dwarf_version > 2
23710 || a->dw_attr != DW_AT_data_member_location
23711 || l == NULL
23712 || l->dw_loc_opc != DW_OP_plus_uconst
23713 || l->dw_loc_next != NULL)
23714 && !resolve_addr_in_expr (l))
23715 {
23716 if (dwarf_split_debug_info)
23717 remove_loc_list_addr_table_entries (l);
23718 if (l != NULL
23719 && l->dw_loc_next == NULL
23720 && l->dw_loc_opc == DW_OP_addr
23721 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23722 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23723 && a->dw_attr == DW_AT_location)
23724 {
23725 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23726 remove_AT (die, a->dw_attr);
23727 ix--;
23728 optimize_location_into_implicit_ptr (die, decl);
23729 break;
23730 }
23731 remove_AT (die, a->dw_attr);
23732 ix--;
23733 }
23734 else
23735 mark_base_types (l);
23736 }
23737 break;
23738 case dw_val_class_addr:
23739 if (a->dw_attr == DW_AT_const_value
23740 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23741 {
23742 if (AT_index (a) != NOT_INDEXED)
23743 remove_addr_table_entry (a->dw_attr_val.val_entry);
23744 remove_AT (die, a->dw_attr);
23745 ix--;
23746 }
23747 if (die->die_tag == DW_TAG_GNU_call_site
23748 && a->dw_attr == DW_AT_abstract_origin)
23749 {
23750 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23751 dw_die_ref tdie = lookup_decl_die (tdecl);
23752 if (tdie == NULL
23753 && DECL_EXTERNAL (tdecl)
23754 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23755 {
23756 force_decl_die (tdecl);
23757 tdie = lookup_decl_die (tdecl);
23758 }
23759 if (tdie)
23760 {
23761 a->dw_attr_val.val_class = dw_val_class_die_ref;
23762 a->dw_attr_val.v.val_die_ref.die = tdie;
23763 a->dw_attr_val.v.val_die_ref.external = 0;
23764 }
23765 else
23766 {
23767 if (AT_index (a) != NOT_INDEXED)
23768 remove_addr_table_entry (a->dw_attr_val.val_entry);
23769 remove_AT (die, a->dw_attr);
23770 ix--;
23771 }
23772 }
23773 break;
23774 default:
23775 break;
23776 }
23777
23778 FOR_EACH_CHILD (die, c, resolve_addr (c));
23779 }
23780 \f
23781 /* Helper routines for optimize_location_lists.
23782 This pass tries to share identical local lists in .debug_loc
23783 section. */
23784
23785 /* Iteratively hash operands of LOC opcode into HSTATE. */
23786
23787 static void
23788 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23789 {
23790 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23791 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23792
23793 switch (loc->dw_loc_opc)
23794 {
23795 case DW_OP_const4u:
23796 case DW_OP_const8u:
23797 if (loc->dtprel)
23798 goto hash_addr;
23799 /* FALLTHRU */
23800 case DW_OP_const1u:
23801 case DW_OP_const1s:
23802 case DW_OP_const2u:
23803 case DW_OP_const2s:
23804 case DW_OP_const4s:
23805 case DW_OP_const8s:
23806 case DW_OP_constu:
23807 case DW_OP_consts:
23808 case DW_OP_pick:
23809 case DW_OP_plus_uconst:
23810 case DW_OP_breg0:
23811 case DW_OP_breg1:
23812 case DW_OP_breg2:
23813 case DW_OP_breg3:
23814 case DW_OP_breg4:
23815 case DW_OP_breg5:
23816 case DW_OP_breg6:
23817 case DW_OP_breg7:
23818 case DW_OP_breg8:
23819 case DW_OP_breg9:
23820 case DW_OP_breg10:
23821 case DW_OP_breg11:
23822 case DW_OP_breg12:
23823 case DW_OP_breg13:
23824 case DW_OP_breg14:
23825 case DW_OP_breg15:
23826 case DW_OP_breg16:
23827 case DW_OP_breg17:
23828 case DW_OP_breg18:
23829 case DW_OP_breg19:
23830 case DW_OP_breg20:
23831 case DW_OP_breg21:
23832 case DW_OP_breg22:
23833 case DW_OP_breg23:
23834 case DW_OP_breg24:
23835 case DW_OP_breg25:
23836 case DW_OP_breg26:
23837 case DW_OP_breg27:
23838 case DW_OP_breg28:
23839 case DW_OP_breg29:
23840 case DW_OP_breg30:
23841 case DW_OP_breg31:
23842 case DW_OP_regx:
23843 case DW_OP_fbreg:
23844 case DW_OP_piece:
23845 case DW_OP_deref_size:
23846 case DW_OP_xderef_size:
23847 hstate.add_object (val1->v.val_int);
23848 break;
23849 case DW_OP_skip:
23850 case DW_OP_bra:
23851 {
23852 int offset;
23853
23854 gcc_assert (val1->val_class == dw_val_class_loc);
23855 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23856 hstate.add_object (offset);
23857 }
23858 break;
23859 case DW_OP_implicit_value:
23860 hstate.add_object (val1->v.val_unsigned);
23861 switch (val2->val_class)
23862 {
23863 case dw_val_class_const:
23864 hstate.add_object (val2->v.val_int);
23865 break;
23866 case dw_val_class_vec:
23867 {
23868 unsigned int elt_size = val2->v.val_vec.elt_size;
23869 unsigned int len = val2->v.val_vec.length;
23870
23871 hstate.add_int (elt_size);
23872 hstate.add_int (len);
23873 hstate.add (val2->v.val_vec.array, len * elt_size);
23874 }
23875 break;
23876 case dw_val_class_const_double:
23877 hstate.add_object (val2->v.val_double.low);
23878 hstate.add_object (val2->v.val_double.high);
23879 break;
23880 case dw_val_class_wide_int:
23881 hstate.add_object (*val2->v.val_wide);
23882 break;
23883 case dw_val_class_addr:
23884 inchash::add_rtx (val2->v.val_addr, hstate);
23885 break;
23886 default:
23887 gcc_unreachable ();
23888 }
23889 break;
23890 case DW_OP_bregx:
23891 case DW_OP_bit_piece:
23892 hstate.add_object (val1->v.val_int);
23893 hstate.add_object (val2->v.val_int);
23894 break;
23895 case DW_OP_addr:
23896 hash_addr:
23897 if (loc->dtprel)
23898 {
23899 unsigned char dtprel = 0xd1;
23900 hstate.add_object (dtprel);
23901 }
23902 inchash::add_rtx (val1->v.val_addr, hstate);
23903 break;
23904 case DW_OP_GNU_addr_index:
23905 case DW_OP_GNU_const_index:
23906 {
23907 if (loc->dtprel)
23908 {
23909 unsigned char dtprel = 0xd1;
23910 hstate.add_object (dtprel);
23911 }
23912 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23913 }
23914 break;
23915 case DW_OP_GNU_implicit_pointer:
23916 hstate.add_int (val2->v.val_int);
23917 break;
23918 case DW_OP_GNU_entry_value:
23919 hstate.add_object (val1->v.val_loc);
23920 break;
23921 case DW_OP_GNU_regval_type:
23922 case DW_OP_GNU_deref_type:
23923 {
23924 unsigned int byte_size
23925 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23926 unsigned int encoding
23927 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23928 hstate.add_object (val1->v.val_int);
23929 hstate.add_object (byte_size);
23930 hstate.add_object (encoding);
23931 }
23932 break;
23933 case DW_OP_GNU_convert:
23934 case DW_OP_GNU_reinterpret:
23935 if (val1->val_class == dw_val_class_unsigned_const)
23936 {
23937 hstate.add_object (val1->v.val_unsigned);
23938 break;
23939 }
23940 /* FALLTHRU */
23941 case DW_OP_GNU_const_type:
23942 {
23943 unsigned int byte_size
23944 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23945 unsigned int encoding
23946 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23947 hstate.add_object (byte_size);
23948 hstate.add_object (encoding);
23949 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23950 break;
23951 hstate.add_object (val2->val_class);
23952 switch (val2->val_class)
23953 {
23954 case dw_val_class_const:
23955 hstate.add_object (val2->v.val_int);
23956 break;
23957 case dw_val_class_vec:
23958 {
23959 unsigned int elt_size = val2->v.val_vec.elt_size;
23960 unsigned int len = val2->v.val_vec.length;
23961
23962 hstate.add_object (elt_size);
23963 hstate.add_object (len);
23964 hstate.add (val2->v.val_vec.array, len * elt_size);
23965 }
23966 break;
23967 case dw_val_class_const_double:
23968 hstate.add_object (val2->v.val_double.low);
23969 hstate.add_object (val2->v.val_double.high);
23970 break;
23971 case dw_val_class_wide_int:
23972 hstate.add_object (*val2->v.val_wide);
23973 break;
23974 default:
23975 gcc_unreachable ();
23976 }
23977 }
23978 break;
23979
23980 default:
23981 /* Other codes have no operands. */
23982 break;
23983 }
23984 }
23985
23986 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23987
23988 static inline void
23989 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23990 {
23991 dw_loc_descr_ref l;
23992 bool sizes_computed = false;
23993 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23994 size_of_locs (loc);
23995
23996 for (l = loc; l != NULL; l = l->dw_loc_next)
23997 {
23998 enum dwarf_location_atom opc = l->dw_loc_opc;
23999 hstate.add_object (opc);
24000 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24001 {
24002 size_of_locs (loc);
24003 sizes_computed = true;
24004 }
24005 hash_loc_operands (l, hstate);
24006 }
24007 }
24008
24009 /* Compute hash of the whole location list LIST_HEAD. */
24010
24011 static inline void
24012 hash_loc_list (dw_loc_list_ref list_head)
24013 {
24014 dw_loc_list_ref curr = list_head;
24015 inchash::hash hstate;
24016
24017 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24018 {
24019 hstate.add (curr->begin, strlen (curr->begin) + 1);
24020 hstate.add (curr->end, strlen (curr->end) + 1);
24021 if (curr->section)
24022 hstate.add (curr->section, strlen (curr->section) + 1);
24023 hash_locs (curr->expr, hstate);
24024 }
24025 list_head->hash = hstate.end ();
24026 }
24027
24028 /* Return true if X and Y opcodes have the same operands. */
24029
24030 static inline bool
24031 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24032 {
24033 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24034 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24035 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24036 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24037
24038 switch (x->dw_loc_opc)
24039 {
24040 case DW_OP_const4u:
24041 case DW_OP_const8u:
24042 if (x->dtprel)
24043 goto hash_addr;
24044 /* FALLTHRU */
24045 case DW_OP_const1u:
24046 case DW_OP_const1s:
24047 case DW_OP_const2u:
24048 case DW_OP_const2s:
24049 case DW_OP_const4s:
24050 case DW_OP_const8s:
24051 case DW_OP_constu:
24052 case DW_OP_consts:
24053 case DW_OP_pick:
24054 case DW_OP_plus_uconst:
24055 case DW_OP_breg0:
24056 case DW_OP_breg1:
24057 case DW_OP_breg2:
24058 case DW_OP_breg3:
24059 case DW_OP_breg4:
24060 case DW_OP_breg5:
24061 case DW_OP_breg6:
24062 case DW_OP_breg7:
24063 case DW_OP_breg8:
24064 case DW_OP_breg9:
24065 case DW_OP_breg10:
24066 case DW_OP_breg11:
24067 case DW_OP_breg12:
24068 case DW_OP_breg13:
24069 case DW_OP_breg14:
24070 case DW_OP_breg15:
24071 case DW_OP_breg16:
24072 case DW_OP_breg17:
24073 case DW_OP_breg18:
24074 case DW_OP_breg19:
24075 case DW_OP_breg20:
24076 case DW_OP_breg21:
24077 case DW_OP_breg22:
24078 case DW_OP_breg23:
24079 case DW_OP_breg24:
24080 case DW_OP_breg25:
24081 case DW_OP_breg26:
24082 case DW_OP_breg27:
24083 case DW_OP_breg28:
24084 case DW_OP_breg29:
24085 case DW_OP_breg30:
24086 case DW_OP_breg31:
24087 case DW_OP_regx:
24088 case DW_OP_fbreg:
24089 case DW_OP_piece:
24090 case DW_OP_deref_size:
24091 case DW_OP_xderef_size:
24092 return valx1->v.val_int == valy1->v.val_int;
24093 case DW_OP_skip:
24094 case DW_OP_bra:
24095 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24096 can cause irrelevant differences in dw_loc_addr. */
24097 gcc_assert (valx1->val_class == dw_val_class_loc
24098 && valy1->val_class == dw_val_class_loc
24099 && (dwarf_split_debug_info
24100 || x->dw_loc_addr == y->dw_loc_addr));
24101 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24102 case DW_OP_implicit_value:
24103 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24104 || valx2->val_class != valy2->val_class)
24105 return false;
24106 switch (valx2->val_class)
24107 {
24108 case dw_val_class_const:
24109 return valx2->v.val_int == valy2->v.val_int;
24110 case dw_val_class_vec:
24111 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24112 && valx2->v.val_vec.length == valy2->v.val_vec.length
24113 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24114 valx2->v.val_vec.elt_size
24115 * valx2->v.val_vec.length) == 0;
24116 case dw_val_class_const_double:
24117 return valx2->v.val_double.low == valy2->v.val_double.low
24118 && valx2->v.val_double.high == valy2->v.val_double.high;
24119 case dw_val_class_wide_int:
24120 return *valx2->v.val_wide == *valy2->v.val_wide;
24121 case dw_val_class_addr:
24122 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24123 default:
24124 gcc_unreachable ();
24125 }
24126 case DW_OP_bregx:
24127 case DW_OP_bit_piece:
24128 return valx1->v.val_int == valy1->v.val_int
24129 && valx2->v.val_int == valy2->v.val_int;
24130 case DW_OP_addr:
24131 hash_addr:
24132 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24133 case DW_OP_GNU_addr_index:
24134 case DW_OP_GNU_const_index:
24135 {
24136 rtx ax1 = valx1->val_entry->addr.rtl;
24137 rtx ay1 = valy1->val_entry->addr.rtl;
24138 return rtx_equal_p (ax1, ay1);
24139 }
24140 case DW_OP_GNU_implicit_pointer:
24141 return valx1->val_class == dw_val_class_die_ref
24142 && valx1->val_class == valy1->val_class
24143 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24144 && valx2->v.val_int == valy2->v.val_int;
24145 case DW_OP_GNU_entry_value:
24146 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24147 case DW_OP_GNU_const_type:
24148 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24149 || valx2->val_class != valy2->val_class)
24150 return false;
24151 switch (valx2->val_class)
24152 {
24153 case dw_val_class_const:
24154 return valx2->v.val_int == valy2->v.val_int;
24155 case dw_val_class_vec:
24156 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24157 && valx2->v.val_vec.length == valy2->v.val_vec.length
24158 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24159 valx2->v.val_vec.elt_size
24160 * valx2->v.val_vec.length) == 0;
24161 case dw_val_class_const_double:
24162 return valx2->v.val_double.low == valy2->v.val_double.low
24163 && valx2->v.val_double.high == valy2->v.val_double.high;
24164 case dw_val_class_wide_int:
24165 return *valx2->v.val_wide == *valy2->v.val_wide;
24166 default:
24167 gcc_unreachable ();
24168 }
24169 case DW_OP_GNU_regval_type:
24170 case DW_OP_GNU_deref_type:
24171 return valx1->v.val_int == valy1->v.val_int
24172 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24173 case DW_OP_GNU_convert:
24174 case DW_OP_GNU_reinterpret:
24175 if (valx1->val_class != valy1->val_class)
24176 return false;
24177 if (valx1->val_class == dw_val_class_unsigned_const)
24178 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24179 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24180 case DW_OP_GNU_parameter_ref:
24181 return valx1->val_class == dw_val_class_die_ref
24182 && valx1->val_class == valy1->val_class
24183 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24184 default:
24185 /* Other codes have no operands. */
24186 return true;
24187 }
24188 }
24189
24190 /* Return true if DWARF location expressions X and Y are the same. */
24191
24192 static inline bool
24193 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24194 {
24195 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24196 if (x->dw_loc_opc != y->dw_loc_opc
24197 || x->dtprel != y->dtprel
24198 || !compare_loc_operands (x, y))
24199 break;
24200 return x == NULL && y == NULL;
24201 }
24202
24203 /* Hashtable helpers. */
24204
24205 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24206 {
24207 typedef dw_loc_list_struct value_type;
24208 typedef dw_loc_list_struct compare_type;
24209 static inline hashval_t hash (const value_type *);
24210 static inline bool equal (const value_type *, const compare_type *);
24211 };
24212
24213 /* Return precomputed hash of location list X. */
24214
24215 inline hashval_t
24216 loc_list_hasher::hash (const value_type *x)
24217 {
24218 return x->hash;
24219 }
24220
24221 /* Return true if location lists A and B are the same. */
24222
24223 inline bool
24224 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24225 {
24226 if (a == b)
24227 return 1;
24228 if (a->hash != b->hash)
24229 return 0;
24230 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24231 if (strcmp (a->begin, b->begin) != 0
24232 || strcmp (a->end, b->end) != 0
24233 || (a->section == NULL) != (b->section == NULL)
24234 || (a->section && strcmp (a->section, b->section) != 0)
24235 || !compare_locs (a->expr, b->expr))
24236 break;
24237 return a == NULL && b == NULL;
24238 }
24239
24240 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24241
24242
24243 /* Recursively optimize location lists referenced from DIE
24244 children and share them whenever possible. */
24245
24246 static void
24247 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24248 {
24249 dw_die_ref c;
24250 dw_attr_ref a;
24251 unsigned ix;
24252 dw_loc_list_struct **slot;
24253
24254 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24255 if (AT_class (a) == dw_val_class_loc_list)
24256 {
24257 dw_loc_list_ref list = AT_loc_list (a);
24258 /* TODO: perform some optimizations here, before hashing
24259 it and storing into the hash table. */
24260 hash_loc_list (list);
24261 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24262 if (*slot == NULL)
24263 *slot = list;
24264 else
24265 a->dw_attr_val.v.val_loc_list = *slot;
24266 }
24267
24268 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24269 }
24270
24271
24272 /* Recursively assign each location list a unique index into the debug_addr
24273 section. */
24274
24275 static void
24276 index_location_lists (dw_die_ref die)
24277 {
24278 dw_die_ref c;
24279 dw_attr_ref a;
24280 unsigned ix;
24281
24282 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24283 if (AT_class (a) == dw_val_class_loc_list)
24284 {
24285 dw_loc_list_ref list = AT_loc_list (a);
24286 dw_loc_list_ref curr;
24287 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24288 {
24289 /* Don't index an entry that has already been indexed
24290 or won't be output. */
24291 if (curr->begin_entry != NULL
24292 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24293 continue;
24294
24295 curr->begin_entry
24296 = add_addr_table_entry (xstrdup (curr->begin),
24297 ate_kind_label);
24298 }
24299 }
24300
24301 FOR_EACH_CHILD (die, c, index_location_lists (c));
24302 }
24303
24304 /* Optimize location lists referenced from DIE
24305 children and share them whenever possible. */
24306
24307 static void
24308 optimize_location_lists (dw_die_ref die)
24309 {
24310 loc_list_hash_type htab (500);
24311 optimize_location_lists_1 (die, &htab);
24312 }
24313 \f
24314 /* Output stuff that dwarf requires at the end of every file,
24315 and generate the DWARF-2 debugging info. */
24316
24317 static void
24318 dwarf2out_finish (const char *filename)
24319 {
24320 limbo_die_node *node, *next_node;
24321 comdat_type_node *ctnode;
24322 unsigned int i;
24323 dw_die_ref main_comp_unit_die;
24324
24325 /* PCH might result in DW_AT_producer string being restored from the
24326 header compilation, so always fill it with empty string initially
24327 and overwrite only here. */
24328 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24329 producer_string = gen_producer_string ();
24330 producer->dw_attr_val.v.val_str->refcount--;
24331 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24332
24333 gen_scheduled_generic_parms_dies ();
24334 gen_remaining_tmpl_value_param_die_attribute ();
24335
24336 /* Add the name for the main input file now. We delayed this from
24337 dwarf2out_init to avoid complications with PCH. */
24338 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24339 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24340 add_comp_dir_attribute (comp_unit_die ());
24341 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24342 {
24343 bool p = false;
24344 file_table->traverse<bool *, file_table_relative_p> (&p);
24345 if (p)
24346 add_comp_dir_attribute (comp_unit_die ());
24347 }
24348
24349 if (deferred_locations_list)
24350 for (i = 0; i < deferred_locations_list->length (); i++)
24351 {
24352 add_location_or_const_value_attribute (
24353 (*deferred_locations_list)[i].die,
24354 (*deferred_locations_list)[i].variable,
24355 false,
24356 DW_AT_location);
24357 }
24358
24359 /* Traverse the limbo die list, and add parent/child links. The only
24360 dies without parents that should be here are concrete instances of
24361 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24362 For concrete instances, we can get the parent die from the abstract
24363 instance. */
24364 for (node = limbo_die_list; node; node = next_node)
24365 {
24366 dw_die_ref die = node->die;
24367 next_node = node->next;
24368
24369 if (die->die_parent == NULL)
24370 {
24371 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24372
24373 if (origin && origin->die_parent)
24374 add_child_die (origin->die_parent, die);
24375 else if (is_cu_die (die))
24376 ;
24377 else if (seen_error ())
24378 /* It's OK to be confused by errors in the input. */
24379 add_child_die (comp_unit_die (), die);
24380 else
24381 {
24382 /* In certain situations, the lexical block containing a
24383 nested function can be optimized away, which results
24384 in the nested function die being orphaned. Likewise
24385 with the return type of that nested function. Force
24386 this to be a child of the containing function.
24387
24388 It may happen that even the containing function got fully
24389 inlined and optimized out. In that case we are lost and
24390 assign the empty child. This should not be big issue as
24391 the function is likely unreachable too. */
24392 gcc_assert (node->created_for);
24393
24394 if (DECL_P (node->created_for))
24395 origin = get_context_die (DECL_CONTEXT (node->created_for));
24396 else if (TYPE_P (node->created_for))
24397 origin = scope_die_for (node->created_for, comp_unit_die ());
24398 else
24399 origin = comp_unit_die ();
24400
24401 add_child_die (origin, die);
24402 }
24403 }
24404 }
24405
24406 limbo_die_list = NULL;
24407
24408 #if ENABLE_ASSERT_CHECKING
24409 {
24410 dw_die_ref die = comp_unit_die (), c;
24411 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24412 }
24413 #endif
24414 resolve_addr (comp_unit_die ());
24415 move_marked_base_types ();
24416
24417 for (node = deferred_asm_name; node; node = node->next)
24418 {
24419 tree decl = node->created_for;
24420 /* When generating LTO bytecode we can not generate new assembler
24421 names at this point and all important decls got theirs via
24422 free-lang-data. */
24423 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24424 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24425 {
24426 add_linkage_attr (node->die, decl);
24427 move_linkage_attr (node->die);
24428 }
24429 }
24430
24431 deferred_asm_name = NULL;
24432
24433 /* Walk through the list of incomplete types again, trying once more to
24434 emit full debugging info for them. */
24435 retry_incomplete_types ();
24436
24437 if (flag_eliminate_unused_debug_types)
24438 prune_unused_types ();
24439
24440 /* Generate separate COMDAT sections for type DIEs. */
24441 if (use_debug_types)
24442 {
24443 break_out_comdat_types (comp_unit_die ());
24444
24445 /* Each new type_unit DIE was added to the limbo die list when created.
24446 Since these have all been added to comdat_type_list, clear the
24447 limbo die list. */
24448 limbo_die_list = NULL;
24449
24450 /* For each new comdat type unit, copy declarations for incomplete
24451 types to make the new unit self-contained (i.e., no direct
24452 references to the main compile unit). */
24453 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24454 copy_decls_for_unworthy_types (ctnode->root_die);
24455 copy_decls_for_unworthy_types (comp_unit_die ());
24456
24457 /* In the process of copying declarations from one unit to another,
24458 we may have left some declarations behind that are no longer
24459 referenced. Prune them. */
24460 prune_unused_types ();
24461 }
24462
24463 /* Generate separate CUs for each of the include files we've seen.
24464 They will go into limbo_die_list. */
24465 if (flag_eliminate_dwarf2_dups)
24466 break_out_includes (comp_unit_die ());
24467
24468 /* Traverse the DIE's and add add sibling attributes to those DIE's
24469 that have children. */
24470 add_sibling_attributes (comp_unit_die ());
24471 for (node = limbo_die_list; node; node = node->next)
24472 add_sibling_attributes (node->die);
24473 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24474 add_sibling_attributes (ctnode->root_die);
24475
24476 /* When splitting DWARF info, we put some attributes in the
24477 skeleton compile_unit DIE that remains in the .o, while
24478 most attributes go in the DWO compile_unit_die. */
24479 if (dwarf_split_debug_info)
24480 main_comp_unit_die = gen_compile_unit_die (NULL);
24481 else
24482 main_comp_unit_die = comp_unit_die ();
24483
24484 /* Output a terminator label for the .text section. */
24485 switch_to_section (text_section);
24486 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24487 if (cold_text_section)
24488 {
24489 switch_to_section (cold_text_section);
24490 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24491 }
24492
24493 /* We can only use the low/high_pc attributes if all of the code was
24494 in .text. */
24495 if (!have_multiple_function_sections
24496 || (dwarf_version < 3 && dwarf_strict))
24497 {
24498 /* Don't add if the CU has no associated code. */
24499 if (text_section_used)
24500 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24501 text_end_label, true);
24502 }
24503 else
24504 {
24505 unsigned fde_idx;
24506 dw_fde_ref fde;
24507 bool range_list_added = false;
24508
24509 if (text_section_used)
24510 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24511 text_end_label, &range_list_added, true);
24512 if (cold_text_section_used)
24513 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24514 cold_end_label, &range_list_added, true);
24515
24516 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24517 {
24518 if (DECL_IGNORED_P (fde->decl))
24519 continue;
24520 if (!fde->in_std_section)
24521 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24522 fde->dw_fde_end, &range_list_added,
24523 true);
24524 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24525 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24526 fde->dw_fde_second_end, &range_list_added,
24527 true);
24528 }
24529
24530 if (range_list_added)
24531 {
24532 /* We need to give .debug_loc and .debug_ranges an appropriate
24533 "base address". Use zero so that these addresses become
24534 absolute. Historically, we've emitted the unexpected
24535 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24536 Emit both to give time for other tools to adapt. */
24537 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24538 if (! dwarf_strict && dwarf_version < 4)
24539 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24540
24541 add_ranges (NULL);
24542 }
24543 }
24544
24545 if (debug_info_level >= DINFO_LEVEL_TERSE)
24546 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24547 debug_line_section_label);
24548
24549 if (have_macinfo)
24550 add_AT_macptr (comp_unit_die (),
24551 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24552 macinfo_section_label);
24553
24554 if (dwarf_split_debug_info)
24555 {
24556 /* optimize_location_lists calculates the size of the lists,
24557 so index them first, and assign indices to the entries.
24558 Although optimize_location_lists will remove entries from
24559 the table, it only does so for duplicates, and therefore
24560 only reduces ref_counts to 1. */
24561 index_location_lists (comp_unit_die ());
24562
24563 if (addr_index_table != NULL)
24564 {
24565 unsigned int index = 0;
24566 addr_index_table
24567 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24568 (&index);
24569 }
24570 }
24571
24572 if (have_location_lists)
24573 optimize_location_lists (comp_unit_die ());
24574
24575 save_macinfo_strings ();
24576
24577 if (dwarf_split_debug_info)
24578 {
24579 unsigned int index = 0;
24580
24581 /* Add attributes common to skeleton compile_units and
24582 type_units. Because these attributes include strings, it
24583 must be done before freezing the string table. Top-level
24584 skeleton die attrs are added when the skeleton type unit is
24585 created, so ensure it is created by this point. */
24586 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24587 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24588 }
24589
24590 /* Output all of the compilation units. We put the main one last so that
24591 the offsets are available to output_pubnames. */
24592 for (node = limbo_die_list; node; node = node->next)
24593 output_comp_unit (node->die, 0);
24594
24595 hash_table<comdat_type_hasher> comdat_type_table (100);
24596 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24597 {
24598 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24599
24600 /* Don't output duplicate types. */
24601 if (*slot != HTAB_EMPTY_ENTRY)
24602 continue;
24603
24604 /* Add a pointer to the line table for the main compilation unit
24605 so that the debugger can make sense of DW_AT_decl_file
24606 attributes. */
24607 if (debug_info_level >= DINFO_LEVEL_TERSE)
24608 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24609 (!dwarf_split_debug_info
24610 ? debug_line_section_label
24611 : debug_skeleton_line_section_label));
24612
24613 output_comdat_type_unit (ctnode);
24614 *slot = ctnode;
24615 }
24616
24617 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24618 both the main_cu and all skeleton TUs. Making this call unconditional
24619 would end up either adding a second copy of the AT_pubnames attribute, or
24620 requiring a special case in add_top_level_skeleton_die_attrs. */
24621 if (!dwarf_split_debug_info)
24622 add_AT_pubnames (comp_unit_die ());
24623
24624 if (dwarf_split_debug_info)
24625 {
24626 int mark;
24627 unsigned char checksum[16];
24628 struct md5_ctx ctx;
24629
24630 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24631 md5_init_ctx (&ctx);
24632 mark = 0;
24633 die_checksum (comp_unit_die (), &ctx, &mark);
24634 unmark_all_dies (comp_unit_die ());
24635 md5_finish_ctx (&ctx, checksum);
24636
24637 /* Use the first 8 bytes of the checksum as the dwo_id,
24638 and add it to both comp-unit DIEs. */
24639 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24640 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24641
24642 /* Add the base offset of the ranges table to the skeleton
24643 comp-unit DIE. */
24644 if (ranges_table_in_use)
24645 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24646 ranges_section_label);
24647
24648 switch_to_section (debug_addr_section);
24649 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24650 output_addr_table ();
24651 }
24652
24653 /* Output the main compilation unit if non-empty or if .debug_macinfo
24654 or .debug_macro will be emitted. */
24655 output_comp_unit (comp_unit_die (), have_macinfo);
24656
24657 if (dwarf_split_debug_info && info_section_emitted)
24658 output_skeleton_debug_sections (main_comp_unit_die);
24659
24660 /* Output the abbreviation table. */
24661 if (abbrev_die_table_in_use != 1)
24662 {
24663 switch_to_section (debug_abbrev_section);
24664 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24665 output_abbrev_section ();
24666 }
24667
24668 /* Output location list section if necessary. */
24669 if (have_location_lists)
24670 {
24671 /* Output the location lists info. */
24672 switch_to_section (debug_loc_section);
24673 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24674 output_location_lists (comp_unit_die ());
24675 }
24676
24677 output_pubtables ();
24678
24679 /* Output the address range information if a CU (.debug_info section)
24680 was emitted. We output an empty table even if we had no functions
24681 to put in it. This because the consumer has no way to tell the
24682 difference between an empty table that we omitted and failure to
24683 generate a table that would have contained data. */
24684 if (info_section_emitted)
24685 {
24686 unsigned long aranges_length = size_of_aranges ();
24687
24688 switch_to_section (debug_aranges_section);
24689 output_aranges (aranges_length);
24690 }
24691
24692 /* Output ranges section if necessary. */
24693 if (ranges_table_in_use)
24694 {
24695 switch_to_section (debug_ranges_section);
24696 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24697 output_ranges ();
24698 }
24699
24700 /* Have to end the macro section. */
24701 if (have_macinfo)
24702 {
24703 switch_to_section (debug_macinfo_section);
24704 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24705 output_macinfo ();
24706 dw2_asm_output_data (1, 0, "End compilation unit");
24707 }
24708
24709 /* Output the source line correspondence table. We must do this
24710 even if there is no line information. Otherwise, on an empty
24711 translation unit, we will generate a present, but empty,
24712 .debug_info section. IRIX 6.5 `nm' will then complain when
24713 examining the file. This is done late so that any filenames
24714 used by the debug_info section are marked as 'used'. */
24715 switch_to_section (debug_line_section);
24716 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24717 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24718 output_line_info (false);
24719
24720 if (dwarf_split_debug_info && info_section_emitted)
24721 {
24722 switch_to_section (debug_skeleton_line_section);
24723 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24724 output_line_info (true);
24725 }
24726
24727 /* If we emitted any indirect strings, output the string table too. */
24728 if (debug_str_hash || skeleton_debug_str_hash)
24729 output_indirect_strings ();
24730 }
24731
24732 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24733 within the same process. For use by toplev::finalize. */
24734
24735 void
24736 dwarf2out_c_finalize (void)
24737 {
24738 last_var_location_insn = NULL;
24739 cached_next_real_insn = NULL;
24740 used_rtx_array = NULL;
24741 incomplete_types = NULL;
24742 decl_scope_table = NULL;
24743 debug_info_section = NULL;
24744 debug_skeleton_info_section = NULL;
24745 debug_abbrev_section = NULL;
24746 debug_skeleton_abbrev_section = NULL;
24747 debug_aranges_section = NULL;
24748 debug_addr_section = NULL;
24749 debug_macinfo_section = NULL;
24750 debug_line_section = NULL;
24751 debug_skeleton_line_section = NULL;
24752 debug_loc_section = NULL;
24753 debug_pubnames_section = NULL;
24754 debug_pubtypes_section = NULL;
24755 debug_str_section = NULL;
24756 debug_str_dwo_section = NULL;
24757 debug_str_offsets_section = NULL;
24758 debug_ranges_section = NULL;
24759 debug_frame_section = NULL;
24760 fde_vec = NULL;
24761 debug_str_hash = NULL;
24762 skeleton_debug_str_hash = NULL;
24763 dw2_string_counter = 0;
24764 have_multiple_function_sections = false;
24765 text_section_used = false;
24766 cold_text_section_used = false;
24767 cold_text_section = NULL;
24768 current_unit_personality = NULL;
24769
24770 deferred_locations_list = NULL;
24771
24772 next_die_offset = 0;
24773 single_comp_unit_die = NULL;
24774 comdat_type_list = NULL;
24775 limbo_die_list = NULL;
24776 deferred_asm_name = NULL;
24777 file_table = NULL;
24778 decl_die_table = NULL;
24779 common_block_die_table = NULL;
24780 decl_loc_table = NULL;
24781 call_arg_locations = NULL;
24782 call_arg_loc_last = NULL;
24783 call_site_count = -1;
24784 tail_call_site_count = -1;
24785 //block_map = NULL;
24786 cached_dw_loc_list_table = NULL;
24787 abbrev_die_table = NULL;
24788 abbrev_die_table_allocated = 0;
24789 abbrev_die_table_in_use = 0;
24790 line_info_label_num = 0;
24791 cur_line_info_table = NULL;
24792 text_section_line_info = NULL;
24793 cold_text_section_line_info = NULL;
24794 separate_line_info = NULL;
24795 info_section_emitted = false;
24796 pubname_table = NULL;
24797 pubtype_table = NULL;
24798 macinfo_table = NULL;
24799 ranges_table = NULL;
24800 ranges_table_allocated = 0;
24801 ranges_table_in_use = 0;
24802 ranges_by_label = 0;
24803 ranges_by_label_allocated = 0;
24804 ranges_by_label_in_use = 0;
24805 have_location_lists = false;
24806 loclabel_num = 0;
24807 poc_label_num = 0;
24808 last_emitted_file = NULL;
24809 label_num = 0;
24810 file_table_last_lookup = NULL;
24811 tmpl_value_parm_die_table = NULL;
24812 generic_type_instances = NULL;
24813 frame_pointer_fb_offset = 0;
24814 frame_pointer_fb_offset_valid = false;
24815 base_types.release ();
24816 XDELETEVEC (producer_string);
24817 producer_string = NULL;
24818 }
24819
24820 #include "gt-dwarf2out.h"