]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dwarf2out.c
25307d22d7b017a7f8ece71ac3bcb71a11f6bebb
[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 }
4689
4690 /* Return TRUE if the language is Java. */
4691
4692 static inline bool
4693 is_java (void)
4694 {
4695 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4696
4697 return lang == DW_LANG_Java;
4698 }
4699
4700 /* Return TRUE if the language is Fortran. */
4701
4702 static inline bool
4703 is_fortran (void)
4704 {
4705 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4706
4707 return (lang == DW_LANG_Fortran77
4708 || lang == DW_LANG_Fortran90
4709 || lang == DW_LANG_Fortran95);
4710 }
4711
4712 /* Return TRUE if the language is Ada. */
4713
4714 static inline bool
4715 is_ada (void)
4716 {
4717 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4718
4719 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4720 }
4721
4722 /* Remove the specified attribute if present. */
4723
4724 static void
4725 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4726 {
4727 dw_attr_ref a;
4728 unsigned ix;
4729
4730 if (! die)
4731 return;
4732
4733 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4734 if (a->dw_attr == attr_kind)
4735 {
4736 if (AT_class (a) == dw_val_class_str)
4737 if (a->dw_attr_val.v.val_str->refcount)
4738 a->dw_attr_val.v.val_str->refcount--;
4739
4740 /* vec::ordered_remove should help reduce the number of abbrevs
4741 that are needed. */
4742 die->die_attr->ordered_remove (ix);
4743 return;
4744 }
4745 }
4746
4747 /* Remove CHILD from its parent. PREV must have the property that
4748 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4749
4750 static void
4751 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4752 {
4753 gcc_assert (child->die_parent == prev->die_parent);
4754 gcc_assert (prev->die_sib == child);
4755 if (prev == child)
4756 {
4757 gcc_assert (child->die_parent->die_child == child);
4758 prev = NULL;
4759 }
4760 else
4761 prev->die_sib = child->die_sib;
4762 if (child->die_parent->die_child == child)
4763 child->die_parent->die_child = prev;
4764 }
4765
4766 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4767 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4768
4769 static void
4770 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4771 {
4772 dw_die_ref parent = old_child->die_parent;
4773
4774 gcc_assert (parent == prev->die_parent);
4775 gcc_assert (prev->die_sib == old_child);
4776
4777 new_child->die_parent = parent;
4778 if (prev == old_child)
4779 {
4780 gcc_assert (parent->die_child == old_child);
4781 new_child->die_sib = new_child;
4782 }
4783 else
4784 {
4785 prev->die_sib = new_child;
4786 new_child->die_sib = old_child->die_sib;
4787 }
4788 if (old_child->die_parent->die_child == old_child)
4789 old_child->die_parent->die_child = new_child;
4790 }
4791
4792 /* Move all children from OLD_PARENT to NEW_PARENT. */
4793
4794 static void
4795 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4796 {
4797 dw_die_ref c;
4798 new_parent->die_child = old_parent->die_child;
4799 old_parent->die_child = NULL;
4800 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4801 }
4802
4803 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4804 matches TAG. */
4805
4806 static void
4807 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4808 {
4809 dw_die_ref c;
4810
4811 c = die->die_child;
4812 if (c) do {
4813 dw_die_ref prev = c;
4814 c = c->die_sib;
4815 while (c->die_tag == tag)
4816 {
4817 remove_child_with_prev (c, prev);
4818 /* Might have removed every child. */
4819 if (c == c->die_sib)
4820 return;
4821 c = c->die_sib;
4822 }
4823 } while (c != die->die_child);
4824 }
4825
4826 /* Add a CHILD_DIE as the last child of DIE. */
4827
4828 static void
4829 add_child_die (dw_die_ref die, dw_die_ref child_die)
4830 {
4831 /* FIXME this should probably be an assert. */
4832 if (! die || ! child_die)
4833 return;
4834 gcc_assert (die != child_die);
4835
4836 child_die->die_parent = die;
4837 if (die->die_child)
4838 {
4839 child_die->die_sib = die->die_child->die_sib;
4840 die->die_child->die_sib = child_die;
4841 }
4842 else
4843 child_die->die_sib = child_die;
4844 die->die_child = child_die;
4845 }
4846
4847 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4848 is the specification, to the end of PARENT's list of children.
4849 This is done by removing and re-adding it. */
4850
4851 static void
4852 splice_child_die (dw_die_ref parent, dw_die_ref child)
4853 {
4854 dw_die_ref p;
4855
4856 /* We want the declaration DIE from inside the class, not the
4857 specification DIE at toplevel. */
4858 if (child->die_parent != parent)
4859 {
4860 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4861
4862 if (tmp)
4863 child = tmp;
4864 }
4865
4866 gcc_assert (child->die_parent == parent
4867 || (child->die_parent
4868 == get_AT_ref (parent, DW_AT_specification)));
4869
4870 for (p = child->die_parent->die_child; ; p = p->die_sib)
4871 if (p->die_sib == child)
4872 {
4873 remove_child_with_prev (child, p);
4874 break;
4875 }
4876
4877 add_child_die (parent, child);
4878 }
4879
4880 /* Return a pointer to a newly created DIE node. */
4881
4882 static inline dw_die_ref
4883 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4884 {
4885 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4886
4887 die->die_tag = tag_value;
4888
4889 if (parent_die != NULL)
4890 add_child_die (parent_die, die);
4891 else
4892 {
4893 limbo_die_node *limbo_node;
4894
4895 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4896 limbo_node->die = die;
4897 limbo_node->created_for = t;
4898 limbo_node->next = limbo_die_list;
4899 limbo_die_list = limbo_node;
4900 }
4901
4902 return die;
4903 }
4904
4905 /* Return the DIE associated with the given type specifier. */
4906
4907 static inline dw_die_ref
4908 lookup_type_die (tree type)
4909 {
4910 return TYPE_SYMTAB_DIE (type);
4911 }
4912
4913 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4914 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4915 anonymous type instead the one of the naming typedef. */
4916
4917 static inline dw_die_ref
4918 strip_naming_typedef (tree type, dw_die_ref type_die)
4919 {
4920 if (type
4921 && TREE_CODE (type) == RECORD_TYPE
4922 && type_die
4923 && type_die->die_tag == DW_TAG_typedef
4924 && is_naming_typedef_decl (TYPE_NAME (type)))
4925 type_die = get_AT_ref (type_die, DW_AT_type);
4926 return type_die;
4927 }
4928
4929 /* Like lookup_type_die, but if type is an anonymous type named by a
4930 typedef[1], return the DIE of the anonymous type instead the one of
4931 the naming typedef. This is because in gen_typedef_die, we did
4932 equate the anonymous struct named by the typedef with the DIE of
4933 the naming typedef. So by default, lookup_type_die on an anonymous
4934 struct yields the DIE of the naming typedef.
4935
4936 [1]: Read the comment of is_naming_typedef_decl to learn about what
4937 a naming typedef is. */
4938
4939 static inline dw_die_ref
4940 lookup_type_die_strip_naming_typedef (tree type)
4941 {
4942 dw_die_ref die = lookup_type_die (type);
4943 return strip_naming_typedef (type, die);
4944 }
4945
4946 /* Equate a DIE to a given type specifier. */
4947
4948 static inline void
4949 equate_type_number_to_die (tree type, dw_die_ref type_die)
4950 {
4951 TYPE_SYMTAB_DIE (type) = type_die;
4952 }
4953
4954 /* Returns a hash value for X (which really is a die_struct). */
4955
4956 inline hashval_t
4957 decl_die_hasher::hash (die_node *x)
4958 {
4959 return (hashval_t) x->decl_id;
4960 }
4961
4962 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4963
4964 inline bool
4965 decl_die_hasher::equal (die_node *x, tree y)
4966 {
4967 return (x->decl_id == DECL_UID (y));
4968 }
4969
4970 /* Return the DIE associated with a given declaration. */
4971
4972 static inline dw_die_ref
4973 lookup_decl_die (tree decl)
4974 {
4975 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
4976 }
4977
4978 /* Returns a hash value for X (which really is a var_loc_list). */
4979
4980 inline hashval_t
4981 decl_loc_hasher::hash (var_loc_list *x)
4982 {
4983 return (hashval_t) x->decl_id;
4984 }
4985
4986 /* Return nonzero if decl_id of var_loc_list X is the same as
4987 UID of decl *Y. */
4988
4989 inline bool
4990 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
4991 {
4992 return (x->decl_id == DECL_UID (y));
4993 }
4994
4995 /* Return the var_loc list associated with a given declaration. */
4996
4997 static inline var_loc_list *
4998 lookup_decl_loc (const_tree decl)
4999 {
5000 if (!decl_loc_table)
5001 return NULL;
5002 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5003 }
5004
5005 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5006
5007 inline hashval_t
5008 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5009 {
5010 return (hashval_t) x->decl_id;
5011 }
5012
5013 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5014 UID of decl *Y. */
5015
5016 inline bool
5017 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5018 {
5019 return (x->decl_id == DECL_UID (y));
5020 }
5021
5022 /* Equate a DIE to a particular declaration. */
5023
5024 static void
5025 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5026 {
5027 unsigned int decl_id = DECL_UID (decl);
5028
5029 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5030 decl_die->decl_id = decl_id;
5031 }
5032
5033 /* Return how many bits covers PIECE EXPR_LIST. */
5034
5035 static int
5036 decl_piece_bitsize (rtx piece)
5037 {
5038 int ret = (int) GET_MODE (piece);
5039 if (ret)
5040 return ret;
5041 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5042 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5043 return INTVAL (XEXP (XEXP (piece, 0), 0));
5044 }
5045
5046 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5047
5048 static rtx *
5049 decl_piece_varloc_ptr (rtx piece)
5050 {
5051 if ((int) GET_MODE (piece))
5052 return &XEXP (piece, 0);
5053 else
5054 return &XEXP (XEXP (piece, 0), 1);
5055 }
5056
5057 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5058 Next is the chain of following piece nodes. */
5059
5060 static rtx_expr_list *
5061 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5062 {
5063 if (bitsize <= (int) MAX_MACHINE_MODE)
5064 return alloc_EXPR_LIST (bitsize, loc_note, next);
5065 else
5066 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5067 GEN_INT (bitsize),
5068 loc_note), next);
5069 }
5070
5071 /* Return rtx that should be stored into loc field for
5072 LOC_NOTE and BITPOS/BITSIZE. */
5073
5074 static rtx
5075 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5076 HOST_WIDE_INT bitsize)
5077 {
5078 if (bitsize != -1)
5079 {
5080 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5081 if (bitpos != 0)
5082 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5083 }
5084 return loc_note;
5085 }
5086
5087 /* This function either modifies location piece list *DEST in
5088 place (if SRC and INNER is NULL), or copies location piece list
5089 *SRC to *DEST while modifying it. Location BITPOS is modified
5090 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5091 not copied and if needed some padding around it is added.
5092 When modifying in place, DEST should point to EXPR_LIST where
5093 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5094 to the start of the whole list and INNER points to the EXPR_LIST
5095 where earlier pieces cover PIECE_BITPOS bits. */
5096
5097 static void
5098 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5099 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5100 HOST_WIDE_INT bitsize, rtx loc_note)
5101 {
5102 int diff;
5103 bool copy = inner != NULL;
5104
5105 if (copy)
5106 {
5107 /* First copy all nodes preceding the current bitpos. */
5108 while (src != inner)
5109 {
5110 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5111 decl_piece_bitsize (*src), NULL_RTX);
5112 dest = &XEXP (*dest, 1);
5113 src = &XEXP (*src, 1);
5114 }
5115 }
5116 /* Add padding if needed. */
5117 if (bitpos != piece_bitpos)
5118 {
5119 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5120 copy ? NULL_RTX : *dest);
5121 dest = &XEXP (*dest, 1);
5122 }
5123 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5124 {
5125 gcc_assert (!copy);
5126 /* A piece with correct bitpos and bitsize already exist,
5127 just update the location for it and return. */
5128 *decl_piece_varloc_ptr (*dest) = loc_note;
5129 return;
5130 }
5131 /* Add the piece that changed. */
5132 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5133 dest = &XEXP (*dest, 1);
5134 /* Skip over pieces that overlap it. */
5135 diff = bitpos - piece_bitpos + bitsize;
5136 if (!copy)
5137 src = dest;
5138 while (diff > 0 && *src)
5139 {
5140 rtx piece = *src;
5141 diff -= decl_piece_bitsize (piece);
5142 if (copy)
5143 src = &XEXP (piece, 1);
5144 else
5145 {
5146 *src = XEXP (piece, 1);
5147 free_EXPR_LIST_node (piece);
5148 }
5149 }
5150 /* Add padding if needed. */
5151 if (diff < 0 && *src)
5152 {
5153 if (!copy)
5154 dest = src;
5155 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5156 dest = &XEXP (*dest, 1);
5157 }
5158 if (!copy)
5159 return;
5160 /* Finally copy all nodes following it. */
5161 while (*src)
5162 {
5163 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5164 decl_piece_bitsize (*src), NULL_RTX);
5165 dest = &XEXP (*dest, 1);
5166 src = &XEXP (*src, 1);
5167 }
5168 }
5169
5170 /* Add a variable location node to the linked list for DECL. */
5171
5172 static struct var_loc_node *
5173 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5174 {
5175 unsigned int decl_id;
5176 var_loc_list *temp;
5177 struct var_loc_node *loc = NULL;
5178 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5179
5180 if (TREE_CODE (decl) == VAR_DECL
5181 && DECL_HAS_DEBUG_EXPR_P (decl))
5182 {
5183 tree realdecl = DECL_DEBUG_EXPR (decl);
5184 if (handled_component_p (realdecl)
5185 || (TREE_CODE (realdecl) == MEM_REF
5186 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5187 {
5188 HOST_WIDE_INT maxsize;
5189 tree innerdecl;
5190 innerdecl
5191 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5192 if (!DECL_P (innerdecl)
5193 || DECL_IGNORED_P (innerdecl)
5194 || TREE_STATIC (innerdecl)
5195 || bitsize <= 0
5196 || bitpos + bitsize > 256
5197 || bitsize != maxsize)
5198 return NULL;
5199 decl = innerdecl;
5200 }
5201 }
5202
5203 decl_id = DECL_UID (decl);
5204 var_loc_list **slot
5205 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5206 if (*slot == NULL)
5207 {
5208 temp = ggc_cleared_alloc<var_loc_list> ();
5209 temp->decl_id = decl_id;
5210 *slot = temp;
5211 }
5212 else
5213 temp = *slot;
5214
5215 /* For PARM_DECLs try to keep around the original incoming value,
5216 even if that means we'll emit a zero-range .debug_loc entry. */
5217 if (temp->last
5218 && temp->first == temp->last
5219 && TREE_CODE (decl) == PARM_DECL
5220 && NOTE_P (temp->first->loc)
5221 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5222 && DECL_INCOMING_RTL (decl)
5223 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5224 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5225 == GET_CODE (DECL_INCOMING_RTL (decl))
5226 && prev_real_insn (temp->first->loc) == NULL_RTX
5227 && (bitsize != -1
5228 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5229 NOTE_VAR_LOCATION_LOC (loc_note))
5230 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5231 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5232 {
5233 loc = ggc_cleared_alloc<var_loc_node> ();
5234 temp->first->next = loc;
5235 temp->last = loc;
5236 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5237 }
5238 else if (temp->last)
5239 {
5240 struct var_loc_node *last = temp->last, *unused = NULL;
5241 rtx *piece_loc = NULL, last_loc_note;
5242 int piece_bitpos = 0;
5243 if (last->next)
5244 {
5245 last = last->next;
5246 gcc_assert (last->next == NULL);
5247 }
5248 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5249 {
5250 piece_loc = &last->loc;
5251 do
5252 {
5253 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5254 if (piece_bitpos + cur_bitsize > bitpos)
5255 break;
5256 piece_bitpos += cur_bitsize;
5257 piece_loc = &XEXP (*piece_loc, 1);
5258 }
5259 while (*piece_loc);
5260 }
5261 /* TEMP->LAST here is either pointer to the last but one or
5262 last element in the chained list, LAST is pointer to the
5263 last element. */
5264 if (label && strcmp (last->label, label) == 0)
5265 {
5266 /* For SRA optimized variables if there weren't any real
5267 insns since last note, just modify the last node. */
5268 if (piece_loc != NULL)
5269 {
5270 adjust_piece_list (piece_loc, NULL, NULL,
5271 bitpos, piece_bitpos, bitsize, loc_note);
5272 return NULL;
5273 }
5274 /* If the last note doesn't cover any instructions, remove it. */
5275 if (temp->last != last)
5276 {
5277 temp->last->next = NULL;
5278 unused = last;
5279 last = temp->last;
5280 gcc_assert (strcmp (last->label, label) != 0);
5281 }
5282 else
5283 {
5284 gcc_assert (temp->first == temp->last
5285 || (temp->first->next == temp->last
5286 && TREE_CODE (decl) == PARM_DECL));
5287 memset (temp->last, '\0', sizeof (*temp->last));
5288 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5289 return temp->last;
5290 }
5291 }
5292 if (bitsize == -1 && NOTE_P (last->loc))
5293 last_loc_note = last->loc;
5294 else if (piece_loc != NULL
5295 && *piece_loc != NULL_RTX
5296 && piece_bitpos == bitpos
5297 && decl_piece_bitsize (*piece_loc) == bitsize)
5298 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5299 else
5300 last_loc_note = NULL_RTX;
5301 /* If the current location is the same as the end of the list,
5302 and either both or neither of the locations is uninitialized,
5303 we have nothing to do. */
5304 if (last_loc_note == NULL_RTX
5305 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5306 NOTE_VAR_LOCATION_LOC (loc_note)))
5307 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5308 != NOTE_VAR_LOCATION_STATUS (loc_note))
5309 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5310 == VAR_INIT_STATUS_UNINITIALIZED)
5311 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5312 == VAR_INIT_STATUS_UNINITIALIZED))))
5313 {
5314 /* Add LOC to the end of list and update LAST. If the last
5315 element of the list has been removed above, reuse its
5316 memory for the new node, otherwise allocate a new one. */
5317 if (unused)
5318 {
5319 loc = unused;
5320 memset (loc, '\0', sizeof (*loc));
5321 }
5322 else
5323 loc = ggc_cleared_alloc<var_loc_node> ();
5324 if (bitsize == -1 || piece_loc == NULL)
5325 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5326 else
5327 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5328 bitpos, piece_bitpos, bitsize, loc_note);
5329 last->next = loc;
5330 /* Ensure TEMP->LAST will point either to the new last but one
5331 element of the chain, or to the last element in it. */
5332 if (last != temp->last)
5333 temp->last = last;
5334 }
5335 else if (unused)
5336 ggc_free (unused);
5337 }
5338 else
5339 {
5340 loc = ggc_cleared_alloc<var_loc_node> ();
5341 temp->first = loc;
5342 temp->last = loc;
5343 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5344 }
5345 return loc;
5346 }
5347 \f
5348 /* Keep track of the number of spaces used to indent the
5349 output of the debugging routines that print the structure of
5350 the DIE internal representation. */
5351 static int print_indent;
5352
5353 /* Indent the line the number of spaces given by print_indent. */
5354
5355 static inline void
5356 print_spaces (FILE *outfile)
5357 {
5358 fprintf (outfile, "%*s", print_indent, "");
5359 }
5360
5361 /* Print a type signature in hex. */
5362
5363 static inline void
5364 print_signature (FILE *outfile, char *sig)
5365 {
5366 int i;
5367
5368 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5369 fprintf (outfile, "%02x", sig[i] & 0xff);
5370 }
5371
5372 /* Print the information associated with a given DIE, and its children.
5373 This routine is a debugging aid only. */
5374
5375 static void
5376 print_die (dw_die_ref die, FILE *outfile)
5377 {
5378 dw_attr_ref a;
5379 dw_die_ref c;
5380 unsigned ix;
5381
5382 print_spaces (outfile);
5383 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5384 die->die_offset, dwarf_tag_name (die->die_tag),
5385 (void*) die);
5386 print_spaces (outfile);
5387 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5388 fprintf (outfile, " offset: %ld", die->die_offset);
5389 fprintf (outfile, " mark: %d\n", die->die_mark);
5390
5391 if (die->comdat_type_p)
5392 {
5393 print_spaces (outfile);
5394 fprintf (outfile, " signature: ");
5395 print_signature (outfile, die->die_id.die_type_node->signature);
5396 fprintf (outfile, "\n");
5397 }
5398
5399 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5400 {
5401 print_spaces (outfile);
5402 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5403
5404 switch (AT_class (a))
5405 {
5406 case dw_val_class_addr:
5407 fprintf (outfile, "address");
5408 break;
5409 case dw_val_class_offset:
5410 fprintf (outfile, "offset");
5411 break;
5412 case dw_val_class_loc:
5413 fprintf (outfile, "location descriptor");
5414 break;
5415 case dw_val_class_loc_list:
5416 fprintf (outfile, "location list -> label:%s",
5417 AT_loc_list (a)->ll_symbol);
5418 break;
5419 case dw_val_class_range_list:
5420 fprintf (outfile, "range list");
5421 break;
5422 case dw_val_class_const:
5423 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5424 break;
5425 case dw_val_class_unsigned_const:
5426 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5427 break;
5428 case dw_val_class_const_double:
5429 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5430 HOST_WIDE_INT_PRINT_UNSIGNED")",
5431 a->dw_attr_val.v.val_double.high,
5432 a->dw_attr_val.v.val_double.low);
5433 break;
5434 case dw_val_class_wide_int:
5435 {
5436 int i = a->dw_attr_val.v.val_wide->get_len ();
5437 fprintf (outfile, "constant (");
5438 gcc_assert (i > 0);
5439 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5440 fprintf (outfile, "0x");
5441 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5442 a->dw_attr_val.v.val_wide->elt (--i));
5443 while (--i >= 0)
5444 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5445 a->dw_attr_val.v.val_wide->elt (i));
5446 fprintf (outfile, ")");
5447 break;
5448 }
5449 case dw_val_class_vec:
5450 fprintf (outfile, "floating-point or vector constant");
5451 break;
5452 case dw_val_class_flag:
5453 fprintf (outfile, "%u", AT_flag (a));
5454 break;
5455 case dw_val_class_die_ref:
5456 if (AT_ref (a) != NULL)
5457 {
5458 if (AT_ref (a)->comdat_type_p)
5459 {
5460 fprintf (outfile, "die -> signature: ");
5461 print_signature (outfile,
5462 AT_ref (a)->die_id.die_type_node->signature);
5463 }
5464 else if (AT_ref (a)->die_id.die_symbol)
5465 fprintf (outfile, "die -> label: %s",
5466 AT_ref (a)->die_id.die_symbol);
5467 else
5468 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5469 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5470 }
5471 else
5472 fprintf (outfile, "die -> <null>");
5473 break;
5474 case dw_val_class_vms_delta:
5475 fprintf (outfile, "delta: @slotcount(%s-%s)",
5476 AT_vms_delta2 (a), AT_vms_delta1 (a));
5477 break;
5478 case dw_val_class_lbl_id:
5479 case dw_val_class_lineptr:
5480 case dw_val_class_macptr:
5481 case dw_val_class_high_pc:
5482 fprintf (outfile, "label: %s", AT_lbl (a));
5483 break;
5484 case dw_val_class_str:
5485 if (AT_string (a) != NULL)
5486 fprintf (outfile, "\"%s\"", AT_string (a));
5487 else
5488 fprintf (outfile, "<null>");
5489 break;
5490 case dw_val_class_file:
5491 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5492 AT_file (a)->emitted_number);
5493 break;
5494 case dw_val_class_data8:
5495 {
5496 int i;
5497
5498 for (i = 0; i < 8; i++)
5499 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5500 break;
5501 }
5502 default:
5503 break;
5504 }
5505
5506 fprintf (outfile, "\n");
5507 }
5508
5509 if (die->die_child != NULL)
5510 {
5511 print_indent += 4;
5512 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5513 print_indent -= 4;
5514 }
5515 if (print_indent == 0)
5516 fprintf (outfile, "\n");
5517 }
5518
5519 /* Print the information collected for a given DIE. */
5520
5521 DEBUG_FUNCTION void
5522 debug_dwarf_die (dw_die_ref die)
5523 {
5524 print_die (die, stderr);
5525 }
5526
5527 DEBUG_FUNCTION void
5528 debug (die_struct &ref)
5529 {
5530 print_die (&ref, stderr);
5531 }
5532
5533 DEBUG_FUNCTION void
5534 debug (die_struct *ptr)
5535 {
5536 if (ptr)
5537 debug (*ptr);
5538 else
5539 fprintf (stderr, "<nil>\n");
5540 }
5541
5542
5543 /* Print all DWARF information collected for the compilation unit.
5544 This routine is a debugging aid only. */
5545
5546 DEBUG_FUNCTION void
5547 debug_dwarf (void)
5548 {
5549 print_indent = 0;
5550 print_die (comp_unit_die (), stderr);
5551 }
5552 \f
5553 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5554 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5555 DIE that marks the start of the DIEs for this include file. */
5556
5557 static dw_die_ref
5558 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5559 {
5560 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5561 dw_die_ref new_unit = gen_compile_unit_die (filename);
5562
5563 new_unit->die_sib = old_unit;
5564 return new_unit;
5565 }
5566
5567 /* Close an include-file CU and reopen the enclosing one. */
5568
5569 static dw_die_ref
5570 pop_compile_unit (dw_die_ref old_unit)
5571 {
5572 dw_die_ref new_unit = old_unit->die_sib;
5573
5574 old_unit->die_sib = NULL;
5575 return new_unit;
5576 }
5577
5578 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5579 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5580 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5581
5582 /* Calculate the checksum of a location expression. */
5583
5584 static inline void
5585 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5586 {
5587 int tem;
5588 inchash::hash hstate;
5589 hashval_t hash;
5590
5591 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5592 CHECKSUM (tem);
5593 hash_loc_operands (loc, hstate);
5594 hash = hstate.end();
5595 CHECKSUM (hash);
5596 }
5597
5598 /* Calculate the checksum of an attribute. */
5599
5600 static void
5601 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5602 {
5603 dw_loc_descr_ref loc;
5604 rtx r;
5605
5606 CHECKSUM (at->dw_attr);
5607
5608 /* We don't care that this was compiled with a different compiler
5609 snapshot; if the output is the same, that's what matters. */
5610 if (at->dw_attr == DW_AT_producer)
5611 return;
5612
5613 switch (AT_class (at))
5614 {
5615 case dw_val_class_const:
5616 CHECKSUM (at->dw_attr_val.v.val_int);
5617 break;
5618 case dw_val_class_unsigned_const:
5619 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5620 break;
5621 case dw_val_class_const_double:
5622 CHECKSUM (at->dw_attr_val.v.val_double);
5623 break;
5624 case dw_val_class_wide_int:
5625 CHECKSUM (*at->dw_attr_val.v.val_wide);
5626 break;
5627 case dw_val_class_vec:
5628 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5629 (at->dw_attr_val.v.val_vec.length
5630 * at->dw_attr_val.v.val_vec.elt_size));
5631 break;
5632 case dw_val_class_flag:
5633 CHECKSUM (at->dw_attr_val.v.val_flag);
5634 break;
5635 case dw_val_class_str:
5636 CHECKSUM_STRING (AT_string (at));
5637 break;
5638
5639 case dw_val_class_addr:
5640 r = AT_addr (at);
5641 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5642 CHECKSUM_STRING (XSTR (r, 0));
5643 break;
5644
5645 case dw_val_class_offset:
5646 CHECKSUM (at->dw_attr_val.v.val_offset);
5647 break;
5648
5649 case dw_val_class_loc:
5650 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5651 loc_checksum (loc, ctx);
5652 break;
5653
5654 case dw_val_class_die_ref:
5655 die_checksum (AT_ref (at), ctx, mark);
5656 break;
5657
5658 case dw_val_class_fde_ref:
5659 case dw_val_class_vms_delta:
5660 case dw_val_class_lbl_id:
5661 case dw_val_class_lineptr:
5662 case dw_val_class_macptr:
5663 case dw_val_class_high_pc:
5664 break;
5665
5666 case dw_val_class_file:
5667 CHECKSUM_STRING (AT_file (at)->filename);
5668 break;
5669
5670 case dw_val_class_data8:
5671 CHECKSUM (at->dw_attr_val.v.val_data8);
5672 break;
5673
5674 default:
5675 break;
5676 }
5677 }
5678
5679 /* Calculate the checksum of a DIE. */
5680
5681 static void
5682 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5683 {
5684 dw_die_ref c;
5685 dw_attr_ref a;
5686 unsigned ix;
5687
5688 /* To avoid infinite recursion. */
5689 if (die->die_mark)
5690 {
5691 CHECKSUM (die->die_mark);
5692 return;
5693 }
5694 die->die_mark = ++(*mark);
5695
5696 CHECKSUM (die->die_tag);
5697
5698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5699 attr_checksum (a, ctx, mark);
5700
5701 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5702 }
5703
5704 #undef CHECKSUM
5705 #undef CHECKSUM_BLOCK
5706 #undef CHECKSUM_STRING
5707
5708 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5709 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5710 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5711 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5712 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5713 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5714 #define CHECKSUM_ATTR(FOO) \
5715 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5716
5717 /* Calculate the checksum of a number in signed LEB128 format. */
5718
5719 static void
5720 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5721 {
5722 unsigned char byte;
5723 bool more;
5724
5725 while (1)
5726 {
5727 byte = (value & 0x7f);
5728 value >>= 7;
5729 more = !((value == 0 && (byte & 0x40) == 0)
5730 || (value == -1 && (byte & 0x40) != 0));
5731 if (more)
5732 byte |= 0x80;
5733 CHECKSUM (byte);
5734 if (!more)
5735 break;
5736 }
5737 }
5738
5739 /* Calculate the checksum of a number in unsigned LEB128 format. */
5740
5741 static void
5742 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5743 {
5744 while (1)
5745 {
5746 unsigned char byte = (value & 0x7f);
5747 value >>= 7;
5748 if (value != 0)
5749 /* More bytes to follow. */
5750 byte |= 0x80;
5751 CHECKSUM (byte);
5752 if (value == 0)
5753 break;
5754 }
5755 }
5756
5757 /* Checksum the context of the DIE. This adds the names of any
5758 surrounding namespaces or structures to the checksum. */
5759
5760 static void
5761 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5762 {
5763 const char *name;
5764 dw_die_ref spec;
5765 int tag = die->die_tag;
5766
5767 if (tag != DW_TAG_namespace
5768 && tag != DW_TAG_structure_type
5769 && tag != DW_TAG_class_type)
5770 return;
5771
5772 name = get_AT_string (die, DW_AT_name);
5773
5774 spec = get_AT_ref (die, DW_AT_specification);
5775 if (spec != NULL)
5776 die = spec;
5777
5778 if (die->die_parent != NULL)
5779 checksum_die_context (die->die_parent, ctx);
5780
5781 CHECKSUM_ULEB128 ('C');
5782 CHECKSUM_ULEB128 (tag);
5783 if (name != NULL)
5784 CHECKSUM_STRING (name);
5785 }
5786
5787 /* Calculate the checksum of a location expression. */
5788
5789 static inline void
5790 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5791 {
5792 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5793 were emitted as a DW_FORM_sdata instead of a location expression. */
5794 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5795 {
5796 CHECKSUM_ULEB128 (DW_FORM_sdata);
5797 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5798 return;
5799 }
5800
5801 /* Otherwise, just checksum the raw location expression. */
5802 while (loc != NULL)
5803 {
5804 inchash::hash hstate;
5805 hashval_t hash;
5806
5807 CHECKSUM_ULEB128 (loc->dtprel);
5808 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5809 hash_loc_operands (loc, hstate);
5810 hash = hstate.end ();
5811 CHECKSUM (hash);
5812 loc = loc->dw_loc_next;
5813 }
5814 }
5815
5816 /* Calculate the checksum of an attribute. */
5817
5818 static void
5819 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5820 struct md5_ctx *ctx, int *mark)
5821 {
5822 dw_loc_descr_ref loc;
5823 rtx r;
5824
5825 if (AT_class (at) == dw_val_class_die_ref)
5826 {
5827 dw_die_ref target_die = AT_ref (at);
5828
5829 /* For pointer and reference types, we checksum only the (qualified)
5830 name of the target type (if there is a name). For friend entries,
5831 we checksum only the (qualified) name of the target type or function.
5832 This allows the checksum to remain the same whether the target type
5833 is complete or not. */
5834 if ((at->dw_attr == DW_AT_type
5835 && (tag == DW_TAG_pointer_type
5836 || tag == DW_TAG_reference_type
5837 || tag == DW_TAG_rvalue_reference_type
5838 || tag == DW_TAG_ptr_to_member_type))
5839 || (at->dw_attr == DW_AT_friend
5840 && tag == DW_TAG_friend))
5841 {
5842 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5843
5844 if (name_attr != NULL)
5845 {
5846 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5847
5848 if (decl == NULL)
5849 decl = target_die;
5850 CHECKSUM_ULEB128 ('N');
5851 CHECKSUM_ULEB128 (at->dw_attr);
5852 if (decl->die_parent != NULL)
5853 checksum_die_context (decl->die_parent, ctx);
5854 CHECKSUM_ULEB128 ('E');
5855 CHECKSUM_STRING (AT_string (name_attr));
5856 return;
5857 }
5858 }
5859
5860 /* For all other references to another DIE, we check to see if the
5861 target DIE has already been visited. If it has, we emit a
5862 backward reference; if not, we descend recursively. */
5863 if (target_die->die_mark > 0)
5864 {
5865 CHECKSUM_ULEB128 ('R');
5866 CHECKSUM_ULEB128 (at->dw_attr);
5867 CHECKSUM_ULEB128 (target_die->die_mark);
5868 }
5869 else
5870 {
5871 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5872
5873 if (decl == NULL)
5874 decl = target_die;
5875 target_die->die_mark = ++(*mark);
5876 CHECKSUM_ULEB128 ('T');
5877 CHECKSUM_ULEB128 (at->dw_attr);
5878 if (decl->die_parent != NULL)
5879 checksum_die_context (decl->die_parent, ctx);
5880 die_checksum_ordered (target_die, ctx, mark);
5881 }
5882 return;
5883 }
5884
5885 CHECKSUM_ULEB128 ('A');
5886 CHECKSUM_ULEB128 (at->dw_attr);
5887
5888 switch (AT_class (at))
5889 {
5890 case dw_val_class_const:
5891 CHECKSUM_ULEB128 (DW_FORM_sdata);
5892 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5893 break;
5894
5895 case dw_val_class_unsigned_const:
5896 CHECKSUM_ULEB128 (DW_FORM_sdata);
5897 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5898 break;
5899
5900 case dw_val_class_const_double:
5901 CHECKSUM_ULEB128 (DW_FORM_block);
5902 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5903 CHECKSUM (at->dw_attr_val.v.val_double);
5904 break;
5905
5906 case dw_val_class_wide_int:
5907 CHECKSUM_ULEB128 (DW_FORM_block);
5908 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5909 CHECKSUM (*at->dw_attr_val.v.val_wide);
5910 break;
5911
5912 case dw_val_class_vec:
5913 CHECKSUM_ULEB128 (DW_FORM_block);
5914 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5915 * at->dw_attr_val.v.val_vec.elt_size);
5916 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5917 (at->dw_attr_val.v.val_vec.length
5918 * at->dw_attr_val.v.val_vec.elt_size));
5919 break;
5920
5921 case dw_val_class_flag:
5922 CHECKSUM_ULEB128 (DW_FORM_flag);
5923 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5924 break;
5925
5926 case dw_val_class_str:
5927 CHECKSUM_ULEB128 (DW_FORM_string);
5928 CHECKSUM_STRING (AT_string (at));
5929 break;
5930
5931 case dw_val_class_addr:
5932 r = AT_addr (at);
5933 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5934 CHECKSUM_ULEB128 (DW_FORM_string);
5935 CHECKSUM_STRING (XSTR (r, 0));
5936 break;
5937
5938 case dw_val_class_offset:
5939 CHECKSUM_ULEB128 (DW_FORM_sdata);
5940 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5941 break;
5942
5943 case dw_val_class_loc:
5944 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5945 loc_checksum_ordered (loc, ctx);
5946 break;
5947
5948 case dw_val_class_fde_ref:
5949 case dw_val_class_lbl_id:
5950 case dw_val_class_lineptr:
5951 case dw_val_class_macptr:
5952 case dw_val_class_high_pc:
5953 break;
5954
5955 case dw_val_class_file:
5956 CHECKSUM_ULEB128 (DW_FORM_string);
5957 CHECKSUM_STRING (AT_file (at)->filename);
5958 break;
5959
5960 case dw_val_class_data8:
5961 CHECKSUM (at->dw_attr_val.v.val_data8);
5962 break;
5963
5964 default:
5965 break;
5966 }
5967 }
5968
5969 struct checksum_attributes
5970 {
5971 dw_attr_ref at_name;
5972 dw_attr_ref at_type;
5973 dw_attr_ref at_friend;
5974 dw_attr_ref at_accessibility;
5975 dw_attr_ref at_address_class;
5976 dw_attr_ref at_allocated;
5977 dw_attr_ref at_artificial;
5978 dw_attr_ref at_associated;
5979 dw_attr_ref at_binary_scale;
5980 dw_attr_ref at_bit_offset;
5981 dw_attr_ref at_bit_size;
5982 dw_attr_ref at_bit_stride;
5983 dw_attr_ref at_byte_size;
5984 dw_attr_ref at_byte_stride;
5985 dw_attr_ref at_const_value;
5986 dw_attr_ref at_containing_type;
5987 dw_attr_ref at_count;
5988 dw_attr_ref at_data_location;
5989 dw_attr_ref at_data_member_location;
5990 dw_attr_ref at_decimal_scale;
5991 dw_attr_ref at_decimal_sign;
5992 dw_attr_ref at_default_value;
5993 dw_attr_ref at_digit_count;
5994 dw_attr_ref at_discr;
5995 dw_attr_ref at_discr_list;
5996 dw_attr_ref at_discr_value;
5997 dw_attr_ref at_encoding;
5998 dw_attr_ref at_endianity;
5999 dw_attr_ref at_explicit;
6000 dw_attr_ref at_is_optional;
6001 dw_attr_ref at_location;
6002 dw_attr_ref at_lower_bound;
6003 dw_attr_ref at_mutable;
6004 dw_attr_ref at_ordering;
6005 dw_attr_ref at_picture_string;
6006 dw_attr_ref at_prototyped;
6007 dw_attr_ref at_small;
6008 dw_attr_ref at_segment;
6009 dw_attr_ref at_string_length;
6010 dw_attr_ref at_threads_scaled;
6011 dw_attr_ref at_upper_bound;
6012 dw_attr_ref at_use_location;
6013 dw_attr_ref at_use_UTF8;
6014 dw_attr_ref at_variable_parameter;
6015 dw_attr_ref at_virtuality;
6016 dw_attr_ref at_visibility;
6017 dw_attr_ref at_vtable_elem_location;
6018 };
6019
6020 /* Collect the attributes that we will want to use for the checksum. */
6021
6022 static void
6023 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6024 {
6025 dw_attr_ref a;
6026 unsigned ix;
6027
6028 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6029 {
6030 switch (a->dw_attr)
6031 {
6032 case DW_AT_name:
6033 attrs->at_name = a;
6034 break;
6035 case DW_AT_type:
6036 attrs->at_type = a;
6037 break;
6038 case DW_AT_friend:
6039 attrs->at_friend = a;
6040 break;
6041 case DW_AT_accessibility:
6042 attrs->at_accessibility = a;
6043 break;
6044 case DW_AT_address_class:
6045 attrs->at_address_class = a;
6046 break;
6047 case DW_AT_allocated:
6048 attrs->at_allocated = a;
6049 break;
6050 case DW_AT_artificial:
6051 attrs->at_artificial = a;
6052 break;
6053 case DW_AT_associated:
6054 attrs->at_associated = a;
6055 break;
6056 case DW_AT_binary_scale:
6057 attrs->at_binary_scale = a;
6058 break;
6059 case DW_AT_bit_offset:
6060 attrs->at_bit_offset = a;
6061 break;
6062 case DW_AT_bit_size:
6063 attrs->at_bit_size = a;
6064 break;
6065 case DW_AT_bit_stride:
6066 attrs->at_bit_stride = a;
6067 break;
6068 case DW_AT_byte_size:
6069 attrs->at_byte_size = a;
6070 break;
6071 case DW_AT_byte_stride:
6072 attrs->at_byte_stride = a;
6073 break;
6074 case DW_AT_const_value:
6075 attrs->at_const_value = a;
6076 break;
6077 case DW_AT_containing_type:
6078 attrs->at_containing_type = a;
6079 break;
6080 case DW_AT_count:
6081 attrs->at_count = a;
6082 break;
6083 case DW_AT_data_location:
6084 attrs->at_data_location = a;
6085 break;
6086 case DW_AT_data_member_location:
6087 attrs->at_data_member_location = a;
6088 break;
6089 case DW_AT_decimal_scale:
6090 attrs->at_decimal_scale = a;
6091 break;
6092 case DW_AT_decimal_sign:
6093 attrs->at_decimal_sign = a;
6094 break;
6095 case DW_AT_default_value:
6096 attrs->at_default_value = a;
6097 break;
6098 case DW_AT_digit_count:
6099 attrs->at_digit_count = a;
6100 break;
6101 case DW_AT_discr:
6102 attrs->at_discr = a;
6103 break;
6104 case DW_AT_discr_list:
6105 attrs->at_discr_list = a;
6106 break;
6107 case DW_AT_discr_value:
6108 attrs->at_discr_value = a;
6109 break;
6110 case DW_AT_encoding:
6111 attrs->at_encoding = a;
6112 break;
6113 case DW_AT_endianity:
6114 attrs->at_endianity = a;
6115 break;
6116 case DW_AT_explicit:
6117 attrs->at_explicit = a;
6118 break;
6119 case DW_AT_is_optional:
6120 attrs->at_is_optional = a;
6121 break;
6122 case DW_AT_location:
6123 attrs->at_location = a;
6124 break;
6125 case DW_AT_lower_bound:
6126 attrs->at_lower_bound = a;
6127 break;
6128 case DW_AT_mutable:
6129 attrs->at_mutable = a;
6130 break;
6131 case DW_AT_ordering:
6132 attrs->at_ordering = a;
6133 break;
6134 case DW_AT_picture_string:
6135 attrs->at_picture_string = a;
6136 break;
6137 case DW_AT_prototyped:
6138 attrs->at_prototyped = a;
6139 break;
6140 case DW_AT_small:
6141 attrs->at_small = a;
6142 break;
6143 case DW_AT_segment:
6144 attrs->at_segment = a;
6145 break;
6146 case DW_AT_string_length:
6147 attrs->at_string_length = a;
6148 break;
6149 case DW_AT_threads_scaled:
6150 attrs->at_threads_scaled = a;
6151 break;
6152 case DW_AT_upper_bound:
6153 attrs->at_upper_bound = a;
6154 break;
6155 case DW_AT_use_location:
6156 attrs->at_use_location = a;
6157 break;
6158 case DW_AT_use_UTF8:
6159 attrs->at_use_UTF8 = a;
6160 break;
6161 case DW_AT_variable_parameter:
6162 attrs->at_variable_parameter = a;
6163 break;
6164 case DW_AT_virtuality:
6165 attrs->at_virtuality = a;
6166 break;
6167 case DW_AT_visibility:
6168 attrs->at_visibility = a;
6169 break;
6170 case DW_AT_vtable_elem_location:
6171 attrs->at_vtable_elem_location = a;
6172 break;
6173 default:
6174 break;
6175 }
6176 }
6177 }
6178
6179 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6180
6181 static void
6182 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6183 {
6184 dw_die_ref c;
6185 dw_die_ref decl;
6186 struct checksum_attributes attrs;
6187
6188 CHECKSUM_ULEB128 ('D');
6189 CHECKSUM_ULEB128 (die->die_tag);
6190
6191 memset (&attrs, 0, sizeof (attrs));
6192
6193 decl = get_AT_ref (die, DW_AT_specification);
6194 if (decl != NULL)
6195 collect_checksum_attributes (&attrs, decl);
6196 collect_checksum_attributes (&attrs, die);
6197
6198 CHECKSUM_ATTR (attrs.at_name);
6199 CHECKSUM_ATTR (attrs.at_accessibility);
6200 CHECKSUM_ATTR (attrs.at_address_class);
6201 CHECKSUM_ATTR (attrs.at_allocated);
6202 CHECKSUM_ATTR (attrs.at_artificial);
6203 CHECKSUM_ATTR (attrs.at_associated);
6204 CHECKSUM_ATTR (attrs.at_binary_scale);
6205 CHECKSUM_ATTR (attrs.at_bit_offset);
6206 CHECKSUM_ATTR (attrs.at_bit_size);
6207 CHECKSUM_ATTR (attrs.at_bit_stride);
6208 CHECKSUM_ATTR (attrs.at_byte_size);
6209 CHECKSUM_ATTR (attrs.at_byte_stride);
6210 CHECKSUM_ATTR (attrs.at_const_value);
6211 CHECKSUM_ATTR (attrs.at_containing_type);
6212 CHECKSUM_ATTR (attrs.at_count);
6213 CHECKSUM_ATTR (attrs.at_data_location);
6214 CHECKSUM_ATTR (attrs.at_data_member_location);
6215 CHECKSUM_ATTR (attrs.at_decimal_scale);
6216 CHECKSUM_ATTR (attrs.at_decimal_sign);
6217 CHECKSUM_ATTR (attrs.at_default_value);
6218 CHECKSUM_ATTR (attrs.at_digit_count);
6219 CHECKSUM_ATTR (attrs.at_discr);
6220 CHECKSUM_ATTR (attrs.at_discr_list);
6221 CHECKSUM_ATTR (attrs.at_discr_value);
6222 CHECKSUM_ATTR (attrs.at_encoding);
6223 CHECKSUM_ATTR (attrs.at_endianity);
6224 CHECKSUM_ATTR (attrs.at_explicit);
6225 CHECKSUM_ATTR (attrs.at_is_optional);
6226 CHECKSUM_ATTR (attrs.at_location);
6227 CHECKSUM_ATTR (attrs.at_lower_bound);
6228 CHECKSUM_ATTR (attrs.at_mutable);
6229 CHECKSUM_ATTR (attrs.at_ordering);
6230 CHECKSUM_ATTR (attrs.at_picture_string);
6231 CHECKSUM_ATTR (attrs.at_prototyped);
6232 CHECKSUM_ATTR (attrs.at_small);
6233 CHECKSUM_ATTR (attrs.at_segment);
6234 CHECKSUM_ATTR (attrs.at_string_length);
6235 CHECKSUM_ATTR (attrs.at_threads_scaled);
6236 CHECKSUM_ATTR (attrs.at_upper_bound);
6237 CHECKSUM_ATTR (attrs.at_use_location);
6238 CHECKSUM_ATTR (attrs.at_use_UTF8);
6239 CHECKSUM_ATTR (attrs.at_variable_parameter);
6240 CHECKSUM_ATTR (attrs.at_virtuality);
6241 CHECKSUM_ATTR (attrs.at_visibility);
6242 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6243 CHECKSUM_ATTR (attrs.at_type);
6244 CHECKSUM_ATTR (attrs.at_friend);
6245
6246 /* Checksum the child DIEs. */
6247 c = die->die_child;
6248 if (c) do {
6249 dw_attr_ref name_attr;
6250
6251 c = c->die_sib;
6252 name_attr = get_AT (c, DW_AT_name);
6253 if (is_template_instantiation (c))
6254 {
6255 /* Ignore instantiations of member type and function templates. */
6256 }
6257 else if (name_attr != NULL
6258 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6259 {
6260 /* Use a shallow checksum for named nested types and member
6261 functions. */
6262 CHECKSUM_ULEB128 ('S');
6263 CHECKSUM_ULEB128 (c->die_tag);
6264 CHECKSUM_STRING (AT_string (name_attr));
6265 }
6266 else
6267 {
6268 /* Use a deep checksum for other children. */
6269 /* Mark this DIE so it gets processed when unmarking. */
6270 if (c->die_mark == 0)
6271 c->die_mark = -1;
6272 die_checksum_ordered (c, ctx, mark);
6273 }
6274 } while (c != die->die_child);
6275
6276 CHECKSUM_ULEB128 (0);
6277 }
6278
6279 /* Add a type name and tag to a hash. */
6280 static void
6281 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6282 {
6283 CHECKSUM_ULEB128 (tag);
6284 CHECKSUM_STRING (name);
6285 }
6286
6287 #undef CHECKSUM
6288 #undef CHECKSUM_STRING
6289 #undef CHECKSUM_ATTR
6290 #undef CHECKSUM_LEB128
6291 #undef CHECKSUM_ULEB128
6292
6293 /* Generate the type signature for DIE. This is computed by generating an
6294 MD5 checksum over the DIE's tag, its relevant attributes, and its
6295 children. Attributes that are references to other DIEs are processed
6296 by recursion, using the MARK field to prevent infinite recursion.
6297 If the DIE is nested inside a namespace or another type, we also
6298 need to include that context in the signature. The lower 64 bits
6299 of the resulting MD5 checksum comprise the signature. */
6300
6301 static void
6302 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6303 {
6304 int mark;
6305 const char *name;
6306 unsigned char checksum[16];
6307 struct md5_ctx ctx;
6308 dw_die_ref decl;
6309 dw_die_ref parent;
6310
6311 name = get_AT_string (die, DW_AT_name);
6312 decl = get_AT_ref (die, DW_AT_specification);
6313 parent = get_die_parent (die);
6314
6315 /* First, compute a signature for just the type name (and its surrounding
6316 context, if any. This is stored in the type unit DIE for link-time
6317 ODR (one-definition rule) checking. */
6318
6319 if (is_cxx () && name != NULL)
6320 {
6321 md5_init_ctx (&ctx);
6322
6323 /* Checksum the names of surrounding namespaces and structures. */
6324 if (parent != NULL)
6325 checksum_die_context (parent, &ctx);
6326
6327 /* Checksum the current DIE. */
6328 die_odr_checksum (die->die_tag, name, &ctx);
6329 md5_finish_ctx (&ctx, checksum);
6330
6331 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6332 }
6333
6334 /* Next, compute the complete type signature. */
6335
6336 md5_init_ctx (&ctx);
6337 mark = 1;
6338 die->die_mark = mark;
6339
6340 /* Checksum the names of surrounding namespaces and structures. */
6341 if (parent != NULL)
6342 checksum_die_context (parent, &ctx);
6343
6344 /* Checksum the DIE and its children. */
6345 die_checksum_ordered (die, &ctx, &mark);
6346 unmark_all_dies (die);
6347 md5_finish_ctx (&ctx, checksum);
6348
6349 /* Store the signature in the type node and link the type DIE and the
6350 type node together. */
6351 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6352 DWARF_TYPE_SIGNATURE_SIZE);
6353 die->comdat_type_p = true;
6354 die->die_id.die_type_node = type_node;
6355 type_node->type_die = die;
6356
6357 /* If the DIE is a specification, link its declaration to the type node
6358 as well. */
6359 if (decl != NULL)
6360 {
6361 decl->comdat_type_p = true;
6362 decl->die_id.die_type_node = type_node;
6363 }
6364 }
6365
6366 /* Do the location expressions look same? */
6367 static inline int
6368 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6369 {
6370 return loc1->dw_loc_opc == loc2->dw_loc_opc
6371 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6372 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6373 }
6374
6375 /* Do the values look the same? */
6376 static int
6377 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6378 {
6379 dw_loc_descr_ref loc1, loc2;
6380 rtx r1, r2;
6381
6382 if (v1->val_class != v2->val_class)
6383 return 0;
6384
6385 switch (v1->val_class)
6386 {
6387 case dw_val_class_const:
6388 return v1->v.val_int == v2->v.val_int;
6389 case dw_val_class_unsigned_const:
6390 return v1->v.val_unsigned == v2->v.val_unsigned;
6391 case dw_val_class_const_double:
6392 return v1->v.val_double.high == v2->v.val_double.high
6393 && v1->v.val_double.low == v2->v.val_double.low;
6394 case dw_val_class_wide_int:
6395 return *v1->v.val_wide == *v2->v.val_wide;
6396 case dw_val_class_vec:
6397 if (v1->v.val_vec.length != v2->v.val_vec.length
6398 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6399 return 0;
6400 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6401 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6402 return 0;
6403 return 1;
6404 case dw_val_class_flag:
6405 return v1->v.val_flag == v2->v.val_flag;
6406 case dw_val_class_str:
6407 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6408
6409 case dw_val_class_addr:
6410 r1 = v1->v.val_addr;
6411 r2 = v2->v.val_addr;
6412 if (GET_CODE (r1) != GET_CODE (r2))
6413 return 0;
6414 return !rtx_equal_p (r1, r2);
6415
6416 case dw_val_class_offset:
6417 return v1->v.val_offset == v2->v.val_offset;
6418
6419 case dw_val_class_loc:
6420 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6421 loc1 && loc2;
6422 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6423 if (!same_loc_p (loc1, loc2, mark))
6424 return 0;
6425 return !loc1 && !loc2;
6426
6427 case dw_val_class_die_ref:
6428 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6429
6430 case dw_val_class_fde_ref:
6431 case dw_val_class_vms_delta:
6432 case dw_val_class_lbl_id:
6433 case dw_val_class_lineptr:
6434 case dw_val_class_macptr:
6435 case dw_val_class_high_pc:
6436 return 1;
6437
6438 case dw_val_class_file:
6439 return v1->v.val_file == v2->v.val_file;
6440
6441 case dw_val_class_data8:
6442 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6443
6444 default:
6445 return 1;
6446 }
6447 }
6448
6449 /* Do the attributes look the same? */
6450
6451 static int
6452 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6453 {
6454 if (at1->dw_attr != at2->dw_attr)
6455 return 0;
6456
6457 /* We don't care that this was compiled with a different compiler
6458 snapshot; if the output is the same, that's what matters. */
6459 if (at1->dw_attr == DW_AT_producer)
6460 return 1;
6461
6462 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6463 }
6464
6465 /* Do the dies look the same? */
6466
6467 static int
6468 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6469 {
6470 dw_die_ref c1, c2;
6471 dw_attr_ref a1;
6472 unsigned ix;
6473
6474 /* To avoid infinite recursion. */
6475 if (die1->die_mark)
6476 return die1->die_mark == die2->die_mark;
6477 die1->die_mark = die2->die_mark = ++(*mark);
6478
6479 if (die1->die_tag != die2->die_tag)
6480 return 0;
6481
6482 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6483 return 0;
6484
6485 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6486 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6487 return 0;
6488
6489 c1 = die1->die_child;
6490 c2 = die2->die_child;
6491 if (! c1)
6492 {
6493 if (c2)
6494 return 0;
6495 }
6496 else
6497 for (;;)
6498 {
6499 if (!same_die_p (c1, c2, mark))
6500 return 0;
6501 c1 = c1->die_sib;
6502 c2 = c2->die_sib;
6503 if (c1 == die1->die_child)
6504 {
6505 if (c2 == die2->die_child)
6506 break;
6507 else
6508 return 0;
6509 }
6510 }
6511
6512 return 1;
6513 }
6514
6515 /* Do the dies look the same? Wrapper around same_die_p. */
6516
6517 static int
6518 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6519 {
6520 int mark = 0;
6521 int ret = same_die_p (die1, die2, &mark);
6522
6523 unmark_all_dies (die1);
6524 unmark_all_dies (die2);
6525
6526 return ret;
6527 }
6528
6529 /* The prefix to attach to symbols on DIEs in the current comdat debug
6530 info section. */
6531 static const char *comdat_symbol_id;
6532
6533 /* The index of the current symbol within the current comdat CU. */
6534 static unsigned int comdat_symbol_number;
6535
6536 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6537 children, and set comdat_symbol_id accordingly. */
6538
6539 static void
6540 compute_section_prefix (dw_die_ref unit_die)
6541 {
6542 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6543 const char *base = die_name ? lbasename (die_name) : "anonymous";
6544 char *name = XALLOCAVEC (char, strlen (base) + 64);
6545 char *p;
6546 int i, mark;
6547 unsigned char checksum[16];
6548 struct md5_ctx ctx;
6549
6550 /* Compute the checksum of the DIE, then append part of it as hex digits to
6551 the name filename of the unit. */
6552
6553 md5_init_ctx (&ctx);
6554 mark = 0;
6555 die_checksum (unit_die, &ctx, &mark);
6556 unmark_all_dies (unit_die);
6557 md5_finish_ctx (&ctx, checksum);
6558
6559 sprintf (name, "%s.", base);
6560 clean_symbol_name (name);
6561
6562 p = name + strlen (name);
6563 for (i = 0; i < 4; i++)
6564 {
6565 sprintf (p, "%.2x", checksum[i]);
6566 p += 2;
6567 }
6568
6569 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6570 comdat_symbol_number = 0;
6571 }
6572
6573 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6574
6575 static int
6576 is_type_die (dw_die_ref die)
6577 {
6578 switch (die->die_tag)
6579 {
6580 case DW_TAG_array_type:
6581 case DW_TAG_class_type:
6582 case DW_TAG_interface_type:
6583 case DW_TAG_enumeration_type:
6584 case DW_TAG_pointer_type:
6585 case DW_TAG_reference_type:
6586 case DW_TAG_rvalue_reference_type:
6587 case DW_TAG_string_type:
6588 case DW_TAG_structure_type:
6589 case DW_TAG_subroutine_type:
6590 case DW_TAG_union_type:
6591 case DW_TAG_ptr_to_member_type:
6592 case DW_TAG_set_type:
6593 case DW_TAG_subrange_type:
6594 case DW_TAG_base_type:
6595 case DW_TAG_const_type:
6596 case DW_TAG_file_type:
6597 case DW_TAG_packed_type:
6598 case DW_TAG_volatile_type:
6599 case DW_TAG_typedef:
6600 return 1;
6601 default:
6602 return 0;
6603 }
6604 }
6605
6606 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6607 Basically, we want to choose the bits that are likely to be shared between
6608 compilations (types) and leave out the bits that are specific to individual
6609 compilations (functions). */
6610
6611 static int
6612 is_comdat_die (dw_die_ref c)
6613 {
6614 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6615 we do for stabs. The advantage is a greater likelihood of sharing between
6616 objects that don't include headers in the same order (and therefore would
6617 put the base types in a different comdat). jason 8/28/00 */
6618
6619 if (c->die_tag == DW_TAG_base_type)
6620 return 0;
6621
6622 if (c->die_tag == DW_TAG_pointer_type
6623 || c->die_tag == DW_TAG_reference_type
6624 || c->die_tag == DW_TAG_rvalue_reference_type
6625 || c->die_tag == DW_TAG_const_type
6626 || c->die_tag == DW_TAG_volatile_type)
6627 {
6628 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6629
6630 return t ? is_comdat_die (t) : 0;
6631 }
6632
6633 return is_type_die (c);
6634 }
6635
6636 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6637 compilation unit. */
6638
6639 static int
6640 is_symbol_die (dw_die_ref c)
6641 {
6642 return (is_type_die (c)
6643 || is_declaration_die (c)
6644 || c->die_tag == DW_TAG_namespace
6645 || c->die_tag == DW_TAG_module);
6646 }
6647
6648 /* Returns true iff C is a compile-unit DIE. */
6649
6650 static inline bool
6651 is_cu_die (dw_die_ref c)
6652 {
6653 return c && c->die_tag == DW_TAG_compile_unit;
6654 }
6655
6656 /* Returns true iff C is a unit DIE of some sort. */
6657
6658 static inline bool
6659 is_unit_die (dw_die_ref c)
6660 {
6661 return c && (c->die_tag == DW_TAG_compile_unit
6662 || c->die_tag == DW_TAG_partial_unit
6663 || c->die_tag == DW_TAG_type_unit);
6664 }
6665
6666 /* Returns true iff C is a namespace DIE. */
6667
6668 static inline bool
6669 is_namespace_die (dw_die_ref c)
6670 {
6671 return c && c->die_tag == DW_TAG_namespace;
6672 }
6673
6674 /* Returns true iff C is a class or structure DIE. */
6675
6676 static inline bool
6677 is_class_die (dw_die_ref c)
6678 {
6679 return c && (c->die_tag == DW_TAG_class_type
6680 || c->die_tag == DW_TAG_structure_type);
6681 }
6682
6683 /* Return non-zero if this DIE is a template parameter. */
6684
6685 static inline bool
6686 is_template_parameter (dw_die_ref die)
6687 {
6688 switch (die->die_tag)
6689 {
6690 case DW_TAG_template_type_param:
6691 case DW_TAG_template_value_param:
6692 case DW_TAG_GNU_template_template_param:
6693 case DW_TAG_GNU_template_parameter_pack:
6694 return true;
6695 default:
6696 return false;
6697 }
6698 }
6699
6700 /* Return non-zero if this DIE represents a template instantiation. */
6701
6702 static inline bool
6703 is_template_instantiation (dw_die_ref die)
6704 {
6705 dw_die_ref c;
6706
6707 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6708 return false;
6709 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6710 return false;
6711 }
6712
6713 static char *
6714 gen_internal_sym (const char *prefix)
6715 {
6716 char buf[256];
6717
6718 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6719 return xstrdup (buf);
6720 }
6721
6722 /* Assign symbols to all worthy DIEs under DIE. */
6723
6724 static void
6725 assign_symbol_names (dw_die_ref die)
6726 {
6727 dw_die_ref c;
6728
6729 if (is_symbol_die (die) && !die->comdat_type_p)
6730 {
6731 if (comdat_symbol_id)
6732 {
6733 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6734
6735 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6736 comdat_symbol_id, comdat_symbol_number++);
6737 die->die_id.die_symbol = xstrdup (p);
6738 }
6739 else
6740 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6741 }
6742
6743 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6744 }
6745
6746 struct cu_hash_table_entry
6747 {
6748 dw_die_ref cu;
6749 unsigned min_comdat_num, max_comdat_num;
6750 struct cu_hash_table_entry *next;
6751 };
6752
6753 /* Helpers to manipulate hash table of CUs. */
6754
6755 struct cu_hash_table_entry_hasher
6756 {
6757 typedef cu_hash_table_entry value_type;
6758 typedef die_struct compare_type;
6759 static inline hashval_t hash (const value_type *);
6760 static inline bool equal (const value_type *, const compare_type *);
6761 static inline void remove (value_type *);
6762 };
6763
6764 inline hashval_t
6765 cu_hash_table_entry_hasher::hash (const value_type *entry)
6766 {
6767 return htab_hash_string (entry->cu->die_id.die_symbol);
6768 }
6769
6770 inline bool
6771 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6772 const compare_type *entry2)
6773 {
6774 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6775 }
6776
6777 inline void
6778 cu_hash_table_entry_hasher::remove (value_type *entry)
6779 {
6780 struct cu_hash_table_entry *next;
6781
6782 while (entry)
6783 {
6784 next = entry->next;
6785 free (entry);
6786 entry = next;
6787 }
6788 }
6789
6790 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6791
6792 /* Check whether we have already seen this CU and set up SYM_NUM
6793 accordingly. */
6794 static int
6795 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6796 {
6797 struct cu_hash_table_entry dummy;
6798 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6799
6800 dummy.max_comdat_num = 0;
6801
6802 slot = htable->find_slot_with_hash (cu,
6803 htab_hash_string (cu->die_id.die_symbol),
6804 INSERT);
6805 entry = *slot;
6806
6807 for (; entry; last = entry, entry = entry->next)
6808 {
6809 if (same_die_p_wrap (cu, entry->cu))
6810 break;
6811 }
6812
6813 if (entry)
6814 {
6815 *sym_num = entry->min_comdat_num;
6816 return 1;
6817 }
6818
6819 entry = XCNEW (struct cu_hash_table_entry);
6820 entry->cu = cu;
6821 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6822 entry->next = *slot;
6823 *slot = entry;
6824
6825 return 0;
6826 }
6827
6828 /* Record SYM_NUM to record of CU in HTABLE. */
6829 static void
6830 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6831 unsigned int sym_num)
6832 {
6833 struct cu_hash_table_entry **slot, *entry;
6834
6835 slot = htable->find_slot_with_hash (cu,
6836 htab_hash_string (cu->die_id.die_symbol),
6837 NO_INSERT);
6838 entry = *slot;
6839
6840 entry->max_comdat_num = sym_num;
6841 }
6842
6843 /* Traverse the DIE (which is always comp_unit_die), and set up
6844 additional compilation units for each of the include files we see
6845 bracketed by BINCL/EINCL. */
6846
6847 static void
6848 break_out_includes (dw_die_ref die)
6849 {
6850 dw_die_ref c;
6851 dw_die_ref unit = NULL;
6852 limbo_die_node *node, **pnode;
6853
6854 c = die->die_child;
6855 if (c) do {
6856 dw_die_ref prev = c;
6857 c = c->die_sib;
6858 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6859 || (unit && is_comdat_die (c)))
6860 {
6861 dw_die_ref next = c->die_sib;
6862
6863 /* This DIE is for a secondary CU; remove it from the main one. */
6864 remove_child_with_prev (c, prev);
6865
6866 if (c->die_tag == DW_TAG_GNU_BINCL)
6867 unit = push_new_compile_unit (unit, c);
6868 else if (c->die_tag == DW_TAG_GNU_EINCL)
6869 unit = pop_compile_unit (unit);
6870 else
6871 add_child_die (unit, c);
6872 c = next;
6873 if (c == die->die_child)
6874 break;
6875 }
6876 } while (c != die->die_child);
6877
6878 #if 0
6879 /* We can only use this in debugging, since the frontend doesn't check
6880 to make sure that we leave every include file we enter. */
6881 gcc_assert (!unit);
6882 #endif
6883
6884 assign_symbol_names (die);
6885 cu_hash_type cu_hash_table (10);
6886 for (node = limbo_die_list, pnode = &limbo_die_list;
6887 node;
6888 node = node->next)
6889 {
6890 int is_dupl;
6891
6892 compute_section_prefix (node->die);
6893 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6894 &comdat_symbol_number);
6895 assign_symbol_names (node->die);
6896 if (is_dupl)
6897 *pnode = node->next;
6898 else
6899 {
6900 pnode = &node->next;
6901 record_comdat_symbol_number (node->die, &cu_hash_table,
6902 comdat_symbol_number);
6903 }
6904 }
6905 }
6906
6907 /* Return non-zero if this DIE is a declaration. */
6908
6909 static int
6910 is_declaration_die (dw_die_ref die)
6911 {
6912 dw_attr_ref a;
6913 unsigned ix;
6914
6915 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6916 if (a->dw_attr == DW_AT_declaration)
6917 return 1;
6918
6919 return 0;
6920 }
6921
6922 /* Return non-zero if this DIE is nested inside a subprogram. */
6923
6924 static int
6925 is_nested_in_subprogram (dw_die_ref die)
6926 {
6927 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6928
6929 if (decl == NULL)
6930 decl = die;
6931 return local_scope_p (decl);
6932 }
6933
6934 /* Return non-zero if this DIE contains a defining declaration of a
6935 subprogram. */
6936
6937 static int
6938 contains_subprogram_definition (dw_die_ref die)
6939 {
6940 dw_die_ref c;
6941
6942 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6943 return 1;
6944 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6945 return 0;
6946 }
6947
6948 /* Return non-zero if this is a type DIE that should be moved to a
6949 COMDAT .debug_types section. */
6950
6951 static int
6952 should_move_die_to_comdat (dw_die_ref die)
6953 {
6954 switch (die->die_tag)
6955 {
6956 case DW_TAG_class_type:
6957 case DW_TAG_structure_type:
6958 case DW_TAG_enumeration_type:
6959 case DW_TAG_union_type:
6960 /* Don't move declarations, inlined instances, types nested in a
6961 subprogram, or types that contain subprogram definitions. */
6962 if (is_declaration_die (die)
6963 || get_AT (die, DW_AT_abstract_origin)
6964 || is_nested_in_subprogram (die)
6965 || contains_subprogram_definition (die))
6966 return 0;
6967 return 1;
6968 case DW_TAG_array_type:
6969 case DW_TAG_interface_type:
6970 case DW_TAG_pointer_type:
6971 case DW_TAG_reference_type:
6972 case DW_TAG_rvalue_reference_type:
6973 case DW_TAG_string_type:
6974 case DW_TAG_subroutine_type:
6975 case DW_TAG_ptr_to_member_type:
6976 case DW_TAG_set_type:
6977 case DW_TAG_subrange_type:
6978 case DW_TAG_base_type:
6979 case DW_TAG_const_type:
6980 case DW_TAG_file_type:
6981 case DW_TAG_packed_type:
6982 case DW_TAG_volatile_type:
6983 case DW_TAG_typedef:
6984 default:
6985 return 0;
6986 }
6987 }
6988
6989 /* Make a clone of DIE. */
6990
6991 static dw_die_ref
6992 clone_die (dw_die_ref die)
6993 {
6994 dw_die_ref clone;
6995 dw_attr_ref a;
6996 unsigned ix;
6997
6998 clone = ggc_cleared_alloc<die_node> ();
6999 clone->die_tag = die->die_tag;
7000
7001 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7002 add_dwarf_attr (clone, a);
7003
7004 return clone;
7005 }
7006
7007 /* Make a clone of the tree rooted at DIE. */
7008
7009 static dw_die_ref
7010 clone_tree (dw_die_ref die)
7011 {
7012 dw_die_ref c;
7013 dw_die_ref clone = clone_die (die);
7014
7015 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7016
7017 return clone;
7018 }
7019
7020 /* Make a clone of DIE as a declaration. */
7021
7022 static dw_die_ref
7023 clone_as_declaration (dw_die_ref die)
7024 {
7025 dw_die_ref clone;
7026 dw_die_ref decl;
7027 dw_attr_ref a;
7028 unsigned ix;
7029
7030 /* If the DIE is already a declaration, just clone it. */
7031 if (is_declaration_die (die))
7032 return clone_die (die);
7033
7034 /* If the DIE is a specification, just clone its declaration DIE. */
7035 decl = get_AT_ref (die, DW_AT_specification);
7036 if (decl != NULL)
7037 {
7038 clone = clone_die (decl);
7039 if (die->comdat_type_p)
7040 add_AT_die_ref (clone, DW_AT_signature, die);
7041 return clone;
7042 }
7043
7044 clone = ggc_cleared_alloc<die_node> ();
7045 clone->die_tag = die->die_tag;
7046
7047 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7048 {
7049 /* We don't want to copy over all attributes.
7050 For example we don't want DW_AT_byte_size because otherwise we will no
7051 longer have a declaration and GDB will treat it as a definition. */
7052
7053 switch (a->dw_attr)
7054 {
7055 case DW_AT_abstract_origin:
7056 case DW_AT_artificial:
7057 case DW_AT_containing_type:
7058 case DW_AT_external:
7059 case DW_AT_name:
7060 case DW_AT_type:
7061 case DW_AT_virtuality:
7062 case DW_AT_linkage_name:
7063 case DW_AT_MIPS_linkage_name:
7064 add_dwarf_attr (clone, a);
7065 break;
7066 case DW_AT_byte_size:
7067 default:
7068 break;
7069 }
7070 }
7071
7072 if (die->comdat_type_p)
7073 add_AT_die_ref (clone, DW_AT_signature, die);
7074
7075 add_AT_flag (clone, DW_AT_declaration, 1);
7076 return clone;
7077 }
7078
7079
7080 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7081
7082 struct decl_table_entry
7083 {
7084 dw_die_ref orig;
7085 dw_die_ref copy;
7086 };
7087
7088 /* Helpers to manipulate hash table of copied declarations. */
7089
7090 /* Hashtable helpers. */
7091
7092 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7093 {
7094 typedef decl_table_entry value_type;
7095 typedef die_struct compare_type;
7096 static inline hashval_t hash (const value_type *);
7097 static inline bool equal (const value_type *, const compare_type *);
7098 };
7099
7100 inline hashval_t
7101 decl_table_entry_hasher::hash (const value_type *entry)
7102 {
7103 return htab_hash_pointer (entry->orig);
7104 }
7105
7106 inline bool
7107 decl_table_entry_hasher::equal (const value_type *entry1,
7108 const compare_type *entry2)
7109 {
7110 return entry1->orig == entry2;
7111 }
7112
7113 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7114
7115 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7116 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7117 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7118 to check if the ancestor has already been copied into UNIT. */
7119
7120 static dw_die_ref
7121 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7122 decl_hash_type *decl_table)
7123 {
7124 dw_die_ref parent = die->die_parent;
7125 dw_die_ref new_parent = unit;
7126 dw_die_ref copy;
7127 decl_table_entry **slot = NULL;
7128 struct decl_table_entry *entry = NULL;
7129
7130 if (decl_table)
7131 {
7132 /* Check if the entry has already been copied to UNIT. */
7133 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7134 INSERT);
7135 if (*slot != HTAB_EMPTY_ENTRY)
7136 {
7137 entry = *slot;
7138 return entry->copy;
7139 }
7140
7141 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7142 entry = XCNEW (struct decl_table_entry);
7143 entry->orig = die;
7144 entry->copy = NULL;
7145 *slot = entry;
7146 }
7147
7148 if (parent != NULL)
7149 {
7150 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7151 if (spec != NULL)
7152 parent = spec;
7153 if (!is_unit_die (parent))
7154 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7155 }
7156
7157 copy = clone_as_declaration (die);
7158 add_child_die (new_parent, copy);
7159
7160 if (decl_table)
7161 {
7162 /* Record the pointer to the copy. */
7163 entry->copy = copy;
7164 }
7165
7166 return copy;
7167 }
7168 /* Copy the declaration context to the new type unit DIE. This includes
7169 any surrounding namespace or type declarations. If the DIE has an
7170 AT_specification attribute, it also includes attributes and children
7171 attached to the specification, and returns a pointer to the original
7172 parent of the declaration DIE. Returns NULL otherwise. */
7173
7174 static dw_die_ref
7175 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7176 {
7177 dw_die_ref decl;
7178 dw_die_ref new_decl;
7179 dw_die_ref orig_parent = NULL;
7180
7181 decl = get_AT_ref (die, DW_AT_specification);
7182 if (decl == NULL)
7183 decl = die;
7184 else
7185 {
7186 unsigned ix;
7187 dw_die_ref c;
7188 dw_attr_ref a;
7189
7190 /* The original DIE will be changed to a declaration, and must
7191 be moved to be a child of the original declaration DIE. */
7192 orig_parent = decl->die_parent;
7193
7194 /* Copy the type node pointer from the new DIE to the original
7195 declaration DIE so we can forward references later. */
7196 decl->comdat_type_p = true;
7197 decl->die_id.die_type_node = die->die_id.die_type_node;
7198
7199 remove_AT (die, DW_AT_specification);
7200
7201 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7202 {
7203 if (a->dw_attr != DW_AT_name
7204 && a->dw_attr != DW_AT_declaration
7205 && a->dw_attr != DW_AT_external)
7206 add_dwarf_attr (die, a);
7207 }
7208
7209 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7210 }
7211
7212 if (decl->die_parent != NULL
7213 && !is_unit_die (decl->die_parent))
7214 {
7215 new_decl = copy_ancestor_tree (unit, decl, NULL);
7216 if (new_decl != NULL)
7217 {
7218 remove_AT (new_decl, DW_AT_signature);
7219 add_AT_specification (die, new_decl);
7220 }
7221 }
7222
7223 return orig_parent;
7224 }
7225
7226 /* Generate the skeleton ancestor tree for the given NODE, then clone
7227 the DIE and add the clone into the tree. */
7228
7229 static void
7230 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7231 {
7232 if (node->new_die != NULL)
7233 return;
7234
7235 node->new_die = clone_as_declaration (node->old_die);
7236
7237 if (node->parent != NULL)
7238 {
7239 generate_skeleton_ancestor_tree (node->parent);
7240 add_child_die (node->parent->new_die, node->new_die);
7241 }
7242 }
7243
7244 /* Generate a skeleton tree of DIEs containing any declarations that are
7245 found in the original tree. We traverse the tree looking for declaration
7246 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7247
7248 static void
7249 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7250 {
7251 skeleton_chain_node node;
7252 dw_die_ref c;
7253 dw_die_ref first;
7254 dw_die_ref prev = NULL;
7255 dw_die_ref next = NULL;
7256
7257 node.parent = parent;
7258
7259 first = c = parent->old_die->die_child;
7260 if (c)
7261 next = c->die_sib;
7262 if (c) do {
7263 if (prev == NULL || prev->die_sib == c)
7264 prev = c;
7265 c = next;
7266 next = (c == first ? NULL : c->die_sib);
7267 node.old_die = c;
7268 node.new_die = NULL;
7269 if (is_declaration_die (c))
7270 {
7271 if (is_template_instantiation (c))
7272 {
7273 /* Instantiated templates do not need to be cloned into the
7274 type unit. Just move the DIE and its children back to
7275 the skeleton tree (in the main CU). */
7276 remove_child_with_prev (c, prev);
7277 add_child_die (parent->new_die, c);
7278 c = prev;
7279 }
7280 else
7281 {
7282 /* Clone the existing DIE, move the original to the skeleton
7283 tree (which is in the main CU), and put the clone, with
7284 all the original's children, where the original came from
7285 (which is about to be moved to the type unit). */
7286 dw_die_ref clone = clone_die (c);
7287 move_all_children (c, clone);
7288
7289 /* If the original has a DW_AT_object_pointer attribute,
7290 it would now point to a child DIE just moved to the
7291 cloned tree, so we need to remove that attribute from
7292 the original. */
7293 remove_AT (c, DW_AT_object_pointer);
7294
7295 replace_child (c, clone, prev);
7296 generate_skeleton_ancestor_tree (parent);
7297 add_child_die (parent->new_die, c);
7298 node.new_die = c;
7299 c = clone;
7300 }
7301 }
7302 generate_skeleton_bottom_up (&node);
7303 } while (next != NULL);
7304 }
7305
7306 /* Wrapper function for generate_skeleton_bottom_up. */
7307
7308 static dw_die_ref
7309 generate_skeleton (dw_die_ref die)
7310 {
7311 skeleton_chain_node node;
7312
7313 node.old_die = die;
7314 node.new_die = NULL;
7315 node.parent = NULL;
7316
7317 /* If this type definition is nested inside another type,
7318 and is not an instantiation of a template, always leave
7319 at least a declaration in its place. */
7320 if (die->die_parent != NULL
7321 && is_type_die (die->die_parent)
7322 && !is_template_instantiation (die))
7323 node.new_die = clone_as_declaration (die);
7324
7325 generate_skeleton_bottom_up (&node);
7326 return node.new_die;
7327 }
7328
7329 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7330 declaration. The original DIE is moved to a new compile unit so that
7331 existing references to it follow it to the new location. If any of the
7332 original DIE's descendants is a declaration, we need to replace the
7333 original DIE with a skeleton tree and move the declarations back into the
7334 skeleton tree. */
7335
7336 static dw_die_ref
7337 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7338 dw_die_ref prev)
7339 {
7340 dw_die_ref skeleton, orig_parent;
7341
7342 /* Copy the declaration context to the type unit DIE. If the returned
7343 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7344 that DIE. */
7345 orig_parent = copy_declaration_context (unit, child);
7346
7347 skeleton = generate_skeleton (child);
7348 if (skeleton == NULL)
7349 remove_child_with_prev (child, prev);
7350 else
7351 {
7352 skeleton->comdat_type_p = true;
7353 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7354
7355 /* If the original DIE was a specification, we need to put
7356 the skeleton under the parent DIE of the declaration.
7357 This leaves the original declaration in the tree, but
7358 it will be pruned later since there are no longer any
7359 references to it. */
7360 if (orig_parent != NULL)
7361 {
7362 remove_child_with_prev (child, prev);
7363 add_child_die (orig_parent, skeleton);
7364 }
7365 else
7366 replace_child (child, skeleton, prev);
7367 }
7368
7369 return skeleton;
7370 }
7371
7372 /* Traverse the DIE and set up additional .debug_types sections for each
7373 type worthy of being placed in a COMDAT section. */
7374
7375 static void
7376 break_out_comdat_types (dw_die_ref die)
7377 {
7378 dw_die_ref c;
7379 dw_die_ref first;
7380 dw_die_ref prev = NULL;
7381 dw_die_ref next = NULL;
7382 dw_die_ref unit = NULL;
7383
7384 first = c = die->die_child;
7385 if (c)
7386 next = c->die_sib;
7387 if (c) do {
7388 if (prev == NULL || prev->die_sib == c)
7389 prev = c;
7390 c = next;
7391 next = (c == first ? NULL : c->die_sib);
7392 if (should_move_die_to_comdat (c))
7393 {
7394 dw_die_ref replacement;
7395 comdat_type_node_ref type_node;
7396
7397 /* Break out nested types into their own type units. */
7398 break_out_comdat_types (c);
7399
7400 /* Create a new type unit DIE as the root for the new tree, and
7401 add it to the list of comdat types. */
7402 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7403 add_AT_unsigned (unit, DW_AT_language,
7404 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7405 type_node = ggc_cleared_alloc<comdat_type_node> ();
7406 type_node->root_die = unit;
7407 type_node->next = comdat_type_list;
7408 comdat_type_list = type_node;
7409
7410 /* Generate the type signature. */
7411 generate_type_signature (c, type_node);
7412
7413 /* Copy the declaration context, attributes, and children of the
7414 declaration into the new type unit DIE, then remove this DIE
7415 from the main CU (or replace it with a skeleton if necessary). */
7416 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7417 type_node->skeleton_die = replacement;
7418
7419 /* Add the DIE to the new compunit. */
7420 add_child_die (unit, c);
7421
7422 if (replacement != NULL)
7423 c = replacement;
7424 }
7425 else if (c->die_tag == DW_TAG_namespace
7426 || c->die_tag == DW_TAG_class_type
7427 || c->die_tag == DW_TAG_structure_type
7428 || c->die_tag == DW_TAG_union_type)
7429 {
7430 /* Look for nested types that can be broken out. */
7431 break_out_comdat_types (c);
7432 }
7433 } while (next != NULL);
7434 }
7435
7436 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7437 Enter all the cloned children into the hash table decl_table. */
7438
7439 static dw_die_ref
7440 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7441 {
7442 dw_die_ref c;
7443 dw_die_ref clone;
7444 struct decl_table_entry *entry;
7445 decl_table_entry **slot;
7446
7447 if (die->die_tag == DW_TAG_subprogram)
7448 clone = clone_as_declaration (die);
7449 else
7450 clone = clone_die (die);
7451
7452 slot = decl_table->find_slot_with_hash (die,
7453 htab_hash_pointer (die), INSERT);
7454
7455 /* Assert that DIE isn't in the hash table yet. If it would be there
7456 before, the ancestors would be necessarily there as well, therefore
7457 clone_tree_partial wouldn't be called. */
7458 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7459
7460 entry = XCNEW (struct decl_table_entry);
7461 entry->orig = die;
7462 entry->copy = clone;
7463 *slot = entry;
7464
7465 if (die->die_tag != DW_TAG_subprogram)
7466 FOR_EACH_CHILD (die, c,
7467 add_child_die (clone, clone_tree_partial (c, decl_table)));
7468
7469 return clone;
7470 }
7471
7472 /* Walk the DIE and its children, looking for references to incomplete
7473 or trivial types that are unmarked (i.e., that are not in the current
7474 type_unit). */
7475
7476 static void
7477 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7478 {
7479 dw_die_ref c;
7480 dw_attr_ref a;
7481 unsigned ix;
7482
7483 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7484 {
7485 if (AT_class (a) == dw_val_class_die_ref)
7486 {
7487 dw_die_ref targ = AT_ref (a);
7488 decl_table_entry **slot;
7489 struct decl_table_entry *entry;
7490
7491 if (targ->die_mark != 0 || targ->comdat_type_p)
7492 continue;
7493
7494 slot = decl_table->find_slot_with_hash (targ,
7495 htab_hash_pointer (targ),
7496 INSERT);
7497
7498 if (*slot != HTAB_EMPTY_ENTRY)
7499 {
7500 /* TARG has already been copied, so we just need to
7501 modify the reference to point to the copy. */
7502 entry = *slot;
7503 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7504 }
7505 else
7506 {
7507 dw_die_ref parent = unit;
7508 dw_die_ref copy = clone_die (targ);
7509
7510 /* Record in DECL_TABLE that TARG has been copied.
7511 Need to do this now, before the recursive call,
7512 because DECL_TABLE may be expanded and SLOT
7513 would no longer be a valid pointer. */
7514 entry = XCNEW (struct decl_table_entry);
7515 entry->orig = targ;
7516 entry->copy = copy;
7517 *slot = entry;
7518
7519 /* If TARG is not a declaration DIE, we need to copy its
7520 children. */
7521 if (!is_declaration_die (targ))
7522 {
7523 FOR_EACH_CHILD (
7524 targ, c,
7525 add_child_die (copy,
7526 clone_tree_partial (c, decl_table)));
7527 }
7528
7529 /* Make sure the cloned tree is marked as part of the
7530 type unit. */
7531 mark_dies (copy);
7532
7533 /* If TARG has surrounding context, copy its ancestor tree
7534 into the new type unit. */
7535 if (targ->die_parent != NULL
7536 && !is_unit_die (targ->die_parent))
7537 parent = copy_ancestor_tree (unit, targ->die_parent,
7538 decl_table);
7539
7540 add_child_die (parent, copy);
7541 a->dw_attr_val.v.val_die_ref.die = copy;
7542
7543 /* Make sure the newly-copied DIE is walked. If it was
7544 installed in a previously-added context, it won't
7545 get visited otherwise. */
7546 if (parent != unit)
7547 {
7548 /* Find the highest point of the newly-added tree,
7549 mark each node along the way, and walk from there. */
7550 parent->die_mark = 1;
7551 while (parent->die_parent
7552 && parent->die_parent->die_mark == 0)
7553 {
7554 parent = parent->die_parent;
7555 parent->die_mark = 1;
7556 }
7557 copy_decls_walk (unit, parent, decl_table);
7558 }
7559 }
7560 }
7561 }
7562
7563 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7564 }
7565
7566 /* Copy declarations for "unworthy" types into the new comdat section.
7567 Incomplete types, modified types, and certain other types aren't broken
7568 out into comdat sections of their own, so they don't have a signature,
7569 and we need to copy the declaration into the same section so that we
7570 don't have an external reference. */
7571
7572 static void
7573 copy_decls_for_unworthy_types (dw_die_ref unit)
7574 {
7575 mark_dies (unit);
7576 decl_hash_type decl_table (10);
7577 copy_decls_walk (unit, unit, &decl_table);
7578 unmark_dies (unit);
7579 }
7580
7581 /* Traverse the DIE and add a sibling attribute if it may have the
7582 effect of speeding up access to siblings. To save some space,
7583 avoid generating sibling attributes for DIE's without children. */
7584
7585 static void
7586 add_sibling_attributes (dw_die_ref die)
7587 {
7588 dw_die_ref c;
7589
7590 if (! die->die_child)
7591 return;
7592
7593 if (die->die_parent && die != die->die_parent->die_child)
7594 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7595
7596 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7597 }
7598
7599 /* Output all location lists for the DIE and its children. */
7600
7601 static void
7602 output_location_lists (dw_die_ref die)
7603 {
7604 dw_die_ref c;
7605 dw_attr_ref a;
7606 unsigned ix;
7607
7608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7609 if (AT_class (a) == dw_val_class_loc_list)
7610 output_loc_list (AT_loc_list (a));
7611
7612 FOR_EACH_CHILD (die, c, output_location_lists (c));
7613 }
7614
7615 /* We want to limit the number of external references, because they are
7616 larger than local references: a relocation takes multiple words, and
7617 even a sig8 reference is always eight bytes, whereas a local reference
7618 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7619 So if we encounter multiple external references to the same type DIE, we
7620 make a local typedef stub for it and redirect all references there.
7621
7622 This is the element of the hash table for keeping track of these
7623 references. */
7624
7625 struct external_ref
7626 {
7627 dw_die_ref type;
7628 dw_die_ref stub;
7629 unsigned n_refs;
7630 };
7631
7632 /* Hashtable helpers. */
7633
7634 struct external_ref_hasher : typed_free_remove <external_ref>
7635 {
7636 typedef external_ref value_type;
7637 typedef external_ref compare_type;
7638 static inline hashval_t hash (const value_type *);
7639 static inline bool equal (const value_type *, const compare_type *);
7640 };
7641
7642 inline hashval_t
7643 external_ref_hasher::hash (const value_type *r)
7644 {
7645 dw_die_ref die = r->type;
7646 hashval_t h = 0;
7647
7648 /* We can't use the address of the DIE for hashing, because
7649 that will make the order of the stub DIEs non-deterministic. */
7650 if (! die->comdat_type_p)
7651 /* We have a symbol; use it to compute a hash. */
7652 h = htab_hash_string (die->die_id.die_symbol);
7653 else
7654 {
7655 /* We have a type signature; use a subset of the bits as the hash.
7656 The 8-byte signature is at least as large as hashval_t. */
7657 comdat_type_node_ref type_node = die->die_id.die_type_node;
7658 memcpy (&h, type_node->signature, sizeof (h));
7659 }
7660 return h;
7661 }
7662
7663 inline bool
7664 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7665 {
7666 return r1->type == r2->type;
7667 }
7668
7669 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7670
7671 /* Return a pointer to the external_ref for references to DIE. */
7672
7673 static struct external_ref *
7674 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7675 {
7676 struct external_ref ref, *ref_p;
7677 external_ref **slot;
7678
7679 ref.type = die;
7680 slot = map->find_slot (&ref, INSERT);
7681 if (*slot != HTAB_EMPTY_ENTRY)
7682 return *slot;
7683
7684 ref_p = XCNEW (struct external_ref);
7685 ref_p->type = die;
7686 *slot = ref_p;
7687 return ref_p;
7688 }
7689
7690 /* Subroutine of optimize_external_refs, below.
7691
7692 If we see a type skeleton, record it as our stub. If we see external
7693 references, remember how many we've seen. */
7694
7695 static void
7696 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7697 {
7698 dw_die_ref c;
7699 dw_attr_ref a;
7700 unsigned ix;
7701 struct external_ref *ref_p;
7702
7703 if (is_type_die (die)
7704 && (c = get_AT_ref (die, DW_AT_signature)))
7705 {
7706 /* This is a local skeleton; use it for local references. */
7707 ref_p = lookup_external_ref (map, c);
7708 ref_p->stub = die;
7709 }
7710
7711 /* Scan the DIE references, and remember any that refer to DIEs from
7712 other CUs (i.e. those which are not marked). */
7713 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7714 if (AT_class (a) == dw_val_class_die_ref
7715 && (c = AT_ref (a))->die_mark == 0
7716 && is_type_die (c))
7717 {
7718 ref_p = lookup_external_ref (map, c);
7719 ref_p->n_refs++;
7720 }
7721
7722 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7723 }
7724
7725 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7726 points to an external_ref, DATA is the CU we're processing. If we don't
7727 already have a local stub, and we have multiple refs, build a stub. */
7728
7729 int
7730 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7731 {
7732 struct external_ref *ref_p = *slot;
7733
7734 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7735 {
7736 /* We have multiple references to this type, so build a small stub.
7737 Both of these forms are a bit dodgy from the perspective of the
7738 DWARF standard, since technically they should have names. */
7739 dw_die_ref cu = data;
7740 dw_die_ref type = ref_p->type;
7741 dw_die_ref stub = NULL;
7742
7743 if (type->comdat_type_p)
7744 {
7745 /* If we refer to this type via sig8, use AT_signature. */
7746 stub = new_die (type->die_tag, cu, NULL_TREE);
7747 add_AT_die_ref (stub, DW_AT_signature, type);
7748 }
7749 else
7750 {
7751 /* Otherwise, use a typedef with no name. */
7752 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7753 add_AT_die_ref (stub, DW_AT_type, type);
7754 }
7755
7756 stub->die_mark++;
7757 ref_p->stub = stub;
7758 }
7759 return 1;
7760 }
7761
7762 /* DIE is a unit; look through all the DIE references to see if there are
7763 any external references to types, and if so, create local stubs for
7764 them which will be applied in build_abbrev_table. This is useful because
7765 references to local DIEs are smaller. */
7766
7767 static external_ref_hash_type *
7768 optimize_external_refs (dw_die_ref die)
7769 {
7770 external_ref_hash_type *map = new external_ref_hash_type (10);
7771 optimize_external_refs_1 (die, map);
7772 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7773 return map;
7774 }
7775
7776 /* The format of each DIE (and its attribute value pairs) is encoded in an
7777 abbreviation table. This routine builds the abbreviation table and assigns
7778 a unique abbreviation id for each abbreviation entry. The children of each
7779 die are visited recursively. */
7780
7781 static void
7782 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7783 {
7784 unsigned long abbrev_id;
7785 unsigned int n_alloc;
7786 dw_die_ref c;
7787 dw_attr_ref a;
7788 unsigned ix;
7789
7790 /* Scan the DIE references, and replace any that refer to
7791 DIEs from other CUs (i.e. those which are not marked) with
7792 the local stubs we built in optimize_external_refs. */
7793 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7794 if (AT_class (a) == dw_val_class_die_ref
7795 && (c = AT_ref (a))->die_mark == 0)
7796 {
7797 struct external_ref *ref_p;
7798 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7799
7800 ref_p = lookup_external_ref (extern_map, c);
7801 if (ref_p->stub && ref_p->stub != die)
7802 change_AT_die_ref (a, ref_p->stub);
7803 else
7804 /* We aren't changing this reference, so mark it external. */
7805 set_AT_ref_external (a, 1);
7806 }
7807
7808 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7809 {
7810 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7811 dw_attr_ref die_a, abbrev_a;
7812 unsigned ix;
7813 bool ok = true;
7814
7815 if (abbrev->die_tag != die->die_tag)
7816 continue;
7817 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7818 continue;
7819
7820 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7821 continue;
7822
7823 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7824 {
7825 abbrev_a = &(*abbrev->die_attr)[ix];
7826 if ((abbrev_a->dw_attr != die_a->dw_attr)
7827 || (value_format (abbrev_a) != value_format (die_a)))
7828 {
7829 ok = false;
7830 break;
7831 }
7832 }
7833 if (ok)
7834 break;
7835 }
7836
7837 if (abbrev_id >= abbrev_die_table_in_use)
7838 {
7839 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7840 {
7841 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7842 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7843 n_alloc);
7844
7845 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7846 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7847 abbrev_die_table_allocated = n_alloc;
7848 }
7849
7850 ++abbrev_die_table_in_use;
7851 abbrev_die_table[abbrev_id] = die;
7852 }
7853
7854 die->die_abbrev = abbrev_id;
7855 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7856 }
7857 \f
7858 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7859
7860 static int
7861 constant_size (unsigned HOST_WIDE_INT value)
7862 {
7863 int log;
7864
7865 if (value == 0)
7866 log = 0;
7867 else
7868 log = floor_log2 (value);
7869
7870 log = log / 8;
7871 log = 1 << (floor_log2 (log) + 1);
7872
7873 return log;
7874 }
7875
7876 /* Return the size of a DIE as it is represented in the
7877 .debug_info section. */
7878
7879 static unsigned long
7880 size_of_die (dw_die_ref die)
7881 {
7882 unsigned long size = 0;
7883 dw_attr_ref a;
7884 unsigned ix;
7885 enum dwarf_form form;
7886
7887 size += size_of_uleb128 (die->die_abbrev);
7888 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7889 {
7890 switch (AT_class (a))
7891 {
7892 case dw_val_class_addr:
7893 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7894 {
7895 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7896 size += size_of_uleb128 (AT_index (a));
7897 }
7898 else
7899 size += DWARF2_ADDR_SIZE;
7900 break;
7901 case dw_val_class_offset:
7902 size += DWARF_OFFSET_SIZE;
7903 break;
7904 case dw_val_class_loc:
7905 {
7906 unsigned long lsize = size_of_locs (AT_loc (a));
7907
7908 /* Block length. */
7909 if (dwarf_version >= 4)
7910 size += size_of_uleb128 (lsize);
7911 else
7912 size += constant_size (lsize);
7913 size += lsize;
7914 }
7915 break;
7916 case dw_val_class_loc_list:
7917 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7918 {
7919 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7920 size += size_of_uleb128 (AT_index (a));
7921 }
7922 else
7923 size += DWARF_OFFSET_SIZE;
7924 break;
7925 case dw_val_class_range_list:
7926 size += DWARF_OFFSET_SIZE;
7927 break;
7928 case dw_val_class_const:
7929 size += size_of_sleb128 (AT_int (a));
7930 break;
7931 case dw_val_class_unsigned_const:
7932 {
7933 int csize = constant_size (AT_unsigned (a));
7934 if (dwarf_version == 3
7935 && a->dw_attr == DW_AT_data_member_location
7936 && csize >= 4)
7937 size += size_of_uleb128 (AT_unsigned (a));
7938 else
7939 size += csize;
7940 }
7941 break;
7942 case dw_val_class_const_double:
7943 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7944 if (HOST_BITS_PER_WIDE_INT >= 64)
7945 size++; /* block */
7946 break;
7947 case dw_val_class_wide_int:
7948 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7949 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7950 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7951 > 64)
7952 size++; /* block */
7953 break;
7954 case dw_val_class_vec:
7955 size += constant_size (a->dw_attr_val.v.val_vec.length
7956 * a->dw_attr_val.v.val_vec.elt_size)
7957 + a->dw_attr_val.v.val_vec.length
7958 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7959 break;
7960 case dw_val_class_flag:
7961 if (dwarf_version >= 4)
7962 /* Currently all add_AT_flag calls pass in 1 as last argument,
7963 so DW_FORM_flag_present can be used. If that ever changes,
7964 we'll need to use DW_FORM_flag and have some optimization
7965 in build_abbrev_table that will change those to
7966 DW_FORM_flag_present if it is set to 1 in all DIEs using
7967 the same abbrev entry. */
7968 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7969 else
7970 size += 1;
7971 break;
7972 case dw_val_class_die_ref:
7973 if (AT_ref_external (a))
7974 {
7975 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7976 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7977 is sized by target address length, whereas in DWARF3
7978 it's always sized as an offset. */
7979 if (use_debug_types)
7980 size += DWARF_TYPE_SIGNATURE_SIZE;
7981 else if (dwarf_version == 2)
7982 size += DWARF2_ADDR_SIZE;
7983 else
7984 size += DWARF_OFFSET_SIZE;
7985 }
7986 else
7987 size += DWARF_OFFSET_SIZE;
7988 break;
7989 case dw_val_class_fde_ref:
7990 size += DWARF_OFFSET_SIZE;
7991 break;
7992 case dw_val_class_lbl_id:
7993 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7994 {
7995 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7996 size += size_of_uleb128 (AT_index (a));
7997 }
7998 else
7999 size += DWARF2_ADDR_SIZE;
8000 break;
8001 case dw_val_class_lineptr:
8002 case dw_val_class_macptr:
8003 size += DWARF_OFFSET_SIZE;
8004 break;
8005 case dw_val_class_str:
8006 form = AT_string_form (a);
8007 if (form == DW_FORM_strp)
8008 size += DWARF_OFFSET_SIZE;
8009 else if (form == DW_FORM_GNU_str_index)
8010 size += size_of_uleb128 (AT_index (a));
8011 else
8012 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8013 break;
8014 case dw_val_class_file:
8015 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8016 break;
8017 case dw_val_class_data8:
8018 size += 8;
8019 break;
8020 case dw_val_class_vms_delta:
8021 size += DWARF_OFFSET_SIZE;
8022 break;
8023 case dw_val_class_high_pc:
8024 size += DWARF2_ADDR_SIZE;
8025 break;
8026 default:
8027 gcc_unreachable ();
8028 }
8029 }
8030
8031 return size;
8032 }
8033
8034 /* Size the debugging information associated with a given DIE. Visits the
8035 DIE's children recursively. Updates the global variable next_die_offset, on
8036 each time through. Uses the current value of next_die_offset to update the
8037 die_offset field in each DIE. */
8038
8039 static void
8040 calc_die_sizes (dw_die_ref die)
8041 {
8042 dw_die_ref c;
8043
8044 gcc_assert (die->die_offset == 0
8045 || (unsigned long int) die->die_offset == next_die_offset);
8046 die->die_offset = next_die_offset;
8047 next_die_offset += size_of_die (die);
8048
8049 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8050
8051 if (die->die_child != NULL)
8052 /* Count the null byte used to terminate sibling lists. */
8053 next_die_offset += 1;
8054 }
8055
8056 /* Size just the base type children at the start of the CU.
8057 This is needed because build_abbrev needs to size locs
8058 and sizing of type based stack ops needs to know die_offset
8059 values for the base types. */
8060
8061 static void
8062 calc_base_type_die_sizes (void)
8063 {
8064 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8065 unsigned int i;
8066 dw_die_ref base_type;
8067 #if ENABLE_ASSERT_CHECKING
8068 dw_die_ref prev = comp_unit_die ()->die_child;
8069 #endif
8070
8071 die_offset += size_of_die (comp_unit_die ());
8072 for (i = 0; base_types.iterate (i, &base_type); i++)
8073 {
8074 #if ENABLE_ASSERT_CHECKING
8075 gcc_assert (base_type->die_offset == 0
8076 && prev->die_sib == base_type
8077 && base_type->die_child == NULL
8078 && base_type->die_abbrev);
8079 prev = base_type;
8080 #endif
8081 base_type->die_offset = die_offset;
8082 die_offset += size_of_die (base_type);
8083 }
8084 }
8085
8086 /* Set the marks for a die and its children. We do this so
8087 that we know whether or not a reference needs to use FORM_ref_addr; only
8088 DIEs in the same CU will be marked. We used to clear out the offset
8089 and use that as the flag, but ran into ordering problems. */
8090
8091 static void
8092 mark_dies (dw_die_ref die)
8093 {
8094 dw_die_ref c;
8095
8096 gcc_assert (!die->die_mark);
8097
8098 die->die_mark = 1;
8099 FOR_EACH_CHILD (die, c, mark_dies (c));
8100 }
8101
8102 /* Clear the marks for a die and its children. */
8103
8104 static void
8105 unmark_dies (dw_die_ref die)
8106 {
8107 dw_die_ref c;
8108
8109 if (! use_debug_types)
8110 gcc_assert (die->die_mark);
8111
8112 die->die_mark = 0;
8113 FOR_EACH_CHILD (die, c, unmark_dies (c));
8114 }
8115
8116 /* Clear the marks for a die, its children and referred dies. */
8117
8118 static void
8119 unmark_all_dies (dw_die_ref die)
8120 {
8121 dw_die_ref c;
8122 dw_attr_ref a;
8123 unsigned ix;
8124
8125 if (!die->die_mark)
8126 return;
8127 die->die_mark = 0;
8128
8129 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8130
8131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8132 if (AT_class (a) == dw_val_class_die_ref)
8133 unmark_all_dies (AT_ref (a));
8134 }
8135
8136 /* Calculate if the entry should appear in the final output file. It may be
8137 from a pruned a type. */
8138
8139 static bool
8140 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8141 {
8142 /* By limiting gnu pubnames to definitions only, gold can generate a
8143 gdb index without entries for declarations, which don't include
8144 enough information to be useful. */
8145 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8146 return false;
8147
8148 if (table == pubname_table)
8149 {
8150 /* Enumerator names are part of the pubname table, but the
8151 parent DW_TAG_enumeration_type die may have been pruned.
8152 Don't output them if that is the case. */
8153 if (p->die->die_tag == DW_TAG_enumerator &&
8154 (p->die->die_parent == NULL
8155 || !p->die->die_parent->die_perennial_p))
8156 return false;
8157
8158 /* Everything else in the pubname table is included. */
8159 return true;
8160 }
8161
8162 /* The pubtypes table shouldn't include types that have been
8163 pruned. */
8164 return (p->die->die_offset != 0
8165 || !flag_eliminate_unused_debug_types);
8166 }
8167
8168 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8169 generated for the compilation unit. */
8170
8171 static unsigned long
8172 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8173 {
8174 unsigned long size;
8175 unsigned i;
8176 pubname_ref p;
8177 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8178
8179 size = DWARF_PUBNAMES_HEADER_SIZE;
8180 FOR_EACH_VEC_ELT (*names, i, p)
8181 if (include_pubname_in_output (names, p))
8182 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8183
8184 size += DWARF_OFFSET_SIZE;
8185 return size;
8186 }
8187
8188 /* Return the size of the information in the .debug_aranges section. */
8189
8190 static unsigned long
8191 size_of_aranges (void)
8192 {
8193 unsigned long size;
8194
8195 size = DWARF_ARANGES_HEADER_SIZE;
8196
8197 /* Count the address/length pair for this compilation unit. */
8198 if (text_section_used)
8199 size += 2 * DWARF2_ADDR_SIZE;
8200 if (cold_text_section_used)
8201 size += 2 * DWARF2_ADDR_SIZE;
8202 if (have_multiple_function_sections)
8203 {
8204 unsigned fde_idx;
8205 dw_fde_ref fde;
8206
8207 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8208 {
8209 if (DECL_IGNORED_P (fde->decl))
8210 continue;
8211 if (!fde->in_std_section)
8212 size += 2 * DWARF2_ADDR_SIZE;
8213 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8214 size += 2 * DWARF2_ADDR_SIZE;
8215 }
8216 }
8217
8218 /* Count the two zero words used to terminated the address range table. */
8219 size += 2 * DWARF2_ADDR_SIZE;
8220 return size;
8221 }
8222 \f
8223 /* Select the encoding of an attribute value. */
8224
8225 static enum dwarf_form
8226 value_format (dw_attr_ref a)
8227 {
8228 switch (AT_class (a))
8229 {
8230 case dw_val_class_addr:
8231 /* Only very few attributes allow DW_FORM_addr. */
8232 switch (a->dw_attr)
8233 {
8234 case DW_AT_low_pc:
8235 case DW_AT_high_pc:
8236 case DW_AT_entry_pc:
8237 case DW_AT_trampoline:
8238 return (AT_index (a) == NOT_INDEXED
8239 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8240 default:
8241 break;
8242 }
8243 switch (DWARF2_ADDR_SIZE)
8244 {
8245 case 1:
8246 return DW_FORM_data1;
8247 case 2:
8248 return DW_FORM_data2;
8249 case 4:
8250 return DW_FORM_data4;
8251 case 8:
8252 return DW_FORM_data8;
8253 default:
8254 gcc_unreachable ();
8255 }
8256 case dw_val_class_range_list:
8257 case dw_val_class_loc_list:
8258 if (dwarf_version >= 4)
8259 return DW_FORM_sec_offset;
8260 /* FALLTHRU */
8261 case dw_val_class_vms_delta:
8262 case dw_val_class_offset:
8263 switch (DWARF_OFFSET_SIZE)
8264 {
8265 case 4:
8266 return DW_FORM_data4;
8267 case 8:
8268 return DW_FORM_data8;
8269 default:
8270 gcc_unreachable ();
8271 }
8272 case dw_val_class_loc:
8273 if (dwarf_version >= 4)
8274 return DW_FORM_exprloc;
8275 switch (constant_size (size_of_locs (AT_loc (a))))
8276 {
8277 case 1:
8278 return DW_FORM_block1;
8279 case 2:
8280 return DW_FORM_block2;
8281 case 4:
8282 return DW_FORM_block4;
8283 default:
8284 gcc_unreachable ();
8285 }
8286 case dw_val_class_const:
8287 return DW_FORM_sdata;
8288 case dw_val_class_unsigned_const:
8289 switch (constant_size (AT_unsigned (a)))
8290 {
8291 case 1:
8292 return DW_FORM_data1;
8293 case 2:
8294 return DW_FORM_data2;
8295 case 4:
8296 /* In DWARF3 DW_AT_data_member_location with
8297 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8298 constant, so we need to use DW_FORM_udata if we need
8299 a large constant. */
8300 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8301 return DW_FORM_udata;
8302 return DW_FORM_data4;
8303 case 8:
8304 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8305 return DW_FORM_udata;
8306 return DW_FORM_data8;
8307 default:
8308 gcc_unreachable ();
8309 }
8310 case dw_val_class_const_double:
8311 switch (HOST_BITS_PER_WIDE_INT)
8312 {
8313 case 8:
8314 return DW_FORM_data2;
8315 case 16:
8316 return DW_FORM_data4;
8317 case 32:
8318 return DW_FORM_data8;
8319 case 64:
8320 default:
8321 return DW_FORM_block1;
8322 }
8323 case dw_val_class_wide_int:
8324 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8325 {
8326 case 8:
8327 return DW_FORM_data1;
8328 case 16:
8329 return DW_FORM_data2;
8330 case 32:
8331 return DW_FORM_data4;
8332 case 64:
8333 return DW_FORM_data8;
8334 default:
8335 return DW_FORM_block1;
8336 }
8337 case dw_val_class_vec:
8338 switch (constant_size (a->dw_attr_val.v.val_vec.length
8339 * a->dw_attr_val.v.val_vec.elt_size))
8340 {
8341 case 1:
8342 return DW_FORM_block1;
8343 case 2:
8344 return DW_FORM_block2;
8345 case 4:
8346 return DW_FORM_block4;
8347 default:
8348 gcc_unreachable ();
8349 }
8350 case dw_val_class_flag:
8351 if (dwarf_version >= 4)
8352 {
8353 /* Currently all add_AT_flag calls pass in 1 as last argument,
8354 so DW_FORM_flag_present can be used. If that ever changes,
8355 we'll need to use DW_FORM_flag and have some optimization
8356 in build_abbrev_table that will change those to
8357 DW_FORM_flag_present if it is set to 1 in all DIEs using
8358 the same abbrev entry. */
8359 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8360 return DW_FORM_flag_present;
8361 }
8362 return DW_FORM_flag;
8363 case dw_val_class_die_ref:
8364 if (AT_ref_external (a))
8365 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8366 else
8367 return DW_FORM_ref;
8368 case dw_val_class_fde_ref:
8369 return DW_FORM_data;
8370 case dw_val_class_lbl_id:
8371 return (AT_index (a) == NOT_INDEXED
8372 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8373 case dw_val_class_lineptr:
8374 case dw_val_class_macptr:
8375 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8376 case dw_val_class_str:
8377 return AT_string_form (a);
8378 case dw_val_class_file:
8379 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8380 {
8381 case 1:
8382 return DW_FORM_data1;
8383 case 2:
8384 return DW_FORM_data2;
8385 case 4:
8386 return DW_FORM_data4;
8387 default:
8388 gcc_unreachable ();
8389 }
8390
8391 case dw_val_class_data8:
8392 return DW_FORM_data8;
8393
8394 case dw_val_class_high_pc:
8395 switch (DWARF2_ADDR_SIZE)
8396 {
8397 case 1:
8398 return DW_FORM_data1;
8399 case 2:
8400 return DW_FORM_data2;
8401 case 4:
8402 return DW_FORM_data4;
8403 case 8:
8404 return DW_FORM_data8;
8405 default:
8406 gcc_unreachable ();
8407 }
8408
8409 default:
8410 gcc_unreachable ();
8411 }
8412 }
8413
8414 /* Output the encoding of an attribute value. */
8415
8416 static void
8417 output_value_format (dw_attr_ref a)
8418 {
8419 enum dwarf_form form = value_format (a);
8420
8421 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8422 }
8423
8424 /* Given a die and id, produce the appropriate abbreviations. */
8425
8426 static void
8427 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8428 {
8429 unsigned ix;
8430 dw_attr_ref a_attr;
8431
8432 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8433 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8434 dwarf_tag_name (abbrev->die_tag));
8435
8436 if (abbrev->die_child != NULL)
8437 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8438 else
8439 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8440
8441 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8442 {
8443 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8444 dwarf_attr_name (a_attr->dw_attr));
8445 output_value_format (a_attr);
8446 }
8447
8448 dw2_asm_output_data (1, 0, NULL);
8449 dw2_asm_output_data (1, 0, NULL);
8450 }
8451
8452
8453 /* Output the .debug_abbrev section which defines the DIE abbreviation
8454 table. */
8455
8456 static void
8457 output_abbrev_section (void)
8458 {
8459 unsigned long abbrev_id;
8460
8461 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8462 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8463
8464 /* Terminate the table. */
8465 dw2_asm_output_data (1, 0, NULL);
8466 }
8467
8468 /* Output a symbol we can use to refer to this DIE from another CU. */
8469
8470 static inline void
8471 output_die_symbol (dw_die_ref die)
8472 {
8473 const char *sym = die->die_id.die_symbol;
8474
8475 gcc_assert (!die->comdat_type_p);
8476
8477 if (sym == 0)
8478 return;
8479
8480 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8481 /* We make these global, not weak; if the target doesn't support
8482 .linkonce, it doesn't support combining the sections, so debugging
8483 will break. */
8484 targetm.asm_out.globalize_label (asm_out_file, sym);
8485
8486 ASM_OUTPUT_LABEL (asm_out_file, sym);
8487 }
8488
8489 /* Return a new location list, given the begin and end range, and the
8490 expression. */
8491
8492 static inline dw_loc_list_ref
8493 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8494 const char *section)
8495 {
8496 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8497
8498 retlist->begin = begin;
8499 retlist->begin_entry = NULL;
8500 retlist->end = end;
8501 retlist->expr = expr;
8502 retlist->section = section;
8503
8504 return retlist;
8505 }
8506
8507 /* Generate a new internal symbol for this location list node, if it
8508 hasn't got one yet. */
8509
8510 static inline void
8511 gen_llsym (dw_loc_list_ref list)
8512 {
8513 gcc_assert (!list->ll_symbol);
8514 list->ll_symbol = gen_internal_sym ("LLST");
8515 }
8516
8517 /* Output the location list given to us. */
8518
8519 static void
8520 output_loc_list (dw_loc_list_ref list_head)
8521 {
8522 dw_loc_list_ref curr = list_head;
8523
8524 if (list_head->emitted)
8525 return;
8526 list_head->emitted = true;
8527
8528 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8529
8530 /* Walk the location list, and output each range + expression. */
8531 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8532 {
8533 unsigned long size;
8534 /* Don't output an entry that starts and ends at the same address. */
8535 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8536 continue;
8537 size = size_of_locs (curr->expr);
8538 /* If the expression is too large, drop it on the floor. We could
8539 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8540 in the expression, but >= 64KB expressions for a single value
8541 in a single range are unlikely very useful. */
8542 if (size > 0xffff)
8543 continue;
8544 if (dwarf_split_debug_info)
8545 {
8546 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8547 "Location list start/length entry (%s)",
8548 list_head->ll_symbol);
8549 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8550 "Location list range start index (%s)",
8551 curr->begin);
8552 /* The length field is 4 bytes. If we ever need to support
8553 an 8-byte length, we can add a new DW_LLE code or fall back
8554 to DW_LLE_GNU_start_end_entry. */
8555 dw2_asm_output_delta (4, curr->end, curr->begin,
8556 "Location list range length (%s)",
8557 list_head->ll_symbol);
8558 }
8559 else if (!have_multiple_function_sections)
8560 {
8561 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8562 "Location list begin address (%s)",
8563 list_head->ll_symbol);
8564 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8565 "Location list end address (%s)",
8566 list_head->ll_symbol);
8567 }
8568 else
8569 {
8570 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8571 "Location list begin address (%s)",
8572 list_head->ll_symbol);
8573 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8574 "Location list end address (%s)",
8575 list_head->ll_symbol);
8576 }
8577
8578 /* Output the block length for this list of location operations. */
8579 gcc_assert (size <= 0xffff);
8580 dw2_asm_output_data (2, size, "%s", "Location expression size");
8581
8582 output_loc_sequence (curr->expr, -1);
8583 }
8584
8585 if (dwarf_split_debug_info)
8586 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8587 "Location list terminator (%s)",
8588 list_head->ll_symbol);
8589 else
8590 {
8591 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8592 "Location list terminator begin (%s)",
8593 list_head->ll_symbol);
8594 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8595 "Location list terminator end (%s)",
8596 list_head->ll_symbol);
8597 }
8598 }
8599
8600 /* Output a range_list offset into the debug_range section. Emit a
8601 relocated reference if val_entry is NULL, otherwise, emit an
8602 indirect reference. */
8603
8604 static void
8605 output_range_list_offset (dw_attr_ref a)
8606 {
8607 const char *name = dwarf_attr_name (a->dw_attr);
8608
8609 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8610 {
8611 char *p = strchr (ranges_section_label, '\0');
8612 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8613 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8614 debug_ranges_section, "%s", name);
8615 *p = '\0';
8616 }
8617 else
8618 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8619 "%s (offset from %s)", name, ranges_section_label);
8620 }
8621
8622 /* Output the offset into the debug_loc section. */
8623
8624 static void
8625 output_loc_list_offset (dw_attr_ref a)
8626 {
8627 char *sym = AT_loc_list (a)->ll_symbol;
8628
8629 gcc_assert (sym);
8630 if (dwarf_split_debug_info)
8631 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8632 "%s", dwarf_attr_name (a->dw_attr));
8633 else
8634 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8635 "%s", dwarf_attr_name (a->dw_attr));
8636 }
8637
8638 /* Output an attribute's index or value appropriately. */
8639
8640 static void
8641 output_attr_index_or_value (dw_attr_ref a)
8642 {
8643 const char *name = dwarf_attr_name (a->dw_attr);
8644
8645 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8646 {
8647 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8648 return;
8649 }
8650 switch (AT_class (a))
8651 {
8652 case dw_val_class_addr:
8653 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8654 break;
8655 case dw_val_class_high_pc:
8656 case dw_val_class_lbl_id:
8657 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8658 break;
8659 case dw_val_class_loc_list:
8660 output_loc_list_offset (a);
8661 break;
8662 default:
8663 gcc_unreachable ();
8664 }
8665 }
8666
8667 /* Output a type signature. */
8668
8669 static inline void
8670 output_signature (const char *sig, const char *name)
8671 {
8672 int i;
8673
8674 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8675 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8676 }
8677
8678 /* Output the DIE and its attributes. Called recursively to generate
8679 the definitions of each child DIE. */
8680
8681 static void
8682 output_die (dw_die_ref die)
8683 {
8684 dw_attr_ref a;
8685 dw_die_ref c;
8686 unsigned long size;
8687 unsigned ix;
8688
8689 /* If someone in another CU might refer to us, set up a symbol for
8690 them to point to. */
8691 if (! die->comdat_type_p && die->die_id.die_symbol)
8692 output_die_symbol (die);
8693
8694 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8695 (unsigned long)die->die_offset,
8696 dwarf_tag_name (die->die_tag));
8697
8698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8699 {
8700 const char *name = dwarf_attr_name (a->dw_attr);
8701
8702 switch (AT_class (a))
8703 {
8704 case dw_val_class_addr:
8705 output_attr_index_or_value (a);
8706 break;
8707
8708 case dw_val_class_offset:
8709 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8710 "%s", name);
8711 break;
8712
8713 case dw_val_class_range_list:
8714 output_range_list_offset (a);
8715 break;
8716
8717 case dw_val_class_loc:
8718 size = size_of_locs (AT_loc (a));
8719
8720 /* Output the block length for this list of location operations. */
8721 if (dwarf_version >= 4)
8722 dw2_asm_output_data_uleb128 (size, "%s", name);
8723 else
8724 dw2_asm_output_data (constant_size (size), size, "%s", name);
8725
8726 output_loc_sequence (AT_loc (a), -1);
8727 break;
8728
8729 case dw_val_class_const:
8730 /* ??? It would be slightly more efficient to use a scheme like is
8731 used for unsigned constants below, but gdb 4.x does not sign
8732 extend. Gdb 5.x does sign extend. */
8733 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8734 break;
8735
8736 case dw_val_class_unsigned_const:
8737 {
8738 int csize = constant_size (AT_unsigned (a));
8739 if (dwarf_version == 3
8740 && a->dw_attr == DW_AT_data_member_location
8741 && csize >= 4)
8742 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8743 else
8744 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8745 }
8746 break;
8747
8748 case dw_val_class_const_double:
8749 {
8750 unsigned HOST_WIDE_INT first, second;
8751
8752 if (HOST_BITS_PER_WIDE_INT >= 64)
8753 dw2_asm_output_data (1,
8754 HOST_BITS_PER_DOUBLE_INT
8755 / HOST_BITS_PER_CHAR,
8756 NULL);
8757
8758 if (WORDS_BIG_ENDIAN)
8759 {
8760 first = a->dw_attr_val.v.val_double.high;
8761 second = a->dw_attr_val.v.val_double.low;
8762 }
8763 else
8764 {
8765 first = a->dw_attr_val.v.val_double.low;
8766 second = a->dw_attr_val.v.val_double.high;
8767 }
8768
8769 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8770 first, "%s", name);
8771 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8772 second, NULL);
8773 }
8774 break;
8775
8776 case dw_val_class_wide_int:
8777 {
8778 int i;
8779 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8780 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8781 if (len * HOST_BITS_PER_WIDE_INT > 64)
8782 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8783 NULL);
8784
8785 if (WORDS_BIG_ENDIAN)
8786 for (i = len - 1; i >= 0; --i)
8787 {
8788 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8789 name);
8790 name = NULL;
8791 }
8792 else
8793 for (i = 0; i < len; ++i)
8794 {
8795 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8796 name);
8797 name = NULL;
8798 }
8799 }
8800 break;
8801
8802 case dw_val_class_vec:
8803 {
8804 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8805 unsigned int len = a->dw_attr_val.v.val_vec.length;
8806 unsigned int i;
8807 unsigned char *p;
8808
8809 dw2_asm_output_data (constant_size (len * elt_size),
8810 len * elt_size, "%s", name);
8811 if (elt_size > sizeof (HOST_WIDE_INT))
8812 {
8813 elt_size /= 2;
8814 len *= 2;
8815 }
8816 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8817 i < len;
8818 i++, p += elt_size)
8819 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8820 "fp or vector constant word %u", i);
8821 break;
8822 }
8823
8824 case dw_val_class_flag:
8825 if (dwarf_version >= 4)
8826 {
8827 /* Currently all add_AT_flag calls pass in 1 as last argument,
8828 so DW_FORM_flag_present can be used. If that ever changes,
8829 we'll need to use DW_FORM_flag and have some optimization
8830 in build_abbrev_table that will change those to
8831 DW_FORM_flag_present if it is set to 1 in all DIEs using
8832 the same abbrev entry. */
8833 gcc_assert (AT_flag (a) == 1);
8834 if (flag_debug_asm)
8835 fprintf (asm_out_file, "\t\t\t%s %s\n",
8836 ASM_COMMENT_START, name);
8837 break;
8838 }
8839 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8840 break;
8841
8842 case dw_val_class_loc_list:
8843 output_attr_index_or_value (a);
8844 break;
8845
8846 case dw_val_class_die_ref:
8847 if (AT_ref_external (a))
8848 {
8849 if (AT_ref (a)->comdat_type_p)
8850 {
8851 comdat_type_node_ref type_node =
8852 AT_ref (a)->die_id.die_type_node;
8853
8854 gcc_assert (type_node);
8855 output_signature (type_node->signature, name);
8856 }
8857 else
8858 {
8859 const char *sym = AT_ref (a)->die_id.die_symbol;
8860 int size;
8861
8862 gcc_assert (sym);
8863 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8864 length, whereas in DWARF3 it's always sized as an
8865 offset. */
8866 if (dwarf_version == 2)
8867 size = DWARF2_ADDR_SIZE;
8868 else
8869 size = DWARF_OFFSET_SIZE;
8870 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8871 name);
8872 }
8873 }
8874 else
8875 {
8876 gcc_assert (AT_ref (a)->die_offset);
8877 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8878 "%s", name);
8879 }
8880 break;
8881
8882 case dw_val_class_fde_ref:
8883 {
8884 char l1[20];
8885
8886 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8887 a->dw_attr_val.v.val_fde_index * 2);
8888 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8889 "%s", name);
8890 }
8891 break;
8892
8893 case dw_val_class_vms_delta:
8894 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8895 AT_vms_delta2 (a), AT_vms_delta1 (a),
8896 "%s", name);
8897 break;
8898
8899 case dw_val_class_lbl_id:
8900 output_attr_index_or_value (a);
8901 break;
8902
8903 case dw_val_class_lineptr:
8904 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8905 debug_line_section, "%s", name);
8906 break;
8907
8908 case dw_val_class_macptr:
8909 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8910 debug_macinfo_section, "%s", name);
8911 break;
8912
8913 case dw_val_class_str:
8914 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8915 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8916 a->dw_attr_val.v.val_str->label,
8917 debug_str_section,
8918 "%s: \"%s\"", name, AT_string (a));
8919 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8920 dw2_asm_output_data_uleb128 (AT_index (a),
8921 "%s: \"%s\"", name, AT_string (a));
8922 else
8923 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8924 break;
8925
8926 case dw_val_class_file:
8927 {
8928 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8929
8930 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8931 a->dw_attr_val.v.val_file->filename);
8932 break;
8933 }
8934
8935 case dw_val_class_data8:
8936 {
8937 int i;
8938
8939 for (i = 0; i < 8; i++)
8940 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8941 i == 0 ? "%s" : NULL, name);
8942 break;
8943 }
8944
8945 case dw_val_class_high_pc:
8946 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8947 get_AT_low_pc (die), "DW_AT_high_pc");
8948 break;
8949
8950 default:
8951 gcc_unreachable ();
8952 }
8953 }
8954
8955 FOR_EACH_CHILD (die, c, output_die (c));
8956
8957 /* Add null byte to terminate sibling list. */
8958 if (die->die_child != NULL)
8959 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8960 (unsigned long) die->die_offset);
8961 }
8962
8963 /* Output the compilation unit that appears at the beginning of the
8964 .debug_info section, and precedes the DIE descriptions. */
8965
8966 static void
8967 output_compilation_unit_header (void)
8968 {
8969 int ver = dwarf_version;
8970
8971 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8972 dw2_asm_output_data (4, 0xffffffff,
8973 "Initial length escape value indicating 64-bit DWARF extension");
8974 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8975 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8976 "Length of Compilation Unit Info");
8977 dw2_asm_output_data (2, ver, "DWARF version number");
8978 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8979 debug_abbrev_section,
8980 "Offset Into Abbrev. Section");
8981 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8982 }
8983
8984 /* Output the compilation unit DIE and its children. */
8985
8986 static void
8987 output_comp_unit (dw_die_ref die, int output_if_empty)
8988 {
8989 const char *secname, *oldsym;
8990 char *tmp;
8991
8992 /* Unless we are outputting main CU, we may throw away empty ones. */
8993 if (!output_if_empty && die->die_child == NULL)
8994 return;
8995
8996 /* Even if there are no children of this DIE, we must output the information
8997 about the compilation unit. Otherwise, on an empty translation unit, we
8998 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8999 will then complain when examining the file. First mark all the DIEs in
9000 this CU so we know which get local refs. */
9001 mark_dies (die);
9002
9003 external_ref_hash_type *extern_map = optimize_external_refs (die);
9004
9005 build_abbrev_table (die, extern_map);
9006
9007 delete extern_map;
9008
9009 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9010 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9011 calc_die_sizes (die);
9012
9013 oldsym = die->die_id.die_symbol;
9014 if (oldsym)
9015 {
9016 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9017
9018 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9019 secname = tmp;
9020 die->die_id.die_symbol = NULL;
9021 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9022 }
9023 else
9024 {
9025 switch_to_section (debug_info_section);
9026 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9027 info_section_emitted = true;
9028 }
9029
9030 /* Output debugging information. */
9031 output_compilation_unit_header ();
9032 output_die (die);
9033
9034 /* Leave the marks on the main CU, so we can check them in
9035 output_pubnames. */
9036 if (oldsym)
9037 {
9038 unmark_dies (die);
9039 die->die_id.die_symbol = oldsym;
9040 }
9041 }
9042
9043 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9044 and .debug_pubtypes. This is configured per-target, but can be
9045 overridden by the -gpubnames or -gno-pubnames options. */
9046
9047 static inline bool
9048 want_pubnames (void)
9049 {
9050 if (debug_info_level <= DINFO_LEVEL_TERSE)
9051 return false;
9052 if (debug_generate_pub_sections != -1)
9053 return debug_generate_pub_sections;
9054 return targetm.want_debug_pub_sections;
9055 }
9056
9057 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9058
9059 static void
9060 add_AT_pubnames (dw_die_ref die)
9061 {
9062 if (want_pubnames ())
9063 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9064 }
9065
9066 /* Add a string attribute value to a skeleton DIE. */
9067
9068 static inline void
9069 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9070 const char *str)
9071 {
9072 dw_attr_node attr;
9073 struct indirect_string_node *node;
9074
9075 if (! skeleton_debug_str_hash)
9076 skeleton_debug_str_hash
9077 = hash_table<indirect_string_hasher>::create_ggc (10);
9078
9079 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9080 find_string_form (node);
9081 if (node->form == DW_FORM_GNU_str_index)
9082 node->form = DW_FORM_strp;
9083
9084 attr.dw_attr = attr_kind;
9085 attr.dw_attr_val.val_class = dw_val_class_str;
9086 attr.dw_attr_val.val_entry = NULL;
9087 attr.dw_attr_val.v.val_str = node;
9088 add_dwarf_attr (die, &attr);
9089 }
9090
9091 /* Helper function to generate top-level dies for skeleton debug_info and
9092 debug_types. */
9093
9094 static void
9095 add_top_level_skeleton_die_attrs (dw_die_ref die)
9096 {
9097 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9098 const char *comp_dir = comp_dir_string ();
9099
9100 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9101 if (comp_dir != NULL)
9102 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9103 add_AT_pubnames (die);
9104 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9105 }
9106
9107 /* Output skeleton debug sections that point to the dwo file. */
9108
9109 static void
9110 output_skeleton_debug_sections (dw_die_ref comp_unit)
9111 {
9112 /* These attributes will be found in the full debug_info section. */
9113 remove_AT (comp_unit, DW_AT_producer);
9114 remove_AT (comp_unit, DW_AT_language);
9115
9116 switch_to_section (debug_skeleton_info_section);
9117 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9118
9119 /* Produce the skeleton compilation-unit header. This one differs enough from
9120 a normal CU header that it's better not to call output_compilation_unit
9121 header. */
9122 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9123 dw2_asm_output_data (4, 0xffffffff,
9124 "Initial length escape value indicating 64-bit DWARF extension");
9125
9126 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9127 DWARF_COMPILE_UNIT_HEADER_SIZE
9128 - DWARF_INITIAL_LENGTH_SIZE
9129 + size_of_die (comp_unit),
9130 "Length of Compilation Unit Info");
9131 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9132 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9133 debug_abbrev_section,
9134 "Offset Into Abbrev. Section");
9135 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9136
9137 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9138 output_die (comp_unit);
9139
9140 /* Build the skeleton debug_abbrev section. */
9141 switch_to_section (debug_skeleton_abbrev_section);
9142 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9143
9144 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9145
9146 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9147 }
9148
9149 /* Output a comdat type unit DIE and its children. */
9150
9151 static void
9152 output_comdat_type_unit (comdat_type_node *node)
9153 {
9154 const char *secname;
9155 char *tmp;
9156 int i;
9157 #if defined (OBJECT_FORMAT_ELF)
9158 tree comdat_key;
9159 #endif
9160
9161 /* First mark all the DIEs in this CU so we know which get local refs. */
9162 mark_dies (node->root_die);
9163
9164 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9165
9166 build_abbrev_table (node->root_die, extern_map);
9167
9168 delete extern_map;
9169 extern_map = NULL;
9170
9171 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9172 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9173 calc_die_sizes (node->root_die);
9174
9175 #if defined (OBJECT_FORMAT_ELF)
9176 if (!dwarf_split_debug_info)
9177 secname = ".debug_types";
9178 else
9179 secname = ".debug_types.dwo";
9180
9181 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9182 sprintf (tmp, "wt.");
9183 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9184 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9185 comdat_key = get_identifier (tmp);
9186 targetm.asm_out.named_section (secname,
9187 SECTION_DEBUG | SECTION_LINKONCE,
9188 comdat_key);
9189 #else
9190 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9191 sprintf (tmp, ".gnu.linkonce.wt.");
9192 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9193 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9194 secname = tmp;
9195 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9196 #endif
9197
9198 /* Output debugging information. */
9199 output_compilation_unit_header ();
9200 output_signature (node->signature, "Type Signature");
9201 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9202 "Offset to Type DIE");
9203 output_die (node->root_die);
9204
9205 unmark_dies (node->root_die);
9206 }
9207
9208 /* Return the DWARF2/3 pubname associated with a decl. */
9209
9210 static const char *
9211 dwarf2_name (tree decl, int scope)
9212 {
9213 if (DECL_NAMELESS (decl))
9214 return NULL;
9215 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9216 }
9217
9218 /* Add a new entry to .debug_pubnames if appropriate. */
9219
9220 static void
9221 add_pubname_string (const char *str, dw_die_ref die)
9222 {
9223 pubname_entry e;
9224
9225 e.die = die;
9226 e.name = xstrdup (str);
9227 vec_safe_push (pubname_table, e);
9228 }
9229
9230 static void
9231 add_pubname (tree decl, dw_die_ref die)
9232 {
9233 if (!want_pubnames ())
9234 return;
9235
9236 /* Don't add items to the table when we expect that the consumer will have
9237 just read the enclosing die. For example, if the consumer is looking at a
9238 class_member, it will either be inside the class already, or will have just
9239 looked up the class to find the member. Either way, searching the class is
9240 faster than searching the index. */
9241 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9242 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9243 {
9244 const char *name = dwarf2_name (decl, 1);
9245
9246 if (name)
9247 add_pubname_string (name, die);
9248 }
9249 }
9250
9251 /* Add an enumerator to the pubnames section. */
9252
9253 static void
9254 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9255 {
9256 pubname_entry e;
9257
9258 gcc_assert (scope_name);
9259 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9260 e.die = die;
9261 vec_safe_push (pubname_table, e);
9262 }
9263
9264 /* Add a new entry to .debug_pubtypes if appropriate. */
9265
9266 static void
9267 add_pubtype (tree decl, dw_die_ref die)
9268 {
9269 pubname_entry e;
9270
9271 if (!want_pubnames ())
9272 return;
9273
9274 if ((TREE_PUBLIC (decl)
9275 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9276 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9277 {
9278 tree scope = NULL;
9279 const char *scope_name = "";
9280 const char *sep = is_cxx () ? "::" : ".";
9281 const char *name;
9282
9283 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9284 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9285 {
9286 scope_name = lang_hooks.dwarf_name (scope, 1);
9287 if (scope_name != NULL && scope_name[0] != '\0')
9288 scope_name = concat (scope_name, sep, NULL);
9289 else
9290 scope_name = "";
9291 }
9292
9293 if (TYPE_P (decl))
9294 name = type_tag (decl);
9295 else
9296 name = lang_hooks.dwarf_name (decl, 1);
9297
9298 /* If we don't have a name for the type, there's no point in adding
9299 it to the table. */
9300 if (name != NULL && name[0] != '\0')
9301 {
9302 e.die = die;
9303 e.name = concat (scope_name, name, NULL);
9304 vec_safe_push (pubtype_table, e);
9305 }
9306
9307 /* Although it might be more consistent to add the pubinfo for the
9308 enumerators as their dies are created, they should only be added if the
9309 enum type meets the criteria above. So rather than re-check the parent
9310 enum type whenever an enumerator die is created, just output them all
9311 here. This isn't protected by the name conditional because anonymous
9312 enums don't have names. */
9313 if (die->die_tag == DW_TAG_enumeration_type)
9314 {
9315 dw_die_ref c;
9316
9317 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9318 }
9319 }
9320 }
9321
9322 /* Output a single entry in the pubnames table. */
9323
9324 static void
9325 output_pubname (dw_offset die_offset, pubname_entry *entry)
9326 {
9327 dw_die_ref die = entry->die;
9328 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9329
9330 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9331
9332 if (debug_generate_pub_sections == 2)
9333 {
9334 /* This logic follows gdb's method for determining the value of the flag
9335 byte. */
9336 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9337 switch (die->die_tag)
9338 {
9339 case DW_TAG_typedef:
9340 case DW_TAG_base_type:
9341 case DW_TAG_subrange_type:
9342 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9343 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9344 break;
9345 case DW_TAG_enumerator:
9346 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9347 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9348 if (!is_cxx () && !is_java ())
9349 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9350 break;
9351 case DW_TAG_subprogram:
9352 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9353 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9354 if (!is_ada ())
9355 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9356 break;
9357 case DW_TAG_constant:
9358 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9359 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9360 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9361 break;
9362 case DW_TAG_variable:
9363 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9364 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9365 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9366 break;
9367 case DW_TAG_namespace:
9368 case DW_TAG_imported_declaration:
9369 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9370 break;
9371 case DW_TAG_class_type:
9372 case DW_TAG_interface_type:
9373 case DW_TAG_structure_type:
9374 case DW_TAG_union_type:
9375 case DW_TAG_enumeration_type:
9376 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9377 if (!is_cxx () && !is_java ())
9378 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9379 break;
9380 default:
9381 /* An unusual tag. Leave the flag-byte empty. */
9382 break;
9383 }
9384 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9385 "GDB-index flags");
9386 }
9387
9388 dw2_asm_output_nstring (entry->name, -1, "external name");
9389 }
9390
9391
9392 /* Output the public names table used to speed up access to externally
9393 visible names; or the public types table used to find type definitions. */
9394
9395 static void
9396 output_pubnames (vec<pubname_entry, va_gc> *names)
9397 {
9398 unsigned i;
9399 unsigned long pubnames_length = size_of_pubnames (names);
9400 pubname_ref pub;
9401
9402 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9403 dw2_asm_output_data (4, 0xffffffff,
9404 "Initial length escape value indicating 64-bit DWARF extension");
9405 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9406
9407 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9408 dw2_asm_output_data (2, 2, "DWARF Version");
9409
9410 if (dwarf_split_debug_info)
9411 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9412 debug_skeleton_info_section,
9413 "Offset of Compilation Unit Info");
9414 else
9415 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9416 debug_info_section,
9417 "Offset of Compilation Unit Info");
9418 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9419 "Compilation Unit Length");
9420
9421 FOR_EACH_VEC_ELT (*names, i, pub)
9422 {
9423 if (include_pubname_in_output (names, pub))
9424 {
9425 dw_offset die_offset = pub->die->die_offset;
9426
9427 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9428 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9429 gcc_assert (pub->die->die_mark);
9430
9431 /* If we're putting types in their own .debug_types sections,
9432 the .debug_pubtypes table will still point to the compile
9433 unit (not the type unit), so we want to use the offset of
9434 the skeleton DIE (if there is one). */
9435 if (pub->die->comdat_type_p && names == pubtype_table)
9436 {
9437 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9438
9439 if (type_node != NULL)
9440 die_offset = (type_node->skeleton_die != NULL
9441 ? type_node->skeleton_die->die_offset
9442 : comp_unit_die ()->die_offset);
9443 }
9444
9445 output_pubname (die_offset, pub);
9446 }
9447 }
9448
9449 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9450 }
9451
9452 /* Output public names and types tables if necessary. */
9453
9454 static void
9455 output_pubtables (void)
9456 {
9457 if (!want_pubnames () || !info_section_emitted)
9458 return;
9459
9460 switch_to_section (debug_pubnames_section);
9461 output_pubnames (pubname_table);
9462 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9463 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9464 simply won't look for the section. */
9465 switch_to_section (debug_pubtypes_section);
9466 output_pubnames (pubtype_table);
9467 }
9468
9469
9470 /* Output the information that goes into the .debug_aranges table.
9471 Namely, define the beginning and ending address range of the
9472 text section generated for this compilation unit. */
9473
9474 static void
9475 output_aranges (unsigned long aranges_length)
9476 {
9477 unsigned i;
9478
9479 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9480 dw2_asm_output_data (4, 0xffffffff,
9481 "Initial length escape value indicating 64-bit DWARF extension");
9482 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9483 "Length of Address Ranges Info");
9484 /* Version number for aranges is still 2, even in DWARF3. */
9485 dw2_asm_output_data (2, 2, "DWARF Version");
9486 if (dwarf_split_debug_info)
9487 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9488 debug_skeleton_info_section,
9489 "Offset of Compilation Unit Info");
9490 else
9491 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9492 debug_info_section,
9493 "Offset of Compilation Unit Info");
9494 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9495 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9496
9497 /* We need to align to twice the pointer size here. */
9498 if (DWARF_ARANGES_PAD_SIZE)
9499 {
9500 /* Pad using a 2 byte words so that padding is correct for any
9501 pointer size. */
9502 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9503 2 * DWARF2_ADDR_SIZE);
9504 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9505 dw2_asm_output_data (2, 0, NULL);
9506 }
9507
9508 /* It is necessary not to output these entries if the sections were
9509 not used; if the sections were not used, the length will be 0 and
9510 the address may end up as 0 if the section is discarded by ld
9511 --gc-sections, leaving an invalid (0, 0) entry that can be
9512 confused with the terminator. */
9513 if (text_section_used)
9514 {
9515 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9516 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9517 text_section_label, "Length");
9518 }
9519 if (cold_text_section_used)
9520 {
9521 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9522 "Address");
9523 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9524 cold_text_section_label, "Length");
9525 }
9526
9527 if (have_multiple_function_sections)
9528 {
9529 unsigned fde_idx;
9530 dw_fde_ref fde;
9531
9532 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9533 {
9534 if (DECL_IGNORED_P (fde->decl))
9535 continue;
9536 if (!fde->in_std_section)
9537 {
9538 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9539 "Address");
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9541 fde->dw_fde_begin, "Length");
9542 }
9543 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9544 {
9545 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9546 "Address");
9547 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9548 fde->dw_fde_second_begin, "Length");
9549 }
9550 }
9551 }
9552
9553 /* Output the terminator words. */
9554 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9555 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9556 }
9557
9558 /* Add a new entry to .debug_ranges. Return the offset at which it
9559 was placed. */
9560
9561 static unsigned int
9562 add_ranges_num (int num)
9563 {
9564 unsigned int in_use = ranges_table_in_use;
9565
9566 if (in_use == ranges_table_allocated)
9567 {
9568 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9569 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9570 ranges_table_allocated);
9571 memset (ranges_table + ranges_table_in_use, 0,
9572 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9573 }
9574
9575 ranges_table[in_use].num = num;
9576 ranges_table_in_use = in_use + 1;
9577
9578 return in_use * 2 * DWARF2_ADDR_SIZE;
9579 }
9580
9581 /* Add a new entry to .debug_ranges corresponding to a block, or a
9582 range terminator if BLOCK is NULL. */
9583
9584 static unsigned int
9585 add_ranges (const_tree block)
9586 {
9587 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9588 }
9589
9590 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9591 When using dwarf_split_debug_info, address attributes in dies destined
9592 for the final executable should be direct references--setting the
9593 parameter force_direct ensures this behavior. */
9594
9595 static void
9596 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9597 bool *added, bool force_direct)
9598 {
9599 unsigned int in_use = ranges_by_label_in_use;
9600 unsigned int offset;
9601
9602 if (in_use == ranges_by_label_allocated)
9603 {
9604 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9605 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9606 ranges_by_label,
9607 ranges_by_label_allocated);
9608 memset (ranges_by_label + ranges_by_label_in_use, 0,
9609 RANGES_TABLE_INCREMENT
9610 * sizeof (struct dw_ranges_by_label_struct));
9611 }
9612
9613 ranges_by_label[in_use].begin = begin;
9614 ranges_by_label[in_use].end = end;
9615 ranges_by_label_in_use = in_use + 1;
9616
9617 offset = add_ranges_num (-(int)in_use - 1);
9618 if (!*added)
9619 {
9620 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9621 *added = true;
9622 }
9623 }
9624
9625 static void
9626 output_ranges (void)
9627 {
9628 unsigned i;
9629 static const char *const start_fmt = "Offset %#x";
9630 const char *fmt = start_fmt;
9631
9632 for (i = 0; i < ranges_table_in_use; i++)
9633 {
9634 int block_num = ranges_table[i].num;
9635
9636 if (block_num > 0)
9637 {
9638 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9639 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9640
9641 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9642 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9643
9644 /* If all code is in the text section, then the compilation
9645 unit base address defaults to DW_AT_low_pc, which is the
9646 base of the text section. */
9647 if (!have_multiple_function_sections)
9648 {
9649 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9650 text_section_label,
9651 fmt, i * 2 * DWARF2_ADDR_SIZE);
9652 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9653 text_section_label, NULL);
9654 }
9655
9656 /* Otherwise, the compilation unit base address is zero,
9657 which allows us to use absolute addresses, and not worry
9658 about whether the target supports cross-section
9659 arithmetic. */
9660 else
9661 {
9662 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9663 fmt, i * 2 * DWARF2_ADDR_SIZE);
9664 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9665 }
9666
9667 fmt = NULL;
9668 }
9669
9670 /* Negative block_num stands for an index into ranges_by_label. */
9671 else if (block_num < 0)
9672 {
9673 int lab_idx = - block_num - 1;
9674
9675 if (!have_multiple_function_sections)
9676 {
9677 gcc_unreachable ();
9678 #if 0
9679 /* If we ever use add_ranges_by_labels () for a single
9680 function section, all we have to do is to take out
9681 the #if 0 above. */
9682 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9683 ranges_by_label[lab_idx].begin,
9684 text_section_label,
9685 fmt, i * 2 * DWARF2_ADDR_SIZE);
9686 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9687 ranges_by_label[lab_idx].end,
9688 text_section_label, NULL);
9689 #endif
9690 }
9691 else
9692 {
9693 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9694 ranges_by_label[lab_idx].begin,
9695 fmt, i * 2 * DWARF2_ADDR_SIZE);
9696 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9697 ranges_by_label[lab_idx].end,
9698 NULL);
9699 }
9700 }
9701 else
9702 {
9703 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9704 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9705 fmt = start_fmt;
9706 }
9707 }
9708 }
9709
9710 /* Data structure containing information about input files. */
9711 struct file_info
9712 {
9713 const char *path; /* Complete file name. */
9714 const char *fname; /* File name part. */
9715 int length; /* Length of entire string. */
9716 struct dwarf_file_data * file_idx; /* Index in input file table. */
9717 int dir_idx; /* Index in directory table. */
9718 };
9719
9720 /* Data structure containing information about directories with source
9721 files. */
9722 struct dir_info
9723 {
9724 const char *path; /* Path including directory name. */
9725 int length; /* Path length. */
9726 int prefix; /* Index of directory entry which is a prefix. */
9727 int count; /* Number of files in this directory. */
9728 int dir_idx; /* Index of directory used as base. */
9729 };
9730
9731 /* Callback function for file_info comparison. We sort by looking at
9732 the directories in the path. */
9733
9734 static int
9735 file_info_cmp (const void *p1, const void *p2)
9736 {
9737 const struct file_info *const s1 = (const struct file_info *) p1;
9738 const struct file_info *const s2 = (const struct file_info *) p2;
9739 const unsigned char *cp1;
9740 const unsigned char *cp2;
9741
9742 /* Take care of file names without directories. We need to make sure that
9743 we return consistent values to qsort since some will get confused if
9744 we return the same value when identical operands are passed in opposite
9745 orders. So if neither has a directory, return 0 and otherwise return
9746 1 or -1 depending on which one has the directory. */
9747 if ((s1->path == s1->fname || s2->path == s2->fname))
9748 return (s2->path == s2->fname) - (s1->path == s1->fname);
9749
9750 cp1 = (const unsigned char *) s1->path;
9751 cp2 = (const unsigned char *) s2->path;
9752
9753 while (1)
9754 {
9755 ++cp1;
9756 ++cp2;
9757 /* Reached the end of the first path? If so, handle like above. */
9758 if ((cp1 == (const unsigned char *) s1->fname)
9759 || (cp2 == (const unsigned char *) s2->fname))
9760 return ((cp2 == (const unsigned char *) s2->fname)
9761 - (cp1 == (const unsigned char *) s1->fname));
9762
9763 /* Character of current path component the same? */
9764 else if (*cp1 != *cp2)
9765 return *cp1 - *cp2;
9766 }
9767 }
9768
9769 struct file_name_acquire_data
9770 {
9771 struct file_info *files;
9772 int used_files;
9773 int max_files;
9774 };
9775
9776 /* Traversal function for the hash table. */
9777
9778 int
9779 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9780 {
9781 struct dwarf_file_data *d = *slot;
9782 struct file_info *fi;
9783 const char *f;
9784
9785 gcc_assert (fnad->max_files >= d->emitted_number);
9786
9787 if (! d->emitted_number)
9788 return 1;
9789
9790 gcc_assert (fnad->max_files != fnad->used_files);
9791
9792 fi = fnad->files + fnad->used_files++;
9793
9794 /* Skip all leading "./". */
9795 f = d->filename;
9796 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9797 f += 2;
9798
9799 /* Create a new array entry. */
9800 fi->path = f;
9801 fi->length = strlen (f);
9802 fi->file_idx = d;
9803
9804 /* Search for the file name part. */
9805 f = strrchr (f, DIR_SEPARATOR);
9806 #if defined (DIR_SEPARATOR_2)
9807 {
9808 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9809
9810 if (g != NULL)
9811 {
9812 if (f == NULL || f < g)
9813 f = g;
9814 }
9815 }
9816 #endif
9817
9818 fi->fname = f == NULL ? fi->path : f + 1;
9819 return 1;
9820 }
9821
9822 /* Output the directory table and the file name table. We try to minimize
9823 the total amount of memory needed. A heuristic is used to avoid large
9824 slowdowns with many input files. */
9825
9826 static void
9827 output_file_names (void)
9828 {
9829 struct file_name_acquire_data fnad;
9830 int numfiles;
9831 struct file_info *files;
9832 struct dir_info *dirs;
9833 int *saved;
9834 int *savehere;
9835 int *backmap;
9836 int ndirs;
9837 int idx_offset;
9838 int i;
9839
9840 if (!last_emitted_file)
9841 {
9842 dw2_asm_output_data (1, 0, "End directory table");
9843 dw2_asm_output_data (1, 0, "End file name table");
9844 return;
9845 }
9846
9847 numfiles = last_emitted_file->emitted_number;
9848
9849 /* Allocate the various arrays we need. */
9850 files = XALLOCAVEC (struct file_info, numfiles);
9851 dirs = XALLOCAVEC (struct dir_info, numfiles);
9852
9853 fnad.files = files;
9854 fnad.used_files = 0;
9855 fnad.max_files = numfiles;
9856 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9857 gcc_assert (fnad.used_files == fnad.max_files);
9858
9859 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9860
9861 /* Find all the different directories used. */
9862 dirs[0].path = files[0].path;
9863 dirs[0].length = files[0].fname - files[0].path;
9864 dirs[0].prefix = -1;
9865 dirs[0].count = 1;
9866 dirs[0].dir_idx = 0;
9867 files[0].dir_idx = 0;
9868 ndirs = 1;
9869
9870 for (i = 1; i < numfiles; i++)
9871 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9872 && memcmp (dirs[ndirs - 1].path, files[i].path,
9873 dirs[ndirs - 1].length) == 0)
9874 {
9875 /* Same directory as last entry. */
9876 files[i].dir_idx = ndirs - 1;
9877 ++dirs[ndirs - 1].count;
9878 }
9879 else
9880 {
9881 int j;
9882
9883 /* This is a new directory. */
9884 dirs[ndirs].path = files[i].path;
9885 dirs[ndirs].length = files[i].fname - files[i].path;
9886 dirs[ndirs].count = 1;
9887 dirs[ndirs].dir_idx = ndirs;
9888 files[i].dir_idx = ndirs;
9889
9890 /* Search for a prefix. */
9891 dirs[ndirs].prefix = -1;
9892 for (j = 0; j < ndirs; j++)
9893 if (dirs[j].length < dirs[ndirs].length
9894 && dirs[j].length > 1
9895 && (dirs[ndirs].prefix == -1
9896 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9897 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9898 dirs[ndirs].prefix = j;
9899
9900 ++ndirs;
9901 }
9902
9903 /* Now to the actual work. We have to find a subset of the directories which
9904 allow expressing the file name using references to the directory table
9905 with the least amount of characters. We do not do an exhaustive search
9906 where we would have to check out every combination of every single
9907 possible prefix. Instead we use a heuristic which provides nearly optimal
9908 results in most cases and never is much off. */
9909 saved = XALLOCAVEC (int, ndirs);
9910 savehere = XALLOCAVEC (int, ndirs);
9911
9912 memset (saved, '\0', ndirs * sizeof (saved[0]));
9913 for (i = 0; i < ndirs; i++)
9914 {
9915 int j;
9916 int total;
9917
9918 /* We can always save some space for the current directory. But this
9919 does not mean it will be enough to justify adding the directory. */
9920 savehere[i] = dirs[i].length;
9921 total = (savehere[i] - saved[i]) * dirs[i].count;
9922
9923 for (j = i + 1; j < ndirs; j++)
9924 {
9925 savehere[j] = 0;
9926 if (saved[j] < dirs[i].length)
9927 {
9928 /* Determine whether the dirs[i] path is a prefix of the
9929 dirs[j] path. */
9930 int k;
9931
9932 k = dirs[j].prefix;
9933 while (k != -1 && k != (int) i)
9934 k = dirs[k].prefix;
9935
9936 if (k == (int) i)
9937 {
9938 /* Yes it is. We can possibly save some memory by
9939 writing the filenames in dirs[j] relative to
9940 dirs[i]. */
9941 savehere[j] = dirs[i].length;
9942 total += (savehere[j] - saved[j]) * dirs[j].count;
9943 }
9944 }
9945 }
9946
9947 /* Check whether we can save enough to justify adding the dirs[i]
9948 directory. */
9949 if (total > dirs[i].length + 1)
9950 {
9951 /* It's worthwhile adding. */
9952 for (j = i; j < ndirs; j++)
9953 if (savehere[j] > 0)
9954 {
9955 /* Remember how much we saved for this directory so far. */
9956 saved[j] = savehere[j];
9957
9958 /* Remember the prefix directory. */
9959 dirs[j].dir_idx = i;
9960 }
9961 }
9962 }
9963
9964 /* Emit the directory name table. */
9965 idx_offset = dirs[0].length > 0 ? 1 : 0;
9966 for (i = 1 - idx_offset; i < ndirs; i++)
9967 dw2_asm_output_nstring (dirs[i].path,
9968 dirs[i].length
9969 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9970 "Directory Entry: %#x", i + idx_offset);
9971
9972 dw2_asm_output_data (1, 0, "End directory table");
9973
9974 /* We have to emit them in the order of emitted_number since that's
9975 used in the debug info generation. To do this efficiently we
9976 generate a back-mapping of the indices first. */
9977 backmap = XALLOCAVEC (int, numfiles);
9978 for (i = 0; i < numfiles; i++)
9979 backmap[files[i].file_idx->emitted_number - 1] = i;
9980
9981 /* Now write all the file names. */
9982 for (i = 0; i < numfiles; i++)
9983 {
9984 int file_idx = backmap[i];
9985 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9986
9987 #ifdef VMS_DEBUGGING_INFO
9988 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9989
9990 /* Setting these fields can lead to debugger miscomparisons,
9991 but VMS Debug requires them to be set correctly. */
9992
9993 int ver;
9994 long long cdt;
9995 long siz;
9996 int maxfilelen = strlen (files[file_idx].path)
9997 + dirs[dir_idx].length
9998 + MAX_VMS_VERSION_LEN + 1;
9999 char *filebuf = XALLOCAVEC (char, maxfilelen);
10000
10001 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10002 snprintf (filebuf, maxfilelen, "%s;%d",
10003 files[file_idx].path + dirs[dir_idx].length, ver);
10004
10005 dw2_asm_output_nstring
10006 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10007
10008 /* Include directory index. */
10009 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10010
10011 /* Modification time. */
10012 dw2_asm_output_data_uleb128
10013 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10014 ? cdt : 0,
10015 NULL);
10016
10017 /* File length in bytes. */
10018 dw2_asm_output_data_uleb128
10019 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10020 ? siz : 0,
10021 NULL);
10022 #else
10023 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10024 "File Entry: %#x", (unsigned) i + 1);
10025
10026 /* Include directory index. */
10027 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10028
10029 /* Modification time. */
10030 dw2_asm_output_data_uleb128 (0, NULL);
10031
10032 /* File length in bytes. */
10033 dw2_asm_output_data_uleb128 (0, NULL);
10034 #endif /* VMS_DEBUGGING_INFO */
10035 }
10036
10037 dw2_asm_output_data (1, 0, "End file name table");
10038 }
10039
10040
10041 /* Output one line number table into the .debug_line section. */
10042
10043 static void
10044 output_one_line_info_table (dw_line_info_table *table)
10045 {
10046 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10047 unsigned int current_line = 1;
10048 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10049 dw_line_info_entry *ent;
10050 size_t i;
10051
10052 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10053 {
10054 switch (ent->opcode)
10055 {
10056 case LI_set_address:
10057 /* ??? Unfortunately, we have little choice here currently, and
10058 must always use the most general form. GCC does not know the
10059 address delta itself, so we can't use DW_LNS_advance_pc. Many
10060 ports do have length attributes which will give an upper bound
10061 on the address range. We could perhaps use length attributes
10062 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10063 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10064
10065 /* This can handle any delta. This takes
10066 4+DWARF2_ADDR_SIZE bytes. */
10067 dw2_asm_output_data (1, 0, "set address %s", line_label);
10068 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10069 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10070 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10071 break;
10072
10073 case LI_set_line:
10074 if (ent->val == current_line)
10075 {
10076 /* We still need to start a new row, so output a copy insn. */
10077 dw2_asm_output_data (1, DW_LNS_copy,
10078 "copy line %u", current_line);
10079 }
10080 else
10081 {
10082 int line_offset = ent->val - current_line;
10083 int line_delta = line_offset - DWARF_LINE_BASE;
10084
10085 current_line = ent->val;
10086 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10087 {
10088 /* This can handle deltas from -10 to 234, using the current
10089 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10090 This takes 1 byte. */
10091 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10092 "line %u", current_line);
10093 }
10094 else
10095 {
10096 /* This can handle any delta. This takes at least 4 bytes,
10097 depending on the value being encoded. */
10098 dw2_asm_output_data (1, DW_LNS_advance_line,
10099 "advance to line %u", current_line);
10100 dw2_asm_output_data_sleb128 (line_offset, NULL);
10101 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10102 }
10103 }
10104 break;
10105
10106 case LI_set_file:
10107 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10108 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10109 break;
10110
10111 case LI_set_column:
10112 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10113 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10114 break;
10115
10116 case LI_negate_stmt:
10117 current_is_stmt = !current_is_stmt;
10118 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10119 "is_stmt %d", current_is_stmt);
10120 break;
10121
10122 case LI_set_prologue_end:
10123 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10124 "set prologue end");
10125 break;
10126
10127 case LI_set_epilogue_begin:
10128 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10129 "set epilogue begin");
10130 break;
10131
10132 case LI_set_discriminator:
10133 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10134 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10135 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10136 dw2_asm_output_data_uleb128 (ent->val, NULL);
10137 break;
10138 }
10139 }
10140
10141 /* Emit debug info for the address of the end of the table. */
10142 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10143 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10144 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10145 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10146
10147 dw2_asm_output_data (1, 0, "end sequence");
10148 dw2_asm_output_data_uleb128 (1, NULL);
10149 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10150 }
10151
10152 /* Output the source line number correspondence information. This
10153 information goes into the .debug_line section. */
10154
10155 static void
10156 output_line_info (bool prologue_only)
10157 {
10158 char l1[20], l2[20], p1[20], p2[20];
10159 int ver = dwarf_version;
10160 bool saw_one = false;
10161 int opc;
10162
10163 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10164 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10165 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10166 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10167
10168 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10169 dw2_asm_output_data (4, 0xffffffff,
10170 "Initial length escape value indicating 64-bit DWARF extension");
10171 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10172 "Length of Source Line Info");
10173 ASM_OUTPUT_LABEL (asm_out_file, l1);
10174
10175 dw2_asm_output_data (2, ver, "DWARF Version");
10176 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10177 ASM_OUTPUT_LABEL (asm_out_file, p1);
10178
10179 /* Define the architecture-dependent minimum instruction length (in bytes).
10180 In this implementation of DWARF, this field is used for information
10181 purposes only. Since GCC generates assembly language, we have no
10182 a priori knowledge of how many instruction bytes are generated for each
10183 source line, and therefore can use only the DW_LNE_set_address and
10184 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10185 this as '1', which is "correct enough" for all architectures,
10186 and don't let the target override. */
10187 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10188
10189 if (ver >= 4)
10190 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10191 "Maximum Operations Per Instruction");
10192 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10193 "Default is_stmt_start flag");
10194 dw2_asm_output_data (1, DWARF_LINE_BASE,
10195 "Line Base Value (Special Opcodes)");
10196 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10197 "Line Range Value (Special Opcodes)");
10198 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10199 "Special Opcode Base");
10200
10201 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10202 {
10203 int n_op_args;
10204 switch (opc)
10205 {
10206 case DW_LNS_advance_pc:
10207 case DW_LNS_advance_line:
10208 case DW_LNS_set_file:
10209 case DW_LNS_set_column:
10210 case DW_LNS_fixed_advance_pc:
10211 case DW_LNS_set_isa:
10212 n_op_args = 1;
10213 break;
10214 default:
10215 n_op_args = 0;
10216 break;
10217 }
10218
10219 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10220 opc, n_op_args);
10221 }
10222
10223 /* Write out the information about the files we use. */
10224 output_file_names ();
10225 ASM_OUTPUT_LABEL (asm_out_file, p2);
10226 if (prologue_only)
10227 {
10228 /* Output the marker for the end of the line number info. */
10229 ASM_OUTPUT_LABEL (asm_out_file, l2);
10230 return;
10231 }
10232
10233 if (separate_line_info)
10234 {
10235 dw_line_info_table *table;
10236 size_t i;
10237
10238 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10239 if (table->in_use)
10240 {
10241 output_one_line_info_table (table);
10242 saw_one = true;
10243 }
10244 }
10245 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10246 {
10247 output_one_line_info_table (cold_text_section_line_info);
10248 saw_one = true;
10249 }
10250
10251 /* ??? Some Darwin linkers crash on a .debug_line section with no
10252 sequences. Further, merely a DW_LNE_end_sequence entry is not
10253 sufficient -- the address column must also be initialized.
10254 Make sure to output at least one set_address/end_sequence pair,
10255 choosing .text since that section is always present. */
10256 if (text_section_line_info->in_use || !saw_one)
10257 output_one_line_info_table (text_section_line_info);
10258
10259 /* Output the marker for the end of the line number info. */
10260 ASM_OUTPUT_LABEL (asm_out_file, l2);
10261 }
10262 \f
10263 /* Given a pointer to a tree node for some base type, return a pointer to
10264 a DIE that describes the given type.
10265
10266 This routine must only be called for GCC type nodes that correspond to
10267 Dwarf base (fundamental) types. */
10268
10269 static dw_die_ref
10270 base_type_die (tree type)
10271 {
10272 dw_die_ref base_type_result;
10273 enum dwarf_type encoding;
10274
10275 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10276 return 0;
10277
10278 /* If this is a subtype that should not be emitted as a subrange type,
10279 use the base type. See subrange_type_for_debug_p. */
10280 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10281 type = TREE_TYPE (type);
10282
10283 switch (TREE_CODE (type))
10284 {
10285 case INTEGER_TYPE:
10286 if ((dwarf_version >= 4 || !dwarf_strict)
10287 && TYPE_NAME (type)
10288 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10289 && DECL_IS_BUILTIN (TYPE_NAME (type))
10290 && DECL_NAME (TYPE_NAME (type)))
10291 {
10292 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10293 if (strcmp (name, "char16_t") == 0
10294 || strcmp (name, "char32_t") == 0)
10295 {
10296 encoding = DW_ATE_UTF;
10297 break;
10298 }
10299 }
10300 if (TYPE_STRING_FLAG (type))
10301 {
10302 if (TYPE_UNSIGNED (type))
10303 encoding = DW_ATE_unsigned_char;
10304 else
10305 encoding = DW_ATE_signed_char;
10306 }
10307 else if (TYPE_UNSIGNED (type))
10308 encoding = DW_ATE_unsigned;
10309 else
10310 encoding = DW_ATE_signed;
10311 break;
10312
10313 case REAL_TYPE:
10314 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10315 {
10316 if (dwarf_version >= 3 || !dwarf_strict)
10317 encoding = DW_ATE_decimal_float;
10318 else
10319 encoding = DW_ATE_lo_user;
10320 }
10321 else
10322 encoding = DW_ATE_float;
10323 break;
10324
10325 case FIXED_POINT_TYPE:
10326 if (!(dwarf_version >= 3 || !dwarf_strict))
10327 encoding = DW_ATE_lo_user;
10328 else if (TYPE_UNSIGNED (type))
10329 encoding = DW_ATE_unsigned_fixed;
10330 else
10331 encoding = DW_ATE_signed_fixed;
10332 break;
10333
10334 /* Dwarf2 doesn't know anything about complex ints, so use
10335 a user defined type for it. */
10336 case COMPLEX_TYPE:
10337 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10338 encoding = DW_ATE_complex_float;
10339 else
10340 encoding = DW_ATE_lo_user;
10341 break;
10342
10343 case BOOLEAN_TYPE:
10344 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10345 encoding = DW_ATE_boolean;
10346 break;
10347
10348 default:
10349 /* No other TREE_CODEs are Dwarf fundamental types. */
10350 gcc_unreachable ();
10351 }
10352
10353 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10354
10355 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10356 int_size_in_bytes (type));
10357 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10358 add_pubtype (type, base_type_result);
10359
10360 return base_type_result;
10361 }
10362
10363 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10364 named 'auto' in its type: return true for it, false otherwise. */
10365
10366 static inline bool
10367 is_cxx_auto (tree type)
10368 {
10369 if (is_cxx ())
10370 {
10371 tree name = TYPE_IDENTIFIER (type);
10372 if (name == get_identifier ("auto")
10373 || name == get_identifier ("decltype(auto)"))
10374 return true;
10375 }
10376 return false;
10377 }
10378
10379 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10380 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10381
10382 static inline int
10383 is_base_type (tree type)
10384 {
10385 switch (TREE_CODE (type))
10386 {
10387 case ERROR_MARK:
10388 case VOID_TYPE:
10389 case INTEGER_TYPE:
10390 case REAL_TYPE:
10391 case FIXED_POINT_TYPE:
10392 case COMPLEX_TYPE:
10393 case BOOLEAN_TYPE:
10394 case POINTER_BOUNDS_TYPE:
10395 return 1;
10396
10397 case ARRAY_TYPE:
10398 case RECORD_TYPE:
10399 case UNION_TYPE:
10400 case QUAL_UNION_TYPE:
10401 case ENUMERAL_TYPE:
10402 case FUNCTION_TYPE:
10403 case METHOD_TYPE:
10404 case POINTER_TYPE:
10405 case REFERENCE_TYPE:
10406 case NULLPTR_TYPE:
10407 case OFFSET_TYPE:
10408 case LANG_TYPE:
10409 case VECTOR_TYPE:
10410 return 0;
10411
10412 default:
10413 if (is_cxx_auto (type))
10414 return 0;
10415 gcc_unreachable ();
10416 }
10417
10418 return 0;
10419 }
10420
10421 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10422 node, return the size in bits for the type if it is a constant, or else
10423 return the alignment for the type if the type's size is not constant, or
10424 else return BITS_PER_WORD if the type actually turns out to be an
10425 ERROR_MARK node. */
10426
10427 static inline unsigned HOST_WIDE_INT
10428 simple_type_size_in_bits (const_tree type)
10429 {
10430 if (TREE_CODE (type) == ERROR_MARK)
10431 return BITS_PER_WORD;
10432 else if (TYPE_SIZE (type) == NULL_TREE)
10433 return 0;
10434 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10435 return tree_to_uhwi (TYPE_SIZE (type));
10436 else
10437 return TYPE_ALIGN (type);
10438 }
10439
10440 /* Similarly, but return an offset_int instead of UHWI. */
10441
10442 static inline offset_int
10443 offset_int_type_size_in_bits (const_tree type)
10444 {
10445 if (TREE_CODE (type) == ERROR_MARK)
10446 return BITS_PER_WORD;
10447 else if (TYPE_SIZE (type) == NULL_TREE)
10448 return 0;
10449 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10450 return wi::to_offset (TYPE_SIZE (type));
10451 else
10452 return TYPE_ALIGN (type);
10453 }
10454
10455 /* Given a pointer to a tree node for a subrange type, return a pointer
10456 to a DIE that describes the given type. */
10457
10458 static dw_die_ref
10459 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10460 {
10461 dw_die_ref subrange_die;
10462 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10463
10464 if (context_die == NULL)
10465 context_die = comp_unit_die ();
10466
10467 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10468
10469 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10470 {
10471 /* The size of the subrange type and its base type do not match,
10472 so we need to generate a size attribute for the subrange type. */
10473 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10474 }
10475
10476 if (low)
10477 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10478 if (high)
10479 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10480
10481 return subrange_die;
10482 }
10483
10484 /* Returns the (const and/or volatile) cv_qualifiers associated with
10485 the decl node. This will normally be augmented with the
10486 cv_qualifiers of the underlying type in add_type_attribute. */
10487
10488 static int
10489 decl_quals (const_tree decl)
10490 {
10491 return ((TREE_READONLY (decl)
10492 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10493 | (TREE_THIS_VOLATILE (decl)
10494 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10495 }
10496
10497 /* Determine the TYPE whose qualifiers match the largest strict subset
10498 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10499 qualifiers outside QUAL_MASK. */
10500
10501 static int
10502 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10503 {
10504 tree t;
10505 int best_rank = 0, best_qual = 0, max_rank;
10506
10507 type_quals &= qual_mask;
10508 max_rank = popcount_hwi (type_quals) - 1;
10509
10510 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10511 t = TYPE_NEXT_VARIANT (t))
10512 {
10513 int q = TYPE_QUALS (t) & qual_mask;
10514
10515 if ((q & type_quals) == q && q != type_quals
10516 && check_base_type (t, type))
10517 {
10518 int rank = popcount_hwi (q);
10519
10520 if (rank > best_rank)
10521 {
10522 best_rank = rank;
10523 best_qual = q;
10524 }
10525 }
10526 }
10527
10528 return best_qual;
10529 }
10530
10531 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10532 entry that chains various modifiers in front of the given type. */
10533
10534 static dw_die_ref
10535 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10536 {
10537 enum tree_code code = TREE_CODE (type);
10538 dw_die_ref mod_type_die;
10539 dw_die_ref sub_die = NULL;
10540 tree item_type = NULL;
10541 tree qualified_type;
10542 tree name, low, high;
10543 dw_die_ref mod_scope;
10544 /* Only these cv-qualifiers are currently handled. */
10545 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10546 | TYPE_QUAL_RESTRICT);
10547
10548 if (code == ERROR_MARK)
10549 return NULL;
10550
10551 cv_quals &= cv_qual_mask;
10552
10553 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10554 tag modifier (and not an attribute) old consumers won't be able
10555 to handle it. */
10556 if (dwarf_version < 3)
10557 cv_quals &= ~TYPE_QUAL_RESTRICT;
10558
10559 /* See if we already have the appropriately qualified variant of
10560 this type. */
10561 qualified_type = get_qualified_type (type, cv_quals);
10562
10563 if (qualified_type == sizetype
10564 && TYPE_NAME (qualified_type)
10565 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10566 {
10567 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10568
10569 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10570 && TYPE_PRECISION (t)
10571 == TYPE_PRECISION (qualified_type)
10572 && TYPE_UNSIGNED (t)
10573 == TYPE_UNSIGNED (qualified_type));
10574 qualified_type = t;
10575 }
10576
10577 /* If we do, then we can just use its DIE, if it exists. */
10578 if (qualified_type)
10579 {
10580 mod_type_die = lookup_type_die (qualified_type);
10581 if (mod_type_die)
10582 return mod_type_die;
10583 }
10584
10585 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10586
10587 /* Handle C typedef types. */
10588 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10589 && !DECL_ARTIFICIAL (name))
10590 {
10591 tree dtype = TREE_TYPE (name);
10592
10593 if (qualified_type == dtype)
10594 {
10595 /* For a named type, use the typedef. */
10596 gen_type_die (qualified_type, context_die);
10597 return lookup_type_die (qualified_type);
10598 }
10599 else
10600 {
10601 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10602 dquals &= cv_qual_mask;
10603 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10604 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10605 /* cv-unqualified version of named type. Just use
10606 the unnamed type to which it refers. */
10607 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10608 cv_quals, context_die);
10609 /* Else cv-qualified version of named type; fall through. */
10610 }
10611 }
10612
10613 mod_scope = scope_die_for (type, context_die);
10614
10615 if (cv_quals)
10616 {
10617 struct qual_info { int q; enum dwarf_tag t; };
10618 static const struct qual_info qual_info[] =
10619 {
10620 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10621 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10622 { TYPE_QUAL_CONST, DW_TAG_const_type },
10623 };
10624 int sub_quals;
10625 unsigned i;
10626
10627 /* Determine a lesser qualified type that most closely matches
10628 this one. Then generate DW_TAG_* entries for the remaining
10629 qualifiers. */
10630 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10631 cv_qual_mask);
10632 mod_type_die = modified_type_die (type, sub_quals, context_die);
10633
10634 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10635 if (qual_info[i].q & cv_quals & ~sub_quals)
10636 {
10637 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10638 if (mod_type_die)
10639 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10640 mod_type_die = d;
10641 }
10642 }
10643 else if (code == POINTER_TYPE)
10644 {
10645 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10646 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10647 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10648 item_type = TREE_TYPE (type);
10649 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10650 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10651 TYPE_ADDR_SPACE (item_type));
10652 }
10653 else if (code == REFERENCE_TYPE)
10654 {
10655 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10656 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10657 type);
10658 else
10659 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10660 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10661 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10662 item_type = TREE_TYPE (type);
10663 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10664 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10665 TYPE_ADDR_SPACE (item_type));
10666 }
10667 else if (code == INTEGER_TYPE
10668 && TREE_TYPE (type) != NULL_TREE
10669 && subrange_type_for_debug_p (type, &low, &high))
10670 {
10671 mod_type_die = subrange_type_die (type, low, high, context_die);
10672 item_type = TREE_TYPE (type);
10673 }
10674 else if (is_base_type (type))
10675 mod_type_die = base_type_die (type);
10676 else
10677 {
10678 gen_type_die (type, context_die);
10679
10680 /* We have to get the type_main_variant here (and pass that to the
10681 `lookup_type_die' routine) because the ..._TYPE node we have
10682 might simply be a *copy* of some original type node (where the
10683 copy was created to help us keep track of typedef names) and
10684 that copy might have a different TYPE_UID from the original
10685 ..._TYPE node. */
10686 if (TREE_CODE (type) != VECTOR_TYPE)
10687 return lookup_type_die (type_main_variant (type));
10688 else
10689 /* Vectors have the debugging information in the type,
10690 not the main variant. */
10691 return lookup_type_die (type);
10692 }
10693
10694 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10695 don't output a DW_TAG_typedef, since there isn't one in the
10696 user's program; just attach a DW_AT_name to the type.
10697 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10698 if the base type already has the same name. */
10699 if (name
10700 && ((TREE_CODE (name) != TYPE_DECL
10701 && (qualified_type == TYPE_MAIN_VARIANT (type)
10702 || (cv_quals == TYPE_UNQUALIFIED)))
10703 || (TREE_CODE (name) == TYPE_DECL
10704 && TREE_TYPE (name) == qualified_type
10705 && DECL_NAME (name))))
10706 {
10707 if (TREE_CODE (name) == TYPE_DECL)
10708 /* Could just call add_name_and_src_coords_attributes here,
10709 but since this is a builtin type it doesn't have any
10710 useful source coordinates anyway. */
10711 name = DECL_NAME (name);
10712 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10713 }
10714 /* This probably indicates a bug. */
10715 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10716 {
10717 name = TYPE_IDENTIFIER (type);
10718 add_name_attribute (mod_type_die,
10719 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10720 }
10721
10722 if (qualified_type)
10723 equate_type_number_to_die (qualified_type, mod_type_die);
10724
10725 if (item_type)
10726 /* We must do this after the equate_type_number_to_die call, in case
10727 this is a recursive type. This ensures that the modified_type_die
10728 recursion will terminate even if the type is recursive. Recursive
10729 types are possible in Ada. */
10730 sub_die = modified_type_die (item_type,
10731 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10732 context_die);
10733
10734 if (sub_die != NULL)
10735 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10736
10737 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10738 if (TYPE_ARTIFICIAL (type))
10739 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10740
10741 return mod_type_die;
10742 }
10743
10744 /* Generate DIEs for the generic parameters of T.
10745 T must be either a generic type or a generic function.
10746 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10747
10748 static void
10749 gen_generic_params_dies (tree t)
10750 {
10751 tree parms, args;
10752 int parms_num, i;
10753 dw_die_ref die = NULL;
10754 int non_default;
10755
10756 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10757 return;
10758
10759 if (TYPE_P (t))
10760 die = lookup_type_die (t);
10761 else if (DECL_P (t))
10762 die = lookup_decl_die (t);
10763
10764 gcc_assert (die);
10765
10766 parms = lang_hooks.get_innermost_generic_parms (t);
10767 if (!parms)
10768 /* T has no generic parameter. It means T is neither a generic type
10769 or function. End of story. */
10770 return;
10771
10772 parms_num = TREE_VEC_LENGTH (parms);
10773 args = lang_hooks.get_innermost_generic_args (t);
10774 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10775 non_default = int_cst_value (TREE_CHAIN (args));
10776 else
10777 non_default = TREE_VEC_LENGTH (args);
10778 for (i = 0; i < parms_num; i++)
10779 {
10780 tree parm, arg, arg_pack_elems;
10781 dw_die_ref parm_die;
10782
10783 parm = TREE_VEC_ELT (parms, i);
10784 arg = TREE_VEC_ELT (args, i);
10785 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10786 gcc_assert (parm && TREE_VALUE (parm) && arg);
10787
10788 if (parm && TREE_VALUE (parm) && arg)
10789 {
10790 /* If PARM represents a template parameter pack,
10791 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10792 by DW_TAG_template_*_parameter DIEs for the argument
10793 pack elements of ARG. Note that ARG would then be
10794 an argument pack. */
10795 if (arg_pack_elems)
10796 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10797 arg_pack_elems,
10798 die);
10799 else
10800 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10801 true /* emit name */, die);
10802 if (i >= non_default)
10803 add_AT_flag (parm_die, DW_AT_default_value, 1);
10804 }
10805 }
10806 }
10807
10808 /* Create and return a DIE for PARM which should be
10809 the representation of a generic type parameter.
10810 For instance, in the C++ front end, PARM would be a template parameter.
10811 ARG is the argument to PARM.
10812 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10813 name of the PARM.
10814 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10815 as a child node. */
10816
10817 static dw_die_ref
10818 generic_parameter_die (tree parm, tree arg,
10819 bool emit_name_p,
10820 dw_die_ref parent_die)
10821 {
10822 dw_die_ref tmpl_die = NULL;
10823 const char *name = NULL;
10824
10825 if (!parm || !DECL_NAME (parm) || !arg)
10826 return NULL;
10827
10828 /* We support non-type generic parameters and arguments,
10829 type generic parameters and arguments, as well as
10830 generic generic parameters (a.k.a. template template parameters in C++)
10831 and arguments. */
10832 if (TREE_CODE (parm) == PARM_DECL)
10833 /* PARM is a nontype generic parameter */
10834 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10835 else if (TREE_CODE (parm) == TYPE_DECL)
10836 /* PARM is a type generic parameter. */
10837 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10838 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10839 /* PARM is a generic generic parameter.
10840 Its DIE is a GNU extension. It shall have a
10841 DW_AT_name attribute to represent the name of the template template
10842 parameter, and a DW_AT_GNU_template_name attribute to represent the
10843 name of the template template argument. */
10844 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10845 parent_die, parm);
10846 else
10847 gcc_unreachable ();
10848
10849 if (tmpl_die)
10850 {
10851 tree tmpl_type;
10852
10853 /* If PARM is a generic parameter pack, it means we are
10854 emitting debug info for a template argument pack element.
10855 In other terms, ARG is a template argument pack element.
10856 In that case, we don't emit any DW_AT_name attribute for
10857 the die. */
10858 if (emit_name_p)
10859 {
10860 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10861 gcc_assert (name);
10862 add_AT_string (tmpl_die, DW_AT_name, name);
10863 }
10864
10865 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10866 {
10867 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10868 TMPL_DIE should have a child DW_AT_type attribute that is set
10869 to the type of the argument to PARM, which is ARG.
10870 If PARM is a type generic parameter, TMPL_DIE should have a
10871 child DW_AT_type that is set to ARG. */
10872 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10873 add_type_attribute (tmpl_die, tmpl_type,
10874 (TREE_THIS_VOLATILE (tmpl_type)
10875 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10876 parent_die);
10877 }
10878 else
10879 {
10880 /* So TMPL_DIE is a DIE representing a
10881 a generic generic template parameter, a.k.a template template
10882 parameter in C++ and arg is a template. */
10883
10884 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10885 to the name of the argument. */
10886 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10887 if (name)
10888 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10889 }
10890
10891 if (TREE_CODE (parm) == PARM_DECL)
10892 /* So PARM is a non-type generic parameter.
10893 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10894 attribute of TMPL_DIE which value represents the value
10895 of ARG.
10896 We must be careful here:
10897 The value of ARG might reference some function decls.
10898 We might currently be emitting debug info for a generic
10899 type and types are emitted before function decls, we don't
10900 know if the function decls referenced by ARG will actually be
10901 emitted after cgraph computations.
10902 So must defer the generation of the DW_AT_const_value to
10903 after cgraph is ready. */
10904 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10905 }
10906
10907 return tmpl_die;
10908 }
10909
10910 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10911 PARM_PACK must be a template parameter pack. The returned DIE
10912 will be child DIE of PARENT_DIE. */
10913
10914 static dw_die_ref
10915 template_parameter_pack_die (tree parm_pack,
10916 tree parm_pack_args,
10917 dw_die_ref parent_die)
10918 {
10919 dw_die_ref die;
10920 int j;
10921
10922 gcc_assert (parent_die && parm_pack);
10923
10924 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10925 add_name_and_src_coords_attributes (die, parm_pack);
10926 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10927 generic_parameter_die (parm_pack,
10928 TREE_VEC_ELT (parm_pack_args, j),
10929 false /* Don't emit DW_AT_name */,
10930 die);
10931 return die;
10932 }
10933
10934 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10935 an enumerated type. */
10936
10937 static inline int
10938 type_is_enum (const_tree type)
10939 {
10940 return TREE_CODE (type) == ENUMERAL_TYPE;
10941 }
10942
10943 /* Return the DBX register number described by a given RTL node. */
10944
10945 static unsigned int
10946 dbx_reg_number (const_rtx rtl)
10947 {
10948 unsigned regno = REGNO (rtl);
10949
10950 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10951
10952 #ifdef LEAF_REG_REMAP
10953 if (crtl->uses_only_leaf_regs)
10954 {
10955 int leaf_reg = LEAF_REG_REMAP (regno);
10956 if (leaf_reg != -1)
10957 regno = (unsigned) leaf_reg;
10958 }
10959 #endif
10960
10961 regno = DBX_REGISTER_NUMBER (regno);
10962 gcc_assert (regno != INVALID_REGNUM);
10963 return regno;
10964 }
10965
10966 /* Optionally add a DW_OP_piece term to a location description expression.
10967 DW_OP_piece is only added if the location description expression already
10968 doesn't end with DW_OP_piece. */
10969
10970 static void
10971 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10972 {
10973 dw_loc_descr_ref loc;
10974
10975 if (*list_head != NULL)
10976 {
10977 /* Find the end of the chain. */
10978 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10979 ;
10980
10981 if (loc->dw_loc_opc != DW_OP_piece)
10982 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10983 }
10984 }
10985
10986 /* Return a location descriptor that designates a machine register or
10987 zero if there is none. */
10988
10989 static dw_loc_descr_ref
10990 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10991 {
10992 rtx regs;
10993
10994 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10995 return 0;
10996
10997 /* We only use "frame base" when we're sure we're talking about the
10998 post-prologue local stack frame. We do this by *not* running
10999 register elimination until this point, and recognizing the special
11000 argument pointer and soft frame pointer rtx's.
11001 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11002 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11003 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11004 {
11005 dw_loc_descr_ref result = NULL;
11006
11007 if (dwarf_version >= 4 || !dwarf_strict)
11008 {
11009 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11010 initialized);
11011 if (result)
11012 add_loc_descr (&result,
11013 new_loc_descr (DW_OP_stack_value, 0, 0));
11014 }
11015 return result;
11016 }
11017
11018 regs = targetm.dwarf_register_span (rtl);
11019
11020 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11021 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11022 else
11023 {
11024 unsigned int dbx_regnum = dbx_reg_number (rtl);
11025 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11026 return 0;
11027 return one_reg_loc_descriptor (dbx_regnum, initialized);
11028 }
11029 }
11030
11031 /* Return a location descriptor that designates a machine register for
11032 a given hard register number. */
11033
11034 static dw_loc_descr_ref
11035 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11036 {
11037 dw_loc_descr_ref reg_loc_descr;
11038
11039 if (regno <= 31)
11040 reg_loc_descr
11041 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11042 else
11043 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11044
11045 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11046 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11047
11048 return reg_loc_descr;
11049 }
11050
11051 /* Given an RTL of a register, return a location descriptor that
11052 designates a value that spans more than one register. */
11053
11054 static dw_loc_descr_ref
11055 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11056 enum var_init_status initialized)
11057 {
11058 int size, i;
11059 dw_loc_descr_ref loc_result = NULL;
11060
11061 /* Simple, contiguous registers. */
11062 if (regs == NULL_RTX)
11063 {
11064 unsigned reg = REGNO (rtl);
11065 int nregs;
11066
11067 #ifdef LEAF_REG_REMAP
11068 if (crtl->uses_only_leaf_regs)
11069 {
11070 int leaf_reg = LEAF_REG_REMAP (reg);
11071 if (leaf_reg != -1)
11072 reg = (unsigned) leaf_reg;
11073 }
11074 #endif
11075
11076 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11077 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11078
11079 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11080
11081 loc_result = NULL;
11082 while (nregs--)
11083 {
11084 dw_loc_descr_ref t;
11085
11086 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11087 VAR_INIT_STATUS_INITIALIZED);
11088 add_loc_descr (&loc_result, t);
11089 add_loc_descr_op_piece (&loc_result, size);
11090 ++reg;
11091 }
11092 return loc_result;
11093 }
11094
11095 /* Now onto stupid register sets in non contiguous locations. */
11096
11097 gcc_assert (GET_CODE (regs) == PARALLEL);
11098
11099 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11100 loc_result = NULL;
11101
11102 for (i = 0; i < XVECLEN (regs, 0); ++i)
11103 {
11104 dw_loc_descr_ref t;
11105
11106 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11107 VAR_INIT_STATUS_INITIALIZED);
11108 add_loc_descr (&loc_result, t);
11109 add_loc_descr_op_piece (&loc_result, size);
11110 }
11111
11112 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11113 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11114 return loc_result;
11115 }
11116
11117 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11118
11119 /* Return a location descriptor that designates a constant i,
11120 as a compound operation from constant (i >> shift), constant shift
11121 and DW_OP_shl. */
11122
11123 static dw_loc_descr_ref
11124 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11125 {
11126 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11127 add_loc_descr (&ret, int_loc_descriptor (shift));
11128 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11129 return ret;
11130 }
11131
11132 /* Return a location descriptor that designates a constant. */
11133
11134 static dw_loc_descr_ref
11135 int_loc_descriptor (HOST_WIDE_INT i)
11136 {
11137 enum dwarf_location_atom op;
11138
11139 /* Pick the smallest representation of a constant, rather than just
11140 defaulting to the LEB encoding. */
11141 if (i >= 0)
11142 {
11143 int clz = clz_hwi (i);
11144 int ctz = ctz_hwi (i);
11145 if (i <= 31)
11146 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11147 else if (i <= 0xff)
11148 op = DW_OP_const1u;
11149 else if (i <= 0xffff)
11150 op = DW_OP_const2u;
11151 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11152 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11153 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11154 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11155 while DW_OP_const4u is 5 bytes. */
11156 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11157 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11158 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11159 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11160 while DW_OP_const4u is 5 bytes. */
11161 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11162 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11163 op = DW_OP_const4u;
11164 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11165 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11166 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11167 while DW_OP_constu of constant >= 0x100000000 takes at least
11168 6 bytes. */
11169 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11170 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11171 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11172 >= HOST_BITS_PER_WIDE_INT)
11173 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11174 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11175 while DW_OP_constu takes in this case at least 6 bytes. */
11176 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11177 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11178 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11179 && size_of_uleb128 (i) > 6)
11180 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11181 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11182 else
11183 op = DW_OP_constu;
11184 }
11185 else
11186 {
11187 if (i >= -0x80)
11188 op = DW_OP_const1s;
11189 else if (i >= -0x8000)
11190 op = DW_OP_const2s;
11191 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11192 {
11193 if (size_of_int_loc_descriptor (i) < 5)
11194 {
11195 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11196 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11197 return ret;
11198 }
11199 op = DW_OP_const4s;
11200 }
11201 else
11202 {
11203 if (size_of_int_loc_descriptor (i)
11204 < (unsigned long) 1 + size_of_sleb128 (i))
11205 {
11206 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11207 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11208 return ret;
11209 }
11210 op = DW_OP_consts;
11211 }
11212 }
11213
11214 return new_loc_descr (op, i, 0);
11215 }
11216
11217 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11218 without actually allocating it. */
11219
11220 static unsigned long
11221 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11222 {
11223 return size_of_int_loc_descriptor (i >> shift)
11224 + size_of_int_loc_descriptor (shift)
11225 + 1;
11226 }
11227
11228 /* Return size_of_locs (int_loc_descriptor (i)) without
11229 actually allocating it. */
11230
11231 static unsigned long
11232 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11233 {
11234 unsigned long s;
11235
11236 if (i >= 0)
11237 {
11238 int clz, ctz;
11239 if (i <= 31)
11240 return 1;
11241 else if (i <= 0xff)
11242 return 2;
11243 else if (i <= 0xffff)
11244 return 3;
11245 clz = clz_hwi (i);
11246 ctz = ctz_hwi (i);
11247 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11248 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11249 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11250 - clz - 5);
11251 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11252 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11253 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11254 - clz - 8);
11255 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11256 return 5;
11257 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11258 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11259 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11260 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11261 - clz - 8);
11262 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11263 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11264 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11265 - clz - 16);
11266 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11267 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11268 && s > 6)
11269 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11270 - clz - 32);
11271 else
11272 return 1 + s;
11273 }
11274 else
11275 {
11276 if (i >= -0x80)
11277 return 2;
11278 else if (i >= -0x8000)
11279 return 3;
11280 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11281 {
11282 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11283 {
11284 s = size_of_int_loc_descriptor (-i) + 1;
11285 if (s < 5)
11286 return s;
11287 }
11288 return 5;
11289 }
11290 else
11291 {
11292 unsigned long r = 1 + size_of_sleb128 (i);
11293 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11294 {
11295 s = size_of_int_loc_descriptor (-i) + 1;
11296 if (s < r)
11297 return s;
11298 }
11299 return r;
11300 }
11301 }
11302 }
11303
11304 /* Return loc description representing "address" of integer value.
11305 This can appear only as toplevel expression. */
11306
11307 static dw_loc_descr_ref
11308 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11309 {
11310 int litsize;
11311 dw_loc_descr_ref loc_result = NULL;
11312
11313 if (!(dwarf_version >= 4 || !dwarf_strict))
11314 return NULL;
11315
11316 litsize = size_of_int_loc_descriptor (i);
11317 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11318 is more compact. For DW_OP_stack_value we need:
11319 litsize + 1 (DW_OP_stack_value)
11320 and for DW_OP_implicit_value:
11321 1 (DW_OP_implicit_value) + 1 (length) + size. */
11322 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11323 {
11324 loc_result = int_loc_descriptor (i);
11325 add_loc_descr (&loc_result,
11326 new_loc_descr (DW_OP_stack_value, 0, 0));
11327 return loc_result;
11328 }
11329
11330 loc_result = new_loc_descr (DW_OP_implicit_value,
11331 size, 0);
11332 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11333 loc_result->dw_loc_oprnd2.v.val_int = i;
11334 return loc_result;
11335 }
11336
11337 /* Return a location descriptor that designates a base+offset location. */
11338
11339 static dw_loc_descr_ref
11340 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11341 enum var_init_status initialized)
11342 {
11343 unsigned int regno;
11344 dw_loc_descr_ref result;
11345 dw_fde_ref fde = cfun->fde;
11346
11347 /* We only use "frame base" when we're sure we're talking about the
11348 post-prologue local stack frame. We do this by *not* running
11349 register elimination until this point, and recognizing the special
11350 argument pointer and soft frame pointer rtx's. */
11351 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11352 {
11353 rtx elim = (ira_use_lra_p
11354 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11355 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11356
11357 if (elim != reg)
11358 {
11359 if (GET_CODE (elim) == PLUS)
11360 {
11361 offset += INTVAL (XEXP (elim, 1));
11362 elim = XEXP (elim, 0);
11363 }
11364 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11365 && (elim == hard_frame_pointer_rtx
11366 || elim == stack_pointer_rtx))
11367 || elim == (frame_pointer_needed
11368 ? hard_frame_pointer_rtx
11369 : stack_pointer_rtx));
11370
11371 /* If drap register is used to align stack, use frame
11372 pointer + offset to access stack variables. If stack
11373 is aligned without drap, use stack pointer + offset to
11374 access stack variables. */
11375 if (crtl->stack_realign_tried
11376 && reg == frame_pointer_rtx)
11377 {
11378 int base_reg
11379 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11380 ? HARD_FRAME_POINTER_REGNUM
11381 : REGNO (elim));
11382 return new_reg_loc_descr (base_reg, offset);
11383 }
11384
11385 gcc_assert (frame_pointer_fb_offset_valid);
11386 offset += frame_pointer_fb_offset;
11387 return new_loc_descr (DW_OP_fbreg, offset, 0);
11388 }
11389 }
11390
11391 regno = REGNO (reg);
11392 #ifdef LEAF_REG_REMAP
11393 if (crtl->uses_only_leaf_regs)
11394 {
11395 int leaf_reg = LEAF_REG_REMAP (regno);
11396 if (leaf_reg != -1)
11397 regno = (unsigned) leaf_reg;
11398 }
11399 #endif
11400 regno = DWARF_FRAME_REGNUM (regno);
11401
11402 if (!optimize && fde
11403 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11404 {
11405 /* Use cfa+offset to represent the location of arguments passed
11406 on the stack when drap is used to align stack.
11407 Only do this when not optimizing, for optimized code var-tracking
11408 is supposed to track where the arguments live and the register
11409 used as vdrap or drap in some spot might be used for something
11410 else in other part of the routine. */
11411 return new_loc_descr (DW_OP_fbreg, offset, 0);
11412 }
11413
11414 if (regno <= 31)
11415 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11416 offset, 0);
11417 else
11418 result = new_loc_descr (DW_OP_bregx, regno, offset);
11419
11420 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11421 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11422
11423 return result;
11424 }
11425
11426 /* Return true if this RTL expression describes a base+offset calculation. */
11427
11428 static inline int
11429 is_based_loc (const_rtx rtl)
11430 {
11431 return (GET_CODE (rtl) == PLUS
11432 && ((REG_P (XEXP (rtl, 0))
11433 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11434 && CONST_INT_P (XEXP (rtl, 1)))));
11435 }
11436
11437 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11438 failed. */
11439
11440 static dw_loc_descr_ref
11441 tls_mem_loc_descriptor (rtx mem)
11442 {
11443 tree base;
11444 dw_loc_descr_ref loc_result;
11445
11446 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11447 return NULL;
11448
11449 base = get_base_address (MEM_EXPR (mem));
11450 if (base == NULL
11451 || TREE_CODE (base) != VAR_DECL
11452 || !DECL_THREAD_LOCAL_P (base))
11453 return NULL;
11454
11455 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11456 if (loc_result == NULL)
11457 return NULL;
11458
11459 if (MEM_OFFSET (mem))
11460 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11461
11462 return loc_result;
11463 }
11464
11465 /* Output debug info about reason why we failed to expand expression as dwarf
11466 expression. */
11467
11468 static void
11469 expansion_failed (tree expr, rtx rtl, char const *reason)
11470 {
11471 if (dump_file && (dump_flags & TDF_DETAILS))
11472 {
11473 fprintf (dump_file, "Failed to expand as dwarf: ");
11474 if (expr)
11475 print_generic_expr (dump_file, expr, dump_flags);
11476 if (rtl)
11477 {
11478 fprintf (dump_file, "\n");
11479 print_rtl (dump_file, rtl);
11480 }
11481 fprintf (dump_file, "\nReason: %s\n", reason);
11482 }
11483 }
11484
11485 /* Helper function for const_ok_for_output. */
11486
11487 static bool
11488 const_ok_for_output_1 (rtx rtl)
11489 {
11490 if (GET_CODE (rtl) == UNSPEC)
11491 {
11492 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11493 we can't express it in the debug info. */
11494 #ifdef ENABLE_CHECKING
11495 /* Don't complain about TLS UNSPECs, those are just too hard to
11496 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11497 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11498 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11499 if (XVECLEN (rtl, 0) == 0
11500 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11501 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11502 inform (current_function_decl
11503 ? DECL_SOURCE_LOCATION (current_function_decl)
11504 : UNKNOWN_LOCATION,
11505 #if NUM_UNSPEC_VALUES > 0
11506 "non-delegitimized UNSPEC %s (%d) found in variable location",
11507 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11508 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11509 XINT (rtl, 1));
11510 #else
11511 "non-delegitimized UNSPEC %d found in variable location",
11512 XINT (rtl, 1));
11513 #endif
11514 #endif
11515 expansion_failed (NULL_TREE, rtl,
11516 "UNSPEC hasn't been delegitimized.\n");
11517 return false;
11518 }
11519
11520 if (targetm.const_not_ok_for_debug_p (rtl))
11521 {
11522 expansion_failed (NULL_TREE, rtl,
11523 "Expression rejected for debug by the backend.\n");
11524 return false;
11525 }
11526
11527 /* FIXME: Refer to PR60655. It is possible for simplification
11528 of rtl expressions in var tracking to produce such expressions.
11529 We should really identify / validate expressions
11530 enclosed in CONST that can be handled by assemblers on various
11531 targets and only handle legitimate cases here. */
11532 if (GET_CODE (rtl) != SYMBOL_REF)
11533 {
11534 if (GET_CODE (rtl) == NOT)
11535 return false;
11536 return true;
11537 }
11538
11539 if (CONSTANT_POOL_ADDRESS_P (rtl))
11540 {
11541 bool marked;
11542 get_pool_constant_mark (rtl, &marked);
11543 /* If all references to this pool constant were optimized away,
11544 it was not output and thus we can't represent it. */
11545 if (!marked)
11546 {
11547 expansion_failed (NULL_TREE, rtl,
11548 "Constant was removed from constant pool.\n");
11549 return false;
11550 }
11551 }
11552
11553 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11554 return false;
11555
11556 /* Avoid references to external symbols in debug info, on several targets
11557 the linker might even refuse to link when linking a shared library,
11558 and in many other cases the relocations for .debug_info/.debug_loc are
11559 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11560 to be defined within the same shared library or executable are fine. */
11561 if (SYMBOL_REF_EXTERNAL_P (rtl))
11562 {
11563 tree decl = SYMBOL_REF_DECL (rtl);
11564
11565 if (decl == NULL || !targetm.binds_local_p (decl))
11566 {
11567 expansion_failed (NULL_TREE, rtl,
11568 "Symbol not defined in current TU.\n");
11569 return false;
11570 }
11571 }
11572
11573 return true;
11574 }
11575
11576 /* Return true if constant RTL can be emitted in DW_OP_addr or
11577 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11578 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11579
11580 static bool
11581 const_ok_for_output (rtx rtl)
11582 {
11583 if (GET_CODE (rtl) == SYMBOL_REF)
11584 return const_ok_for_output_1 (rtl);
11585
11586 if (GET_CODE (rtl) == CONST)
11587 {
11588 subrtx_var_iterator::array_type array;
11589 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11590 if (!const_ok_for_output_1 (*iter))
11591 return false;
11592 return true;
11593 }
11594
11595 return true;
11596 }
11597
11598 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11599 if possible, NULL otherwise. */
11600
11601 static dw_die_ref
11602 base_type_for_mode (machine_mode mode, bool unsignedp)
11603 {
11604 dw_die_ref type_die;
11605 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11606
11607 if (type == NULL)
11608 return NULL;
11609 switch (TREE_CODE (type))
11610 {
11611 case INTEGER_TYPE:
11612 case REAL_TYPE:
11613 break;
11614 default:
11615 return NULL;
11616 }
11617 type_die = lookup_type_die (type);
11618 if (!type_die)
11619 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11620 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11621 return NULL;
11622 return type_die;
11623 }
11624
11625 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11626 type matching MODE, or, if MODE is narrower than or as wide as
11627 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11628 possible. */
11629
11630 static dw_loc_descr_ref
11631 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11632 {
11633 machine_mode outer_mode = mode;
11634 dw_die_ref type_die;
11635 dw_loc_descr_ref cvt;
11636
11637 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11638 {
11639 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11640 return op;
11641 }
11642 type_die = base_type_for_mode (outer_mode, 1);
11643 if (type_die == NULL)
11644 return NULL;
11645 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11646 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11647 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11648 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11649 add_loc_descr (&op, cvt);
11650 return op;
11651 }
11652
11653 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11654
11655 static dw_loc_descr_ref
11656 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11657 dw_loc_descr_ref op1)
11658 {
11659 dw_loc_descr_ref ret = op0;
11660 add_loc_descr (&ret, op1);
11661 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11662 if (STORE_FLAG_VALUE != 1)
11663 {
11664 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11665 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11666 }
11667 return ret;
11668 }
11669
11670 /* Return location descriptor for signed comparison OP RTL. */
11671
11672 static dw_loc_descr_ref
11673 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11674 machine_mode mem_mode)
11675 {
11676 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11677 dw_loc_descr_ref op0, op1;
11678 int shift;
11679
11680 if (op_mode == VOIDmode)
11681 op_mode = GET_MODE (XEXP (rtl, 1));
11682 if (op_mode == VOIDmode)
11683 return NULL;
11684
11685 if (dwarf_strict
11686 && (GET_MODE_CLASS (op_mode) != MODE_INT
11687 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11688 return NULL;
11689
11690 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11691 VAR_INIT_STATUS_INITIALIZED);
11692 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11693 VAR_INIT_STATUS_INITIALIZED);
11694
11695 if (op0 == NULL || op1 == NULL)
11696 return NULL;
11697
11698 if (GET_MODE_CLASS (op_mode) != MODE_INT
11699 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11700 return compare_loc_descriptor (op, op0, op1);
11701
11702 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11703 {
11704 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11705 dw_loc_descr_ref cvt;
11706
11707 if (type_die == NULL)
11708 return NULL;
11709 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11710 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11711 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11712 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11713 add_loc_descr (&op0, cvt);
11714 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11715 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11716 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11717 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11718 add_loc_descr (&op1, cvt);
11719 return compare_loc_descriptor (op, op0, op1);
11720 }
11721
11722 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11723 /* For eq/ne, if the operands are known to be zero-extended,
11724 there is no need to do the fancy shifting up. */
11725 if (op == DW_OP_eq || op == DW_OP_ne)
11726 {
11727 dw_loc_descr_ref last0, last1;
11728 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11729 ;
11730 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11731 ;
11732 /* deref_size zero extends, and for constants we can check
11733 whether they are zero extended or not. */
11734 if (((last0->dw_loc_opc == DW_OP_deref_size
11735 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11736 || (CONST_INT_P (XEXP (rtl, 0))
11737 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11738 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11739 && ((last1->dw_loc_opc == DW_OP_deref_size
11740 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11741 || (CONST_INT_P (XEXP (rtl, 1))
11742 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11743 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11744 return compare_loc_descriptor (op, op0, op1);
11745
11746 /* EQ/NE comparison against constant in narrower type than
11747 DWARF2_ADDR_SIZE can be performed either as
11748 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11749 DW_OP_{eq,ne}
11750 or
11751 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11752 DW_OP_{eq,ne}. Pick whatever is shorter. */
11753 if (CONST_INT_P (XEXP (rtl, 1))
11754 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11755 && (size_of_int_loc_descriptor (shift) + 1
11756 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11757 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11758 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11759 & GET_MODE_MASK (op_mode))))
11760 {
11761 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11762 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11763 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11764 & GET_MODE_MASK (op_mode));
11765 return compare_loc_descriptor (op, op0, op1);
11766 }
11767 }
11768 add_loc_descr (&op0, int_loc_descriptor (shift));
11769 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11770 if (CONST_INT_P (XEXP (rtl, 1)))
11771 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11772 else
11773 {
11774 add_loc_descr (&op1, int_loc_descriptor (shift));
11775 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11776 }
11777 return compare_loc_descriptor (op, op0, op1);
11778 }
11779
11780 /* Return location descriptor for unsigned comparison OP RTL. */
11781
11782 static dw_loc_descr_ref
11783 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11784 machine_mode mem_mode)
11785 {
11786 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11787 dw_loc_descr_ref op0, op1;
11788
11789 if (op_mode == VOIDmode)
11790 op_mode = GET_MODE (XEXP (rtl, 1));
11791 if (op_mode == VOIDmode)
11792 return NULL;
11793 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11794 return NULL;
11795
11796 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11797 return NULL;
11798
11799 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11800 VAR_INIT_STATUS_INITIALIZED);
11801 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11802 VAR_INIT_STATUS_INITIALIZED);
11803
11804 if (op0 == NULL || op1 == NULL)
11805 return NULL;
11806
11807 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11808 {
11809 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11810 dw_loc_descr_ref last0, last1;
11811 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11812 ;
11813 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11814 ;
11815 if (CONST_INT_P (XEXP (rtl, 0)))
11816 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11817 /* deref_size zero extends, so no need to mask it again. */
11818 else if (last0->dw_loc_opc != DW_OP_deref_size
11819 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11820 {
11821 add_loc_descr (&op0, int_loc_descriptor (mask));
11822 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11823 }
11824 if (CONST_INT_P (XEXP (rtl, 1)))
11825 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11826 /* deref_size zero extends, so no need to mask it again. */
11827 else if (last1->dw_loc_opc != DW_OP_deref_size
11828 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11829 {
11830 add_loc_descr (&op1, int_loc_descriptor (mask));
11831 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11832 }
11833 }
11834 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11835 {
11836 HOST_WIDE_INT bias = 1;
11837 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11838 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11839 if (CONST_INT_P (XEXP (rtl, 1)))
11840 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11841 + INTVAL (XEXP (rtl, 1)));
11842 else
11843 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11844 bias, 0));
11845 }
11846 return compare_loc_descriptor (op, op0, op1);
11847 }
11848
11849 /* Return location descriptor for {U,S}{MIN,MAX}. */
11850
11851 static dw_loc_descr_ref
11852 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11853 machine_mode mem_mode)
11854 {
11855 enum dwarf_location_atom op;
11856 dw_loc_descr_ref op0, op1, ret;
11857 dw_loc_descr_ref bra_node, drop_node;
11858
11859 if (dwarf_strict
11860 && (GET_MODE_CLASS (mode) != MODE_INT
11861 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11862 return NULL;
11863
11864 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11865 VAR_INIT_STATUS_INITIALIZED);
11866 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11867 VAR_INIT_STATUS_INITIALIZED);
11868
11869 if (op0 == NULL || op1 == NULL)
11870 return NULL;
11871
11872 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11873 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11874 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11875 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11876 {
11877 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11878 {
11879 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11880 add_loc_descr (&op0, int_loc_descriptor (mask));
11881 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11882 add_loc_descr (&op1, int_loc_descriptor (mask));
11883 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11884 }
11885 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11886 {
11887 HOST_WIDE_INT bias = 1;
11888 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11889 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11890 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11891 }
11892 }
11893 else if (GET_MODE_CLASS (mode) == MODE_INT
11894 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11895 {
11896 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11897 add_loc_descr (&op0, int_loc_descriptor (shift));
11898 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11899 add_loc_descr (&op1, int_loc_descriptor (shift));
11900 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11901 }
11902 else if (GET_MODE_CLASS (mode) == MODE_INT
11903 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11904 {
11905 dw_die_ref type_die = base_type_for_mode (mode, 0);
11906 dw_loc_descr_ref cvt;
11907 if (type_die == NULL)
11908 return NULL;
11909 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11910 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11911 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11912 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11913 add_loc_descr (&op0, cvt);
11914 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11915 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11916 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11917 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11918 add_loc_descr (&op1, cvt);
11919 }
11920
11921 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11922 op = DW_OP_lt;
11923 else
11924 op = DW_OP_gt;
11925 ret = op0;
11926 add_loc_descr (&ret, op1);
11927 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11928 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11929 add_loc_descr (&ret, bra_node);
11930 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11931 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11932 add_loc_descr (&ret, drop_node);
11933 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11934 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11935 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11936 && GET_MODE_CLASS (mode) == MODE_INT
11937 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11938 ret = convert_descriptor_to_mode (mode, ret);
11939 return ret;
11940 }
11941
11942 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11943 but after converting arguments to type_die, afterwards
11944 convert back to unsigned. */
11945
11946 static dw_loc_descr_ref
11947 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11948 machine_mode mode, machine_mode mem_mode)
11949 {
11950 dw_loc_descr_ref cvt, op0, op1;
11951
11952 if (type_die == NULL)
11953 return NULL;
11954 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11955 VAR_INIT_STATUS_INITIALIZED);
11956 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11957 VAR_INIT_STATUS_INITIALIZED);
11958 if (op0 == NULL || op1 == NULL)
11959 return NULL;
11960 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11961 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11962 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11963 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11964 add_loc_descr (&op0, cvt);
11965 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11966 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11967 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11968 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11969 add_loc_descr (&op1, cvt);
11970 add_loc_descr (&op0, op1);
11971 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11972 return convert_descriptor_to_mode (mode, op0);
11973 }
11974
11975 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11976 const0 is DW_OP_lit0 or corresponding typed constant,
11977 const1 is DW_OP_lit1 or corresponding typed constant
11978 and constMSB is constant with just the MSB bit set
11979 for the mode):
11980 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11981 L1: const0 DW_OP_swap
11982 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11983 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11984 L3: DW_OP_drop
11985 L4: DW_OP_nop
11986
11987 CTZ is similar:
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 const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
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 FFS is similar:
11996 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11997 L1: const1 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 static dw_loc_descr_ref
12004 clz_loc_descriptor (rtx rtl, machine_mode mode,
12005 machine_mode mem_mode)
12006 {
12007 dw_loc_descr_ref op0, ret, tmp;
12008 HOST_WIDE_INT valv;
12009 dw_loc_descr_ref l1jump, l1label;
12010 dw_loc_descr_ref l2jump, l2label;
12011 dw_loc_descr_ref l3jump, l3label;
12012 dw_loc_descr_ref l4jump, l4label;
12013 rtx msb;
12014
12015 if (GET_MODE_CLASS (mode) != MODE_INT
12016 || GET_MODE (XEXP (rtl, 0)) != mode)
12017 return NULL;
12018
12019 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12020 VAR_INIT_STATUS_INITIALIZED);
12021 if (op0 == NULL)
12022 return NULL;
12023 ret = op0;
12024 if (GET_CODE (rtl) == CLZ)
12025 {
12026 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12027 valv = GET_MODE_BITSIZE (mode);
12028 }
12029 else if (GET_CODE (rtl) == FFS)
12030 valv = 0;
12031 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12032 valv = GET_MODE_BITSIZE (mode);
12033 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12034 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12035 add_loc_descr (&ret, l1jump);
12036 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12037 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12038 VAR_INIT_STATUS_INITIALIZED);
12039 if (tmp == NULL)
12040 return NULL;
12041 add_loc_descr (&ret, tmp);
12042 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12043 add_loc_descr (&ret, l4jump);
12044 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12045 ? const1_rtx : const0_rtx,
12046 mode, mem_mode,
12047 VAR_INIT_STATUS_INITIALIZED);
12048 if (l1label == NULL)
12049 return NULL;
12050 add_loc_descr (&ret, l1label);
12051 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12052 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12053 add_loc_descr (&ret, l2label);
12054 if (GET_CODE (rtl) != CLZ)
12055 msb = const1_rtx;
12056 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12057 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12058 << (GET_MODE_BITSIZE (mode) - 1));
12059 else
12060 msb = immed_wide_int_const
12061 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12062 GET_MODE_PRECISION (mode)), mode);
12063 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12064 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12065 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12066 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12067 else
12068 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12069 VAR_INIT_STATUS_INITIALIZED);
12070 if (tmp == NULL)
12071 return NULL;
12072 add_loc_descr (&ret, tmp);
12073 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12074 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12075 add_loc_descr (&ret, l3jump);
12076 tmp = mem_loc_descriptor (const1_rtx, 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 (GET_CODE (rtl) == CLZ
12082 ? DW_OP_shl : DW_OP_shr, 0, 0));
12083 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12085 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12086 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12087 add_loc_descr (&ret, l2jump);
12088 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12089 add_loc_descr (&ret, l3label);
12090 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12091 add_loc_descr (&ret, l4label);
12092 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12093 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12094 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12095 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12096 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12097 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12098 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12099 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12100 return ret;
12101 }
12102
12103 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12104 const1 is DW_OP_lit1 or corresponding typed constant):
12105 const0 DW_OP_swap
12106 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12107 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12108 L2: DW_OP_drop
12109
12110 PARITY is similar:
12111 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12112 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12113 L2: DW_OP_drop */
12114
12115 static dw_loc_descr_ref
12116 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12117 machine_mode mem_mode)
12118 {
12119 dw_loc_descr_ref op0, ret, tmp;
12120 dw_loc_descr_ref l1jump, l1label;
12121 dw_loc_descr_ref l2jump, l2label;
12122
12123 if (GET_MODE_CLASS (mode) != MODE_INT
12124 || GET_MODE (XEXP (rtl, 0)) != mode)
12125 return NULL;
12126
12127 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12128 VAR_INIT_STATUS_INITIALIZED);
12129 if (op0 == NULL)
12130 return NULL;
12131 ret = op0;
12132 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12133 VAR_INIT_STATUS_INITIALIZED);
12134 if (tmp == NULL)
12135 return NULL;
12136 add_loc_descr (&ret, tmp);
12137 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12138 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12139 add_loc_descr (&ret, l1label);
12140 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12141 add_loc_descr (&ret, l2jump);
12142 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12143 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12144 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12145 VAR_INIT_STATUS_INITIALIZED);
12146 if (tmp == NULL)
12147 return NULL;
12148 add_loc_descr (&ret, tmp);
12149 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12150 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12151 ? DW_OP_plus : DW_OP_xor, 0, 0));
12152 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12153 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12154 VAR_INIT_STATUS_INITIALIZED);
12155 add_loc_descr (&ret, tmp);
12156 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12157 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12158 add_loc_descr (&ret, l1jump);
12159 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12160 add_loc_descr (&ret, l2label);
12161 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12162 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12163 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12164 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12165 return ret;
12166 }
12167
12168 /* BSWAP (constS is initial shift count, either 56 or 24):
12169 constS const0
12170 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12171 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12172 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12173 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12174 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12175
12176 static dw_loc_descr_ref
12177 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12178 machine_mode mem_mode)
12179 {
12180 dw_loc_descr_ref op0, ret, tmp;
12181 dw_loc_descr_ref l1jump, l1label;
12182 dw_loc_descr_ref l2jump, l2label;
12183
12184 if (GET_MODE_CLASS (mode) != MODE_INT
12185 || BITS_PER_UNIT != 8
12186 || (GET_MODE_BITSIZE (mode) != 32
12187 && GET_MODE_BITSIZE (mode) != 64))
12188 return NULL;
12189
12190 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12191 VAR_INIT_STATUS_INITIALIZED);
12192 if (op0 == NULL)
12193 return NULL;
12194
12195 ret = op0;
12196 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12197 mode, mem_mode,
12198 VAR_INIT_STATUS_INITIALIZED);
12199 if (tmp == NULL)
12200 return NULL;
12201 add_loc_descr (&ret, tmp);
12202 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12203 VAR_INIT_STATUS_INITIALIZED);
12204 if (tmp == NULL)
12205 return NULL;
12206 add_loc_descr (&ret, tmp);
12207 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12208 add_loc_descr (&ret, l1label);
12209 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12210 mode, mem_mode,
12211 VAR_INIT_STATUS_INITIALIZED);
12212 add_loc_descr (&ret, tmp);
12213 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12214 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12215 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12216 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12217 VAR_INIT_STATUS_INITIALIZED);
12218 if (tmp == NULL)
12219 return NULL;
12220 add_loc_descr (&ret, tmp);
12221 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12222 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12223 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12224 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12225 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12226 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12227 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12228 VAR_INIT_STATUS_INITIALIZED);
12229 add_loc_descr (&ret, tmp);
12230 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12231 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12232 add_loc_descr (&ret, l2jump);
12233 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12234 VAR_INIT_STATUS_INITIALIZED);
12235 add_loc_descr (&ret, tmp);
12236 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12237 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12238 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12239 add_loc_descr (&ret, l1jump);
12240 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12241 add_loc_descr (&ret, l2label);
12242 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12243 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12244 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12245 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12246 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12247 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12248 return ret;
12249 }
12250
12251 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12252 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12253 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12254 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12255
12256 ROTATERT is similar:
12257 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12258 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12259 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12260
12261 static dw_loc_descr_ref
12262 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12263 machine_mode mem_mode)
12264 {
12265 rtx rtlop1 = XEXP (rtl, 1);
12266 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12267 int i;
12268
12269 if (GET_MODE_CLASS (mode) != MODE_INT)
12270 return NULL;
12271
12272 if (GET_MODE (rtlop1) != VOIDmode
12273 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12274 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12275 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12276 VAR_INIT_STATUS_INITIALIZED);
12277 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12278 VAR_INIT_STATUS_INITIALIZED);
12279 if (op0 == NULL || op1 == NULL)
12280 return NULL;
12281 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12282 for (i = 0; i < 2; i++)
12283 {
12284 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12285 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12286 mode, mem_mode,
12287 VAR_INIT_STATUS_INITIALIZED);
12288 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12289 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12290 ? DW_OP_const4u
12291 : HOST_BITS_PER_WIDE_INT == 64
12292 ? DW_OP_const8u : DW_OP_constu,
12293 GET_MODE_MASK (mode), 0);
12294 else
12295 mask[i] = NULL;
12296 if (mask[i] == NULL)
12297 return NULL;
12298 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12299 }
12300 ret = op0;
12301 add_loc_descr (&ret, op1);
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12303 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12304 if (GET_CODE (rtl) == ROTATERT)
12305 {
12306 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12307 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12308 GET_MODE_BITSIZE (mode), 0));
12309 }
12310 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12311 if (mask[0] != NULL)
12312 add_loc_descr (&ret, mask[0]);
12313 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12314 if (mask[1] != NULL)
12315 {
12316 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12317 add_loc_descr (&ret, mask[1]);
12318 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12319 }
12320 if (GET_CODE (rtl) == ROTATE)
12321 {
12322 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12323 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12324 GET_MODE_BITSIZE (mode), 0));
12325 }
12326 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12327 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12328 return ret;
12329 }
12330
12331 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12332 for DEBUG_PARAMETER_REF RTL. */
12333
12334 static dw_loc_descr_ref
12335 parameter_ref_descriptor (rtx rtl)
12336 {
12337 dw_loc_descr_ref ret;
12338 dw_die_ref ref;
12339
12340 if (dwarf_strict)
12341 return NULL;
12342 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12343 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12344 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12345 if (ref)
12346 {
12347 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12348 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12349 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12350 }
12351 else
12352 {
12353 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12354 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12355 }
12356 return ret;
12357 }
12358
12359 /* The following routine converts the RTL for a variable or parameter
12360 (resident in memory) into an equivalent Dwarf representation of a
12361 mechanism for getting the address of that same variable onto the top of a
12362 hypothetical "address evaluation" stack.
12363
12364 When creating memory location descriptors, we are effectively transforming
12365 the RTL for a memory-resident object into its Dwarf postfix expression
12366 equivalent. This routine recursively descends an RTL tree, turning
12367 it into Dwarf postfix code as it goes.
12368
12369 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12370
12371 MEM_MODE is the mode of the memory reference, needed to handle some
12372 autoincrement addressing modes.
12373
12374 Return 0 if we can't represent the location. */
12375
12376 dw_loc_descr_ref
12377 mem_loc_descriptor (rtx rtl, machine_mode mode,
12378 machine_mode mem_mode,
12379 enum var_init_status initialized)
12380 {
12381 dw_loc_descr_ref mem_loc_result = NULL;
12382 enum dwarf_location_atom op;
12383 dw_loc_descr_ref op0, op1;
12384 rtx inner = NULL_RTX;
12385
12386 if (mode == VOIDmode)
12387 mode = GET_MODE (rtl);
12388
12389 /* Note that for a dynamically sized array, the location we will generate a
12390 description of here will be the lowest numbered location which is
12391 actually within the array. That's *not* necessarily the same as the
12392 zeroth element of the array. */
12393
12394 rtl = targetm.delegitimize_address (rtl);
12395
12396 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12397 return NULL;
12398
12399 switch (GET_CODE (rtl))
12400 {
12401 case POST_INC:
12402 case POST_DEC:
12403 case POST_MODIFY:
12404 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12405
12406 case SUBREG:
12407 /* The case of a subreg may arise when we have a local (register)
12408 variable or a formal (register) parameter which doesn't quite fill
12409 up an entire register. For now, just assume that it is
12410 legitimate to make the Dwarf info refer to the whole register which
12411 contains the given subreg. */
12412 if (!subreg_lowpart_p (rtl))
12413 break;
12414 inner = SUBREG_REG (rtl);
12415 case TRUNCATE:
12416 if (inner == NULL_RTX)
12417 inner = XEXP (rtl, 0);
12418 if (GET_MODE_CLASS (mode) == MODE_INT
12419 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12420 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12421 #ifdef POINTERS_EXTEND_UNSIGNED
12422 || (mode == Pmode && mem_mode != VOIDmode)
12423 #endif
12424 )
12425 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12426 {
12427 mem_loc_result = mem_loc_descriptor (inner,
12428 GET_MODE (inner),
12429 mem_mode, initialized);
12430 break;
12431 }
12432 if (dwarf_strict)
12433 break;
12434 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12435 break;
12436 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12437 && (GET_MODE_CLASS (mode) != MODE_INT
12438 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12439 break;
12440 else
12441 {
12442 dw_die_ref type_die;
12443 dw_loc_descr_ref cvt;
12444
12445 mem_loc_result = mem_loc_descriptor (inner,
12446 GET_MODE (inner),
12447 mem_mode, initialized);
12448 if (mem_loc_result == NULL)
12449 break;
12450 type_die = base_type_for_mode (mode,
12451 GET_MODE_CLASS (mode) == MODE_INT);
12452 if (type_die == NULL)
12453 {
12454 mem_loc_result = NULL;
12455 break;
12456 }
12457 if (GET_MODE_SIZE (mode)
12458 != GET_MODE_SIZE (GET_MODE (inner)))
12459 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12460 else
12461 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12462 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12463 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12464 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12465 add_loc_descr (&mem_loc_result, cvt);
12466 }
12467 break;
12468
12469 case REG:
12470 if (GET_MODE_CLASS (mode) != MODE_INT
12471 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12472 && rtl != arg_pointer_rtx
12473 && rtl != frame_pointer_rtx
12474 #ifdef POINTERS_EXTEND_UNSIGNED
12475 && (mode != Pmode || mem_mode == VOIDmode)
12476 #endif
12477 ))
12478 {
12479 dw_die_ref type_die;
12480 unsigned int dbx_regnum;
12481
12482 if (dwarf_strict)
12483 break;
12484 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12485 break;
12486 type_die = base_type_for_mode (mode,
12487 GET_MODE_CLASS (mode) == MODE_INT);
12488 if (type_die == NULL)
12489 break;
12490
12491 dbx_regnum = dbx_reg_number (rtl);
12492 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12493 break;
12494 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12495 dbx_regnum, 0);
12496 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12497 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12498 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12499 break;
12500 }
12501 /* Whenever a register number forms a part of the description of the
12502 method for calculating the (dynamic) address of a memory resident
12503 object, DWARF rules require the register number be referred to as
12504 a "base register". This distinction is not based in any way upon
12505 what category of register the hardware believes the given register
12506 belongs to. This is strictly DWARF terminology we're dealing with
12507 here. Note that in cases where the location of a memory-resident
12508 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12509 OP_CONST (0)) the actual DWARF location descriptor that we generate
12510 may just be OP_BASEREG (basereg). This may look deceptively like
12511 the object in question was allocated to a register (rather than in
12512 memory) so DWARF consumers need to be aware of the subtle
12513 distinction between OP_REG and OP_BASEREG. */
12514 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12515 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12516 else if (stack_realign_drap
12517 && crtl->drap_reg
12518 && crtl->args.internal_arg_pointer == rtl
12519 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12520 {
12521 /* If RTL is internal_arg_pointer, which has been optimized
12522 out, use DRAP instead. */
12523 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12524 VAR_INIT_STATUS_INITIALIZED);
12525 }
12526 break;
12527
12528 case SIGN_EXTEND:
12529 case ZERO_EXTEND:
12530 if (GET_MODE_CLASS (mode) != MODE_INT)
12531 break;
12532 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12533 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12534 if (op0 == 0)
12535 break;
12536 else if (GET_CODE (rtl) == ZERO_EXTEND
12537 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12538 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12539 < HOST_BITS_PER_WIDE_INT
12540 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12541 to expand zero extend as two shifts instead of
12542 masking. */
12543 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12544 {
12545 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12546 mem_loc_result = op0;
12547 add_loc_descr (&mem_loc_result,
12548 int_loc_descriptor (GET_MODE_MASK (imode)));
12549 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12550 }
12551 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12552 {
12553 int shift = DWARF2_ADDR_SIZE
12554 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12555 shift *= BITS_PER_UNIT;
12556 if (GET_CODE (rtl) == SIGN_EXTEND)
12557 op = DW_OP_shra;
12558 else
12559 op = DW_OP_shr;
12560 mem_loc_result = op0;
12561 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12562 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12563 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12564 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12565 }
12566 else if (!dwarf_strict)
12567 {
12568 dw_die_ref type_die1, type_die2;
12569 dw_loc_descr_ref cvt;
12570
12571 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12572 GET_CODE (rtl) == ZERO_EXTEND);
12573 if (type_die1 == NULL)
12574 break;
12575 type_die2 = base_type_for_mode (mode, 1);
12576 if (type_die2 == NULL)
12577 break;
12578 mem_loc_result = op0;
12579 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12580 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12581 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12582 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12583 add_loc_descr (&mem_loc_result, cvt);
12584 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12585 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12586 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12587 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12588 add_loc_descr (&mem_loc_result, cvt);
12589 }
12590 break;
12591
12592 case MEM:
12593 {
12594 rtx new_rtl = avoid_constant_pool_reference (rtl);
12595 if (new_rtl != rtl)
12596 {
12597 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12598 initialized);
12599 if (mem_loc_result != NULL)
12600 return mem_loc_result;
12601 }
12602 }
12603 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12604 get_address_mode (rtl), mode,
12605 VAR_INIT_STATUS_INITIALIZED);
12606 if (mem_loc_result == NULL)
12607 mem_loc_result = tls_mem_loc_descriptor (rtl);
12608 if (mem_loc_result != NULL)
12609 {
12610 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12611 || GET_MODE_CLASS (mode) != MODE_INT)
12612 {
12613 dw_die_ref type_die;
12614 dw_loc_descr_ref deref;
12615
12616 if (dwarf_strict)
12617 return NULL;
12618 type_die
12619 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12620 if (type_die == NULL)
12621 return NULL;
12622 deref = new_loc_descr (DW_OP_GNU_deref_type,
12623 GET_MODE_SIZE (mode), 0);
12624 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12625 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12626 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12627 add_loc_descr (&mem_loc_result, deref);
12628 }
12629 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12630 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12631 else
12632 add_loc_descr (&mem_loc_result,
12633 new_loc_descr (DW_OP_deref_size,
12634 GET_MODE_SIZE (mode), 0));
12635 }
12636 break;
12637
12638 case LO_SUM:
12639 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12640
12641 case LABEL_REF:
12642 /* Some ports can transform a symbol ref into a label ref, because
12643 the symbol ref is too far away and has to be dumped into a constant
12644 pool. */
12645 case CONST:
12646 case SYMBOL_REF:
12647 if ((GET_MODE_CLASS (mode) != MODE_INT
12648 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12649 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12650 #ifdef POINTERS_EXTEND_UNSIGNED
12651 && (mode != Pmode || mem_mode == VOIDmode)
12652 #endif
12653 ))
12654 break;
12655 if (GET_CODE (rtl) == SYMBOL_REF
12656 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12657 {
12658 dw_loc_descr_ref temp;
12659
12660 /* If this is not defined, we have no way to emit the data. */
12661 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12662 break;
12663
12664 temp = new_addr_loc_descr (rtl, dtprel_true);
12665
12666 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12667 add_loc_descr (&mem_loc_result, temp);
12668
12669 break;
12670 }
12671
12672 if (!const_ok_for_output (rtl))
12673 break;
12674
12675 symref:
12676 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12677 vec_safe_push (used_rtx_array, rtl);
12678 break;
12679
12680 case CONCAT:
12681 case CONCATN:
12682 case VAR_LOCATION:
12683 case DEBUG_IMPLICIT_PTR:
12684 expansion_failed (NULL_TREE, rtl,
12685 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12686 return 0;
12687
12688 case ENTRY_VALUE:
12689 if (dwarf_strict)
12690 return NULL;
12691 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12692 {
12693 if (GET_MODE_CLASS (mode) != MODE_INT
12694 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12695 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12696 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12697 else
12698 {
12699 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12700 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12701 return NULL;
12702 op0 = one_reg_loc_descriptor (dbx_regnum,
12703 VAR_INIT_STATUS_INITIALIZED);
12704 }
12705 }
12706 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12707 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12708 {
12709 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12710 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12711 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12712 return NULL;
12713 }
12714 else
12715 gcc_unreachable ();
12716 if (op0 == NULL)
12717 return NULL;
12718 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12719 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12720 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12721 break;
12722
12723 case DEBUG_PARAMETER_REF:
12724 mem_loc_result = parameter_ref_descriptor (rtl);
12725 break;
12726
12727 case PRE_MODIFY:
12728 /* Extract the PLUS expression nested inside and fall into
12729 PLUS code below. */
12730 rtl = XEXP (rtl, 1);
12731 goto plus;
12732
12733 case PRE_INC:
12734 case PRE_DEC:
12735 /* Turn these into a PLUS expression and fall into the PLUS code
12736 below. */
12737 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12738 gen_int_mode (GET_CODE (rtl) == PRE_INC
12739 ? GET_MODE_UNIT_SIZE (mem_mode)
12740 : -GET_MODE_UNIT_SIZE (mem_mode),
12741 mode));
12742
12743 /* ... fall through ... */
12744
12745 case PLUS:
12746 plus:
12747 if (is_based_loc (rtl)
12748 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12749 || XEXP (rtl, 0) == arg_pointer_rtx
12750 || XEXP (rtl, 0) == frame_pointer_rtx)
12751 && GET_MODE_CLASS (mode) == MODE_INT)
12752 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12753 INTVAL (XEXP (rtl, 1)),
12754 VAR_INIT_STATUS_INITIALIZED);
12755 else
12756 {
12757 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12758 VAR_INIT_STATUS_INITIALIZED);
12759 if (mem_loc_result == 0)
12760 break;
12761
12762 if (CONST_INT_P (XEXP (rtl, 1))
12763 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12764 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12765 else
12766 {
12767 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12768 VAR_INIT_STATUS_INITIALIZED);
12769 if (op1 == 0)
12770 return NULL;
12771 add_loc_descr (&mem_loc_result, op1);
12772 add_loc_descr (&mem_loc_result,
12773 new_loc_descr (DW_OP_plus, 0, 0));
12774 }
12775 }
12776 break;
12777
12778 /* If a pseudo-reg is optimized away, it is possible for it to
12779 be replaced with a MEM containing a multiply or shift. */
12780 case MINUS:
12781 op = DW_OP_minus;
12782 goto do_binop;
12783
12784 case MULT:
12785 op = DW_OP_mul;
12786 goto do_binop;
12787
12788 case DIV:
12789 if (!dwarf_strict
12790 && GET_MODE_CLASS (mode) == MODE_INT
12791 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12792 {
12793 mem_loc_result = typed_binop (DW_OP_div, rtl,
12794 base_type_for_mode (mode, 0),
12795 mode, mem_mode);
12796 break;
12797 }
12798 op = DW_OP_div;
12799 goto do_binop;
12800
12801 case UMOD:
12802 op = DW_OP_mod;
12803 goto do_binop;
12804
12805 case ASHIFT:
12806 op = DW_OP_shl;
12807 goto do_shift;
12808
12809 case ASHIFTRT:
12810 op = DW_OP_shra;
12811 goto do_shift;
12812
12813 case LSHIFTRT:
12814 op = DW_OP_shr;
12815 goto do_shift;
12816
12817 do_shift:
12818 if (GET_MODE_CLASS (mode) != MODE_INT)
12819 break;
12820 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12821 VAR_INIT_STATUS_INITIALIZED);
12822 {
12823 rtx rtlop1 = XEXP (rtl, 1);
12824 if (GET_MODE (rtlop1) != VOIDmode
12825 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12826 < GET_MODE_BITSIZE (mode))
12827 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12828 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12829 VAR_INIT_STATUS_INITIALIZED);
12830 }
12831
12832 if (op0 == 0 || op1 == 0)
12833 break;
12834
12835 mem_loc_result = op0;
12836 add_loc_descr (&mem_loc_result, op1);
12837 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12838 break;
12839
12840 case AND:
12841 op = DW_OP_and;
12842 goto do_binop;
12843
12844 case IOR:
12845 op = DW_OP_or;
12846 goto do_binop;
12847
12848 case XOR:
12849 op = DW_OP_xor;
12850 goto do_binop;
12851
12852 do_binop:
12853 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12854 VAR_INIT_STATUS_INITIALIZED);
12855 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12856 VAR_INIT_STATUS_INITIALIZED);
12857
12858 if (op0 == 0 || op1 == 0)
12859 break;
12860
12861 mem_loc_result = op0;
12862 add_loc_descr (&mem_loc_result, op1);
12863 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12864 break;
12865
12866 case MOD:
12867 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12868 {
12869 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12870 base_type_for_mode (mode, 0),
12871 mode, mem_mode);
12872 break;
12873 }
12874
12875 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12876 VAR_INIT_STATUS_INITIALIZED);
12877 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12878 VAR_INIT_STATUS_INITIALIZED);
12879
12880 if (op0 == 0 || op1 == 0)
12881 break;
12882
12883 mem_loc_result = op0;
12884 add_loc_descr (&mem_loc_result, op1);
12885 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12886 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12887 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12888 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12889 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12890 break;
12891
12892 case UDIV:
12893 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12894 {
12895 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12896 {
12897 op = DW_OP_div;
12898 goto do_binop;
12899 }
12900 mem_loc_result = typed_binop (DW_OP_div, rtl,
12901 base_type_for_mode (mode, 1),
12902 mode, mem_mode);
12903 }
12904 break;
12905
12906 case NOT:
12907 op = DW_OP_not;
12908 goto do_unop;
12909
12910 case ABS:
12911 op = DW_OP_abs;
12912 goto do_unop;
12913
12914 case NEG:
12915 op = DW_OP_neg;
12916 goto do_unop;
12917
12918 do_unop:
12919 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12920 VAR_INIT_STATUS_INITIALIZED);
12921
12922 if (op0 == 0)
12923 break;
12924
12925 mem_loc_result = op0;
12926 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12927 break;
12928
12929 case CONST_INT:
12930 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12931 #ifdef POINTERS_EXTEND_UNSIGNED
12932 || (mode == Pmode
12933 && mem_mode != VOIDmode
12934 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12935 #endif
12936 )
12937 {
12938 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12939 break;
12940 }
12941 if (!dwarf_strict
12942 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12943 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12944 {
12945 dw_die_ref type_die = base_type_for_mode (mode, 1);
12946 machine_mode amode;
12947 if (type_die == NULL)
12948 return NULL;
12949 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12950 MODE_INT, 0);
12951 if (INTVAL (rtl) >= 0
12952 && amode != BLKmode
12953 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12954 /* const DW_OP_GNU_convert <XXX> vs.
12955 DW_OP_GNU_const_type <XXX, 1, const>. */
12956 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12957 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12958 {
12959 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12960 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12961 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12962 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12963 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12964 add_loc_descr (&mem_loc_result, op0);
12965 return mem_loc_result;
12966 }
12967 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12968 INTVAL (rtl));
12969 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12970 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12971 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12972 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12973 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12974 else
12975 {
12976 mem_loc_result->dw_loc_oprnd2.val_class
12977 = dw_val_class_const_double;
12978 mem_loc_result->dw_loc_oprnd2.v.val_double
12979 = double_int::from_shwi (INTVAL (rtl));
12980 }
12981 }
12982 break;
12983
12984 case CONST_DOUBLE:
12985 if (!dwarf_strict)
12986 {
12987 dw_die_ref type_die;
12988
12989 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12990 CONST_DOUBLE rtx could represent either a large integer
12991 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12992 the value is always a floating point constant.
12993
12994 When it is an integer, a CONST_DOUBLE is used whenever
12995 the constant requires 2 HWIs to be adequately represented.
12996 We output CONST_DOUBLEs as blocks. */
12997 if (mode == VOIDmode
12998 || (GET_MODE (rtl) == VOIDmode
12999 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13000 break;
13001 type_die = base_type_for_mode (mode,
13002 GET_MODE_CLASS (mode) == MODE_INT);
13003 if (type_die == NULL)
13004 return NULL;
13005 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13006 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13007 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13008 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13009 #if TARGET_SUPPORTS_WIDE_INT == 0
13010 if (!SCALAR_FLOAT_MODE_P (mode))
13011 {
13012 mem_loc_result->dw_loc_oprnd2.val_class
13013 = dw_val_class_const_double;
13014 mem_loc_result->dw_loc_oprnd2.v.val_double
13015 = rtx_to_double_int (rtl);
13016 }
13017 else
13018 #endif
13019 {
13020 unsigned int length = GET_MODE_SIZE (mode);
13021 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13022
13023 insert_float (rtl, array);
13024 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13025 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13026 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13027 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13028 }
13029 }
13030 break;
13031
13032 case CONST_WIDE_INT:
13033 if (!dwarf_strict)
13034 {
13035 dw_die_ref type_die;
13036
13037 type_die = base_type_for_mode (mode,
13038 GET_MODE_CLASS (mode) == MODE_INT);
13039 if (type_die == NULL)
13040 return NULL;
13041 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13042 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13043 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13044 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13045 mem_loc_result->dw_loc_oprnd2.val_class
13046 = dw_val_class_wide_int;
13047 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13048 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13049 }
13050 break;
13051
13052 case EQ:
13053 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13054 break;
13055
13056 case GE:
13057 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13058 break;
13059
13060 case GT:
13061 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13062 break;
13063
13064 case LE:
13065 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13066 break;
13067
13068 case LT:
13069 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13070 break;
13071
13072 case NE:
13073 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13074 break;
13075
13076 case GEU:
13077 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13078 break;
13079
13080 case GTU:
13081 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13082 break;
13083
13084 case LEU:
13085 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13086 break;
13087
13088 case LTU:
13089 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13090 break;
13091
13092 case UMIN:
13093 case UMAX:
13094 if (GET_MODE_CLASS (mode) != MODE_INT)
13095 break;
13096 /* FALLTHRU */
13097 case SMIN:
13098 case SMAX:
13099 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13100 break;
13101
13102 case ZERO_EXTRACT:
13103 case SIGN_EXTRACT:
13104 if (CONST_INT_P (XEXP (rtl, 1))
13105 && CONST_INT_P (XEXP (rtl, 2))
13106 && ((unsigned) INTVAL (XEXP (rtl, 1))
13107 + (unsigned) INTVAL (XEXP (rtl, 2))
13108 <= GET_MODE_BITSIZE (mode))
13109 && GET_MODE_CLASS (mode) == MODE_INT
13110 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13111 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13112 {
13113 int shift, size;
13114 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13115 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13116 if (op0 == 0)
13117 break;
13118 if (GET_CODE (rtl) == SIGN_EXTRACT)
13119 op = DW_OP_shra;
13120 else
13121 op = DW_OP_shr;
13122 mem_loc_result = op0;
13123 size = INTVAL (XEXP (rtl, 1));
13124 shift = INTVAL (XEXP (rtl, 2));
13125 if (BITS_BIG_ENDIAN)
13126 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13127 - shift - size;
13128 if (shift + size != (int) DWARF2_ADDR_SIZE)
13129 {
13130 add_loc_descr (&mem_loc_result,
13131 int_loc_descriptor (DWARF2_ADDR_SIZE
13132 - shift - size));
13133 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13134 }
13135 if (size != (int) DWARF2_ADDR_SIZE)
13136 {
13137 add_loc_descr (&mem_loc_result,
13138 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13139 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13140 }
13141 }
13142 break;
13143
13144 case IF_THEN_ELSE:
13145 {
13146 dw_loc_descr_ref op2, bra_node, drop_node;
13147 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13148 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13149 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13150 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13151 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13152 VAR_INIT_STATUS_INITIALIZED);
13153 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13154 VAR_INIT_STATUS_INITIALIZED);
13155 if (op0 == NULL || op1 == NULL || op2 == NULL)
13156 break;
13157
13158 mem_loc_result = op1;
13159 add_loc_descr (&mem_loc_result, op2);
13160 add_loc_descr (&mem_loc_result, op0);
13161 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13162 add_loc_descr (&mem_loc_result, bra_node);
13163 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13164 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13165 add_loc_descr (&mem_loc_result, drop_node);
13166 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13167 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13168 }
13169 break;
13170
13171 case FLOAT_EXTEND:
13172 case FLOAT_TRUNCATE:
13173 case FLOAT:
13174 case UNSIGNED_FLOAT:
13175 case FIX:
13176 case UNSIGNED_FIX:
13177 if (!dwarf_strict)
13178 {
13179 dw_die_ref type_die;
13180 dw_loc_descr_ref cvt;
13181
13182 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13183 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13184 if (op0 == NULL)
13185 break;
13186 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13187 && (GET_CODE (rtl) == FLOAT
13188 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13189 <= DWARF2_ADDR_SIZE))
13190 {
13191 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13192 GET_CODE (rtl) == UNSIGNED_FLOAT);
13193 if (type_die == NULL)
13194 break;
13195 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13196 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13197 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13198 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13199 add_loc_descr (&op0, cvt);
13200 }
13201 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13202 if (type_die == NULL)
13203 break;
13204 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13205 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13206 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13207 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13208 add_loc_descr (&op0, cvt);
13209 if (GET_MODE_CLASS (mode) == MODE_INT
13210 && (GET_CODE (rtl) == FIX
13211 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13212 {
13213 op0 = convert_descriptor_to_mode (mode, op0);
13214 if (op0 == NULL)
13215 break;
13216 }
13217 mem_loc_result = op0;
13218 }
13219 break;
13220
13221 case CLZ:
13222 case CTZ:
13223 case FFS:
13224 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13225 break;
13226
13227 case POPCOUNT:
13228 case PARITY:
13229 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13230 break;
13231
13232 case BSWAP:
13233 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13234 break;
13235
13236 case ROTATE:
13237 case ROTATERT:
13238 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13239 break;
13240
13241 case COMPARE:
13242 /* In theory, we could implement the above. */
13243 /* DWARF cannot represent the unsigned compare operations
13244 natively. */
13245 case SS_MULT:
13246 case US_MULT:
13247 case SS_DIV:
13248 case US_DIV:
13249 case SS_PLUS:
13250 case US_PLUS:
13251 case SS_MINUS:
13252 case US_MINUS:
13253 case SS_NEG:
13254 case US_NEG:
13255 case SS_ABS:
13256 case SS_ASHIFT:
13257 case US_ASHIFT:
13258 case SS_TRUNCATE:
13259 case US_TRUNCATE:
13260 case UNORDERED:
13261 case ORDERED:
13262 case UNEQ:
13263 case UNGE:
13264 case UNGT:
13265 case UNLE:
13266 case UNLT:
13267 case LTGT:
13268 case FRACT_CONVERT:
13269 case UNSIGNED_FRACT_CONVERT:
13270 case SAT_FRACT:
13271 case UNSIGNED_SAT_FRACT:
13272 case SQRT:
13273 case ASM_OPERANDS:
13274 case VEC_MERGE:
13275 case VEC_SELECT:
13276 case VEC_CONCAT:
13277 case VEC_DUPLICATE:
13278 case UNSPEC:
13279 case HIGH:
13280 case FMA:
13281 case STRICT_LOW_PART:
13282 case CONST_VECTOR:
13283 case CONST_FIXED:
13284 case CLRSB:
13285 case CLOBBER:
13286 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13287 can't express it in the debug info. This can happen e.g. with some
13288 TLS UNSPECs. */
13289 break;
13290
13291 case CONST_STRING:
13292 resolve_one_addr (&rtl);
13293 goto symref;
13294
13295 default:
13296 #ifdef ENABLE_CHECKING
13297 print_rtl (stderr, rtl);
13298 gcc_unreachable ();
13299 #else
13300 break;
13301 #endif
13302 }
13303
13304 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13305 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13306
13307 return mem_loc_result;
13308 }
13309
13310 /* Return a descriptor that describes the concatenation of two locations.
13311 This is typically a complex variable. */
13312
13313 static dw_loc_descr_ref
13314 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13315 {
13316 dw_loc_descr_ref cc_loc_result = NULL;
13317 dw_loc_descr_ref x0_ref
13318 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13319 dw_loc_descr_ref x1_ref
13320 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13321
13322 if (x0_ref == 0 || x1_ref == 0)
13323 return 0;
13324
13325 cc_loc_result = x0_ref;
13326 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13327
13328 add_loc_descr (&cc_loc_result, x1_ref);
13329 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13330
13331 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13332 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13333
13334 return cc_loc_result;
13335 }
13336
13337 /* Return a descriptor that describes the concatenation of N
13338 locations. */
13339
13340 static dw_loc_descr_ref
13341 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13342 {
13343 unsigned int i;
13344 dw_loc_descr_ref cc_loc_result = NULL;
13345 unsigned int n = XVECLEN (concatn, 0);
13346
13347 for (i = 0; i < n; ++i)
13348 {
13349 dw_loc_descr_ref ref;
13350 rtx x = XVECEXP (concatn, 0, i);
13351
13352 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13353 if (ref == NULL)
13354 return NULL;
13355
13356 add_loc_descr (&cc_loc_result, ref);
13357 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13358 }
13359
13360 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13361 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13362
13363 return cc_loc_result;
13364 }
13365
13366 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13367 for DEBUG_IMPLICIT_PTR RTL. */
13368
13369 static dw_loc_descr_ref
13370 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13371 {
13372 dw_loc_descr_ref ret;
13373 dw_die_ref ref;
13374
13375 if (dwarf_strict)
13376 return NULL;
13377 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13378 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13379 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13380 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13381 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13382 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13383 if (ref)
13384 {
13385 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13386 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13387 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13388 }
13389 else
13390 {
13391 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13392 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13393 }
13394 return ret;
13395 }
13396
13397 /* Output a proper Dwarf location descriptor for a variable or parameter
13398 which is either allocated in a register or in a memory location. For a
13399 register, we just generate an OP_REG and the register number. For a
13400 memory location we provide a Dwarf postfix expression describing how to
13401 generate the (dynamic) address of the object onto the address stack.
13402
13403 MODE is mode of the decl if this loc_descriptor is going to be used in
13404 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13405 allowed, VOIDmode otherwise.
13406
13407 If we don't know how to describe it, return 0. */
13408
13409 static dw_loc_descr_ref
13410 loc_descriptor (rtx rtl, machine_mode mode,
13411 enum var_init_status initialized)
13412 {
13413 dw_loc_descr_ref loc_result = NULL;
13414
13415 switch (GET_CODE (rtl))
13416 {
13417 case SUBREG:
13418 /* The case of a subreg may arise when we have a local (register)
13419 variable or a formal (register) parameter which doesn't quite fill
13420 up an entire register. For now, just assume that it is
13421 legitimate to make the Dwarf info refer to the whole register which
13422 contains the given subreg. */
13423 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13424 loc_result = loc_descriptor (SUBREG_REG (rtl),
13425 GET_MODE (SUBREG_REG (rtl)), initialized);
13426 else
13427 goto do_default;
13428 break;
13429
13430 case REG:
13431 loc_result = reg_loc_descriptor (rtl, initialized);
13432 break;
13433
13434 case MEM:
13435 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13436 GET_MODE (rtl), initialized);
13437 if (loc_result == NULL)
13438 loc_result = tls_mem_loc_descriptor (rtl);
13439 if (loc_result == NULL)
13440 {
13441 rtx new_rtl = avoid_constant_pool_reference (rtl);
13442 if (new_rtl != rtl)
13443 loc_result = loc_descriptor (new_rtl, mode, initialized);
13444 }
13445 break;
13446
13447 case CONCAT:
13448 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13449 initialized);
13450 break;
13451
13452 case CONCATN:
13453 loc_result = concatn_loc_descriptor (rtl, initialized);
13454 break;
13455
13456 case VAR_LOCATION:
13457 /* Single part. */
13458 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13459 {
13460 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13461 if (GET_CODE (loc) == EXPR_LIST)
13462 loc = XEXP (loc, 0);
13463 loc_result = loc_descriptor (loc, mode, initialized);
13464 break;
13465 }
13466
13467 rtl = XEXP (rtl, 1);
13468 /* FALLTHRU */
13469
13470 case PARALLEL:
13471 {
13472 rtvec par_elems = XVEC (rtl, 0);
13473 int num_elem = GET_NUM_ELEM (par_elems);
13474 machine_mode mode;
13475 int i;
13476
13477 /* Create the first one, so we have something to add to. */
13478 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13479 VOIDmode, initialized);
13480 if (loc_result == NULL)
13481 return NULL;
13482 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13483 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13484 for (i = 1; i < num_elem; i++)
13485 {
13486 dw_loc_descr_ref temp;
13487
13488 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13489 VOIDmode, initialized);
13490 if (temp == NULL)
13491 return NULL;
13492 add_loc_descr (&loc_result, temp);
13493 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13494 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13495 }
13496 }
13497 break;
13498
13499 case CONST_INT:
13500 if (mode != VOIDmode && mode != BLKmode)
13501 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13502 INTVAL (rtl));
13503 break;
13504
13505 case CONST_DOUBLE:
13506 if (mode == VOIDmode)
13507 mode = GET_MODE (rtl);
13508
13509 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13510 {
13511 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13512
13513 /* Note that a CONST_DOUBLE rtx could represent either an integer
13514 or a floating-point constant. A CONST_DOUBLE is used whenever
13515 the constant requires more than one word in order to be
13516 adequately represented. We output CONST_DOUBLEs as blocks. */
13517 loc_result = new_loc_descr (DW_OP_implicit_value,
13518 GET_MODE_SIZE (mode), 0);
13519 #if TARGET_SUPPORTS_WIDE_INT == 0
13520 if (!SCALAR_FLOAT_MODE_P (mode))
13521 {
13522 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13523 loc_result->dw_loc_oprnd2.v.val_double
13524 = rtx_to_double_int (rtl);
13525 }
13526 else
13527 #endif
13528 {
13529 unsigned int length = GET_MODE_SIZE (mode);
13530 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13531
13532 insert_float (rtl, array);
13533 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13534 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13535 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13536 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13537 }
13538 }
13539 break;
13540
13541 case CONST_WIDE_INT:
13542 if (mode == VOIDmode)
13543 mode = GET_MODE (rtl);
13544
13545 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13546 {
13547 loc_result = new_loc_descr (DW_OP_implicit_value,
13548 GET_MODE_SIZE (mode), 0);
13549 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13550 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13551 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13552 }
13553 break;
13554
13555 case CONST_VECTOR:
13556 if (mode == VOIDmode)
13557 mode = GET_MODE (rtl);
13558
13559 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13560 {
13561 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13562 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13563 unsigned char *array
13564 = ggc_vec_alloc<unsigned char> (length * elt_size);
13565 unsigned int i;
13566 unsigned char *p;
13567 machine_mode imode = GET_MODE_INNER (mode);
13568
13569 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13570 switch (GET_MODE_CLASS (mode))
13571 {
13572 case MODE_VECTOR_INT:
13573 for (i = 0, p = array; i < length; i++, p += elt_size)
13574 {
13575 rtx elt = CONST_VECTOR_ELT (rtl, i);
13576 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13577 }
13578 break;
13579
13580 case MODE_VECTOR_FLOAT:
13581 for (i = 0, p = array; i < length; i++, p += elt_size)
13582 {
13583 rtx elt = CONST_VECTOR_ELT (rtl, i);
13584 insert_float (elt, p);
13585 }
13586 break;
13587
13588 default:
13589 gcc_unreachable ();
13590 }
13591
13592 loc_result = new_loc_descr (DW_OP_implicit_value,
13593 length * elt_size, 0);
13594 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13595 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13596 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13597 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13598 }
13599 break;
13600
13601 case CONST:
13602 if (mode == VOIDmode
13603 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13604 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13605 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13606 {
13607 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13608 break;
13609 }
13610 /* FALLTHROUGH */
13611 case SYMBOL_REF:
13612 if (!const_ok_for_output (rtl))
13613 break;
13614 case LABEL_REF:
13615 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13616 && (dwarf_version >= 4 || !dwarf_strict))
13617 {
13618 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13619 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13620 vec_safe_push (used_rtx_array, rtl);
13621 }
13622 break;
13623
13624 case DEBUG_IMPLICIT_PTR:
13625 loc_result = implicit_ptr_descriptor (rtl, 0);
13626 break;
13627
13628 case PLUS:
13629 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13630 && CONST_INT_P (XEXP (rtl, 1)))
13631 {
13632 loc_result
13633 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13634 break;
13635 }
13636 /* FALLTHRU */
13637 do_default:
13638 default:
13639 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13640 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13641 && dwarf_version >= 4)
13642 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13643 {
13644 /* Value expression. */
13645 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13646 if (loc_result)
13647 add_loc_descr (&loc_result,
13648 new_loc_descr (DW_OP_stack_value, 0, 0));
13649 }
13650 break;
13651 }
13652
13653 return loc_result;
13654 }
13655
13656 /* We need to figure out what section we should use as the base for the
13657 address ranges where a given location is valid.
13658 1. If this particular DECL has a section associated with it, use that.
13659 2. If this function has a section associated with it, use that.
13660 3. Otherwise, use the text section.
13661 XXX: If you split a variable across multiple sections, we won't notice. */
13662
13663 static const char *
13664 secname_for_decl (const_tree decl)
13665 {
13666 const char *secname;
13667
13668 if (VAR_OR_FUNCTION_DECL_P (decl)
13669 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13670 && DECL_SECTION_NAME (decl))
13671 secname = DECL_SECTION_NAME (decl);
13672 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13673 secname = DECL_SECTION_NAME (current_function_decl);
13674 else if (cfun && in_cold_section_p)
13675 secname = crtl->subsections.cold_section_label;
13676 else
13677 secname = text_section_label;
13678
13679 return secname;
13680 }
13681
13682 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13683
13684 static bool
13685 decl_by_reference_p (tree decl)
13686 {
13687 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13688 || TREE_CODE (decl) == VAR_DECL)
13689 && DECL_BY_REFERENCE (decl));
13690 }
13691
13692 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13693 for VARLOC. */
13694
13695 static dw_loc_descr_ref
13696 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13697 enum var_init_status initialized)
13698 {
13699 int have_address = 0;
13700 dw_loc_descr_ref descr;
13701 machine_mode mode;
13702
13703 if (want_address != 2)
13704 {
13705 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13706 /* Single part. */
13707 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13708 {
13709 varloc = PAT_VAR_LOCATION_LOC (varloc);
13710 if (GET_CODE (varloc) == EXPR_LIST)
13711 varloc = XEXP (varloc, 0);
13712 mode = GET_MODE (varloc);
13713 if (MEM_P (varloc))
13714 {
13715 rtx addr = XEXP (varloc, 0);
13716 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13717 mode, initialized);
13718 if (descr)
13719 have_address = 1;
13720 else
13721 {
13722 rtx x = avoid_constant_pool_reference (varloc);
13723 if (x != varloc)
13724 descr = mem_loc_descriptor (x, mode, VOIDmode,
13725 initialized);
13726 }
13727 }
13728 else
13729 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13730 }
13731 else
13732 return 0;
13733 }
13734 else
13735 {
13736 if (GET_CODE (varloc) == VAR_LOCATION)
13737 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13738 else
13739 mode = DECL_MODE (loc);
13740 descr = loc_descriptor (varloc, mode, initialized);
13741 have_address = 1;
13742 }
13743
13744 if (!descr)
13745 return 0;
13746
13747 if (want_address == 2 && !have_address
13748 && (dwarf_version >= 4 || !dwarf_strict))
13749 {
13750 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13751 {
13752 expansion_failed (loc, NULL_RTX,
13753 "DWARF address size mismatch");
13754 return 0;
13755 }
13756 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13757 have_address = 1;
13758 }
13759 /* Show if we can't fill the request for an address. */
13760 if (want_address && !have_address)
13761 {
13762 expansion_failed (loc, NULL_RTX,
13763 "Want address and only have value");
13764 return 0;
13765 }
13766
13767 /* If we've got an address and don't want one, dereference. */
13768 if (!want_address && have_address)
13769 {
13770 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13771 enum dwarf_location_atom op;
13772
13773 if (size > DWARF2_ADDR_SIZE || size == -1)
13774 {
13775 expansion_failed (loc, NULL_RTX,
13776 "DWARF address size mismatch");
13777 return 0;
13778 }
13779 else if (size == DWARF2_ADDR_SIZE)
13780 op = DW_OP_deref;
13781 else
13782 op = DW_OP_deref_size;
13783
13784 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13785 }
13786
13787 return descr;
13788 }
13789
13790 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13791 if it is not possible. */
13792
13793 static dw_loc_descr_ref
13794 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13795 {
13796 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13797 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13798 else if (dwarf_version >= 3 || !dwarf_strict)
13799 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13800 else
13801 return NULL;
13802 }
13803
13804 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13805 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13806
13807 static dw_loc_descr_ref
13808 dw_sra_loc_expr (tree decl, rtx loc)
13809 {
13810 rtx p;
13811 unsigned int padsize = 0;
13812 dw_loc_descr_ref descr, *descr_tail;
13813 unsigned HOST_WIDE_INT decl_size;
13814 rtx varloc;
13815 enum var_init_status initialized;
13816
13817 if (DECL_SIZE (decl) == NULL
13818 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13819 return NULL;
13820
13821 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13822 descr = NULL;
13823 descr_tail = &descr;
13824
13825 for (p = loc; p; p = XEXP (p, 1))
13826 {
13827 unsigned int bitsize = decl_piece_bitsize (p);
13828 rtx loc_note = *decl_piece_varloc_ptr (p);
13829 dw_loc_descr_ref cur_descr;
13830 dw_loc_descr_ref *tail, last = NULL;
13831 unsigned int opsize = 0;
13832
13833 if (loc_note == NULL_RTX
13834 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13835 {
13836 padsize += bitsize;
13837 continue;
13838 }
13839 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13840 varloc = NOTE_VAR_LOCATION (loc_note);
13841 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13842 if (cur_descr == NULL)
13843 {
13844 padsize += bitsize;
13845 continue;
13846 }
13847
13848 /* Check that cur_descr either doesn't use
13849 DW_OP_*piece operations, or their sum is equal
13850 to bitsize. Otherwise we can't embed it. */
13851 for (tail = &cur_descr; *tail != NULL;
13852 tail = &(*tail)->dw_loc_next)
13853 if ((*tail)->dw_loc_opc == DW_OP_piece)
13854 {
13855 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13856 * BITS_PER_UNIT;
13857 last = *tail;
13858 }
13859 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13860 {
13861 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13862 last = *tail;
13863 }
13864
13865 if (last != NULL && opsize != bitsize)
13866 {
13867 padsize += bitsize;
13868 /* Discard the current piece of the descriptor and release any
13869 addr_table entries it uses. */
13870 remove_loc_list_addr_table_entries (cur_descr);
13871 continue;
13872 }
13873
13874 /* If there is a hole, add DW_OP_*piece after empty DWARF
13875 expression, which means that those bits are optimized out. */
13876 if (padsize)
13877 {
13878 if (padsize > decl_size)
13879 {
13880 remove_loc_list_addr_table_entries (cur_descr);
13881 goto discard_descr;
13882 }
13883 decl_size -= padsize;
13884 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13885 if (*descr_tail == NULL)
13886 {
13887 remove_loc_list_addr_table_entries (cur_descr);
13888 goto discard_descr;
13889 }
13890 descr_tail = &(*descr_tail)->dw_loc_next;
13891 padsize = 0;
13892 }
13893 *descr_tail = cur_descr;
13894 descr_tail = tail;
13895 if (bitsize > decl_size)
13896 goto discard_descr;
13897 decl_size -= bitsize;
13898 if (last == NULL)
13899 {
13900 HOST_WIDE_INT offset = 0;
13901 if (GET_CODE (varloc) == VAR_LOCATION
13902 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13903 {
13904 varloc = PAT_VAR_LOCATION_LOC (varloc);
13905 if (GET_CODE (varloc) == EXPR_LIST)
13906 varloc = XEXP (varloc, 0);
13907 }
13908 do
13909 {
13910 if (GET_CODE (varloc) == CONST
13911 || GET_CODE (varloc) == SIGN_EXTEND
13912 || GET_CODE (varloc) == ZERO_EXTEND)
13913 varloc = XEXP (varloc, 0);
13914 else if (GET_CODE (varloc) == SUBREG)
13915 varloc = SUBREG_REG (varloc);
13916 else
13917 break;
13918 }
13919 while (1);
13920 /* DW_OP_bit_size offset should be zero for register
13921 or implicit location descriptions and empty location
13922 descriptions, but for memory addresses needs big endian
13923 adjustment. */
13924 if (MEM_P (varloc))
13925 {
13926 unsigned HOST_WIDE_INT memsize
13927 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13928 if (memsize != bitsize)
13929 {
13930 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13931 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13932 goto discard_descr;
13933 if (memsize < bitsize)
13934 goto discard_descr;
13935 if (BITS_BIG_ENDIAN)
13936 offset = memsize - bitsize;
13937 }
13938 }
13939
13940 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13941 if (*descr_tail == NULL)
13942 goto discard_descr;
13943 descr_tail = &(*descr_tail)->dw_loc_next;
13944 }
13945 }
13946
13947 /* If there were any non-empty expressions, add padding till the end of
13948 the decl. */
13949 if (descr != NULL && decl_size != 0)
13950 {
13951 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13952 if (*descr_tail == NULL)
13953 goto discard_descr;
13954 }
13955 return descr;
13956
13957 discard_descr:
13958 /* Discard the descriptor and release any addr_table entries it uses. */
13959 remove_loc_list_addr_table_entries (descr);
13960 return NULL;
13961 }
13962
13963 /* Return the dwarf representation of the location list LOC_LIST of
13964 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13965 function. */
13966
13967 static dw_loc_list_ref
13968 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13969 {
13970 const char *endname, *secname;
13971 rtx varloc;
13972 enum var_init_status initialized;
13973 struct var_loc_node *node;
13974 dw_loc_descr_ref descr;
13975 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13976 dw_loc_list_ref list = NULL;
13977 dw_loc_list_ref *listp = &list;
13978
13979 /* Now that we know what section we are using for a base,
13980 actually construct the list of locations.
13981 The first location information is what is passed to the
13982 function that creates the location list, and the remaining
13983 locations just get added on to that list.
13984 Note that we only know the start address for a location
13985 (IE location changes), so to build the range, we use
13986 the range [current location start, next location start].
13987 This means we have to special case the last node, and generate
13988 a range of [last location start, end of function label]. */
13989
13990 secname = secname_for_decl (decl);
13991
13992 for (node = loc_list->first; node; node = node->next)
13993 if (GET_CODE (node->loc) == EXPR_LIST
13994 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13995 {
13996 if (GET_CODE (node->loc) == EXPR_LIST)
13997 {
13998 /* This requires DW_OP_{,bit_}piece, which is not usable
13999 inside DWARF expressions. */
14000 if (want_address != 2)
14001 continue;
14002 descr = dw_sra_loc_expr (decl, node->loc);
14003 if (descr == NULL)
14004 continue;
14005 }
14006 else
14007 {
14008 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14009 varloc = NOTE_VAR_LOCATION (node->loc);
14010 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14011 }
14012 if (descr)
14013 {
14014 bool range_across_switch = false;
14015 /* If section switch happens in between node->label
14016 and node->next->label (or end of function) and
14017 we can't emit it as a single entry list,
14018 emit two ranges, first one ending at the end
14019 of first partition and second one starting at the
14020 beginning of second partition. */
14021 if (node == loc_list->last_before_switch
14022 && (node != loc_list->first || loc_list->first->next)
14023 && current_function_decl)
14024 {
14025 endname = cfun->fde->dw_fde_end;
14026 range_across_switch = true;
14027 }
14028 /* The variable has a location between NODE->LABEL and
14029 NODE->NEXT->LABEL. */
14030 else if (node->next)
14031 endname = node->next->label;
14032 /* If the variable has a location at the last label
14033 it keeps its location until the end of function. */
14034 else if (!current_function_decl)
14035 endname = text_end_label;
14036 else
14037 {
14038 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14039 current_function_funcdef_no);
14040 endname = ggc_strdup (label_id);
14041 }
14042
14043 *listp = new_loc_list (descr, node->label, endname, secname);
14044 if (TREE_CODE (decl) == PARM_DECL
14045 && node == loc_list->first
14046 && NOTE_P (node->loc)
14047 && strcmp (node->label, endname) == 0)
14048 (*listp)->force = true;
14049 listp = &(*listp)->dw_loc_next;
14050
14051 if (range_across_switch)
14052 {
14053 if (GET_CODE (node->loc) == EXPR_LIST)
14054 descr = dw_sra_loc_expr (decl, node->loc);
14055 else
14056 {
14057 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14058 varloc = NOTE_VAR_LOCATION (node->loc);
14059 descr = dw_loc_list_1 (decl, varloc, want_address,
14060 initialized);
14061 }
14062 gcc_assert (descr);
14063 /* The variable has a location between NODE->LABEL and
14064 NODE->NEXT->LABEL. */
14065 if (node->next)
14066 endname = node->next->label;
14067 else
14068 endname = cfun->fde->dw_fde_second_end;
14069 *listp = new_loc_list (descr,
14070 cfun->fde->dw_fde_second_begin,
14071 endname, secname);
14072 listp = &(*listp)->dw_loc_next;
14073 }
14074 }
14075 }
14076
14077 /* Try to avoid the overhead of a location list emitting a location
14078 expression instead, but only if we didn't have more than one
14079 location entry in the first place. If some entries were not
14080 representable, we don't want to pretend a single entry that was
14081 applies to the entire scope in which the variable is
14082 available. */
14083 if (list && loc_list->first->next)
14084 gen_llsym (list);
14085
14086 return list;
14087 }
14088
14089 /* Return if the loc_list has only single element and thus can be represented
14090 as location description. */
14091
14092 static bool
14093 single_element_loc_list_p (dw_loc_list_ref list)
14094 {
14095 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14096 return !list->ll_symbol;
14097 }
14098
14099 /* To each location in list LIST add loc descr REF. */
14100
14101 static void
14102 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14103 {
14104 dw_loc_descr_ref copy;
14105 add_loc_descr (&list->expr, ref);
14106 list = list->dw_loc_next;
14107 while (list)
14108 {
14109 copy = ggc_alloc<dw_loc_descr_node> ();
14110 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14111 add_loc_descr (&list->expr, copy);
14112 while (copy->dw_loc_next)
14113 {
14114 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14115 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14116 copy->dw_loc_next = new_copy;
14117 copy = new_copy;
14118 }
14119 list = list->dw_loc_next;
14120 }
14121 }
14122
14123 /* Given two lists RET and LIST
14124 produce location list that is result of adding expression in LIST
14125 to expression in RET on each position in program.
14126 Might be destructive on both RET and LIST.
14127
14128 TODO: We handle only simple cases of RET or LIST having at most one
14129 element. General case would inolve sorting the lists in program order
14130 and merging them that will need some additional work.
14131 Adding that will improve quality of debug info especially for SRA-ed
14132 structures. */
14133
14134 static void
14135 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14136 {
14137 if (!list)
14138 return;
14139 if (!*ret)
14140 {
14141 *ret = list;
14142 return;
14143 }
14144 if (!list->dw_loc_next)
14145 {
14146 add_loc_descr_to_each (*ret, list->expr);
14147 return;
14148 }
14149 if (!(*ret)->dw_loc_next)
14150 {
14151 add_loc_descr_to_each (list, (*ret)->expr);
14152 *ret = list;
14153 return;
14154 }
14155 expansion_failed (NULL_TREE, NULL_RTX,
14156 "Don't know how to merge two non-trivial"
14157 " location lists.\n");
14158 *ret = NULL;
14159 return;
14160 }
14161
14162 /* LOC is constant expression. Try a luck, look it up in constant
14163 pool and return its loc_descr of its address. */
14164
14165 static dw_loc_descr_ref
14166 cst_pool_loc_descr (tree loc)
14167 {
14168 /* Get an RTL for this, if something has been emitted. */
14169 rtx rtl = lookup_constant_def (loc);
14170
14171 if (!rtl || !MEM_P (rtl))
14172 {
14173 gcc_assert (!rtl);
14174 return 0;
14175 }
14176 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14177
14178 /* TODO: We might get more coverage if we was actually delaying expansion
14179 of all expressions till end of compilation when constant pools are fully
14180 populated. */
14181 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14182 {
14183 expansion_failed (loc, NULL_RTX,
14184 "CST value in contant pool but not marked.");
14185 return 0;
14186 }
14187 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14188 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14189 }
14190
14191 /* Return dw_loc_list representing address of addr_expr LOC
14192 by looking for inner INDIRECT_REF expression and turning
14193 it into simple arithmetics. */
14194
14195 static dw_loc_list_ref
14196 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14197 {
14198 tree obj, offset;
14199 HOST_WIDE_INT bitsize, bitpos, bytepos;
14200 machine_mode mode;
14201 int unsignedp, volatilep = 0;
14202 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14203
14204 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14205 &bitsize, &bitpos, &offset, &mode,
14206 &unsignedp, &volatilep, false);
14207 STRIP_NOPS (obj);
14208 if (bitpos % BITS_PER_UNIT)
14209 {
14210 expansion_failed (loc, NULL_RTX, "bitfield access");
14211 return 0;
14212 }
14213 if (!INDIRECT_REF_P (obj))
14214 {
14215 expansion_failed (obj,
14216 NULL_RTX, "no indirect ref in inner refrence");
14217 return 0;
14218 }
14219 if (!offset && !bitpos)
14220 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14221 else if (toplev
14222 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14223 && (dwarf_version >= 4 || !dwarf_strict))
14224 {
14225 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14226 if (!list_ret)
14227 return 0;
14228 if (offset)
14229 {
14230 /* Variable offset. */
14231 list_ret1 = loc_list_from_tree (offset, 0);
14232 if (list_ret1 == 0)
14233 return 0;
14234 add_loc_list (&list_ret, list_ret1);
14235 if (!list_ret)
14236 return 0;
14237 add_loc_descr_to_each (list_ret,
14238 new_loc_descr (DW_OP_plus, 0, 0));
14239 }
14240 bytepos = bitpos / BITS_PER_UNIT;
14241 if (bytepos > 0)
14242 add_loc_descr_to_each (list_ret,
14243 new_loc_descr (DW_OP_plus_uconst,
14244 bytepos, 0));
14245 else if (bytepos < 0)
14246 loc_list_plus_const (list_ret, bytepos);
14247 add_loc_descr_to_each (list_ret,
14248 new_loc_descr (DW_OP_stack_value, 0, 0));
14249 }
14250 return list_ret;
14251 }
14252
14253
14254 /* Generate Dwarf location list representing LOC.
14255 If WANT_ADDRESS is false, expression computing LOC will be computed
14256 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14257 if WANT_ADDRESS is 2, expression computing address useable in location
14258 will be returned (i.e. DW_OP_reg can be used
14259 to refer to register values). */
14260
14261 static dw_loc_list_ref
14262 loc_list_from_tree (tree loc, int want_address)
14263 {
14264 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14265 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14266 int have_address = 0;
14267 enum dwarf_location_atom op;
14268
14269 /* ??? Most of the time we do not take proper care for sign/zero
14270 extending the values properly. Hopefully this won't be a real
14271 problem... */
14272
14273 switch (TREE_CODE (loc))
14274 {
14275 case ERROR_MARK:
14276 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14277 return 0;
14278
14279 case PLACEHOLDER_EXPR:
14280 /* This case involves extracting fields from an object to determine the
14281 position of other fields. We don't try to encode this here. The
14282 only user of this is Ada, which encodes the needed information using
14283 the names of types. */
14284 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14285 return 0;
14286
14287 case CALL_EXPR:
14288 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14289 /* There are no opcodes for these operations. */
14290 return 0;
14291
14292 case PREINCREMENT_EXPR:
14293 case PREDECREMENT_EXPR:
14294 case POSTINCREMENT_EXPR:
14295 case POSTDECREMENT_EXPR:
14296 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14297 /* There are no opcodes for these operations. */
14298 return 0;
14299
14300 case ADDR_EXPR:
14301 /* If we already want an address, see if there is INDIRECT_REF inside
14302 e.g. for &this->field. */
14303 if (want_address)
14304 {
14305 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14306 (loc, want_address == 2);
14307 if (list_ret)
14308 have_address = 1;
14309 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14310 && (ret = cst_pool_loc_descr (loc)))
14311 have_address = 1;
14312 }
14313 /* Otherwise, process the argument and look for the address. */
14314 if (!list_ret && !ret)
14315 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14316 else
14317 {
14318 if (want_address)
14319 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14320 return NULL;
14321 }
14322 break;
14323
14324 case VAR_DECL:
14325 if (DECL_THREAD_LOCAL_P (loc))
14326 {
14327 rtx rtl;
14328 enum dwarf_location_atom tls_op;
14329 enum dtprel_bool dtprel = dtprel_false;
14330
14331 if (targetm.have_tls)
14332 {
14333 /* If this is not defined, we have no way to emit the
14334 data. */
14335 if (!targetm.asm_out.output_dwarf_dtprel)
14336 return 0;
14337
14338 /* The way DW_OP_GNU_push_tls_address is specified, we
14339 can only look up addresses of objects in the current
14340 module. We used DW_OP_addr as first op, but that's
14341 wrong, because DW_OP_addr is relocated by the debug
14342 info consumer, while DW_OP_GNU_push_tls_address
14343 operand shouldn't be. */
14344 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14345 return 0;
14346 dtprel = dtprel_true;
14347 tls_op = DW_OP_GNU_push_tls_address;
14348 }
14349 else
14350 {
14351 if (!targetm.emutls.debug_form_tls_address
14352 || !(dwarf_version >= 3 || !dwarf_strict))
14353 return 0;
14354 /* We stuffed the control variable into the DECL_VALUE_EXPR
14355 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14356 no longer appear in gimple code. We used the control
14357 variable in specific so that we could pick it up here. */
14358 loc = DECL_VALUE_EXPR (loc);
14359 tls_op = DW_OP_form_tls_address;
14360 }
14361
14362 rtl = rtl_for_decl_location (loc);
14363 if (rtl == NULL_RTX)
14364 return 0;
14365
14366 if (!MEM_P (rtl))
14367 return 0;
14368 rtl = XEXP (rtl, 0);
14369 if (! CONSTANT_P (rtl))
14370 return 0;
14371
14372 ret = new_addr_loc_descr (rtl, dtprel);
14373 ret1 = new_loc_descr (tls_op, 0, 0);
14374 add_loc_descr (&ret, ret1);
14375
14376 have_address = 1;
14377 break;
14378 }
14379 /* FALLTHRU */
14380
14381 case PARM_DECL:
14382 case RESULT_DECL:
14383 if (DECL_HAS_VALUE_EXPR_P (loc))
14384 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14385 want_address);
14386 /* FALLTHRU */
14387
14388 case FUNCTION_DECL:
14389 {
14390 rtx rtl;
14391 var_loc_list *loc_list = lookup_decl_loc (loc);
14392
14393 if (loc_list && loc_list->first)
14394 {
14395 list_ret = dw_loc_list (loc_list, loc, want_address);
14396 have_address = want_address != 0;
14397 break;
14398 }
14399 rtl = rtl_for_decl_location (loc);
14400 if (rtl == NULL_RTX)
14401 {
14402 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14403 return 0;
14404 }
14405 else if (CONST_INT_P (rtl))
14406 {
14407 HOST_WIDE_INT val = INTVAL (rtl);
14408 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14409 val &= GET_MODE_MASK (DECL_MODE (loc));
14410 ret = int_loc_descriptor (val);
14411 }
14412 else if (GET_CODE (rtl) == CONST_STRING)
14413 {
14414 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14415 return 0;
14416 }
14417 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14418 ret = new_addr_loc_descr (rtl, dtprel_false);
14419 else
14420 {
14421 machine_mode mode, mem_mode;
14422
14423 /* Certain constructs can only be represented at top-level. */
14424 if (want_address == 2)
14425 {
14426 ret = loc_descriptor (rtl, VOIDmode,
14427 VAR_INIT_STATUS_INITIALIZED);
14428 have_address = 1;
14429 }
14430 else
14431 {
14432 mode = GET_MODE (rtl);
14433 mem_mode = VOIDmode;
14434 if (MEM_P (rtl))
14435 {
14436 mem_mode = mode;
14437 mode = get_address_mode (rtl);
14438 rtl = XEXP (rtl, 0);
14439 have_address = 1;
14440 }
14441 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14442 VAR_INIT_STATUS_INITIALIZED);
14443 }
14444 if (!ret)
14445 expansion_failed (loc, rtl,
14446 "failed to produce loc descriptor for rtl");
14447 }
14448 }
14449 break;
14450
14451 case MEM_REF:
14452 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14453 {
14454 have_address = 1;
14455 goto do_plus;
14456 }
14457 /* Fallthru. */
14458 case INDIRECT_REF:
14459 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14460 have_address = 1;
14461 break;
14462
14463 case TARGET_MEM_REF:
14464 case SSA_NAME:
14465 return NULL;
14466
14467 case COMPOUND_EXPR:
14468 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14469
14470 CASE_CONVERT:
14471 case VIEW_CONVERT_EXPR:
14472 case SAVE_EXPR:
14473 case MODIFY_EXPR:
14474 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14475
14476 case COMPONENT_REF:
14477 case BIT_FIELD_REF:
14478 case ARRAY_REF:
14479 case ARRAY_RANGE_REF:
14480 case REALPART_EXPR:
14481 case IMAGPART_EXPR:
14482 {
14483 tree obj, offset;
14484 HOST_WIDE_INT bitsize, bitpos, bytepos;
14485 machine_mode mode;
14486 int unsignedp, volatilep = 0;
14487
14488 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14489 &unsignedp, &volatilep, false);
14490
14491 gcc_assert (obj != loc);
14492
14493 list_ret = loc_list_from_tree (obj,
14494 want_address == 2
14495 && !bitpos && !offset ? 2 : 1);
14496 /* TODO: We can extract value of the small expression via shifting even
14497 for nonzero bitpos. */
14498 if (list_ret == 0)
14499 return 0;
14500 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14501 {
14502 expansion_failed (loc, NULL_RTX,
14503 "bitfield access");
14504 return 0;
14505 }
14506
14507 if (offset != NULL_TREE)
14508 {
14509 /* Variable offset. */
14510 list_ret1 = loc_list_from_tree (offset, 0);
14511 if (list_ret1 == 0)
14512 return 0;
14513 add_loc_list (&list_ret, list_ret1);
14514 if (!list_ret)
14515 return 0;
14516 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14517 }
14518
14519 bytepos = bitpos / BITS_PER_UNIT;
14520 if (bytepos > 0)
14521 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14522 else if (bytepos < 0)
14523 loc_list_plus_const (list_ret, bytepos);
14524
14525 have_address = 1;
14526 break;
14527 }
14528
14529 case INTEGER_CST:
14530 if ((want_address || !tree_fits_shwi_p (loc))
14531 && (ret = cst_pool_loc_descr (loc)))
14532 have_address = 1;
14533 else if (want_address == 2
14534 && tree_fits_shwi_p (loc)
14535 && (ret = address_of_int_loc_descriptor
14536 (int_size_in_bytes (TREE_TYPE (loc)),
14537 tree_to_shwi (loc))))
14538 have_address = 1;
14539 else if (tree_fits_shwi_p (loc))
14540 ret = int_loc_descriptor (tree_to_shwi (loc));
14541 else
14542 {
14543 expansion_failed (loc, NULL_RTX,
14544 "Integer operand is not host integer");
14545 return 0;
14546 }
14547 break;
14548
14549 case CONSTRUCTOR:
14550 case REAL_CST:
14551 case STRING_CST:
14552 case COMPLEX_CST:
14553 if ((ret = cst_pool_loc_descr (loc)))
14554 have_address = 1;
14555 else
14556 /* We can construct small constants here using int_loc_descriptor. */
14557 expansion_failed (loc, NULL_RTX,
14558 "constructor or constant not in constant pool");
14559 break;
14560
14561 case TRUTH_AND_EXPR:
14562 case TRUTH_ANDIF_EXPR:
14563 case BIT_AND_EXPR:
14564 op = DW_OP_and;
14565 goto do_binop;
14566
14567 case TRUTH_XOR_EXPR:
14568 case BIT_XOR_EXPR:
14569 op = DW_OP_xor;
14570 goto do_binop;
14571
14572 case TRUTH_OR_EXPR:
14573 case TRUTH_ORIF_EXPR:
14574 case BIT_IOR_EXPR:
14575 op = DW_OP_or;
14576 goto do_binop;
14577
14578 case FLOOR_DIV_EXPR:
14579 case CEIL_DIV_EXPR:
14580 case ROUND_DIV_EXPR:
14581 case TRUNC_DIV_EXPR:
14582 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14583 return 0;
14584 op = DW_OP_div;
14585 goto do_binop;
14586
14587 case MINUS_EXPR:
14588 op = DW_OP_minus;
14589 goto do_binop;
14590
14591 case FLOOR_MOD_EXPR:
14592 case CEIL_MOD_EXPR:
14593 case ROUND_MOD_EXPR:
14594 case TRUNC_MOD_EXPR:
14595 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14596 {
14597 op = DW_OP_mod;
14598 goto do_binop;
14599 }
14600 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14601 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14602 if (list_ret == 0 || list_ret1 == 0)
14603 return 0;
14604
14605 add_loc_list (&list_ret, list_ret1);
14606 if (list_ret == 0)
14607 return 0;
14608 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14609 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14610 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14611 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14612 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14613 break;
14614
14615 case MULT_EXPR:
14616 op = DW_OP_mul;
14617 goto do_binop;
14618
14619 case LSHIFT_EXPR:
14620 op = DW_OP_shl;
14621 goto do_binop;
14622
14623 case RSHIFT_EXPR:
14624 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14625 goto do_binop;
14626
14627 case POINTER_PLUS_EXPR:
14628 case PLUS_EXPR:
14629 do_plus:
14630 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14631 {
14632 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14633 if (list_ret == 0)
14634 return 0;
14635
14636 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14637 break;
14638 }
14639
14640 op = DW_OP_plus;
14641 goto do_binop;
14642
14643 case LE_EXPR:
14644 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14645 return 0;
14646
14647 op = DW_OP_le;
14648 goto do_binop;
14649
14650 case GE_EXPR:
14651 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14652 return 0;
14653
14654 op = DW_OP_ge;
14655 goto do_binop;
14656
14657 case LT_EXPR:
14658 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14659 return 0;
14660
14661 op = DW_OP_lt;
14662 goto do_binop;
14663
14664 case GT_EXPR:
14665 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14666 return 0;
14667
14668 op = DW_OP_gt;
14669 goto do_binop;
14670
14671 case EQ_EXPR:
14672 op = DW_OP_eq;
14673 goto do_binop;
14674
14675 case NE_EXPR:
14676 op = DW_OP_ne;
14677 goto do_binop;
14678
14679 do_binop:
14680 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14681 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14682 if (list_ret == 0 || list_ret1 == 0)
14683 return 0;
14684
14685 add_loc_list (&list_ret, list_ret1);
14686 if (list_ret == 0)
14687 return 0;
14688 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14689 break;
14690
14691 case TRUTH_NOT_EXPR:
14692 case BIT_NOT_EXPR:
14693 op = DW_OP_not;
14694 goto do_unop;
14695
14696 case ABS_EXPR:
14697 op = DW_OP_abs;
14698 goto do_unop;
14699
14700 case NEGATE_EXPR:
14701 op = DW_OP_neg;
14702 goto do_unop;
14703
14704 do_unop:
14705 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14706 if (list_ret == 0)
14707 return 0;
14708
14709 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14710 break;
14711
14712 case MIN_EXPR:
14713 case MAX_EXPR:
14714 {
14715 const enum tree_code code =
14716 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14717
14718 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14719 build2 (code, integer_type_node,
14720 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14721 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14722 }
14723
14724 /* ... fall through ... */
14725
14726 case COND_EXPR:
14727 {
14728 dw_loc_descr_ref lhs
14729 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14730 dw_loc_list_ref rhs
14731 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14732 dw_loc_descr_ref bra_node, jump_node, tmp;
14733
14734 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14735 if (list_ret == 0 || lhs == 0 || rhs == 0)
14736 return 0;
14737
14738 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14739 add_loc_descr_to_each (list_ret, bra_node);
14740
14741 add_loc_list (&list_ret, rhs);
14742 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14743 add_loc_descr_to_each (list_ret, jump_node);
14744
14745 add_loc_descr_to_each (list_ret, lhs);
14746 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14747 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14748
14749 /* ??? Need a node to point the skip at. Use a nop. */
14750 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14751 add_loc_descr_to_each (list_ret, tmp);
14752 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14753 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14754 }
14755 break;
14756
14757 case FIX_TRUNC_EXPR:
14758 return 0;
14759
14760 default:
14761 /* Leave front-end specific codes as simply unknown. This comes
14762 up, for instance, with the C STMT_EXPR. */
14763 if ((unsigned int) TREE_CODE (loc)
14764 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14765 {
14766 expansion_failed (loc, NULL_RTX,
14767 "language specific tree node");
14768 return 0;
14769 }
14770
14771 #ifdef ENABLE_CHECKING
14772 /* Otherwise this is a generic code; we should just lists all of
14773 these explicitly. We forgot one. */
14774 gcc_unreachable ();
14775 #else
14776 /* In a release build, we want to degrade gracefully: better to
14777 generate incomplete debugging information than to crash. */
14778 return NULL;
14779 #endif
14780 }
14781
14782 if (!ret && !list_ret)
14783 return 0;
14784
14785 if (want_address == 2 && !have_address
14786 && (dwarf_version >= 4 || !dwarf_strict))
14787 {
14788 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14789 {
14790 expansion_failed (loc, NULL_RTX,
14791 "DWARF address size mismatch");
14792 return 0;
14793 }
14794 if (ret)
14795 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14796 else
14797 add_loc_descr_to_each (list_ret,
14798 new_loc_descr (DW_OP_stack_value, 0, 0));
14799 have_address = 1;
14800 }
14801 /* Show if we can't fill the request for an address. */
14802 if (want_address && !have_address)
14803 {
14804 expansion_failed (loc, NULL_RTX,
14805 "Want address and only have value");
14806 return 0;
14807 }
14808
14809 gcc_assert (!ret || !list_ret);
14810
14811 /* If we've got an address and don't want one, dereference. */
14812 if (!want_address && have_address)
14813 {
14814 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14815
14816 if (size > DWARF2_ADDR_SIZE || size == -1)
14817 {
14818 expansion_failed (loc, NULL_RTX,
14819 "DWARF address size mismatch");
14820 return 0;
14821 }
14822 else if (size == DWARF2_ADDR_SIZE)
14823 op = DW_OP_deref;
14824 else
14825 op = DW_OP_deref_size;
14826
14827 if (ret)
14828 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14829 else
14830 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14831 }
14832 if (ret)
14833 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14834
14835 return list_ret;
14836 }
14837
14838 /* Same as above but return only single location expression. */
14839 static dw_loc_descr_ref
14840 loc_descriptor_from_tree (tree loc, int want_address)
14841 {
14842 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14843 if (!ret)
14844 return NULL;
14845 if (ret->dw_loc_next)
14846 {
14847 expansion_failed (loc, NULL_RTX,
14848 "Location list where only loc descriptor needed");
14849 return NULL;
14850 }
14851 return ret->expr;
14852 }
14853
14854 /* Given a value, round it up to the lowest multiple of `boundary'
14855 which is not less than the value itself. */
14856
14857 static inline HOST_WIDE_INT
14858 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14859 {
14860 return (((value + boundary - 1) / boundary) * boundary);
14861 }
14862
14863 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14864 pointer to the declared type for the relevant field variable, or return
14865 `integer_type_node' if the given node turns out to be an
14866 ERROR_MARK node. */
14867
14868 static inline tree
14869 field_type (const_tree decl)
14870 {
14871 tree type;
14872
14873 if (TREE_CODE (decl) == ERROR_MARK)
14874 return integer_type_node;
14875
14876 type = DECL_BIT_FIELD_TYPE (decl);
14877 if (type == NULL_TREE)
14878 type = TREE_TYPE (decl);
14879
14880 return type;
14881 }
14882
14883 /* Given a pointer to a tree node, return the alignment in bits for
14884 it, or else return BITS_PER_WORD if the node actually turns out to
14885 be an ERROR_MARK node. */
14886
14887 static inline unsigned
14888 simple_type_align_in_bits (const_tree type)
14889 {
14890 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14891 }
14892
14893 static inline unsigned
14894 simple_decl_align_in_bits (const_tree decl)
14895 {
14896 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14897 }
14898
14899 /* Return the result of rounding T up to ALIGN. */
14900
14901 static inline offset_int
14902 round_up_to_align (const offset_int &t, unsigned int align)
14903 {
14904 return wi::udiv_trunc (t + align - 1, align) * align;
14905 }
14906
14907 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14908 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14909 or return 0 if we are unable to determine what that offset is, either
14910 because the argument turns out to be a pointer to an ERROR_MARK node, or
14911 because the offset is actually variable. (We can't handle the latter case
14912 just yet). */
14913
14914 static HOST_WIDE_INT
14915 field_byte_offset (const_tree decl)
14916 {
14917 offset_int object_offset_in_bits;
14918 offset_int object_offset_in_bytes;
14919 offset_int bitpos_int;
14920
14921 if (TREE_CODE (decl) == ERROR_MARK)
14922 return 0;
14923
14924 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14925
14926 /* We cannot yet cope with fields whose positions are variable, so
14927 for now, when we see such things, we simply return 0. Someday, we may
14928 be able to handle such cases, but it will be damn difficult. */
14929 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14930 return 0;
14931
14932 bitpos_int = wi::to_offset (bit_position (decl));
14933
14934 #ifdef PCC_BITFIELD_TYPE_MATTERS
14935 if (PCC_BITFIELD_TYPE_MATTERS)
14936 {
14937 tree type;
14938 tree field_size_tree;
14939 offset_int deepest_bitpos;
14940 offset_int field_size_in_bits;
14941 unsigned int type_align_in_bits;
14942 unsigned int decl_align_in_bits;
14943 offset_int type_size_in_bits;
14944
14945 type = field_type (decl);
14946 type_size_in_bits = offset_int_type_size_in_bits (type);
14947 type_align_in_bits = simple_type_align_in_bits (type);
14948
14949 field_size_tree = DECL_SIZE (decl);
14950
14951 /* The size could be unspecified if there was an error, or for
14952 a flexible array member. */
14953 if (!field_size_tree)
14954 field_size_tree = bitsize_zero_node;
14955
14956 /* If the size of the field is not constant, use the type size. */
14957 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14958 field_size_in_bits = wi::to_offset (field_size_tree);
14959 else
14960 field_size_in_bits = type_size_in_bits;
14961
14962 decl_align_in_bits = simple_decl_align_in_bits (decl);
14963
14964 /* The GCC front-end doesn't make any attempt to keep track of the
14965 starting bit offset (relative to the start of the containing
14966 structure type) of the hypothetical "containing object" for a
14967 bit-field. Thus, when computing the byte offset value for the
14968 start of the "containing object" of a bit-field, we must deduce
14969 this information on our own. This can be rather tricky to do in
14970 some cases. For example, handling the following structure type
14971 definition when compiling for an i386/i486 target (which only
14972 aligns long long's to 32-bit boundaries) can be very tricky:
14973
14974 struct S { int field1; long long field2:31; };
14975
14976 Fortunately, there is a simple rule-of-thumb which can be used
14977 in such cases. When compiling for an i386/i486, GCC will
14978 allocate 8 bytes for the structure shown above. It decides to
14979 do this based upon one simple rule for bit-field allocation.
14980 GCC allocates each "containing object" for each bit-field at
14981 the first (i.e. lowest addressed) legitimate alignment boundary
14982 (based upon the required minimum alignment for the declared
14983 type of the field) which it can possibly use, subject to the
14984 condition that there is still enough available space remaining
14985 in the containing object (when allocated at the selected point)
14986 to fully accommodate all of the bits of the bit-field itself.
14987
14988 This simple rule makes it obvious why GCC allocates 8 bytes for
14989 each object of the structure type shown above. When looking
14990 for a place to allocate the "containing object" for `field2',
14991 the compiler simply tries to allocate a 64-bit "containing
14992 object" at each successive 32-bit boundary (starting at zero)
14993 until it finds a place to allocate that 64- bit field such that
14994 at least 31 contiguous (and previously unallocated) bits remain
14995 within that selected 64 bit field. (As it turns out, for the
14996 example above, the compiler finds it is OK to allocate the
14997 "containing object" 64-bit field at bit-offset zero within the
14998 structure type.)
14999
15000 Here we attempt to work backwards from the limited set of facts
15001 we're given, and we try to deduce from those facts, where GCC
15002 must have believed that the containing object started (within
15003 the structure type). The value we deduce is then used (by the
15004 callers of this routine) to generate DW_AT_location and
15005 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15006 the case of DW_AT_location, regular fields as well). */
15007
15008 /* Figure out the bit-distance from the start of the structure to
15009 the "deepest" bit of the bit-field. */
15010 deepest_bitpos = bitpos_int + field_size_in_bits;
15011
15012 /* This is the tricky part. Use some fancy footwork to deduce
15013 where the lowest addressed bit of the containing object must
15014 be. */
15015 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15016
15017 /* Round up to type_align by default. This works best for
15018 bitfields. */
15019 object_offset_in_bits
15020 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15021
15022 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15023 {
15024 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15025
15026 /* Round up to decl_align instead. */
15027 object_offset_in_bits
15028 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15029 }
15030 }
15031 else
15032 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15033 object_offset_in_bits = bitpos_int;
15034
15035 object_offset_in_bytes
15036 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15037 return object_offset_in_bytes.to_shwi ();
15038 }
15039 \f
15040 /* The following routines define various Dwarf attributes and any data
15041 associated with them. */
15042
15043 /* Add a location description attribute value to a DIE.
15044
15045 This emits location attributes suitable for whole variables and
15046 whole parameters. Note that the location attributes for struct fields are
15047 generated by the routine `data_member_location_attribute' below. */
15048
15049 static inline void
15050 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15051 dw_loc_list_ref descr)
15052 {
15053 if (descr == 0)
15054 return;
15055 if (single_element_loc_list_p (descr))
15056 add_AT_loc (die, attr_kind, descr->expr);
15057 else
15058 add_AT_loc_list (die, attr_kind, descr);
15059 }
15060
15061 /* Add DW_AT_accessibility attribute to DIE if needed. */
15062
15063 static void
15064 add_accessibility_attribute (dw_die_ref die, tree decl)
15065 {
15066 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15067 children, otherwise the default is DW_ACCESS_public. In DWARF2
15068 the default has always been DW_ACCESS_public. */
15069 if (TREE_PROTECTED (decl))
15070 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15071 else if (TREE_PRIVATE (decl))
15072 {
15073 if (dwarf_version == 2
15074 || die->die_parent == NULL
15075 || die->die_parent->die_tag != DW_TAG_class_type)
15076 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15077 }
15078 else if (dwarf_version > 2
15079 && die->die_parent
15080 && die->die_parent->die_tag == DW_TAG_class_type)
15081 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15082 }
15083
15084 /* Attach the specialized form of location attribute used for data members of
15085 struct and union types. In the special case of a FIELD_DECL node which
15086 represents a bit-field, the "offset" part of this special location
15087 descriptor must indicate the distance in bytes from the lowest-addressed
15088 byte of the containing struct or union type to the lowest-addressed byte of
15089 the "containing object" for the bit-field. (See the `field_byte_offset'
15090 function above).
15091
15092 For any given bit-field, the "containing object" is a hypothetical object
15093 (of some integral or enum type) within which the given bit-field lives. The
15094 type of this hypothetical "containing object" is always the same as the
15095 declared type of the individual bit-field itself (for GCC anyway... the
15096 DWARF spec doesn't actually mandate this). Note that it is the size (in
15097 bytes) of the hypothetical "containing object" which will be given in the
15098 DW_AT_byte_size attribute for this bit-field. (See the
15099 `byte_size_attribute' function below.) It is also used when calculating the
15100 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15101 function below.) */
15102
15103 static void
15104 add_data_member_location_attribute (dw_die_ref die, tree decl)
15105 {
15106 HOST_WIDE_INT offset;
15107 dw_loc_descr_ref loc_descr = 0;
15108
15109 if (TREE_CODE (decl) == TREE_BINFO)
15110 {
15111 /* We're working on the TAG_inheritance for a base class. */
15112 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15113 {
15114 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15115 aren't at a fixed offset from all (sub)objects of the same
15116 type. We need to extract the appropriate offset from our
15117 vtable. The following dwarf expression means
15118
15119 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15120
15121 This is specific to the V3 ABI, of course. */
15122
15123 dw_loc_descr_ref tmp;
15124
15125 /* Make a copy of the object address. */
15126 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15127 add_loc_descr (&loc_descr, tmp);
15128
15129 /* Extract the vtable address. */
15130 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15131 add_loc_descr (&loc_descr, tmp);
15132
15133 /* Calculate the address of the offset. */
15134 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15135 gcc_assert (offset < 0);
15136
15137 tmp = int_loc_descriptor (-offset);
15138 add_loc_descr (&loc_descr, tmp);
15139 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15140 add_loc_descr (&loc_descr, tmp);
15141
15142 /* Extract the offset. */
15143 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15144 add_loc_descr (&loc_descr, tmp);
15145
15146 /* Add it to the object address. */
15147 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15148 add_loc_descr (&loc_descr, tmp);
15149 }
15150 else
15151 offset = tree_to_shwi (BINFO_OFFSET (decl));
15152 }
15153 else
15154 offset = field_byte_offset (decl);
15155
15156 if (! loc_descr)
15157 {
15158 if (dwarf_version > 2)
15159 {
15160 /* Don't need to output a location expression, just the constant. */
15161 if (offset < 0)
15162 add_AT_int (die, DW_AT_data_member_location, offset);
15163 else
15164 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15165 return;
15166 }
15167 else
15168 {
15169 enum dwarf_location_atom op;
15170
15171 /* The DWARF2 standard says that we should assume that the structure
15172 address is already on the stack, so we can specify a structure
15173 field address by using DW_OP_plus_uconst. */
15174 op = DW_OP_plus_uconst;
15175 loc_descr = new_loc_descr (op, offset, 0);
15176 }
15177 }
15178
15179 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15180 }
15181
15182 /* Writes integer values to dw_vec_const array. */
15183
15184 static void
15185 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15186 {
15187 while (size != 0)
15188 {
15189 *dest++ = val & 0xff;
15190 val >>= 8;
15191 --size;
15192 }
15193 }
15194
15195 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15196
15197 static HOST_WIDE_INT
15198 extract_int (const unsigned char *src, unsigned int size)
15199 {
15200 HOST_WIDE_INT val = 0;
15201
15202 src += size;
15203 while (size != 0)
15204 {
15205 val <<= 8;
15206 val |= *--src & 0xff;
15207 --size;
15208 }
15209 return val;
15210 }
15211
15212 /* Writes wide_int values to dw_vec_const array. */
15213
15214 static void
15215 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15216 {
15217 int i;
15218
15219 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15220 {
15221 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15222 return;
15223 }
15224
15225 /* We'd have to extend this code to support odd sizes. */
15226 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15227
15228 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15229
15230 if (WORDS_BIG_ENDIAN)
15231 for (i = n - 1; i >= 0; i--)
15232 {
15233 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15234 dest += sizeof (HOST_WIDE_INT);
15235 }
15236 else
15237 for (i = 0; i < n; i++)
15238 {
15239 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15240 dest += sizeof (HOST_WIDE_INT);
15241 }
15242 }
15243
15244 /* Writes floating point values to dw_vec_const array. */
15245
15246 static void
15247 insert_float (const_rtx rtl, unsigned char *array)
15248 {
15249 REAL_VALUE_TYPE rv;
15250 long val[4];
15251 int i;
15252
15253 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15254 real_to_target (val, &rv, GET_MODE (rtl));
15255
15256 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15257 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15258 {
15259 insert_int (val[i], 4, array);
15260 array += 4;
15261 }
15262 }
15263
15264 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15265 does not have a "location" either in memory or in a register. These
15266 things can arise in GNU C when a constant is passed as an actual parameter
15267 to an inlined function. They can also arise in C++ where declared
15268 constants do not necessarily get memory "homes". */
15269
15270 static bool
15271 add_const_value_attribute (dw_die_ref die, rtx rtl)
15272 {
15273 switch (GET_CODE (rtl))
15274 {
15275 case CONST_INT:
15276 {
15277 HOST_WIDE_INT val = INTVAL (rtl);
15278
15279 if (val < 0)
15280 add_AT_int (die, DW_AT_const_value, val);
15281 else
15282 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15283 }
15284 return true;
15285
15286 case CONST_WIDE_INT:
15287 add_AT_wide (die, DW_AT_const_value,
15288 std::make_pair (rtl, GET_MODE (rtl)));
15289 return true;
15290
15291 case CONST_DOUBLE:
15292 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15293 floating-point constant. A CONST_DOUBLE is used whenever the
15294 constant requires more than one word in order to be adequately
15295 represented. */
15296 {
15297 machine_mode mode = GET_MODE (rtl);
15298
15299 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15300 add_AT_double (die, DW_AT_const_value,
15301 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15302 else
15303 {
15304 unsigned int length = GET_MODE_SIZE (mode);
15305 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15306
15307 insert_float (rtl, array);
15308 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15309 }
15310 }
15311 return true;
15312
15313 case CONST_VECTOR:
15314 {
15315 machine_mode mode = GET_MODE (rtl);
15316 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15317 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15318 unsigned char *array
15319 = ggc_vec_alloc<unsigned char> (length * elt_size);
15320 unsigned int i;
15321 unsigned char *p;
15322 machine_mode imode = GET_MODE_INNER (mode);
15323
15324 switch (GET_MODE_CLASS (mode))
15325 {
15326 case MODE_VECTOR_INT:
15327 for (i = 0, p = array; i < length; i++, p += elt_size)
15328 {
15329 rtx elt = CONST_VECTOR_ELT (rtl, i);
15330 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15331 }
15332 break;
15333
15334 case MODE_VECTOR_FLOAT:
15335 for (i = 0, p = array; i < length; i++, p += elt_size)
15336 {
15337 rtx elt = CONST_VECTOR_ELT (rtl, i);
15338 insert_float (elt, p);
15339 }
15340 break;
15341
15342 default:
15343 gcc_unreachable ();
15344 }
15345
15346 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15347 }
15348 return true;
15349
15350 case CONST_STRING:
15351 if (dwarf_version >= 4 || !dwarf_strict)
15352 {
15353 dw_loc_descr_ref loc_result;
15354 resolve_one_addr (&rtl);
15355 rtl_addr:
15356 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15357 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15358 add_AT_loc (die, DW_AT_location, loc_result);
15359 vec_safe_push (used_rtx_array, rtl);
15360 return true;
15361 }
15362 return false;
15363
15364 case CONST:
15365 if (CONSTANT_P (XEXP (rtl, 0)))
15366 return add_const_value_attribute (die, XEXP (rtl, 0));
15367 /* FALLTHROUGH */
15368 case SYMBOL_REF:
15369 if (!const_ok_for_output (rtl))
15370 return false;
15371 case LABEL_REF:
15372 if (dwarf_version >= 4 || !dwarf_strict)
15373 goto rtl_addr;
15374 return false;
15375
15376 case PLUS:
15377 /* In cases where an inlined instance of an inline function is passed
15378 the address of an `auto' variable (which is local to the caller) we
15379 can get a situation where the DECL_RTL of the artificial local
15380 variable (for the inlining) which acts as a stand-in for the
15381 corresponding formal parameter (of the inline function) will look
15382 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15383 exactly a compile-time constant expression, but it isn't the address
15384 of the (artificial) local variable either. Rather, it represents the
15385 *value* which the artificial local variable always has during its
15386 lifetime. We currently have no way to represent such quasi-constant
15387 values in Dwarf, so for now we just punt and generate nothing. */
15388 return false;
15389
15390 case HIGH:
15391 case CONST_FIXED:
15392 return false;
15393
15394 case MEM:
15395 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15396 && MEM_READONLY_P (rtl)
15397 && GET_MODE (rtl) == BLKmode)
15398 {
15399 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15400 return true;
15401 }
15402 return false;
15403
15404 default:
15405 /* No other kinds of rtx should be possible here. */
15406 gcc_unreachable ();
15407 }
15408 return false;
15409 }
15410
15411 /* Determine whether the evaluation of EXPR references any variables
15412 or functions which aren't otherwise used (and therefore may not be
15413 output). */
15414 static tree
15415 reference_to_unused (tree * tp, int * walk_subtrees,
15416 void * data ATTRIBUTE_UNUSED)
15417 {
15418 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15419 *walk_subtrees = 0;
15420
15421 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15422 && ! TREE_ASM_WRITTEN (*tp))
15423 return *tp;
15424 /* ??? The C++ FE emits debug information for using decls, so
15425 putting gcc_unreachable here falls over. See PR31899. For now
15426 be conservative. */
15427 else if (!symtab->global_info_ready
15428 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15429 return *tp;
15430 else if (TREE_CODE (*tp) == VAR_DECL)
15431 {
15432 varpool_node *node = varpool_node::get (*tp);
15433 if (!node || !node->definition)
15434 return *tp;
15435 }
15436 else if (TREE_CODE (*tp) == FUNCTION_DECL
15437 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15438 {
15439 /* The call graph machinery must have finished analyzing,
15440 optimizing and gimplifying the CU by now.
15441 So if *TP has no call graph node associated
15442 to it, it means *TP will not be emitted. */
15443 if (!cgraph_node::get (*tp))
15444 return *tp;
15445 }
15446 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15447 return *tp;
15448
15449 return NULL_TREE;
15450 }
15451
15452 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15453 for use in a later add_const_value_attribute call. */
15454
15455 static rtx
15456 rtl_for_decl_init (tree init, tree type)
15457 {
15458 rtx rtl = NULL_RTX;
15459
15460 STRIP_NOPS (init);
15461
15462 /* If a variable is initialized with a string constant without embedded
15463 zeros, build CONST_STRING. */
15464 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15465 {
15466 tree enttype = TREE_TYPE (type);
15467 tree domain = TYPE_DOMAIN (type);
15468 machine_mode mode = TYPE_MODE (enttype);
15469
15470 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15471 && domain
15472 && integer_zerop (TYPE_MIN_VALUE (domain))
15473 && compare_tree_int (TYPE_MAX_VALUE (domain),
15474 TREE_STRING_LENGTH (init) - 1) == 0
15475 && ((size_t) TREE_STRING_LENGTH (init)
15476 == strlen (TREE_STRING_POINTER (init)) + 1))
15477 {
15478 rtl = gen_rtx_CONST_STRING (VOIDmode,
15479 ggc_strdup (TREE_STRING_POINTER (init)));
15480 rtl = gen_rtx_MEM (BLKmode, rtl);
15481 MEM_READONLY_P (rtl) = 1;
15482 }
15483 }
15484 /* Other aggregates, and complex values, could be represented using
15485 CONCAT: FIXME! */
15486 else if (AGGREGATE_TYPE_P (type)
15487 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15488 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15489 || TREE_CODE (type) == COMPLEX_TYPE)
15490 ;
15491 /* Vectors only work if their mode is supported by the target.
15492 FIXME: generic vectors ought to work too. */
15493 else if (TREE_CODE (type) == VECTOR_TYPE
15494 && !VECTOR_MODE_P (TYPE_MODE (type)))
15495 ;
15496 /* If the initializer is something that we know will expand into an
15497 immediate RTL constant, expand it now. We must be careful not to
15498 reference variables which won't be output. */
15499 else if (initializer_constant_valid_p (init, type)
15500 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15501 {
15502 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15503 possible. */
15504 if (TREE_CODE (type) == VECTOR_TYPE)
15505 switch (TREE_CODE (init))
15506 {
15507 case VECTOR_CST:
15508 break;
15509 case CONSTRUCTOR:
15510 if (TREE_CONSTANT (init))
15511 {
15512 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15513 bool constant_p = true;
15514 tree value;
15515 unsigned HOST_WIDE_INT ix;
15516
15517 /* Even when ctor is constant, it might contain non-*_CST
15518 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15519 belong into VECTOR_CST nodes. */
15520 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15521 if (!CONSTANT_CLASS_P (value))
15522 {
15523 constant_p = false;
15524 break;
15525 }
15526
15527 if (constant_p)
15528 {
15529 init = build_vector_from_ctor (type, elts);
15530 break;
15531 }
15532 }
15533 /* FALLTHRU */
15534
15535 default:
15536 return NULL;
15537 }
15538
15539 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15540
15541 /* If expand_expr returns a MEM, it wasn't immediate. */
15542 gcc_assert (!rtl || !MEM_P (rtl));
15543 }
15544
15545 return rtl;
15546 }
15547
15548 /* Generate RTL for the variable DECL to represent its location. */
15549
15550 static rtx
15551 rtl_for_decl_location (tree decl)
15552 {
15553 rtx rtl;
15554
15555 /* Here we have to decide where we are going to say the parameter "lives"
15556 (as far as the debugger is concerned). We only have a couple of
15557 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15558
15559 DECL_RTL normally indicates where the parameter lives during most of the
15560 activation of the function. If optimization is enabled however, this
15561 could be either NULL or else a pseudo-reg. Both of those cases indicate
15562 that the parameter doesn't really live anywhere (as far as the code
15563 generation parts of GCC are concerned) during most of the function's
15564 activation. That will happen (for example) if the parameter is never
15565 referenced within the function.
15566
15567 We could just generate a location descriptor here for all non-NULL
15568 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15569 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15570 where DECL_RTL is NULL or is a pseudo-reg.
15571
15572 Note however that we can only get away with using DECL_INCOMING_RTL as
15573 a backup substitute for DECL_RTL in certain limited cases. In cases
15574 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15575 we can be sure that the parameter was passed using the same type as it is
15576 declared to have within the function, and that its DECL_INCOMING_RTL
15577 points us to a place where a value of that type is passed.
15578
15579 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15580 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15581 because in these cases DECL_INCOMING_RTL points us to a value of some
15582 type which is *different* from the type of the parameter itself. Thus,
15583 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15584 such cases, the debugger would end up (for example) trying to fetch a
15585 `float' from a place which actually contains the first part of a
15586 `double'. That would lead to really incorrect and confusing
15587 output at debug-time.
15588
15589 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15590 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15591 are a couple of exceptions however. On little-endian machines we can
15592 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15593 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15594 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15595 when (on a little-endian machine) a non-prototyped function has a
15596 parameter declared to be of type `short' or `char'. In such cases,
15597 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15598 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15599 passed `int' value. If the debugger then uses that address to fetch
15600 a `short' or a `char' (on a little-endian machine) the result will be
15601 the correct data, so we allow for such exceptional cases below.
15602
15603 Note that our goal here is to describe the place where the given formal
15604 parameter lives during most of the function's activation (i.e. between the
15605 end of the prologue and the start of the epilogue). We'll do that as best
15606 as we can. Note however that if the given formal parameter is modified
15607 sometime during the execution of the function, then a stack backtrace (at
15608 debug-time) will show the function as having been called with the *new*
15609 value rather than the value which was originally passed in. This happens
15610 rarely enough that it is not a major problem, but it *is* a problem, and
15611 I'd like to fix it.
15612
15613 A future version of dwarf2out.c may generate two additional attributes for
15614 any given DW_TAG_formal_parameter DIE which will describe the "passed
15615 type" and the "passed location" for the given formal parameter in addition
15616 to the attributes we now generate to indicate the "declared type" and the
15617 "active location" for each parameter. This additional set of attributes
15618 could be used by debuggers for stack backtraces. Separately, note that
15619 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15620 This happens (for example) for inlined-instances of inline function formal
15621 parameters which are never referenced. This really shouldn't be
15622 happening. All PARM_DECL nodes should get valid non-NULL
15623 DECL_INCOMING_RTL values. FIXME. */
15624
15625 /* Use DECL_RTL as the "location" unless we find something better. */
15626 rtl = DECL_RTL_IF_SET (decl);
15627
15628 /* When generating abstract instances, ignore everything except
15629 constants, symbols living in memory, and symbols living in
15630 fixed registers. */
15631 if (! reload_completed)
15632 {
15633 if (rtl
15634 && (CONSTANT_P (rtl)
15635 || (MEM_P (rtl)
15636 && CONSTANT_P (XEXP (rtl, 0)))
15637 || (REG_P (rtl)
15638 && TREE_CODE (decl) == VAR_DECL
15639 && TREE_STATIC (decl))))
15640 {
15641 rtl = targetm.delegitimize_address (rtl);
15642 return rtl;
15643 }
15644 rtl = NULL_RTX;
15645 }
15646 else if (TREE_CODE (decl) == PARM_DECL)
15647 {
15648 if (rtl == NULL_RTX
15649 || is_pseudo_reg (rtl)
15650 || (MEM_P (rtl)
15651 && is_pseudo_reg (XEXP (rtl, 0))
15652 && DECL_INCOMING_RTL (decl)
15653 && MEM_P (DECL_INCOMING_RTL (decl))
15654 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15655 {
15656 tree declared_type = TREE_TYPE (decl);
15657 tree passed_type = DECL_ARG_TYPE (decl);
15658 machine_mode dmode = TYPE_MODE (declared_type);
15659 machine_mode pmode = TYPE_MODE (passed_type);
15660
15661 /* This decl represents a formal parameter which was optimized out.
15662 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15663 all cases where (rtl == NULL_RTX) just below. */
15664 if (dmode == pmode)
15665 rtl = DECL_INCOMING_RTL (decl);
15666 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15667 && SCALAR_INT_MODE_P (dmode)
15668 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15669 && DECL_INCOMING_RTL (decl))
15670 {
15671 rtx inc = DECL_INCOMING_RTL (decl);
15672 if (REG_P (inc))
15673 rtl = inc;
15674 else if (MEM_P (inc))
15675 {
15676 if (BYTES_BIG_ENDIAN)
15677 rtl = adjust_address_nv (inc, dmode,
15678 GET_MODE_SIZE (pmode)
15679 - GET_MODE_SIZE (dmode));
15680 else
15681 rtl = inc;
15682 }
15683 }
15684 }
15685
15686 /* If the parm was passed in registers, but lives on the stack, then
15687 make a big endian correction if the mode of the type of the
15688 parameter is not the same as the mode of the rtl. */
15689 /* ??? This is the same series of checks that are made in dbxout.c before
15690 we reach the big endian correction code there. It isn't clear if all
15691 of these checks are necessary here, but keeping them all is the safe
15692 thing to do. */
15693 else if (MEM_P (rtl)
15694 && XEXP (rtl, 0) != const0_rtx
15695 && ! CONSTANT_P (XEXP (rtl, 0))
15696 /* Not passed in memory. */
15697 && !MEM_P (DECL_INCOMING_RTL (decl))
15698 /* Not passed by invisible reference. */
15699 && (!REG_P (XEXP (rtl, 0))
15700 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15701 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15702 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15703 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15704 #endif
15705 )
15706 /* Big endian correction check. */
15707 && BYTES_BIG_ENDIAN
15708 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15709 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15710 < UNITS_PER_WORD))
15711 {
15712 machine_mode addr_mode = get_address_mode (rtl);
15713 int offset = (UNITS_PER_WORD
15714 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15715
15716 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15717 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15718 }
15719 }
15720 else if (TREE_CODE (decl) == VAR_DECL
15721 && rtl
15722 && MEM_P (rtl)
15723 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15724 && BYTES_BIG_ENDIAN)
15725 {
15726 machine_mode addr_mode = get_address_mode (rtl);
15727 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15728 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15729
15730 /* If a variable is declared "register" yet is smaller than
15731 a register, then if we store the variable to memory, it
15732 looks like we're storing a register-sized value, when in
15733 fact we are not. We need to adjust the offset of the
15734 storage location to reflect the actual value's bytes,
15735 else gdb will not be able to display it. */
15736 if (rsize > dsize)
15737 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15738 plus_constant (addr_mode, XEXP (rtl, 0),
15739 rsize - dsize));
15740 }
15741
15742 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15743 and will have been substituted directly into all expressions that use it.
15744 C does not have such a concept, but C++ and other languages do. */
15745 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15746 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15747
15748 if (rtl)
15749 rtl = targetm.delegitimize_address (rtl);
15750
15751 /* If we don't look past the constant pool, we risk emitting a
15752 reference to a constant pool entry that isn't referenced from
15753 code, and thus is not emitted. */
15754 if (rtl)
15755 rtl = avoid_constant_pool_reference (rtl);
15756
15757 /* Try harder to get a rtl. If this symbol ends up not being emitted
15758 in the current CU, resolve_addr will remove the expression referencing
15759 it. */
15760 if (rtl == NULL_RTX
15761 && TREE_CODE (decl) == VAR_DECL
15762 && !DECL_EXTERNAL (decl)
15763 && TREE_STATIC (decl)
15764 && DECL_NAME (decl)
15765 && !DECL_HARD_REGISTER (decl)
15766 && DECL_MODE (decl) != VOIDmode)
15767 {
15768 rtl = make_decl_rtl_for_debug (decl);
15769 if (!MEM_P (rtl)
15770 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15771 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15772 rtl = NULL_RTX;
15773 }
15774
15775 return rtl;
15776 }
15777
15778 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15779 returned. If so, the decl for the COMMON block is returned, and the
15780 value is the offset into the common block for the symbol. */
15781
15782 static tree
15783 fortran_common (tree decl, HOST_WIDE_INT *value)
15784 {
15785 tree val_expr, cvar;
15786 machine_mode mode;
15787 HOST_WIDE_INT bitsize, bitpos;
15788 tree offset;
15789 int unsignedp, volatilep = 0;
15790
15791 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15792 it does not have a value (the offset into the common area), or if it
15793 is thread local (as opposed to global) then it isn't common, and shouldn't
15794 be handled as such. */
15795 if (TREE_CODE (decl) != VAR_DECL
15796 || !TREE_STATIC (decl)
15797 || !DECL_HAS_VALUE_EXPR_P (decl)
15798 || !is_fortran ())
15799 return NULL_TREE;
15800
15801 val_expr = DECL_VALUE_EXPR (decl);
15802 if (TREE_CODE (val_expr) != COMPONENT_REF)
15803 return NULL_TREE;
15804
15805 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15806 &mode, &unsignedp, &volatilep, true);
15807
15808 if (cvar == NULL_TREE
15809 || TREE_CODE (cvar) != VAR_DECL
15810 || DECL_ARTIFICIAL (cvar)
15811 || !TREE_PUBLIC (cvar))
15812 return NULL_TREE;
15813
15814 *value = 0;
15815 if (offset != NULL)
15816 {
15817 if (!tree_fits_shwi_p (offset))
15818 return NULL_TREE;
15819 *value = tree_to_shwi (offset);
15820 }
15821 if (bitpos != 0)
15822 *value += bitpos / BITS_PER_UNIT;
15823
15824 return cvar;
15825 }
15826
15827 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15828 data attribute for a variable or a parameter. We generate the
15829 DW_AT_const_value attribute only in those cases where the given variable
15830 or parameter does not have a true "location" either in memory or in a
15831 register. This can happen (for example) when a constant is passed as an
15832 actual argument in a call to an inline function. (It's possible that
15833 these things can crop up in other ways also.) Note that one type of
15834 constant value which can be passed into an inlined function is a constant
15835 pointer. This can happen for example if an actual argument in an inlined
15836 function call evaluates to a compile-time constant address.
15837
15838 CACHE_P is true if it is worth caching the location list for DECL,
15839 so that future calls can reuse it rather than regenerate it from scratch.
15840 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15841 since we will need to refer to them each time the function is inlined. */
15842
15843 static bool
15844 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15845 enum dwarf_attribute attr)
15846 {
15847 rtx rtl;
15848 dw_loc_list_ref list;
15849 var_loc_list *loc_list;
15850 cached_dw_loc_list *cache;
15851
15852 if (TREE_CODE (decl) == ERROR_MARK)
15853 return false;
15854
15855 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15856 || TREE_CODE (decl) == RESULT_DECL);
15857
15858 /* Try to get some constant RTL for this decl, and use that as the value of
15859 the location. */
15860
15861 rtl = rtl_for_decl_location (decl);
15862 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15863 && add_const_value_attribute (die, rtl))
15864 return true;
15865
15866 /* See if we have single element location list that is equivalent to
15867 a constant value. That way we are better to use add_const_value_attribute
15868 rather than expanding constant value equivalent. */
15869 loc_list = lookup_decl_loc (decl);
15870 if (loc_list
15871 && loc_list->first
15872 && loc_list->first->next == NULL
15873 && NOTE_P (loc_list->first->loc)
15874 && NOTE_VAR_LOCATION (loc_list->first->loc)
15875 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15876 {
15877 struct var_loc_node *node;
15878
15879 node = loc_list->first;
15880 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15881 if (GET_CODE (rtl) == EXPR_LIST)
15882 rtl = XEXP (rtl, 0);
15883 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15884 && add_const_value_attribute (die, rtl))
15885 return true;
15886 }
15887 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15888 list several times. See if we've already cached the contents. */
15889 list = NULL;
15890 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15891 cache_p = false;
15892 if (cache_p)
15893 {
15894 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
15895 if (cache)
15896 list = cache->loc_list;
15897 }
15898 if (list == NULL)
15899 {
15900 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15901 /* It is usually worth caching this result if the decl is from
15902 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15903 if (cache_p && list && list->dw_loc_next)
15904 {
15905 cached_dw_loc_list **slot
15906 = cached_dw_loc_list_table->find_slot_with_hash (decl,
15907 DECL_UID (decl),
15908 INSERT);
15909 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15910 cache->decl_id = DECL_UID (decl);
15911 cache->loc_list = list;
15912 *slot = cache;
15913 }
15914 }
15915 if (list)
15916 {
15917 add_AT_location_description (die, attr, list);
15918 return true;
15919 }
15920 /* None of that worked, so it must not really have a location;
15921 try adding a constant value attribute from the DECL_INITIAL. */
15922 return tree_add_const_value_attribute_for_decl (die, decl);
15923 }
15924
15925 /* Add VARIABLE and DIE into deferred locations list. */
15926
15927 static void
15928 defer_location (tree variable, dw_die_ref die)
15929 {
15930 deferred_locations entry;
15931 entry.variable = variable;
15932 entry.die = die;
15933 vec_safe_push (deferred_locations_list, entry);
15934 }
15935
15936 /* Helper function for tree_add_const_value_attribute. Natively encode
15937 initializer INIT into an array. Return true if successful. */
15938
15939 static bool
15940 native_encode_initializer (tree init, unsigned char *array, int size)
15941 {
15942 tree type;
15943
15944 if (init == NULL_TREE)
15945 return false;
15946
15947 STRIP_NOPS (init);
15948 switch (TREE_CODE (init))
15949 {
15950 case STRING_CST:
15951 type = TREE_TYPE (init);
15952 if (TREE_CODE (type) == ARRAY_TYPE)
15953 {
15954 tree enttype = TREE_TYPE (type);
15955 machine_mode mode = TYPE_MODE (enttype);
15956
15957 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15958 return false;
15959 if (int_size_in_bytes (type) != size)
15960 return false;
15961 if (size > TREE_STRING_LENGTH (init))
15962 {
15963 memcpy (array, TREE_STRING_POINTER (init),
15964 TREE_STRING_LENGTH (init));
15965 memset (array + TREE_STRING_LENGTH (init),
15966 '\0', size - TREE_STRING_LENGTH (init));
15967 }
15968 else
15969 memcpy (array, TREE_STRING_POINTER (init), size);
15970 return true;
15971 }
15972 return false;
15973 case CONSTRUCTOR:
15974 type = TREE_TYPE (init);
15975 if (int_size_in_bytes (type) != size)
15976 return false;
15977 if (TREE_CODE (type) == ARRAY_TYPE)
15978 {
15979 HOST_WIDE_INT min_index;
15980 unsigned HOST_WIDE_INT cnt;
15981 int curpos = 0, fieldsize;
15982 constructor_elt *ce;
15983
15984 if (TYPE_DOMAIN (type) == NULL_TREE
15985 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15986 return false;
15987
15988 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15989 if (fieldsize <= 0)
15990 return false;
15991
15992 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15993 memset (array, '\0', size);
15994 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15995 {
15996 tree val = ce->value;
15997 tree index = ce->index;
15998 int pos = curpos;
15999 if (index && TREE_CODE (index) == RANGE_EXPR)
16000 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16001 * fieldsize;
16002 else if (index)
16003 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16004
16005 if (val)
16006 {
16007 STRIP_NOPS (val);
16008 if (!native_encode_initializer (val, array + pos, fieldsize))
16009 return false;
16010 }
16011 curpos = pos + fieldsize;
16012 if (index && TREE_CODE (index) == RANGE_EXPR)
16013 {
16014 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16015 - tree_to_shwi (TREE_OPERAND (index, 0));
16016 while (count-- > 0)
16017 {
16018 if (val)
16019 memcpy (array + curpos, array + pos, fieldsize);
16020 curpos += fieldsize;
16021 }
16022 }
16023 gcc_assert (curpos <= size);
16024 }
16025 return true;
16026 }
16027 else if (TREE_CODE (type) == RECORD_TYPE
16028 || TREE_CODE (type) == UNION_TYPE)
16029 {
16030 tree field = NULL_TREE;
16031 unsigned HOST_WIDE_INT cnt;
16032 constructor_elt *ce;
16033
16034 if (int_size_in_bytes (type) != size)
16035 return false;
16036
16037 if (TREE_CODE (type) == RECORD_TYPE)
16038 field = TYPE_FIELDS (type);
16039
16040 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16041 {
16042 tree val = ce->value;
16043 int pos, fieldsize;
16044
16045 if (ce->index != 0)
16046 field = ce->index;
16047
16048 if (val)
16049 STRIP_NOPS (val);
16050
16051 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16052 return false;
16053
16054 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16055 && TYPE_DOMAIN (TREE_TYPE (field))
16056 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16057 return false;
16058 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16059 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16060 return false;
16061 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16062 pos = int_byte_position (field);
16063 gcc_assert (pos + fieldsize <= size);
16064 if (val
16065 && !native_encode_initializer (val, array + pos, fieldsize))
16066 return false;
16067 }
16068 return true;
16069 }
16070 return false;
16071 case VIEW_CONVERT_EXPR:
16072 case NON_LVALUE_EXPR:
16073 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16074 default:
16075 return native_encode_expr (init, array, size) == size;
16076 }
16077 }
16078
16079 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16080 attribute is the const value T. */
16081
16082 static bool
16083 tree_add_const_value_attribute (dw_die_ref die, tree t)
16084 {
16085 tree init;
16086 tree type = TREE_TYPE (t);
16087 rtx rtl;
16088
16089 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16090 return false;
16091
16092 init = t;
16093 gcc_assert (!DECL_P (init));
16094
16095 rtl = rtl_for_decl_init (init, type);
16096 if (rtl)
16097 return add_const_value_attribute (die, rtl);
16098 /* If the host and target are sane, try harder. */
16099 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16100 && initializer_constant_valid_p (init, type))
16101 {
16102 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16103 if (size > 0 && (int) size == size)
16104 {
16105 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16106
16107 if (native_encode_initializer (init, array, size))
16108 {
16109 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16110 return true;
16111 }
16112 ggc_free (array);
16113 }
16114 }
16115 return false;
16116 }
16117
16118 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16119 attribute is the const value of T, where T is an integral constant
16120 variable with static storage duration
16121 (so it can't be a PARM_DECL or a RESULT_DECL). */
16122
16123 static bool
16124 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16125 {
16126
16127 if (!decl
16128 || (TREE_CODE (decl) != VAR_DECL
16129 && TREE_CODE (decl) != CONST_DECL)
16130 || (TREE_CODE (decl) == VAR_DECL
16131 && !TREE_STATIC (decl)))
16132 return false;
16133
16134 if (TREE_READONLY (decl)
16135 && ! TREE_THIS_VOLATILE (decl)
16136 && DECL_INITIAL (decl))
16137 /* OK */;
16138 else
16139 return false;
16140
16141 /* Don't add DW_AT_const_value if abstract origin already has one. */
16142 if (get_AT (var_die, DW_AT_const_value))
16143 return false;
16144
16145 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16146 }
16147
16148 /* Convert the CFI instructions for the current function into a
16149 location list. This is used for DW_AT_frame_base when we targeting
16150 a dwarf2 consumer that does not support the dwarf3
16151 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16152 expressions. */
16153
16154 static dw_loc_list_ref
16155 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16156 {
16157 int ix;
16158 dw_fde_ref fde;
16159 dw_loc_list_ref list, *list_tail;
16160 dw_cfi_ref cfi;
16161 dw_cfa_location last_cfa, next_cfa;
16162 const char *start_label, *last_label, *section;
16163 dw_cfa_location remember;
16164
16165 fde = cfun->fde;
16166 gcc_assert (fde != NULL);
16167
16168 section = secname_for_decl (current_function_decl);
16169 list_tail = &list;
16170 list = NULL;
16171
16172 memset (&next_cfa, 0, sizeof (next_cfa));
16173 next_cfa.reg = INVALID_REGNUM;
16174 remember = next_cfa;
16175
16176 start_label = fde->dw_fde_begin;
16177
16178 /* ??? Bald assumption that the CIE opcode list does not contain
16179 advance opcodes. */
16180 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16181 lookup_cfa_1 (cfi, &next_cfa, &remember);
16182
16183 last_cfa = next_cfa;
16184 last_label = start_label;
16185
16186 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16187 {
16188 /* If the first partition contained no CFI adjustments, the
16189 CIE opcodes apply to the whole first partition. */
16190 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16191 fde->dw_fde_begin, fde->dw_fde_end, section);
16192 list_tail =&(*list_tail)->dw_loc_next;
16193 start_label = last_label = fde->dw_fde_second_begin;
16194 }
16195
16196 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16197 {
16198 switch (cfi->dw_cfi_opc)
16199 {
16200 case DW_CFA_set_loc:
16201 case DW_CFA_advance_loc1:
16202 case DW_CFA_advance_loc2:
16203 case DW_CFA_advance_loc4:
16204 if (!cfa_equal_p (&last_cfa, &next_cfa))
16205 {
16206 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16207 start_label, last_label, section);
16208
16209 list_tail = &(*list_tail)->dw_loc_next;
16210 last_cfa = next_cfa;
16211 start_label = last_label;
16212 }
16213 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16214 break;
16215
16216 case DW_CFA_advance_loc:
16217 /* The encoding is complex enough that we should never emit this. */
16218 gcc_unreachable ();
16219
16220 default:
16221 lookup_cfa_1 (cfi, &next_cfa, &remember);
16222 break;
16223 }
16224 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16225 {
16226 if (!cfa_equal_p (&last_cfa, &next_cfa))
16227 {
16228 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16229 start_label, last_label, section);
16230
16231 list_tail = &(*list_tail)->dw_loc_next;
16232 last_cfa = next_cfa;
16233 start_label = last_label;
16234 }
16235 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16236 start_label, fde->dw_fde_end, section);
16237 list_tail = &(*list_tail)->dw_loc_next;
16238 start_label = last_label = fde->dw_fde_second_begin;
16239 }
16240 }
16241
16242 if (!cfa_equal_p (&last_cfa, &next_cfa))
16243 {
16244 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16245 start_label, last_label, section);
16246 list_tail = &(*list_tail)->dw_loc_next;
16247 start_label = last_label;
16248 }
16249
16250 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16251 start_label,
16252 fde->dw_fde_second_begin
16253 ? fde->dw_fde_second_end : fde->dw_fde_end,
16254 section);
16255
16256 if (list && list->dw_loc_next)
16257 gen_llsym (list);
16258
16259 return list;
16260 }
16261
16262 /* Compute a displacement from the "steady-state frame pointer" to the
16263 frame base (often the same as the CFA), and store it in
16264 frame_pointer_fb_offset. OFFSET is added to the displacement
16265 before the latter is negated. */
16266
16267 static void
16268 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16269 {
16270 rtx reg, elim;
16271
16272 #ifdef FRAME_POINTER_CFA_OFFSET
16273 reg = frame_pointer_rtx;
16274 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16275 #else
16276 reg = arg_pointer_rtx;
16277 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16278 #endif
16279
16280 elim = (ira_use_lra_p
16281 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16282 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16283 if (GET_CODE (elim) == PLUS)
16284 {
16285 offset += INTVAL (XEXP (elim, 1));
16286 elim = XEXP (elim, 0);
16287 }
16288
16289 frame_pointer_fb_offset = -offset;
16290
16291 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16292 in which to eliminate. This is because it's stack pointer isn't
16293 directly accessible as a register within the ISA. To work around
16294 this, assume that while we cannot provide a proper value for
16295 frame_pointer_fb_offset, we won't need one either. */
16296 frame_pointer_fb_offset_valid
16297 = ((SUPPORTS_STACK_ALIGNMENT
16298 && (elim == hard_frame_pointer_rtx
16299 || elim == stack_pointer_rtx))
16300 || elim == (frame_pointer_needed
16301 ? hard_frame_pointer_rtx
16302 : stack_pointer_rtx));
16303 }
16304
16305 /* Generate a DW_AT_name attribute given some string value to be included as
16306 the value of the attribute. */
16307
16308 static void
16309 add_name_attribute (dw_die_ref die, const char *name_string)
16310 {
16311 if (name_string != NULL && *name_string != 0)
16312 {
16313 if (demangle_name_func)
16314 name_string = (*demangle_name_func) (name_string);
16315
16316 add_AT_string (die, DW_AT_name, name_string);
16317 }
16318 }
16319
16320 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16321 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16322 of TYPE accordingly.
16323
16324 ??? This is a temporary measure until after we're able to generate
16325 regular DWARF for the complex Ada type system. */
16326
16327 static void
16328 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16329 dw_die_ref context_die)
16330 {
16331 tree dtype;
16332 dw_die_ref dtype_die;
16333
16334 if (!lang_hooks.types.descriptive_type)
16335 return;
16336
16337 dtype = lang_hooks.types.descriptive_type (type);
16338 if (!dtype)
16339 return;
16340
16341 dtype_die = lookup_type_die (dtype);
16342 if (!dtype_die)
16343 {
16344 gen_type_die (dtype, context_die);
16345 dtype_die = lookup_type_die (dtype);
16346 gcc_assert (dtype_die);
16347 }
16348
16349 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16350 }
16351
16352 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16353
16354 static const char *
16355 comp_dir_string (void)
16356 {
16357 const char *wd;
16358 char *wd1;
16359 static const char *cached_wd = NULL;
16360
16361 if (cached_wd != NULL)
16362 return cached_wd;
16363
16364 wd = get_src_pwd ();
16365 if (wd == NULL)
16366 return NULL;
16367
16368 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16369 {
16370 int wdlen;
16371
16372 wdlen = strlen (wd);
16373 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16374 strcpy (wd1, wd);
16375 wd1 [wdlen] = DIR_SEPARATOR;
16376 wd1 [wdlen + 1] = 0;
16377 wd = wd1;
16378 }
16379
16380 cached_wd = remap_debug_filename (wd);
16381 return cached_wd;
16382 }
16383
16384 /* Generate a DW_AT_comp_dir attribute for DIE. */
16385
16386 static void
16387 add_comp_dir_attribute (dw_die_ref die)
16388 {
16389 const char * wd = comp_dir_string ();
16390 if (wd != NULL)
16391 add_AT_string (die, DW_AT_comp_dir, wd);
16392 }
16393
16394 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16395 default. */
16396
16397 static int
16398 lower_bound_default (void)
16399 {
16400 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16401 {
16402 case DW_LANG_C:
16403 case DW_LANG_C89:
16404 case DW_LANG_C99:
16405 case DW_LANG_C_plus_plus:
16406 case DW_LANG_ObjC:
16407 case DW_LANG_ObjC_plus_plus:
16408 case DW_LANG_Java:
16409 return 0;
16410 case DW_LANG_Fortran77:
16411 case DW_LANG_Fortran90:
16412 case DW_LANG_Fortran95:
16413 return 1;
16414 case DW_LANG_UPC:
16415 case DW_LANG_D:
16416 case DW_LANG_Python:
16417 return dwarf_version >= 4 ? 0 : -1;
16418 case DW_LANG_Ada95:
16419 case DW_LANG_Ada83:
16420 case DW_LANG_Cobol74:
16421 case DW_LANG_Cobol85:
16422 case DW_LANG_Pascal83:
16423 case DW_LANG_Modula2:
16424 case DW_LANG_PLI:
16425 return dwarf_version >= 4 ? 1 : -1;
16426 default:
16427 return -1;
16428 }
16429 }
16430
16431 /* Given a tree node describing an array bound (either lower or upper) output
16432 a representation for that bound. */
16433
16434 static void
16435 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16436 {
16437 switch (TREE_CODE (bound))
16438 {
16439 case ERROR_MARK:
16440 return;
16441
16442 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16443 case INTEGER_CST:
16444 {
16445 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16446 int dflt;
16447
16448 /* Use the default if possible. */
16449 if (bound_attr == DW_AT_lower_bound
16450 && tree_fits_shwi_p (bound)
16451 && (dflt = lower_bound_default ()) != -1
16452 && tree_to_shwi (bound) == dflt)
16453 ;
16454
16455 /* If HOST_WIDE_INT is big enough then represent the bound as
16456 a constant value. We need to choose a form based on
16457 whether the type is signed or unsigned. We cannot just
16458 call add_AT_unsigned if the value itself is positive
16459 (add_AT_unsigned might add the unsigned value encoded as
16460 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16461 bounds type and then sign extend any unsigned values found
16462 for signed types. This is needed only for
16463 DW_AT_{lower,upper}_bound, since for most other attributes,
16464 consumers will treat DW_FORM_data[1248] as unsigned values,
16465 regardless of the underlying type. */
16466 else if (prec <= HOST_BITS_PER_WIDE_INT
16467 || tree_fits_uhwi_p (bound))
16468 {
16469 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16470 add_AT_unsigned (subrange_die, bound_attr,
16471 TREE_INT_CST_LOW (bound));
16472 else
16473 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16474 }
16475 else
16476 /* Otherwise represent the bound as an unsigned value with
16477 the precision of its type. The precision and signedness
16478 of the type will be necessary to re-interpret it
16479 unambiguously. */
16480 add_AT_wide (subrange_die, bound_attr, bound);
16481 }
16482 break;
16483
16484 CASE_CONVERT:
16485 case VIEW_CONVERT_EXPR:
16486 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16487 break;
16488
16489 case SAVE_EXPR:
16490 break;
16491
16492 case VAR_DECL:
16493 case PARM_DECL:
16494 case RESULT_DECL:
16495 {
16496 dw_die_ref decl_die = lookup_decl_die (bound);
16497
16498 /* ??? Can this happen, or should the variable have been bound
16499 first? Probably it can, since I imagine that we try to create
16500 the types of parameters in the order in which they exist in
16501 the list, and won't have created a forward reference to a
16502 later parameter. */
16503 if (decl_die != NULL)
16504 {
16505 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16506 break;
16507 }
16508 }
16509 /* FALLTHRU */
16510
16511 default:
16512 {
16513 /* Otherwise try to create a stack operation procedure to
16514 evaluate the value of the array bound. */
16515
16516 dw_die_ref ctx, decl_die;
16517 dw_loc_list_ref list;
16518
16519 list = loc_list_from_tree (bound, 2);
16520 if (list == NULL || single_element_loc_list_p (list))
16521 {
16522 /* If DW_AT_*bound is not a reference nor constant, it is
16523 a DWARF expression rather than location description.
16524 For that loc_list_from_tree (bound, 0) is needed.
16525 If that fails to give a single element list,
16526 fall back to outputting this as a reference anyway. */
16527 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16528 if (list2 && single_element_loc_list_p (list2))
16529 {
16530 add_AT_loc (subrange_die, bound_attr, list2->expr);
16531 break;
16532 }
16533 }
16534 if (list == NULL)
16535 break;
16536
16537 if (current_function_decl == 0)
16538 ctx = comp_unit_die ();
16539 else
16540 ctx = lookup_decl_die (current_function_decl);
16541
16542 decl_die = new_die (DW_TAG_variable, ctx, bound);
16543 add_AT_flag (decl_die, DW_AT_artificial, 1);
16544 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16545 add_AT_location_description (decl_die, DW_AT_location, list);
16546 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16547 break;
16548 }
16549 }
16550 }
16551
16552 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16553 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16554 Note that the block of subscript information for an array type also
16555 includes information about the element type of the given array type. */
16556
16557 static void
16558 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16559 {
16560 unsigned dimension_number;
16561 tree lower, upper;
16562 dw_die_ref subrange_die;
16563
16564 for (dimension_number = 0;
16565 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16566 type = TREE_TYPE (type), dimension_number++)
16567 {
16568 tree domain = TYPE_DOMAIN (type);
16569
16570 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16571 break;
16572
16573 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16574 and (in GNU C only) variable bounds. Handle all three forms
16575 here. */
16576 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16577 if (domain)
16578 {
16579 /* We have an array type with specified bounds. */
16580 lower = TYPE_MIN_VALUE (domain);
16581 upper = TYPE_MAX_VALUE (domain);
16582
16583 /* Define the index type. */
16584 if (TREE_TYPE (domain))
16585 {
16586 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16587 TREE_TYPE field. We can't emit debug info for this
16588 because it is an unnamed integral type. */
16589 if (TREE_CODE (domain) == INTEGER_TYPE
16590 && TYPE_NAME (domain) == NULL_TREE
16591 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16592 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16593 ;
16594 else
16595 add_type_attribute (subrange_die, TREE_TYPE (domain),
16596 TYPE_UNQUALIFIED, type_die);
16597 }
16598
16599 /* ??? If upper is NULL, the array has unspecified length,
16600 but it does have a lower bound. This happens with Fortran
16601 dimension arr(N:*)
16602 Since the debugger is definitely going to need to know N
16603 to produce useful results, go ahead and output the lower
16604 bound solo, and hope the debugger can cope. */
16605
16606 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16607 if (upper)
16608 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16609 }
16610
16611 /* Otherwise we have an array type with an unspecified length. The
16612 DWARF-2 spec does not say how to handle this; let's just leave out the
16613 bounds. */
16614 }
16615 }
16616
16617 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16618
16619 static void
16620 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16621 {
16622 dw_die_ref decl_die;
16623 HOST_WIDE_INT size;
16624
16625 switch (TREE_CODE (tree_node))
16626 {
16627 case ERROR_MARK:
16628 size = 0;
16629 break;
16630 case ENUMERAL_TYPE:
16631 case RECORD_TYPE:
16632 case UNION_TYPE:
16633 case QUAL_UNION_TYPE:
16634 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16635 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16636 {
16637 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16638 return;
16639 }
16640 size = int_size_in_bytes (tree_node);
16641 break;
16642 case FIELD_DECL:
16643 /* For a data member of a struct or union, the DW_AT_byte_size is
16644 generally given as the number of bytes normally allocated for an
16645 object of the *declared* type of the member itself. This is true
16646 even for bit-fields. */
16647 size = int_size_in_bytes (field_type (tree_node));
16648 break;
16649 default:
16650 gcc_unreachable ();
16651 }
16652
16653 /* Note that `size' might be -1 when we get to this point. If it is, that
16654 indicates that the byte size of the entity in question is variable. We
16655 have no good way of expressing this fact in Dwarf at the present time,
16656 when location description was not used by the caller code instead. */
16657 if (size >= 0)
16658 add_AT_unsigned (die, DW_AT_byte_size, size);
16659 }
16660
16661 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16662 which specifies the distance in bits from the highest order bit of the
16663 "containing object" for the bit-field to the highest order bit of the
16664 bit-field itself.
16665
16666 For any given bit-field, the "containing object" is a hypothetical object
16667 (of some integral or enum type) within which the given bit-field lives. The
16668 type of this hypothetical "containing object" is always the same as the
16669 declared type of the individual bit-field itself. The determination of the
16670 exact location of the "containing object" for a bit-field is rather
16671 complicated. It's handled by the `field_byte_offset' function (above).
16672
16673 Note that it is the size (in bytes) of the hypothetical "containing object"
16674 which will be given in the DW_AT_byte_size attribute for this bit-field.
16675 (See `byte_size_attribute' above). */
16676
16677 static inline void
16678 add_bit_offset_attribute (dw_die_ref die, tree decl)
16679 {
16680 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16681 tree type = DECL_BIT_FIELD_TYPE (decl);
16682 HOST_WIDE_INT bitpos_int;
16683 HOST_WIDE_INT highest_order_object_bit_offset;
16684 HOST_WIDE_INT highest_order_field_bit_offset;
16685 HOST_WIDE_INT bit_offset;
16686
16687 /* Must be a field and a bit field. */
16688 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16689
16690 /* We can't yet handle bit-fields whose offsets are variable, so if we
16691 encounter such things, just return without generating any attribute
16692 whatsoever. Likewise for variable or too large size. */
16693 if (! tree_fits_shwi_p (bit_position (decl))
16694 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16695 return;
16696
16697 bitpos_int = int_bit_position (decl);
16698
16699 /* Note that the bit offset is always the distance (in bits) from the
16700 highest-order bit of the "containing object" to the highest-order bit of
16701 the bit-field itself. Since the "high-order end" of any object or field
16702 is different on big-endian and little-endian machines, the computation
16703 below must take account of these differences. */
16704 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16705 highest_order_field_bit_offset = bitpos_int;
16706
16707 if (! BYTES_BIG_ENDIAN)
16708 {
16709 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16710 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16711 }
16712
16713 bit_offset
16714 = (! BYTES_BIG_ENDIAN
16715 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16716 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16717
16718 if (bit_offset < 0)
16719 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16720 else
16721 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16722 }
16723
16724 /* For a FIELD_DECL node which represents a bit field, output an attribute
16725 which specifies the length in bits of the given field. */
16726
16727 static inline void
16728 add_bit_size_attribute (dw_die_ref die, tree decl)
16729 {
16730 /* Must be a field and a bit field. */
16731 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16732 && DECL_BIT_FIELD_TYPE (decl));
16733
16734 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16735 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16736 }
16737
16738 /* If the compiled language is ANSI C, then add a 'prototyped'
16739 attribute, if arg types are given for the parameters of a function. */
16740
16741 static inline void
16742 add_prototyped_attribute (dw_die_ref die, tree func_type)
16743 {
16744 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16745 {
16746 case DW_LANG_C:
16747 case DW_LANG_C89:
16748 case DW_LANG_C99:
16749 case DW_LANG_ObjC:
16750 if (prototype_p (func_type))
16751 add_AT_flag (die, DW_AT_prototyped, 1);
16752 break;
16753 default:
16754 break;
16755 }
16756 }
16757
16758 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16759 by looking in either the type declaration or object declaration
16760 equate table. */
16761
16762 static inline dw_die_ref
16763 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16764 {
16765 dw_die_ref origin_die = NULL;
16766
16767 if (TREE_CODE (origin) != FUNCTION_DECL)
16768 {
16769 /* We may have gotten separated from the block for the inlined
16770 function, if we're in an exception handler or some such; make
16771 sure that the abstract function has been written out.
16772
16773 Doing this for nested functions is wrong, however; functions are
16774 distinct units, and our context might not even be inline. */
16775 tree fn = origin;
16776
16777 if (TYPE_P (fn))
16778 fn = TYPE_STUB_DECL (fn);
16779
16780 fn = decl_function_context (fn);
16781 if (fn)
16782 dwarf2out_abstract_function (fn);
16783 }
16784
16785 if (DECL_P (origin))
16786 origin_die = lookup_decl_die (origin);
16787 else if (TYPE_P (origin))
16788 origin_die = lookup_type_die (origin);
16789
16790 /* XXX: Functions that are never lowered don't always have correct block
16791 trees (in the case of java, they simply have no block tree, in some other
16792 languages). For these functions, there is nothing we can really do to
16793 output correct debug info for inlined functions in all cases. Rather
16794 than die, we'll just produce deficient debug info now, in that we will
16795 have variables without a proper abstract origin. In the future, when all
16796 functions are lowered, we should re-add a gcc_assert (origin_die)
16797 here. */
16798
16799 if (origin_die)
16800 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16801 return origin_die;
16802 }
16803
16804 /* We do not currently support the pure_virtual attribute. */
16805
16806 static inline void
16807 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16808 {
16809 if (DECL_VINDEX (func_decl))
16810 {
16811 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16812
16813 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16814 add_AT_loc (die, DW_AT_vtable_elem_location,
16815 new_loc_descr (DW_OP_constu,
16816 tree_to_shwi (DECL_VINDEX (func_decl)),
16817 0));
16818
16819 /* GNU extension: Record what type this method came from originally. */
16820 if (debug_info_level > DINFO_LEVEL_TERSE
16821 && DECL_CONTEXT (func_decl))
16822 add_AT_die_ref (die, DW_AT_containing_type,
16823 lookup_type_die (DECL_CONTEXT (func_decl)));
16824 }
16825 }
16826 \f
16827 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16828 given decl. This used to be a vendor extension until after DWARF 4
16829 standardized it. */
16830
16831 static void
16832 add_linkage_attr (dw_die_ref die, tree decl)
16833 {
16834 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16835
16836 /* Mimic what assemble_name_raw does with a leading '*'. */
16837 if (name[0] == '*')
16838 name = &name[1];
16839
16840 if (dwarf_version >= 4)
16841 add_AT_string (die, DW_AT_linkage_name, name);
16842 else
16843 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16844 }
16845
16846 /* Add source coordinate attributes for the given decl. */
16847
16848 static void
16849 add_src_coords_attributes (dw_die_ref die, tree decl)
16850 {
16851 expanded_location s;
16852
16853 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16854 return;
16855 s = expand_location (DECL_SOURCE_LOCATION (decl));
16856 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16857 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16858 }
16859
16860 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16861
16862 static void
16863 add_linkage_name (dw_die_ref die, tree decl)
16864 {
16865 if (debug_info_level > DINFO_LEVEL_NONE
16866 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16867 && TREE_PUBLIC (decl)
16868 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16869 && die->die_tag != DW_TAG_member)
16870 {
16871 /* Defer until we have an assembler name set. */
16872 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16873 {
16874 limbo_die_node *asm_name;
16875
16876 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16877 asm_name->die = die;
16878 asm_name->created_for = decl;
16879 asm_name->next = deferred_asm_name;
16880 deferred_asm_name = asm_name;
16881 }
16882 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16883 add_linkage_attr (die, decl);
16884 }
16885 }
16886
16887 /* Add a DW_AT_name attribute and source coordinate attribute for the
16888 given decl, but only if it actually has a name. */
16889
16890 static void
16891 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16892 {
16893 tree decl_name;
16894
16895 decl_name = DECL_NAME (decl);
16896 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16897 {
16898 const char *name = dwarf2_name (decl, 0);
16899 if (name)
16900 add_name_attribute (die, name);
16901 if (! DECL_ARTIFICIAL (decl))
16902 add_src_coords_attributes (die, decl);
16903
16904 add_linkage_name (die, decl);
16905 }
16906
16907 #ifdef VMS_DEBUGGING_INFO
16908 /* Get the function's name, as described by its RTL. This may be different
16909 from the DECL_NAME name used in the source file. */
16910 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16911 {
16912 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16913 XEXP (DECL_RTL (decl), 0), false);
16914 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16915 }
16916 #endif /* VMS_DEBUGGING_INFO */
16917 }
16918
16919 #ifdef VMS_DEBUGGING_INFO
16920 /* Output the debug main pointer die for VMS */
16921
16922 void
16923 dwarf2out_vms_debug_main_pointer (void)
16924 {
16925 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16926 dw_die_ref die;
16927
16928 /* Allocate the VMS debug main subprogram die. */
16929 die = ggc_cleared_alloc<die_node> ();
16930 die->die_tag = DW_TAG_subprogram;
16931 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16932 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16933 current_function_funcdef_no);
16934 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16935
16936 /* Make it the first child of comp_unit_die (). */
16937 die->die_parent = comp_unit_die ();
16938 if (comp_unit_die ()->die_child)
16939 {
16940 die->die_sib = comp_unit_die ()->die_child->die_sib;
16941 comp_unit_die ()->die_child->die_sib = die;
16942 }
16943 else
16944 {
16945 die->die_sib = die;
16946 comp_unit_die ()->die_child = die;
16947 }
16948 }
16949 #endif /* VMS_DEBUGGING_INFO */
16950
16951 /* Push a new declaration scope. */
16952
16953 static void
16954 push_decl_scope (tree scope)
16955 {
16956 vec_safe_push (decl_scope_table, scope);
16957 }
16958
16959 /* Pop a declaration scope. */
16960
16961 static inline void
16962 pop_decl_scope (void)
16963 {
16964 decl_scope_table->pop ();
16965 }
16966
16967 /* walk_tree helper function for uses_local_type, below. */
16968
16969 static tree
16970 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16971 {
16972 if (!TYPE_P (*tp))
16973 *walk_subtrees = 0;
16974 else
16975 {
16976 tree name = TYPE_NAME (*tp);
16977 if (name && DECL_P (name) && decl_function_context (name))
16978 return *tp;
16979 }
16980 return NULL_TREE;
16981 }
16982
16983 /* If TYPE involves a function-local type (including a local typedef to a
16984 non-local type), returns that type; otherwise returns NULL_TREE. */
16985
16986 static tree
16987 uses_local_type (tree type)
16988 {
16989 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16990 return used;
16991 }
16992
16993 /* Return the DIE for the scope that immediately contains this type.
16994 Non-named types that do not involve a function-local type get global
16995 scope. Named types nested in namespaces or other types get their
16996 containing scope. All other types (i.e. function-local named types) get
16997 the current active scope. */
16998
16999 static dw_die_ref
17000 scope_die_for (tree t, dw_die_ref context_die)
17001 {
17002 dw_die_ref scope_die = NULL;
17003 tree containing_scope;
17004
17005 /* Non-types always go in the current scope. */
17006 gcc_assert (TYPE_P (t));
17007
17008 /* Use the scope of the typedef, rather than the scope of the type
17009 it refers to. */
17010 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17011 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17012 else
17013 containing_scope = TYPE_CONTEXT (t);
17014
17015 /* Use the containing namespace if there is one. */
17016 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17017 {
17018 if (context_die == lookup_decl_die (containing_scope))
17019 /* OK */;
17020 else if (debug_info_level > DINFO_LEVEL_TERSE)
17021 context_die = get_context_die (containing_scope);
17022 else
17023 containing_scope = NULL_TREE;
17024 }
17025
17026 /* Ignore function type "scopes" from the C frontend. They mean that
17027 a tagged type is local to a parmlist of a function declarator, but
17028 that isn't useful to DWARF. */
17029 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17030 containing_scope = NULL_TREE;
17031
17032 if (SCOPE_FILE_SCOPE_P (containing_scope))
17033 {
17034 /* If T uses a local type keep it local as well, to avoid references
17035 to function-local DIEs from outside the function. */
17036 if (current_function_decl && uses_local_type (t))
17037 scope_die = context_die;
17038 else
17039 scope_die = comp_unit_die ();
17040 }
17041 else if (TYPE_P (containing_scope))
17042 {
17043 /* For types, we can just look up the appropriate DIE. */
17044 if (debug_info_level > DINFO_LEVEL_TERSE)
17045 scope_die = get_context_die (containing_scope);
17046 else
17047 {
17048 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17049 if (scope_die == NULL)
17050 scope_die = comp_unit_die ();
17051 }
17052 }
17053 else
17054 scope_die = context_die;
17055
17056 return scope_die;
17057 }
17058
17059 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17060
17061 static inline int
17062 local_scope_p (dw_die_ref context_die)
17063 {
17064 for (; context_die; context_die = context_die->die_parent)
17065 if (context_die->die_tag == DW_TAG_inlined_subroutine
17066 || context_die->die_tag == DW_TAG_subprogram)
17067 return 1;
17068
17069 return 0;
17070 }
17071
17072 /* Returns nonzero if CONTEXT_DIE is a class. */
17073
17074 static inline int
17075 class_scope_p (dw_die_ref context_die)
17076 {
17077 return (context_die
17078 && (context_die->die_tag == DW_TAG_structure_type
17079 || context_die->die_tag == DW_TAG_class_type
17080 || context_die->die_tag == DW_TAG_interface_type
17081 || context_die->die_tag == DW_TAG_union_type));
17082 }
17083
17084 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17085 whether or not to treat a DIE in this context as a declaration. */
17086
17087 static inline int
17088 class_or_namespace_scope_p (dw_die_ref context_die)
17089 {
17090 return (class_scope_p (context_die)
17091 || (context_die && context_die->die_tag == DW_TAG_namespace));
17092 }
17093
17094 /* Many forms of DIEs require a "type description" attribute. This
17095 routine locates the proper "type descriptor" die for the type given
17096 by 'type' plus any additional qualifiers given by 'cv_quals', and
17097 adds a DW_AT_type attribute below the given die. */
17098
17099 static void
17100 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17101 dw_die_ref context_die)
17102 {
17103 enum tree_code code = TREE_CODE (type);
17104 dw_die_ref type_die = NULL;
17105
17106 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17107 or fixed-point type, use the inner type. This is because we have no
17108 support for unnamed types in base_type_die. This can happen if this is
17109 an Ada subrange type. Correct solution is emit a subrange type die. */
17110 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17111 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17112 type = TREE_TYPE (type), code = TREE_CODE (type);
17113
17114 if (code == ERROR_MARK
17115 /* Handle a special case. For functions whose return type is void, we
17116 generate *no* type attribute. (Note that no object may have type
17117 `void', so this only applies to function return types). */
17118 || code == VOID_TYPE)
17119 return;
17120
17121 type_die = modified_type_die (type,
17122 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17123 context_die);
17124
17125 if (type_die != NULL)
17126 add_AT_die_ref (object_die, DW_AT_type, type_die);
17127 }
17128
17129 /* Given an object die, add the calling convention attribute for the
17130 function call type. */
17131 static void
17132 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17133 {
17134 enum dwarf_calling_convention value = DW_CC_normal;
17135
17136 value = ((enum dwarf_calling_convention)
17137 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17138
17139 if (is_fortran ()
17140 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17141 {
17142 /* DWARF 2 doesn't provide a way to identify a program's source-level
17143 entry point. DW_AT_calling_convention attributes are only meant
17144 to describe functions' calling conventions. However, lacking a
17145 better way to signal the Fortran main program, we used this for
17146 a long time, following existing custom. Now, DWARF 4 has
17147 DW_AT_main_subprogram, which we add below, but some tools still
17148 rely on the old way, which we thus keep. */
17149 value = DW_CC_program;
17150
17151 if (dwarf_version >= 4 || !dwarf_strict)
17152 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17153 }
17154
17155 /* Only add the attribute if the backend requests it, and
17156 is not DW_CC_normal. */
17157 if (value && (value != DW_CC_normal))
17158 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17159 }
17160
17161 /* Given a tree pointer to a struct, class, union, or enum type node, return
17162 a pointer to the (string) tag name for the given type, or zero if the type
17163 was declared without a tag. */
17164
17165 static const char *
17166 type_tag (const_tree type)
17167 {
17168 const char *name = 0;
17169
17170 if (TYPE_NAME (type) != 0)
17171 {
17172 tree t = 0;
17173
17174 /* Find the IDENTIFIER_NODE for the type name. */
17175 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17176 && !TYPE_NAMELESS (type))
17177 t = TYPE_NAME (type);
17178
17179 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17180 a TYPE_DECL node, regardless of whether or not a `typedef' was
17181 involved. */
17182 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17183 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17184 {
17185 /* We want to be extra verbose. Don't call dwarf_name if
17186 DECL_NAME isn't set. The default hook for decl_printable_name
17187 doesn't like that, and in this context it's correct to return
17188 0, instead of "<anonymous>" or the like. */
17189 if (DECL_NAME (TYPE_NAME (type))
17190 && !DECL_NAMELESS (TYPE_NAME (type)))
17191 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17192 }
17193
17194 /* Now get the name as a string, or invent one. */
17195 if (!name && t != 0)
17196 name = IDENTIFIER_POINTER (t);
17197 }
17198
17199 return (name == 0 || *name == '\0') ? 0 : name;
17200 }
17201
17202 /* Return the type associated with a data member, make a special check
17203 for bit field types. */
17204
17205 static inline tree
17206 member_declared_type (const_tree member)
17207 {
17208 return (DECL_BIT_FIELD_TYPE (member)
17209 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17210 }
17211
17212 /* Get the decl's label, as described by its RTL. This may be different
17213 from the DECL_NAME name used in the source file. */
17214
17215 #if 0
17216 static const char *
17217 decl_start_label (tree decl)
17218 {
17219 rtx x;
17220 const char *fnname;
17221
17222 x = DECL_RTL (decl);
17223 gcc_assert (MEM_P (x));
17224
17225 x = XEXP (x, 0);
17226 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17227
17228 fnname = XSTR (x, 0);
17229 return fnname;
17230 }
17231 #endif
17232 \f
17233 /* These routines generate the internal representation of the DIE's for
17234 the compilation unit. Debugging information is collected by walking
17235 the declaration trees passed in from dwarf2out_decl(). */
17236
17237 static void
17238 gen_array_type_die (tree type, dw_die_ref context_die)
17239 {
17240 dw_die_ref scope_die = scope_die_for (type, context_die);
17241 dw_die_ref array_die;
17242
17243 /* GNU compilers represent multidimensional array types as sequences of one
17244 dimensional array types whose element types are themselves array types.
17245 We sometimes squish that down to a single array_type DIE with multiple
17246 subscripts in the Dwarf debugging info. The draft Dwarf specification
17247 say that we are allowed to do this kind of compression in C, because
17248 there is no difference between an array of arrays and a multidimensional
17249 array. We don't do this for Ada to remain as close as possible to the
17250 actual representation, which is especially important against the language
17251 flexibilty wrt arrays of variable size. */
17252
17253 bool collapse_nested_arrays = !is_ada ();
17254 tree element_type;
17255
17256 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17257 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17258 if (TYPE_STRING_FLAG (type)
17259 && TREE_CODE (type) == ARRAY_TYPE
17260 && is_fortran ()
17261 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17262 {
17263 HOST_WIDE_INT size;
17264
17265 array_die = new_die (DW_TAG_string_type, scope_die, type);
17266 add_name_attribute (array_die, type_tag (type));
17267 equate_type_number_to_die (type, array_die);
17268 size = int_size_in_bytes (type);
17269 if (size >= 0)
17270 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17271 else if (TYPE_DOMAIN (type) != NULL_TREE
17272 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17273 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17274 {
17275 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17276 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17277
17278 size = int_size_in_bytes (TREE_TYPE (szdecl));
17279 if (loc && size > 0)
17280 {
17281 add_AT_location_description (array_die, DW_AT_string_length, loc);
17282 if (size != DWARF2_ADDR_SIZE)
17283 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17284 }
17285 }
17286 return;
17287 }
17288
17289 array_die = new_die (DW_TAG_array_type, scope_die, type);
17290 add_name_attribute (array_die, type_tag (type));
17291 equate_type_number_to_die (type, array_die);
17292
17293 if (TREE_CODE (type) == VECTOR_TYPE)
17294 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17295
17296 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17297 if (is_fortran ()
17298 && TREE_CODE (type) == ARRAY_TYPE
17299 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17300 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17301 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17302
17303 #if 0
17304 /* We default the array ordering. SDB will probably do
17305 the right things even if DW_AT_ordering is not present. It's not even
17306 an issue until we start to get into multidimensional arrays anyway. If
17307 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17308 then we'll have to put the DW_AT_ordering attribute back in. (But if
17309 and when we find out that we need to put these in, we will only do so
17310 for multidimensional arrays. */
17311 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17312 #endif
17313
17314 if (TREE_CODE (type) == VECTOR_TYPE)
17315 {
17316 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17317 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17318 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17319 add_bound_info (subrange_die, DW_AT_upper_bound,
17320 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17321 }
17322 else
17323 add_subscript_info (array_die, type, collapse_nested_arrays);
17324
17325 /* Add representation of the type of the elements of this array type and
17326 emit the corresponding DIE if we haven't done it already. */
17327 element_type = TREE_TYPE (type);
17328 if (collapse_nested_arrays)
17329 while (TREE_CODE (element_type) == ARRAY_TYPE)
17330 {
17331 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17332 break;
17333 element_type = TREE_TYPE (element_type);
17334 }
17335
17336 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17337
17338 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17339 if (TYPE_ARTIFICIAL (type))
17340 add_AT_flag (array_die, DW_AT_artificial, 1);
17341
17342 if (get_AT (array_die, DW_AT_name))
17343 add_pubtype (type, array_die);
17344 }
17345
17346 static dw_loc_descr_ref
17347 descr_info_loc (tree val, tree base_decl)
17348 {
17349 HOST_WIDE_INT size;
17350 dw_loc_descr_ref loc, loc2;
17351 enum dwarf_location_atom op;
17352
17353 if (val == base_decl)
17354 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17355
17356 switch (TREE_CODE (val))
17357 {
17358 CASE_CONVERT:
17359 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17360 case VAR_DECL:
17361 return loc_descriptor_from_tree (val, 0);
17362 case INTEGER_CST:
17363 if (tree_fits_shwi_p (val))
17364 return int_loc_descriptor (tree_to_shwi (val));
17365 break;
17366 case INDIRECT_REF:
17367 size = int_size_in_bytes (TREE_TYPE (val));
17368 if (size < 0)
17369 break;
17370 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17371 if (!loc)
17372 break;
17373 if (size == DWARF2_ADDR_SIZE)
17374 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17375 else
17376 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17377 return loc;
17378 case POINTER_PLUS_EXPR:
17379 case PLUS_EXPR:
17380 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17381 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17382 {
17383 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17384 if (!loc)
17385 break;
17386 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17387 }
17388 else
17389 {
17390 op = DW_OP_plus;
17391 do_binop:
17392 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17393 if (!loc)
17394 break;
17395 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17396 if (!loc2)
17397 break;
17398 add_loc_descr (&loc, loc2);
17399 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17400 }
17401 return loc;
17402 case MINUS_EXPR:
17403 op = DW_OP_minus;
17404 goto do_binop;
17405 case MULT_EXPR:
17406 op = DW_OP_mul;
17407 goto do_binop;
17408 case EQ_EXPR:
17409 op = DW_OP_eq;
17410 goto do_binop;
17411 case NE_EXPR:
17412 op = DW_OP_ne;
17413 goto do_binop;
17414 default:
17415 break;
17416 }
17417 return NULL;
17418 }
17419
17420 static void
17421 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17422 tree val, tree base_decl)
17423 {
17424 dw_loc_descr_ref loc;
17425
17426 if (tree_fits_shwi_p (val))
17427 {
17428 add_AT_unsigned (die, attr, tree_to_shwi (val));
17429 return;
17430 }
17431
17432 loc = descr_info_loc (val, base_decl);
17433 if (!loc)
17434 return;
17435
17436 add_AT_loc (die, attr, loc);
17437 }
17438
17439 /* This routine generates DIE for array with hidden descriptor, details
17440 are filled into *info by a langhook. */
17441
17442 static void
17443 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17444 dw_die_ref context_die)
17445 {
17446 dw_die_ref scope_die = scope_die_for (type, context_die);
17447 dw_die_ref array_die;
17448 int dim;
17449
17450 array_die = new_die (DW_TAG_array_type, scope_die, type);
17451 add_name_attribute (array_die, type_tag (type));
17452 equate_type_number_to_die (type, array_die);
17453
17454 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17455 if (is_fortran ()
17456 && info->ndimensions >= 2)
17457 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17458
17459 if (info->data_location)
17460 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17461 info->base_decl);
17462 if (info->associated)
17463 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17464 info->base_decl);
17465 if (info->allocated)
17466 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17467 info->base_decl);
17468
17469 for (dim = 0; dim < info->ndimensions; dim++)
17470 {
17471 dw_die_ref subrange_die
17472 = new_die (DW_TAG_subrange_type, array_die, NULL);
17473
17474 if (info->dimen[dim].lower_bound)
17475 {
17476 /* If it is the default value, omit it. */
17477 int dflt;
17478
17479 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17480 && (dflt = lower_bound_default ()) != -1
17481 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17482 ;
17483 else
17484 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17485 info->dimen[dim].lower_bound,
17486 info->base_decl);
17487 }
17488 if (info->dimen[dim].upper_bound)
17489 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17490 info->dimen[dim].upper_bound,
17491 info->base_decl);
17492 if (info->dimen[dim].stride)
17493 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17494 info->dimen[dim].stride,
17495 info->base_decl);
17496 }
17497
17498 gen_type_die (info->element_type, context_die);
17499 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17500 context_die);
17501
17502 if (get_AT (array_die, DW_AT_name))
17503 add_pubtype (type, array_die);
17504 }
17505
17506 #if 0
17507 static void
17508 gen_entry_point_die (tree decl, dw_die_ref context_die)
17509 {
17510 tree origin = decl_ultimate_origin (decl);
17511 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17512
17513 if (origin != NULL)
17514 add_abstract_origin_attribute (decl_die, origin);
17515 else
17516 {
17517 add_name_and_src_coords_attributes (decl_die, decl);
17518 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17519 TYPE_UNQUALIFIED, context_die);
17520 }
17521
17522 if (DECL_ABSTRACT_P (decl))
17523 equate_decl_number_to_die (decl, decl_die);
17524 else
17525 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17526 }
17527 #endif
17528
17529 /* Walk through the list of incomplete types again, trying once more to
17530 emit full debugging info for them. */
17531
17532 static void
17533 retry_incomplete_types (void)
17534 {
17535 int i;
17536
17537 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17538 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17539 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17540 }
17541
17542 /* Determine what tag to use for a record type. */
17543
17544 static enum dwarf_tag
17545 record_type_tag (tree type)
17546 {
17547 if (! lang_hooks.types.classify_record)
17548 return DW_TAG_structure_type;
17549
17550 switch (lang_hooks.types.classify_record (type))
17551 {
17552 case RECORD_IS_STRUCT:
17553 return DW_TAG_structure_type;
17554
17555 case RECORD_IS_CLASS:
17556 return DW_TAG_class_type;
17557
17558 case RECORD_IS_INTERFACE:
17559 if (dwarf_version >= 3 || !dwarf_strict)
17560 return DW_TAG_interface_type;
17561 return DW_TAG_structure_type;
17562
17563 default:
17564 gcc_unreachable ();
17565 }
17566 }
17567
17568 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17569 include all of the information about the enumeration values also. Each
17570 enumerated type name/value is listed as a child of the enumerated type
17571 DIE. */
17572
17573 static dw_die_ref
17574 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17575 {
17576 dw_die_ref type_die = lookup_type_die (type);
17577
17578 if (type_die == NULL)
17579 {
17580 type_die = new_die (DW_TAG_enumeration_type,
17581 scope_die_for (type, context_die), type);
17582 equate_type_number_to_die (type, type_die);
17583 add_name_attribute (type_die, type_tag (type));
17584 if (dwarf_version >= 4 || !dwarf_strict)
17585 {
17586 if (ENUM_IS_SCOPED (type))
17587 add_AT_flag (type_die, DW_AT_enum_class, 1);
17588 if (ENUM_IS_OPAQUE (type))
17589 add_AT_flag (type_die, DW_AT_declaration, 1);
17590 }
17591 }
17592 else if (! TYPE_SIZE (type))
17593 return type_die;
17594 else
17595 remove_AT (type_die, DW_AT_declaration);
17596
17597 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17598 given enum type is incomplete, do not generate the DW_AT_byte_size
17599 attribute or the DW_AT_element_list attribute. */
17600 if (TYPE_SIZE (type))
17601 {
17602 tree link;
17603
17604 TREE_ASM_WRITTEN (type) = 1;
17605 add_byte_size_attribute (type_die, type);
17606 if (dwarf_version >= 3 || !dwarf_strict)
17607 {
17608 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17609 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17610 context_die);
17611 }
17612 if (TYPE_STUB_DECL (type) != NULL_TREE)
17613 {
17614 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17615 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17616 }
17617
17618 /* If the first reference to this type was as the return type of an
17619 inline function, then it may not have a parent. Fix this now. */
17620 if (type_die->die_parent == NULL)
17621 add_child_die (scope_die_for (type, context_die), type_die);
17622
17623 for (link = TYPE_VALUES (type);
17624 link != NULL; link = TREE_CHAIN (link))
17625 {
17626 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17627 tree value = TREE_VALUE (link);
17628
17629 add_name_attribute (enum_die,
17630 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17631
17632 if (TREE_CODE (value) == CONST_DECL)
17633 value = DECL_INITIAL (value);
17634
17635 if (simple_type_size_in_bits (TREE_TYPE (value))
17636 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17637 {
17638 /* For constant forms created by add_AT_unsigned DWARF
17639 consumers (GDB, elfutils, etc.) always zero extend
17640 the value. Only when the actual value is negative
17641 do we need to use add_AT_int to generate a constant
17642 form that can represent negative values. */
17643 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17644 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17645 add_AT_unsigned (enum_die, DW_AT_const_value,
17646 (unsigned HOST_WIDE_INT) val);
17647 else
17648 add_AT_int (enum_die, DW_AT_const_value, val);
17649 }
17650 else
17651 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17652 that here. TODO: This should be re-worked to use correct
17653 signed/unsigned double tags for all cases. */
17654 add_AT_wide (enum_die, DW_AT_const_value, value);
17655 }
17656
17657 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17658 if (TYPE_ARTIFICIAL (type))
17659 add_AT_flag (type_die, DW_AT_artificial, 1);
17660 }
17661 else
17662 add_AT_flag (type_die, DW_AT_declaration, 1);
17663
17664 add_pubtype (type, type_die);
17665
17666 return type_die;
17667 }
17668
17669 /* Generate a DIE to represent either a real live formal parameter decl or to
17670 represent just the type of some formal parameter position in some function
17671 type.
17672
17673 Note that this routine is a bit unusual because its argument may be a
17674 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17675 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17676 node. If it's the former then this function is being called to output a
17677 DIE to represent a formal parameter object (or some inlining thereof). If
17678 it's the latter, then this function is only being called to output a
17679 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17680 argument type of some subprogram type.
17681 If EMIT_NAME_P is true, name and source coordinate attributes
17682 are emitted. */
17683
17684 static dw_die_ref
17685 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17686 dw_die_ref context_die)
17687 {
17688 tree node_or_origin = node ? node : origin;
17689 tree ultimate_origin;
17690 dw_die_ref parm_die
17691 = new_die (DW_TAG_formal_parameter, context_die, node);
17692
17693 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17694 {
17695 case tcc_declaration:
17696 ultimate_origin = decl_ultimate_origin (node_or_origin);
17697 if (node || ultimate_origin)
17698 origin = ultimate_origin;
17699 if (origin != NULL)
17700 add_abstract_origin_attribute (parm_die, origin);
17701 else if (emit_name_p)
17702 add_name_and_src_coords_attributes (parm_die, node);
17703 if (origin == NULL
17704 || (! DECL_ABSTRACT_P (node_or_origin)
17705 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17706 decl_function_context
17707 (node_or_origin))))
17708 {
17709 tree type = TREE_TYPE (node_or_origin);
17710 if (decl_by_reference_p (node_or_origin))
17711 add_type_attribute (parm_die, TREE_TYPE (type),
17712 TYPE_UNQUALIFIED, context_die);
17713 else
17714 add_type_attribute (parm_die, type,
17715 decl_quals (node_or_origin),
17716 context_die);
17717 }
17718 if (origin == NULL && DECL_ARTIFICIAL (node))
17719 add_AT_flag (parm_die, DW_AT_artificial, 1);
17720
17721 if (node && node != origin)
17722 equate_decl_number_to_die (node, parm_die);
17723 if (! DECL_ABSTRACT_P (node_or_origin))
17724 add_location_or_const_value_attribute (parm_die, node_or_origin,
17725 node == NULL, DW_AT_location);
17726
17727 break;
17728
17729 case tcc_type:
17730 /* We were called with some kind of a ..._TYPE node. */
17731 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17732 context_die);
17733 break;
17734
17735 default:
17736 gcc_unreachable ();
17737 }
17738
17739 return parm_die;
17740 }
17741
17742 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17743 children DW_TAG_formal_parameter DIEs representing the arguments of the
17744 parameter pack.
17745
17746 PARM_PACK must be a function parameter pack.
17747 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17748 must point to the subsequent arguments of the function PACK_ARG belongs to.
17749 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17750 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17751 following the last one for which a DIE was generated. */
17752
17753 static dw_die_ref
17754 gen_formal_parameter_pack_die (tree parm_pack,
17755 tree pack_arg,
17756 dw_die_ref subr_die,
17757 tree *next_arg)
17758 {
17759 tree arg;
17760 dw_die_ref parm_pack_die;
17761
17762 gcc_assert (parm_pack
17763 && lang_hooks.function_parameter_pack_p (parm_pack)
17764 && subr_die);
17765
17766 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17767 add_src_coords_attributes (parm_pack_die, parm_pack);
17768
17769 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17770 {
17771 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17772 parm_pack))
17773 break;
17774 gen_formal_parameter_die (arg, NULL,
17775 false /* Don't emit name attribute. */,
17776 parm_pack_die);
17777 }
17778 if (next_arg)
17779 *next_arg = arg;
17780 return parm_pack_die;
17781 }
17782
17783 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17784 at the end of an (ANSI prototyped) formal parameters list. */
17785
17786 static void
17787 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17788 {
17789 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17790 }
17791
17792 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17793 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17794 parameters as specified in some function type specification (except for
17795 those which appear as part of a function *definition*). */
17796
17797 static void
17798 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17799 {
17800 tree link;
17801 tree formal_type = NULL;
17802 tree first_parm_type;
17803 tree arg;
17804
17805 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17806 {
17807 arg = DECL_ARGUMENTS (function_or_method_type);
17808 function_or_method_type = TREE_TYPE (function_or_method_type);
17809 }
17810 else
17811 arg = NULL_TREE;
17812
17813 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17814
17815 /* Make our first pass over the list of formal parameter types and output a
17816 DW_TAG_formal_parameter DIE for each one. */
17817 for (link = first_parm_type; link; )
17818 {
17819 dw_die_ref parm_die;
17820
17821 formal_type = TREE_VALUE (link);
17822 if (formal_type == void_type_node)
17823 break;
17824
17825 /* Output a (nameless) DIE to represent the formal parameter itself. */
17826 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17827 {
17828 parm_die = gen_formal_parameter_die (formal_type, NULL,
17829 true /* Emit name attribute. */,
17830 context_die);
17831 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17832 && link == first_parm_type)
17833 {
17834 add_AT_flag (parm_die, DW_AT_artificial, 1);
17835 if (dwarf_version >= 3 || !dwarf_strict)
17836 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17837 }
17838 else if (arg && DECL_ARTIFICIAL (arg))
17839 add_AT_flag (parm_die, DW_AT_artificial, 1);
17840 }
17841
17842 link = TREE_CHAIN (link);
17843 if (arg)
17844 arg = DECL_CHAIN (arg);
17845 }
17846
17847 /* If this function type has an ellipsis, add a
17848 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17849 if (formal_type != void_type_node)
17850 gen_unspecified_parameters_die (function_or_method_type, context_die);
17851
17852 /* Make our second (and final) pass over the list of formal parameter types
17853 and output DIEs to represent those types (as necessary). */
17854 for (link = TYPE_ARG_TYPES (function_or_method_type);
17855 link && TREE_VALUE (link);
17856 link = TREE_CHAIN (link))
17857 gen_type_die (TREE_VALUE (link), context_die);
17858 }
17859
17860 /* We want to generate the DIE for TYPE so that we can generate the
17861 die for MEMBER, which has been defined; we will need to refer back
17862 to the member declaration nested within TYPE. If we're trying to
17863 generate minimal debug info for TYPE, processing TYPE won't do the
17864 trick; we need to attach the member declaration by hand. */
17865
17866 static void
17867 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17868 {
17869 gen_type_die (type, context_die);
17870
17871 /* If we're trying to avoid duplicate debug info, we may not have
17872 emitted the member decl for this function. Emit it now. */
17873 if (TYPE_STUB_DECL (type)
17874 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17875 && ! lookup_decl_die (member))
17876 {
17877 dw_die_ref type_die;
17878 gcc_assert (!decl_ultimate_origin (member));
17879
17880 push_decl_scope (type);
17881 type_die = lookup_type_die_strip_naming_typedef (type);
17882 if (TREE_CODE (member) == FUNCTION_DECL)
17883 gen_subprogram_die (member, type_die);
17884 else if (TREE_CODE (member) == FIELD_DECL)
17885 {
17886 /* Ignore the nameless fields that are used to skip bits but handle
17887 C++ anonymous unions and structs. */
17888 if (DECL_NAME (member) != NULL_TREE
17889 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17890 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17891 {
17892 gen_type_die (member_declared_type (member), type_die);
17893 gen_field_die (member, type_die);
17894 }
17895 }
17896 else
17897 gen_variable_die (member, NULL_TREE, type_die);
17898
17899 pop_decl_scope ();
17900 }
17901 }
17902 \f
17903 /* Forward declare these functions, because they are mutually recursive
17904 with their set_block_* pairing functions. */
17905 static void set_decl_origin_self (tree);
17906 static void set_decl_abstract_flags (tree, int);
17907
17908 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17909 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17910 that it points to the node itself, thus indicating that the node is its
17911 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17912 the given node is NULL, recursively descend the decl/block tree which
17913 it is the root of, and for each other ..._DECL or BLOCK node contained
17914 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17915 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17916 values to point to themselves. */
17917
17918 static void
17919 set_block_origin_self (tree stmt)
17920 {
17921 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17922 {
17923 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17924
17925 {
17926 tree local_decl;
17927
17928 for (local_decl = BLOCK_VARS (stmt);
17929 local_decl != NULL_TREE;
17930 local_decl = DECL_CHAIN (local_decl))
17931 if (! DECL_EXTERNAL (local_decl))
17932 set_decl_origin_self (local_decl); /* Potential recursion. */
17933 }
17934
17935 {
17936 tree subblock;
17937
17938 for (subblock = BLOCK_SUBBLOCKS (stmt);
17939 subblock != NULL_TREE;
17940 subblock = BLOCK_CHAIN (subblock))
17941 set_block_origin_self (subblock); /* Recurse. */
17942 }
17943 }
17944 }
17945
17946 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17947 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17948 node to so that it points to the node itself, thus indicating that the
17949 node represents its own (abstract) origin. Additionally, if the
17950 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17951 the decl/block tree of which the given node is the root of, and for
17952 each other ..._DECL or BLOCK node contained therein whose
17953 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17954 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17955 point to themselves. */
17956
17957 static void
17958 set_decl_origin_self (tree decl)
17959 {
17960 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17961 {
17962 DECL_ABSTRACT_ORIGIN (decl) = decl;
17963 if (TREE_CODE (decl) == FUNCTION_DECL)
17964 {
17965 tree arg;
17966
17967 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17968 DECL_ABSTRACT_ORIGIN (arg) = arg;
17969 if (DECL_INITIAL (decl) != NULL_TREE
17970 && DECL_INITIAL (decl) != error_mark_node)
17971 set_block_origin_self (DECL_INITIAL (decl));
17972 }
17973 }
17974 }
17975 \f
17976 /* Given a pointer to some BLOCK node, and a boolean value to set the
17977 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17978 the given block, and for all local decls and all local sub-blocks
17979 (recursively) which are contained therein. */
17980
17981 static void
17982 set_block_abstract_flags (tree stmt, int setting)
17983 {
17984 tree local_decl;
17985 tree subblock;
17986 unsigned int i;
17987
17988 BLOCK_ABSTRACT (stmt) = setting;
17989
17990 for (local_decl = BLOCK_VARS (stmt);
17991 local_decl != NULL_TREE;
17992 local_decl = DECL_CHAIN (local_decl))
17993 if (! DECL_EXTERNAL (local_decl))
17994 set_decl_abstract_flags (local_decl, setting);
17995
17996 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17997 {
17998 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17999 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18000 || TREE_CODE (local_decl) == PARM_DECL)
18001 set_decl_abstract_flags (local_decl, setting);
18002 }
18003
18004 for (subblock = BLOCK_SUBBLOCKS (stmt);
18005 subblock != NULL_TREE;
18006 subblock = BLOCK_CHAIN (subblock))
18007 set_block_abstract_flags (subblock, setting);
18008 }
18009
18010 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18011 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18012 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18013 set the abstract flags for all of the parameters, local vars, local
18014 blocks and sub-blocks (recursively) to the same setting. */
18015
18016 static void
18017 set_decl_abstract_flags (tree decl, int setting)
18018 {
18019 DECL_ABSTRACT_P (decl) = setting;
18020 if (TREE_CODE (decl) == FUNCTION_DECL)
18021 {
18022 tree arg;
18023
18024 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18025 DECL_ABSTRACT_P (arg) = setting;
18026 if (DECL_INITIAL (decl) != NULL_TREE
18027 && DECL_INITIAL (decl) != error_mark_node)
18028 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18029 }
18030 }
18031
18032 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18033 may later generate inlined and/or out-of-line instances of. */
18034
18035 static void
18036 dwarf2out_abstract_function (tree decl)
18037 {
18038 dw_die_ref old_die;
18039 tree save_fn;
18040 tree context;
18041 int was_abstract;
18042 hash_table<decl_loc_hasher> *old_decl_loc_table;
18043 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18044 int old_call_site_count, old_tail_call_site_count;
18045 struct call_arg_loc_node *old_call_arg_locations;
18046
18047 /* Make sure we have the actual abstract inline, not a clone. */
18048 decl = DECL_ORIGIN (decl);
18049
18050 old_die = lookup_decl_die (decl);
18051 if (old_die && get_AT (old_die, DW_AT_inline))
18052 /* We've already generated the abstract instance. */
18053 return;
18054
18055 /* We can be called while recursively when seeing block defining inlined subroutine
18056 DIE. Be sure to not clobber the outer location table nor use it or we would
18057 get locations in abstract instantces. */
18058 old_decl_loc_table = decl_loc_table;
18059 decl_loc_table = NULL;
18060 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18061 cached_dw_loc_list_table = NULL;
18062 old_call_arg_locations = call_arg_locations;
18063 call_arg_locations = NULL;
18064 old_call_site_count = call_site_count;
18065 call_site_count = -1;
18066 old_tail_call_site_count = tail_call_site_count;
18067 tail_call_site_count = -1;
18068
18069 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18070 we don't get confused by DECL_ABSTRACT_P. */
18071 if (debug_info_level > DINFO_LEVEL_TERSE)
18072 {
18073 context = decl_class_context (decl);
18074 if (context)
18075 gen_type_die_for_member
18076 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18077 }
18078
18079 /* Pretend we've just finished compiling this function. */
18080 save_fn = current_function_decl;
18081 current_function_decl = decl;
18082
18083 was_abstract = DECL_ABSTRACT_P (decl);
18084 set_decl_abstract_flags (decl, 1);
18085 dwarf2out_decl (decl);
18086 if (! was_abstract)
18087 set_decl_abstract_flags (decl, 0);
18088
18089 current_function_decl = save_fn;
18090 decl_loc_table = old_decl_loc_table;
18091 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18092 call_arg_locations = old_call_arg_locations;
18093 call_site_count = old_call_site_count;
18094 tail_call_site_count = old_tail_call_site_count;
18095 }
18096
18097 /* Helper function of premark_used_types() which gets called through
18098 htab_traverse.
18099
18100 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18101 marked as unused by prune_unused_types. */
18102
18103 bool
18104 premark_used_types_helper (tree const &type, void *)
18105 {
18106 dw_die_ref die;
18107
18108 die = lookup_type_die (type);
18109 if (die != NULL)
18110 die->die_perennial_p = 1;
18111 return true;
18112 }
18113
18114 /* Helper function of premark_types_used_by_global_vars which gets called
18115 through htab_traverse.
18116
18117 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18118 marked as unused by prune_unused_types. The DIE of the type is marked
18119 only if the global variable using the type will actually be emitted. */
18120
18121 int
18122 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18123 void *)
18124 {
18125 struct types_used_by_vars_entry *entry;
18126 dw_die_ref die;
18127
18128 entry = (struct types_used_by_vars_entry *) *slot;
18129 gcc_assert (entry->type != NULL
18130 && entry->var_decl != NULL);
18131 die = lookup_type_die (entry->type);
18132 if (die)
18133 {
18134 /* Ask cgraph if the global variable really is to be emitted.
18135 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18136 varpool_node *node = varpool_node::get (entry->var_decl);
18137 if (node && node->definition)
18138 {
18139 die->die_perennial_p = 1;
18140 /* Keep the parent DIEs as well. */
18141 while ((die = die->die_parent) && die->die_perennial_p == 0)
18142 die->die_perennial_p = 1;
18143 }
18144 }
18145 return 1;
18146 }
18147
18148 /* Mark all members of used_types_hash as perennial. */
18149
18150 static void
18151 premark_used_types (struct function *fun)
18152 {
18153 if (fun && fun->used_types_hash)
18154 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18155 }
18156
18157 /* Mark all members of types_used_by_vars_entry as perennial. */
18158
18159 static void
18160 premark_types_used_by_global_vars (void)
18161 {
18162 if (types_used_by_vars_hash)
18163 types_used_by_vars_hash
18164 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18165 }
18166
18167 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18168 for CA_LOC call arg loc node. */
18169
18170 static dw_die_ref
18171 gen_call_site_die (tree decl, dw_die_ref subr_die,
18172 struct call_arg_loc_node *ca_loc)
18173 {
18174 dw_die_ref stmt_die = NULL, die;
18175 tree block = ca_loc->block;
18176
18177 while (block
18178 && block != DECL_INITIAL (decl)
18179 && TREE_CODE (block) == BLOCK)
18180 {
18181 if (block_map.length () > BLOCK_NUMBER (block))
18182 stmt_die = block_map[BLOCK_NUMBER (block)];
18183 if (stmt_die)
18184 break;
18185 block = BLOCK_SUPERCONTEXT (block);
18186 }
18187 if (stmt_die == NULL)
18188 stmt_die = subr_die;
18189 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18190 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18191 if (ca_loc->tail_call_p)
18192 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18193 if (ca_loc->symbol_ref)
18194 {
18195 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18196 if (tdie)
18197 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18198 else
18199 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18200 }
18201 return die;
18202 }
18203
18204 /* Generate a DIE to represent a declared function (either file-scope or
18205 block-local). */
18206
18207 static void
18208 gen_subprogram_die (tree decl, dw_die_ref context_die)
18209 {
18210 tree origin = decl_ultimate_origin (decl);
18211 dw_die_ref subr_die;
18212 tree outer_scope;
18213 dw_die_ref old_die = lookup_decl_die (decl);
18214 int declaration = (current_function_decl != decl
18215 || class_or_namespace_scope_p (context_die));
18216
18217 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18218
18219 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18220 started to generate the abstract instance of an inline, decided to output
18221 its containing class, and proceeded to emit the declaration of the inline
18222 from the member list for the class. If so, DECLARATION takes priority;
18223 we'll get back to the abstract instance when done with the class. */
18224
18225 /* The class-scope declaration DIE must be the primary DIE. */
18226 if (origin && declaration && class_or_namespace_scope_p (context_die))
18227 {
18228 origin = NULL;
18229 gcc_assert (!old_die);
18230 }
18231
18232 /* Now that the C++ front end lazily declares artificial member fns, we
18233 might need to retrofit the declaration into its class. */
18234 if (!declaration && !origin && !old_die
18235 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18236 && !class_or_namespace_scope_p (context_die)
18237 && debug_info_level > DINFO_LEVEL_TERSE)
18238 old_die = force_decl_die (decl);
18239
18240 if (origin != NULL)
18241 {
18242 gcc_assert (!declaration || local_scope_p (context_die));
18243
18244 /* Fixup die_parent for the abstract instance of a nested
18245 inline function. */
18246 if (old_die && old_die->die_parent == NULL)
18247 add_child_die (context_die, old_die);
18248
18249 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18250 add_abstract_origin_attribute (subr_die, origin);
18251 /* This is where the actual code for a cloned function is.
18252 Let's emit linkage name attribute for it. This helps
18253 debuggers to e.g, set breakpoints into
18254 constructors/destructors when the user asks "break
18255 K::K". */
18256 add_linkage_name (subr_die, decl);
18257 }
18258 else if (old_die)
18259 {
18260 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18261 struct dwarf_file_data * file_index = lookup_filename (s.file);
18262
18263 if (!get_AT_flag (old_die, DW_AT_declaration)
18264 /* We can have a normal definition following an inline one in the
18265 case of redefinition of GNU C extern inlines.
18266 It seems reasonable to use AT_specification in this case. */
18267 && !get_AT (old_die, DW_AT_inline))
18268 {
18269 /* Detect and ignore this case, where we are trying to output
18270 something we have already output. */
18271 return;
18272 }
18273
18274 /* If the definition comes from the same place as the declaration,
18275 maybe use the old DIE. We always want the DIE for this function
18276 that has the *_pc attributes to be under comp_unit_die so the
18277 debugger can find it. We also need to do this for abstract
18278 instances of inlines, since the spec requires the out-of-line copy
18279 to have the same parent. For local class methods, this doesn't
18280 apply; we just use the old DIE. */
18281 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18282 && (DECL_ARTIFICIAL (decl)
18283 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18284 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18285 == (unsigned) s.line))))
18286 {
18287 subr_die = old_die;
18288
18289 /* Clear out the declaration attribute and the formal parameters.
18290 Do not remove all children, because it is possible that this
18291 declaration die was forced using force_decl_die(). In such
18292 cases die that forced declaration die (e.g. TAG_imported_module)
18293 is one of the children that we do not want to remove. */
18294 remove_AT (subr_die, DW_AT_declaration);
18295 remove_AT (subr_die, DW_AT_object_pointer);
18296 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18297 }
18298 else
18299 {
18300 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18301 add_AT_specification (subr_die, old_die);
18302 add_pubname (decl, subr_die);
18303 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18304 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18305 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18306 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18307
18308 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18309 emit the real type on the definition die. */
18310 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18311 {
18312 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18313 if (die == auto_die || die == decltype_auto_die)
18314 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18315 TYPE_UNQUALIFIED, context_die);
18316 }
18317 }
18318 }
18319 else
18320 {
18321 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18322
18323 if (TREE_PUBLIC (decl))
18324 add_AT_flag (subr_die, DW_AT_external, 1);
18325
18326 add_name_and_src_coords_attributes (subr_die, decl);
18327 add_pubname (decl, subr_die);
18328 if (debug_info_level > DINFO_LEVEL_TERSE)
18329 {
18330 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18331 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18332 TYPE_UNQUALIFIED, context_die);
18333 }
18334
18335 add_pure_or_virtual_attribute (subr_die, decl);
18336 if (DECL_ARTIFICIAL (decl))
18337 add_AT_flag (subr_die, DW_AT_artificial, 1);
18338
18339 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18340 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18341
18342 add_accessibility_attribute (subr_die, decl);
18343 }
18344
18345 if (declaration)
18346 {
18347 if (!old_die || !get_AT (old_die, DW_AT_inline))
18348 {
18349 add_AT_flag (subr_die, DW_AT_declaration, 1);
18350
18351 /* If this is an explicit function declaration then generate
18352 a DW_AT_explicit attribute. */
18353 if (lang_hooks.decls.function_decl_explicit_p (decl)
18354 && (dwarf_version >= 3 || !dwarf_strict))
18355 add_AT_flag (subr_die, DW_AT_explicit, 1);
18356
18357 /* If this is a C++11 deleted special function member then generate
18358 a DW_AT_GNU_deleted attribute. */
18359 if (lang_hooks.decls.function_decl_deleted_p (decl)
18360 && (! dwarf_strict))
18361 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18362
18363 /* The first time we see a member function, it is in the context of
18364 the class to which it belongs. We make sure of this by emitting
18365 the class first. The next time is the definition, which is
18366 handled above. The two may come from the same source text.
18367
18368 Note that force_decl_die() forces function declaration die. It is
18369 later reused to represent definition. */
18370 equate_decl_number_to_die (decl, subr_die);
18371 }
18372 }
18373 else if (DECL_ABSTRACT_P (decl))
18374 {
18375 if (DECL_DECLARED_INLINE_P (decl))
18376 {
18377 if (cgraph_function_possibly_inlined_p (decl))
18378 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18379 else
18380 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18381 }
18382 else
18383 {
18384 if (cgraph_function_possibly_inlined_p (decl))
18385 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18386 else
18387 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18388 }
18389
18390 if (DECL_DECLARED_INLINE_P (decl)
18391 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18392 add_AT_flag (subr_die, DW_AT_artificial, 1);
18393
18394 equate_decl_number_to_die (decl, subr_die);
18395 }
18396 else if (!DECL_EXTERNAL (decl))
18397 {
18398 HOST_WIDE_INT cfa_fb_offset;
18399 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18400
18401 if (!old_die || !get_AT (old_die, DW_AT_inline))
18402 equate_decl_number_to_die (decl, subr_die);
18403
18404 gcc_checking_assert (fun);
18405 if (!flag_reorder_blocks_and_partition)
18406 {
18407 dw_fde_ref fde = fun->fde;
18408 if (fde->dw_fde_begin)
18409 {
18410 /* We have already generated the labels. */
18411 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18412 fde->dw_fde_end, false);
18413 }
18414 else
18415 {
18416 /* Create start/end labels and add the range. */
18417 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18418 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18419 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18420 current_function_funcdef_no);
18421 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18422 current_function_funcdef_no);
18423 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18424 false);
18425 }
18426
18427 #if VMS_DEBUGGING_INFO
18428 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18429 Section 2.3 Prologue and Epilogue Attributes:
18430 When a breakpoint is set on entry to a function, it is generally
18431 desirable for execution to be suspended, not on the very first
18432 instruction of the function, but rather at a point after the
18433 function's frame has been set up, after any language defined local
18434 declaration processing has been completed, and before execution of
18435 the first statement of the function begins. Debuggers generally
18436 cannot properly determine where this point is. Similarly for a
18437 breakpoint set on exit from a function. The prologue and epilogue
18438 attributes allow a compiler to communicate the location(s) to use. */
18439
18440 {
18441 if (fde->dw_fde_vms_end_prologue)
18442 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18443 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18444
18445 if (fde->dw_fde_vms_begin_epilogue)
18446 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18447 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18448 }
18449 #endif
18450
18451 }
18452 else
18453 {
18454 /* Generate pubnames entries for the split function code ranges. */
18455 dw_fde_ref fde = fun->fde;
18456
18457 if (fde->dw_fde_second_begin)
18458 {
18459 if (dwarf_version >= 3 || !dwarf_strict)
18460 {
18461 /* We should use ranges for non-contiguous code section
18462 addresses. Use the actual code range for the initial
18463 section, since the HOT/COLD labels might precede an
18464 alignment offset. */
18465 bool range_list_added = false;
18466 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18467 fde->dw_fde_end, &range_list_added,
18468 false);
18469 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18470 fde->dw_fde_second_end,
18471 &range_list_added, false);
18472 if (range_list_added)
18473 add_ranges (NULL);
18474 }
18475 else
18476 {
18477 /* There is no real support in DW2 for this .. so we make
18478 a work-around. First, emit the pub name for the segment
18479 containing the function label. Then make and emit a
18480 simplified subprogram DIE for the second segment with the
18481 name pre-fixed by __hot/cold_sect_of_. We use the same
18482 linkage name for the second die so that gdb will find both
18483 sections when given "b foo". */
18484 const char *name = NULL;
18485 tree decl_name = DECL_NAME (decl);
18486 dw_die_ref seg_die;
18487
18488 /* Do the 'primary' section. */
18489 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18490 fde->dw_fde_end, false);
18491
18492 /* Build a minimal DIE for the secondary section. */
18493 seg_die = new_die (DW_TAG_subprogram,
18494 subr_die->die_parent, decl);
18495
18496 if (TREE_PUBLIC (decl))
18497 add_AT_flag (seg_die, DW_AT_external, 1);
18498
18499 if (decl_name != NULL
18500 && IDENTIFIER_POINTER (decl_name) != NULL)
18501 {
18502 name = dwarf2_name (decl, 1);
18503 if (! DECL_ARTIFICIAL (decl))
18504 add_src_coords_attributes (seg_die, decl);
18505
18506 add_linkage_name (seg_die, decl);
18507 }
18508 gcc_assert (name != NULL);
18509 add_pure_or_virtual_attribute (seg_die, decl);
18510 if (DECL_ARTIFICIAL (decl))
18511 add_AT_flag (seg_die, DW_AT_artificial, 1);
18512
18513 name = concat ("__second_sect_of_", name, NULL);
18514 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18515 fde->dw_fde_second_end, false);
18516 add_name_attribute (seg_die, name);
18517 if (want_pubnames ())
18518 add_pubname_string (name, seg_die);
18519 }
18520 }
18521 else
18522 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18523 false);
18524 }
18525
18526 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18527
18528 /* We define the "frame base" as the function's CFA. This is more
18529 convenient for several reasons: (1) It's stable across the prologue
18530 and epilogue, which makes it better than just a frame pointer,
18531 (2) With dwarf3, there exists a one-byte encoding that allows us
18532 to reference the .debug_frame data by proxy, but failing that,
18533 (3) We can at least reuse the code inspection and interpretation
18534 code that determines the CFA position at various points in the
18535 function. */
18536 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18537 {
18538 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18539 add_AT_loc (subr_die, DW_AT_frame_base, op);
18540 }
18541 else
18542 {
18543 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18544 if (list->dw_loc_next)
18545 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18546 else
18547 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18548 }
18549
18550 /* Compute a displacement from the "steady-state frame pointer" to
18551 the CFA. The former is what all stack slots and argument slots
18552 will reference in the rtl; the latter is what we've told the
18553 debugger about. We'll need to adjust all frame_base references
18554 by this displacement. */
18555 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18556
18557 if (fun->static_chain_decl)
18558 add_AT_location_description (subr_die, DW_AT_static_link,
18559 loc_list_from_tree (fun->static_chain_decl, 2));
18560 }
18561
18562 /* Generate child dies for template paramaters. */
18563 if (debug_info_level > DINFO_LEVEL_TERSE)
18564 gen_generic_params_dies (decl);
18565
18566 /* Now output descriptions of the arguments for this function. This gets
18567 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18568 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18569 `...' at the end of the formal parameter list. In order to find out if
18570 there was a trailing ellipsis or not, we must instead look at the type
18571 associated with the FUNCTION_DECL. This will be a node of type
18572 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18573 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18574 an ellipsis at the end. */
18575
18576 /* In the case where we are describing a mere function declaration, all we
18577 need to do here (and all we *can* do here) is to describe the *types* of
18578 its formal parameters. */
18579 if (debug_info_level <= DINFO_LEVEL_TERSE)
18580 ;
18581 else if (declaration)
18582 gen_formal_types_die (decl, subr_die);
18583 else
18584 {
18585 /* Generate DIEs to represent all known formal parameters. */
18586 tree parm = DECL_ARGUMENTS (decl);
18587 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18588 tree generic_decl_parm = generic_decl
18589 ? DECL_ARGUMENTS (generic_decl)
18590 : NULL;
18591
18592 /* Now we want to walk the list of parameters of the function and
18593 emit their relevant DIEs.
18594
18595 We consider the case of DECL being an instance of a generic function
18596 as well as it being a normal function.
18597
18598 If DECL is an instance of a generic function we walk the
18599 parameters of the generic function declaration _and_ the parameters of
18600 DECL itself. This is useful because we want to emit specific DIEs for
18601 function parameter packs and those are declared as part of the
18602 generic function declaration. In that particular case,
18603 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18604 That DIE has children DIEs representing the set of arguments
18605 of the pack. Note that the set of pack arguments can be empty.
18606 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18607 children DIE.
18608
18609 Otherwise, we just consider the parameters of DECL. */
18610 while (generic_decl_parm || parm)
18611 {
18612 if (generic_decl_parm
18613 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18614 gen_formal_parameter_pack_die (generic_decl_parm,
18615 parm, subr_die,
18616 &parm);
18617 else if (parm && !POINTER_BOUNDS_P (parm))
18618 {
18619 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18620
18621 if (parm == DECL_ARGUMENTS (decl)
18622 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18623 && parm_die
18624 && (dwarf_version >= 3 || !dwarf_strict))
18625 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18626
18627 parm = DECL_CHAIN (parm);
18628 }
18629 else if (parm)
18630 parm = DECL_CHAIN (parm);
18631
18632 if (generic_decl_parm)
18633 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18634 }
18635
18636 /* Decide whether we need an unspecified_parameters DIE at the end.
18637 There are 2 more cases to do this for: 1) the ansi ... declaration -
18638 this is detectable when the end of the arg list is not a
18639 void_type_node 2) an unprototyped function declaration (not a
18640 definition). This just means that we have no info about the
18641 parameters at all. */
18642 if (prototype_p (TREE_TYPE (decl)))
18643 {
18644 /* This is the prototyped case, check for.... */
18645 if (stdarg_p (TREE_TYPE (decl)))
18646 gen_unspecified_parameters_die (decl, subr_die);
18647 }
18648 else if (DECL_INITIAL (decl) == NULL_TREE)
18649 gen_unspecified_parameters_die (decl, subr_die);
18650 }
18651
18652 /* Output Dwarf info for all of the stuff within the body of the function
18653 (if it has one - it may be just a declaration). */
18654 outer_scope = DECL_INITIAL (decl);
18655
18656 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18657 a function. This BLOCK actually represents the outermost binding contour
18658 for the function, i.e. the contour in which the function's formal
18659 parameters and labels get declared. Curiously, it appears that the front
18660 end doesn't actually put the PARM_DECL nodes for the current function onto
18661 the BLOCK_VARS list for this outer scope, but are strung off of the
18662 DECL_ARGUMENTS list for the function instead.
18663
18664 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18665 the LABEL_DECL nodes for the function however, and we output DWARF info
18666 for those in decls_for_scope. Just within the `outer_scope' there will be
18667 a BLOCK node representing the function's outermost pair of curly braces,
18668 and any blocks used for the base and member initializers of a C++
18669 constructor function. */
18670 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18671 {
18672 int call_site_note_count = 0;
18673 int tail_call_site_note_count = 0;
18674
18675 /* Emit a DW_TAG_variable DIE for a named return value. */
18676 if (DECL_NAME (DECL_RESULT (decl)))
18677 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18678
18679 decls_for_scope (outer_scope, subr_die, 0);
18680
18681 if (call_arg_locations && !dwarf_strict)
18682 {
18683 struct call_arg_loc_node *ca_loc;
18684 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18685 {
18686 dw_die_ref die = NULL;
18687 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18688 rtx arg, next_arg;
18689
18690 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18691 arg; arg = next_arg)
18692 {
18693 dw_loc_descr_ref reg, val;
18694 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18695 dw_die_ref cdie, tdie = NULL;
18696
18697 next_arg = XEXP (arg, 1);
18698 if (REG_P (XEXP (XEXP (arg, 0), 0))
18699 && next_arg
18700 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18701 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18702 && REGNO (XEXP (XEXP (arg, 0), 0))
18703 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18704 next_arg = XEXP (next_arg, 1);
18705 if (mode == VOIDmode)
18706 {
18707 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18708 if (mode == VOIDmode)
18709 mode = GET_MODE (XEXP (arg, 0));
18710 }
18711 if (mode == VOIDmode || mode == BLKmode)
18712 continue;
18713 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18714 {
18715 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18716 tloc = XEXP (XEXP (arg, 0), 1);
18717 continue;
18718 }
18719 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18720 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18721 {
18722 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18723 tlocc = XEXP (XEXP (arg, 0), 1);
18724 continue;
18725 }
18726 reg = NULL;
18727 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18728 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18729 VAR_INIT_STATUS_INITIALIZED);
18730 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18731 {
18732 rtx mem = XEXP (XEXP (arg, 0), 0);
18733 reg = mem_loc_descriptor (XEXP (mem, 0),
18734 get_address_mode (mem),
18735 GET_MODE (mem),
18736 VAR_INIT_STATUS_INITIALIZED);
18737 }
18738 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18739 == DEBUG_PARAMETER_REF)
18740 {
18741 tree tdecl
18742 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18743 tdie = lookup_decl_die (tdecl);
18744 if (tdie == NULL)
18745 continue;
18746 }
18747 else
18748 continue;
18749 if (reg == NULL
18750 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18751 != DEBUG_PARAMETER_REF)
18752 continue;
18753 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18754 VOIDmode,
18755 VAR_INIT_STATUS_INITIALIZED);
18756 if (val == NULL)
18757 continue;
18758 if (die == NULL)
18759 die = gen_call_site_die (decl, subr_die, ca_loc);
18760 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18761 NULL_TREE);
18762 if (reg != NULL)
18763 add_AT_loc (cdie, DW_AT_location, reg);
18764 else if (tdie != NULL)
18765 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18766 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18767 if (next_arg != XEXP (arg, 1))
18768 {
18769 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18770 if (mode == VOIDmode)
18771 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18772 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18773 0), 1),
18774 mode, VOIDmode,
18775 VAR_INIT_STATUS_INITIALIZED);
18776 if (val != NULL)
18777 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18778 }
18779 }
18780 if (die == NULL
18781 && (ca_loc->symbol_ref || tloc))
18782 die = gen_call_site_die (decl, subr_die, ca_loc);
18783 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18784 {
18785 dw_loc_descr_ref tval = NULL;
18786
18787 if (tloc != NULL_RTX)
18788 tval = mem_loc_descriptor (tloc,
18789 GET_MODE (tloc) == VOIDmode
18790 ? Pmode : GET_MODE (tloc),
18791 VOIDmode,
18792 VAR_INIT_STATUS_INITIALIZED);
18793 if (tval)
18794 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18795 else if (tlocc != NULL_RTX)
18796 {
18797 tval = mem_loc_descriptor (tlocc,
18798 GET_MODE (tlocc) == VOIDmode
18799 ? Pmode : GET_MODE (tlocc),
18800 VOIDmode,
18801 VAR_INIT_STATUS_INITIALIZED);
18802 if (tval)
18803 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18804 tval);
18805 }
18806 }
18807 if (die != NULL)
18808 {
18809 call_site_note_count++;
18810 if (ca_loc->tail_call_p)
18811 tail_call_site_note_count++;
18812 }
18813 }
18814 }
18815 call_arg_locations = NULL;
18816 call_arg_loc_last = NULL;
18817 if (tail_call_site_count >= 0
18818 && tail_call_site_count == tail_call_site_note_count
18819 && !dwarf_strict)
18820 {
18821 if (call_site_count >= 0
18822 && call_site_count == call_site_note_count)
18823 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18824 else
18825 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18826 }
18827 call_site_count = -1;
18828 tail_call_site_count = -1;
18829 }
18830
18831 if (subr_die != old_die)
18832 /* Add the calling convention attribute if requested. */
18833 add_calling_convention_attribute (subr_die, decl);
18834 }
18835
18836 /* Returns a hash value for X (which really is a die_struct). */
18837
18838 hashval_t
18839 block_die_hasher::hash (die_struct *d)
18840 {
18841 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18842 }
18843
18844 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18845 as decl_id and die_parent of die_struct Y. */
18846
18847 bool
18848 block_die_hasher::equal (die_struct *x, die_struct *y)
18849 {
18850 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18851 }
18852
18853 /* Generate a DIE to represent a declared data object.
18854 Either DECL or ORIGIN must be non-null. */
18855
18856 static void
18857 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18858 {
18859 HOST_WIDE_INT off = 0;
18860 tree com_decl;
18861 tree decl_or_origin = decl ? decl : origin;
18862 tree ultimate_origin;
18863 dw_die_ref var_die;
18864 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18865 dw_die_ref origin_die;
18866 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18867 || class_or_namespace_scope_p (context_die));
18868 bool specialization_p = false;
18869
18870 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18871 if (decl || ultimate_origin)
18872 origin = ultimate_origin;
18873 com_decl = fortran_common (decl_or_origin, &off);
18874
18875 /* Symbol in common gets emitted as a child of the common block, in the form
18876 of a data member. */
18877 if (com_decl)
18878 {
18879 dw_die_ref com_die;
18880 dw_loc_list_ref loc;
18881 die_node com_die_arg;
18882
18883 var_die = lookup_decl_die (decl_or_origin);
18884 if (var_die)
18885 {
18886 if (get_AT (var_die, DW_AT_location) == NULL)
18887 {
18888 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18889 if (loc)
18890 {
18891 if (off)
18892 {
18893 /* Optimize the common case. */
18894 if (single_element_loc_list_p (loc)
18895 && loc->expr->dw_loc_opc == DW_OP_addr
18896 && loc->expr->dw_loc_next == NULL
18897 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18898 == SYMBOL_REF)
18899 {
18900 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18901 loc->expr->dw_loc_oprnd1.v.val_addr
18902 = plus_constant (GET_MODE (x), x , off);
18903 }
18904 else
18905 loc_list_plus_const (loc, off);
18906 }
18907 add_AT_location_description (var_die, DW_AT_location, loc);
18908 remove_AT (var_die, DW_AT_declaration);
18909 }
18910 }
18911 return;
18912 }
18913
18914 if (common_block_die_table == NULL)
18915 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
18916
18917 com_die_arg.decl_id = DECL_UID (com_decl);
18918 com_die_arg.die_parent = context_die;
18919 com_die = common_block_die_table->find (&com_die_arg);
18920 loc = loc_list_from_tree (com_decl, 2);
18921 if (com_die == NULL)
18922 {
18923 const char *cnam
18924 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18925 die_node **slot;
18926
18927 com_die = new_die (DW_TAG_common_block, context_die, decl);
18928 add_name_and_src_coords_attributes (com_die, com_decl);
18929 if (loc)
18930 {
18931 add_AT_location_description (com_die, DW_AT_location, loc);
18932 /* Avoid sharing the same loc descriptor between
18933 DW_TAG_common_block and DW_TAG_variable. */
18934 loc = loc_list_from_tree (com_decl, 2);
18935 }
18936 else if (DECL_EXTERNAL (decl))
18937 add_AT_flag (com_die, DW_AT_declaration, 1);
18938 if (want_pubnames ())
18939 add_pubname_string (cnam, com_die); /* ??? needed? */
18940 com_die->decl_id = DECL_UID (com_decl);
18941 slot = common_block_die_table->find_slot (com_die, INSERT);
18942 *slot = com_die;
18943 }
18944 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18945 {
18946 add_AT_location_description (com_die, DW_AT_location, loc);
18947 loc = loc_list_from_tree (com_decl, 2);
18948 remove_AT (com_die, DW_AT_declaration);
18949 }
18950 var_die = new_die (DW_TAG_variable, com_die, decl);
18951 add_name_and_src_coords_attributes (var_die, decl);
18952 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18953 context_die);
18954 add_AT_flag (var_die, DW_AT_external, 1);
18955 if (loc)
18956 {
18957 if (off)
18958 {
18959 /* Optimize the common case. */
18960 if (single_element_loc_list_p (loc)
18961 && loc->expr->dw_loc_opc == DW_OP_addr
18962 && loc->expr->dw_loc_next == NULL
18963 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18964 {
18965 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18966 loc->expr->dw_loc_oprnd1.v.val_addr
18967 = plus_constant (GET_MODE (x), x, off);
18968 }
18969 else
18970 loc_list_plus_const (loc, off);
18971 }
18972 add_AT_location_description (var_die, DW_AT_location, loc);
18973 }
18974 else if (DECL_EXTERNAL (decl))
18975 add_AT_flag (var_die, DW_AT_declaration, 1);
18976 equate_decl_number_to_die (decl, var_die);
18977 return;
18978 }
18979
18980 /* If the compiler emitted a definition for the DECL declaration
18981 and if we already emitted a DIE for it, don't emit a second
18982 DIE for it again. Allow re-declarations of DECLs that are
18983 inside functions, though. */
18984 if (old_die && declaration && !local_scope_p (context_die))
18985 return;
18986
18987 /* For static data members, the declaration in the class is supposed
18988 to have DW_TAG_member tag; the specification should still be
18989 DW_TAG_variable referencing the DW_TAG_member DIE. */
18990 if (declaration && class_scope_p (context_die))
18991 var_die = new_die (DW_TAG_member, context_die, decl);
18992 else
18993 var_die = new_die (DW_TAG_variable, context_die, decl);
18994
18995 origin_die = NULL;
18996 if (origin != NULL)
18997 origin_die = add_abstract_origin_attribute (var_die, origin);
18998
18999 /* Loop unrolling can create multiple blocks that refer to the same
19000 static variable, so we must test for the DW_AT_declaration flag.
19001
19002 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19003 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19004 sharing them.
19005
19006 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19007
19008 ??? The declare_in_namespace support causes us to get two DIEs for one
19009 variable, both of which are declarations. We want to avoid considering
19010 one to be a specification, so we must test that this DIE is not a
19011 declaration. */
19012 else if (old_die && TREE_STATIC (decl) && ! declaration
19013 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19014 {
19015 /* This is a definition of a C++ class level static. */
19016 add_AT_specification (var_die, old_die);
19017 specialization_p = true;
19018 if (DECL_NAME (decl))
19019 {
19020 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19021 struct dwarf_file_data * file_index = lookup_filename (s.file);
19022
19023 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19024 add_AT_file (var_die, DW_AT_decl_file, file_index);
19025
19026 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19027 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19028
19029 if (old_die->die_tag == DW_TAG_member)
19030 add_linkage_name (var_die, decl);
19031 }
19032 }
19033 else
19034 add_name_and_src_coords_attributes (var_die, decl);
19035
19036 if ((origin == NULL && !specialization_p)
19037 || (origin != NULL
19038 && !DECL_ABSTRACT_P (decl_or_origin)
19039 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19040 decl_function_context
19041 (decl_or_origin))))
19042 {
19043 tree type = TREE_TYPE (decl_or_origin);
19044
19045 if (decl_by_reference_p (decl_or_origin))
19046 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19047 context_die);
19048 else
19049 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19050 context_die);
19051 }
19052
19053 if (origin == NULL && !specialization_p)
19054 {
19055 if (TREE_PUBLIC (decl))
19056 add_AT_flag (var_die, DW_AT_external, 1);
19057
19058 if (DECL_ARTIFICIAL (decl))
19059 add_AT_flag (var_die, DW_AT_artificial, 1);
19060
19061 add_accessibility_attribute (var_die, decl);
19062 }
19063
19064 if (declaration)
19065 add_AT_flag (var_die, DW_AT_declaration, 1);
19066
19067 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19068 equate_decl_number_to_die (decl, var_die);
19069
19070 if (! declaration
19071 && (! DECL_ABSTRACT_P (decl_or_origin)
19072 /* Local static vars are shared between all clones/inlines,
19073 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19074 already set. */
19075 || (TREE_CODE (decl_or_origin) == VAR_DECL
19076 && TREE_STATIC (decl_or_origin)
19077 && DECL_RTL_SET_P (decl_or_origin)))
19078 /* When abstract origin already has DW_AT_location attribute, no need
19079 to add it again. */
19080 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19081 {
19082 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19083 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19084 defer_location (decl_or_origin, var_die);
19085 else
19086 add_location_or_const_value_attribute (var_die, decl_or_origin,
19087 decl == NULL, DW_AT_location);
19088 add_pubname (decl_or_origin, var_die);
19089 }
19090 else
19091 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19092 }
19093
19094 /* Generate a DIE to represent a named constant. */
19095
19096 static void
19097 gen_const_die (tree decl, dw_die_ref context_die)
19098 {
19099 dw_die_ref const_die;
19100 tree type = TREE_TYPE (decl);
19101
19102 const_die = new_die (DW_TAG_constant, context_die, decl);
19103 add_name_and_src_coords_attributes (const_die, decl);
19104 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19105 if (TREE_PUBLIC (decl))
19106 add_AT_flag (const_die, DW_AT_external, 1);
19107 if (DECL_ARTIFICIAL (decl))
19108 add_AT_flag (const_die, DW_AT_artificial, 1);
19109 tree_add_const_value_attribute_for_decl (const_die, decl);
19110 }
19111
19112 /* Generate a DIE to represent a label identifier. */
19113
19114 static void
19115 gen_label_die (tree decl, dw_die_ref context_die)
19116 {
19117 tree origin = decl_ultimate_origin (decl);
19118 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19119 rtx insn;
19120 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19121
19122 if (origin != NULL)
19123 add_abstract_origin_attribute (lbl_die, origin);
19124 else
19125 add_name_and_src_coords_attributes (lbl_die, decl);
19126
19127 if (DECL_ABSTRACT_P (decl))
19128 equate_decl_number_to_die (decl, lbl_die);
19129 else
19130 {
19131 insn = DECL_RTL_IF_SET (decl);
19132
19133 /* Deleted labels are programmer specified labels which have been
19134 eliminated because of various optimizations. We still emit them
19135 here so that it is possible to put breakpoints on them. */
19136 if (insn
19137 && (LABEL_P (insn)
19138 || ((NOTE_P (insn)
19139 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19140 {
19141 /* When optimization is enabled (via -O) some parts of the compiler
19142 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19143 represent source-level labels which were explicitly declared by
19144 the user. This really shouldn't be happening though, so catch
19145 it if it ever does happen. */
19146 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19147
19148 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19149 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19150 }
19151 else if (insn
19152 && NOTE_P (insn)
19153 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19154 && CODE_LABEL_NUMBER (insn) != -1)
19155 {
19156 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19157 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19158 }
19159 }
19160 }
19161
19162 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19163 attributes to the DIE for a block STMT, to describe where the inlined
19164 function was called from. This is similar to add_src_coords_attributes. */
19165
19166 static inline void
19167 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19168 {
19169 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19170
19171 if (dwarf_version >= 3 || !dwarf_strict)
19172 {
19173 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19174 add_AT_unsigned (die, DW_AT_call_line, s.line);
19175 }
19176 }
19177
19178
19179 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19180 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19181
19182 static inline void
19183 add_high_low_attributes (tree stmt, dw_die_ref die)
19184 {
19185 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19186
19187 if (BLOCK_FRAGMENT_CHAIN (stmt)
19188 && (dwarf_version >= 3 || !dwarf_strict))
19189 {
19190 tree chain, superblock = NULL_TREE;
19191 dw_die_ref pdie;
19192 dw_attr_ref attr = NULL;
19193
19194 if (inlined_function_outer_scope_p (stmt))
19195 {
19196 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19197 BLOCK_NUMBER (stmt));
19198 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19199 }
19200
19201 /* Optimize duplicate .debug_ranges lists or even tails of
19202 lists. If this BLOCK has same ranges as its supercontext,
19203 lookup DW_AT_ranges attribute in the supercontext (and
19204 recursively so), verify that the ranges_table contains the
19205 right values and use it instead of adding a new .debug_range. */
19206 for (chain = stmt, pdie = die;
19207 BLOCK_SAME_RANGE (chain);
19208 chain = BLOCK_SUPERCONTEXT (chain))
19209 {
19210 dw_attr_ref new_attr;
19211
19212 pdie = pdie->die_parent;
19213 if (pdie == NULL)
19214 break;
19215 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19216 break;
19217 new_attr = get_AT (pdie, DW_AT_ranges);
19218 if (new_attr == NULL
19219 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19220 break;
19221 attr = new_attr;
19222 superblock = BLOCK_SUPERCONTEXT (chain);
19223 }
19224 if (attr != NULL
19225 && (ranges_table[attr->dw_attr_val.v.val_offset
19226 / 2 / DWARF2_ADDR_SIZE].num
19227 == BLOCK_NUMBER (superblock))
19228 && BLOCK_FRAGMENT_CHAIN (superblock))
19229 {
19230 unsigned long off = attr->dw_attr_val.v.val_offset
19231 / 2 / DWARF2_ADDR_SIZE;
19232 unsigned long supercnt = 0, thiscnt = 0;
19233 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19234 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19235 {
19236 ++supercnt;
19237 gcc_checking_assert (ranges_table[off + supercnt].num
19238 == BLOCK_NUMBER (chain));
19239 }
19240 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19241 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19242 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19243 ++thiscnt;
19244 gcc_assert (supercnt >= thiscnt);
19245 add_AT_range_list (die, DW_AT_ranges,
19246 ((off + supercnt - thiscnt)
19247 * 2 * DWARF2_ADDR_SIZE),
19248 false);
19249 return;
19250 }
19251
19252 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19253
19254 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19255 do
19256 {
19257 add_ranges (chain);
19258 chain = BLOCK_FRAGMENT_CHAIN (chain);
19259 }
19260 while (chain);
19261 add_ranges (NULL);
19262 }
19263 else
19264 {
19265 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19266 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19267 BLOCK_NUMBER (stmt));
19268 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19269 BLOCK_NUMBER (stmt));
19270 add_AT_low_high_pc (die, label, label_high, false);
19271 }
19272 }
19273
19274 /* Generate a DIE for a lexical block. */
19275
19276 static void
19277 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19278 {
19279 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19280
19281 if (call_arg_locations)
19282 {
19283 if (block_map.length () <= BLOCK_NUMBER (stmt))
19284 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19285 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19286 }
19287
19288 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19289 add_high_low_attributes (stmt, stmt_die);
19290
19291 decls_for_scope (stmt, stmt_die, depth);
19292 }
19293
19294 /* Generate a DIE for an inlined subprogram. */
19295
19296 static void
19297 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19298 {
19299 tree decl;
19300
19301 /* The instance of function that is effectively being inlined shall not
19302 be abstract. */
19303 gcc_assert (! BLOCK_ABSTRACT (stmt));
19304
19305 decl = block_ultimate_origin (stmt);
19306
19307 /* Emit info for the abstract instance first, if we haven't yet. We
19308 must emit this even if the block is abstract, otherwise when we
19309 emit the block below (or elsewhere), we may end up trying to emit
19310 a die whose origin die hasn't been emitted, and crashing. */
19311 dwarf2out_abstract_function (decl);
19312
19313 if (! BLOCK_ABSTRACT (stmt))
19314 {
19315 dw_die_ref subr_die
19316 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19317
19318 if (call_arg_locations)
19319 {
19320 if (block_map.length () <= BLOCK_NUMBER (stmt))
19321 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19322 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19323 }
19324 add_abstract_origin_attribute (subr_die, decl);
19325 if (TREE_ASM_WRITTEN (stmt))
19326 add_high_low_attributes (stmt, subr_die);
19327 add_call_src_coords_attributes (stmt, subr_die);
19328
19329 decls_for_scope (stmt, subr_die, depth);
19330 }
19331 }
19332
19333 /* Generate a DIE for a field in a record, or structure. */
19334
19335 static void
19336 gen_field_die (tree decl, dw_die_ref context_die)
19337 {
19338 dw_die_ref decl_die;
19339
19340 if (TREE_TYPE (decl) == error_mark_node)
19341 return;
19342
19343 decl_die = new_die (DW_TAG_member, context_die, decl);
19344 add_name_and_src_coords_attributes (decl_die, decl);
19345 add_type_attribute (decl_die, member_declared_type (decl),
19346 decl_quals (decl), context_die);
19347
19348 if (DECL_BIT_FIELD_TYPE (decl))
19349 {
19350 add_byte_size_attribute (decl_die, decl);
19351 add_bit_size_attribute (decl_die, decl);
19352 add_bit_offset_attribute (decl_die, decl);
19353 }
19354
19355 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19356 add_data_member_location_attribute (decl_die, decl);
19357
19358 if (DECL_ARTIFICIAL (decl))
19359 add_AT_flag (decl_die, DW_AT_artificial, 1);
19360
19361 add_accessibility_attribute (decl_die, decl);
19362
19363 /* Equate decl number to die, so that we can look up this decl later on. */
19364 equate_decl_number_to_die (decl, decl_die);
19365 }
19366
19367 #if 0
19368 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19369 Use modified_type_die instead.
19370 We keep this code here just in case these types of DIEs may be needed to
19371 represent certain things in other languages (e.g. Pascal) someday. */
19372
19373 static void
19374 gen_pointer_type_die (tree type, dw_die_ref context_die)
19375 {
19376 dw_die_ref ptr_die
19377 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19378
19379 equate_type_number_to_die (type, ptr_die);
19380 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19381 context_die);
19382 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19383 }
19384
19385 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19386 Use modified_type_die instead.
19387 We keep this code here just in case these types of DIEs may be needed to
19388 represent certain things in other languages (e.g. Pascal) someday. */
19389
19390 static void
19391 gen_reference_type_die (tree type, dw_die_ref context_die)
19392 {
19393 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19394
19395 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19396 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19397 else
19398 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19399
19400 equate_type_number_to_die (type, ref_die);
19401 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19402 context_die);
19403 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19404 }
19405 #endif
19406
19407 /* Generate a DIE for a pointer to a member type. */
19408
19409 static void
19410 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19411 {
19412 dw_die_ref ptr_die
19413 = new_die (DW_TAG_ptr_to_member_type,
19414 scope_die_for (type, context_die), type);
19415
19416 equate_type_number_to_die (type, ptr_die);
19417 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19418 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19419 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19420 context_die);
19421 }
19422
19423 typedef const char *dchar_p; /* For DEF_VEC_P. */
19424
19425 static char *producer_string;
19426
19427 /* Return a heap allocated producer string including command line options
19428 if -grecord-gcc-switches. */
19429
19430 static char *
19431 gen_producer_string (void)
19432 {
19433 size_t j;
19434 auto_vec<dchar_p> switches;
19435 const char *language_string = lang_hooks.name;
19436 char *producer, *tail;
19437 const char *p;
19438 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19439 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19440
19441 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19442 switch (save_decoded_options[j].opt_index)
19443 {
19444 case OPT_o:
19445 case OPT_d:
19446 case OPT_dumpbase:
19447 case OPT_dumpdir:
19448 case OPT_auxbase:
19449 case OPT_auxbase_strip:
19450 case OPT_quiet:
19451 case OPT_version:
19452 case OPT_v:
19453 case OPT_w:
19454 case OPT_L:
19455 case OPT_D:
19456 case OPT_I:
19457 case OPT_U:
19458 case OPT_SPECIAL_unknown:
19459 case OPT_SPECIAL_ignore:
19460 case OPT_SPECIAL_program_name:
19461 case OPT_SPECIAL_input_file:
19462 case OPT_grecord_gcc_switches:
19463 case OPT_gno_record_gcc_switches:
19464 case OPT__output_pch_:
19465 case OPT_fdiagnostics_show_location_:
19466 case OPT_fdiagnostics_show_option:
19467 case OPT_fdiagnostics_show_caret:
19468 case OPT_fdiagnostics_color_:
19469 case OPT_fverbose_asm:
19470 case OPT____:
19471 case OPT__sysroot_:
19472 case OPT_nostdinc:
19473 case OPT_nostdinc__:
19474 /* Ignore these. */
19475 continue;
19476 default:
19477 if (cl_options[save_decoded_options[j].opt_index].flags
19478 & CL_NO_DWARF_RECORD)
19479 continue;
19480 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19481 == '-');
19482 switch (save_decoded_options[j].canonical_option[0][1])
19483 {
19484 case 'M':
19485 case 'i':
19486 case 'W':
19487 continue;
19488 case 'f':
19489 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19490 "dump", 4) == 0)
19491 continue;
19492 break;
19493 default:
19494 break;
19495 }
19496 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19497 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19498 break;
19499 }
19500
19501 producer = XNEWVEC (char, plen + 1 + len + 1);
19502 tail = producer;
19503 sprintf (tail, "%s %s", language_string, version_string);
19504 tail += plen;
19505
19506 FOR_EACH_VEC_ELT (switches, j, p)
19507 {
19508 len = strlen (p);
19509 *tail = ' ';
19510 memcpy (tail + 1, p, len);
19511 tail += len + 1;
19512 }
19513
19514 *tail = '\0';
19515 return producer;
19516 }
19517
19518 /* Given a C and/or C++ language/version string return the "highest".
19519 C++ is assumed to be "higher" than C in this case. Used for merging
19520 LTO translation unit languages. */
19521 static const char *
19522 highest_c_language (const char *lang1, const char *lang2)
19523 {
19524 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19525 return "GNU C++14";
19526 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19527 return "GNU C++11";
19528 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19529 return "GNU C++98";
19530
19531 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19532 return "GNU C11";
19533 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19534 return "GNU C99";
19535 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19536 return "GNU C89";
19537
19538 gcc_unreachable ();
19539 }
19540
19541
19542 /* Generate the DIE for the compilation unit. */
19543
19544 static dw_die_ref
19545 gen_compile_unit_die (const char *filename)
19546 {
19547 dw_die_ref die;
19548 const char *language_string = lang_hooks.name;
19549 int language;
19550
19551 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19552
19553 if (filename)
19554 {
19555 add_name_attribute (die, filename);
19556 /* Don't add cwd for <built-in>. */
19557 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19558 add_comp_dir_attribute (die);
19559 }
19560
19561 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19562
19563 /* If our producer is LTO try to figure out a common language to use
19564 from the global list of translation units. */
19565 if (strcmp (language_string, "GNU GIMPLE") == 0)
19566 {
19567 unsigned i;
19568 tree t;
19569 const char *common_lang = NULL;
19570
19571 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19572 {
19573 if (!TRANSLATION_UNIT_LANGUAGE (t))
19574 continue;
19575 if (!common_lang)
19576 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19577 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19578 ;
19579 else if (strncmp (common_lang, "GNU C", 5) == 0
19580 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19581 /* Mixing C and C++ is ok, use C++ in that case. */
19582 common_lang = highest_c_language (common_lang,
19583 TRANSLATION_UNIT_LANGUAGE (t));
19584 else
19585 {
19586 /* Fall back to C. */
19587 common_lang = NULL;
19588 break;
19589 }
19590 }
19591
19592 if (common_lang)
19593 language_string = common_lang;
19594 }
19595
19596 language = DW_LANG_C;
19597 if (strncmp (language_string, "GNU C", 5) == 0
19598 && (language_string[5] == 0 || ISDIGIT (language_string[5])))
19599 {
19600 language = DW_LANG_C89;
19601 if (dwarf_version >= 3 || !dwarf_strict)
19602 if (strcmp (language_string, "GNU C99") == 0)
19603 language = DW_LANG_C99;
19604 }
19605 else if (strncmp (language_string, "GNU C++", 7) == 0)
19606 language = DW_LANG_C_plus_plus;
19607 else if (strcmp (language_string, "GNU F77") == 0)
19608 language = DW_LANG_Fortran77;
19609 else if (strcmp (language_string, "GNU Pascal") == 0)
19610 language = DW_LANG_Pascal83;
19611 else if (dwarf_version >= 3 || !dwarf_strict)
19612 {
19613 if (strcmp (language_string, "GNU Ada") == 0)
19614 language = DW_LANG_Ada95;
19615 else if (strcmp (language_string, "GNU Fortran") == 0)
19616 language = DW_LANG_Fortran95;
19617 else if (strcmp (language_string, "GNU Java") == 0)
19618 language = DW_LANG_Java;
19619 else if (strcmp (language_string, "GNU Objective-C") == 0)
19620 language = DW_LANG_ObjC;
19621 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19622 language = DW_LANG_ObjC_plus_plus;
19623 else if (dwarf_version >= 5 || !dwarf_strict)
19624 {
19625 if (strcmp (language_string, "GNU Go") == 0)
19626 language = DW_LANG_Go;
19627 }
19628 }
19629 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19630 else if (strcmp (language_string, "GNU Fortran") == 0)
19631 language = DW_LANG_Fortran90;
19632
19633 add_AT_unsigned (die, DW_AT_language, language);
19634
19635 switch (language)
19636 {
19637 case DW_LANG_Fortran77:
19638 case DW_LANG_Fortran90:
19639 case DW_LANG_Fortran95:
19640 /* Fortran has case insensitive identifiers and the front-end
19641 lowercases everything. */
19642 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19643 break;
19644 default:
19645 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19646 break;
19647 }
19648 return die;
19649 }
19650
19651 /* Generate the DIE for a base class. */
19652
19653 static void
19654 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19655 {
19656 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19657
19658 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19659 add_data_member_location_attribute (die, binfo);
19660
19661 if (BINFO_VIRTUAL_P (binfo))
19662 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19663
19664 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19665 children, otherwise the default is DW_ACCESS_public. In DWARF2
19666 the default has always been DW_ACCESS_private. */
19667 if (access == access_public_node)
19668 {
19669 if (dwarf_version == 2
19670 || context_die->die_tag == DW_TAG_class_type)
19671 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19672 }
19673 else if (access == access_protected_node)
19674 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19675 else if (dwarf_version > 2
19676 && context_die->die_tag != DW_TAG_class_type)
19677 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19678 }
19679
19680 /* Generate a DIE for a class member. */
19681
19682 static void
19683 gen_member_die (tree type, dw_die_ref context_die)
19684 {
19685 tree member;
19686 tree binfo = TYPE_BINFO (type);
19687 dw_die_ref child;
19688
19689 /* If this is not an incomplete type, output descriptions of each of its
19690 members. Note that as we output the DIEs necessary to represent the
19691 members of this record or union type, we will also be trying to output
19692 DIEs to represent the *types* of those members. However the `type'
19693 function (above) will specifically avoid generating type DIEs for member
19694 types *within* the list of member DIEs for this (containing) type except
19695 for those types (of members) which are explicitly marked as also being
19696 members of this (containing) type themselves. The g++ front- end can
19697 force any given type to be treated as a member of some other (containing)
19698 type by setting the TYPE_CONTEXT of the given (member) type to point to
19699 the TREE node representing the appropriate (containing) type. */
19700
19701 /* First output info about the base classes. */
19702 if (binfo)
19703 {
19704 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19705 int i;
19706 tree base;
19707
19708 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19709 gen_inheritance_die (base,
19710 (accesses ? (*accesses)[i] : access_public_node),
19711 context_die);
19712 }
19713
19714 /* Now output info about the data members and type members. */
19715 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19716 {
19717 /* If we thought we were generating minimal debug info for TYPE
19718 and then changed our minds, some of the member declarations
19719 may have already been defined. Don't define them again, but
19720 do put them in the right order. */
19721
19722 child = lookup_decl_die (member);
19723 if (child)
19724 splice_child_die (context_die, child);
19725 else
19726 gen_decl_die (member, NULL, context_die);
19727 }
19728
19729 /* Now output info about the function members (if any). */
19730 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19731 {
19732 /* Don't include clones in the member list. */
19733 if (DECL_ABSTRACT_ORIGIN (member))
19734 continue;
19735
19736 child = lookup_decl_die (member);
19737 if (child)
19738 splice_child_die (context_die, child);
19739 else
19740 gen_decl_die (member, NULL, context_die);
19741 }
19742 }
19743
19744 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19745 is set, we pretend that the type was never defined, so we only get the
19746 member DIEs needed by later specification DIEs. */
19747
19748 static void
19749 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19750 enum debug_info_usage usage)
19751 {
19752 dw_die_ref type_die = lookup_type_die (type);
19753 dw_die_ref scope_die = 0;
19754 int nested = 0;
19755 int complete = (TYPE_SIZE (type)
19756 && (! TYPE_STUB_DECL (type)
19757 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19758 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19759 complete = complete && should_emit_struct_debug (type, usage);
19760
19761 if (type_die && ! complete)
19762 return;
19763
19764 if (TYPE_CONTEXT (type) != NULL_TREE
19765 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19766 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19767 nested = 1;
19768
19769 scope_die = scope_die_for (type, context_die);
19770
19771 /* Generate child dies for template paramaters. */
19772 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19773 schedule_generic_params_dies_gen (type);
19774
19775 if (! type_die || (nested && is_cu_die (scope_die)))
19776 /* First occurrence of type or toplevel definition of nested class. */
19777 {
19778 dw_die_ref old_die = type_die;
19779
19780 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19781 ? record_type_tag (type) : DW_TAG_union_type,
19782 scope_die, type);
19783 equate_type_number_to_die (type, type_die);
19784 if (old_die)
19785 add_AT_specification (type_die, old_die);
19786 else
19787 add_name_attribute (type_die, type_tag (type));
19788 }
19789 else
19790 remove_AT (type_die, DW_AT_declaration);
19791
19792 /* If this type has been completed, then give it a byte_size attribute and
19793 then give a list of members. */
19794 if (complete && !ns_decl)
19795 {
19796 /* Prevent infinite recursion in cases where the type of some member of
19797 this type is expressed in terms of this type itself. */
19798 TREE_ASM_WRITTEN (type) = 1;
19799 add_byte_size_attribute (type_die, type);
19800 if (TYPE_STUB_DECL (type) != NULL_TREE)
19801 {
19802 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19803 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19804 }
19805
19806 /* If the first reference to this type was as the return type of an
19807 inline function, then it may not have a parent. Fix this now. */
19808 if (type_die->die_parent == NULL)
19809 add_child_die (scope_die, type_die);
19810
19811 push_decl_scope (type);
19812 gen_member_die (type, type_die);
19813 pop_decl_scope ();
19814
19815 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19816 if (TYPE_ARTIFICIAL (type))
19817 add_AT_flag (type_die, DW_AT_artificial, 1);
19818
19819 /* GNU extension: Record what type our vtable lives in. */
19820 if (TYPE_VFIELD (type))
19821 {
19822 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19823
19824 gen_type_die (vtype, context_die);
19825 add_AT_die_ref (type_die, DW_AT_containing_type,
19826 lookup_type_die (vtype));
19827 }
19828 }
19829 else
19830 {
19831 add_AT_flag (type_die, DW_AT_declaration, 1);
19832
19833 /* We don't need to do this for function-local types. */
19834 if (TYPE_STUB_DECL (type)
19835 && ! decl_function_context (TYPE_STUB_DECL (type)))
19836 vec_safe_push (incomplete_types, type);
19837 }
19838
19839 if (get_AT (type_die, DW_AT_name))
19840 add_pubtype (type, type_die);
19841 }
19842
19843 /* Generate a DIE for a subroutine _type_. */
19844
19845 static void
19846 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19847 {
19848 tree return_type = TREE_TYPE (type);
19849 dw_die_ref subr_die
19850 = new_die (DW_TAG_subroutine_type,
19851 scope_die_for (type, context_die), type);
19852
19853 equate_type_number_to_die (type, subr_die);
19854 add_prototyped_attribute (subr_die, type);
19855 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19856 gen_formal_types_die (type, subr_die);
19857
19858 if (get_AT (subr_die, DW_AT_name))
19859 add_pubtype (type, subr_die);
19860 }
19861
19862 /* Generate a DIE for a type definition. */
19863
19864 static void
19865 gen_typedef_die (tree decl, dw_die_ref context_die)
19866 {
19867 dw_die_ref type_die;
19868 tree origin;
19869
19870 if (TREE_ASM_WRITTEN (decl))
19871 return;
19872
19873 TREE_ASM_WRITTEN (decl) = 1;
19874 type_die = new_die (DW_TAG_typedef, context_die, decl);
19875 origin = decl_ultimate_origin (decl);
19876 if (origin != NULL)
19877 add_abstract_origin_attribute (type_die, origin);
19878 else
19879 {
19880 tree type;
19881
19882 add_name_and_src_coords_attributes (type_die, decl);
19883 if (DECL_ORIGINAL_TYPE (decl))
19884 {
19885 type = DECL_ORIGINAL_TYPE (decl);
19886
19887 gcc_assert (type != TREE_TYPE (decl));
19888 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19889 }
19890 else
19891 {
19892 type = TREE_TYPE (decl);
19893
19894 if (is_naming_typedef_decl (TYPE_NAME (type)))
19895 {
19896 /* Here, we are in the case of decl being a typedef naming
19897 an anonymous type, e.g:
19898 typedef struct {...} foo;
19899 In that case TREE_TYPE (decl) is not a typedef variant
19900 type and TYPE_NAME of the anonymous type is set to the
19901 TYPE_DECL of the typedef. This construct is emitted by
19902 the C++ FE.
19903
19904 TYPE is the anonymous struct named by the typedef
19905 DECL. As we need the DW_AT_type attribute of the
19906 DW_TAG_typedef to point to the DIE of TYPE, let's
19907 generate that DIE right away. add_type_attribute
19908 called below will then pick (via lookup_type_die) that
19909 anonymous struct DIE. */
19910 if (!TREE_ASM_WRITTEN (type))
19911 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19912
19913 /* This is a GNU Extension. We are adding a
19914 DW_AT_linkage_name attribute to the DIE of the
19915 anonymous struct TYPE. The value of that attribute
19916 is the name of the typedef decl naming the anonymous
19917 struct. This greatly eases the work of consumers of
19918 this debug info. */
19919 add_linkage_attr (lookup_type_die (type), decl);
19920 }
19921 }
19922
19923 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19924
19925 if (is_naming_typedef_decl (decl))
19926 /* We want that all subsequent calls to lookup_type_die with
19927 TYPE in argument yield the DW_TAG_typedef we have just
19928 created. */
19929 equate_type_number_to_die (type, type_die);
19930
19931 add_accessibility_attribute (type_die, decl);
19932 }
19933
19934 if (DECL_ABSTRACT_P (decl))
19935 equate_decl_number_to_die (decl, type_die);
19936
19937 if (get_AT (type_die, DW_AT_name))
19938 add_pubtype (decl, type_die);
19939 }
19940
19941 /* Generate a DIE for a struct, class, enum or union type. */
19942
19943 static void
19944 gen_tagged_type_die (tree type,
19945 dw_die_ref context_die,
19946 enum debug_info_usage usage)
19947 {
19948 int need_pop;
19949
19950 if (type == NULL_TREE
19951 || !is_tagged_type (type))
19952 return;
19953
19954 /* If this is a nested type whose containing class hasn't been written
19955 out yet, writing it out will cover this one, too. This does not apply
19956 to instantiations of member class templates; they need to be added to
19957 the containing class as they are generated. FIXME: This hurts the
19958 idea of combining type decls from multiple TUs, since we can't predict
19959 what set of template instantiations we'll get. */
19960 if (TYPE_CONTEXT (type)
19961 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19962 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19963 {
19964 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19965
19966 if (TREE_ASM_WRITTEN (type))
19967 return;
19968
19969 /* If that failed, attach ourselves to the stub. */
19970 push_decl_scope (TYPE_CONTEXT (type));
19971 context_die = lookup_type_die (TYPE_CONTEXT (type));
19972 need_pop = 1;
19973 }
19974 else if (TYPE_CONTEXT (type) != NULL_TREE
19975 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19976 {
19977 /* If this type is local to a function that hasn't been written
19978 out yet, use a NULL context for now; it will be fixed up in
19979 decls_for_scope. */
19980 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19981 /* A declaration DIE doesn't count; nested types need to go in the
19982 specification. */
19983 if (context_die && is_declaration_die (context_die))
19984 context_die = NULL;
19985 need_pop = 0;
19986 }
19987 else
19988 {
19989 context_die = declare_in_namespace (type, context_die);
19990 need_pop = 0;
19991 }
19992
19993 if (TREE_CODE (type) == ENUMERAL_TYPE)
19994 {
19995 /* This might have been written out by the call to
19996 declare_in_namespace. */
19997 if (!TREE_ASM_WRITTEN (type))
19998 gen_enumeration_type_die (type, context_die);
19999 }
20000 else
20001 gen_struct_or_union_type_die (type, context_die, usage);
20002
20003 if (need_pop)
20004 pop_decl_scope ();
20005
20006 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20007 it up if it is ever completed. gen_*_type_die will set it for us
20008 when appropriate. */
20009 }
20010
20011 /* Generate a type description DIE. */
20012
20013 static void
20014 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20015 enum debug_info_usage usage)
20016 {
20017 struct array_descr_info info;
20018
20019 if (type == NULL_TREE || type == error_mark_node)
20020 return;
20021
20022 if (TYPE_NAME (type) != NULL_TREE
20023 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20024 && is_redundant_typedef (TYPE_NAME (type))
20025 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20026 /* The DECL of this type is a typedef we don't want to emit debug
20027 info for but we want debug info for its underlying typedef.
20028 This can happen for e.g, the injected-class-name of a C++
20029 type. */
20030 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20031
20032 /* If TYPE is a typedef type variant, let's generate debug info
20033 for the parent typedef which TYPE is a type of. */
20034 if (typedef_variant_p (type))
20035 {
20036 if (TREE_ASM_WRITTEN (type))
20037 return;
20038
20039 /* Prevent broken recursion; we can't hand off to the same type. */
20040 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20041
20042 /* Give typedefs the right scope. */
20043 context_die = scope_die_for (type, context_die);
20044
20045 TREE_ASM_WRITTEN (type) = 1;
20046
20047 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20048 return;
20049 }
20050
20051 /* If type is an anonymous tagged type named by a typedef, let's
20052 generate debug info for the typedef. */
20053 if (is_naming_typedef_decl (TYPE_NAME (type)))
20054 {
20055 /* Use the DIE of the containing namespace as the parent DIE of
20056 the type description DIE we want to generate. */
20057 if (DECL_CONTEXT (TYPE_NAME (type))
20058 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20059 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20060
20061 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20062 return;
20063 }
20064
20065 /* If this is an array type with hidden descriptor, handle it first. */
20066 if (!TREE_ASM_WRITTEN (type)
20067 && lang_hooks.types.get_array_descr_info
20068 && lang_hooks.types.get_array_descr_info (type, &info)
20069 && (dwarf_version >= 3 || !dwarf_strict))
20070 {
20071 gen_descr_array_type_die (type, &info, context_die);
20072 TREE_ASM_WRITTEN (type) = 1;
20073 return;
20074 }
20075
20076 /* We are going to output a DIE to represent the unqualified version
20077 of this type (i.e. without any const or volatile qualifiers) so
20078 get the main variant (i.e. the unqualified version) of this type
20079 now. (Vectors are special because the debugging info is in the
20080 cloned type itself). */
20081 if (TREE_CODE (type) != VECTOR_TYPE)
20082 type = type_main_variant (type);
20083
20084 if (TREE_ASM_WRITTEN (type))
20085 return;
20086
20087 switch (TREE_CODE (type))
20088 {
20089 case ERROR_MARK:
20090 break;
20091
20092 case POINTER_TYPE:
20093 case REFERENCE_TYPE:
20094 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20095 ensures that the gen_type_die recursion will terminate even if the
20096 type is recursive. Recursive types are possible in Ada. */
20097 /* ??? We could perhaps do this for all types before the switch
20098 statement. */
20099 TREE_ASM_WRITTEN (type) = 1;
20100
20101 /* For these types, all that is required is that we output a DIE (or a
20102 set of DIEs) to represent the "basis" type. */
20103 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20104 DINFO_USAGE_IND_USE);
20105 break;
20106
20107 case OFFSET_TYPE:
20108 /* This code is used for C++ pointer-to-data-member types.
20109 Output a description of the relevant class type. */
20110 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20111 DINFO_USAGE_IND_USE);
20112
20113 /* Output a description of the type of the object pointed to. */
20114 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20115 DINFO_USAGE_IND_USE);
20116
20117 /* Now output a DIE to represent this pointer-to-data-member type
20118 itself. */
20119 gen_ptr_to_mbr_type_die (type, context_die);
20120 break;
20121
20122 case FUNCTION_TYPE:
20123 /* Force out return type (in case it wasn't forced out already). */
20124 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20125 DINFO_USAGE_DIR_USE);
20126 gen_subroutine_type_die (type, context_die);
20127 break;
20128
20129 case METHOD_TYPE:
20130 /* Force out return type (in case it wasn't forced out already). */
20131 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20132 DINFO_USAGE_DIR_USE);
20133 gen_subroutine_type_die (type, context_die);
20134 break;
20135
20136 case ARRAY_TYPE:
20137 gen_array_type_die (type, context_die);
20138 break;
20139
20140 case VECTOR_TYPE:
20141 gen_array_type_die (type, context_die);
20142 break;
20143
20144 case ENUMERAL_TYPE:
20145 case RECORD_TYPE:
20146 case UNION_TYPE:
20147 case QUAL_UNION_TYPE:
20148 gen_tagged_type_die (type, context_die, usage);
20149 return;
20150
20151 case VOID_TYPE:
20152 case INTEGER_TYPE:
20153 case REAL_TYPE:
20154 case FIXED_POINT_TYPE:
20155 case COMPLEX_TYPE:
20156 case BOOLEAN_TYPE:
20157 case POINTER_BOUNDS_TYPE:
20158 /* No DIEs needed for fundamental types. */
20159 break;
20160
20161 case NULLPTR_TYPE:
20162 case LANG_TYPE:
20163 /* Just use DW_TAG_unspecified_type. */
20164 {
20165 dw_die_ref type_die = lookup_type_die (type);
20166 if (type_die == NULL)
20167 {
20168 tree name = TYPE_IDENTIFIER (type);
20169 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20170 type);
20171 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20172 equate_type_number_to_die (type, type_die);
20173 }
20174 }
20175 break;
20176
20177 default:
20178 if (is_cxx_auto (type))
20179 {
20180 tree name = TYPE_IDENTIFIER (type);
20181 dw_die_ref *die = (name == get_identifier ("auto")
20182 ? &auto_die : &decltype_auto_die);
20183 if (!*die)
20184 {
20185 *die = new_die (DW_TAG_unspecified_type,
20186 comp_unit_die (), NULL_TREE);
20187 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20188 }
20189 equate_type_number_to_die (type, *die);
20190 break;
20191 }
20192 gcc_unreachable ();
20193 }
20194
20195 TREE_ASM_WRITTEN (type) = 1;
20196 }
20197
20198 static void
20199 gen_type_die (tree type, dw_die_ref context_die)
20200 {
20201 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20202 }
20203
20204 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20205 things which are local to the given block. */
20206
20207 static void
20208 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20209 {
20210 int must_output_die = 0;
20211 bool inlined_func;
20212
20213 /* Ignore blocks that are NULL. */
20214 if (stmt == NULL_TREE)
20215 return;
20216
20217 inlined_func = inlined_function_outer_scope_p (stmt);
20218
20219 /* If the block is one fragment of a non-contiguous block, do not
20220 process the variables, since they will have been done by the
20221 origin block. Do process subblocks. */
20222 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20223 {
20224 tree sub;
20225
20226 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20227 gen_block_die (sub, context_die, depth + 1);
20228
20229 return;
20230 }
20231
20232 /* Determine if we need to output any Dwarf DIEs at all to represent this
20233 block. */
20234 if (inlined_func)
20235 /* The outer scopes for inlinings *must* always be represented. We
20236 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20237 must_output_die = 1;
20238 else
20239 {
20240 /* Determine if this block directly contains any "significant"
20241 local declarations which we will need to output DIEs for. */
20242 if (debug_info_level > DINFO_LEVEL_TERSE)
20243 /* We are not in terse mode so *any* local declaration counts
20244 as being a "significant" one. */
20245 must_output_die = ((BLOCK_VARS (stmt) != NULL
20246 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20247 && (TREE_USED (stmt)
20248 || TREE_ASM_WRITTEN (stmt)
20249 || BLOCK_ABSTRACT (stmt)));
20250 else if ((TREE_USED (stmt)
20251 || TREE_ASM_WRITTEN (stmt)
20252 || BLOCK_ABSTRACT (stmt))
20253 && !dwarf2out_ignore_block (stmt))
20254 must_output_die = 1;
20255 }
20256
20257 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20258 DIE for any block which contains no significant local declarations at
20259 all. Rather, in such cases we just call `decls_for_scope' so that any
20260 needed Dwarf info for any sub-blocks will get properly generated. Note
20261 that in terse mode, our definition of what constitutes a "significant"
20262 local declaration gets restricted to include only inlined function
20263 instances and local (nested) function definitions. */
20264 if (must_output_die)
20265 {
20266 if (inlined_func)
20267 {
20268 /* If STMT block is abstract, that means we have been called
20269 indirectly from dwarf2out_abstract_function.
20270 That function rightfully marks the descendent blocks (of
20271 the abstract function it is dealing with) as being abstract,
20272 precisely to prevent us from emitting any
20273 DW_TAG_inlined_subroutine DIE as a descendent
20274 of an abstract function instance. So in that case, we should
20275 not call gen_inlined_subroutine_die.
20276
20277 Later though, when cgraph asks dwarf2out to emit info
20278 for the concrete instance of the function decl into which
20279 the concrete instance of STMT got inlined, the later will lead
20280 to the generation of a DW_TAG_inlined_subroutine DIE. */
20281 if (! BLOCK_ABSTRACT (stmt))
20282 gen_inlined_subroutine_die (stmt, context_die, depth);
20283 }
20284 else
20285 gen_lexical_block_die (stmt, context_die, depth);
20286 }
20287 else
20288 decls_for_scope (stmt, context_die, depth);
20289 }
20290
20291 /* Process variable DECL (or variable with origin ORIGIN) within
20292 block STMT and add it to CONTEXT_DIE. */
20293 static void
20294 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20295 {
20296 dw_die_ref die;
20297 tree decl_or_origin = decl ? decl : origin;
20298
20299 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20300 die = lookup_decl_die (decl_or_origin);
20301 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20302 && TYPE_DECL_IS_STUB (decl_or_origin))
20303 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20304 else
20305 die = NULL;
20306
20307 if (die != NULL && die->die_parent == NULL)
20308 add_child_die (context_die, die);
20309 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20310 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20311 stmt, context_die);
20312 else
20313 gen_decl_die (decl, origin, context_die);
20314 }
20315
20316 /* Generate all of the decls declared within a given scope and (recursively)
20317 all of its sub-blocks. */
20318
20319 static void
20320 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20321 {
20322 tree decl;
20323 unsigned int i;
20324 tree subblocks;
20325
20326 /* Ignore NULL blocks. */
20327 if (stmt == NULL_TREE)
20328 return;
20329
20330 /* Output the DIEs to represent all of the data objects and typedefs
20331 declared directly within this block but not within any nested
20332 sub-blocks. Also, nested function and tag DIEs have been
20333 generated with a parent of NULL; fix that up now. We don't
20334 have to do this if we're at -g1. */
20335 if (debug_info_level > DINFO_LEVEL_TERSE)
20336 {
20337 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20338 process_scope_var (stmt, decl, NULL_TREE, context_die);
20339 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20340 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20341 context_die);
20342 }
20343
20344 /* Even if we're at -g1, we need to process the subblocks in order to get
20345 inlined call information. */
20346
20347 /* Output the DIEs to represent all sub-blocks (and the items declared
20348 therein) of this block. */
20349 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20350 subblocks != NULL;
20351 subblocks = BLOCK_CHAIN (subblocks))
20352 gen_block_die (subblocks, context_die, depth + 1);
20353 }
20354
20355 /* Is this a typedef we can avoid emitting? */
20356
20357 static inline int
20358 is_redundant_typedef (const_tree decl)
20359 {
20360 if (TYPE_DECL_IS_STUB (decl))
20361 return 1;
20362
20363 if (DECL_ARTIFICIAL (decl)
20364 && DECL_CONTEXT (decl)
20365 && is_tagged_type (DECL_CONTEXT (decl))
20366 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20367 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20368 /* Also ignore the artificial member typedef for the class name. */
20369 return 1;
20370
20371 return 0;
20372 }
20373
20374 /* Return TRUE if TYPE is a typedef that names a type for linkage
20375 purposes. This kind of typedefs is produced by the C++ FE for
20376 constructs like:
20377
20378 typedef struct {...} foo;
20379
20380 In that case, there is no typedef variant type produced for foo.
20381 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20382 struct type. */
20383
20384 static bool
20385 is_naming_typedef_decl (const_tree decl)
20386 {
20387 if (decl == NULL_TREE
20388 || TREE_CODE (decl) != TYPE_DECL
20389 || !is_tagged_type (TREE_TYPE (decl))
20390 || DECL_IS_BUILTIN (decl)
20391 || is_redundant_typedef (decl)
20392 /* It looks like Ada produces TYPE_DECLs that are very similar
20393 to C++ naming typedefs but that have different
20394 semantics. Let's be specific to c++ for now. */
20395 || !is_cxx ())
20396 return FALSE;
20397
20398 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20399 && TYPE_NAME (TREE_TYPE (decl)) == decl
20400 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20401 != TYPE_NAME (TREE_TYPE (decl))));
20402 }
20403
20404 /* Returns the DIE for a context. */
20405
20406 static inline dw_die_ref
20407 get_context_die (tree context)
20408 {
20409 if (context)
20410 {
20411 /* Find die that represents this context. */
20412 if (TYPE_P (context))
20413 {
20414 context = TYPE_MAIN_VARIANT (context);
20415 return strip_naming_typedef (context, force_type_die (context));
20416 }
20417 else
20418 return force_decl_die (context);
20419 }
20420 return comp_unit_die ();
20421 }
20422
20423 /* Returns the DIE for decl. A DIE will always be returned. */
20424
20425 static dw_die_ref
20426 force_decl_die (tree decl)
20427 {
20428 dw_die_ref decl_die;
20429 unsigned saved_external_flag;
20430 tree save_fn = NULL_TREE;
20431 decl_die = lookup_decl_die (decl);
20432 if (!decl_die)
20433 {
20434 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20435
20436 decl_die = lookup_decl_die (decl);
20437 if (decl_die)
20438 return decl_die;
20439
20440 switch (TREE_CODE (decl))
20441 {
20442 case FUNCTION_DECL:
20443 /* Clear current_function_decl, so that gen_subprogram_die thinks
20444 that this is a declaration. At this point, we just want to force
20445 declaration die. */
20446 save_fn = current_function_decl;
20447 current_function_decl = NULL_TREE;
20448 gen_subprogram_die (decl, context_die);
20449 current_function_decl = save_fn;
20450 break;
20451
20452 case VAR_DECL:
20453 /* Set external flag to force declaration die. Restore it after
20454 gen_decl_die() call. */
20455 saved_external_flag = DECL_EXTERNAL (decl);
20456 DECL_EXTERNAL (decl) = 1;
20457 gen_decl_die (decl, NULL, context_die);
20458 DECL_EXTERNAL (decl) = saved_external_flag;
20459 break;
20460
20461 case NAMESPACE_DECL:
20462 if (dwarf_version >= 3 || !dwarf_strict)
20463 dwarf2out_decl (decl);
20464 else
20465 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20466 decl_die = comp_unit_die ();
20467 break;
20468
20469 case TRANSLATION_UNIT_DECL:
20470 decl_die = comp_unit_die ();
20471 break;
20472
20473 default:
20474 gcc_unreachable ();
20475 }
20476
20477 /* We should be able to find the DIE now. */
20478 if (!decl_die)
20479 decl_die = lookup_decl_die (decl);
20480 gcc_assert (decl_die);
20481 }
20482
20483 return decl_die;
20484 }
20485
20486 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20487 always returned. */
20488
20489 static dw_die_ref
20490 force_type_die (tree type)
20491 {
20492 dw_die_ref type_die;
20493
20494 type_die = lookup_type_die (type);
20495 if (!type_die)
20496 {
20497 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20498
20499 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20500 context_die);
20501 gcc_assert (type_die);
20502 }
20503 return type_die;
20504 }
20505
20506 /* Force out any required namespaces to be able to output DECL,
20507 and return the new context_die for it, if it's changed. */
20508
20509 static dw_die_ref
20510 setup_namespace_context (tree thing, dw_die_ref context_die)
20511 {
20512 tree context = (DECL_P (thing)
20513 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20514 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20515 /* Force out the namespace. */
20516 context_die = force_decl_die (context);
20517
20518 return context_die;
20519 }
20520
20521 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20522 type) within its namespace, if appropriate.
20523
20524 For compatibility with older debuggers, namespace DIEs only contain
20525 declarations; all definitions are emitted at CU scope. */
20526
20527 static dw_die_ref
20528 declare_in_namespace (tree thing, dw_die_ref context_die)
20529 {
20530 dw_die_ref ns_context;
20531
20532 if (debug_info_level <= DINFO_LEVEL_TERSE)
20533 return context_die;
20534
20535 /* External declarations in the local scope only need to be emitted
20536 once, not once in the namespace and once in the scope.
20537
20538 This avoids declaring the `extern' below in the
20539 namespace DIE as well as in the innermost scope:
20540
20541 namespace S
20542 {
20543 int i=5;
20544 int foo()
20545 {
20546 int i=8;
20547 extern int i;
20548 return i;
20549 }
20550 }
20551 */
20552 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20553 return context_die;
20554
20555 /* If this decl is from an inlined function, then don't try to emit it in its
20556 namespace, as we will get confused. It would have already been emitted
20557 when the abstract instance of the inline function was emitted anyways. */
20558 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20559 return context_die;
20560
20561 ns_context = setup_namespace_context (thing, context_die);
20562
20563 if (ns_context != context_die)
20564 {
20565 if (is_fortran ())
20566 return ns_context;
20567 if (DECL_P (thing))
20568 gen_decl_die (thing, NULL, ns_context);
20569 else
20570 gen_type_die (thing, ns_context);
20571 }
20572 return context_die;
20573 }
20574
20575 /* Generate a DIE for a namespace or namespace alias. */
20576
20577 static void
20578 gen_namespace_die (tree decl, dw_die_ref context_die)
20579 {
20580 dw_die_ref namespace_die;
20581
20582 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20583 they are an alias of. */
20584 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20585 {
20586 /* Output a real namespace or module. */
20587 context_die = setup_namespace_context (decl, comp_unit_die ());
20588 namespace_die = new_die (is_fortran ()
20589 ? DW_TAG_module : DW_TAG_namespace,
20590 context_die, decl);
20591 /* For Fortran modules defined in different CU don't add src coords. */
20592 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20593 {
20594 const char *name = dwarf2_name (decl, 0);
20595 if (name)
20596 add_name_attribute (namespace_die, name);
20597 }
20598 else
20599 add_name_and_src_coords_attributes (namespace_die, decl);
20600 if (DECL_EXTERNAL (decl))
20601 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20602 equate_decl_number_to_die (decl, namespace_die);
20603 }
20604 else
20605 {
20606 /* Output a namespace alias. */
20607
20608 /* Force out the namespace we are an alias of, if necessary. */
20609 dw_die_ref origin_die
20610 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20611
20612 if (DECL_FILE_SCOPE_P (decl)
20613 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20614 context_die = setup_namespace_context (decl, comp_unit_die ());
20615 /* Now create the namespace alias DIE. */
20616 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20617 add_name_and_src_coords_attributes (namespace_die, decl);
20618 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20619 equate_decl_number_to_die (decl, namespace_die);
20620 }
20621 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20622 if (want_pubnames ())
20623 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20624 }
20625
20626 /* Generate Dwarf debug information for a decl described by DECL.
20627 The return value is currently only meaningful for PARM_DECLs,
20628 for all other decls it returns NULL. */
20629
20630 static dw_die_ref
20631 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20632 {
20633 tree decl_or_origin = decl ? decl : origin;
20634 tree class_origin = NULL, ultimate_origin;
20635
20636 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20637 return NULL;
20638
20639 /* Ignore pointer bounds decls. */
20640 if (DECL_P (decl_or_origin)
20641 && TREE_TYPE (decl_or_origin)
20642 && POINTER_BOUNDS_P (decl_or_origin))
20643 return NULL;
20644
20645 switch (TREE_CODE (decl_or_origin))
20646 {
20647 case ERROR_MARK:
20648 break;
20649
20650 case CONST_DECL:
20651 if (!is_fortran () && !is_ada ())
20652 {
20653 /* The individual enumerators of an enum type get output when we output
20654 the Dwarf representation of the relevant enum type itself. */
20655 break;
20656 }
20657
20658 /* Emit its type. */
20659 gen_type_die (TREE_TYPE (decl), context_die);
20660
20661 /* And its containing namespace. */
20662 context_die = declare_in_namespace (decl, context_die);
20663
20664 gen_const_die (decl, context_die);
20665 break;
20666
20667 case FUNCTION_DECL:
20668 /* Don't output any DIEs to represent mere function declarations,
20669 unless they are class members or explicit block externs. */
20670 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20671 && DECL_FILE_SCOPE_P (decl_or_origin)
20672 && (current_function_decl == NULL_TREE
20673 || DECL_ARTIFICIAL (decl_or_origin)))
20674 break;
20675
20676 #if 0
20677 /* FIXME */
20678 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20679 on local redeclarations of global functions. That seems broken. */
20680 if (current_function_decl != decl)
20681 /* This is only a declaration. */;
20682 #endif
20683
20684 /* If we're emitting a clone, emit info for the abstract instance. */
20685 if (origin || DECL_ORIGIN (decl) != decl)
20686 dwarf2out_abstract_function (origin
20687 ? DECL_ORIGIN (origin)
20688 : DECL_ABSTRACT_ORIGIN (decl));
20689
20690 /* If we're emitting an out-of-line copy of an inline function,
20691 emit info for the abstract instance and set up to refer to it. */
20692 else if (cgraph_function_possibly_inlined_p (decl)
20693 && ! DECL_ABSTRACT_P (decl)
20694 && ! class_or_namespace_scope_p (context_die)
20695 /* dwarf2out_abstract_function won't emit a die if this is just
20696 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20697 that case, because that works only if we have a die. */
20698 && DECL_INITIAL (decl) != NULL_TREE)
20699 {
20700 dwarf2out_abstract_function (decl);
20701 set_decl_origin_self (decl);
20702 }
20703
20704 /* Otherwise we're emitting the primary DIE for this decl. */
20705 else if (debug_info_level > DINFO_LEVEL_TERSE)
20706 {
20707 /* Before we describe the FUNCTION_DECL itself, make sure that we
20708 have its containing type. */
20709 if (!origin)
20710 origin = decl_class_context (decl);
20711 if (origin != NULL_TREE)
20712 gen_type_die (origin, context_die);
20713
20714 /* And its return type. */
20715 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20716
20717 /* And its virtual context. */
20718 if (DECL_VINDEX (decl) != NULL_TREE)
20719 gen_type_die (DECL_CONTEXT (decl), context_die);
20720
20721 /* Make sure we have a member DIE for decl. */
20722 if (origin != NULL_TREE)
20723 gen_type_die_for_member (origin, decl, context_die);
20724
20725 /* And its containing namespace. */
20726 context_die = declare_in_namespace (decl, context_die);
20727 }
20728
20729 /* Now output a DIE to represent the function itself. */
20730 if (decl)
20731 gen_subprogram_die (decl, context_die);
20732 break;
20733
20734 case TYPE_DECL:
20735 /* If we are in terse mode, don't generate any DIEs to represent any
20736 actual typedefs. */
20737 if (debug_info_level <= DINFO_LEVEL_TERSE)
20738 break;
20739
20740 /* In the special case of a TYPE_DECL node representing the declaration
20741 of some type tag, if the given TYPE_DECL is marked as having been
20742 instantiated from some other (original) TYPE_DECL node (e.g. one which
20743 was generated within the original definition of an inline function) we
20744 used to generate a special (abbreviated) DW_TAG_structure_type,
20745 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20746 should be actually referencing those DIEs, as variable DIEs with that
20747 type would be emitted already in the abstract origin, so it was always
20748 removed during unused type prunning. Don't add anything in this
20749 case. */
20750 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20751 break;
20752
20753 if (is_redundant_typedef (decl))
20754 gen_type_die (TREE_TYPE (decl), context_die);
20755 else
20756 /* Output a DIE to represent the typedef itself. */
20757 gen_typedef_die (decl, context_die);
20758 break;
20759
20760 case LABEL_DECL:
20761 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20762 gen_label_die (decl, context_die);
20763 break;
20764
20765 case VAR_DECL:
20766 case RESULT_DECL:
20767 /* If we are in terse mode, don't generate any DIEs to represent any
20768 variable declarations or definitions. */
20769 if (debug_info_level <= DINFO_LEVEL_TERSE)
20770 break;
20771
20772 /* Output any DIEs that are needed to specify the type of this data
20773 object. */
20774 if (decl_by_reference_p (decl_or_origin))
20775 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20776 else
20777 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20778
20779 /* And its containing type. */
20780 class_origin = decl_class_context (decl_or_origin);
20781 if (class_origin != NULL_TREE)
20782 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20783
20784 /* And its containing namespace. */
20785 context_die = declare_in_namespace (decl_or_origin, context_die);
20786
20787 /* Now output the DIE to represent the data object itself. This gets
20788 complicated because of the possibility that the VAR_DECL really
20789 represents an inlined instance of a formal parameter for an inline
20790 function. */
20791 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20792 if (ultimate_origin != NULL_TREE
20793 && TREE_CODE (ultimate_origin) == PARM_DECL)
20794 gen_formal_parameter_die (decl, origin,
20795 true /* Emit name attribute. */,
20796 context_die);
20797 else
20798 gen_variable_die (decl, origin, context_die);
20799 break;
20800
20801 case FIELD_DECL:
20802 /* Ignore the nameless fields that are used to skip bits but handle C++
20803 anonymous unions and structs. */
20804 if (DECL_NAME (decl) != NULL_TREE
20805 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20806 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20807 {
20808 gen_type_die (member_declared_type (decl), context_die);
20809 gen_field_die (decl, context_die);
20810 }
20811 break;
20812
20813 case PARM_DECL:
20814 if (DECL_BY_REFERENCE (decl_or_origin))
20815 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20816 else
20817 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20818 return gen_formal_parameter_die (decl, origin,
20819 true /* Emit name attribute. */,
20820 context_die);
20821
20822 case NAMESPACE_DECL:
20823 case IMPORTED_DECL:
20824 if (dwarf_version >= 3 || !dwarf_strict)
20825 gen_namespace_die (decl, context_die);
20826 break;
20827
20828 case NAMELIST_DECL:
20829 gen_namelist_decl (DECL_NAME (decl), context_die,
20830 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20831 break;
20832
20833 default:
20834 /* Probably some frontend-internal decl. Assume we don't care. */
20835 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20836 break;
20837 }
20838
20839 return NULL;
20840 }
20841 \f
20842 /* Output debug information for global decl DECL. Called from toplev.c after
20843 compilation proper has finished. */
20844
20845 static void
20846 dwarf2out_global_decl (tree decl)
20847 {
20848 /* Output DWARF2 information for file-scope tentative data object
20849 declarations, file-scope (extern) function declarations (which
20850 had no corresponding body) and file-scope tagged type declarations
20851 and definitions which have not yet been forced out. */
20852 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20853 && !POINTER_BOUNDS_P (decl))
20854 dwarf2out_decl (decl);
20855 }
20856
20857 /* Output debug information for type decl DECL. Called from toplev.c
20858 and from language front ends (to record built-in types). */
20859 static void
20860 dwarf2out_type_decl (tree decl, int local)
20861 {
20862 if (!local)
20863 dwarf2out_decl (decl);
20864 }
20865
20866 /* Output debug information for imported module or decl DECL.
20867 NAME is non-NULL name in the lexical block if the decl has been renamed.
20868 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20869 that DECL belongs to.
20870 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20871 static void
20872 dwarf2out_imported_module_or_decl_1 (tree decl,
20873 tree name,
20874 tree lexical_block,
20875 dw_die_ref lexical_block_die)
20876 {
20877 expanded_location xloc;
20878 dw_die_ref imported_die = NULL;
20879 dw_die_ref at_import_die;
20880
20881 if (TREE_CODE (decl) == IMPORTED_DECL)
20882 {
20883 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20884 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20885 gcc_assert (decl);
20886 }
20887 else
20888 xloc = expand_location (input_location);
20889
20890 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20891 {
20892 at_import_die = force_type_die (TREE_TYPE (decl));
20893 /* For namespace N { typedef void T; } using N::T; base_type_die
20894 returns NULL, but DW_TAG_imported_declaration requires
20895 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20896 if (!at_import_die)
20897 {
20898 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20899 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20900 at_import_die = lookup_type_die (TREE_TYPE (decl));
20901 gcc_assert (at_import_die);
20902 }
20903 }
20904 else
20905 {
20906 at_import_die = lookup_decl_die (decl);
20907 if (!at_import_die)
20908 {
20909 /* If we're trying to avoid duplicate debug info, we may not have
20910 emitted the member decl for this field. Emit it now. */
20911 if (TREE_CODE (decl) == FIELD_DECL)
20912 {
20913 tree type = DECL_CONTEXT (decl);
20914
20915 if (TYPE_CONTEXT (type)
20916 && TYPE_P (TYPE_CONTEXT (type))
20917 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20918 DINFO_USAGE_DIR_USE))
20919 return;
20920 gen_type_die_for_member (type, decl,
20921 get_context_die (TYPE_CONTEXT (type)));
20922 }
20923 if (TREE_CODE (decl) == NAMELIST_DECL)
20924 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20925 get_context_die (DECL_CONTEXT (decl)),
20926 NULL_TREE);
20927 else
20928 at_import_die = force_decl_die (decl);
20929 }
20930 }
20931
20932 if (TREE_CODE (decl) == NAMESPACE_DECL)
20933 {
20934 if (dwarf_version >= 3 || !dwarf_strict)
20935 imported_die = new_die (DW_TAG_imported_module,
20936 lexical_block_die,
20937 lexical_block);
20938 else
20939 return;
20940 }
20941 else
20942 imported_die = new_die (DW_TAG_imported_declaration,
20943 lexical_block_die,
20944 lexical_block);
20945
20946 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20947 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20948 if (name)
20949 add_AT_string (imported_die, DW_AT_name,
20950 IDENTIFIER_POINTER (name));
20951 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20952 }
20953
20954 /* Output debug information for imported module or decl DECL.
20955 NAME is non-NULL name in context if the decl has been renamed.
20956 CHILD is true if decl is one of the renamed decls as part of
20957 importing whole module. */
20958
20959 static void
20960 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20961 bool child)
20962 {
20963 /* dw_die_ref at_import_die; */
20964 dw_die_ref scope_die;
20965
20966 if (debug_info_level <= DINFO_LEVEL_TERSE)
20967 return;
20968
20969 gcc_assert (decl);
20970
20971 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20972 We need decl DIE for reference and scope die. First, get DIE for the decl
20973 itself. */
20974
20975 /* Get the scope die for decl context. Use comp_unit_die for global module
20976 or decl. If die is not found for non globals, force new die. */
20977 if (context
20978 && TYPE_P (context)
20979 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20980 return;
20981
20982 if (!(dwarf_version >= 3 || !dwarf_strict))
20983 return;
20984
20985 scope_die = get_context_die (context);
20986
20987 if (child)
20988 {
20989 gcc_assert (scope_die->die_child);
20990 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20991 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20992 scope_die = scope_die->die_child;
20993 }
20994
20995 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20996 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20997
20998 }
20999
21000 /* Output debug information for namelists. */
21001
21002 static dw_die_ref
21003 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21004 {
21005 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21006 tree value;
21007 unsigned i;
21008
21009 if (debug_info_level <= DINFO_LEVEL_TERSE)
21010 return NULL;
21011
21012 gcc_assert (scope_die != NULL);
21013 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21014 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21015
21016 /* If there are no item_decls, we have a nondefining namelist, e.g.
21017 with USE association; hence, set DW_AT_declaration. */
21018 if (item_decls == NULL_TREE)
21019 {
21020 add_AT_flag (nml_die, DW_AT_declaration, 1);
21021 return nml_die;
21022 }
21023
21024 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21025 {
21026 nml_item_ref_die = lookup_decl_die (value);
21027 if (!nml_item_ref_die)
21028 nml_item_ref_die = force_decl_die (value);
21029
21030 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21031 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21032 }
21033 return nml_die;
21034 }
21035
21036
21037 /* Write the debugging output for DECL. */
21038
21039 static void
21040 dwarf2out_decl (tree decl)
21041 {
21042 dw_die_ref context_die = comp_unit_die ();
21043
21044 switch (TREE_CODE (decl))
21045 {
21046 case ERROR_MARK:
21047 return;
21048
21049 case FUNCTION_DECL:
21050 /* What we would really like to do here is to filter out all mere
21051 file-scope declarations of file-scope functions which are never
21052 referenced later within this translation unit (and keep all of ones
21053 that *are* referenced later on) but we aren't clairvoyant, so we have
21054 no idea which functions will be referenced in the future (i.e. later
21055 on within the current translation unit). So here we just ignore all
21056 file-scope function declarations which are not also definitions. If
21057 and when the debugger needs to know something about these functions,
21058 it will have to hunt around and find the DWARF information associated
21059 with the definition of the function.
21060
21061 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21062 nodes represent definitions and which ones represent mere
21063 declarations. We have to check DECL_INITIAL instead. That's because
21064 the C front-end supports some weird semantics for "extern inline"
21065 function definitions. These can get inlined within the current
21066 translation unit (and thus, we need to generate Dwarf info for their
21067 abstract instances so that the Dwarf info for the concrete inlined
21068 instances can have something to refer to) but the compiler never
21069 generates any out-of-lines instances of such things (despite the fact
21070 that they *are* definitions).
21071
21072 The important point is that the C front-end marks these "extern
21073 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21074 them anyway. Note that the C++ front-end also plays some similar games
21075 for inline function definitions appearing within include files which
21076 also contain `#pragma interface' pragmas.
21077
21078 If we are called from dwarf2out_abstract_function output a DIE
21079 anyway. We can end up here this way with early inlining and LTO
21080 where the inlined function is output in a different LTRANS unit
21081 or not at all. */
21082 if (DECL_INITIAL (decl) == NULL_TREE
21083 && ! DECL_ABSTRACT_P (decl))
21084 return;
21085
21086 /* If we're a nested function, initially use a parent of NULL; if we're
21087 a plain function, this will be fixed up in decls_for_scope. If
21088 we're a method, it will be ignored, since we already have a DIE. */
21089 if (decl_function_context (decl)
21090 /* But if we're in terse mode, we don't care about scope. */
21091 && debug_info_level > DINFO_LEVEL_TERSE)
21092 context_die = NULL;
21093 break;
21094
21095 case VAR_DECL:
21096 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21097 declaration and if the declaration was never even referenced from
21098 within this entire compilation unit. We suppress these DIEs in
21099 order to save space in the .debug section (by eliminating entries
21100 which are probably useless). Note that we must not suppress
21101 block-local extern declarations (whether used or not) because that
21102 would screw-up the debugger's name lookup mechanism and cause it to
21103 miss things which really ought to be in scope at a given point. */
21104 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21105 return;
21106
21107 /* For local statics lookup proper context die. */
21108 if (TREE_STATIC (decl)
21109 && DECL_CONTEXT (decl)
21110 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21111 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21112
21113 /* If we are in terse mode, don't generate any DIEs to represent any
21114 variable declarations or definitions. */
21115 if (debug_info_level <= DINFO_LEVEL_TERSE)
21116 return;
21117 break;
21118
21119 case CONST_DECL:
21120 if (debug_info_level <= DINFO_LEVEL_TERSE)
21121 return;
21122 if (!is_fortran () && !is_ada ())
21123 return;
21124 if (TREE_STATIC (decl) && decl_function_context (decl))
21125 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21126 break;
21127
21128 case NAMESPACE_DECL:
21129 case IMPORTED_DECL:
21130 if (debug_info_level <= DINFO_LEVEL_TERSE)
21131 return;
21132 if (lookup_decl_die (decl) != NULL)
21133 return;
21134 break;
21135
21136 case TYPE_DECL:
21137 /* Don't emit stubs for types unless they are needed by other DIEs. */
21138 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21139 return;
21140
21141 /* Don't bother trying to generate any DIEs to represent any of the
21142 normal built-in types for the language we are compiling. */
21143 if (DECL_IS_BUILTIN (decl))
21144 return;
21145
21146 /* If we are in terse mode, don't generate any DIEs for types. */
21147 if (debug_info_level <= DINFO_LEVEL_TERSE)
21148 return;
21149
21150 /* If we're a function-scope tag, initially use a parent of NULL;
21151 this will be fixed up in decls_for_scope. */
21152 if (decl_function_context (decl))
21153 context_die = NULL;
21154
21155 break;
21156
21157 case NAMELIST_DECL:
21158 break;
21159
21160 default:
21161 return;
21162 }
21163
21164 gen_decl_die (decl, NULL, context_die);
21165 }
21166
21167 /* Write the debugging output for DECL. */
21168
21169 static void
21170 dwarf2out_function_decl (tree decl)
21171 {
21172 dwarf2out_decl (decl);
21173 call_arg_locations = NULL;
21174 call_arg_loc_last = NULL;
21175 call_site_count = -1;
21176 tail_call_site_count = -1;
21177 block_map.release ();
21178 decl_loc_table->empty ();
21179 cached_dw_loc_list_table->empty ();
21180 }
21181
21182 /* Output a marker (i.e. a label) for the beginning of the generated code for
21183 a lexical block. */
21184
21185 static void
21186 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21187 unsigned int blocknum)
21188 {
21189 switch_to_section (current_function_section ());
21190 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21191 }
21192
21193 /* Output a marker (i.e. a label) for the end of the generated code for a
21194 lexical block. */
21195
21196 static void
21197 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21198 {
21199 switch_to_section (current_function_section ());
21200 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21201 }
21202
21203 /* Returns nonzero if it is appropriate not to emit any debugging
21204 information for BLOCK, because it doesn't contain any instructions.
21205
21206 Don't allow this for blocks with nested functions or local classes
21207 as we would end up with orphans, and in the presence of scheduling
21208 we may end up calling them anyway. */
21209
21210 static bool
21211 dwarf2out_ignore_block (const_tree block)
21212 {
21213 tree decl;
21214 unsigned int i;
21215
21216 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21217 if (TREE_CODE (decl) == FUNCTION_DECL
21218 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21219 return 0;
21220 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21221 {
21222 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21223 if (TREE_CODE (decl) == FUNCTION_DECL
21224 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21225 return 0;
21226 }
21227
21228 return 1;
21229 }
21230
21231 /* Hash table routines for file_hash. */
21232
21233 bool
21234 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21235 {
21236 return filename_cmp (p1->filename, p2) == 0;
21237 }
21238
21239 hashval_t
21240 dwarf_file_hasher::hash (dwarf_file_data *p)
21241 {
21242 return htab_hash_string (p->filename);
21243 }
21244
21245 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21246 dwarf2out.c) and return its "index". The index of each (known) filename is
21247 just a unique number which is associated with only that one filename. We
21248 need such numbers for the sake of generating labels (in the .debug_sfnames
21249 section) and references to those files numbers (in the .debug_srcinfo
21250 and.debug_macinfo sections). If the filename given as an argument is not
21251 found in our current list, add it to the list and assign it the next
21252 available unique index number. In order to speed up searches, we remember
21253 the index of the filename was looked up last. This handles the majority of
21254 all searches. */
21255
21256 static struct dwarf_file_data *
21257 lookup_filename (const char *file_name)
21258 {
21259 struct dwarf_file_data * created;
21260
21261 /* Check to see if the file name that was searched on the previous
21262 call matches this file name. If so, return the index. */
21263 if (file_table_last_lookup
21264 && (file_name == file_table_last_lookup->filename
21265 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21266 return file_table_last_lookup;
21267
21268 /* Didn't match the previous lookup, search the table. */
21269 dwarf_file_data **slot
21270 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21271 INSERT);
21272 if (*slot)
21273 return *slot;
21274
21275 created = ggc_alloc<dwarf_file_data> ();
21276 created->filename = file_name;
21277 created->emitted_number = 0;
21278 *slot = created;
21279 return created;
21280 }
21281
21282 /* If the assembler will construct the file table, then translate the compiler
21283 internal file table number into the assembler file table number, and emit
21284 a .file directive if we haven't already emitted one yet. The file table
21285 numbers are different because we prune debug info for unused variables and
21286 types, which may include filenames. */
21287
21288 static int
21289 maybe_emit_file (struct dwarf_file_data * fd)
21290 {
21291 if (! fd->emitted_number)
21292 {
21293 if (last_emitted_file)
21294 fd->emitted_number = last_emitted_file->emitted_number + 1;
21295 else
21296 fd->emitted_number = 1;
21297 last_emitted_file = fd;
21298
21299 if (DWARF2_ASM_LINE_DEBUG_INFO)
21300 {
21301 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21302 output_quoted_string (asm_out_file,
21303 remap_debug_filename (fd->filename));
21304 fputc ('\n', asm_out_file);
21305 }
21306 }
21307
21308 return fd->emitted_number;
21309 }
21310
21311 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21312 That generation should happen after function debug info has been
21313 generated. The value of the attribute is the constant value of ARG. */
21314
21315 static void
21316 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21317 {
21318 die_arg_entry entry;
21319
21320 if (!die || !arg)
21321 return;
21322
21323 if (!tmpl_value_parm_die_table)
21324 vec_alloc (tmpl_value_parm_die_table, 32);
21325
21326 entry.die = die;
21327 entry.arg = arg;
21328 vec_safe_push (tmpl_value_parm_die_table, entry);
21329 }
21330
21331 /* Return TRUE if T is an instance of generic type, FALSE
21332 otherwise. */
21333
21334 static bool
21335 generic_type_p (tree t)
21336 {
21337 if (t == NULL_TREE || !TYPE_P (t))
21338 return false;
21339 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21340 }
21341
21342 /* Schedule the generation of the generic parameter dies for the
21343 instance of generic type T. The proper generation itself is later
21344 done by gen_scheduled_generic_parms_dies. */
21345
21346 static void
21347 schedule_generic_params_dies_gen (tree t)
21348 {
21349 if (!generic_type_p (t))
21350 return;
21351
21352 if (!generic_type_instances)
21353 vec_alloc (generic_type_instances, 256);
21354
21355 vec_safe_push (generic_type_instances, t);
21356 }
21357
21358 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21359 by append_entry_to_tmpl_value_parm_die_table. This function must
21360 be called after function DIEs have been generated. */
21361
21362 static void
21363 gen_remaining_tmpl_value_param_die_attribute (void)
21364 {
21365 if (tmpl_value_parm_die_table)
21366 {
21367 unsigned i;
21368 die_arg_entry *e;
21369
21370 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21371 tree_add_const_value_attribute (e->die, e->arg);
21372 }
21373 }
21374
21375 /* Generate generic parameters DIEs for instances of generic types
21376 that have been previously scheduled by
21377 schedule_generic_params_dies_gen. This function must be called
21378 after all the types of the CU have been laid out. */
21379
21380 static void
21381 gen_scheduled_generic_parms_dies (void)
21382 {
21383 unsigned i;
21384 tree t;
21385
21386 if (!generic_type_instances)
21387 return;
21388
21389 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21390 if (COMPLETE_TYPE_P (t))
21391 gen_generic_params_dies (t);
21392 }
21393
21394
21395 /* Replace DW_AT_name for the decl with name. */
21396
21397 static void
21398 dwarf2out_set_name (tree decl, tree name)
21399 {
21400 dw_die_ref die;
21401 dw_attr_ref attr;
21402 const char *dname;
21403
21404 die = TYPE_SYMTAB_DIE (decl);
21405 if (!die)
21406 return;
21407
21408 dname = dwarf2_name (name, 0);
21409 if (!dname)
21410 return;
21411
21412 attr = get_AT (die, DW_AT_name);
21413 if (attr)
21414 {
21415 struct indirect_string_node *node;
21416
21417 node = find_AT_string (dname);
21418 /* replace the string. */
21419 attr->dw_attr_val.v.val_str = node;
21420 }
21421
21422 else
21423 add_name_attribute (die, dname);
21424 }
21425
21426 /* True if before or during processing of the first function being emitted. */
21427 static bool in_first_function_p = true;
21428 /* True if loc_note during dwarf2out_var_location call might still be
21429 before first real instruction at address equal to .Ltext0. */
21430 static bool maybe_at_text_label_p = true;
21431 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21432 static unsigned int first_loclabel_num_not_at_text_label;
21433
21434 /* Called by the final INSN scan whenever we see a var location. We
21435 use it to drop labels in the right places, and throw the location in
21436 our lookup table. */
21437
21438 static void
21439 dwarf2out_var_location (rtx_insn *loc_note)
21440 {
21441 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21442 struct var_loc_node *newloc;
21443 rtx_insn *next_real, *next_note;
21444 static const char *last_label;
21445 static const char *last_postcall_label;
21446 static bool last_in_cold_section_p;
21447 static rtx_insn *expected_next_loc_note;
21448 tree decl;
21449 bool var_loc_p;
21450
21451 if (!NOTE_P (loc_note))
21452 {
21453 if (CALL_P (loc_note))
21454 {
21455 call_site_count++;
21456 if (SIBLING_CALL_P (loc_note))
21457 tail_call_site_count++;
21458 }
21459 return;
21460 }
21461
21462 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21463 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21464 return;
21465
21466 /* Optimize processing a large consecutive sequence of location
21467 notes so we don't spend too much time in next_real_insn. If the
21468 next insn is another location note, remember the next_real_insn
21469 calculation for next time. */
21470 next_real = cached_next_real_insn;
21471 if (next_real)
21472 {
21473 if (expected_next_loc_note != loc_note)
21474 next_real = NULL;
21475 }
21476
21477 next_note = NEXT_INSN (loc_note);
21478 if (! next_note
21479 || next_note->deleted ()
21480 || ! NOTE_P (next_note)
21481 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21482 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21483 next_note = NULL;
21484
21485 if (! next_real)
21486 next_real = next_real_insn (loc_note);
21487
21488 if (next_note)
21489 {
21490 expected_next_loc_note = next_note;
21491 cached_next_real_insn = next_real;
21492 }
21493 else
21494 cached_next_real_insn = NULL;
21495
21496 /* If there are no instructions which would be affected by this note,
21497 don't do anything. */
21498 if (var_loc_p
21499 && next_real == NULL_RTX
21500 && !NOTE_DURING_CALL_P (loc_note))
21501 return;
21502
21503 if (next_real == NULL_RTX)
21504 next_real = get_last_insn ();
21505
21506 /* If there were any real insns between note we processed last time
21507 and this note (or if it is the first note), clear
21508 last_{,postcall_}label so that they are not reused this time. */
21509 if (last_var_location_insn == NULL_RTX
21510 || last_var_location_insn != next_real
21511 || last_in_cold_section_p != in_cold_section_p)
21512 {
21513 last_label = NULL;
21514 last_postcall_label = NULL;
21515 }
21516
21517 if (var_loc_p)
21518 {
21519 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21520 newloc = add_var_loc_to_decl (decl, loc_note,
21521 NOTE_DURING_CALL_P (loc_note)
21522 ? last_postcall_label : last_label);
21523 if (newloc == NULL)
21524 return;
21525 }
21526 else
21527 {
21528 decl = NULL_TREE;
21529 newloc = NULL;
21530 }
21531
21532 /* If there were no real insns between note we processed last time
21533 and this note, use the label we emitted last time. Otherwise
21534 create a new label and emit it. */
21535 if (last_label == NULL)
21536 {
21537 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21538 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21539 loclabel_num++;
21540 last_label = ggc_strdup (loclabel);
21541 /* See if loclabel might be equal to .Ltext0. If yes,
21542 bump first_loclabel_num_not_at_text_label. */
21543 if (!have_multiple_function_sections
21544 && in_first_function_p
21545 && maybe_at_text_label_p)
21546 {
21547 static rtx_insn *last_start;
21548 rtx_insn *insn;
21549 for (insn = loc_note; insn; insn = previous_insn (insn))
21550 if (insn == last_start)
21551 break;
21552 else if (!NONDEBUG_INSN_P (insn))
21553 continue;
21554 else
21555 {
21556 rtx body = PATTERN (insn);
21557 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21558 continue;
21559 /* Inline asm could occupy zero bytes. */
21560 else if (GET_CODE (body) == ASM_INPUT
21561 || asm_noperands (body) >= 0)
21562 continue;
21563 #ifdef HAVE_attr_length
21564 else if (get_attr_min_length (insn) == 0)
21565 continue;
21566 #endif
21567 else
21568 {
21569 /* Assume insn has non-zero length. */
21570 maybe_at_text_label_p = false;
21571 break;
21572 }
21573 }
21574 if (maybe_at_text_label_p)
21575 {
21576 last_start = loc_note;
21577 first_loclabel_num_not_at_text_label = loclabel_num;
21578 }
21579 }
21580 }
21581
21582 if (!var_loc_p)
21583 {
21584 struct call_arg_loc_node *ca_loc
21585 = ggc_cleared_alloc<call_arg_loc_node> ();
21586 rtx_insn *prev = prev_real_insn (loc_note);
21587 rtx x;
21588 ca_loc->call_arg_loc_note = loc_note;
21589 ca_loc->next = NULL;
21590 ca_loc->label = last_label;
21591 gcc_assert (prev
21592 && (CALL_P (prev)
21593 || (NONJUMP_INSN_P (prev)
21594 && GET_CODE (PATTERN (prev)) == SEQUENCE
21595 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21596 if (!CALL_P (prev))
21597 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21598 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21599 x = get_call_rtx_from (PATTERN (prev));
21600 if (x)
21601 {
21602 x = XEXP (XEXP (x, 0), 0);
21603 if (GET_CODE (x) == SYMBOL_REF
21604 && SYMBOL_REF_DECL (x)
21605 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21606 ca_loc->symbol_ref = x;
21607 }
21608 ca_loc->block = insn_scope (prev);
21609 if (call_arg_locations)
21610 call_arg_loc_last->next = ca_loc;
21611 else
21612 call_arg_locations = ca_loc;
21613 call_arg_loc_last = ca_loc;
21614 }
21615 else if (!NOTE_DURING_CALL_P (loc_note))
21616 newloc->label = last_label;
21617 else
21618 {
21619 if (!last_postcall_label)
21620 {
21621 sprintf (loclabel, "%s-1", last_label);
21622 last_postcall_label = ggc_strdup (loclabel);
21623 }
21624 newloc->label = last_postcall_label;
21625 }
21626
21627 last_var_location_insn = next_real;
21628 last_in_cold_section_p = in_cold_section_p;
21629 }
21630
21631 /* Note in one location list that text section has changed. */
21632
21633 int
21634 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21635 {
21636 var_loc_list *list = *slot;
21637 if (list->first)
21638 list->last_before_switch
21639 = list->last->next ? list->last->next : list->last;
21640 return 1;
21641 }
21642
21643 /* Note in all location lists that text section has changed. */
21644
21645 static void
21646 var_location_switch_text_section (void)
21647 {
21648 if (decl_loc_table == NULL)
21649 return;
21650
21651 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21652 }
21653
21654 /* Create a new line number table. */
21655
21656 static dw_line_info_table *
21657 new_line_info_table (void)
21658 {
21659 dw_line_info_table *table;
21660
21661 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21662 table->file_num = 1;
21663 table->line_num = 1;
21664 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21665
21666 return table;
21667 }
21668
21669 /* Lookup the "current" table into which we emit line info, so
21670 that we don't have to do it for every source line. */
21671
21672 static void
21673 set_cur_line_info_table (section *sec)
21674 {
21675 dw_line_info_table *table;
21676
21677 if (sec == text_section)
21678 table = text_section_line_info;
21679 else if (sec == cold_text_section)
21680 {
21681 table = cold_text_section_line_info;
21682 if (!table)
21683 {
21684 cold_text_section_line_info = table = new_line_info_table ();
21685 table->end_label = cold_end_label;
21686 }
21687 }
21688 else
21689 {
21690 const char *end_label;
21691
21692 if (flag_reorder_blocks_and_partition)
21693 {
21694 if (in_cold_section_p)
21695 end_label = crtl->subsections.cold_section_end_label;
21696 else
21697 end_label = crtl->subsections.hot_section_end_label;
21698 }
21699 else
21700 {
21701 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21702 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21703 current_function_funcdef_no);
21704 end_label = ggc_strdup (label);
21705 }
21706
21707 table = new_line_info_table ();
21708 table->end_label = end_label;
21709
21710 vec_safe_push (separate_line_info, table);
21711 }
21712
21713 if (DWARF2_ASM_LINE_DEBUG_INFO)
21714 table->is_stmt = (cur_line_info_table
21715 ? cur_line_info_table->is_stmt
21716 : DWARF_LINE_DEFAULT_IS_STMT_START);
21717 cur_line_info_table = table;
21718 }
21719
21720
21721 /* We need to reset the locations at the beginning of each
21722 function. We can't do this in the end_function hook, because the
21723 declarations that use the locations won't have been output when
21724 that hook is called. Also compute have_multiple_function_sections here. */
21725
21726 static void
21727 dwarf2out_begin_function (tree fun)
21728 {
21729 section *sec = function_section (fun);
21730
21731 if (sec != text_section)
21732 have_multiple_function_sections = true;
21733
21734 if (flag_reorder_blocks_and_partition && !cold_text_section)
21735 {
21736 gcc_assert (current_function_decl == fun);
21737 cold_text_section = unlikely_text_section ();
21738 switch_to_section (cold_text_section);
21739 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21740 switch_to_section (sec);
21741 }
21742
21743 dwarf2out_note_section_used ();
21744 call_site_count = 0;
21745 tail_call_site_count = 0;
21746
21747 set_cur_line_info_table (sec);
21748 }
21749
21750 /* Helper function of dwarf2out_end_function, called only after emitting
21751 the very first function into assembly. Check if some .debug_loc range
21752 might end with a .LVL* label that could be equal to .Ltext0.
21753 In that case we must force using absolute addresses in .debug_loc ranges,
21754 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21755 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21756 list terminator.
21757 Set have_multiple_function_sections to true in that case and
21758 terminate htab traversal. */
21759
21760 int
21761 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21762 {
21763 var_loc_list *entry = *slot;
21764 struct var_loc_node *node;
21765
21766 node = entry->first;
21767 if (node && node->next && node->next->label)
21768 {
21769 unsigned int i;
21770 const char *label = node->next->label;
21771 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21772
21773 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21774 {
21775 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21776 if (strcmp (label, loclabel) == 0)
21777 {
21778 have_multiple_function_sections = true;
21779 return 0;
21780 }
21781 }
21782 }
21783 return 1;
21784 }
21785
21786 /* Hook called after emitting a function into assembly.
21787 This does something only for the very first function emitted. */
21788
21789 static void
21790 dwarf2out_end_function (unsigned int)
21791 {
21792 if (in_first_function_p
21793 && !have_multiple_function_sections
21794 && first_loclabel_num_not_at_text_label
21795 && decl_loc_table)
21796 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21797 in_first_function_p = false;
21798 maybe_at_text_label_p = false;
21799 }
21800
21801 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21802
21803 static void
21804 push_dw_line_info_entry (dw_line_info_table *table,
21805 enum dw_line_info_opcode opcode, unsigned int val)
21806 {
21807 dw_line_info_entry e;
21808 e.opcode = opcode;
21809 e.val = val;
21810 vec_safe_push (table->entries, e);
21811 }
21812
21813 /* Output a label to mark the beginning of a source code line entry
21814 and record information relating to this source line, in
21815 'line_info_table' for later output of the .debug_line section. */
21816 /* ??? The discriminator parameter ought to be unsigned. */
21817
21818 static void
21819 dwarf2out_source_line (unsigned int line, const char *filename,
21820 int discriminator, bool is_stmt)
21821 {
21822 unsigned int file_num;
21823 dw_line_info_table *table;
21824
21825 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21826 return;
21827
21828 /* The discriminator column was added in dwarf4. Simplify the below
21829 by simply removing it if we're not supposed to output it. */
21830 if (dwarf_version < 4 && dwarf_strict)
21831 discriminator = 0;
21832
21833 table = cur_line_info_table;
21834 file_num = maybe_emit_file (lookup_filename (filename));
21835
21836 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21837 the debugger has used the second (possibly duplicate) line number
21838 at the beginning of the function to mark the end of the prologue.
21839 We could eliminate any other duplicates within the function. For
21840 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21841 that second line number entry. */
21842 /* Recall that this end-of-prologue indication is *not* the same thing
21843 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21844 to which the hook corresponds, follows the last insn that was
21845 emitted by gen_prologue. What we need is to precede the first insn
21846 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21847 insn that corresponds to something the user wrote. These may be
21848 very different locations once scheduling is enabled. */
21849
21850 if (0 && file_num == table->file_num
21851 && line == table->line_num
21852 && discriminator == table->discrim_num
21853 && is_stmt == table->is_stmt)
21854 return;
21855
21856 switch_to_section (current_function_section ());
21857
21858 /* If requested, emit something human-readable. */
21859 if (flag_debug_asm)
21860 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21861
21862 if (DWARF2_ASM_LINE_DEBUG_INFO)
21863 {
21864 /* Emit the .loc directive understood by GNU as. */
21865 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21866 file_num, line, is_stmt, discriminator */
21867 fputs ("\t.loc ", asm_out_file);
21868 fprint_ul (asm_out_file, file_num);
21869 putc (' ', asm_out_file);
21870 fprint_ul (asm_out_file, line);
21871 putc (' ', asm_out_file);
21872 putc ('0', asm_out_file);
21873
21874 if (is_stmt != table->is_stmt)
21875 {
21876 fputs (" is_stmt ", asm_out_file);
21877 putc (is_stmt ? '1' : '0', asm_out_file);
21878 }
21879 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21880 {
21881 gcc_assert (discriminator > 0);
21882 fputs (" discriminator ", asm_out_file);
21883 fprint_ul (asm_out_file, (unsigned long) discriminator);
21884 }
21885 putc ('\n', asm_out_file);
21886 }
21887 else
21888 {
21889 unsigned int label_num = ++line_info_label_num;
21890
21891 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21892
21893 push_dw_line_info_entry (table, LI_set_address, label_num);
21894 if (file_num != table->file_num)
21895 push_dw_line_info_entry (table, LI_set_file, file_num);
21896 if (discriminator != table->discrim_num)
21897 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21898 if (is_stmt != table->is_stmt)
21899 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21900 push_dw_line_info_entry (table, LI_set_line, line);
21901 }
21902
21903 table->file_num = file_num;
21904 table->line_num = line;
21905 table->discrim_num = discriminator;
21906 table->is_stmt = is_stmt;
21907 table->in_use = true;
21908 }
21909
21910 /* Record the beginning of a new source file. */
21911
21912 static void
21913 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21914 {
21915 if (flag_eliminate_dwarf2_dups)
21916 {
21917 /* Record the beginning of the file for break_out_includes. */
21918 dw_die_ref bincl_die;
21919
21920 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21921 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21922 }
21923
21924 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21925 {
21926 macinfo_entry e;
21927 e.code = DW_MACINFO_start_file;
21928 e.lineno = lineno;
21929 e.info = ggc_strdup (filename);
21930 vec_safe_push (macinfo_table, e);
21931 }
21932 }
21933
21934 /* Record the end of a source file. */
21935
21936 static void
21937 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21938 {
21939 if (flag_eliminate_dwarf2_dups)
21940 /* Record the end of the file for break_out_includes. */
21941 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21942
21943 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21944 {
21945 macinfo_entry e;
21946 e.code = DW_MACINFO_end_file;
21947 e.lineno = lineno;
21948 e.info = NULL;
21949 vec_safe_push (macinfo_table, e);
21950 }
21951 }
21952
21953 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21954 the tail part of the directive line, i.e. the part which is past the
21955 initial whitespace, #, whitespace, directive-name, whitespace part. */
21956
21957 static void
21958 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21959 const char *buffer ATTRIBUTE_UNUSED)
21960 {
21961 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21962 {
21963 macinfo_entry e;
21964 /* Insert a dummy first entry to be able to optimize the whole
21965 predefined macro block using DW_MACRO_GNU_transparent_include. */
21966 if (macinfo_table->is_empty () && lineno <= 1)
21967 {
21968 e.code = 0;
21969 e.lineno = 0;
21970 e.info = NULL;
21971 vec_safe_push (macinfo_table, e);
21972 }
21973 e.code = DW_MACINFO_define;
21974 e.lineno = lineno;
21975 e.info = ggc_strdup (buffer);
21976 vec_safe_push (macinfo_table, e);
21977 }
21978 }
21979
21980 /* Called from debug_undef 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_undef (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_undef;
22001 e.lineno = lineno;
22002 e.info = ggc_strdup (buffer);
22003 vec_safe_push (macinfo_table, e);
22004 }
22005 }
22006
22007 /* Helpers to manipulate hash table of CUs. */
22008
22009 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22010 {
22011 typedef macinfo_entry value_type;
22012 typedef macinfo_entry compare_type;
22013 static inline hashval_t hash (const value_type *);
22014 static inline bool equal (const value_type *, const compare_type *);
22015 };
22016
22017 inline hashval_t
22018 macinfo_entry_hasher::hash (const value_type *entry)
22019 {
22020 return htab_hash_string (entry->info);
22021 }
22022
22023 inline bool
22024 macinfo_entry_hasher::equal (const value_type *entry1,
22025 const compare_type *entry2)
22026 {
22027 return !strcmp (entry1->info, entry2->info);
22028 }
22029
22030 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22031
22032 /* Output a single .debug_macinfo entry. */
22033
22034 static void
22035 output_macinfo_op (macinfo_entry *ref)
22036 {
22037 int file_num;
22038 size_t len;
22039 struct indirect_string_node *node;
22040 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22041 struct dwarf_file_data *fd;
22042
22043 switch (ref->code)
22044 {
22045 case DW_MACINFO_start_file:
22046 fd = lookup_filename (ref->info);
22047 file_num = maybe_emit_file (fd);
22048 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22049 dw2_asm_output_data_uleb128 (ref->lineno,
22050 "Included from line number %lu",
22051 (unsigned long) ref->lineno);
22052 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22053 break;
22054 case DW_MACINFO_end_file:
22055 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22056 break;
22057 case DW_MACINFO_define:
22058 case DW_MACINFO_undef:
22059 len = strlen (ref->info) + 1;
22060 if (!dwarf_strict
22061 && len > DWARF_OFFSET_SIZE
22062 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22063 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22064 {
22065 ref->code = ref->code == DW_MACINFO_define
22066 ? DW_MACRO_GNU_define_indirect
22067 : DW_MACRO_GNU_undef_indirect;
22068 output_macinfo_op (ref);
22069 return;
22070 }
22071 dw2_asm_output_data (1, ref->code,
22072 ref->code == DW_MACINFO_define
22073 ? "Define macro" : "Undefine macro");
22074 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22075 (unsigned long) ref->lineno);
22076 dw2_asm_output_nstring (ref->info, -1, "The macro");
22077 break;
22078 case DW_MACRO_GNU_define_indirect:
22079 case DW_MACRO_GNU_undef_indirect:
22080 node = find_AT_string (ref->info);
22081 gcc_assert (node
22082 && ((node->form == DW_FORM_strp)
22083 || (node->form == DW_FORM_GNU_str_index)));
22084 dw2_asm_output_data (1, ref->code,
22085 ref->code == DW_MACRO_GNU_define_indirect
22086 ? "Define macro indirect"
22087 : "Undefine macro indirect");
22088 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22089 (unsigned long) ref->lineno);
22090 if (node->form == DW_FORM_strp)
22091 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22092 debug_str_section, "The macro: \"%s\"",
22093 ref->info);
22094 else
22095 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22096 ref->info);
22097 break;
22098 case DW_MACRO_GNU_transparent_include:
22099 dw2_asm_output_data (1, ref->code, "Transparent include");
22100 ASM_GENERATE_INTERNAL_LABEL (label,
22101 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22102 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22103 break;
22104 default:
22105 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22106 ASM_COMMENT_START, (unsigned long) ref->code);
22107 break;
22108 }
22109 }
22110
22111 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22112 other compilation unit .debug_macinfo sections. IDX is the first
22113 index of a define/undef, return the number of ops that should be
22114 emitted in a comdat .debug_macinfo section and emit
22115 a DW_MACRO_GNU_transparent_include entry referencing it.
22116 If the define/undef entry should be emitted normally, return 0. */
22117
22118 static unsigned
22119 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22120 macinfo_hash_type **macinfo_htab)
22121 {
22122 macinfo_entry *first, *second, *cur, *inc;
22123 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22124 unsigned char checksum[16];
22125 struct md5_ctx ctx;
22126 char *grp_name, *tail;
22127 const char *base;
22128 unsigned int i, count, encoded_filename_len, linebuf_len;
22129 macinfo_entry **slot;
22130
22131 first = &(*macinfo_table)[idx];
22132 second = &(*macinfo_table)[idx + 1];
22133
22134 /* Optimize only if there are at least two consecutive define/undef ops,
22135 and either all of them are before first DW_MACINFO_start_file
22136 with lineno {0,1} (i.e. predefined macro block), or all of them are
22137 in some included header file. */
22138 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22139 return 0;
22140 if (vec_safe_is_empty (files))
22141 {
22142 if (first->lineno > 1 || second->lineno > 1)
22143 return 0;
22144 }
22145 else if (first->lineno == 0)
22146 return 0;
22147
22148 /* Find the last define/undef entry that can be grouped together
22149 with first and at the same time compute md5 checksum of their
22150 codes, linenumbers and strings. */
22151 md5_init_ctx (&ctx);
22152 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22153 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22154 break;
22155 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22156 break;
22157 else
22158 {
22159 unsigned char code = cur->code;
22160 md5_process_bytes (&code, 1, &ctx);
22161 checksum_uleb128 (cur->lineno, &ctx);
22162 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22163 }
22164 md5_finish_ctx (&ctx, checksum);
22165 count = i - idx;
22166
22167 /* From the containing include filename (if any) pick up just
22168 usable characters from its basename. */
22169 if (vec_safe_is_empty (files))
22170 base = "";
22171 else
22172 base = lbasename (files->last ().info);
22173 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22174 if (ISIDNUM (base[i]) || base[i] == '.')
22175 encoded_filename_len++;
22176 /* Count . at the end. */
22177 if (encoded_filename_len)
22178 encoded_filename_len++;
22179
22180 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22181 linebuf_len = strlen (linebuf);
22182
22183 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22184 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22185 + 16 * 2 + 1);
22186 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22187 tail = grp_name + 4;
22188 if (encoded_filename_len)
22189 {
22190 for (i = 0; base[i]; i++)
22191 if (ISIDNUM (base[i]) || base[i] == '.')
22192 *tail++ = base[i];
22193 *tail++ = '.';
22194 }
22195 memcpy (tail, linebuf, linebuf_len);
22196 tail += linebuf_len;
22197 *tail++ = '.';
22198 for (i = 0; i < 16; i++)
22199 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22200
22201 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22202 in the empty vector entry before the first define/undef. */
22203 inc = &(*macinfo_table)[idx - 1];
22204 inc->code = DW_MACRO_GNU_transparent_include;
22205 inc->lineno = 0;
22206 inc->info = ggc_strdup (grp_name);
22207 if (!*macinfo_htab)
22208 *macinfo_htab = new macinfo_hash_type (10);
22209 /* Avoid emitting duplicates. */
22210 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22211 if (*slot != NULL)
22212 {
22213 inc->code = 0;
22214 inc->info = NULL;
22215 /* If such an entry has been used before, just emit
22216 a DW_MACRO_GNU_transparent_include op. */
22217 inc = *slot;
22218 output_macinfo_op (inc);
22219 /* And clear all macinfo_entry in the range to avoid emitting them
22220 in the second pass. */
22221 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22222 {
22223 cur->code = 0;
22224 cur->info = NULL;
22225 }
22226 }
22227 else
22228 {
22229 *slot = inc;
22230 inc->lineno = (*macinfo_htab)->elements ();
22231 output_macinfo_op (inc);
22232 }
22233 return count;
22234 }
22235
22236 /* Save any strings needed by the macinfo table in the debug str
22237 table. All strings must be collected into the table by the time
22238 index_string is called. */
22239
22240 static void
22241 save_macinfo_strings (void)
22242 {
22243 unsigned len;
22244 unsigned i;
22245 macinfo_entry *ref;
22246
22247 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22248 {
22249 switch (ref->code)
22250 {
22251 /* Match the logic in output_macinfo_op to decide on
22252 indirect strings. */
22253 case DW_MACINFO_define:
22254 case DW_MACINFO_undef:
22255 len = strlen (ref->info) + 1;
22256 if (!dwarf_strict
22257 && len > DWARF_OFFSET_SIZE
22258 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22259 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22260 set_indirect_string (find_AT_string (ref->info));
22261 break;
22262 case DW_MACRO_GNU_define_indirect:
22263 case DW_MACRO_GNU_undef_indirect:
22264 set_indirect_string (find_AT_string (ref->info));
22265 break;
22266 default:
22267 break;
22268 }
22269 }
22270 }
22271
22272 /* Output macinfo section(s). */
22273
22274 static void
22275 output_macinfo (void)
22276 {
22277 unsigned i;
22278 unsigned long length = vec_safe_length (macinfo_table);
22279 macinfo_entry *ref;
22280 vec<macinfo_entry, va_gc> *files = NULL;
22281 macinfo_hash_type *macinfo_htab = NULL;
22282
22283 if (! length)
22284 return;
22285
22286 /* output_macinfo* uses these interchangeably. */
22287 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22288 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22289 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22290 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22291
22292 /* For .debug_macro emit the section header. */
22293 if (!dwarf_strict)
22294 {
22295 dw2_asm_output_data (2, 4, "DWARF macro version number");
22296 if (DWARF_OFFSET_SIZE == 8)
22297 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22298 else
22299 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22300 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22301 (!dwarf_split_debug_info ? debug_line_section_label
22302 : debug_skeleton_line_section_label),
22303 debug_line_section, NULL);
22304 }
22305
22306 /* In the first loop, it emits the primary .debug_macinfo section
22307 and after each emitted op the macinfo_entry is cleared.
22308 If a longer range of define/undef ops can be optimized using
22309 DW_MACRO_GNU_transparent_include, the
22310 DW_MACRO_GNU_transparent_include op is emitted and kept in
22311 the vector before the first define/undef in the range and the
22312 whole range of define/undef ops is not emitted and kept. */
22313 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22314 {
22315 switch (ref->code)
22316 {
22317 case DW_MACINFO_start_file:
22318 vec_safe_push (files, *ref);
22319 break;
22320 case DW_MACINFO_end_file:
22321 if (!vec_safe_is_empty (files))
22322 files->pop ();
22323 break;
22324 case DW_MACINFO_define:
22325 case DW_MACINFO_undef:
22326 if (!dwarf_strict
22327 && HAVE_COMDAT_GROUP
22328 && vec_safe_length (files) != 1
22329 && i > 0
22330 && i + 1 < length
22331 && (*macinfo_table)[i - 1].code == 0)
22332 {
22333 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22334 if (count)
22335 {
22336 i += count - 1;
22337 continue;
22338 }
22339 }
22340 break;
22341 case 0:
22342 /* A dummy entry may be inserted at the beginning to be able
22343 to optimize the whole block of predefined macros. */
22344 if (i == 0)
22345 continue;
22346 default:
22347 break;
22348 }
22349 output_macinfo_op (ref);
22350 ref->info = NULL;
22351 ref->code = 0;
22352 }
22353
22354 if (!macinfo_htab)
22355 return;
22356
22357 delete macinfo_htab;
22358 macinfo_htab = NULL;
22359
22360 /* If any DW_MACRO_GNU_transparent_include were used, on those
22361 DW_MACRO_GNU_transparent_include entries terminate the
22362 current chain and switch to a new comdat .debug_macinfo
22363 section and emit the define/undef entries within it. */
22364 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22365 switch (ref->code)
22366 {
22367 case 0:
22368 continue;
22369 case DW_MACRO_GNU_transparent_include:
22370 {
22371 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22372 tree comdat_key = get_identifier (ref->info);
22373 /* Terminate the previous .debug_macinfo section. */
22374 dw2_asm_output_data (1, 0, "End compilation unit");
22375 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22376 SECTION_DEBUG
22377 | SECTION_LINKONCE,
22378 comdat_key);
22379 ASM_GENERATE_INTERNAL_LABEL (label,
22380 DEBUG_MACRO_SECTION_LABEL,
22381 ref->lineno);
22382 ASM_OUTPUT_LABEL (asm_out_file, label);
22383 ref->code = 0;
22384 ref->info = NULL;
22385 dw2_asm_output_data (2, 4, "DWARF macro version number");
22386 if (DWARF_OFFSET_SIZE == 8)
22387 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22388 else
22389 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22390 }
22391 break;
22392 case DW_MACINFO_define:
22393 case DW_MACINFO_undef:
22394 output_macinfo_op (ref);
22395 ref->code = 0;
22396 ref->info = NULL;
22397 break;
22398 default:
22399 gcc_unreachable ();
22400 }
22401 }
22402
22403 /* Set up for Dwarf output at the start of compilation. */
22404
22405 static void
22406 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22407 {
22408 /* Allocate the file_table. */
22409 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22410
22411 /* Allocate the decl_die_table. */
22412 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22413
22414 /* Allocate the decl_loc_table. */
22415 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22416
22417 /* Allocate the cached_dw_loc_list_table. */
22418 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22419
22420 /* Allocate the initial hunk of the decl_scope_table. */
22421 vec_alloc (decl_scope_table, 256);
22422
22423 /* Allocate the initial hunk of the abbrev_die_table. */
22424 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22425 (ABBREV_DIE_TABLE_INCREMENT);
22426 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22427 /* Zero-th entry is allocated, but unused. */
22428 abbrev_die_table_in_use = 1;
22429
22430 /* Allocate the pubtypes and pubnames vectors. */
22431 vec_alloc (pubname_table, 32);
22432 vec_alloc (pubtype_table, 32);
22433
22434 vec_alloc (incomplete_types, 64);
22435
22436 vec_alloc (used_rtx_array, 32);
22437
22438 if (!dwarf_split_debug_info)
22439 {
22440 debug_info_section = get_section (DEBUG_INFO_SECTION,
22441 SECTION_DEBUG, NULL);
22442 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22443 SECTION_DEBUG, NULL);
22444 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22445 SECTION_DEBUG, NULL);
22446 }
22447 else
22448 {
22449 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22450 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22451 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22452 SECTION_DEBUG | SECTION_EXCLUDE,
22453 NULL);
22454 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22455 SECTION_DEBUG, NULL);
22456 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22457 SECTION_DEBUG, NULL);
22458 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22459 SECTION_DEBUG, NULL);
22460 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22461 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22462
22463 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22464 the main .o, but the skeleton_line goes into the split off dwo. */
22465 debug_skeleton_line_section
22466 = get_section (DEBUG_DWO_LINE_SECTION,
22467 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22468 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22469 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22470 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22471 SECTION_DEBUG | SECTION_EXCLUDE,
22472 NULL);
22473 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22474 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22475 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22476 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22477 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22478 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22479 }
22480 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22481 SECTION_DEBUG, NULL);
22482 debug_macinfo_section = get_section (dwarf_strict
22483 ? DEBUG_MACINFO_SECTION
22484 : DEBUG_MACRO_SECTION,
22485 DEBUG_MACRO_SECTION_FLAGS, NULL);
22486 debug_line_section = get_section (DEBUG_LINE_SECTION,
22487 SECTION_DEBUG, NULL);
22488 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22489 SECTION_DEBUG, NULL);
22490 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22491 SECTION_DEBUG, NULL);
22492 debug_str_section = get_section (DEBUG_STR_SECTION,
22493 DEBUG_STR_SECTION_FLAGS, NULL);
22494 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22495 SECTION_DEBUG, NULL);
22496 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22497 SECTION_DEBUG, NULL);
22498
22499 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22500 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22501 DEBUG_ABBREV_SECTION_LABEL, 0);
22502 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22503 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22504 COLD_TEXT_SECTION_LABEL, 0);
22505 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22506
22507 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22508 DEBUG_INFO_SECTION_LABEL, 0);
22509 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22510 DEBUG_LINE_SECTION_LABEL, 0);
22511 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22512 DEBUG_RANGES_SECTION_LABEL, 0);
22513 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22514 DEBUG_ADDR_SECTION_LABEL, 0);
22515 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22516 dwarf_strict
22517 ? DEBUG_MACINFO_SECTION_LABEL
22518 : DEBUG_MACRO_SECTION_LABEL, 0);
22519 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22520
22521 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22522 vec_alloc (macinfo_table, 64);
22523
22524 switch_to_section (text_section);
22525 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22526
22527 /* Make sure the line number table for .text always exists. */
22528 text_section_line_info = new_line_info_table ();
22529 text_section_line_info->end_label = text_end_label;
22530 }
22531
22532 /* Called before compile () starts outputtting functions, variables
22533 and toplevel asms into assembly. */
22534
22535 static void
22536 dwarf2out_assembly_start (void)
22537 {
22538 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22539 && dwarf2out_do_cfi_asm ()
22540 && (!(flag_unwind_tables || flag_exceptions)
22541 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22542 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22543 }
22544
22545 /* A helper function for dwarf2out_finish called through
22546 htab_traverse. Assign a string its index. All strings must be
22547 collected into the table by the time index_string is called,
22548 because the indexing code relies on htab_traverse to traverse nodes
22549 in the same order for each run. */
22550
22551 int
22552 index_string (indirect_string_node **h, unsigned int *index)
22553 {
22554 indirect_string_node *node = *h;
22555
22556 find_string_form (node);
22557 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22558 {
22559 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22560 node->index = *index;
22561 *index += 1;
22562 }
22563 return 1;
22564 }
22565
22566 /* A helper function for output_indirect_strings called through
22567 htab_traverse. Output the offset to a string and update the
22568 current offset. */
22569
22570 int
22571 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22572 {
22573 indirect_string_node *node = *h;
22574
22575 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22576 {
22577 /* Assert that this node has been assigned an index. */
22578 gcc_assert (node->index != NO_INDEX_ASSIGNED
22579 && node->index != NOT_INDEXED);
22580 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22581 "indexed string 0x%x: %s", node->index, node->str);
22582 *offset += strlen (node->str) + 1;
22583 }
22584 return 1;
22585 }
22586
22587 /* A helper function for dwarf2out_finish called through
22588 htab_traverse. Output the indexed string. */
22589
22590 int
22591 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22592 {
22593 struct indirect_string_node *node = *h;
22594
22595 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22596 {
22597 /* Assert that the strings are output in the same order as their
22598 indexes were assigned. */
22599 gcc_assert (*cur_idx == node->index);
22600 assemble_string (node->str, strlen (node->str) + 1);
22601 *cur_idx += 1;
22602 }
22603 return 1;
22604 }
22605
22606 /* A helper function for dwarf2out_finish called through
22607 htab_traverse. Emit one queued .debug_str string. */
22608
22609 int
22610 output_indirect_string (indirect_string_node **h, void *)
22611 {
22612 struct indirect_string_node *node = *h;
22613
22614 node->form = find_string_form (node);
22615 if (node->form == DW_FORM_strp && node->refcount > 0)
22616 {
22617 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22618 assemble_string (node->str, strlen (node->str) + 1);
22619 }
22620
22621 return 1;
22622 }
22623
22624 /* Output the indexed string table. */
22625
22626 static void
22627 output_indirect_strings (void)
22628 {
22629 switch_to_section (debug_str_section);
22630 if (!dwarf_split_debug_info)
22631 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22632 else
22633 {
22634 unsigned int offset = 0;
22635 unsigned int cur_idx = 0;
22636
22637 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22638
22639 switch_to_section (debug_str_offsets_section);
22640 debug_str_hash->traverse_noresize
22641 <unsigned int *, output_index_string_offset> (&offset);
22642 switch_to_section (debug_str_dwo_section);
22643 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22644 (&cur_idx);
22645 }
22646 }
22647
22648 /* Callback for htab_traverse to assign an index to an entry in the
22649 table, and to write that entry to the .debug_addr section. */
22650
22651 int
22652 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22653 {
22654 addr_table_entry *entry = *slot;
22655
22656 if (entry->refcount == 0)
22657 {
22658 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22659 || entry->index == NOT_INDEXED);
22660 return 1;
22661 }
22662
22663 gcc_assert (entry->index == *cur_index);
22664 (*cur_index)++;
22665
22666 switch (entry->kind)
22667 {
22668 case ate_kind_rtx:
22669 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22670 "0x%x", entry->index);
22671 break;
22672 case ate_kind_rtx_dtprel:
22673 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22674 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22675 DWARF2_ADDR_SIZE,
22676 entry->addr.rtl);
22677 fputc ('\n', asm_out_file);
22678 break;
22679 case ate_kind_label:
22680 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22681 "0x%x", entry->index);
22682 break;
22683 default:
22684 gcc_unreachable ();
22685 }
22686 return 1;
22687 }
22688
22689 /* Produce the .debug_addr section. */
22690
22691 static void
22692 output_addr_table (void)
22693 {
22694 unsigned int index = 0;
22695 if (addr_index_table == NULL || addr_index_table->size () == 0)
22696 return;
22697
22698 switch_to_section (debug_addr_section);
22699 addr_index_table
22700 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22701 }
22702
22703 #if ENABLE_ASSERT_CHECKING
22704 /* Verify that all marks are clear. */
22705
22706 static void
22707 verify_marks_clear (dw_die_ref die)
22708 {
22709 dw_die_ref c;
22710
22711 gcc_assert (! die->die_mark);
22712 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22713 }
22714 #endif /* ENABLE_ASSERT_CHECKING */
22715
22716 /* Clear the marks for a die and its children.
22717 Be cool if the mark isn't set. */
22718
22719 static void
22720 prune_unmark_dies (dw_die_ref die)
22721 {
22722 dw_die_ref c;
22723
22724 if (die->die_mark)
22725 die->die_mark = 0;
22726 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22727 }
22728
22729 /* Given DIE that we're marking as used, find any other dies
22730 it references as attributes and mark them as used. */
22731
22732 static void
22733 prune_unused_types_walk_attribs (dw_die_ref die)
22734 {
22735 dw_attr_ref a;
22736 unsigned ix;
22737
22738 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22739 {
22740 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22741 {
22742 /* A reference to another DIE.
22743 Make sure that it will get emitted.
22744 If it was broken out into a comdat group, don't follow it. */
22745 if (! AT_ref (a)->comdat_type_p
22746 || a->dw_attr == DW_AT_specification)
22747 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22748 }
22749 /* Set the string's refcount to 0 so that prune_unused_types_mark
22750 accounts properly for it. */
22751 if (AT_class (a) == dw_val_class_str)
22752 a->dw_attr_val.v.val_str->refcount = 0;
22753 }
22754 }
22755
22756 /* Mark the generic parameters and arguments children DIEs of DIE. */
22757
22758 static void
22759 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22760 {
22761 dw_die_ref c;
22762
22763 if (die == NULL || die->die_child == NULL)
22764 return;
22765 c = die->die_child;
22766 do
22767 {
22768 if (is_template_parameter (c))
22769 prune_unused_types_mark (c, 1);
22770 c = c->die_sib;
22771 } while (c && c != die->die_child);
22772 }
22773
22774 /* Mark DIE as being used. If DOKIDS is true, then walk down
22775 to DIE's children. */
22776
22777 static void
22778 prune_unused_types_mark (dw_die_ref die, int dokids)
22779 {
22780 dw_die_ref c;
22781
22782 if (die->die_mark == 0)
22783 {
22784 /* We haven't done this node yet. Mark it as used. */
22785 die->die_mark = 1;
22786 /* If this is the DIE of a generic type instantiation,
22787 mark the children DIEs that describe its generic parms and
22788 args. */
22789 prune_unused_types_mark_generic_parms_dies (die);
22790
22791 /* We also have to mark its parents as used.
22792 (But we don't want to mark our parent's kids due to this,
22793 unless it is a class.) */
22794 if (die->die_parent)
22795 prune_unused_types_mark (die->die_parent,
22796 class_scope_p (die->die_parent));
22797
22798 /* Mark any referenced nodes. */
22799 prune_unused_types_walk_attribs (die);
22800
22801 /* If this node is a specification,
22802 also mark the definition, if it exists. */
22803 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22804 prune_unused_types_mark (die->die_definition, 1);
22805 }
22806
22807 if (dokids && die->die_mark != 2)
22808 {
22809 /* We need to walk the children, but haven't done so yet.
22810 Remember that we've walked the kids. */
22811 die->die_mark = 2;
22812
22813 /* If this is an array type, we need to make sure our
22814 kids get marked, even if they're types. If we're
22815 breaking out types into comdat sections, do this
22816 for all type definitions. */
22817 if (die->die_tag == DW_TAG_array_type
22818 || (use_debug_types
22819 && is_type_die (die) && ! is_declaration_die (die)))
22820 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22821 else
22822 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22823 }
22824 }
22825
22826 /* For local classes, look if any static member functions were emitted
22827 and if so, mark them. */
22828
22829 static void
22830 prune_unused_types_walk_local_classes (dw_die_ref die)
22831 {
22832 dw_die_ref c;
22833
22834 if (die->die_mark == 2)
22835 return;
22836
22837 switch (die->die_tag)
22838 {
22839 case DW_TAG_structure_type:
22840 case DW_TAG_union_type:
22841 case DW_TAG_class_type:
22842 break;
22843
22844 case DW_TAG_subprogram:
22845 if (!get_AT_flag (die, DW_AT_declaration)
22846 || die->die_definition != NULL)
22847 prune_unused_types_mark (die, 1);
22848 return;
22849
22850 default:
22851 return;
22852 }
22853
22854 /* Mark children. */
22855 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22856 }
22857
22858 /* Walk the tree DIE and mark types that we actually use. */
22859
22860 static void
22861 prune_unused_types_walk (dw_die_ref die)
22862 {
22863 dw_die_ref c;
22864
22865 /* Don't do anything if this node is already marked and
22866 children have been marked as well. */
22867 if (die->die_mark == 2)
22868 return;
22869
22870 switch (die->die_tag)
22871 {
22872 case DW_TAG_structure_type:
22873 case DW_TAG_union_type:
22874 case DW_TAG_class_type:
22875 if (die->die_perennial_p)
22876 break;
22877
22878 for (c = die->die_parent; c; c = c->die_parent)
22879 if (c->die_tag == DW_TAG_subprogram)
22880 break;
22881
22882 /* Finding used static member functions inside of classes
22883 is needed just for local classes, because for other classes
22884 static member function DIEs with DW_AT_specification
22885 are emitted outside of the DW_TAG_*_type. If we ever change
22886 it, we'd need to call this even for non-local classes. */
22887 if (c)
22888 prune_unused_types_walk_local_classes (die);
22889
22890 /* It's a type node --- don't mark it. */
22891 return;
22892
22893 case DW_TAG_const_type:
22894 case DW_TAG_packed_type:
22895 case DW_TAG_pointer_type:
22896 case DW_TAG_reference_type:
22897 case DW_TAG_rvalue_reference_type:
22898 case DW_TAG_volatile_type:
22899 case DW_TAG_typedef:
22900 case DW_TAG_array_type:
22901 case DW_TAG_interface_type:
22902 case DW_TAG_friend:
22903 case DW_TAG_variant_part:
22904 case DW_TAG_enumeration_type:
22905 case DW_TAG_subroutine_type:
22906 case DW_TAG_string_type:
22907 case DW_TAG_set_type:
22908 case DW_TAG_subrange_type:
22909 case DW_TAG_ptr_to_member_type:
22910 case DW_TAG_file_type:
22911 if (die->die_perennial_p)
22912 break;
22913
22914 /* It's a type node --- don't mark it. */
22915 return;
22916
22917 default:
22918 /* Mark everything else. */
22919 break;
22920 }
22921
22922 if (die->die_mark == 0)
22923 {
22924 die->die_mark = 1;
22925
22926 /* Now, mark any dies referenced from here. */
22927 prune_unused_types_walk_attribs (die);
22928 }
22929
22930 die->die_mark = 2;
22931
22932 /* Mark children. */
22933 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22934 }
22935
22936 /* Increment the string counts on strings referred to from DIE's
22937 attributes. */
22938
22939 static void
22940 prune_unused_types_update_strings (dw_die_ref die)
22941 {
22942 dw_attr_ref a;
22943 unsigned ix;
22944
22945 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22946 if (AT_class (a) == dw_val_class_str)
22947 {
22948 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22949 s->refcount++;
22950 /* Avoid unnecessarily putting strings that are used less than
22951 twice in the hash table. */
22952 if (s->refcount
22953 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22954 {
22955 indirect_string_node **slot
22956 = debug_str_hash->find_slot_with_hash (s->str,
22957 htab_hash_string (s->str),
22958 INSERT);
22959 gcc_assert (*slot == NULL);
22960 *slot = s;
22961 }
22962 }
22963 }
22964
22965 /* Remove from the tree DIE any dies that aren't marked. */
22966
22967 static void
22968 prune_unused_types_prune (dw_die_ref die)
22969 {
22970 dw_die_ref c;
22971
22972 gcc_assert (die->die_mark);
22973 prune_unused_types_update_strings (die);
22974
22975 if (! die->die_child)
22976 return;
22977
22978 c = die->die_child;
22979 do {
22980 dw_die_ref prev = c;
22981 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22982 if (c == die->die_child)
22983 {
22984 /* No marked children between 'prev' and the end of the list. */
22985 if (prev == c)
22986 /* No marked children at all. */
22987 die->die_child = NULL;
22988 else
22989 {
22990 prev->die_sib = c->die_sib;
22991 die->die_child = prev;
22992 }
22993 return;
22994 }
22995
22996 if (c != prev->die_sib)
22997 prev->die_sib = c;
22998 prune_unused_types_prune (c);
22999 } while (c != die->die_child);
23000 }
23001
23002 /* Remove dies representing declarations that we never use. */
23003
23004 static void
23005 prune_unused_types (void)
23006 {
23007 unsigned int i;
23008 limbo_die_node *node;
23009 comdat_type_node *ctnode;
23010 pubname_ref pub;
23011 dw_die_ref base_type;
23012
23013 #if ENABLE_ASSERT_CHECKING
23014 /* All the marks should already be clear. */
23015 verify_marks_clear (comp_unit_die ());
23016 for (node = limbo_die_list; node; node = node->next)
23017 verify_marks_clear (node->die);
23018 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23019 verify_marks_clear (ctnode->root_die);
23020 #endif /* ENABLE_ASSERT_CHECKING */
23021
23022 /* Mark types that are used in global variables. */
23023 premark_types_used_by_global_vars ();
23024
23025 /* Set the mark on nodes that are actually used. */
23026 prune_unused_types_walk (comp_unit_die ());
23027 for (node = limbo_die_list; node; node = node->next)
23028 prune_unused_types_walk (node->die);
23029 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23030 {
23031 prune_unused_types_walk (ctnode->root_die);
23032 prune_unused_types_mark (ctnode->type_die, 1);
23033 }
23034
23035 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23036 are unusual in that they are pubnames that are the children of pubtypes.
23037 They should only be marked via their parent DW_TAG_enumeration_type die,
23038 not as roots in themselves. */
23039 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23040 if (pub->die->die_tag != DW_TAG_enumerator)
23041 prune_unused_types_mark (pub->die, 1);
23042 for (i = 0; base_types.iterate (i, &base_type); i++)
23043 prune_unused_types_mark (base_type, 1);
23044
23045 if (debug_str_hash)
23046 debug_str_hash->empty ();
23047 if (skeleton_debug_str_hash)
23048 skeleton_debug_str_hash->empty ();
23049 prune_unused_types_prune (comp_unit_die ());
23050 for (node = limbo_die_list; node; node = node->next)
23051 prune_unused_types_prune (node->die);
23052 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23053 prune_unused_types_prune (ctnode->root_die);
23054
23055 /* Leave the marks clear. */
23056 prune_unmark_dies (comp_unit_die ());
23057 for (node = limbo_die_list; node; node = node->next)
23058 prune_unmark_dies (node->die);
23059 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23060 prune_unmark_dies (ctnode->root_die);
23061 }
23062
23063 /* Set the parameter to true if there are any relative pathnames in
23064 the file table. */
23065 int
23066 file_table_relative_p (dwarf_file_data **slot, bool *p)
23067 {
23068 struct dwarf_file_data *d = *slot;
23069 if (!IS_ABSOLUTE_PATH (d->filename))
23070 {
23071 *p = true;
23072 return 0;
23073 }
23074 return 1;
23075 }
23076
23077 /* Helpers to manipulate hash table of comdat type units. */
23078
23079 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23080 {
23081 typedef comdat_type_node value_type;
23082 typedef comdat_type_node compare_type;
23083 static inline hashval_t hash (const value_type *);
23084 static inline bool equal (const value_type *, const compare_type *);
23085 };
23086
23087 inline hashval_t
23088 comdat_type_hasher::hash (const value_type *type_node)
23089 {
23090 hashval_t h;
23091 memcpy (&h, type_node->signature, sizeof (h));
23092 return h;
23093 }
23094
23095 inline bool
23096 comdat_type_hasher::equal (const value_type *type_node_1,
23097 const compare_type *type_node_2)
23098 {
23099 return (! memcmp (type_node_1->signature, type_node_2->signature,
23100 DWARF_TYPE_SIGNATURE_SIZE));
23101 }
23102
23103 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23104 to the location it would have been added, should we know its
23105 DECL_ASSEMBLER_NAME when we added other attributes. This will
23106 probably improve compactness of debug info, removing equivalent
23107 abbrevs, and hide any differences caused by deferring the
23108 computation of the assembler name, triggered by e.g. PCH. */
23109
23110 static inline void
23111 move_linkage_attr (dw_die_ref die)
23112 {
23113 unsigned ix = vec_safe_length (die->die_attr);
23114 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23115
23116 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23117 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23118
23119 while (--ix > 0)
23120 {
23121 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23122
23123 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23124 break;
23125 }
23126
23127 if (ix != vec_safe_length (die->die_attr) - 1)
23128 {
23129 die->die_attr->pop ();
23130 die->die_attr->quick_insert (ix, linkage);
23131 }
23132 }
23133
23134 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23135 referenced from typed stack ops and count how often they are used. */
23136
23137 static void
23138 mark_base_types (dw_loc_descr_ref loc)
23139 {
23140 dw_die_ref base_type = NULL;
23141
23142 for (; loc; loc = loc->dw_loc_next)
23143 {
23144 switch (loc->dw_loc_opc)
23145 {
23146 case DW_OP_GNU_regval_type:
23147 case DW_OP_GNU_deref_type:
23148 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23149 break;
23150 case DW_OP_GNU_convert:
23151 case DW_OP_GNU_reinterpret:
23152 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23153 continue;
23154 /* FALLTHRU */
23155 case DW_OP_GNU_const_type:
23156 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23157 break;
23158 case DW_OP_GNU_entry_value:
23159 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23160 continue;
23161 default:
23162 continue;
23163 }
23164 gcc_assert (base_type->die_parent == comp_unit_die ());
23165 if (base_type->die_mark)
23166 base_type->die_mark++;
23167 else
23168 {
23169 base_types.safe_push (base_type);
23170 base_type->die_mark = 1;
23171 }
23172 }
23173 }
23174
23175 /* Comparison function for sorting marked base types. */
23176
23177 static int
23178 base_type_cmp (const void *x, const void *y)
23179 {
23180 dw_die_ref dx = *(const dw_die_ref *) x;
23181 dw_die_ref dy = *(const dw_die_ref *) y;
23182 unsigned int byte_size1, byte_size2;
23183 unsigned int encoding1, encoding2;
23184 if (dx->die_mark > dy->die_mark)
23185 return -1;
23186 if (dx->die_mark < dy->die_mark)
23187 return 1;
23188 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23189 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23190 if (byte_size1 < byte_size2)
23191 return 1;
23192 if (byte_size1 > byte_size2)
23193 return -1;
23194 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23195 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23196 if (encoding1 < encoding2)
23197 return 1;
23198 if (encoding1 > encoding2)
23199 return -1;
23200 return 0;
23201 }
23202
23203 /* Move base types marked by mark_base_types as early as possible
23204 in the CU, sorted by decreasing usage count both to make the
23205 uleb128 references as small as possible and to make sure they
23206 will have die_offset already computed by calc_die_sizes when
23207 sizes of typed stack loc ops is computed. */
23208
23209 static void
23210 move_marked_base_types (void)
23211 {
23212 unsigned int i;
23213 dw_die_ref base_type, die, c;
23214
23215 if (base_types.is_empty ())
23216 return;
23217
23218 /* Sort by decreasing usage count, they will be added again in that
23219 order later on. */
23220 base_types.qsort (base_type_cmp);
23221 die = comp_unit_die ();
23222 c = die->die_child;
23223 do
23224 {
23225 dw_die_ref prev = c;
23226 c = c->die_sib;
23227 while (c->die_mark)
23228 {
23229 remove_child_with_prev (c, prev);
23230 /* As base types got marked, there must be at least
23231 one node other than DW_TAG_base_type. */
23232 gcc_assert (c != c->die_sib);
23233 c = c->die_sib;
23234 }
23235 }
23236 while (c != die->die_child);
23237 gcc_assert (die->die_child);
23238 c = die->die_child;
23239 for (i = 0; base_types.iterate (i, &base_type); i++)
23240 {
23241 base_type->die_mark = 0;
23242 base_type->die_sib = c->die_sib;
23243 c->die_sib = base_type;
23244 c = base_type;
23245 }
23246 }
23247
23248 /* Helper function for resolve_addr, attempt to resolve
23249 one CONST_STRING, return true if successful. Similarly verify that
23250 SYMBOL_REFs refer to variables emitted in the current CU. */
23251
23252 static bool
23253 resolve_one_addr (rtx *addr)
23254 {
23255 rtx rtl = *addr;
23256
23257 if (GET_CODE (rtl) == CONST_STRING)
23258 {
23259 size_t len = strlen (XSTR (rtl, 0)) + 1;
23260 tree t = build_string (len, XSTR (rtl, 0));
23261 tree tlen = size_int (len - 1);
23262 TREE_TYPE (t)
23263 = build_array_type (char_type_node, build_index_type (tlen));
23264 rtl = lookup_constant_def (t);
23265 if (!rtl || !MEM_P (rtl))
23266 return false;
23267 rtl = XEXP (rtl, 0);
23268 if (GET_CODE (rtl) == SYMBOL_REF
23269 && SYMBOL_REF_DECL (rtl)
23270 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23271 return false;
23272 vec_safe_push (used_rtx_array, rtl);
23273 *addr = rtl;
23274 return true;
23275 }
23276
23277 if (GET_CODE (rtl) == SYMBOL_REF
23278 && SYMBOL_REF_DECL (rtl))
23279 {
23280 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23281 {
23282 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23283 return false;
23284 }
23285 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23286 return false;
23287 }
23288
23289 if (GET_CODE (rtl) == CONST)
23290 {
23291 subrtx_ptr_iterator::array_type array;
23292 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23293 if (!resolve_one_addr (*iter))
23294 return false;
23295 }
23296
23297 return true;
23298 }
23299
23300 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23301 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23302 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23303
23304 static rtx
23305 string_cst_pool_decl (tree t)
23306 {
23307 rtx rtl = output_constant_def (t, 1);
23308 unsigned char *array;
23309 dw_loc_descr_ref l;
23310 tree decl;
23311 size_t len;
23312 dw_die_ref ref;
23313
23314 if (!rtl || !MEM_P (rtl))
23315 return NULL_RTX;
23316 rtl = XEXP (rtl, 0);
23317 if (GET_CODE (rtl) != SYMBOL_REF
23318 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23319 return NULL_RTX;
23320
23321 decl = SYMBOL_REF_DECL (rtl);
23322 if (!lookup_decl_die (decl))
23323 {
23324 len = TREE_STRING_LENGTH (t);
23325 vec_safe_push (used_rtx_array, rtl);
23326 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23327 array = ggc_vec_alloc<unsigned char> (len);
23328 memcpy (array, TREE_STRING_POINTER (t), len);
23329 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23330 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23331 l->dw_loc_oprnd2.v.val_vec.length = len;
23332 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23333 l->dw_loc_oprnd2.v.val_vec.array = array;
23334 add_AT_loc (ref, DW_AT_location, l);
23335 equate_decl_number_to_die (decl, ref);
23336 }
23337 return rtl;
23338 }
23339
23340 /* Helper function of resolve_addr_in_expr. LOC is
23341 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23342 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23343 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23344 with DW_OP_GNU_implicit_pointer if possible
23345 and return true, if unsuccessful, return false. */
23346
23347 static bool
23348 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23349 {
23350 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23351 HOST_WIDE_INT offset = 0;
23352 dw_die_ref ref = NULL;
23353 tree decl;
23354
23355 if (GET_CODE (rtl) == CONST
23356 && GET_CODE (XEXP (rtl, 0)) == PLUS
23357 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23358 {
23359 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23360 rtl = XEXP (XEXP (rtl, 0), 0);
23361 }
23362 if (GET_CODE (rtl) == CONST_STRING)
23363 {
23364 size_t len = strlen (XSTR (rtl, 0)) + 1;
23365 tree t = build_string (len, XSTR (rtl, 0));
23366 tree tlen = size_int (len - 1);
23367
23368 TREE_TYPE (t)
23369 = build_array_type (char_type_node, build_index_type (tlen));
23370 rtl = string_cst_pool_decl (t);
23371 if (!rtl)
23372 return false;
23373 }
23374 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23375 {
23376 decl = SYMBOL_REF_DECL (rtl);
23377 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23378 {
23379 ref = lookup_decl_die (decl);
23380 if (ref && (get_AT (ref, DW_AT_location)
23381 || get_AT (ref, DW_AT_const_value)))
23382 {
23383 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23384 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23385 loc->dw_loc_oprnd1.val_entry = NULL;
23386 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23387 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23388 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23389 loc->dw_loc_oprnd2.v.val_int = offset;
23390 return true;
23391 }
23392 }
23393 }
23394 return false;
23395 }
23396
23397 /* Helper function for resolve_addr, handle one location
23398 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23399 the location list couldn't be resolved. */
23400
23401 static bool
23402 resolve_addr_in_expr (dw_loc_descr_ref loc)
23403 {
23404 dw_loc_descr_ref keep = NULL;
23405 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23406 switch (loc->dw_loc_opc)
23407 {
23408 case DW_OP_addr:
23409 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23410 {
23411 if ((prev == NULL
23412 || prev->dw_loc_opc == DW_OP_piece
23413 || prev->dw_loc_opc == DW_OP_bit_piece)
23414 && loc->dw_loc_next
23415 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23416 && !dwarf_strict
23417 && optimize_one_addr_into_implicit_ptr (loc))
23418 break;
23419 return false;
23420 }
23421 break;
23422 case DW_OP_GNU_addr_index:
23423 case DW_OP_GNU_const_index:
23424 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23425 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23426 {
23427 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23428 if (!resolve_one_addr (&rtl))
23429 return false;
23430 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23431 loc->dw_loc_oprnd1.val_entry =
23432 add_addr_table_entry (rtl, ate_kind_rtx);
23433 }
23434 break;
23435 case DW_OP_const4u:
23436 case DW_OP_const8u:
23437 if (loc->dtprel
23438 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23439 return false;
23440 break;
23441 case DW_OP_plus_uconst:
23442 if (size_of_loc_descr (loc)
23443 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23444 + 1
23445 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23446 {
23447 dw_loc_descr_ref repl
23448 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23449 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23450 add_loc_descr (&repl, loc->dw_loc_next);
23451 *loc = *repl;
23452 }
23453 break;
23454 case DW_OP_implicit_value:
23455 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23456 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23457 return false;
23458 break;
23459 case DW_OP_GNU_implicit_pointer:
23460 case DW_OP_GNU_parameter_ref:
23461 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23462 {
23463 dw_die_ref ref
23464 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23465 if (ref == NULL)
23466 return false;
23467 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23468 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23469 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23470 }
23471 break;
23472 case DW_OP_GNU_const_type:
23473 case DW_OP_GNU_regval_type:
23474 case DW_OP_GNU_deref_type:
23475 case DW_OP_GNU_convert:
23476 case DW_OP_GNU_reinterpret:
23477 while (loc->dw_loc_next
23478 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23479 {
23480 dw_die_ref base1, base2;
23481 unsigned enc1, enc2, size1, size2;
23482 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23483 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23484 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23485 else if (loc->dw_loc_oprnd1.val_class
23486 == dw_val_class_unsigned_const)
23487 break;
23488 else
23489 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23490 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23491 == dw_val_class_unsigned_const)
23492 break;
23493 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23494 gcc_assert (base1->die_tag == DW_TAG_base_type
23495 && base2->die_tag == DW_TAG_base_type);
23496 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23497 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23498 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23499 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23500 if (size1 == size2
23501 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23502 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23503 && loc != keep)
23504 || enc1 == enc2))
23505 {
23506 /* Optimize away next DW_OP_GNU_convert after
23507 adjusting LOC's base type die reference. */
23508 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23509 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23510 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23511 else
23512 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23513 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23514 continue;
23515 }
23516 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23517 point typed stack entry. */
23518 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23519 keep = loc->dw_loc_next;
23520 break;
23521 }
23522 break;
23523 default:
23524 break;
23525 }
23526 return true;
23527 }
23528
23529 /* Helper function of resolve_addr. DIE had DW_AT_location of
23530 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23531 and DW_OP_addr couldn't be resolved. resolve_addr has already
23532 removed the DW_AT_location attribute. This function attempts to
23533 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23534 to it or DW_AT_const_value attribute, if possible. */
23535
23536 static void
23537 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23538 {
23539 if (TREE_CODE (decl) != VAR_DECL
23540 || lookup_decl_die (decl) != die
23541 || DECL_EXTERNAL (decl)
23542 || !TREE_STATIC (decl)
23543 || DECL_INITIAL (decl) == NULL_TREE
23544 || DECL_P (DECL_INITIAL (decl))
23545 || get_AT (die, DW_AT_const_value))
23546 return;
23547
23548 tree init = DECL_INITIAL (decl);
23549 HOST_WIDE_INT offset = 0;
23550 /* For variables that have been optimized away and thus
23551 don't have a memory location, see if we can emit
23552 DW_AT_const_value instead. */
23553 if (tree_add_const_value_attribute (die, init))
23554 return;
23555 if (dwarf_strict)
23556 return;
23557 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23558 and ADDR_EXPR refers to a decl that has DW_AT_location or
23559 DW_AT_const_value (but isn't addressable, otherwise
23560 resolving the original DW_OP_addr wouldn't fail), see if
23561 we can add DW_OP_GNU_implicit_pointer. */
23562 STRIP_NOPS (init);
23563 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23564 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23565 {
23566 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23567 init = TREE_OPERAND (init, 0);
23568 STRIP_NOPS (init);
23569 }
23570 if (TREE_CODE (init) != ADDR_EXPR)
23571 return;
23572 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23573 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23574 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23575 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23576 && TREE_OPERAND (init, 0) != decl))
23577 {
23578 dw_die_ref ref;
23579 dw_loc_descr_ref l;
23580
23581 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23582 {
23583 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23584 if (!rtl)
23585 return;
23586 decl = SYMBOL_REF_DECL (rtl);
23587 }
23588 else
23589 decl = TREE_OPERAND (init, 0);
23590 ref = lookup_decl_die (decl);
23591 if (ref == NULL
23592 || (!get_AT (ref, DW_AT_location)
23593 && !get_AT (ref, DW_AT_const_value)))
23594 return;
23595 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23596 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23597 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23598 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23599 add_AT_loc (die, DW_AT_location, l);
23600 }
23601 }
23602
23603 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23604 an address in .rodata section if the string literal is emitted there,
23605 or remove the containing location list or replace DW_AT_const_value
23606 with DW_AT_location and empty location expression, if it isn't found
23607 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23608 to something that has been emitted in the current CU. */
23609
23610 static void
23611 resolve_addr (dw_die_ref die)
23612 {
23613 dw_die_ref c;
23614 dw_attr_ref a;
23615 dw_loc_list_ref *curr, *start, loc;
23616 unsigned ix;
23617
23618 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23619 switch (AT_class (a))
23620 {
23621 case dw_val_class_loc_list:
23622 start = curr = AT_loc_list_ptr (a);
23623 loc = *curr;
23624 gcc_assert (loc);
23625 /* The same list can be referenced more than once. See if we have
23626 already recorded the result from a previous pass. */
23627 if (loc->replaced)
23628 *curr = loc->dw_loc_next;
23629 else if (!loc->resolved_addr)
23630 {
23631 /* As things stand, we do not expect or allow one die to
23632 reference a suffix of another die's location list chain.
23633 References must be identical or completely separate.
23634 There is therefore no need to cache the result of this
23635 pass on any list other than the first; doing so
23636 would lead to unnecessary writes. */
23637 while (*curr)
23638 {
23639 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23640 if (!resolve_addr_in_expr ((*curr)->expr))
23641 {
23642 dw_loc_list_ref next = (*curr)->dw_loc_next;
23643 dw_loc_descr_ref l = (*curr)->expr;
23644
23645 if (next && (*curr)->ll_symbol)
23646 {
23647 gcc_assert (!next->ll_symbol);
23648 next->ll_symbol = (*curr)->ll_symbol;
23649 }
23650 if (dwarf_split_debug_info)
23651 remove_loc_list_addr_table_entries (l);
23652 *curr = next;
23653 }
23654 else
23655 {
23656 mark_base_types ((*curr)->expr);
23657 curr = &(*curr)->dw_loc_next;
23658 }
23659 }
23660 if (loc == *start)
23661 loc->resolved_addr = 1;
23662 else
23663 {
23664 loc->replaced = 1;
23665 loc->dw_loc_next = *start;
23666 }
23667 }
23668 if (!*start)
23669 {
23670 remove_AT (die, a->dw_attr);
23671 ix--;
23672 }
23673 break;
23674 case dw_val_class_loc:
23675 {
23676 dw_loc_descr_ref l = AT_loc (a);
23677 /* For -gdwarf-2 don't attempt to optimize
23678 DW_AT_data_member_location containing
23679 DW_OP_plus_uconst - older consumers might
23680 rely on it being that op instead of a more complex,
23681 but shorter, location description. */
23682 if ((dwarf_version > 2
23683 || a->dw_attr != DW_AT_data_member_location
23684 || l == NULL
23685 || l->dw_loc_opc != DW_OP_plus_uconst
23686 || l->dw_loc_next != NULL)
23687 && !resolve_addr_in_expr (l))
23688 {
23689 if (dwarf_split_debug_info)
23690 remove_loc_list_addr_table_entries (l);
23691 if (l != NULL
23692 && l->dw_loc_next == NULL
23693 && l->dw_loc_opc == DW_OP_addr
23694 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23695 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23696 && a->dw_attr == DW_AT_location)
23697 {
23698 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23699 remove_AT (die, a->dw_attr);
23700 ix--;
23701 optimize_location_into_implicit_ptr (die, decl);
23702 break;
23703 }
23704 remove_AT (die, a->dw_attr);
23705 ix--;
23706 }
23707 else
23708 mark_base_types (l);
23709 }
23710 break;
23711 case dw_val_class_addr:
23712 if (a->dw_attr == DW_AT_const_value
23713 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23714 {
23715 if (AT_index (a) != NOT_INDEXED)
23716 remove_addr_table_entry (a->dw_attr_val.val_entry);
23717 remove_AT (die, a->dw_attr);
23718 ix--;
23719 }
23720 if (die->die_tag == DW_TAG_GNU_call_site
23721 && a->dw_attr == DW_AT_abstract_origin)
23722 {
23723 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23724 dw_die_ref tdie = lookup_decl_die (tdecl);
23725 if (tdie == NULL
23726 && DECL_EXTERNAL (tdecl)
23727 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23728 {
23729 force_decl_die (tdecl);
23730 tdie = lookup_decl_die (tdecl);
23731 }
23732 if (tdie)
23733 {
23734 a->dw_attr_val.val_class = dw_val_class_die_ref;
23735 a->dw_attr_val.v.val_die_ref.die = tdie;
23736 a->dw_attr_val.v.val_die_ref.external = 0;
23737 }
23738 else
23739 {
23740 if (AT_index (a) != NOT_INDEXED)
23741 remove_addr_table_entry (a->dw_attr_val.val_entry);
23742 remove_AT (die, a->dw_attr);
23743 ix--;
23744 }
23745 }
23746 break;
23747 default:
23748 break;
23749 }
23750
23751 FOR_EACH_CHILD (die, c, resolve_addr (c));
23752 }
23753 \f
23754 /* Helper routines for optimize_location_lists.
23755 This pass tries to share identical local lists in .debug_loc
23756 section. */
23757
23758 /* Iteratively hash operands of LOC opcode into HSTATE. */
23759
23760 static void
23761 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23762 {
23763 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23764 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23765
23766 switch (loc->dw_loc_opc)
23767 {
23768 case DW_OP_const4u:
23769 case DW_OP_const8u:
23770 if (loc->dtprel)
23771 goto hash_addr;
23772 /* FALLTHRU */
23773 case DW_OP_const1u:
23774 case DW_OP_const1s:
23775 case DW_OP_const2u:
23776 case DW_OP_const2s:
23777 case DW_OP_const4s:
23778 case DW_OP_const8s:
23779 case DW_OP_constu:
23780 case DW_OP_consts:
23781 case DW_OP_pick:
23782 case DW_OP_plus_uconst:
23783 case DW_OP_breg0:
23784 case DW_OP_breg1:
23785 case DW_OP_breg2:
23786 case DW_OP_breg3:
23787 case DW_OP_breg4:
23788 case DW_OP_breg5:
23789 case DW_OP_breg6:
23790 case DW_OP_breg7:
23791 case DW_OP_breg8:
23792 case DW_OP_breg9:
23793 case DW_OP_breg10:
23794 case DW_OP_breg11:
23795 case DW_OP_breg12:
23796 case DW_OP_breg13:
23797 case DW_OP_breg14:
23798 case DW_OP_breg15:
23799 case DW_OP_breg16:
23800 case DW_OP_breg17:
23801 case DW_OP_breg18:
23802 case DW_OP_breg19:
23803 case DW_OP_breg20:
23804 case DW_OP_breg21:
23805 case DW_OP_breg22:
23806 case DW_OP_breg23:
23807 case DW_OP_breg24:
23808 case DW_OP_breg25:
23809 case DW_OP_breg26:
23810 case DW_OP_breg27:
23811 case DW_OP_breg28:
23812 case DW_OP_breg29:
23813 case DW_OP_breg30:
23814 case DW_OP_breg31:
23815 case DW_OP_regx:
23816 case DW_OP_fbreg:
23817 case DW_OP_piece:
23818 case DW_OP_deref_size:
23819 case DW_OP_xderef_size:
23820 hstate.add_object (val1->v.val_int);
23821 break;
23822 case DW_OP_skip:
23823 case DW_OP_bra:
23824 {
23825 int offset;
23826
23827 gcc_assert (val1->val_class == dw_val_class_loc);
23828 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23829 hstate.add_object (offset);
23830 }
23831 break;
23832 case DW_OP_implicit_value:
23833 hstate.add_object (val1->v.val_unsigned);
23834 switch (val2->val_class)
23835 {
23836 case dw_val_class_const:
23837 hstate.add_object (val2->v.val_int);
23838 break;
23839 case dw_val_class_vec:
23840 {
23841 unsigned int elt_size = val2->v.val_vec.elt_size;
23842 unsigned int len = val2->v.val_vec.length;
23843
23844 hstate.add_int (elt_size);
23845 hstate.add_int (len);
23846 hstate.add (val2->v.val_vec.array, len * elt_size);
23847 }
23848 break;
23849 case dw_val_class_const_double:
23850 hstate.add_object (val2->v.val_double.low);
23851 hstate.add_object (val2->v.val_double.high);
23852 break;
23853 case dw_val_class_wide_int:
23854 hstate.add_object (*val2->v.val_wide);
23855 break;
23856 case dw_val_class_addr:
23857 inchash::add_rtx (val2->v.val_addr, hstate);
23858 break;
23859 default:
23860 gcc_unreachable ();
23861 }
23862 break;
23863 case DW_OP_bregx:
23864 case DW_OP_bit_piece:
23865 hstate.add_object (val1->v.val_int);
23866 hstate.add_object (val2->v.val_int);
23867 break;
23868 case DW_OP_addr:
23869 hash_addr:
23870 if (loc->dtprel)
23871 {
23872 unsigned char dtprel = 0xd1;
23873 hstate.add_object (dtprel);
23874 }
23875 inchash::add_rtx (val1->v.val_addr, hstate);
23876 break;
23877 case DW_OP_GNU_addr_index:
23878 case DW_OP_GNU_const_index:
23879 {
23880 if (loc->dtprel)
23881 {
23882 unsigned char dtprel = 0xd1;
23883 hstate.add_object (dtprel);
23884 }
23885 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23886 }
23887 break;
23888 case DW_OP_GNU_implicit_pointer:
23889 hstate.add_int (val2->v.val_int);
23890 break;
23891 case DW_OP_GNU_entry_value:
23892 hstate.add_object (val1->v.val_loc);
23893 break;
23894 case DW_OP_GNU_regval_type:
23895 case DW_OP_GNU_deref_type:
23896 {
23897 unsigned int byte_size
23898 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23899 unsigned int encoding
23900 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23901 hstate.add_object (val1->v.val_int);
23902 hstate.add_object (byte_size);
23903 hstate.add_object (encoding);
23904 }
23905 break;
23906 case DW_OP_GNU_convert:
23907 case DW_OP_GNU_reinterpret:
23908 if (val1->val_class == dw_val_class_unsigned_const)
23909 {
23910 hstate.add_object (val1->v.val_unsigned);
23911 break;
23912 }
23913 /* FALLTHRU */
23914 case DW_OP_GNU_const_type:
23915 {
23916 unsigned int byte_size
23917 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23918 unsigned int encoding
23919 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23920 hstate.add_object (byte_size);
23921 hstate.add_object (encoding);
23922 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23923 break;
23924 hstate.add_object (val2->val_class);
23925 switch (val2->val_class)
23926 {
23927 case dw_val_class_const:
23928 hstate.add_object (val2->v.val_int);
23929 break;
23930 case dw_val_class_vec:
23931 {
23932 unsigned int elt_size = val2->v.val_vec.elt_size;
23933 unsigned int len = val2->v.val_vec.length;
23934
23935 hstate.add_object (elt_size);
23936 hstate.add_object (len);
23937 hstate.add (val2->v.val_vec.array, len * elt_size);
23938 }
23939 break;
23940 case dw_val_class_const_double:
23941 hstate.add_object (val2->v.val_double.low);
23942 hstate.add_object (val2->v.val_double.high);
23943 break;
23944 case dw_val_class_wide_int:
23945 hstate.add_object (*val2->v.val_wide);
23946 break;
23947 default:
23948 gcc_unreachable ();
23949 }
23950 }
23951 break;
23952
23953 default:
23954 /* Other codes have no operands. */
23955 break;
23956 }
23957 }
23958
23959 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23960
23961 static inline void
23962 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23963 {
23964 dw_loc_descr_ref l;
23965 bool sizes_computed = false;
23966 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23967 size_of_locs (loc);
23968
23969 for (l = loc; l != NULL; l = l->dw_loc_next)
23970 {
23971 enum dwarf_location_atom opc = l->dw_loc_opc;
23972 hstate.add_object (opc);
23973 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23974 {
23975 size_of_locs (loc);
23976 sizes_computed = true;
23977 }
23978 hash_loc_operands (l, hstate);
23979 }
23980 }
23981
23982 /* Compute hash of the whole location list LIST_HEAD. */
23983
23984 static inline void
23985 hash_loc_list (dw_loc_list_ref list_head)
23986 {
23987 dw_loc_list_ref curr = list_head;
23988 inchash::hash hstate;
23989
23990 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23991 {
23992 hstate.add (curr->begin, strlen (curr->begin) + 1);
23993 hstate.add (curr->end, strlen (curr->end) + 1);
23994 if (curr->section)
23995 hstate.add (curr->section, strlen (curr->section) + 1);
23996 hash_locs (curr->expr, hstate);
23997 }
23998 list_head->hash = hstate.end ();
23999 }
24000
24001 /* Return true if X and Y opcodes have the same operands. */
24002
24003 static inline bool
24004 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24005 {
24006 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24007 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24008 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24009 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24010
24011 switch (x->dw_loc_opc)
24012 {
24013 case DW_OP_const4u:
24014 case DW_OP_const8u:
24015 if (x->dtprel)
24016 goto hash_addr;
24017 /* FALLTHRU */
24018 case DW_OP_const1u:
24019 case DW_OP_const1s:
24020 case DW_OP_const2u:
24021 case DW_OP_const2s:
24022 case DW_OP_const4s:
24023 case DW_OP_const8s:
24024 case DW_OP_constu:
24025 case DW_OP_consts:
24026 case DW_OP_pick:
24027 case DW_OP_plus_uconst:
24028 case DW_OP_breg0:
24029 case DW_OP_breg1:
24030 case DW_OP_breg2:
24031 case DW_OP_breg3:
24032 case DW_OP_breg4:
24033 case DW_OP_breg5:
24034 case DW_OP_breg6:
24035 case DW_OP_breg7:
24036 case DW_OP_breg8:
24037 case DW_OP_breg9:
24038 case DW_OP_breg10:
24039 case DW_OP_breg11:
24040 case DW_OP_breg12:
24041 case DW_OP_breg13:
24042 case DW_OP_breg14:
24043 case DW_OP_breg15:
24044 case DW_OP_breg16:
24045 case DW_OP_breg17:
24046 case DW_OP_breg18:
24047 case DW_OP_breg19:
24048 case DW_OP_breg20:
24049 case DW_OP_breg21:
24050 case DW_OP_breg22:
24051 case DW_OP_breg23:
24052 case DW_OP_breg24:
24053 case DW_OP_breg25:
24054 case DW_OP_breg26:
24055 case DW_OP_breg27:
24056 case DW_OP_breg28:
24057 case DW_OP_breg29:
24058 case DW_OP_breg30:
24059 case DW_OP_breg31:
24060 case DW_OP_regx:
24061 case DW_OP_fbreg:
24062 case DW_OP_piece:
24063 case DW_OP_deref_size:
24064 case DW_OP_xderef_size:
24065 return valx1->v.val_int == valy1->v.val_int;
24066 case DW_OP_skip:
24067 case DW_OP_bra:
24068 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24069 can cause irrelevant differences in dw_loc_addr. */
24070 gcc_assert (valx1->val_class == dw_val_class_loc
24071 && valy1->val_class == dw_val_class_loc
24072 && (dwarf_split_debug_info
24073 || x->dw_loc_addr == y->dw_loc_addr));
24074 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24075 case DW_OP_implicit_value:
24076 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24077 || valx2->val_class != valy2->val_class)
24078 return false;
24079 switch (valx2->val_class)
24080 {
24081 case dw_val_class_const:
24082 return valx2->v.val_int == valy2->v.val_int;
24083 case dw_val_class_vec:
24084 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24085 && valx2->v.val_vec.length == valy2->v.val_vec.length
24086 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24087 valx2->v.val_vec.elt_size
24088 * valx2->v.val_vec.length) == 0;
24089 case dw_val_class_const_double:
24090 return valx2->v.val_double.low == valy2->v.val_double.low
24091 && valx2->v.val_double.high == valy2->v.val_double.high;
24092 case dw_val_class_wide_int:
24093 return *valx2->v.val_wide == *valy2->v.val_wide;
24094 case dw_val_class_addr:
24095 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24096 default:
24097 gcc_unreachable ();
24098 }
24099 case DW_OP_bregx:
24100 case DW_OP_bit_piece:
24101 return valx1->v.val_int == valy1->v.val_int
24102 && valx2->v.val_int == valy2->v.val_int;
24103 case DW_OP_addr:
24104 hash_addr:
24105 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24106 case DW_OP_GNU_addr_index:
24107 case DW_OP_GNU_const_index:
24108 {
24109 rtx ax1 = valx1->val_entry->addr.rtl;
24110 rtx ay1 = valy1->val_entry->addr.rtl;
24111 return rtx_equal_p (ax1, ay1);
24112 }
24113 case DW_OP_GNU_implicit_pointer:
24114 return valx1->val_class == dw_val_class_die_ref
24115 && valx1->val_class == valy1->val_class
24116 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24117 && valx2->v.val_int == valy2->v.val_int;
24118 case DW_OP_GNU_entry_value:
24119 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24120 case DW_OP_GNU_const_type:
24121 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24122 || valx2->val_class != valy2->val_class)
24123 return false;
24124 switch (valx2->val_class)
24125 {
24126 case dw_val_class_const:
24127 return valx2->v.val_int == valy2->v.val_int;
24128 case dw_val_class_vec:
24129 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24130 && valx2->v.val_vec.length == valy2->v.val_vec.length
24131 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24132 valx2->v.val_vec.elt_size
24133 * valx2->v.val_vec.length) == 0;
24134 case dw_val_class_const_double:
24135 return valx2->v.val_double.low == valy2->v.val_double.low
24136 && valx2->v.val_double.high == valy2->v.val_double.high;
24137 case dw_val_class_wide_int:
24138 return *valx2->v.val_wide == *valy2->v.val_wide;
24139 default:
24140 gcc_unreachable ();
24141 }
24142 case DW_OP_GNU_regval_type:
24143 case DW_OP_GNU_deref_type:
24144 return valx1->v.val_int == valy1->v.val_int
24145 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24146 case DW_OP_GNU_convert:
24147 case DW_OP_GNU_reinterpret:
24148 if (valx1->val_class != valy1->val_class)
24149 return false;
24150 if (valx1->val_class == dw_val_class_unsigned_const)
24151 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24152 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24153 case DW_OP_GNU_parameter_ref:
24154 return valx1->val_class == dw_val_class_die_ref
24155 && valx1->val_class == valy1->val_class
24156 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24157 default:
24158 /* Other codes have no operands. */
24159 return true;
24160 }
24161 }
24162
24163 /* Return true if DWARF location expressions X and Y are the same. */
24164
24165 static inline bool
24166 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24167 {
24168 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24169 if (x->dw_loc_opc != y->dw_loc_opc
24170 || x->dtprel != y->dtprel
24171 || !compare_loc_operands (x, y))
24172 break;
24173 return x == NULL && y == NULL;
24174 }
24175
24176 /* Hashtable helpers. */
24177
24178 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24179 {
24180 typedef dw_loc_list_struct value_type;
24181 typedef dw_loc_list_struct compare_type;
24182 static inline hashval_t hash (const value_type *);
24183 static inline bool equal (const value_type *, const compare_type *);
24184 };
24185
24186 /* Return precomputed hash of location list X. */
24187
24188 inline hashval_t
24189 loc_list_hasher::hash (const value_type *x)
24190 {
24191 return x->hash;
24192 }
24193
24194 /* Return true if location lists A and B are the same. */
24195
24196 inline bool
24197 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24198 {
24199 if (a == b)
24200 return 1;
24201 if (a->hash != b->hash)
24202 return 0;
24203 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24204 if (strcmp (a->begin, b->begin) != 0
24205 || strcmp (a->end, b->end) != 0
24206 || (a->section == NULL) != (b->section == NULL)
24207 || (a->section && strcmp (a->section, b->section) != 0)
24208 || !compare_locs (a->expr, b->expr))
24209 break;
24210 return a == NULL && b == NULL;
24211 }
24212
24213 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24214
24215
24216 /* Recursively optimize location lists referenced from DIE
24217 children and share them whenever possible. */
24218
24219 static void
24220 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24221 {
24222 dw_die_ref c;
24223 dw_attr_ref a;
24224 unsigned ix;
24225 dw_loc_list_struct **slot;
24226
24227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24228 if (AT_class (a) == dw_val_class_loc_list)
24229 {
24230 dw_loc_list_ref list = AT_loc_list (a);
24231 /* TODO: perform some optimizations here, before hashing
24232 it and storing into the hash table. */
24233 hash_loc_list (list);
24234 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24235 if (*slot == NULL)
24236 *slot = list;
24237 else
24238 a->dw_attr_val.v.val_loc_list = *slot;
24239 }
24240
24241 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24242 }
24243
24244
24245 /* Recursively assign each location list a unique index into the debug_addr
24246 section. */
24247
24248 static void
24249 index_location_lists (dw_die_ref die)
24250 {
24251 dw_die_ref c;
24252 dw_attr_ref a;
24253 unsigned ix;
24254
24255 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24256 if (AT_class (a) == dw_val_class_loc_list)
24257 {
24258 dw_loc_list_ref list = AT_loc_list (a);
24259 dw_loc_list_ref curr;
24260 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24261 {
24262 /* Don't index an entry that has already been indexed
24263 or won't be output. */
24264 if (curr->begin_entry != NULL
24265 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24266 continue;
24267
24268 curr->begin_entry
24269 = add_addr_table_entry (xstrdup (curr->begin),
24270 ate_kind_label);
24271 }
24272 }
24273
24274 FOR_EACH_CHILD (die, c, index_location_lists (c));
24275 }
24276
24277 /* Optimize location lists referenced from DIE
24278 children and share them whenever possible. */
24279
24280 static void
24281 optimize_location_lists (dw_die_ref die)
24282 {
24283 loc_list_hash_type htab (500);
24284 optimize_location_lists_1 (die, &htab);
24285 }
24286 \f
24287 /* Output stuff that dwarf requires at the end of every file,
24288 and generate the DWARF-2 debugging info. */
24289
24290 static void
24291 dwarf2out_finish (const char *filename)
24292 {
24293 limbo_die_node *node, *next_node;
24294 comdat_type_node *ctnode;
24295 unsigned int i;
24296 dw_die_ref main_comp_unit_die;
24297
24298 /* PCH might result in DW_AT_producer string being restored from the
24299 header compilation, so always fill it with empty string initially
24300 and overwrite only here. */
24301 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24302 producer_string = gen_producer_string ();
24303 producer->dw_attr_val.v.val_str->refcount--;
24304 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24305
24306 gen_scheduled_generic_parms_dies ();
24307 gen_remaining_tmpl_value_param_die_attribute ();
24308
24309 /* Add the name for the main input file now. We delayed this from
24310 dwarf2out_init to avoid complications with PCH. */
24311 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24312 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24313 add_comp_dir_attribute (comp_unit_die ());
24314 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24315 {
24316 bool p = false;
24317 file_table->traverse<bool *, file_table_relative_p> (&p);
24318 if (p)
24319 add_comp_dir_attribute (comp_unit_die ());
24320 }
24321
24322 if (deferred_locations_list)
24323 for (i = 0; i < deferred_locations_list->length (); i++)
24324 {
24325 add_location_or_const_value_attribute (
24326 (*deferred_locations_list)[i].die,
24327 (*deferred_locations_list)[i].variable,
24328 false,
24329 DW_AT_location);
24330 }
24331
24332 /* Traverse the limbo die list, and add parent/child links. The only
24333 dies without parents that should be here are concrete instances of
24334 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24335 For concrete instances, we can get the parent die from the abstract
24336 instance. */
24337 for (node = limbo_die_list; node; node = next_node)
24338 {
24339 dw_die_ref die = node->die;
24340 next_node = node->next;
24341
24342 if (die->die_parent == NULL)
24343 {
24344 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24345
24346 if (origin && origin->die_parent)
24347 add_child_die (origin->die_parent, die);
24348 else if (is_cu_die (die))
24349 ;
24350 else if (seen_error ())
24351 /* It's OK to be confused by errors in the input. */
24352 add_child_die (comp_unit_die (), die);
24353 else
24354 {
24355 /* In certain situations, the lexical block containing a
24356 nested function can be optimized away, which results
24357 in the nested function die being orphaned. Likewise
24358 with the return type of that nested function. Force
24359 this to be a child of the containing function.
24360
24361 It may happen that even the containing function got fully
24362 inlined and optimized out. In that case we are lost and
24363 assign the empty child. This should not be big issue as
24364 the function is likely unreachable too. */
24365 gcc_assert (node->created_for);
24366
24367 if (DECL_P (node->created_for))
24368 origin = get_context_die (DECL_CONTEXT (node->created_for));
24369 else if (TYPE_P (node->created_for))
24370 origin = scope_die_for (node->created_for, comp_unit_die ());
24371 else
24372 origin = comp_unit_die ();
24373
24374 add_child_die (origin, die);
24375 }
24376 }
24377 }
24378
24379 limbo_die_list = NULL;
24380
24381 #if ENABLE_ASSERT_CHECKING
24382 {
24383 dw_die_ref die = comp_unit_die (), c;
24384 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24385 }
24386 #endif
24387 resolve_addr (comp_unit_die ());
24388 move_marked_base_types ();
24389
24390 for (node = deferred_asm_name; node; node = node->next)
24391 {
24392 tree decl = node->created_for;
24393 /* When generating LTO bytecode we can not generate new assembler
24394 names at this point and all important decls got theirs via
24395 free-lang-data. */
24396 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24397 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24398 {
24399 add_linkage_attr (node->die, decl);
24400 move_linkage_attr (node->die);
24401 }
24402 }
24403
24404 deferred_asm_name = NULL;
24405
24406 /* Walk through the list of incomplete types again, trying once more to
24407 emit full debugging info for them. */
24408 retry_incomplete_types ();
24409
24410 if (flag_eliminate_unused_debug_types)
24411 prune_unused_types ();
24412
24413 /* Generate separate COMDAT sections for type DIEs. */
24414 if (use_debug_types)
24415 {
24416 break_out_comdat_types (comp_unit_die ());
24417
24418 /* Each new type_unit DIE was added to the limbo die list when created.
24419 Since these have all been added to comdat_type_list, clear the
24420 limbo die list. */
24421 limbo_die_list = NULL;
24422
24423 /* For each new comdat type unit, copy declarations for incomplete
24424 types to make the new unit self-contained (i.e., no direct
24425 references to the main compile unit). */
24426 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24427 copy_decls_for_unworthy_types (ctnode->root_die);
24428 copy_decls_for_unworthy_types (comp_unit_die ());
24429
24430 /* In the process of copying declarations from one unit to another,
24431 we may have left some declarations behind that are no longer
24432 referenced. Prune them. */
24433 prune_unused_types ();
24434 }
24435
24436 /* Generate separate CUs for each of the include files we've seen.
24437 They will go into limbo_die_list. */
24438 if (flag_eliminate_dwarf2_dups)
24439 break_out_includes (comp_unit_die ());
24440
24441 /* Traverse the DIE's and add add sibling attributes to those DIE's
24442 that have children. */
24443 add_sibling_attributes (comp_unit_die ());
24444 for (node = limbo_die_list; node; node = node->next)
24445 add_sibling_attributes (node->die);
24446 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24447 add_sibling_attributes (ctnode->root_die);
24448
24449 /* When splitting DWARF info, we put some attributes in the
24450 skeleton compile_unit DIE that remains in the .o, while
24451 most attributes go in the DWO compile_unit_die. */
24452 if (dwarf_split_debug_info)
24453 main_comp_unit_die = gen_compile_unit_die (NULL);
24454 else
24455 main_comp_unit_die = comp_unit_die ();
24456
24457 /* Output a terminator label for the .text section. */
24458 switch_to_section (text_section);
24459 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24460 if (cold_text_section)
24461 {
24462 switch_to_section (cold_text_section);
24463 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24464 }
24465
24466 /* We can only use the low/high_pc attributes if all of the code was
24467 in .text. */
24468 if (!have_multiple_function_sections
24469 || (dwarf_version < 3 && dwarf_strict))
24470 {
24471 /* Don't add if the CU has no associated code. */
24472 if (text_section_used)
24473 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24474 text_end_label, true);
24475 }
24476 else
24477 {
24478 unsigned fde_idx;
24479 dw_fde_ref fde;
24480 bool range_list_added = false;
24481
24482 if (text_section_used)
24483 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24484 text_end_label, &range_list_added, true);
24485 if (cold_text_section_used)
24486 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24487 cold_end_label, &range_list_added, true);
24488
24489 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24490 {
24491 if (DECL_IGNORED_P (fde->decl))
24492 continue;
24493 if (!fde->in_std_section)
24494 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24495 fde->dw_fde_end, &range_list_added,
24496 true);
24497 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24498 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24499 fde->dw_fde_second_end, &range_list_added,
24500 true);
24501 }
24502
24503 if (range_list_added)
24504 {
24505 /* We need to give .debug_loc and .debug_ranges an appropriate
24506 "base address". Use zero so that these addresses become
24507 absolute. Historically, we've emitted the unexpected
24508 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24509 Emit both to give time for other tools to adapt. */
24510 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24511 if (! dwarf_strict && dwarf_version < 4)
24512 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24513
24514 add_ranges (NULL);
24515 }
24516 }
24517
24518 if (debug_info_level >= DINFO_LEVEL_TERSE)
24519 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24520 debug_line_section_label);
24521
24522 if (have_macinfo)
24523 add_AT_macptr (comp_unit_die (),
24524 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24525 macinfo_section_label);
24526
24527 if (dwarf_split_debug_info)
24528 {
24529 /* optimize_location_lists calculates the size of the lists,
24530 so index them first, and assign indices to the entries.
24531 Although optimize_location_lists will remove entries from
24532 the table, it only does so for duplicates, and therefore
24533 only reduces ref_counts to 1. */
24534 index_location_lists (comp_unit_die ());
24535
24536 if (addr_index_table != NULL)
24537 {
24538 unsigned int index = 0;
24539 addr_index_table
24540 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24541 (&index);
24542 }
24543 }
24544
24545 if (have_location_lists)
24546 optimize_location_lists (comp_unit_die ());
24547
24548 save_macinfo_strings ();
24549
24550 if (dwarf_split_debug_info)
24551 {
24552 unsigned int index = 0;
24553
24554 /* Add attributes common to skeleton compile_units and
24555 type_units. Because these attributes include strings, it
24556 must be done before freezing the string table. Top-level
24557 skeleton die attrs are added when the skeleton type unit is
24558 created, so ensure it is created by this point. */
24559 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24560 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24561 }
24562
24563 /* Output all of the compilation units. We put the main one last so that
24564 the offsets are available to output_pubnames. */
24565 for (node = limbo_die_list; node; node = node->next)
24566 output_comp_unit (node->die, 0);
24567
24568 hash_table<comdat_type_hasher> comdat_type_table (100);
24569 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24570 {
24571 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24572
24573 /* Don't output duplicate types. */
24574 if (*slot != HTAB_EMPTY_ENTRY)
24575 continue;
24576
24577 /* Add a pointer to the line table for the main compilation unit
24578 so that the debugger can make sense of DW_AT_decl_file
24579 attributes. */
24580 if (debug_info_level >= DINFO_LEVEL_TERSE)
24581 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24582 (!dwarf_split_debug_info
24583 ? debug_line_section_label
24584 : debug_skeleton_line_section_label));
24585
24586 output_comdat_type_unit (ctnode);
24587 *slot = ctnode;
24588 }
24589
24590 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24591 both the main_cu and all skeleton TUs. Making this call unconditional
24592 would end up either adding a second copy of the AT_pubnames attribute, or
24593 requiring a special case in add_top_level_skeleton_die_attrs. */
24594 if (!dwarf_split_debug_info)
24595 add_AT_pubnames (comp_unit_die ());
24596
24597 if (dwarf_split_debug_info)
24598 {
24599 int mark;
24600 unsigned char checksum[16];
24601 struct md5_ctx ctx;
24602
24603 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24604 md5_init_ctx (&ctx);
24605 mark = 0;
24606 die_checksum (comp_unit_die (), &ctx, &mark);
24607 unmark_all_dies (comp_unit_die ());
24608 md5_finish_ctx (&ctx, checksum);
24609
24610 /* Use the first 8 bytes of the checksum as the dwo_id,
24611 and add it to both comp-unit DIEs. */
24612 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24613 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24614
24615 /* Add the base offset of the ranges table to the skeleton
24616 comp-unit DIE. */
24617 if (ranges_table_in_use)
24618 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24619 ranges_section_label);
24620
24621 switch_to_section (debug_addr_section);
24622 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24623 output_addr_table ();
24624 }
24625
24626 /* Output the main compilation unit if non-empty or if .debug_macinfo
24627 or .debug_macro will be emitted. */
24628 output_comp_unit (comp_unit_die (), have_macinfo);
24629
24630 if (dwarf_split_debug_info && info_section_emitted)
24631 output_skeleton_debug_sections (main_comp_unit_die);
24632
24633 /* Output the abbreviation table. */
24634 if (abbrev_die_table_in_use != 1)
24635 {
24636 switch_to_section (debug_abbrev_section);
24637 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24638 output_abbrev_section ();
24639 }
24640
24641 /* Output location list section if necessary. */
24642 if (have_location_lists)
24643 {
24644 /* Output the location lists info. */
24645 switch_to_section (debug_loc_section);
24646 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24647 output_location_lists (comp_unit_die ());
24648 }
24649
24650 output_pubtables ();
24651
24652 /* Output the address range information if a CU (.debug_info section)
24653 was emitted. We output an empty table even if we had no functions
24654 to put in it. This because the consumer has no way to tell the
24655 difference between an empty table that we omitted and failure to
24656 generate a table that would have contained data. */
24657 if (info_section_emitted)
24658 {
24659 unsigned long aranges_length = size_of_aranges ();
24660
24661 switch_to_section (debug_aranges_section);
24662 output_aranges (aranges_length);
24663 }
24664
24665 /* Output ranges section if necessary. */
24666 if (ranges_table_in_use)
24667 {
24668 switch_to_section (debug_ranges_section);
24669 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24670 output_ranges ();
24671 }
24672
24673 /* Have to end the macro section. */
24674 if (have_macinfo)
24675 {
24676 switch_to_section (debug_macinfo_section);
24677 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24678 output_macinfo ();
24679 dw2_asm_output_data (1, 0, "End compilation unit");
24680 }
24681
24682 /* Output the source line correspondence table. We must do this
24683 even if there is no line information. Otherwise, on an empty
24684 translation unit, we will generate a present, but empty,
24685 .debug_info section. IRIX 6.5 `nm' will then complain when
24686 examining the file. This is done late so that any filenames
24687 used by the debug_info section are marked as 'used'. */
24688 switch_to_section (debug_line_section);
24689 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24690 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24691 output_line_info (false);
24692
24693 if (dwarf_split_debug_info && info_section_emitted)
24694 {
24695 switch_to_section (debug_skeleton_line_section);
24696 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24697 output_line_info (true);
24698 }
24699
24700 /* If we emitted any indirect strings, output the string table too. */
24701 if (debug_str_hash || skeleton_debug_str_hash)
24702 output_indirect_strings ();
24703 }
24704
24705 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24706 within the same process. For use by toplev::finalize. */
24707
24708 void
24709 dwarf2out_c_finalize (void)
24710 {
24711 last_var_location_insn = NULL;
24712 cached_next_real_insn = NULL;
24713 used_rtx_array = NULL;
24714 incomplete_types = NULL;
24715 decl_scope_table = NULL;
24716 debug_info_section = NULL;
24717 debug_skeleton_info_section = NULL;
24718 debug_abbrev_section = NULL;
24719 debug_skeleton_abbrev_section = NULL;
24720 debug_aranges_section = NULL;
24721 debug_addr_section = NULL;
24722 debug_macinfo_section = NULL;
24723 debug_line_section = NULL;
24724 debug_skeleton_line_section = NULL;
24725 debug_loc_section = NULL;
24726 debug_pubnames_section = NULL;
24727 debug_pubtypes_section = NULL;
24728 debug_str_section = NULL;
24729 debug_str_dwo_section = NULL;
24730 debug_str_offsets_section = NULL;
24731 debug_ranges_section = NULL;
24732 debug_frame_section = NULL;
24733 fde_vec = NULL;
24734 debug_str_hash = NULL;
24735 skeleton_debug_str_hash = NULL;
24736 dw2_string_counter = 0;
24737 have_multiple_function_sections = false;
24738 text_section_used = false;
24739 cold_text_section_used = false;
24740 cold_text_section = NULL;
24741 current_unit_personality = NULL;
24742
24743 deferred_locations_list = NULL;
24744
24745 next_die_offset = 0;
24746 single_comp_unit_die = NULL;
24747 comdat_type_list = NULL;
24748 limbo_die_list = NULL;
24749 deferred_asm_name = NULL;
24750 file_table = NULL;
24751 decl_die_table = NULL;
24752 common_block_die_table = NULL;
24753 decl_loc_table = NULL;
24754 call_arg_locations = NULL;
24755 call_arg_loc_last = NULL;
24756 call_site_count = -1;
24757 tail_call_site_count = -1;
24758 //block_map = NULL;
24759 cached_dw_loc_list_table = NULL;
24760 abbrev_die_table = NULL;
24761 abbrev_die_table_allocated = 0;
24762 abbrev_die_table_in_use = 0;
24763 line_info_label_num = 0;
24764 cur_line_info_table = NULL;
24765 text_section_line_info = NULL;
24766 cold_text_section_line_info = NULL;
24767 separate_line_info = NULL;
24768 info_section_emitted = false;
24769 pubname_table = NULL;
24770 pubtype_table = NULL;
24771 macinfo_table = NULL;
24772 ranges_table = NULL;
24773 ranges_table_allocated = 0;
24774 ranges_table_in_use = 0;
24775 ranges_by_label = 0;
24776 ranges_by_label_allocated = 0;
24777 ranges_by_label_in_use = 0;
24778 have_location_lists = false;
24779 loclabel_num = 0;
24780 poc_label_num = 0;
24781 last_emitted_file = NULL;
24782 label_num = 0;
24783 file_table_last_lookup = NULL;
24784 tmpl_value_parm_die_table = NULL;
24785 generic_type_instances = NULL;
24786 frame_pointer_fb_offset = 0;
24787 frame_pointer_fb_offset_valid = false;
24788 base_types.release ();
24789 XDELETEVEC (producer_string);
24790 producer_string = NULL;
24791 }
24792
24793 #include "gt-dwarf2out.h"